From 1b565dd67e488504204a43e7ad6c21ee4da44952 Mon Sep 17 00:00:00 2001 From: Michael Buntarman Date: Wed, 18 Dec 2024 18:40:37 +0700 Subject: [PATCH] feat: able to use custom procedure (#26) --- src/contracts-api/stream.ts | 31 +++++++++++++++++++++++ tests/integration/primitiveStream.test.ts | 14 ++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/contracts-api/stream.ts b/src/contracts-api/stream.ts index a3bebbc..bce83a8 100644 --- a/src/contracts-api/stream.ts +++ b/src/contracts-api/stream.ts @@ -515,4 +515,35 @@ export class Stream { ) .throw(); } + + /** + * A custom method that accepts the procedure name and the input of GetRecordInput + * Returns the result of the procedure in the same format as StreamRecord + * I.e. a custom procedure named "get_price" that returns a list of date_value and value + * can be called with customGetProcedure("get_price", { dateFrom: "2021-01-01", dateTo: "2021-01-31" }) + */ + public async customGetProcedure( + procedure: string, + input: GetRecordInput, + ): Promise { + const result = await this.call<{ date_value: string; value: string }[]>( + procedure, + [ + ActionInput.fromObject({ + $date_from: input.dateFrom, + $date_to: input.dateTo, + $frozen_at: input.frozenAt, + $base_date: input.baseDate, + }), + ], + ); + return result + .mapRight((result) => + result.map((row) => ({ + dateValue: row.date_value, + value: row.value, + })), + ) + .throw(); + } } diff --git a/tests/integration/primitiveStream.test.ts b/tests/integration/primitiveStream.test.ts index 1800c82..6cc5a36 100644 --- a/tests/integration/primitiveStream.test.ts +++ b/tests/integration/primitiveStream.test.ts @@ -57,6 +57,20 @@ describe.sequential( expect(records[0].value).toBe("1.000000000000000000"); expect(records[0].dateValue).toBe("2020-01-01"); + // Use Custom Procedure with the same name "get_record" + const customRecords = await primitiveStream.customGetProcedure( + "get_record", + { + dateFrom: "2020-01-01", + dateTo: "2021-01-01", + }, + ); + + // Verify record content from the custom procedure + expect(customRecords.length).toBe(1); + expect(customRecords[0].value).toBe("1.000000000000000000"); + expect(customRecords[0].dateValue).toBe("2020-01-01"); + // Query index const index = await primitiveStream.getIndex({ dateFrom: "2020-01-01",