Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions lib/firefly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ import {
FireFlyVerifierResolveResponse,
FireFlyTokenApprovalRequest,
FireFlyNamespaceResponse,
FireFlyUpdateIdentityRequest,
FireFlyReplaceOptions,
FireFlyUpdateOptions,
FireFlyDeleteOptions,
} from './interfaces';
import { FireFlyWebSocket, FireFlyWebSocketCallback } from './websocket';
import HttpBase, { mapConfig } from './http';
Expand All @@ -93,10 +97,10 @@ export default class FireFly extends HttpBase {
}

getIdentity(
nameOrId: string,
id: string,
options?: FireFlyGetOptions,
): Promise<FireFlyIdentityResponse | undefined> {
return this.getOne<FireFlyIdentityResponse>(`/identities/${nameOrId}`, options);
return this.getOne<FireFlyIdentityResponse>(`/identities/${id}`, options);
}

createIdentity(
Expand All @@ -106,20 +110,42 @@ export default class FireFly extends HttpBase {
return this.createOne<FireFlyIdentityResponse>(`/identities`, identity, options);
}

updateIdentity(
id: string,
update: FireFlyUpdateIdentityRequest,
options?: FireFlyUpdateOptions,
): Promise<FireFlyIdentityResponse> {
return this.updateOne<FireFlyIdentityResponse>(`/identities/${id}`, update, options);
}

getOrganizations(
filter?: FireFlyOrganizationFilter,
options?: FireFlyGetOptions,
): Promise<FireFlyOrganizationResponse[]> {
return this.getMany<FireFlyOrganizationResponse[]>('/network/organizations', filter, options);
}

getOrganization(
nameOrId: string,
options?: FireFlyGetOptions,
): Promise<FireFlyIdentityResponse | undefined> {
return this.getOne<FireFlyIdentityResponse>(`/network/organizations/${nameOrId}`, options);
}

getNodes(
filter?: FireFlyNodeFilter,
options?: FireFlyGetOptions,
): Promise<FireFlyNodeResponse[]> {
return this.getMany<FireFlyNodeResponse[]>('/network/nodes', filter, options);
}

getNode(
nameOrId: string,
options?: FireFlyGetOptions,
): Promise<FireFlyIdentityResponse | undefined> {
return this.getOne<FireFlyIdentityResponse>(`/network/nodes/${nameOrId}`, options);
}

getVerifiers(
namespace?: string,
filter?: FireFlyVerifierFilter,
Expand Down Expand Up @@ -175,12 +201,15 @@ export default class FireFly extends HttpBase {
return this.getMany<FireFlySubscriptionResponse[]>('/subscriptions', filter, options);
}

replaceSubscription(sub: FireFlySubscriptionRequest): Promise<FireFlySubscriptionResponse> {
return this.replaceOne<FireFlySubscriptionResponse>('/subscriptions', sub);
replaceSubscription(
sub: FireFlySubscriptionRequest,
options?: FireFlyReplaceOptions,
): Promise<FireFlySubscriptionResponse> {
return this.replaceOne<FireFlySubscriptionResponse>('/subscriptions', sub, options);
}

async deleteSubscription(subId: string) {
await this.deleteOne(`/subscriptions/${subId}`);
async deleteSubscription(subId: string, options?: FireFlyDeleteOptions) {
await this.deleteOne(`/subscriptions/${subId}`, options);
}

getData(id: string, options?: FireFlyGetOptions): Promise<FireFlyDataResponse | undefined> {
Expand Down Expand Up @@ -248,6 +277,10 @@ export default class FireFly extends HttpBase {
return this.createOne<FireFlyDataResponse>(`/data/${id}/blob/publish`, {}, options);
}

async deleteData(id: string, options?: FireFlyDeleteOptions) {
await this.deleteOne(`/data/${id}`, options);
}

getBatches(
filter?: FireFlyBatchFilter,
options?: FireFlyGetOptions,
Expand Down
40 changes: 30 additions & 10 deletions lib/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,38 @@ import {
FireFlyCreateOptions,
FireFlyGetOptions,
FireFlyError,
FireFlyReplaceOptions,
FireFlyUpdateOptions,
FireFlyDeleteOptions,
} from './interfaces';

function isSuccess(status: number) {
return status >= 200 && status < 300;
}

export function mapConfig(
options: FireFlyGetOptions | FireFlyCreateOptions | undefined,
options:
| FireFlyGetOptions
| FireFlyUpdateOptions
| FireFlyReplaceOptions
| FireFlyCreateOptions
| FireFlyDeleteOptions
| undefined,
params?: any,
): AxiosRequestConfig {
return {
const config: AxiosRequestConfig = {
...options?.requestConfig,
params: {
...params,
confirm: options?.confirm,
},
params,
};
if (options !== undefined) {
if ('confirm' in options) {
config.params = {
...config.params,
confirm: options.confirm,
};
}
}
return config;
}

export default class HttpBase {
Expand Down Expand Up @@ -92,13 +107,18 @@ export default class HttpBase {
return response.data;
}

protected async replaceOne<T>(url: string, data: any) {
const response = await this.wrapError(this.http.put<T>(url, data));
protected async updateOne<T>(url: string, data: any, options?: FireFlyUpdateOptions) {
const response = await this.wrapError(this.http.patch<T>(url, data, mapConfig(options)));
return response.data;
}

protected async replaceOne<T>(url: string, data: any, options?: FireFlyReplaceOptions) {
const response = await this.wrapError(this.http.put<T>(url, data, mapConfig(options)));
return response.data;
}

protected async deleteOne<T>(url: string) {
await this.wrapError(this.http.delete<T>(url));
protected async deleteOne<T>(url: string, options?: FireFlyDeleteOptions) {
await this.wrapError(this.http.delete<T>(url, mapConfig(options)));
}

onError(handler: (err: FireFlyError) => void) {
Expand Down
13 changes: 9 additions & 4 deletions lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ export class FireFlyError extends Error {
}
}

export interface FireFlyGetOptions {
confirm: undefined;
interface FireFlyBaseHttpOptions {
requestConfig?: AxiosRequestConfig;
}

export interface FireFlyCreateOptions {
export interface FireFlyGetOptions extends FireFlyBaseHttpOptions {}
export interface FireFlyUpdateOptions extends FireFlyBaseHttpOptions {}
export interface FireFlyReplaceOptions extends FireFlyBaseHttpOptions {}
export interface FireFlyDeleteOptions extends FireFlyBaseHttpOptions {}

export interface FireFlyCreateOptions extends FireFlyBaseHttpOptions {
confirm?: boolean;
requestConfig?: AxiosRequestConfig;
}

export interface FireFlyOptionsInput {
Expand Down Expand Up @@ -80,6 +83,8 @@ export type FireFlyVerifierFilter = operations['getVerifiers']['parameters']['qu

export type FireFlyIdentityRequest =
operations['postNewIdentity']['requestBody']['content']['application/json'];
export type FireFlyUpdateIdentityRequest =
operations['patchUpdateIdentity']['requestBody']['content']['application/json'];

export type FireFlyIdentityResponse = Required<
operations['getIdentityByID']['responses']['200']['content']['application/json']
Expand Down
Loading