@droz-js/sdk
Advanced tools
Comparing version 0.4.27 to 0.5.0
{ | ||
"name": "@droz-js/sdk", | ||
"description": "Droz SDK", | ||
"version": "0.4.27", | ||
"version": "0.5.0", | ||
"private": false, | ||
@@ -6,0 +6,0 @@ "exports": { |
@@ -11,4 +11,3 @@ import { AuthorizationProvider } from './helpers'; | ||
readonly tenant: string; | ||
private readonly services; | ||
constructor(tenant: string, instances: Service[]); | ||
constructor(tenant: string); | ||
getEndpoint(serviceName: string, type?: 'http' | 'ws'): string; | ||
@@ -28,5 +27,3 @@ } | ||
static getAuthorization(customProvider?: AuthorizationProvider): Promise<string>; | ||
static getTenantConfig(customTenant?: string): Promise<TenantConfig>; | ||
private static getOrDiscoverTenantConfig; | ||
private static discoverTenantConfig; | ||
static getTenantConfig(customTenant?: string): TenantConfig; | ||
static on<T>(event: events, listener: (e: T) => void): void; | ||
@@ -33,0 +30,0 @@ static off<T>(event: events, listener: (e: T) => void): void; |
@@ -7,20 +7,13 @@ "use strict"; | ||
tenant; | ||
services; | ||
constructor(tenant, instances) { | ||
constructor(tenant) { | ||
this.tenant = tenant; | ||
this.services = instances; | ||
} | ||
getEndpoint(serviceName, type = 'http') { | ||
const serviceId = serviceName.replace('@droz/', ''); | ||
const instance = this.services.find(instance => { | ||
return instance.serviceId == serviceId && instance.type == type; | ||
}); | ||
if (instance) { | ||
if (type === 'http') { | ||
const endpoint = new URL(instance.endpoint); | ||
endpoint.pathname = '/graphql'; | ||
return endpoint.toString(); | ||
} | ||
return instance.endpoint; | ||
if (type === 'http') { | ||
return `https://${serviceId}-${this.tenant}.droz.services/graphql`; | ||
} | ||
if (type === 'ws') { | ||
return `wss://${serviceId}-${this.tenant}.droz.services/ws`; | ||
} | ||
throw new helpers_1.SdkConfigurationError(`Tenant "${this.tenant}" endpoint for service "${serviceId}" and type "${type}" not found`); | ||
@@ -55,33 +48,5 @@ } | ||
} | ||
static async getTenantConfig(customTenant) { | ||
if (customTenant) | ||
return await this.getOrDiscoverTenantConfig(customTenant); | ||
const defaultTenant = this.defaultTenant; | ||
return this.getOrDiscoverTenantConfig(defaultTenant); | ||
static getTenantConfig(customTenant) { | ||
return new TenantConfig(customTenant ?? this.defaultTenant); | ||
} | ||
static async getOrDiscoverTenantConfig(tenant) { | ||
if (this.tenantConfigs.has(tenant)) { | ||
return await this.tenantConfigs.get(tenant); | ||
} | ||
return await this.discoverTenantConfig(tenant); | ||
} | ||
static discoverTenantConfig(tenant) { | ||
async function promise() { | ||
// fetch instances from discovery service | ||
const data = await fetch(`${helpers_1.serviceDiscoveryEndpoint}/discover/${tenant}`); | ||
const json = await data.json().catch(error => { | ||
console.error(error); | ||
return {}; | ||
}); | ||
const instances = json.instances ?? []; | ||
// validate it's a valid tenant | ||
if (instances.length === 0) { | ||
throw new helpers_1.SdkConfigurationError(`Invalid tenant '${tenant}', no instances found on service discovery`); | ||
} | ||
// create tenant config and cache it | ||
return new TenantConfig(tenant, instances); | ||
} | ||
this.tenantConfigs.set(tenant, promise()); | ||
return this.tenantConfigs.get(tenant); | ||
} | ||
static on(event, listener) { | ||
@@ -88,0 +53,0 @@ this.addEventListener(event, listener); |
import type { ExecutionResult, GraphQLError } from 'graphql'; | ||
export declare const serviceDiscoveryEndpoint = "https://root.droz.services"; | ||
export type AuthorizationProvider = string | (() => Promise<string>) | (() => string); | ||
@@ -4,0 +3,0 @@ export type Requester<C = {}> = <R, V>(doc: string, vars?: V, options?: C) => Promise<R> | AsyncIterableIterator<R>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.mapAsyncIterableGraphqlResponse = exports.mapGraphqlResponse = exports.resolveAuthorization = exports.toAuthorizationProvider = exports.SdkConfigurationError = exports.SdkError = exports.serviceDiscoveryEndpoint = void 0; | ||
exports.mapAsyncIterableGraphqlResponse = exports.mapGraphqlResponse = exports.resolveAuthorization = exports.toAuthorizationProvider = exports.SdkConfigurationError = exports.SdkError = void 0; | ||
const config_1 = require("./config"); | ||
exports.serviceDiscoveryEndpoint = 'https://root.droz.services'; | ||
class SdkError extends Error { | ||
@@ -7,0 +6,0 @@ statusCode; |
@@ -68,7 +68,5 @@ "use strict"; | ||
async buildRequest() { | ||
const [authorization, tenant] = await Promise.all([ | ||
config_1.DrozSdk.getAuthorization(this.authorization), // | ||
config_1.DrozSdk.getTenantConfig(this.tenant) | ||
]); | ||
const tenant = config_1.DrozSdk.getTenantConfig(this.tenant); | ||
const endpoint = tenant.getEndpoint(this.serviceName); | ||
const authorization = await config_1.DrozSdk.getAuthorization(this.authorization); | ||
const heads = new Headers(); | ||
@@ -75,0 +73,0 @@ if (authorization) |
@@ -16,7 +16,5 @@ "use strict"; | ||
return this.client; | ||
const [tenant, authorization] = await Promise.all([ | ||
config_1.DrozSdk.getTenantConfig(), // | ||
config_1.DrozSdk.getAuthorization() | ||
]); | ||
const tenant = config_1.DrozSdk.getTenantConfig(); | ||
const endpoint = tenant.getEndpoint(this.serviceName, 'ws'); | ||
const authorization = await config_1.DrozSdk.getAuthorization(); | ||
const connectionParams = authorization ? { authorization } : {}; | ||
@@ -23,0 +21,0 @@ this.client = (0, graphql_ws_1.createClient)({ |
@@ -44,2 +44,3 @@ "use strict"; | ||
StateMachineConfigStatus["Published"] = "Published"; | ||
StateMachineConfigStatus["Removed"] = "Removed"; | ||
})(StateMachineConfigStatus || (exports.StateMachineConfigStatus = StateMachineConfigStatus = {})); | ||
@@ -46,0 +47,0 @@ var Typenames; |
Sorry, the diff of this file is too big to display
1
377527
8263