Skip to content

Commit

Permalink
PREAPPS-6008 rename requestApi to customFetch, Added comment for auth…
Browse files Browse the repository at this point in the history
…Token param, Updated README.md file for customFetch
  • Loading branch information
damini-vashishtha authored and silentsakky committed May 14, 2021
1 parent 735a8f9 commit 75947b1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
6 changes: 3 additions & 3 deletions src/batch-client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import {
DEFAULT_HOSTNAME,
DEFAULT_SOAP_PATHNAME,
jsonRequest,
setRequestAPI
setCustomFetch
} from '../request';
import {
JsonRequestOptions,
Expand Down Expand Up @@ -335,8 +335,8 @@ export class ZimbraBatchClient {
cache: false
});

if (options.requestAPI) {
setRequestAPI(options.requestAPI);
if (options.customFetch) {
setCustomFetch(options.customFetch);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/batch-client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 6 additions & 5 deletions src/request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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}`;
}
Expand All @@ -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({
Expand Down

0 comments on commit 75947b1

Please sign in to comment.