From 8337749594590d0e454af134a019d21f406e9dca Mon Sep 17 00:00:00 2001 From: Kuzminov Alexander Date: Tue, 6 Apr 2021 21:08:59 +0300 Subject: [PATCH] refactor: add meta in http --- readme.md | 13 +++++++++++-- src/http/http.ts | 12 ++++++------ src/http/types.ts | 25 +++++++++++++++---------- tests/demo.ts | 19 +++++++++++++++---- 4 files changed, 47 insertions(+), 22 deletions(-) diff --git a/readme.md b/readme.md index 40ffecf..9c268bf 100644 --- a/readme.md +++ b/readme.md @@ -168,7 +168,7 @@ export const grpc = grpcHTTP({ timeout: undefined, // Proxy xhr before request - transformRequest: ({ xhr }) => { + transformRequest: ({ xhr, data, meta }) => { xhr.setRequestHeader('Authorization', 'ANY_TOKEN'); }, @@ -182,7 +182,7 @@ export const grpc = grpcHTTP({ }, // Proxy result after request - transformResponse: ({ data }) => { + transformResponse: ({ xhr, data, meta }) => { if (!data.success) { console.log(data.error); } @@ -357,6 +357,12 @@ await grpc( #### All options ```ts +interface Meta { + token: string; +} + +const grpc = grpcHTTP(...); + const result = await grpc( whisk_api_user_v2_UserAPI_UpdateSettings, // gRPC method { ... }, // gRPC params @@ -366,6 +372,9 @@ const result = await grpc( onDownload: e => console.log(e.loaded / e.total), // download progress with ProgressEvent onUpload: e => console.log(e.loaded / e.total), // upload progress with ProgressEvent timeout: 2000, // number - timeout for this request with cancel request at the end + meta: { // Meta - data for transformRequest and transformResponse methods + token: 'CODE', + }, } ); ``` diff --git a/src/http/http.ts b/src/http/http.ts index 27cc144..5f8826f 100644 --- a/src/http/http.ts +++ b/src/http/http.ts @@ -18,7 +18,7 @@ export const grpcCancel = (): Cancel => { return CancelFn; }; -export const grpcHTTP = ({ +export const grpcHTTP = ({ server, transformRequest, transformResponse, @@ -27,7 +27,7 @@ export const grpcHTTP = ({ devtool = false, debug = false, logger, -}: ConfigGRPC) => { +}: ConfigGRPC) => { if (!isText(server)) { throw new Error('No "server" in GRPC config'); } @@ -37,7 +37,7 @@ export const grpcHTTP = ({ return (( field: Service, values = {} as ServiceRequest>, - { cancel, onDownload, onUpload, mask, timeout }: LocalGRPC = {} + { cancel, onDownload, onUpload, mask, timeout, meta }: LocalGRPC = {} ): Promise>>> => { if (isString(cancel) && isFunction(cancels[cancel])) { cancels[cancel](); @@ -181,7 +181,7 @@ export const grpcHTTP = ({ if (isFunction(transformRequest)) { // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions - sendData = (await transformRequest({ xhr, data: values })) || values; + sendData = (await transformRequest({ xhr, data: values, meta })) || values; } const encoded = Encode( @@ -219,11 +219,11 @@ export const grpcHTTP = ({ return data; }) - .then(data => (isFunction(transformResponse) ? transformResponse({ xhr, data }) : data)) + .then(data => (isFunction(transformResponse) ? transformResponse({ xhr, data, meta }) : data)) .catch(data => { logger?.error(method, data); // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment return { success: false, error: { data } } as GError; }); - }) as GRPC; + }) as GRPC; }; diff --git a/src/http/types.ts b/src/http/types.ts index 0ad694a..e840bca 100644 --- a/src/http/types.ts +++ b/src/http/types.ts @@ -41,7 +41,7 @@ export type GOutput = GSuccess | GError | GResult; type LoggerFn = (message?: any, ...optionalParams: any[]) => void; -export interface ConfigGRPC { +export interface ConfigGRPC { server: string; credentials?: boolean; timeout?: number; @@ -52,8 +52,12 @@ export interface ConfigGRPC { error: LoggerFn; info: LoggerFn; }; - transformRequest?(params: { xhr: XMLHttpRequest; data: T }): T | void | Promise; - transformResponse?(params: { xhr: XMLHttpRequest; data: GOutput }): GOutput | Promise>; + transformRequest?(params: { xhr: XMLHttpRequest; data: T; meta?: Meta }): T | void | Promise; + transformResponse?(params: { + xhr: XMLHttpRequest; + data: GOutput; + meta?: Meta; + }): GOutput | Promise>; } export interface Cancel { @@ -61,7 +65,7 @@ export interface Cancel { abort?(): void; } -export interface LocalGRPC { +export interface LocalGRPC { mask?: | boolean | ('mask' extends keyof FieldGet @@ -77,18 +81,19 @@ export interface LocalGRPC { timeout?: number; onDownload?: (e: ProgressEvent) => void; onUpload?: (e: ProgressEvent) => void; + meta?: Meta; } -export type GRPC = ( +export type GRPC = ( ...[field, values, options]: {} extends ServiceRequest> - ? [Service, ServiceRequest>?, LocalGRPC?] - : [Service, ServiceRequest>, LocalGRPC?] + ? [Service, ServiceRequest>?, LocalGRPC?] + : [Service, ServiceRequest>, LocalGRPC?] ) => Promise>>>; -export type GRPCDeep = ( +export type GRPCDeep = ( ...[field, values, options]: {} extends ServiceRequestDeep> - ? [Service, ServiceRequestDeep>?, LocalGRPC?] - : [Service, ServiceRequestDeep>, LocalGRPC?] + ? [Service, ServiceRequestDeep>?, LocalGRPC?] + : [Service, ServiceRequestDeep>, LocalGRPC?] ) => Promise, DeepReadonly>>>>; // ...[method, params]: T[P]['request'] extends undefined ? [P] : [P, T[P]['request']] diff --git a/tests/demo.ts b/tests/demo.ts index a92d06d..b68212c 100644 --- a/tests/demo.ts +++ b/tests/demo.ts @@ -36,8 +36,12 @@ const forbidden = () => { const TOKEN = '123'; (async () => { + interface Meta { + token: string; + } + // Server config - const grpc = grpcHTTP({ + const grpc = grpcHTTP({ server: 'https://example.com', credentials: true, debug: false, @@ -45,13 +49,17 @@ const TOKEN = '123'; timeout: undefined, // Proxy xhr before request - transformRequest: ({ xhr, data }) => { + transformRequest: ({ xhr, data, meta }) => { xhr.setRequestHeader('Authorization', TOKEN); + console.log(meta?.token); return { ...data, x: 1 }; }, // Proxy result after request - transformResponse: ({ data }) => { + transformResponse: ({ xhr, data, meta }) => { + console.log(xhr.status); + console.log(meta?.token); + if (!data.success) { alert(`v1.Grpc.Event.GRPCError ${data.error.message ?? ''}`); @@ -67,7 +75,7 @@ const TOKEN = '123'; } return data; }, - }) as GRPCDeep; + }) as GRPCDeep; // Deep example // Simple method call const user = await grpc(whisk_api_user_v2_UserAPI_GetMe); @@ -121,6 +129,9 @@ const TOKEN = '123'; onDownload: e => console.log(e.loaded / e.total), onUpload: e => console.log(e.loaded / e.total), timeout: 2000, + meta: { + token: '12', + }, } );