Skip to content

Commit 9034675

Browse files
authored
chore(docs): update the typescript docs (#1711)
- add more details and explanation to the timeout and backoff properties - add more details and explanation to the retryConfig objects and all it's properties including: - maxRetries - backoff - maxDelay - retryDelay
1 parent a7df4f3 commit 9034675

File tree

2 files changed

+98
-12
lines changed

2 files changed

+98
-12
lines changed

docs/sdk/api/typescriptSdk.md

Lines changed: 95 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
We provide a packages written in typescript for using our API
44

5+
#### Note:
6+
7+
Please checkout the new [TypeScript Client](#typescript-sdk-client-v2) that is more updated and actively maintained and also consider migrating.
8+
59
![example usage](typescript_tutorial.gif)
610

711
The source code for these packages are located in the [typescript](https://github.com/commercetools/commercetools-sdk-typescript/) repository.
@@ -27,7 +31,7 @@ npm install --save @commercetools/ml-sdk
2731

2832
#### Usage example
2933

30-
```ts
34+
```typescript
3135
import { createAuthMiddlewareForClientCredentialsFlow } from '@commercetools/sdk-middleware-auth'
3236
import { createHttpMiddleware } from '@commercetools/sdk-middleware-http'
3337
import { createClient } from '@commercetools/sdk-client'
@@ -143,7 +147,8 @@ const client: Client = new ClientBuilder()
143147
.build()
144148
```
145149

146-
It is also important to note that we can also chain other middlewares or further configure the default client to suit the specific need of the client being built.
150+
It is also important to note that we can chain other middlewares or further configure the default client to suit the specific need of the client being built.
151+
147152
Example
148153

149154
```typescript
@@ -223,11 +228,25 @@ getProject().then(console.log).catch(console.error)
223228

224229
`-`
225230

226-
# Middlewares
231+
# Middleware Creator Methods
227232

228233
Below is a list of all the class methods that can be invoked to add the functionality of a given middleware.
229234

230-
### The authMiddleware methods
235+
1. The authMiddleware creator methods
236+
- withAnonymousSessionFlow
237+
- withClientCredentialsFlow
238+
- withExistingTokenFlow
239+
- withPasswordFlow
240+
- withRefreshToken
241+
2. Other middleware creator methods
242+
- withHttpMiddleware
243+
- withUserAgentMiddleware
244+
- withCorrelationIdMiddleware
245+
- withQueueMiddlewareware
246+
- withLoggerMiddleware
247+
- withMiddleware
248+
249+
### 1. The authMiddleware creator methods
231250

232251
These are class methods that creates auth middlewares using different authentication flows or method.
233252

@@ -413,7 +432,7 @@ const client: Client = new ClientBuilder()
413432

414433
`-`
415434

416-
### Other middlewares
435+
### 2. Other middleware creator methods
417436

418437
There are also other class methods that creates middlewares used to fully cusotmize and control the client, they are described in details below.
419438

@@ -432,21 +451,38 @@ The HTTP middleware can run in either a browser or Node.js environment. For Node
432451
5. `maskSensitiveHeaderData` (_Boolean_): flag to mask sensitie data in the header. e.g. Authorization token
433452
6. `enableRetry` (_Boolean_): flag to enable retry on network errors and 500 response. (Default: false)
434453
7. `retryConfig` (_Object_): Field required in the object listed below
435-
8. `maxRetries` (_Number_): number of times to retry the request before failing the request. (Default: 50)
436-
9. `retryDelay` (_Number_): amount of milliseconds to wait before retrying the next request. (Default: 200)
437-
10. `backoff` (_Boolean_): activates exponential backoff. Recommended to prevent spamming of the server. (Default: true)
438-
11. `maxDelay` (_Number_): The maximum duration (milliseconds) to wait before retrying, useful if the delay time grew exponentially more than reasonable
454+
8. - `maxRetries` (_Number_): number of times to retry the request before failing the request. (Default: 50)
455+
9. - `retryDelay` (_Number_): amount of milliseconds to wait before retrying the next request. (Default: 200)
456+
10. - `backoff` (_Boolean_): activates exponential backoff. Recommended to prevent spamming of the server. (Default: true)
457+
11. - `maxDelay` (_Number_): The maximum duration (milliseconds) to wait before retrying, useful if the delay time grew exponentially more than reasonable
439458
12. `fetch` (_Function_): A fetch implementation which can be e.g. `node-fetch` or `unfetch` but also the native browser `fetch` function
440459
13. `timeout` (_Number_): Request/response timeout in ms. Must have globally available or passed in AbortController
441-
14. `abortController` or `getAbortController` depending on you chose to handle the timeout (`abortController`): This property accepts the `AbortController` instance. Could be [abort-controller](https://www.npmjs.com/package/abort-controller) or globally available one.
460+
14. `getAbortController` depending on you chose to handle the timeout (`abortController`): This property accepts the `AbortController` instance. Could be [abort-controller](https://www.npmjs.com/package/abort-controller) or globally available one.
461+
462+
#### Note:
463+
464+
The `arbortController` property is deprecated, use the `getAbortController` property instead.
442465

443466
#### Retrying requests
444467

445-
This modules have a retrying ability incase of network failures or 503 response errors. To enable this behavior, pass the `enableRetry` flag in the options and also set the maximum number of retries (`maxRetries`) and amount of milliseconds to wait before retrying a request (`retryDelay`).
468+
This modules have a retrying ability incase of network failures or 503 response errors. To enable this behavior, pass the `enableRetry` flag in the options and also set the maximum number of retries (`maxRetries`) and amount in milliseconds to wait before retrying a request (`retryDelay`).
446469

447470
The repeater implements an `exponential` delay, meaning the wait time is not constant and it grows on every retry.
448471

449-
##### Token caching
472+
#### retryConfig
473+
474+
This is an object that defines the properties needed to properly configure the retry logic. This configuration only works if the `enableRetries` property is set to `true`
475+
476+
- _maxRetries_ - This property is used to set the number of times the request should retry in an event the previous request failed, the default retries is 50.
477+
- _retryDelay_ - This set the delay (time in milliseconds) between retries, it is important to choose a number that allow the previous request to fully complete before initiating the next retry assuming the previous request failed.
478+
- _backoff_ - This is used to configure how the retries should be carried out, setting this property to `true` means the request will backoff for a while before retrying. Assuming the previous retry was done in a 2 seconds (2000 milliseconds) delay, the next retry will be say 4 seconds (4000 milliseconds) and the next 8 seconds (8 milliseconds) etc. It is also advisable to set the `maxDelay` property so delay doesn't grow too large.
479+
- _maxDelay_ - Used to set the delay time limit for the backoff property, so as to prevent the backoff delay from increasing exponentially to infinity.
480+
481+
#### timeout
482+
483+
For setting the timeout (in milliseconds) for all http requests or responses in a situation where a request or response might take too long to be processed or completed respectively.
484+
485+
#### Token caching
450486

451487
The token is retrieved and cached upon the first request made by the client. Then, it gets refreshed when it expires. To utilize this, please make sure you use the same client instance and do not create new ones.
452488

@@ -465,6 +501,7 @@ const options: HttpMiddlewareOptions = {
465501
retryConfig: {
466502
maxRetries: 2,
467503
retryDelay: 300, //milliseconds
504+
backoff: false,
468505
maxDelay: 5000, //milliseconds
469506
},
470507
fetch,
@@ -482,6 +519,7 @@ This is used to signal the retry module to retry the request in an event of a re
482519
#### Usage example
483520

484521
```typescript
522+
import AbortController from 'abort-controller'
485523
// Use default options
486524
const options: HttpMiddlewareOptions = {
487525
host: testHost,
@@ -596,6 +634,51 @@ cont client: Client = new ClientBuilder()
596634

597635
`-`
598636

637+
#### withMiddleware(middleware: Middleware)
638+
639+
A custom class method that accepts a middleware as a argument which is used to further configure the client. Notice how we called the underlying middleware creator function `createHttpClient` and `createAuthForClientCredentialsFlow` to create the middlewares here. With this one can easily use a custom middleware that serve a specific need.
640+
641+
#### Usage Example
642+
643+
```typescript
644+
// ClientBuilder.js
645+
646+
import fetch from 'node-fetch'
647+
import {
648+
ClientBuilder,
649+
Client,
650+
AuthMiddlewareOptions,
651+
HttpMiddlewareOptions,
652+
createAuthForClientCredentialsFlow,
653+
createHttpClient
654+
} from '@commercetools/sdk-client-v2'
655+
// create the authMiddlewareOptions object
656+
const authMiddlewareOptions: AuthMiddlewareOptions = {
657+
host: 'https://auth.europe-west1.gcp.commercetools.com',
658+
projectKey: 'demo-key',
659+
credentials: {
660+
clientId: 'clientID12345',
661+
clientSecret: 'clientSecret12345',
662+
},
663+
oauthUri: 'adminAuthUrl',
664+
scopes: ['manage_project:demo-key'],
665+
fetch,
666+
}
667+
668+
// create the httpMiddlewareOptions object also
669+
const httpMiddlewareOptions: HttpMiddlewareOptions = {
670+
host: 'https://api.europe-west1.gcp.commercetools.com',
671+
fetch,
672+
}
673+
674+
const Client: client = new clientBuilder()
675+
.withMiddleware(createAuthForClientCredentialsFlow(authMiddlewareOptions))
676+
.withMiddleware(createHttpClient(httpMiddlewareOptions))
677+
...
678+
```
679+
680+
`-`
681+
599682
#### buid()
600683

601684
To build the client after calling the class methods of choice that adds the middleware, we invoke the `build()` as the last method on the `new ClientBuilder()` class instance.

types/sdk.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ export type HttpMiddlewareOptions = {
243243
maxDelay?: number,
244244
},
245245
fetch?: typeof fetch,
246+
/**
247+
* @deprecated use getAbortController instead
248+
*/
246249
abortController?: AbortController, // deprecated
247250
getAbortController: () => AbortController
248251
}

0 commit comments

Comments
 (0)