@droz-js/sdk 0.4.27 → 0.5.0

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@droz-js/sdk",
3
3
  "description": "Droz SDK",
4
- "version": "0.4.27",
4
+ "version": "0.5.0",
5
5
  "private": false,
6
6
  "exports": {
7
7
  ".": "./src/index.js",
@@ -8,8 +8,7 @@ export interface Service {
8
8
  type events = 'error';
9
9
  declare class TenantConfig {
10
10
  readonly tenant: string;
11
- private readonly services;
12
- constructor(tenant: string, instances: Service[]);
11
+ constructor(tenant: string);
13
12
  getEndpoint(serviceName: string, type?: 'http' | 'ws'): string;
14
13
  }
15
14
  /**
@@ -24,9 +23,7 @@ export declare class DrozSdk {
24
23
  static withAuthorization(type?: string, ...values: string[]): void;
25
24
  static withAuthorizationProvider(provider: () => Promise<string>): void;
26
25
  static getAuthorization(customProvider?: AuthorizationProvider): Promise<string>;
27
- static getTenantConfig(customTenant?: string): Promise<TenantConfig>;
28
- private static getOrDiscoverTenantConfig;
29
- private static discoverTenantConfig;
26
+ static getTenantConfig(customTenant?: string): TenantConfig;
30
27
  static on<T>(event: events, listener: (e: T) => void): void;
31
28
  static off<T>(event: events, listener: (e: T) => void): void;
32
29
  static emit(event: events, data: any): void;
@@ -4,23 +4,16 @@ exports.DrozSdk = void 0;
4
4
  const helpers_1 = require("./helpers");
5
5
  class TenantConfig {
6
6
  tenant;
7
- services;
8
- constructor(tenant, instances) {
7
+ constructor(tenant) {
9
8
  this.tenant = tenant;
10
- this.services = instances;
11
9
  }
12
10
  getEndpoint(serviceName, type = 'http') {
13
11
  const serviceId = serviceName.replace('@droz/', '');
14
- const instance = this.services.find(instance => {
15
- return instance.serviceId == serviceId && instance.type == type;
16
- });
17
- if (instance) {
18
- if (type === 'http') {
19
- const endpoint = new URL(instance.endpoint);
20
- endpoint.pathname = '/graphql';
21
- return endpoint.toString();
22
- }
23
- return instance.endpoint;
12
+ if (type === 'http') {
13
+ return `https://${serviceId}-${this.tenant}.droz.services/graphql`;
14
+ }
15
+ if (type === 'ws') {
16
+ return `wss://${serviceId}-${this.tenant}.droz.services/ws`;
24
17
  }
25
18
  throw new helpers_1.SdkConfigurationError(`Tenant "${this.tenant}" endpoint for service "${serviceId}" and type "${type}" not found`);
26
19
  }
@@ -51,36 +44,8 @@ class DrozSdk {
51
44
  if (defaultProvider)
52
45
  return await (0, helpers_1.resolveAuthorization)(defaultProvider);
53
46
  }
54
- static async getTenantConfig(customTenant) {
55
- if (customTenant)
56
- return await this.getOrDiscoverTenantConfig(customTenant);
57
- const defaultTenant = this.defaultTenant;
58
- return this.getOrDiscoverTenantConfig(defaultTenant);
59
- }
60
- static async getOrDiscoverTenantConfig(tenant) {
61
- if (this.tenantConfigs.has(tenant)) {
62
- return await this.tenantConfigs.get(tenant);
63
- }
64
- return await this.discoverTenantConfig(tenant);
65
- }
66
- static discoverTenantConfig(tenant) {
67
- async function promise() {
68
- // fetch instances from discovery service
69
- const data = await fetch(`${helpers_1.serviceDiscoveryEndpoint}/discover/${tenant}`);
70
- const json = await data.json().catch(error => {
71
- console.error(error);
72
- return {};
73
- });
74
- const instances = json.instances ?? [];
75
- // validate it's a valid tenant
76
- if (instances.length === 0) {
77
- throw new helpers_1.SdkConfigurationError(`Invalid tenant '${tenant}', no instances found on service discovery`);
78
- }
79
- // create tenant config and cache it
80
- return new TenantConfig(tenant, instances);
81
- }
82
- this.tenantConfigs.set(tenant, promise());
83
- return this.tenantConfigs.get(tenant);
47
+ static getTenantConfig(customTenant) {
48
+ return new TenantConfig(customTenant ?? this.defaultTenant);
84
49
  }
85
50
  static on(event, listener) {
86
51
  this.addEventListener(event, listener);
@@ -1,5 +1,4 @@
1
1
  import type { ExecutionResult, GraphQLError } from 'graphql';
2
- export declare const serviceDiscoveryEndpoint = "https://root.droz.services";
3
2
  export type AuthorizationProvider = string | (() => Promise<string>) | (() => string);
4
3
  export type Requester<C = {}> = <R, V>(doc: string, vars?: V, options?: C) => Promise<R> | AsyncIterableIterator<R>;
5
4
  export type GetSdk<Sdk> = (requester: Requester<Sdk>) => Sdk;
@@ -1,8 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.mapAsyncIterableGraphqlResponse = exports.mapGraphqlResponse = exports.resolveAuthorization = exports.toAuthorizationProvider = exports.SdkConfigurationError = exports.SdkError = exports.serviceDiscoveryEndpoint = void 0;
3
+ exports.mapAsyncIterableGraphqlResponse = exports.mapGraphqlResponse = exports.resolveAuthorization = exports.toAuthorizationProvider = exports.SdkConfigurationError = exports.SdkError = void 0;
4
4
  const config_1 = require("./config");
5
- exports.serviceDiscoveryEndpoint = 'https://root.droz.services';
6
5
  class SdkError extends Error {
7
6
  statusCode;
8
7
  errorCode;
@@ -65,11 +65,9 @@ class HttpRequester {
65
65
  .then(data => (0, helpers_1.mapGraphqlResponse)(data));
66
66
  }
67
67
  async buildRequest() {
68
- const [authorization, tenant] = await Promise.all([
69
- config_1.DrozSdk.getAuthorization(this.authorization), //
70
- config_1.DrozSdk.getTenantConfig(this.tenant)
71
- ]);
68
+ const tenant = config_1.DrozSdk.getTenantConfig(this.tenant);
72
69
  const endpoint = tenant.getEndpoint(this.serviceName);
70
+ const authorization = await config_1.DrozSdk.getAuthorization(this.authorization);
73
71
  const heads = new Headers();
74
72
  if (authorization)
75
73
  heads.set('Authorization', authorization);
package/src/client/ws.js CHANGED
@@ -13,11 +13,9 @@ class WsRequester {
13
13
  async connect() {
14
14
  if (this.client)
15
15
  return this.client;
16
- const [tenant, authorization] = await Promise.all([
17
- config_1.DrozSdk.getTenantConfig(), //
18
- config_1.DrozSdk.getAuthorization()
19
- ]);
16
+ const tenant = config_1.DrozSdk.getTenantConfig();
20
17
  const endpoint = tenant.getEndpoint(this.serviceName, 'ws');
18
+ const authorization = await config_1.DrozSdk.getAuthorization();
21
19
  const connectionParams = authorization ? { authorization } : {};
22
20
  this.client = (0, graphql_ws_1.createClient)({
23
21
  url: endpoint,
@@ -664,7 +664,8 @@ export type StateMachineConfigStatesOnInput = {
664
664
  export declare enum StateMachineConfigStatus {
665
665
  Draft = "Draft",
666
666
  Live = "Live",
667
- Published = "Published"
667
+ Published = "Published",
668
+ Removed = "Removed"
668
669
  }
669
670
  export type Storage = {
670
671
  cdnUrl: Scalars['String']['output'];
@@ -41,6 +41,7 @@ var StateMachineConfigStatus;
41
41
  StateMachineConfigStatus["Draft"] = "Draft";
42
42
  StateMachineConfigStatus["Live"] = "Live";
43
43
  StateMachineConfigStatus["Published"] = "Published";
44
+ StateMachineConfigStatus["Removed"] = "Removed";
44
45
  })(StateMachineConfigStatus || (exports.StateMachineConfigStatus = StateMachineConfigStatus = {}));
45
46
  var Typenames;
46
47
  (function (Typenames) {