@streamlayer/sdk-web-api 0.11.4 → 0.12.1
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 +3 -2
- package/src/grpc/queries/event.d.ts +0 -1
- package/src/grpc/queries/event.js +21 -22
- package/src/grpc/queries/event.js.map +1 -1
- package/src/grpc/queries/index.js +0 -1
- package/src/grpc/queries/index.js.map +1 -1
- package/src/grpc/queries/organization.d.ts +1 -1
- package/src/grpc/queries/organization.js +14 -20
- package/src/grpc/queries/organization.js.map +1 -1
- package/src/grpc/queries/user.js +17 -39
- package/src/grpc/queries/user.js.map +1 -1
- package/src/grpc/subscription.d.ts +43 -0
- package/src/grpc/subscription.js +107 -0
- package/src/grpc/subscription.js.map +1 -0
- package/src/grpc/transport.d.ts +19 -2
- package/src/grpc/transport.js +141 -90
- package/src/grpc/transport.js.map +1 -1
- package/src/index.d.ts +11 -0
- package/src/index.js +6 -1
- package/src/index.js.map +1 -1
- package/src/utils/devtools.d.ts +1 -0
- package/src/utils/devtools.js +66 -0
- package/src/utils/devtools.js.map +1 -0
- package/src/utils/grpc-stub.js +11 -23
- package/src/utils/grpc-stub.js.map +1 -1
- package/src/grpc/queries/queries.test.js +0 -37
- package/src/grpc/queries/queries.test.js.map +0 -1
- package/src/grpc/transport.test.js +0 -81
- package/src/grpc/transport.test.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@streamlayer/sdk-web-api",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./src/index.js",
|
|
6
6
|
"typings": "./src/index.d.ts",
|
|
@@ -22,7 +22,8 @@
|
|
|
22
22
|
"@nanostores/query": "^0.2.4",
|
|
23
23
|
"@streamlayer/sdk-web-interfaces": "workspace:^",
|
|
24
24
|
"@streamlayer/sl-eslib": "^5.29.0",
|
|
25
|
-
"nanostores": "^0.9.3"
|
|
25
|
+
"nanostores": "^0.9.3",
|
|
26
|
+
"tslib": "^2.6.2"
|
|
26
27
|
},
|
|
27
28
|
"devDependencies": {
|
|
28
29
|
"@swc/helpers": "^0.5.2"
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { ReadableAtom } from 'nanostores';
|
|
2
2
|
import { Transport } from '../transport';
|
|
3
|
-
export { $user } from './user';
|
|
4
3
|
export declare const $retrieveEventId: ($providerStreamId: ReadableAtom<string | undefined>, transport: Transport) => import("@nanostores/query").FetcherStore<string, any>;
|
|
5
4
|
export declare const $streamSettings: (slStreamId: ReadableAtom<string | undefined>, transport: Transport) => import("@nanostores/query").FetcherStore<import("@streamlayer/sl-eslib/sdkSettings/sdkSettings.common_pb").StreamSettings | undefined, any>;
|
|
@@ -1,40 +1,39 @@
|
|
|
1
1
|
import { Events } from '@streamlayer/sl-eslib/sports/events/events_connect';
|
|
2
2
|
import { Client } from '@streamlayer/sl-eslib/sdkSettings/client/client_connect';
|
|
3
|
-
export
|
|
4
|
-
export const $retrieveEventId = ($providerStreamId, transport)=>{
|
|
3
|
+
export const $retrieveEventId = ($providerStreamId, transport) => {
|
|
5
4
|
const { client, queryKey } = transport.createPromiseClient(Events, {
|
|
6
5
|
method: 'retrieveEventId',
|
|
7
|
-
params: [
|
|
8
|
-
$providerStreamId
|
|
9
|
-
]
|
|
6
|
+
params: [$providerStreamId],
|
|
10
7
|
});
|
|
11
8
|
return transport.nanoquery.createFetcherStore(queryKey, {
|
|
12
|
-
fetcher: async (_, __, id)=>{
|
|
9
|
+
fetcher: async (_, __, id) => {
|
|
13
10
|
if (!id || typeof id !== 'string') {
|
|
14
11
|
return '';
|
|
15
12
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
13
|
+
try {
|
|
14
|
+
const res = await client.retrieveEventId({
|
|
15
|
+
id,
|
|
16
|
+
});
|
|
17
|
+
return res.data?.id || '';
|
|
18
|
+
}
|
|
19
|
+
catch (err) {
|
|
20
|
+
return '';
|
|
21
|
+
}
|
|
22
|
+
},
|
|
21
23
|
});
|
|
22
24
|
};
|
|
23
|
-
export const $streamSettings = (slStreamId, transport)=>{
|
|
24
|
-
const { client, queryKey } = transport.createPromiseClient(Client, {
|
|
25
|
-
method: 'getStream',
|
|
26
|
-
params: [
|
|
27
|
-
slStreamId
|
|
28
|
-
]
|
|
29
|
-
});
|
|
25
|
+
export const $streamSettings = (slStreamId, transport) => {
|
|
26
|
+
const { client, queryKey } = transport.createPromiseClient(Client, { method: 'getStream', params: [slStreamId] });
|
|
30
27
|
return transport.nanoquery.createFetcherStore(queryKey, {
|
|
31
|
-
fetcher: async (_, __, id)=>{
|
|
28
|
+
fetcher: async (_, __, id) => {
|
|
29
|
+
if (!id) {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
32
|
const res = await client.getStream({
|
|
33
|
-
id: id
|
|
33
|
+
id: id, // we are sure that id is a string
|
|
34
34
|
});
|
|
35
35
|
return res.data?.attributes;
|
|
36
|
-
}
|
|
36
|
+
},
|
|
37
37
|
});
|
|
38
38
|
};
|
|
39
|
-
|
|
40
39
|
//# sourceMappingURL=event.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../../packages/sdk-web-api/src/grpc/queries/event.ts"],"
|
|
1
|
+
{"version":3,"file":"event.js","sourceRoot":"","sources":["../../../../../../packages/sdk-web-api/src/grpc/queries/event.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,oDAAoD,CAAA;AAC3E,OAAO,EAAE,MAAM,EAAE,MAAM,yDAAyD,CAAA;AAIhF,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,iBAAmD,EAAE,SAAoB,EAAE,EAAE;IAC5G,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,SAAS,CAAC,mBAAmB,CAAC,MAAM,EAAE;QACjE,MAAM,EAAE,iBAAiB;QAC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"version":3,"sources":["../../../../../../packages/sdk-web-api/src/grpc/queries/index.ts"],"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/sdk-web-api/src/grpc/queries/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAA;AACvB,cAAc,gBAAgB,CAAA;AAC9B,cAAc,QAAQ,CAAA"}
|
|
@@ -17,5 +17,5 @@ export declare const $organizationSettings: ($enabled: ReadableAtom<'on' | undef
|
|
|
17
17
|
brandDefaults?: import("@streamlayer/sl-eslib/sdkSettings/sdkSettings.common_pb").BrandDefaults | undefined;
|
|
18
18
|
pub?: import("@streamlayer/sl-eslib/sdkSettings/sdkSettings.common_pb").JWK | undefined;
|
|
19
19
|
getstream?: import("@streamlayer/sl-eslib/sdkSettings/sdkSettings.common_pb").GetStreamSettingsClient | undefined;
|
|
20
|
-
} |
|
|
20
|
+
} | undefined, any>;
|
|
21
21
|
export declare const $organizationAdvertising: ($enabled: ReadableAtom<string | undefined>, transport: Transport) => import("@nanostores/query").FetcherStore<import("@streamlayer/sl-eslib/sdkSettings/sdkSettings.common_pb").Advertising | undefined, any>;
|
|
@@ -1,35 +1,29 @@
|
|
|
1
1
|
import { Client } from '@streamlayer/sl-eslib/sdkSettings/client/client_connect';
|
|
2
2
|
export { $user } from './user';
|
|
3
|
-
export const $organizationSettings = ($enabled, transport)=>{
|
|
4
|
-
const { client, queryKey } = transport.createPromiseClient(Client, {
|
|
5
|
-
method: 'getOrganization',
|
|
6
|
-
params: [
|
|
7
|
-
$enabled
|
|
8
|
-
]
|
|
9
|
-
});
|
|
3
|
+
export const $organizationSettings = ($enabled, transport) => {
|
|
4
|
+
const { client, queryKey } = transport.createPromiseClient(Client, { method: 'getOrganization', params: [$enabled] });
|
|
10
5
|
return transport.nanoquery.createFetcherStore(queryKey, {
|
|
11
|
-
fetcher: async ()=>{
|
|
6
|
+
fetcher: async () => {
|
|
12
7
|
const res = await client.getOrganization({});
|
|
13
|
-
return res.data
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
8
|
+
return res.data
|
|
9
|
+
? {
|
|
10
|
+
...res.data.attributes,
|
|
11
|
+
id: res.data.id,
|
|
12
|
+
}
|
|
13
|
+
: undefined;
|
|
14
|
+
},
|
|
18
15
|
});
|
|
19
16
|
};
|
|
20
|
-
export const $organizationAdvertising = ($enabled, transport)=>{
|
|
17
|
+
export const $organizationAdvertising = ($enabled, transport) => {
|
|
21
18
|
const { client, queryKey } = transport.createPromiseClient(Client, {
|
|
22
19
|
method: 'getOrganizationAdvertising',
|
|
23
|
-
params: [
|
|
24
|
-
$enabled
|
|
25
|
-
]
|
|
20
|
+
params: [$enabled],
|
|
26
21
|
});
|
|
27
22
|
return transport.nanoquery.createFetcherStore(queryKey, {
|
|
28
|
-
fetcher: async ()=>{
|
|
23
|
+
fetcher: async () => {
|
|
29
24
|
const res = await client.getOrganizationAdvertising({});
|
|
30
25
|
return res.data?.attributes;
|
|
31
|
-
}
|
|
26
|
+
},
|
|
32
27
|
});
|
|
33
28
|
};
|
|
34
|
-
|
|
35
29
|
//# sourceMappingURL=organization.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../../packages/sdk-web-api/src/grpc/queries/organization.ts"],"
|
|
1
|
+
{"version":3,"file":"organization.js","sourceRoot":"","sources":["../../../../../../packages/sdk-web-api/src/grpc/queries/organization.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,yDAAyD,CAAA;AAKhF,OAAO,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAA;AAE9B,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,QAAwC,EAAE,SAAoB,EAAE,EAAE;IACtG,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,SAAS,CAAC,mBAAmB,CAAC,MAAM,EAAE,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAA;IAErH,OAAO,SAAS,CAAC,SAAS,CAAC,kBAAkB,CAAC,QAAQ,EAAE;QACtD,OAAO,EAAE,KAAK,IAAI,EAAE;YAClB,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC,eAAe,CAAC,EAAE,CAAC,CAAA;YAE5C,OAAO,GAAG,CAAC,IAAI;gBACb,CAAC,CAAC;oBACE,GAAG,GAAG,CAAC,IAAI,CAAC,UAAU;oBACtB,EAAE,EAAE,GAAG,CAAC,IAAI,CAAC,EAAE;iBAChB;gBACH,CAAC,CAAC,SAAS,CAAA;QACf,CAAC;KACF,CAAC,CAAA;AACJ,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,wBAAwB,GAAG,CAAC,QAA0C,EAAE,SAAoB,EAAE,EAAE;IAC3G,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,SAAS,CAAC,mBAAmB,CAAC,MAAM,EAAE;QACjE,MAAM,EAAE,4BAA4B;QACpC,MAAM,EAAE,CAAC,QAAQ,CAAC;KACnB,CAAC,CAAA;IAEF,OAAO,SAAS,CAAC,SAAS,CAAC,kBAAkB,CAAC,QAAQ,EAAE;QACtD,OAAO,EAAE,KAAK,IAAI,EAAE;YAClB,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC,0BAA0B,CAAC,EAAE,CAAC,CAAA;YAEvD,OAAO,GAAG,CAAC,IAAI,EAAE,UAAU,CAAA;QAC7B,CAAC;KACF,CAAC,CAAA;AACJ,CAAC,CAAA"}
|
package/src/grpc/queries/user.js
CHANGED
|
@@ -6,59 +6,37 @@ import { Client } from '@streamlayer/sl-eslib/sdkSettings/client/client_connect'
|
|
|
6
6
|
// login
|
|
7
7
|
// save token
|
|
8
8
|
// called on update user key
|
|
9
|
-
export const $user = ($userToken, transport)=>{
|
|
10
|
-
const { queryKey, client } = transport.createPromiseClient(Users, {
|
|
11
|
-
method: 'me',
|
|
12
|
-
params: [
|
|
13
|
-
$userToken
|
|
14
|
-
]
|
|
15
|
-
});
|
|
9
|
+
export const $user = ($userToken, transport) => {
|
|
10
|
+
const { queryKey, client } = transport.createPromiseClient(Users, { method: 'me', params: [$userToken] });
|
|
16
11
|
return transport.nanoquery.createFetcherStore(queryKey, {
|
|
17
|
-
fetcher: ()=>client.me({})
|
|
12
|
+
fetcher: () => client.me({}),
|
|
18
13
|
});
|
|
19
14
|
};
|
|
20
|
-
export const $bypassLogin = (transport)=>{
|
|
21
|
-
const { client, queryKeyStr } = transport.createPromiseClient(Users, {
|
|
22
|
-
|
|
23
|
-
});
|
|
24
|
-
return transport.nanoquery.createMutatorStore(async ({ data: { userKey, schema, init }, getCacheUpdater })=>{
|
|
15
|
+
export const $bypassLogin = (transport) => {
|
|
16
|
+
const { client, queryKeyStr } = transport.createPromiseClient(Users, { method: 'bypassAuth' });
|
|
17
|
+
return transport.nanoquery.createMutatorStore(async ({ data: { userKey, schema, init }, getCacheUpdater }) => {
|
|
25
18
|
const [updateCache] = getCacheUpdater(queryKeyStr);
|
|
26
|
-
const user = await client.bypassAuth({
|
|
27
|
-
userKey,
|
|
28
|
-
schema,
|
|
29
|
-
init
|
|
30
|
-
});
|
|
19
|
+
const user = await client.bypassAuth({ userKey, schema, init });
|
|
31
20
|
updateCache(user);
|
|
32
21
|
return user;
|
|
33
22
|
});
|
|
34
23
|
};
|
|
35
|
-
export const bypassAuth = (transport, params)=>{
|
|
36
|
-
const { client } = transport.createPromiseClient(Users, {
|
|
37
|
-
method: 'bypassAuth'
|
|
38
|
-
});
|
|
24
|
+
export const bypassAuth = (transport, params) => {
|
|
25
|
+
const { client } = transport.createPromiseClient(Users, { method: 'bypassAuth' });
|
|
39
26
|
return client.bypassAuth(params);
|
|
40
27
|
};
|
|
41
|
-
export const $userSettings = ($userToken,
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
params: [
|
|
45
|
-
$userToken
|
|
46
|
-
]
|
|
47
|
-
});
|
|
28
|
+
export const $userSettings = ($userToken, // sl user token
|
|
29
|
+
transport) => {
|
|
30
|
+
const { client, queryKey } = transport.createPromiseClient(Client, { method: 'get', params: [$userToken] });
|
|
48
31
|
return transport.nanoquery.createFetcherStore(queryKey, {
|
|
49
|
-
fetcher: async ()=>{
|
|
32
|
+
fetcher: async () => {
|
|
50
33
|
const data = await client.get({});
|
|
51
34
|
return data.data?.attributes;
|
|
52
|
-
}
|
|
35
|
+
},
|
|
53
36
|
});
|
|
54
37
|
};
|
|
55
|
-
export const register = (transport, phone)=>{
|
|
56
|
-
const { client } = transport.createPromiseClient(Users, {
|
|
57
|
-
|
|
58
|
-
});
|
|
59
|
-
return client.register({
|
|
60
|
-
id: phone
|
|
61
|
-
});
|
|
38
|
+
export const register = (transport, phone) => {
|
|
39
|
+
const { client } = transport.createPromiseClient(Users, { method: 'register' });
|
|
40
|
+
return client.register({ id: phone });
|
|
62
41
|
};
|
|
63
|
-
|
|
64
42
|
//# sourceMappingURL=user.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../../packages/sdk-web-api/src/grpc/queries/user.ts"],"
|
|
1
|
+
{"version":3,"file":"user.js","sourceRoot":"","sources":["../../../../../../packages/sdk-web-api/src/grpc/queries/user.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,2CAA2C,CAAA;AAEjE,OAAO,EAAE,MAAM,EAAE,MAAM,yDAAyD,CAAA;AAKhF,aAAa;AACb,aAAa;AACb,mBAAmB;AACnB,QAAQ;AACR,aAAa;AACb,4BAA4B;AAC5B,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,UAA4C,EAAE,SAAoB,EAAE,EAAE;IAC1F,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,SAAS,CAAC,mBAAmB,CAAC,KAAK,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,UAAU,CAAC,EAAE,CAAC,CAAA;IAEzG,OAAO,SAAS,CAAC,SAAS,CAAC,kBAAkB,CAAC,QAAQ,EAAE;QACt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,CAAC,CAAA;IAE3G,OAAO,SAAS,CAAC,SAAS,CAAC,kBAAkB,CAAC,QAAQ,EAAE;QACtD,OAAO,EAAE,KAAK,IAAI,EAAE;YAClB,MAAM,IAAI,GAAG,MAAM,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;YAEjC,OAAO,IAAI,CAAC,IAAI,EAAE,UAAU,CAAA;QAC9B,CAAC;KACF,CAAC,CAAA;AACJ,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,SAAoB,EAAE,KAAa,EAAE,EAAE;IAC9D,MAAM,EAAE,MAAM,EAAE,GAAG,SAAS,CAAC,mBAAmB,CAAC,KAAK,EAAE,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC,CAAA;IAE/E,OAAO,MAAM,CAAC,QAAQ,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC,CAAA;AACvC,CAAC,CAAA"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { CallbackClient } from '@connectrpc/connect';
|
|
2
|
+
import { Atom } from 'nanostores';
|
|
3
|
+
import type { ServiceType, Message, PlainMessage, MethodInfoServerStreaming } from '@bufbuild/protobuf';
|
|
4
|
+
import { Transport } from './transport';
|
|
5
|
+
type StreamMethods<T extends ServiceType> = {
|
|
6
|
+
[P in keyof CallbackClient<T> as T['methods'][P] extends MethodInfoServerStreaming<any, any> ? P : never]: P;
|
|
7
|
+
};
|
|
8
|
+
export type StreamMethod<T extends ServiceType> = StreamMethods<T>[keyof StreamMethods<T>] extends keyof CallbackClient<T> ? StreamMethods<T>[keyof StreamMethods<T>] : never;
|
|
9
|
+
declare enum ServerStreamSubscriptionStatus {
|
|
10
|
+
Init = "init",
|
|
11
|
+
Ready = "ready",
|
|
12
|
+
Connecting = "connecting",
|
|
13
|
+
Connected = "connected",
|
|
14
|
+
Disconnected = "disconnected",
|
|
15
|
+
Failed = "failed",
|
|
16
|
+
Reconnecting = "reconnecting"
|
|
17
|
+
}
|
|
18
|
+
export type ServerStreamSubscriptionOptions = {
|
|
19
|
+
name: string;
|
|
20
|
+
withStore?: boolean;
|
|
21
|
+
};
|
|
22
|
+
export declare class ServerStreamSubscription<T extends ServiceType, Request extends Message<Request>, Response extends Message<Response>, M extends StreamMethod<T> = StreamMethod<T>, Method extends CallbackClient<T>[M] = CallbackClient<T>[M]> {
|
|
23
|
+
params: Atom<PlainMessage<Request>> | PlainMessage<Request>;
|
|
24
|
+
private stream?;
|
|
25
|
+
private method;
|
|
26
|
+
private name;
|
|
27
|
+
private headers;
|
|
28
|
+
private listeners;
|
|
29
|
+
private state;
|
|
30
|
+
private store?;
|
|
31
|
+
constructor(headers: Transport['$headers'], method: Method, params: Atom<PlainMessage<Request>> | PlainMessage<Request>, options: ServerStreamSubscriptionOptions);
|
|
32
|
+
updateState: (status: ServerStreamSubscriptionStatus) => void;
|
|
33
|
+
addStateLog: (msg: string) => void;
|
|
34
|
+
addListener: (name: string, listener: (response: Response) => void) => boolean;
|
|
35
|
+
removeListener: (name: string) => void;
|
|
36
|
+
connect: () => void;
|
|
37
|
+
disconnect: () => void;
|
|
38
|
+
reconnect: () => void;
|
|
39
|
+
getStore: () => import("nanostores").WritableAtom<Response | null | undefined> | undefined;
|
|
40
|
+
private onData;
|
|
41
|
+
private onStreamError;
|
|
42
|
+
}
|
|
43
|
+
export {};
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { MapStore, SingleStore, createMapStore, createSingleStore } from '@streamlayer/sdk-web-interfaces';
|
|
2
|
+
var ServerStreamSubscriptionStatus;
|
|
3
|
+
(function (ServerStreamSubscriptionStatus) {
|
|
4
|
+
ServerStreamSubscriptionStatus["Init"] = "init";
|
|
5
|
+
ServerStreamSubscriptionStatus["Ready"] = "ready";
|
|
6
|
+
ServerStreamSubscriptionStatus["Connecting"] = "connecting";
|
|
7
|
+
ServerStreamSubscriptionStatus["Connected"] = "connected";
|
|
8
|
+
ServerStreamSubscriptionStatus["Disconnected"] = "disconnected";
|
|
9
|
+
ServerStreamSubscriptionStatus["Failed"] = "failed";
|
|
10
|
+
ServerStreamSubscriptionStatus["Reconnecting"] = "reconnecting";
|
|
11
|
+
})(ServerStreamSubscriptionStatus || (ServerStreamSubscriptionStatus = {}));
|
|
12
|
+
export class ServerStreamSubscription {
|
|
13
|
+
params;
|
|
14
|
+
stream;
|
|
15
|
+
method;
|
|
16
|
+
name;
|
|
17
|
+
headers;
|
|
18
|
+
listeners;
|
|
19
|
+
state;
|
|
20
|
+
store;
|
|
21
|
+
constructor(headers, method, params, options) {
|
|
22
|
+
const initState = {
|
|
23
|
+
status: ServerStreamSubscriptionStatus.Init,
|
|
24
|
+
ts: new Date(),
|
|
25
|
+
log: [],
|
|
26
|
+
};
|
|
27
|
+
this.state = new MapStore(createMapStore(initState), `subscription:${options.name}:state`);
|
|
28
|
+
this.name = options.name;
|
|
29
|
+
this.headers = headers;
|
|
30
|
+
this.listeners = new Map();
|
|
31
|
+
this.params = params;
|
|
32
|
+
this.method = method;
|
|
33
|
+
if (options.withStore) {
|
|
34
|
+
this.store = new SingleStore(createSingleStore(null), `subscription:${options.name}:store`);
|
|
35
|
+
}
|
|
36
|
+
if ('subscribe' in params && typeof params.subscribe === 'function') {
|
|
37
|
+
params.subscribe(() => {
|
|
38
|
+
this.reconnect();
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
this.updateState(ServerStreamSubscriptionStatus.Ready);
|
|
42
|
+
}
|
|
43
|
+
updateState = (status) => {
|
|
44
|
+
this.state.setValue('status', status);
|
|
45
|
+
this.state.setValue('ts', new Date());
|
|
46
|
+
this.addStateLog(`status => ${status}`);
|
|
47
|
+
};
|
|
48
|
+
addStateLog = (msg) => {
|
|
49
|
+
const log = this.state.getStore().get()['log'];
|
|
50
|
+
this.state.setValue('log', [...log, `${new Date().toLocaleString()}: ${msg}`]);
|
|
51
|
+
};
|
|
52
|
+
addListener = (name, listener) => {
|
|
53
|
+
if (this.listeners.has(name)) {
|
|
54
|
+
this.addStateLog(`listener '${name}' not added`);
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
this.listeners.set(name, listener);
|
|
58
|
+
this.addStateLog(`listener '${name}' added`);
|
|
59
|
+
return true;
|
|
60
|
+
};
|
|
61
|
+
removeListener = (name) => {
|
|
62
|
+
this.listeners.delete(name);
|
|
63
|
+
this.addStateLog(`listener '${name}' removed`);
|
|
64
|
+
};
|
|
65
|
+
connect = () => {
|
|
66
|
+
this.updateState(ServerStreamSubscriptionStatus.Connecting);
|
|
67
|
+
if (this.stream) {
|
|
68
|
+
this.addStateLog(`disconnect prev connection`);
|
|
69
|
+
this.stream();
|
|
70
|
+
}
|
|
71
|
+
const params = 'get' in this.params && typeof this.params.get === 'function' ? this.params.get() : this.params;
|
|
72
|
+
this.stream = this.method(params,
|
|
73
|
+
// ToDo: fix types
|
|
74
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
75
|
+
// @ts-ignore
|
|
76
|
+
this.onData, this.onStreamError, { headers: this.headers.getValues() });
|
|
77
|
+
this.updateState(ServerStreamSubscriptionStatus.Connected);
|
|
78
|
+
};
|
|
79
|
+
disconnect = () => {
|
|
80
|
+
if (this.stream) {
|
|
81
|
+
this.stream();
|
|
82
|
+
}
|
|
83
|
+
this.listeners.clear();
|
|
84
|
+
this.updateState(ServerStreamSubscriptionStatus.Disconnected);
|
|
85
|
+
};
|
|
86
|
+
reconnect = () => {
|
|
87
|
+
this.updateState(ServerStreamSubscriptionStatus.Reconnecting);
|
|
88
|
+
this.connect();
|
|
89
|
+
};
|
|
90
|
+
getStore = () => this.store?.getStore();
|
|
91
|
+
onData = (response) => {
|
|
92
|
+
this.addStateLog(`received data => ${JSON.stringify(response)}`);
|
|
93
|
+
if (this.store) {
|
|
94
|
+
this.store.setValue(response);
|
|
95
|
+
}
|
|
96
|
+
for (const [, listener] of this.listeners) {
|
|
97
|
+
listener(response);
|
|
98
|
+
}
|
|
99
|
+
this.addStateLog(`data routed to ${this.listeners.size} listeners`);
|
|
100
|
+
};
|
|
101
|
+
onStreamError = (error) => {
|
|
102
|
+
console.error(error);
|
|
103
|
+
this.updateState(ServerStreamSubscriptionStatus.Failed);
|
|
104
|
+
this.state.setValue('error', error);
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=subscription.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subscription.js","sourceRoot":"","sources":["../../../../../packages/sdk-web-api/src/grpc/subscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAA;AAsB1G,IAAK,8BAQJ;AARD,WAAK,8BAA8B;IACjC,+CAAa,CAAA;IACb,iDAAe,CAAA;IACf,2DAAyB,CAAA;IACzB,yDAAuB,CAAA;IACvB,+DAA6B,CAAA;IAC7B,mDAAiB,CAAA;IACjB,+DAA6B,CAAA;AAC/B,CAAC,EARI,8BAA8B,KAA9B,8BAA8B,QAQlC;AAcD,MAAM,OAAO,wBAAwB;IAO5B,MAAM,CAAqD;IAC1D,MAAM,CAAW;IACjB,MAAM,CAAQ;IACd,IAAI,CAAQ;IACZ,OAAO,CAAuB;IAC9B,SAAS,CAA2C;IACpD,KAAK,CAAyC;IAC9C,KAAK,CAA+B;IAE5C,YACE,OAA8B,EAC9B,MAAc,EACd,MAA2D,EAC3D,OAAwC;QAExC,MAAM,SAAS,GAAkC;YAC/C,MAAM,EAAE,8BAA8B,CAAC,IAAI;YAC3C,EAAE,EAAE,IAAI,IAAI,EAAE;YACd,GAAG,EAAE,EAAE;SACR,CAAA;QAED,IAAI,CAAC,KAAK,GAAG,IAAI,QAAQ,CAAC,cAAc,CAAC,SAAS,CAAC,EAAE,gBAAgB,OAAO,CAAC,IAAI,QAAQ,CAAC,CAAA;QAE1F,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAA;QACxB,IAAI,CAAC,OAAO,GAAG,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
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import { createRouterTransport, ConnectRouter, Interceptor, PromiseClient, UnaryRequest, StreamRequest } from '@connectrpc/connect';
|
|
2
|
-
import type { ServiceType } from '@bufbuild/protobuf';
|
|
1
|
+
import { createRouterTransport, ConnectRouter, Interceptor, PromiseClient, CallbackClient, UnaryRequest, StreamRequest } from '@connectrpc/connect';
|
|
2
|
+
import type { ServiceType, Message, PlainMessage } from '@bufbuild/protobuf';
|
|
3
3
|
import { createGrpcWebTransport } from '@bufbuild/connect-web';
|
|
4
4
|
import type { KeyInput } from '@nanostores/query';
|
|
5
5
|
import { nanoquery } from '@nanostores/query';
|
|
6
|
+
import { Atom } from 'nanostores';
|
|
7
|
+
import { ServerStreamSubscription, type ServerStreamSubscriptionOptions } from './subscription';
|
|
6
8
|
type KnownHeaders = {
|
|
7
9
|
authorization?: string;
|
|
8
10
|
sdk?: string;
|
|
11
|
+
'sl-device-id': string;
|
|
9
12
|
} & Record<string, string>;
|
|
10
13
|
export type GrpcTransport = Transport['transport'];
|
|
11
14
|
type ReservedHeaders = 'sdk' | 'authorization';
|
|
@@ -25,15 +28,26 @@ type NanoqueryObjectType = {
|
|
|
25
28
|
* transport wrapper, initialize grpc transport, store headers and connect interceptors
|
|
26
29
|
*/
|
|
27
30
|
export declare class Transport {
|
|
31
|
+
toJsonOptions: {
|
|
32
|
+
emitDefaultValues: boolean;
|
|
33
|
+
enumAsInteger: boolean;
|
|
34
|
+
useProtoFieldName: boolean;
|
|
35
|
+
};
|
|
28
36
|
readonly transport: ReturnType<typeof createGrpcWebTransport>;
|
|
29
37
|
readonly nanoquery: NanoqueryObjectType;
|
|
30
38
|
protected interceptors: Interceptor[];
|
|
31
39
|
private readonly $headers;
|
|
32
40
|
private clients;
|
|
41
|
+
private callbackClients;
|
|
42
|
+
private subscriptions;
|
|
33
43
|
constructor();
|
|
44
|
+
addSubscription: <T extends ServiceType, Req extends Message<Req>, Res extends Message<Res>>(method: CallbackClient<T>[import("./subscription").StreamMethod<T>], params: PlainMessage<Req> | Atom<PlainMessage<Req>>, options: ServerStreamSubscriptionOptions) => ServerStreamSubscription<ServiceType, Message<import("@bufbuild/protobuf").AnyMessage>, Message<import("@bufbuild/protobuf").AnyMessage>, never, never> | ServerStreamSubscription<T, Req, Res, import("./subscription").StreamMethod<T>, CallbackClient<T>[import("./subscription").StreamMethod<T>]>;
|
|
45
|
+
removeSubscription: (subscription: ServerStreamSubscription<ServiceType, Message, Message>) => void;
|
|
46
|
+
disconnect: () => void;
|
|
34
47
|
registerInterceptor: (interceptor: Interceptor) => void;
|
|
35
48
|
removeInterceptor: (interceptor: Interceptor) => void;
|
|
36
49
|
getClient: <T extends ServiceType>(service: T) => PromiseClient<T>;
|
|
50
|
+
getCallbackClient: <T extends ServiceType>(service: T) => CallbackClient<T>;
|
|
37
51
|
createPromiseClient: <T extends ServiceType>(service: T, { params, method }: {
|
|
38
52
|
params?: KeyInput | undefined;
|
|
39
53
|
method: keyof T["methods"];
|
|
@@ -42,6 +56,9 @@ export declare class Transport {
|
|
|
42
56
|
queryKey: ((string | number) | import("nanostores").ReadableAtom<(string | number) | (void | null | undefined)>)[];
|
|
43
57
|
queryKeyStr: string;
|
|
44
58
|
};
|
|
59
|
+
createCallbackClient: <T extends ServiceType>(service: T) => {
|
|
60
|
+
client: CallbackClient<T>;
|
|
61
|
+
};
|
|
45
62
|
setSdkKey: (sdkKey: string) => void;
|
|
46
63
|
setAuth: (token?: string) => void;
|
|
47
64
|
setHeader: <T extends string = string>(name: ExcludeReservedHeaders<T>, value: string) => void;
|
package/src/grpc/transport.js
CHANGED
|
@@ -1,113 +1,164 @@
|
|
|
1
1
|
import { MapStore, createMapStore } from '@streamlayer/sdk-web-interfaces';
|
|
2
|
-
import { createRouterTransport, createPromiseClient } from '@connectrpc/connect';
|
|
2
|
+
import { createRouterTransport, createPromiseClient, createCallbackClient, } from '@connectrpc/connect';
|
|
3
3
|
import { createGrpcWebTransport } from '@bufbuild/connect-web';
|
|
4
4
|
import { nanoquery } from '@nanostores/query';
|
|
5
|
+
import { __GRPC_DEVTOOLS_EXTENSION__ } from '../utils/devtools';
|
|
6
|
+
import { ServerStreamSubscription } from './subscription';
|
|
5
7
|
/**
|
|
6
8
|
* transport wrapper, initialize grpc transport, store headers and connect interceptors
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
25
|
-
this.createPromiseClient = (service, { params = [], method })=>{
|
|
26
|
-
const client = this.getClient(service);
|
|
27
|
-
const methodName = service.methods[method].name;
|
|
28
|
-
const queryKey = [
|
|
29
|
-
service.typeName,
|
|
30
|
-
methodName.charAt(0).toLowerCase() + methodName.slice(1),
|
|
31
|
-
...Array.isArray(params) ? params : [
|
|
32
|
-
params
|
|
33
|
-
]
|
|
34
|
-
];
|
|
35
|
-
return {
|
|
36
|
-
client,
|
|
37
|
-
queryKey,
|
|
38
|
-
queryKeyStr: queryKey.toString()
|
|
39
|
-
};
|
|
40
|
-
};
|
|
41
|
-
this.setSdkKey = (sdkKey)=>{
|
|
42
|
-
this.$headers.setValue('sdk', sdkKey);
|
|
43
|
-
};
|
|
44
|
-
this.setAuth = (token)=>{
|
|
45
|
-
this.$headers.setValue('authorization', token);
|
|
46
|
-
};
|
|
47
|
-
this.setHeader = (name, value)=>this.$headers.setValue(name, value);
|
|
48
|
-
this.getHeader = (name)=>this.$headers.getValue(name);
|
|
49
|
-
this.getHeaders = ()=>this.$headers.getValues();
|
|
50
|
-
this.initInterceptors = ()=>{
|
|
51
|
-
if (this.interceptors.length !== 0) {
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
const auth = (next)=>async (req)=>{
|
|
55
|
-
const headers = this.$headers.getValues();
|
|
56
|
-
for(const header in headers){
|
|
57
|
-
req.header.set(header, headers[header]);
|
|
58
|
-
}
|
|
59
|
-
try {
|
|
60
|
-
return await next(req);
|
|
61
|
-
} catch (err) {
|
|
62
|
-
// eslint-disable-next-line no-console
|
|
63
|
-
console.log({
|
|
64
|
-
err
|
|
65
|
-
}, 'catch err') // logout and something like this
|
|
66
|
-
;
|
|
67
|
-
throw err;
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
this.interceptors.push(auth);
|
|
71
|
-
if (window.__GRPC_DEVTOOLS_EXTENSION__) {
|
|
72
|
-
this.interceptors.push(window.__GRPC_DEVTOOLS_EXTENSION__());
|
|
73
|
-
} else {
|
|
74
|
-
window.addEventListener('grpc_devtools_loaded', ()=>{
|
|
75
|
-
if (window.__GRPC_DEVTOOLS_EXTENSION__) {
|
|
76
|
-
this.interceptors.push(window.__GRPC_DEVTOOLS_EXTENSION__());
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
this.$headers = new MapStore(createMapStore({}));
|
|
9
|
+
*/
|
|
10
|
+
export class Transport {
|
|
11
|
+
toJsonOptions = {
|
|
12
|
+
emitDefaultValues: false,
|
|
13
|
+
enumAsInteger: true,
|
|
14
|
+
useProtoFieldName: false,
|
|
15
|
+
};
|
|
16
|
+
transport;
|
|
17
|
+
nanoquery;
|
|
18
|
+
interceptors = [];
|
|
19
|
+
$headers;
|
|
20
|
+
clients;
|
|
21
|
+
callbackClients;
|
|
22
|
+
subscriptions;
|
|
23
|
+
constructor() {
|
|
24
|
+
this.$headers = new MapStore(createMapStore({
|
|
25
|
+
['sl-device-id']: process.env.NX_DEVICE_ID || 'sdk-web-dev',
|
|
26
|
+
}), 'transport:headers');
|
|
82
27
|
this.initInterceptors();
|
|
83
28
|
this.clients = new Map();
|
|
29
|
+
this.callbackClients = new Map();
|
|
30
|
+
this.subscriptions = new Map();
|
|
84
31
|
const [createFetcherStore, createMutatorStore, utils] = nanoquery();
|
|
85
|
-
this.nanoquery = {
|
|
86
|
-
createFetcherStore,
|
|
87
|
-
createMutatorStore,
|
|
88
|
-
utils
|
|
89
|
-
};
|
|
32
|
+
this.nanoquery = { createFetcherStore, createMutatorStore, utils };
|
|
90
33
|
this.transport = createGrpcWebTransport({
|
|
91
34
|
baseUrl: process.env.NX_GRPC_HOST || 'https://grpc-web.next.streamlayer.io:443',
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
36
|
+
// @ts-ignore
|
|
92
37
|
interceptors: this.interceptors,
|
|
93
|
-
useBinaryFormat: true
|
|
38
|
+
useBinaryFormat: true,
|
|
94
39
|
});
|
|
95
40
|
}
|
|
41
|
+
// use shared request params, based on Atom, each new Atom will create new subscription
|
|
42
|
+
// mutate Atom, subscription automatically reconnect with new params
|
|
43
|
+
// if headers changed, reconnect subscription with new headers automatically
|
|
44
|
+
addSubscription = (method, params, options) => {
|
|
45
|
+
const currentSubscription = this.subscriptions.get(params);
|
|
46
|
+
if (currentSubscription) {
|
|
47
|
+
return currentSubscription;
|
|
48
|
+
}
|
|
49
|
+
const subscription = new ServerStreamSubscription(this.$headers, method, params, options);
|
|
50
|
+
// ToDo: fix types
|
|
51
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
52
|
+
// @ts-ignore
|
|
53
|
+
this.subscriptions.set(params, subscription);
|
|
54
|
+
return subscription;
|
|
55
|
+
};
|
|
56
|
+
removeSubscription = (subscription) => {
|
|
57
|
+
subscription.disconnect();
|
|
58
|
+
this.subscriptions.delete(subscription.params);
|
|
59
|
+
};
|
|
60
|
+
// cleanup subscriptions
|
|
61
|
+
disconnect = () => {
|
|
62
|
+
for (const [name, subscription] of this.subscriptions) {
|
|
63
|
+
subscription.disconnect();
|
|
64
|
+
this.subscriptions.delete(name);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
registerInterceptor = (interceptor) => {
|
|
68
|
+
this.interceptors.push(interceptor);
|
|
69
|
+
};
|
|
70
|
+
removeInterceptor = (interceptor) => {
|
|
71
|
+
this.interceptors = this.interceptors.filter((i) => i !== interceptor);
|
|
72
|
+
};
|
|
73
|
+
getClient = (service) => {
|
|
74
|
+
const serviceName = service.typeName;
|
|
75
|
+
if (this.clients.has(serviceName)) {
|
|
76
|
+
return this.clients.get(serviceName);
|
|
77
|
+
}
|
|
78
|
+
const client = createPromiseClient(service, this.transport);
|
|
79
|
+
this.clients.set(serviceName, client);
|
|
80
|
+
return client;
|
|
81
|
+
};
|
|
82
|
+
getCallbackClient = (service) => {
|
|
83
|
+
const serviceName = service.typeName;
|
|
84
|
+
if (this.callbackClients.has(serviceName)) {
|
|
85
|
+
return this.callbackClients.get(serviceName);
|
|
86
|
+
}
|
|
87
|
+
const client = createCallbackClient(service, this.transport);
|
|
88
|
+
this.callbackClients.set(serviceName, client);
|
|
89
|
+
return client;
|
|
90
|
+
};
|
|
91
|
+
// create unary client, used for query request
|
|
92
|
+
createPromiseClient = (service, { params = [], method }) => {
|
|
93
|
+
const client = this.getClient(service);
|
|
94
|
+
const methodName = service.methods[method].name;
|
|
95
|
+
const queryKey = [
|
|
96
|
+
service.typeName,
|
|
97
|
+
methodName.charAt(0).toLowerCase() + methodName.slice(1),
|
|
98
|
+
...(Array.isArray(params) ? params : [params]),
|
|
99
|
+
];
|
|
100
|
+
const queryKeyWithoutParams = [service.typeName, methodName.charAt(0).toLowerCase() + methodName.slice(1)];
|
|
101
|
+
return { client, queryKey, queryKeyStr: queryKeyWithoutParams.join('') };
|
|
102
|
+
};
|
|
103
|
+
// create callback client, used for server stream subscriptions
|
|
104
|
+
createCallbackClient = (service) => {
|
|
105
|
+
const client = this.getCallbackClient(service);
|
|
106
|
+
return { client };
|
|
107
|
+
};
|
|
108
|
+
setSdkKey = (sdkKey) => {
|
|
109
|
+
this.$headers.setValue('sdk', sdkKey);
|
|
110
|
+
};
|
|
111
|
+
setAuth = (token) => {
|
|
112
|
+
this.$headers.setValue('authorization', token);
|
|
113
|
+
};
|
|
114
|
+
setHeader = (name, value) => this.$headers.setValue(name, value);
|
|
115
|
+
getHeader = (name) => this.$headers.getValue(name);
|
|
116
|
+
getHeaders = () => this.$headers.getValues();
|
|
117
|
+
initInterceptors = () => {
|
|
118
|
+
if (this.interceptors.length !== 0) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const auth = (next) => async (req) => {
|
|
122
|
+
const headers = this.$headers.getValues();
|
|
123
|
+
for (const header in headers) {
|
|
124
|
+
req.header.set(header, headers[header]);
|
|
125
|
+
}
|
|
126
|
+
try {
|
|
127
|
+
return await next(req);
|
|
128
|
+
}
|
|
129
|
+
catch (err) {
|
|
130
|
+
// eslint-disable-next-line no-console
|
|
131
|
+
console.log({ err, req }, 'catch err'); // logout and something like this
|
|
132
|
+
throw err;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
this.interceptors.push(auth);
|
|
136
|
+
this.interceptors.push(__GRPC_DEVTOOLS_EXTENSION__());
|
|
137
|
+
// if (window.__GRPC_DEVTOOLS_EXTENSION__) {
|
|
138
|
+
// this.interceptors.push(window.__GRPC_DEVTOOLS_EXTENSION__())
|
|
139
|
+
// } else {
|
|
140
|
+
// window.addEventListener('grpc_devtools_loaded', () => {
|
|
141
|
+
// if (window.__GRPC_DEVTOOLS_EXTENSION__) {
|
|
142
|
+
// this.interceptors.push(window.__GRPC_DEVTOOLS_EXTENSION__())
|
|
143
|
+
// }
|
|
144
|
+
// })
|
|
145
|
+
// }
|
|
146
|
+
};
|
|
96
147
|
}
|
|
97
148
|
export class MockTransport extends Transport {
|
|
98
|
-
|
|
149
|
+
calls;
|
|
150
|
+
constructor(transport) {
|
|
99
151
|
super();
|
|
100
152
|
this.calls = [];
|
|
101
|
-
this.interceptors.push((next)=>(req)=>{
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
153
|
+
this.interceptors.push((next) => (req) => {
|
|
154
|
+
this.calls.push(req);
|
|
155
|
+
return next(req);
|
|
156
|
+
});
|
|
105
157
|
this.transport = createRouterTransport(transport, {
|
|
106
158
|
transport: {
|
|
107
|
-
interceptors: this.interceptors
|
|
108
|
-
}
|
|
159
|
+
interceptors: this.interceptors,
|
|
160
|
+
},
|
|
109
161
|
});
|
|
110
162
|
}
|
|
111
163
|
}
|
|
112
|
-
|
|
113
164
|
//# sourceMappingURL=transport.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../packages/sdk-web-api/src/grpc/transport.ts"],"
|
|
1
|
+
{"version":3,"file":"transport.js","sourceRoot":"","sources":["../../../../../packages/sdk-web-api/src/grpc/transport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAA;AAE1E,OAAO,EACL,qBAAqB,EAGrB,mBAAmB,EAEnB,oBAAoB,GAIrB,MAAM,qBAAqB,CAAA;AAE5B,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAA;AAE9D,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAG7C,OAAO,EAAE,2BAA2B,EAAE,MAAM,mBAAmB,CAAA;AAE/D,OAAO,EAAE,wBAAwB,EAAwC,MAAM,gBAAgB,CAAA;AAyB/F;;GAEG;AACH,MAAM,OAAO,SAAS;IACpB,aAAa,GAAG;QACd,iBAAiB,EAAE,KAAK;QACxB,aAAa,EAAE,IAAI;QACnB,iBAAiB,EAAE,KAAK;KACzB,CAAA;IAEe,SAAS,CAA2C;IACpD,SAAS,CAAqB;IAEpC,YAAY,GAAkB,EAAE,CAAA;IACzB,QAAQ,CAAwB;IACzC,OAAO,CAA+D;IACtE,eAAe,CAAgE;IAC/E,aAAa,CAGpB;IAED;QACE,IAAI,CAAC,QAAQ,GAAG,IAAI,QAAQ,CAC1B,cAAc,CAAC;YACb,CAAC,cAAc,CAAC,EAAE,OAAO,CAAC,GAAG,CAAC,YAAY,IAAI,aAAa;SAC5D,CAAC,EACF,mBAAmB,CACp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MAAM,GAAG,CAAA;aACV;QACH,CAAC,CAAA;QAED,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QAC5B,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,2BAA2B,EAAE,CAAC,CAAA;QAErD,4CAA4C;QAC5C,iEAAiE;QACjE,WAAW;QACX,4DAA4D;QAC5D,gDAAgD;QAChD,qEAAqE;QACrE,QAAQ;QACR,OAAO;QACP,IAAI;IACN,CAAC,CAAA;CACF;AAED,MAAM,OAAO,aAAc,SAAQ,SAAS;IAEnC,KAAK,CAAqC;IAEjD,YAAY,SAA0C;QACpD,KAAK,EAAE,CAAA;QAEP,IAAI,CAAC,KAAK,GAAG,EAAE,CAAA;QACf,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,GAAG,EAAE,EAAE;YACvC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;YAEpB,OAAO,IAAI,CAAC,GAAG,CAAC,CAAA;QAClB,CAAC,CAAC,CAAA;QACF,IAAI,CAAC,SAAS,GAAG,qBAAqB,CAAC,SAAS,EAAE;YAChD,SAAS,EAAE;gBACT,YAAY,EAAE,IAAI,CAAC,YAAY;aAChC;SACF,CAAC,CAAA;IACJ,CAAC;CACF"}
|
package/src/index.d.ts
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
|
+
import { StreamLayerContext } from '@streamlayer/sdk-web-interfaces';
|
|
1
2
|
import { FetcherStore } from '@nanostores/query';
|
|
3
|
+
export type { ServerStreamSubscriptionOptions } from './grpc/subscription';
|
|
2
4
|
export { Transport } from './grpc/transport';
|
|
5
|
+
import { Transport } from './grpc/transport';
|
|
3
6
|
export type { GrpcTransport } from './grpc/transport';
|
|
4
7
|
export * as queries from './grpc/queries';
|
|
5
8
|
export type GetApiResponseType<T extends (...args: any[]) => FetcherStore> = ReturnType<ReturnType<T>['get']>['data'];
|
|
9
|
+
declare module '@streamlayer/sdk-web-interfaces' {
|
|
10
|
+
interface StreamLayerContext {
|
|
11
|
+
transport: Transport;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export declare const transport: (instance: StreamLayerContext, opts: {
|
|
15
|
+
sdkKey: string;
|
|
16
|
+
}, done: () => void) => void;
|
package/src/index.js
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
export { Transport } from './grpc/transport';
|
|
2
|
+
import { Transport } from './grpc/transport';
|
|
2
3
|
export * as queries from './grpc/queries';
|
|
3
|
-
|
|
4
|
+
export const transport = (instance, opts, done) => {
|
|
5
|
+
instance.transport = new Transport();
|
|
6
|
+
instance.transport.setSdkKey(opts.sdkKey);
|
|
7
|
+
done();
|
|
8
|
+
};
|
|
4
9
|
//# sourceMappingURL=index.js.map
|
package/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../packages/sdk-web-api/src/index.ts"],"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/sdk-web-api/src/index.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAE5C,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAE5C,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAA;AASzC,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,QAA4B,EAAE,IAAwB,EAAE,IAAgB,EAAE,EAAE;IACpG,QAAQ,CAAC,SAAS,GAAG,IAAI,SAAS,EAAE,CAAA;IACpC,QAAQ,CAAC,SAAS,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;IACzC,IAAI,EAAE,CAAA;AACR,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const __GRPC_DEVTOOLS_EXTENSION__: any;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
2
|
+
// @ts-nocheck
|
|
3
|
+
async function* logEach(store, stream) {
|
|
4
|
+
for await (const m of stream) {
|
|
5
|
+
store.response.message = m;
|
|
6
|
+
store.received_at = Date.now();
|
|
7
|
+
const msg = {
|
|
8
|
+
type: '__GRPC_DEVTOOLS_EXTENSION__',
|
|
9
|
+
data: store,
|
|
10
|
+
};
|
|
11
|
+
window.postMessage(msg);
|
|
12
|
+
yield m;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
16
|
+
export const __GRPC_DEVTOOLS_EXTENSION__ = () => (next) => async (request) => {
|
|
17
|
+
const store = {
|
|
18
|
+
name: request.url,
|
|
19
|
+
request: {},
|
|
20
|
+
response: {},
|
|
21
|
+
};
|
|
22
|
+
store.request.header = Object.fromEntries(request.header.entries());
|
|
23
|
+
store.sent_at = Date.now();
|
|
24
|
+
try {
|
|
25
|
+
const response = await next(request);
|
|
26
|
+
store.received_at = Date.now();
|
|
27
|
+
store.stream = response.stream;
|
|
28
|
+
store.response.header = Object.fromEntries(response.header.entries());
|
|
29
|
+
store.response.trailer = Object.fromEntries(response.trailer.entries());
|
|
30
|
+
if (!response.stream) {
|
|
31
|
+
store.request.message = request.message;
|
|
32
|
+
store.response.message = response.message;
|
|
33
|
+
store.latency = store.received_at - store.sent_at;
|
|
34
|
+
const msg = {
|
|
35
|
+
type: '__GRPC_DEVTOOLS_EXTENSION__',
|
|
36
|
+
data: store,
|
|
37
|
+
};
|
|
38
|
+
window.postMessage(msg);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
return {
|
|
42
|
+
...response,
|
|
43
|
+
message: logEach(store, response.message),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
return response;
|
|
47
|
+
}
|
|
48
|
+
catch (e) {
|
|
49
|
+
store.received_at = Date.now();
|
|
50
|
+
store.request.message = request.message;
|
|
51
|
+
store.response.trailer = {
|
|
52
|
+
['grpc-status']: e.code,
|
|
53
|
+
['grpc-message']: e.rawMessage,
|
|
54
|
+
};
|
|
55
|
+
store.response.message = e.rawMessage;
|
|
56
|
+
store.latency = store.received_at - store.sent_at;
|
|
57
|
+
const msg = {
|
|
58
|
+
type: '__GRPC_DEVTOOLS_EXTENSION__',
|
|
59
|
+
data: store,
|
|
60
|
+
};
|
|
61
|
+
window.postMessage(msg);
|
|
62
|
+
throw e;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
window.dispatchEvent(new CustomEvent('grpc_devtools_loaded'));
|
|
66
|
+
//# sourceMappingURL=devtools.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"devtools.js","sourceRoot":"","sources":["../../../../../packages/sdk-web-api/src/utils/devtools.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAC7D,cAAc;AACd,KAAK,SAAS,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,MAAM;IACnC,IAAI,KAAK,EAAE,MAAM,CAAC,IAAI,MAAM,EAAE;QAC5B,KAAK,CAAC,QAAQ,CAAC,OAAO,GAAG,CAAC,CAAA;QAC1B,KAAK,CAAC,WAAW,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;QAC9B,MAAM,GAAG,GAAG;YACV,IAAI,EAAE,6BAA6B;YACnC,IAAI,EAAE,KAAK;SACZ,CAAA;QAED,MAAM,CAAC,WAAW,CAAC,GAAG,CAAC,CAAA;QAEvB,MAAM,CAAC,CAAA;KACR;AACH,CAAC;AAED,8DAA8D;AAC9D,MAAM,CAAC,MAAM,2BAA2B,GAAQ,GAAG,EAAE,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE;IAChF,MAAM,KAAK,GAAG;QACZ,IAAI,EAAE,OAAO,CAAC,GAAG;QACjB,OAAO,EAAE,EAAE;QACX,QAAQ,EAAE,EAAE;KACb,CAAA;IAED,KAAK,CAAC,OAAO,CAAC,MAAM,GAAG,MAAM,CAAC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AAC,OAAO,CAAA;QACvC,KAAK,CAAC,QAAQ,CAAC,OAAO,GAAG;YACvB,CAAC,aAAa,CAAC,EAAE,CAAC,CAAC,IAAI;YACvB,CAAC,cAAc,CAAC,EAAE,CAAC,CAAC,UAAU;SAC/B,CAAA;QACD,KAAK,CAAC,QAAQ,CAAC,OAAO,GAAG,CAAC,CAAC,UAAU,CAAA;QACrC,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC,WAAW,GAAG,KAAK,CAAC,OAAO,CAAA;QACjD,MAAM,GAAG,GAAG;YACV,IAAI,EAAE,6BAA6B;YACnC,IAAI,EAAE,KAAK;SACZ,CAAA;QAED,MAAM,CAAC,WAAW,CAAC,GAAG,CAAC,CAAA;QAEvB,MAAM,CAAC,CAAA;KACR;AACH,CAAC,CAAA;AAED,MAAM,CAAC,aAAa,CAAC,IAAI,WAAW,CAAC,sBAAsB,CAAC,CAAC,CAAA"}
|
package/src/utils/grpc-stub.js
CHANGED
|
@@ -1,36 +1,24 @@
|
|
|
1
1
|
import { Transport } from '..';
|
|
2
2
|
export class GrpcStub {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
};
|
|
3
|
+
transport;
|
|
4
|
+
stubs;
|
|
5
|
+
constructor() {
|
|
7
6
|
this.transport = new Transport();
|
|
8
7
|
this.stubs = new Map();
|
|
9
|
-
const stubbedUnary = (service, method, signal, timeoutMs, header, input)=>{
|
|
8
|
+
const stubbedUnary = (service, method, signal, timeoutMs, header, input) => {
|
|
10
9
|
const key = `${service.typeName}${method.name}`.toLowerCase();
|
|
11
|
-
console.log('grpc unary', {
|
|
12
|
-
|
|
13
|
-
input
|
|
14
|
-
});
|
|
15
|
-
for (const [stubKey, stubValue] of this.stubs){
|
|
10
|
+
console.log('grpc unary', { key, input });
|
|
11
|
+
for (const [stubKey, stubValue] of this.stubs) {
|
|
16
12
|
if (stubKey === key) {
|
|
17
|
-
return {
|
|
18
|
-
message: stubValue
|
|
19
|
-
};
|
|
13
|
+
return { message: stubValue };
|
|
20
14
|
}
|
|
21
15
|
}
|
|
22
|
-
throw new Error(JSON.stringify({
|
|
23
|
-
message: 'not stubbed correctly',
|
|
24
|
-
service,
|
|
25
|
-
method,
|
|
26
|
-
signal,
|
|
27
|
-
timeoutMs,
|
|
28
|
-
header,
|
|
29
|
-
input
|
|
30
|
-
}));
|
|
16
|
+
throw new Error(JSON.stringify({ message: 'not stubbed correctly', service, method, signal, timeoutMs, header, input }));
|
|
31
17
|
};
|
|
32
18
|
this.transport.transport.unary = stubbedUnary;
|
|
33
19
|
}
|
|
20
|
+
stub = (method, response) => {
|
|
21
|
+
this.stubs.set(method.toLowerCase(), response);
|
|
22
|
+
};
|
|
34
23
|
}
|
|
35
|
-
|
|
36
24
|
//# sourceMappingURL=grpc-stub.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../packages/sdk-web-api/src/utils/grpc-stub.ts"],"
|
|
1
|
+
{"version":3,"file":"grpc-stub.js","sourceRoot":"","sources":["../../../../../packages/sdk-web-api/src/utils/grpc-stub.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,IAAI,CAAA;AAI9B,MAAM,OAAO,QAAQ;IACH,SAAS,CAAW;IAC5B,KAAK,CAAsC;IAEnD;QACE,IAAI,CAAC,SAAS,GAAG,IAAI,SAAS,EAAE,CAAA;QAChC,IAAI,CAAC,KAAK,GAAG,IAAI,GAAG,EAAE,CAAA;QAEtB,MAAM,YAAY,GAAU,CAC1B,OAA6B,EAC7B,MAA4B,EAC5B,MAA4B,EAC5B,SAA+B,EAC/B,MAA4B,EAC5B,KAA2B,EAC3B,EAAE;YACF,MAAM,GAAG,GAAG,GAAG,OAAO,CAAC,QAAQ,GAAG,MAAM,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAA;YAE7D,OAAO,CAAC,GAAG,CAAC,YAAY,EAAE,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC,CAAA;YAEzC,KAAK,MAAM,CAAC,OAAO,EAAE,SAAS,CAAC,IAAI,IAAI,CAAC,KAAK,EAAE;gBAC7C,IAAI,OAAO,KAAK,GAAG,EAAE;oBACnB,OAAO,EAAE,OAAO,EAAE,SAAS,EAA6C,CAAA;iBACzE;aACF;YAED,MAAM,IAAI,KAAK,CACb,IAAI,CAAC,SAAS,CAAC,EAAE,OAAO,EAAE,uBAAuB,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CACxG,CAAA;QACH,CAAC,CAAA;QAED,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,KAAK,GAAG,YAAY,CAAA;IAC/C,CAAC;IAED,IAAI,GAAG,CAAC,MAAc,EAAE,QAAiC,EAAE,EAAE;QAC3D,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,WAAW,EAAE,EAAE,QAAQ,CAAC,CAAA;IAChD,CAAC,CAAA;CACF"}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { ApiStore, createSingleStore } from '@streamlayer/sdk-web-interfaces';
|
|
2
|
-
import { Events } from '@streamlayer/sl-eslib/streamlayer/sports/events/events_connect';
|
|
3
|
-
import { MockTransport } from '../transport';
|
|
4
|
-
import { $retrieveEventId } from './event';
|
|
5
|
-
describe('Simple requests test', ()=>{
|
|
6
|
-
const ctx = {
|
|
7
|
-
providerStreamId: createSingleStore('')
|
|
8
|
-
};
|
|
9
|
-
beforeAll(()=>{
|
|
10
|
-
const mock = ({ service })=>{
|
|
11
|
-
service(Events, {
|
|
12
|
-
retrieveEventId: ()=>({
|
|
13
|
-
data: {
|
|
14
|
-
id: 123
|
|
15
|
-
}
|
|
16
|
-
})
|
|
17
|
-
});
|
|
18
|
-
};
|
|
19
|
-
ctx.transport = new MockTransport(mock);
|
|
20
|
-
ctx.api = new ApiStore($retrieveEventId(ctx.providerStreamId, ctx.transport));
|
|
21
|
-
ctx.api.listen(()=>{});
|
|
22
|
-
});
|
|
23
|
-
afterAll(()=>{
|
|
24
|
-
ctx.api.off();
|
|
25
|
-
});
|
|
26
|
-
it('returns empty sl event id if provider id not defined', async ()=>{
|
|
27
|
-
const emptyEvent = await ctx.api.getValue();
|
|
28
|
-
expect(emptyEvent).toEqual('');
|
|
29
|
-
});
|
|
30
|
-
it('returns sl event id if provider id defined', async ()=>{
|
|
31
|
-
ctx.providerStreamId.set('demo:event');
|
|
32
|
-
const resolvedEvent = await ctx.api.getValue();
|
|
33
|
-
expect(resolvedEvent).toEqual('123');
|
|
34
|
-
});
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
//# sourceMappingURL=queries.test.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../../packages/sdk-web-api/src/grpc/queries/queries.test.ts"],"sourcesContent":["import { ApiStore, createSingleStore } from '@streamlayer/sdk-web-interfaces'\n\nimport { Events } from '@streamlayer/sl-eslib/streamlayer/sports/events/events_connect'\n\nimport { MockTransport } from '../transport'\n\nimport { $retrieveEventId } from './event'\n\ndescribe('Simple requests test', () => {\n const ctx: {\n providerStreamId: ReturnType<typeof createSingleStore<string>>\n api?: ApiStore<ReturnType<typeof $retrieveEventId>>\n transport?: MockTransport\n } = {\n providerStreamId: createSingleStore(''),\n }\n\n beforeAll(() => {\n const mock = ({ service }) => {\n service(Events, {\n retrieveEventId: () => ({ data: { id: 123 } }),\n })\n }\n\n ctx.transport = new MockTransport(mock)\n\n ctx.api = new ApiStore($retrieveEventId(ctx.providerStreamId, ctx.transport))\n ctx.api.listen(() => {})\n })\n\n afterAll(() => {\n ctx.api.off()\n })\n\n it('returns empty sl event id if provider id not defined', async () => {\n const emptyEvent = await ctx.api.getValue()\n\n expect(emptyEvent).toEqual('')\n })\n\n it('returns sl event id if provider id defined', async () => {\n ctx.providerStreamId.set('demo:event')\n const resolvedEvent = await ctx.api.getValue()\n\n expect(resolvedEvent).toEqual('123')\n })\n})\n"],"names":["ApiStore","createSingleStore","Events","MockTransport","$retrieveEventId","describe","ctx","providerStreamId","beforeAll","mock","service","retrieveEventId","data","id","transport","api","listen","afterAll","off","it","emptyEvent","getValue","expect","toEqual","set","resolvedEvent"],"mappings":"AAAA,SAASA,QAAQ,EAAEC,iBAAiB,QAAQ,kCAAiC;AAE7E,SAASC,MAAM,QAAQ,iEAAgE;AAEvF,SAASC,aAAa,QAAQ,eAAc;AAE5C,SAASC,gBAAgB,QAAQ,UAAS;AAE1CC,SAAS,wBAAwB;IAC/B,MAAMC,MAIF;QACFC,kBAAkBN,kBAAkB;IACtC;IAEAO,UAAU;QACR,MAAMC,OAAO,CAAC,EAAEC,OAAO,EAAE;YACvBA,QAAQR,QAAQ;gBACdS,iBAAiB,IAAO,CAAA;wBAAEC,MAAM;4BAAEC,IAAI;wBAAI;oBAAE,CAAA;YAC9C;QACF;QAEAP,IAAIQ,SAAS,GAAG,IAAIX,cAAcM;QAElCH,IAAIS,GAAG,GAAG,IAAIf,SAASI,iBAAiBE,IAAIC,gBAAgB,EAAED,IAAIQ,SAAS;QAC3ER,IAAIS,GAAG,CAACC,MAAM,CAAC,KAAO;IACxB;IAEAC,SAAS;QACPX,IAAIS,GAAG,CAACG,GAAG;IACb;IAEAC,GAAG,wDAAwD;QACzD,MAAMC,aAAa,MAAMd,IAAIS,GAAG,CAACM,QAAQ;QAEzCC,OAAOF,YAAYG,OAAO,CAAC;IAC7B;IAEAJ,GAAG,8CAA8C;QAC/Cb,IAAIC,gBAAgB,CAACiB,GAAG,CAAC;QACzB,MAAMC,gBAAgB,MAAMnB,IAAIS,GAAG,CAACM,QAAQ;QAE5CC,OAAOG,eAAeF,OAAO,CAAC;IAChC;AACF"}
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { Health } from '@streamlayer/sl-eslib/grpc/health/v1/health_connect';
|
|
2
|
-
import { MockTransport } from './transport';
|
|
3
|
-
describe('Transport', ()=>{
|
|
4
|
-
let transport;
|
|
5
|
-
beforeAll(()=>{
|
|
6
|
-
const mock = ({ service })=>{
|
|
7
|
-
service(Health, {
|
|
8
|
-
check: ()=>({
|
|
9
|
-
status: 1
|
|
10
|
-
})
|
|
11
|
-
});
|
|
12
|
-
};
|
|
13
|
-
transport = new MockTransport(mock);
|
|
14
|
-
});
|
|
15
|
-
it('should initialize with default values', ()=>{
|
|
16
|
-
expect(transport).toBeDefined();
|
|
17
|
-
expect(transport.transport).toBeDefined();
|
|
18
|
-
});
|
|
19
|
-
it('should set SDK key', ()=>{
|
|
20
|
-
const sdkKey = 'new-sdk-key';
|
|
21
|
-
transport.setSdkKey(sdkKey);
|
|
22
|
-
const headers = transport.getHeaders();
|
|
23
|
-
expect(headers.sdk).toEqual(sdkKey);
|
|
24
|
-
});
|
|
25
|
-
it('should set authorization token', ()=>{
|
|
26
|
-
const authToken = 'my-auth-token';
|
|
27
|
-
transport.setAuth(authToken);
|
|
28
|
-
const headers = transport.getHeaders();
|
|
29
|
-
expect(headers.authorization).toEqual(authToken);
|
|
30
|
-
});
|
|
31
|
-
it('should set custom header', ()=>{
|
|
32
|
-
const headerName = 'custom-header';
|
|
33
|
-
const headerValue = 'custom-value';
|
|
34
|
-
transport.setHeader(headerName, headerValue);
|
|
35
|
-
const headers = transport.getHeaders();
|
|
36
|
-
expect(headers[headerName]).toEqual(headerValue);
|
|
37
|
-
});
|
|
38
|
-
it('should get a specific header', ()=>{
|
|
39
|
-
const headerName = 'custom-header';
|
|
40
|
-
const headerValue = 'custom-value';
|
|
41
|
-
transport.setHeader(headerName, headerValue);
|
|
42
|
-
const retrievedValue = transport.getHeader(headerName);
|
|
43
|
-
expect(retrievedValue).toEqual(headerValue);
|
|
44
|
-
});
|
|
45
|
-
it('should add/remove an interceptor', ()=>{
|
|
46
|
-
const interceptor = (req)=>req;
|
|
47
|
-
transport.registerInterceptor(interceptor);
|
|
48
|
-
expect(transport['interceptors']).toContain(interceptor);
|
|
49
|
-
transport.removeInterceptor(interceptor);
|
|
50
|
-
expect(transport['interceptors']).not.toContain(interceptor);
|
|
51
|
-
});
|
|
52
|
-
it('should create and cache promise client', ()=>{
|
|
53
|
-
const { client, queryKey } = transport.createPromiseClient(Health, {
|
|
54
|
-
method: 'check'
|
|
55
|
-
});
|
|
56
|
-
expect(client).toBeDefined();
|
|
57
|
-
expect(client.check).toBeDefined();
|
|
58
|
-
expect(queryKey).toBeDefined();
|
|
59
|
-
const { client: newClient, queryKey: newQueryKey } = transport.createPromiseClient(Health, {
|
|
60
|
-
method: 'check'
|
|
61
|
-
});
|
|
62
|
-
expect(client === newClient);
|
|
63
|
-
expect(queryKey !== newQueryKey);
|
|
64
|
-
});
|
|
65
|
-
it('should use all headers from store', async ()=>{
|
|
66
|
-
const { client } = transport.createPromiseClient(Health, {
|
|
67
|
-
method: 'check'
|
|
68
|
-
});
|
|
69
|
-
const res = await client.check({});
|
|
70
|
-
expect(res?.status);
|
|
71
|
-
expect(res.status).toEqual(1);
|
|
72
|
-
const lastCall = transport.calls.pop();
|
|
73
|
-
const headers = transport.getHeaders();
|
|
74
|
-
expect(lastCall).toBeDefined();
|
|
75
|
-
for(const header in headers){
|
|
76
|
-
expect(lastCall?.header.get(header)).toEqual(headers[header]);
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
//# sourceMappingURL=transport.test.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../packages/sdk-web-api/src/grpc/transport.test.ts"],"sourcesContent":["import { Health } from '@streamlayer/sl-eslib/grpc/health/v1/health_connect'\n\nimport { MockTransport } from './transport'\n\ndescribe('Transport', () => {\n let transport: MockTransport\n\n beforeAll(() => {\n const mock = ({ service }) => {\n service(Health, {\n check: () => ({ status: 1 }),\n })\n }\n\n transport = new MockTransport(mock)\n })\n\n it('should initialize with default values', () => {\n expect(transport).toBeDefined()\n expect(transport.transport).toBeDefined()\n })\n\n it('should set SDK key', () => {\n const sdkKey = 'new-sdk-key'\n\n transport.setSdkKey(sdkKey)\n const headers = transport.getHeaders()\n\n expect(headers.sdk).toEqual(sdkKey)\n })\n\n it('should set authorization token', () => {\n const authToken = 'my-auth-token'\n\n transport.setAuth(authToken)\n const headers = transport.getHeaders()\n\n expect(headers.authorization).toEqual(authToken)\n })\n\n it('should set custom header', () => {\n const headerName = 'custom-header'\n const headerValue = 'custom-value'\n\n transport.setHeader(headerName, headerValue)\n const headers = transport.getHeaders()\n\n expect(headers[headerName]).toEqual(headerValue)\n })\n\n it('should get a specific header', () => {\n const headerName = 'custom-header'\n const headerValue = 'custom-value'\n\n transport.setHeader(headerName, headerValue)\n const retrievedValue = transport.getHeader(headerName)\n\n expect(retrievedValue).toEqual(headerValue)\n })\n\n it('should add/remove an interceptor', () => {\n const interceptor = (req) => req\n\n transport.registerInterceptor(interceptor)\n expect(transport['interceptors']).toContain(interceptor)\n\n transport.removeInterceptor(interceptor)\n expect(transport['interceptors']).not.toContain(interceptor)\n })\n\n it('should create and cache promise client', () => {\n const { client, queryKey } = transport.createPromiseClient(Health, { method: 'check' })\n\n expect(client).toBeDefined()\n expect(client.check).toBeDefined()\n expect(queryKey).toBeDefined()\n\n const { client: newClient, queryKey: newQueryKey } = transport.createPromiseClient(Health, { method: 'check' })\n\n expect(client === newClient)\n expect(queryKey !== newQueryKey)\n })\n\n it('should use all headers from store', async () => {\n const { client } = transport.createPromiseClient(Health, { method: 'check' })\n\n const res = await client.check({})\n\n expect(res?.status)\n expect(res.status).toEqual(1)\n\n const lastCall = transport.calls.pop()\n const headers = transport.getHeaders()\n\n expect(lastCall).toBeDefined()\n\n for (const header in headers) {\n expect(lastCall?.header.get(header)).toEqual(headers[header])\n }\n })\n})\n"],"names":["Health","MockTransport","describe","transport","beforeAll","mock","service","check","status","it","expect","toBeDefined","sdkKey","setSdkKey","headers","getHeaders","sdk","toEqual","authToken","setAuth","authorization","headerName","headerValue","setHeader","retrievedValue","getHeader","interceptor","req","registerInterceptor","toContain","removeInterceptor","not","client","queryKey","createPromiseClient","method","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