@droz-js/sdk 0.2.6 → 0.2.8
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 +1 -1
- package/src/client/config.js +15 -13
- package/src/client/helpers.d.ts +11 -1
- package/src/client/helpers.js +33 -3
package/package.json
CHANGED
package/src/client/config.js
CHANGED
|
@@ -58,23 +58,25 @@ class DrozSdk {
|
|
|
58
58
|
}
|
|
59
59
|
static async getOrDiscoverTenantConfig(tenant) {
|
|
60
60
|
if (this.tenantConfigs.has(tenant)) {
|
|
61
|
-
return this.tenantConfigs.get(tenant);
|
|
61
|
+
return await this.tenantConfigs.get(tenant);
|
|
62
62
|
}
|
|
63
63
|
return await this.discoverTenantConfig(tenant);
|
|
64
64
|
}
|
|
65
|
-
static
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
65
|
+
static discoverTenantConfig(tenant) {
|
|
66
|
+
async function promise() {
|
|
67
|
+
// fetch instances from discovery service
|
|
68
|
+
const data = await fetch(`${helpers_1.serviceDiscoveryEndpoint}/discover/${tenant}`);
|
|
69
|
+
const json = await data.json();
|
|
70
|
+
const instances = json.instances ?? [];
|
|
71
|
+
// validate it's a valid tenant
|
|
72
|
+
if (instances.length === 0) {
|
|
73
|
+
throw new Error(`Invalid tenant '${tenant}', no instances found on service discovery`);
|
|
74
|
+
}
|
|
75
|
+
// create tenant config and cache it
|
|
76
|
+
return new TenantConfig(tenant, instances);
|
|
73
77
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
this.tenantConfigs.set(tenant, config);
|
|
77
|
-
return config;
|
|
78
|
+
this.tenantConfigs.set(tenant, promise());
|
|
79
|
+
return this.tenantConfigs.get(tenant);
|
|
78
80
|
}
|
|
79
81
|
}
|
|
80
82
|
exports.DrozSdk = DrozSdk;
|
package/src/client/helpers.d.ts
CHANGED
|
@@ -6,4 +6,14 @@ export type GetSdk<Sdk> = (requester: Requester<Sdk>) => Sdk;
|
|
|
6
6
|
export declare function toAuthorizationProvider(type?: string, ...values: string[]): AuthorizationProvider;
|
|
7
7
|
export declare function resolveAuthorization(provider?: AuthorizationProvider): Promise<string>;
|
|
8
8
|
export declare function mapGraphqlResponse<T>(response: ExecutionResult<T> | Error): T;
|
|
9
|
-
|
|
9
|
+
declare class AsyncIterableIteratorMapper<T = any> implements AsyncIterableIterator<T> {
|
|
10
|
+
private from;
|
|
11
|
+
private mapper;
|
|
12
|
+
constructor(from: AsyncIterableIterator<ExecutionResult<T, any>>, mapper: (response: ExecutionResult<T, any>) => T);
|
|
13
|
+
next(...args: [] | [undefined]): Promise<IteratorResult<T, any>>;
|
|
14
|
+
return(value?: any): Promise<IteratorResult<T, any>>;
|
|
15
|
+
throw(e?: any): Promise<IteratorResult<T, any>>;
|
|
16
|
+
[Symbol.asyncIterator](): AsyncIterableIterator<T>;
|
|
17
|
+
}
|
|
18
|
+
export declare function mapAsyncIterableGraphqlResponse<T>(iterable: AsyncIterableIterator<ExecutionResult<T, any>>): AsyncIterableIteratorMapper<T>;
|
|
19
|
+
export {};
|
package/src/client/helpers.js
CHANGED
|
@@ -41,9 +41,39 @@ function mapGraphqlResponse(response) {
|
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
exports.mapGraphqlResponse = mapGraphqlResponse;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
class AsyncIterableIteratorMapper {
|
|
45
|
+
from;
|
|
46
|
+
mapper;
|
|
47
|
+
constructor(from, mapper) {
|
|
48
|
+
this.from = from;
|
|
49
|
+
this.mapper = mapper;
|
|
47
50
|
}
|
|
51
|
+
async next(...args) {
|
|
52
|
+
const next = await this.from.next(...args);
|
|
53
|
+
return {
|
|
54
|
+
done: next.done,
|
|
55
|
+
value: this.mapper(next.value)
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
async return(value) {
|
|
59
|
+
const ret = await this.from.return(value);
|
|
60
|
+
return {
|
|
61
|
+
done: ret.done,
|
|
62
|
+
value: this.mapper(ret.value)
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
async throw(e) {
|
|
66
|
+
const thr = await this.from.throw(e);
|
|
67
|
+
return {
|
|
68
|
+
done: thr.done,
|
|
69
|
+
value: this.mapper(thr.value)
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
[Symbol.asyncIterator]() {
|
|
73
|
+
return this;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
function mapAsyncIterableGraphqlResponse(iterable) {
|
|
77
|
+
return new AsyncIterableIteratorMapper(iterable, mapGraphqlResponse);
|
|
48
78
|
}
|
|
49
79
|
exports.mapAsyncIterableGraphqlResponse = mapAsyncIterableGraphqlResponse;
|