Skip to content

Commit e5250fc

Browse files
authored
Merge pull request #85 from kaleido-io/subscription
Add more helpers around subscriptions and batch delivery
2 parents 6a1a09a + a2d3d29 commit e5250fc

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

lib/firefly.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ import {
8282
FireFlyDeployContractRequest,
8383
FireFlyDeployContractResponse,
8484
FireFlyWebSocketConnectCallback,
85-
FireFlyGetOperationOptions,
85+
FireFlyGetWithStatus,
8686
} from './interfaces';
8787
import { FireFlyWebSocket, FireFlyWebSocketCallback } from './websocket';
8888
import HttpBase, { mapConfig } from './http';
@@ -207,6 +207,13 @@ export default class FireFly extends HttpBase {
207207
return this.getMany<FireFlySubscriptionResponse[]>('/subscriptions', filter, options);
208208
}
209209

210+
getSubscription(
211+
id: string,
212+
options?: FireFlyGetWithStatus,
213+
): Promise<FireFlySubscriptionResponse | undefined> {
214+
return this.getOne<FireFlySubscriptionResponse>(`/subscriptions/${id}`, options);
215+
}
216+
210217
replaceSubscription(
211218
sub: FireFlySubscriptionRequest,
212219
options?: FireFlyReplaceOptions,
@@ -223,7 +230,7 @@ export default class FireFly extends HttpBase {
223230
}
224231

225232
findData(
226-
filter?: FireFlyDataFilter,
233+
filter?: FireFlyDataFilter | URLSearchParams,
227234
options?: FireFlyGetOptions,
228235
): Promise<FireFlyDataResponse[]> {
229236
return this.getMany<FireFlyDataResponse[]>(`/data`, filter, options);
@@ -572,7 +579,7 @@ export default class FireFly extends HttpBase {
572579

573580
getOperation(
574581
id: string,
575-
options?: FireFlyGetOperationOptions,
582+
options?: FireFlyGetWithStatus,
576583
): Promise<FireFlyOperationResponse | undefined> {
577584
const params = { fetchstatus: options?.fetchstatus };
578585
return this.getOne<FireFlyOperationResponse>(`/operations/${id}`, options, params);

lib/http.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
FireFlyUpdateOptions,
1010
FireFlyDeleteOptions,
1111
FireFlyIdempotencyError,
12+
FireFlyGetWithStatus,
1213
} from './interfaces';
1314

1415
function isSuccess(status: number) {
@@ -18,6 +19,7 @@ function isSuccess(status: number) {
1819
export function mapConfig(
1920
options:
2021
| FireFlyGetOptions
22+
| FireFlyGetWithStatus
2123
| FireFlyUpdateOptions
2224
| FireFlyReplaceOptions
2325
| FireFlyCreateOptions
@@ -29,6 +31,7 @@ export function mapConfig(
2931
...options?.requestConfig,
3032
params,
3133
};
34+
3235
if (options !== undefined) {
3336
if ('confirm' in options) {
3437
config.params = {
@@ -42,6 +45,12 @@ export function mapConfig(
4245
publish: options.publish,
4346
};
4447
}
48+
if ('fetchstatus' in options) {
49+
config.params = {
50+
...config.params,
51+
fetchstatus: options.fetchstatus,
52+
};
53+
}
4554
}
4655
return config;
4756
}

lib/interfaces.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ export interface FireFlyCreateOptions extends FireFlyBaseHttpOptions {
3737
publish?: boolean;
3838
}
3939

40+
export interface FireFlyGetWithStatus extends FireFlyGetOptions {
41+
fetchstatus?: string;
42+
}
43+
4044
export interface FireFlyOptionsInput {
4145
host: string;
4246
namespace?: string;
@@ -129,7 +133,7 @@ export type FireFlySubscriptionRequest =
129133

130134
export type FireFlySubscriptionResponse = Required<
131135
operations['getSubscriptionByID']['responses']['200']['content']['application/json']
132-
>;
136+
> & { status?: any };
133137
export type FireFlyEventResponse = Required<
134138
operations['getEventByID']['responses']['200']['content']['application/json']
135139
>;
@@ -181,6 +185,17 @@ export interface FireFlyEventDelivery extends Omit<FireFlyEnrichedEvent, 'type'>
181185
};
182186
}
183187

188+
export interface FireFlyEventBatchDelivery {
189+
type: 'event_batch';
190+
id: string;
191+
subscription: {
192+
id: string;
193+
name: string;
194+
namespace: string;
195+
};
196+
events: FireFlyEventDelivery[];
197+
}
198+
184199
// Datatypes
185200

186201
export type FireFlyDatatypeFilter = operations['getDatatypes']['parameters']['query'];
@@ -282,10 +297,6 @@ export type FireFlyTokenApprovalResponse = typeof approvals[0];
282297

283298
// Operations + Transactions
284299

285-
export interface FireFlyGetOperationOptions extends FireFlyGetOptions {
286-
fetchstatus?: string;
287-
}
288-
289300
export type FireFlyOperationFilter = operations['getOps']['parameters']['query'];
290301
export type FireFlyTransactionFilter = operations['getTxns']['parameters']['query'];
291302

lib/websocket.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
FireFlyEphemeralSubscription,
66
FireFlyWebSocketOptions,
77
FireFlyEventDelivery,
8+
FireFlyEventBatchDelivery,
89
} from './interfaces';
910
import Logger from './logger';
1011

@@ -19,7 +20,10 @@ function buildEphemeralQueryParams(sub: FireFlyEphemeralSubscription) {
1920
}
2021

2122
export interface FireFlyWebSocketCallback {
22-
(socket: FireFlyWebSocket, data: FireFlyEventDelivery): void | Promise<void>;
23+
(
24+
socket: FireFlyWebSocket,
25+
data: FireFlyEventDelivery | FireFlyEventBatchDelivery,
26+
): void | Promise<void>;
2327
}
2428

2529
export class FireFlyWebSocket {
@@ -166,7 +170,7 @@ export class FireFlyWebSocket {
166170
}
167171
}
168172

169-
ack(event: FireFlyEventDelivery) {
173+
ack(event: FireFlyEventDelivery | FireFlyEventBatchDelivery) {
170174
if (this.socket !== undefined && event.id !== undefined) {
171175
this.socket.send(
172176
JSON.stringify({
@@ -179,7 +183,7 @@ export class FireFlyWebSocket {
179183
}
180184

181185
async close(wait?: boolean): Promise<void> {
182-
const closedPromise = new Promise<void>(resolve => {
186+
const closedPromise = new Promise<void>((resolve) => {
183187
this.closed = resolve;
184188
});
185189
this.clearPingTimers();

0 commit comments

Comments
 (0)