@streamlayer/sdk-web-api 0.1.0 → 0.3.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,15 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@streamlayer/sdk-web-api",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./src/index.js",
|
|
6
6
|
"typings": "./src/index.d.ts",
|
|
7
7
|
"files": [
|
|
8
8
|
"src/"
|
|
9
9
|
],
|
|
10
|
-
"scripts": {
|
|
11
|
-
"comp": "tsc --showConfig"
|
|
12
|
-
},
|
|
13
10
|
"dependencies": {
|
|
14
11
|
"@bufbuild/connect": "*",
|
|
15
12
|
"@bufbuild/connect-web": "*",
|
package/src/grpc/transport.d.ts
CHANGED
|
@@ -12,6 +12,9 @@ declare global {
|
|
|
12
12
|
__GRPC_DEVTOOLS_EXTENSION__?: () => import('@bufbuild/connect').Interceptor;
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* transport wrapper, initialize grpc transport, store headers and connect interceptors
|
|
17
|
+
*/
|
|
15
18
|
export declare class Transport {
|
|
16
19
|
readonly transport: ReturnType<typeof createGrpcWebTransport>;
|
|
17
20
|
private interceptors;
|
package/src/grpc/transport.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { MapStore, createMapStore } from '@streamlayer/sdk-web-interfaces';
|
|
2
2
|
import { createGrpcWebTransport } from '@bufbuild/connect-web';
|
|
3
3
|
const initialStore = {
|
|
4
|
-
sdk: process.env.SDK_KEY
|
|
4
|
+
sdk: process.env.SDK_KEY
|
|
5
5
|
};
|
|
6
|
-
|
|
6
|
+
/**
|
|
7
|
+
* transport wrapper, initialize grpc transport, store headers and connect interceptors
|
|
8
|
+
*/ export class Transport {
|
|
7
9
|
constructor(){
|
|
8
10
|
this.interceptors = new Set();
|
|
9
11
|
this.registerInterceptor = (interceptor)=>{
|
|
@@ -53,7 +55,7 @@ export class Transport {
|
|
|
53
55
|
this.$headers = new MapStore(createMapStore(initialStore));
|
|
54
56
|
this.initInterceptors();
|
|
55
57
|
this.transport = createGrpcWebTransport({
|
|
56
|
-
baseUrl: process.env.GRPC_HOST
|
|
58
|
+
baseUrl: process.env.GRPC_HOST,
|
|
57
59
|
interceptors: [
|
|
58
60
|
...this.interceptors.values()
|
|
59
61
|
],
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../packages/sdk-web-api/src/grpc/transport.ts"],"sourcesContent":["import { MapStore, createMapStore } from '@streamlayer/sdk-web-interfaces'\n\nimport { Interceptor } from '@bufbuild/connect'\nimport { createGrpcWebTransport } from '@bufbuild/connect-web'\n\ntype KnownHeaders = {\n authorization?: string\n sdk?: string\n} & Record<string, string>\n\nconst initialStore: KnownHeaders = {\n sdk
|
|
1
|
+
{"version":3,"sources":["../../../../../packages/sdk-web-api/src/grpc/transport.ts"],"sourcesContent":["import { MapStore, createMapStore } from '@streamlayer/sdk-web-interfaces'\n\nimport { Interceptor } from '@bufbuild/connect'\nimport { createGrpcWebTransport } from '@bufbuild/connect-web'\n\ntype KnownHeaders = {\n authorization?: string\n sdk?: string\n} & Record<string, string>\n\nconst initialStore: KnownHeaders = {\n sdk: process.env.SDK_KEY,\n}\n\nexport type GrpcTransport = Transport['transport']\n\ntype ReservedHeaders = 'sdk' | 'authorization'\ntype ExcludeReservedHeaders<T> = ReservedHeaders extends T\n ? never\n : T & T extends ReservedHeaders\n ? never\n : T\n\ndeclare global {\n interface Window {\n __GRPC_DEVTOOLS_EXTENSION__?: () => import('@bufbuild/connect').Interceptor\n }\n}\n\n/**\n * transport wrapper, initialize grpc transport, store headers and connect interceptors\n */\nexport class Transport {\n public readonly transport: ReturnType<typeof createGrpcWebTransport>\n private interceptors: Set<Interceptor> = new Set()\n private readonly $headers: MapStore<KnownHeaders>\n\n constructor() {\n this.$headers = new MapStore<KnownHeaders>(createMapStore(initialStore))\n this.initInterceptors()\n\n this.transport = createGrpcWebTransport({\n baseUrl:\n process.env.GRPC_HOST,\n interceptors: [...this.interceptors.values()],\n useBinaryFormat: true,\n })\n }\n\n registerInterceptor = (interceptor: Interceptor) => {\n this.interceptors.add(interceptor)\n }\n\n removeInterceptor = (interceptor: Interceptor) => {\n this.interceptors.delete(interceptor)\n }\n\n setSdkKey = (sdkKey: string) => {\n this.$headers.setValue('sdk', sdkKey)\n }\n\n setAuth = (token: string) => {\n this.$headers.setValue('authorization', token)\n }\n\n setHeader = <T extends keyof KnownHeaders = string>(\n name: ExcludeReservedHeaders<T>,\n value: string\n ) => this.$headers.setValue<string>(name, value)\n\n getHeader = (name: keyof KnownHeaders) => this.$headers.getValue(name)\n\n getHeaders = () => this.$headers.getValues()\n\n initInterceptors = () => {\n if (this.interceptors.size !== 0) {\n return\n }\n\n const auth: Interceptor = (next) => async (req) => {\n const headers = this.$headers.getValues()\n\n for (const header in headers) {\n req.header.set(header, headers[header])\n }\n\n try {\n return await next(req)\n } catch (err) {\n // eslint-disable-next-line no-console\n console.log('catch err') // logout and something like this\n\n throw err\n }\n }\n\n this.interceptors.add(auth)\n\n if (window.__GRPC_DEVTOOLS_EXTENSION__) {\n this.interceptors.add(window.__GRPC_DEVTOOLS_EXTENSION__())\n } else {\n window.addEventListener('grpc_devtools_loaded', () => {\n if (window.__GRPC_DEVTOOLS_EXTENSION__) {\n this.interceptors.add(window.__GRPC_DEVTOOLS_EXTENSION__())\n }\n })\n }\n }\n}\n"],"names":["MapStore","createMapStore","createGrpcWebTransport","initialStore","sdk","process","env","SDK_KEY","Transport","constructor","interceptors","Set","registerInterceptor","interceptor","add","removeInterceptor","delete","setSdkKey","sdkKey","$headers","setValue","setAuth","token","setHeader","name","value","getHeader","getValue","getHeaders","getValues","initInterceptors","size","auth","next","req","headers","header","set","err","console","log","window","__GRPC_DEVTOOLS_EXTENSION__","addEventListener","transport","base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kB;YAEtB,IAAIS,OAAOC,2BAA2B,EAAE;gBACtC,IAAI,CAAChC,YAAY,CAACI,GAAG,CAAC2B,OAAOC,2BAA2B;YAC1D,OAAO;gBACLD,OAAOE,gBAAgB,CAAC,wBAAwB;oBAC9C,IAAIF,OAAOC,2BAA2B,EAAE;wBACtC,IAAI,CAAChC,YAAY,CAACI,GAAG,CAAC2B,OAAOC,2BAA2B;oBAC1D;gBACF;YACF;QACF;QArEE,IAAI,CAACvB,QAAQ,GAAG,IAAInB,SAAuBC,eAAeE;QAC1D,IAAI,CAAC2B,gBAAgB;QAErB,IAAI,CAACc,SAAS,GAAG1C,uBAAuB;YACtC2C,SACExC,QAAQC,GAAG,CAACwC,SAAS;YACvBpC,cAAc;mBAAI,IAAI,CAACA,YAAY,CAACqC,MAAM;aAAG;YAC7CC,iBAAiB;QACnB;IACF;AA6DF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../packages/sdk-web-api/src/grpc/transport.test.ts"],"sourcesContent":["import { Transport } from './transport'\n\ndescribe('Transport', () => {\n it('init', () => {\n const transport = new Transport()\n\n expect(transport)\n expect(transport.transport)\n });\n});\n"],"names":["Transport","describe","it","transport","expect"],"mappings":"AAAA,SAASA,SAAS,QAAQ,cAAa;AAEvCC,SAAS,aAAa;IACpBC,GAAG,QAAQ;QACT,MAAMC,YAAY,IAAIH;QAEtBI,OAAOD;QACPC,OAAOD,UAAUA,SAAS;IAC5B;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../packages/sdk-web-api/src/index.test.ts"],"sourcesContent":["describe('My Test Suite', () => {\n it('should pass this test', () => {\n expect(true)\n });\n});\n"],"names":["describe","it","expect"],"mappings":"AAAAA,SAAS,iBAAiB;IACxBC,GAAG,yBAAyB;QAC1BC,OAAO;IACT;AACF"}
|