@droz-js/sdk 0.3.10 → 0.3.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -23,6 +23,18 @@ DrozSdk.forTenant('dev');
23
23
  DrozSdk.withAuthorization('Basic', 'username', 'password');
24
24
  ```
25
25
 
26
+ ## Global Error Handling
27
+
28
+ The sdk will throw an error on every request that fails, but you can also catch the error globally by using the `on` method.
29
+
30
+ ```typescript
31
+ import { DrozSdk } from '@droz-js/sdk';
32
+
33
+ DrozSdk.on('error', (error) => {
34
+ console.log(error);
35
+ });
36
+ ```
37
+
26
38
  ## Websocket
27
39
 
28
40
  For websockets we use `graphql-ws` protocol with `AsyncIterator` to handle the subscriptions. The sdk will automatically
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@droz-js/sdk",
3
3
  "description": "Droz SDK",
4
- "version": "0.3.10",
4
+ "version": "0.3.11",
5
5
  "private": false,
6
6
  "exports": {
7
7
  ".": "./src/index.js",
@@ -18,6 +18,7 @@ export declare class DrozSdk {
18
18
  private static defaultTenant?;
19
19
  private static defaultAuthorizationProvider?;
20
20
  private static readonly tenantConfigs;
21
+ private static readonly listeners;
21
22
  static forTenant(tenant: string): typeof DrozSdk;
22
23
  static withAuthorization(type?: string, ...values: string[]): void;
23
24
  static withAuthorizationProvider(provider: () => Promise<string>): void;
@@ -25,5 +26,10 @@ export declare class DrozSdk {
25
26
  static getTenantConfig(customTenant?: string): Promise<TenantConfig>;
26
27
  private static getOrDiscoverTenantConfig;
27
28
  private static discoverTenantConfig;
29
+ static on<T>(event: string, listener: (e: T) => void): void;
30
+ static off<T>(event: string, listener: (e: T) => void): void;
31
+ static emit(event: string, data: any): void;
32
+ private static addEventListener;
33
+ private static removeEventListener;
28
34
  }
29
35
  export {};
@@ -32,6 +32,7 @@ class DrozSdk {
32
32
  static defaultTenant;
33
33
  static defaultAuthorizationProvider;
34
34
  static tenantConfigs = new Map();
35
+ static listeners = new Map();
35
36
  static forTenant(tenant) {
36
37
  this.defaultTenant = tenant;
37
38
  this.tenantConfigs.delete(tenant);
@@ -81,5 +82,36 @@ class DrozSdk {
81
82
  this.tenantConfigs.set(tenant, promise());
82
83
  return this.tenantConfigs.get(tenant);
83
84
  }
85
+ static on(event, listener) {
86
+ this.addEventListener(event, listener);
87
+ }
88
+ static off(event, listener) {
89
+ this.removeEventListener(event, listener);
90
+ }
91
+ static emit(event, data) {
92
+ if (this.listeners.has(event)) {
93
+ const listeners = this.listeners.get(event);
94
+ listeners.forEach(listener => listener(data));
95
+ }
96
+ }
97
+ static addEventListener(event, listener) {
98
+ if (!this.listeners.has(event)) {
99
+ this.listeners.set(event, []);
100
+ }
101
+ const listeners = this.listeners.get(event);
102
+ const index = listeners.indexOf(listener);
103
+ if (index == -1) {
104
+ listeners.push(listener);
105
+ }
106
+ }
107
+ static removeEventListener(event, listener) {
108
+ if (this.listeners.has(event)) {
109
+ const listeners = this.listeners.get(event);
110
+ const index = listeners.indexOf(listener);
111
+ if (index > -1) {
112
+ listeners.splice(index, 1);
113
+ }
114
+ }
115
+ }
84
116
  }
85
117
  exports.DrozSdk = DrozSdk;
@@ -6,8 +6,8 @@ export type GetSdk<Sdk> = (requester: Requester<Sdk>) => Sdk;
6
6
  export declare class SdkError extends Error {
7
7
  statusCode?: number;
8
8
  errorCode?: string;
9
- errors?: ReadonlyArray<GraphQLError>;
10
- constructor(errors: ReadonlyArray<GraphQLError>);
9
+ errors?: ReadonlyArray<Partial<GraphQLError>>;
10
+ constructor(errors: ReadonlyArray<Partial<GraphQLError>>);
11
11
  }
12
12
  export declare class SdkConfigurationError extends Error {
13
13
  constructor(message: string);
@@ -1,14 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.mapAsyncIterableGraphqlResponse = exports.mapGraphqlResponse = exports.resolveAuthorization = exports.toAuthorizationProvider = exports.SdkConfigurationError = exports.SdkError = exports.serviceDiscoveryEndpoint = void 0;
4
+ const config_1 = require("./config");
4
5
  exports.serviceDiscoveryEndpoint = 'https://root.droz.services';
5
6
  class SdkError extends Error {
6
7
  statusCode;
7
8
  errorCode;
8
9
  errors;
9
10
  constructor(errors) {
10
- const { message, extensions } = errors?.[0] ?? {};
11
- super(message ?? 'Unknown Error');
11
+ const { message, extensions, originalError } = errors?.[0] ?? {};
12
+ super(message ?? 'Unknown Error', {
13
+ cause: originalError
14
+ });
12
15
  this.errors = errors;
13
16
  this.statusCode = extensions?.statusCode;
14
17
  this.errorCode = extensions?.errorCode;
@@ -47,12 +50,25 @@ async function resolveAuthorization(provider) {
47
50
  exports.resolveAuthorization = resolveAuthorization;
48
51
  function mapGraphqlResponse(response) {
49
52
  if (response) {
50
- if (response && 'stack' in response && 'message' in response)
51
- throw response;
52
- if ('errors' in response)
53
- throw new SdkError(response.errors);
54
- if ('data' in response)
53
+ if (response && 'stack' in response && 'message' in response) {
54
+ const error = new SdkError([
55
+ {
56
+ message: response.message,
57
+ originalError: response,
58
+ extensions: { errorCode: '500-unknown-error', statusCode: 500 }
59
+ }
60
+ ]);
61
+ config_1.DrozSdk.emit('error', error);
62
+ throw error;
63
+ }
64
+ if ('errors' in response) {
65
+ const error = new SdkError(response.errors);
66
+ config_1.DrozSdk.emit('error', error);
67
+ throw error;
68
+ }
69
+ if ('data' in response) {
55
70
  return response.data;
71
+ }
56
72
  }
57
73
  }
58
74
  exports.mapGraphqlResponse = mapGraphqlResponse;
@@ -60,7 +60,9 @@ class HttpRequester {
60
60
  return emptyAsyncIterator();
61
61
  // extract operation name and enqueue the request to be executed in batches
62
62
  const operationName = /(?:query|mutation) ([^({ ]*)/.exec(query)?.[1];
63
- return this.inbatches({ query, variables, operationName }).then(helpers_1.mapGraphqlResponse);
63
+ return this.inbatches({ query, variables, operationName })
64
+ .catch(error => (0, helpers_1.mapGraphqlResponse)(error))
65
+ .then(data => (0, helpers_1.mapGraphqlResponse)(data));
64
66
  }
65
67
  async buildRequest() {
66
68
  const [authorization, tenant] = await Promise.all([
@@ -94,7 +96,11 @@ class HttpRequester {
94
96
  headers,
95
97
  body
96
98
  });
97
- return await response.json();
99
+ if (response.status === 200) {
100
+ return await response.json();
101
+ }
102
+ const error = await response.text();
103
+ return [].concat(request).map(() => new Error(error)); // one error per request
98
104
  }
99
105
  }
100
106
  __decorate([
@@ -128,6 +128,7 @@ export type DrozNexoConnectionConnection = {
128
128
  };
129
129
  export type DrozNexoConnectionSuggestion = {
130
130
  destination: DrozNexoAppAndAppInstance;
131
+ title: Scalars['String']['output'];
131
132
  trigger: DrozNexoAppAndAppInstance;
132
133
  };
133
134
  export type DrozNexoUsageStatistics = {
@@ -174,10 +175,10 @@ export type DrozNexoAppInstanceFragment = {
174
175
  app: Pick<DrozNexoApp, 'id' | 'name' | 'description'>;
175
176
  appInstance: Pick<DrozNexoAppInstance, 'drn' | 'name' | 'createdAt' | 'updatedAt'>;
176
177
  };
177
- export type DrozNexoSuggestionFragment = {
178
+ export type DrozNexoSuggestionFragment = (Pick<DrozNexoConnectionSuggestion, 'title'> & {
178
179
  trigger: DrozNexoAppInstanceFragment;
179
180
  destination: DrozNexoAppInstanceFragment;
180
- };
181
+ });
181
182
  export type DrozNexoConnectionFragment = (Pick<DrozNexoConnection, 'id' | 'versionId' | 'title' | 'description' | 'createdAt' | 'updatedAt'> & {
182
183
  trigger: DrozNexoAppInstanceFragment;
183
184
  destination: DrozNexoAppInstanceFragment;
@@ -216,9 +217,9 @@ export type RemoveDrozNexoConnectionMutation = {
216
217
  removeDrozNexoConnection: DrozNexoConnectionFragment;
217
218
  };
218
219
  export declare const DrozNexoAppInstanceFragmentDoc = "\n fragment drozNexoAppInstance on DrozNexoAppAndAppInstance {\n app {\n id\n name\n description\n }\n appInstance {\n drn\n name\n createdAt\n updatedAt\n }\n}\n ";
219
- export declare const DrozNexoSuggestionFragmentDoc = "\n fragment drozNexoSuggestion on DrozNexoConnectionSuggestion {\n trigger {\n ...drozNexoAppInstance\n }\n destination {\n ...drozNexoAppInstance\n }\n}\n \n fragment drozNexoAppInstance on DrozNexoAppAndAppInstance {\n app {\n id\n name\n description\n }\n appInstance {\n drn\n name\n createdAt\n updatedAt\n }\n}\n ";
220
+ export declare const DrozNexoSuggestionFragmentDoc = "\n fragment drozNexoSuggestion on DrozNexoConnectionSuggestion {\n title\n trigger {\n ...drozNexoAppInstance\n }\n destination {\n ...drozNexoAppInstance\n }\n}\n \n fragment drozNexoAppInstance on DrozNexoAppAndAppInstance {\n app {\n id\n name\n description\n }\n appInstance {\n drn\n name\n createdAt\n updatedAt\n }\n}\n ";
220
221
  export declare const DrozNexoConnectionFragmentDoc = "\n fragment drozNexoConnection on DrozNexoConnection {\n id\n versionId\n title\n description\n trigger {\n ...drozNexoAppInstance\n }\n destination {\n ...drozNexoAppInstance\n }\n createdAt\n updatedAt\n}\n \n fragment drozNexoAppInstance on DrozNexoAppAndAppInstance {\n app {\n id\n name\n description\n }\n appInstance {\n drn\n name\n createdAt\n updatedAt\n }\n}\n ";
221
- export declare const ListDrozNexoSuggestionsDocument = "\n query listDrozNexoSuggestions {\n listDrozNexoSuggestions {\n ...drozNexoSuggestion\n }\n}\n \n fragment drozNexoSuggestion on DrozNexoConnectionSuggestion {\n trigger {\n ...drozNexoAppInstance\n }\n destination {\n ...drozNexoAppInstance\n }\n}\n \n fragment drozNexoAppInstance on DrozNexoAppAndAppInstance {\n app {\n id\n name\n description\n }\n appInstance {\n drn\n name\n createdAt\n updatedAt\n }\n}\n ";
222
+ export declare const ListDrozNexoSuggestionsDocument = "\n query listDrozNexoSuggestions {\n listDrozNexoSuggestions {\n ...drozNexoSuggestion\n }\n}\n \n fragment drozNexoSuggestion on DrozNexoConnectionSuggestion {\n title\n trigger {\n ...drozNexoAppInstance\n }\n destination {\n ...drozNexoAppInstance\n }\n}\n \n fragment drozNexoAppInstance on DrozNexoAppAndAppInstance {\n app {\n id\n name\n description\n }\n appInstance {\n drn\n name\n createdAt\n updatedAt\n }\n}\n ";
222
223
  export declare const ListDrozNexoConnectionsDocument = "\n query listDrozNexoConnections($next: Base64) {\n listDrozNexoConnections(next: $next) {\n nodes {\n ...drozNexoConnection\n }\n pageInfo {\n next\n hasNext\n }\n }\n}\n \n fragment drozNexoConnection on DrozNexoConnection {\n id\n versionId\n title\n description\n trigger {\n ...drozNexoAppInstance\n }\n destination {\n ...drozNexoAppInstance\n }\n createdAt\n updatedAt\n}\n \n fragment drozNexoAppInstance on DrozNexoAppAndAppInstance {\n app {\n id\n name\n description\n }\n appInstance {\n drn\n name\n createdAt\n updatedAt\n }\n}\n ";
223
224
  export declare const GetUsageStatisticsDocument = "\n query getUsageStatistics {\n getUsageStatistics {\n totalSecrets\n totalAppInstances\n totalConnections\n }\n}\n ";
224
225
  export declare const CreateDrozNexoConnectionDocument = "\n mutation createDrozNexoConnection($input: CreateDrozNexoConnectionInput!) {\n createDrozNexoConnection(input: $input) {\n ...drozNexoConnection\n }\n}\n \n fragment drozNexoConnection on DrozNexoConnection {\n id\n versionId\n title\n description\n trigger {\n ...drozNexoAppInstance\n }\n destination {\n ...drozNexoAppInstance\n }\n createdAt\n updatedAt\n}\n \n fragment drozNexoAppInstance on DrozNexoAppAndAppInstance {\n app {\n id\n name\n description\n }\n appInstance {\n drn\n name\n createdAt\n updatedAt\n }\n}\n ";
@@ -31,6 +31,7 @@ exports.DrozNexoAppInstanceFragmentDoc = `
31
31
  `;
32
32
  exports.DrozNexoSuggestionFragmentDoc = `
33
33
  fragment drozNexoSuggestion on DrozNexoConnectionSuggestion {
34
+ title
34
35
  trigger {
35
36
  ...drozNexoAppInstance
36
37
  }