From 75947b167e89828628a2da256357f0019f6467ff Mon Sep 17 00:00:00 2001 From: Damini Vashistha Date: Tue, 27 Apr 2021 18:00:09 +0530 Subject: [PATCH] PREAPPS-6008 rename requestApi to customFetch, Added comment for authToken param, Updated README.md file for customFetch --- README.md | 8 ++++++++ src/batch-client/index.ts | 6 +++--- src/batch-client/types.ts | 2 +- src/request/index.ts | 11 ++++++----- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 2bac8f0c1..f61ed2809 100644 --- a/README.md +++ b/README.md @@ -124,5 +124,13 @@ In addition, the request primitives are also available: import { jsonRequest, batchJsonRequest } from 'zimbra-graphql/request'; ``` +### Provide Custom `Fetch` API +By default, `ZimbraBatchClient` uses [fetch api](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) of browser. In case, `ZimbraBatchClient` is used on non-browser platforms (i.e. NodeJS App), it will throw an error, such as: _**fetch is not defined.**_ + +In such case, `customFetch` option key allows to override or provide third-party fetch module/method. +```javascript +const client = new ZimbraBatchClient({ customFetch: myCustomFetchMethod }); +``` + ### Hacking on the Client See the [wiki](https://github.com/Zimbra/zm-api-js-client/wiki) for details on how to add new APIs to the client. diff --git a/src/batch-client/index.ts b/src/batch-client/index.ts index 4c741c500..17f94543f 100644 --- a/src/batch-client/index.ts +++ b/src/batch-client/index.ts @@ -52,7 +52,7 @@ import { DEFAULT_HOSTNAME, DEFAULT_SOAP_PATHNAME, jsonRequest, - setRequestAPI + setCustomFetch } from '../request'; import { JsonRequestOptions, @@ -335,8 +335,8 @@ export class ZimbraBatchClient { cache: false }); - if (options.requestAPI) { - setRequestAPI(options.requestAPI); + if (options.customFetch) { + setCustomFetch(options.customFetch); } } diff --git a/src/batch-client/types.ts b/src/batch-client/types.ts index 7b87a081c..0e4f928bd 100644 --- a/src/batch-client/types.ts +++ b/src/batch-client/types.ts @@ -63,10 +63,10 @@ export type SessionHandler = { export interface ZimbraClientOptions { authToken?: string; csrfToken?: string; + customFetch?: any; jwtToken?: string; localStoreClient?: any; notificationHandler?: NotificationHandler; - requestAPI?: any; sessionHandler?: SessionHandler; soapPathname?: string; userAgent?: UserAgent; diff --git a/src/request/index.ts b/src/request/index.ts index eabc9e24a..ec6666d60 100644 --- a/src/request/index.ts +++ b/src/request/index.ts @@ -16,10 +16,10 @@ import { export const DEFAULT_HOSTNAME = '/@zimbra'; export const DEFAULT_SOAP_PATHNAME = '/service/soap'; -let requestAPI: any; +let customFetch: any; -export function setRequestAPI(httpRequestAPI: any) { - requestAPI = httpRequestAPI; +export function setCustomFetch(httpRequestAPI: any) { + customFetch = httpRequestAPI; } function soapCommandBody(options: RequestOptions) { @@ -248,6 +248,7 @@ export function jsonRequest( header.context.csrfToken = requestOptions.csrfToken; } + // Allow to set Auth Token in Cookie in case `ZimbraBatchClient` is used on non-web platforms, like nodejs if (requestOptions.authToken) { options.headers['Cookie'] = `ZM_AUTH_TOKEN=${requestOptions.authToken}`; } @@ -264,9 +265,9 @@ export function jsonRequest( }; } - // Use received `requestAPI` passed as params instead of default fetch API + // Use received `customFetch` passed as params instead of default fetch API // to make `jsonRequest` method compatible with unsupported platforms, i.e. node.js - return (requestAPI || fetch)(url, { + return (customFetch || fetch)(url, { method: 'POST', credentials: options.credentials, body: JSON.stringify({