From ae8cf19b6e89aa849353a1b63cdd7389e606d602 Mon Sep 17 00:00:00 2001 From: Andrew Richardson Date: Fri, 2 Feb 2024 11:31:26 -0500 Subject: [PATCH 1/3] Add getSubscription Signed-off-by: Andrew Richardson --- lib/firefly.ts | 11 +++++++++-- lib/http.ts | 9 +++++++++ lib/interfaces.ts | 10 +++++----- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/firefly.ts b/lib/firefly.ts index ea7b9fd..7ef213f 100644 --- a/lib/firefly.ts +++ b/lib/firefly.ts @@ -82,7 +82,7 @@ import { FireFlyDeployContractRequest, FireFlyDeployContractResponse, FireFlyWebSocketConnectCallback, - FireFlyGetOperationOptions, + FireFlyGetWithStatus, } from './interfaces'; import { FireFlyWebSocket, FireFlyWebSocketCallback } from './websocket'; import HttpBase, { mapConfig } from './http'; @@ -207,6 +207,13 @@ export default class FireFly extends HttpBase { return this.getMany('/subscriptions', filter, options); } + getSubscription( + id: string, + options?: FireFlyGetWithStatus, + ): Promise { + return this.getOne(`/subscriptions/${id}`, options); + } + replaceSubscription( sub: FireFlySubscriptionRequest, options?: FireFlyReplaceOptions, @@ -572,7 +579,7 @@ export default class FireFly extends HttpBase { getOperation( id: string, - options?: FireFlyGetOperationOptions, + options?: FireFlyGetWithStatus, ): Promise { const params = { fetchstatus: options?.fetchstatus }; return this.getOne(`/operations/${id}`, options, params); diff --git a/lib/http.ts b/lib/http.ts index ff0c5b7..289c1b5 100644 --- a/lib/http.ts +++ b/lib/http.ts @@ -9,6 +9,7 @@ import { FireFlyUpdateOptions, FireFlyDeleteOptions, FireFlyIdempotencyError, + FireFlyGetWithStatus, } from './interfaces'; function isSuccess(status: number) { @@ -18,6 +19,7 @@ function isSuccess(status: number) { export function mapConfig( options: | FireFlyGetOptions + | FireFlyGetWithStatus | FireFlyUpdateOptions | FireFlyReplaceOptions | FireFlyCreateOptions @@ -29,6 +31,7 @@ export function mapConfig( ...options?.requestConfig, params, }; + if (options !== undefined) { if ('confirm' in options) { config.params = { @@ -42,6 +45,12 @@ export function mapConfig( publish: options.publish, }; } + if ('fetchstatus' in options) { + config.params = { + ...config.params, + fetchstatus: options.fetchstatus, + }; + } } return config; } diff --git a/lib/interfaces.ts b/lib/interfaces.ts index 7fa9c70..b2162b9 100644 --- a/lib/interfaces.ts +++ b/lib/interfaces.ts @@ -37,6 +37,10 @@ export interface FireFlyCreateOptions extends FireFlyBaseHttpOptions { publish?: boolean; } +export interface FireFlyGetWithStatus extends FireFlyGetOptions { + fetchstatus?: string; +} + export interface FireFlyOptionsInput { host: string; namespace?: string; @@ -129,7 +133,7 @@ export type FireFlySubscriptionRequest = export type FireFlySubscriptionResponse = Required< operations['getSubscriptionByID']['responses']['200']['content']['application/json'] ->; +> & { status?: any }; export type FireFlyEventResponse = Required< operations['getEventByID']['responses']['200']['content']['application/json'] >; @@ -282,10 +286,6 @@ export type FireFlyTokenApprovalResponse = typeof approvals[0]; // Operations + Transactions -export interface FireFlyGetOperationOptions extends FireFlyGetOptions { - fetchstatus?: string; -} - export type FireFlyOperationFilter = operations['getOps']['parameters']['query']; export type FireFlyTransactionFilter = operations['getTxns']['parameters']['query']; From 8cad25e355d46e36b96bcc7453b304a0a769d4d3 Mon Sep 17 00:00:00 2001 From: Andrew Richardson Date: Fri, 2 Feb 2024 11:57:40 -0500 Subject: [PATCH 2/3] Allow passing URLSearchParams when searching for data While the strongly-typed interface is useful in most cases, it does not support some things, such as passing "id" multiple times in order to perform an "OR" query. Adding this to the "findData" API now since bulk fetching of data is a very common case - but we may want to consider adding it to all/most getters. Signed-off-by: Andrew Richardson --- lib/firefly.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/firefly.ts b/lib/firefly.ts index 7ef213f..d1c7a78 100644 --- a/lib/firefly.ts +++ b/lib/firefly.ts @@ -230,7 +230,7 @@ export default class FireFly extends HttpBase { } findData( - filter?: FireFlyDataFilter, + filter?: FireFlyDataFilter | URLSearchParams, options?: FireFlyGetOptions, ): Promise { return this.getMany(`/data`, filter, options); From a2d3d299b8fe89b087619c6e897758b8e6a2d175 Mon Sep 17 00:00:00 2001 From: Andrew Richardson Date: Fri, 2 Feb 2024 12:01:54 -0500 Subject: [PATCH 3/3] Add support for batched websocket delivery Signed-off-by: Andrew Richardson --- lib/interfaces.ts | 11 +++++++++++ lib/websocket.ts | 10 +++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/interfaces.ts b/lib/interfaces.ts index b2162b9..b8a0e4b 100644 --- a/lib/interfaces.ts +++ b/lib/interfaces.ts @@ -185,6 +185,17 @@ export interface FireFlyEventDelivery extends Omit }; } +export interface FireFlyEventBatchDelivery { + type: 'event_batch'; + id: string; + subscription: { + id: string; + name: string; + namespace: string; + }; + events: FireFlyEventDelivery[]; +} + // Datatypes export type FireFlyDatatypeFilter = operations['getDatatypes']['parameters']['query']; diff --git a/lib/websocket.ts b/lib/websocket.ts index 59377a5..f3d79b0 100644 --- a/lib/websocket.ts +++ b/lib/websocket.ts @@ -5,6 +5,7 @@ import { FireFlyEphemeralSubscription, FireFlyWebSocketOptions, FireFlyEventDelivery, + FireFlyEventBatchDelivery, } from './interfaces'; import Logger from './logger'; @@ -19,7 +20,10 @@ function buildEphemeralQueryParams(sub: FireFlyEphemeralSubscription) { } export interface FireFlyWebSocketCallback { - (socket: FireFlyWebSocket, data: FireFlyEventDelivery): void | Promise; + ( + socket: FireFlyWebSocket, + data: FireFlyEventDelivery | FireFlyEventBatchDelivery, + ): void | Promise; } export class FireFlyWebSocket { @@ -166,7 +170,7 @@ export class FireFlyWebSocket { } } - ack(event: FireFlyEventDelivery) { + ack(event: FireFlyEventDelivery | FireFlyEventBatchDelivery) { if (this.socket !== undefined && event.id !== undefined) { this.socket.send( JSON.stringify({ @@ -179,7 +183,7 @@ export class FireFlyWebSocket { } async close(wait?: boolean): Promise { - const closedPromise = new Promise(resolve => { + const closedPromise = new Promise((resolve) => { this.closed = resolve; }); this.clearPingTimers();