https://github.com/labd/commercetools-utilities-javascript
https://github.com/labd/commercetools-utilities-javascript
Last synced: 11 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/labd/commercetools-utilities-javascript
- Owner: labd
- License: mit
- Created: 2021-01-14T12:25:26.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-06-22T16:39:38.000Z (about 3 years ago)
- Last Synced: 2025-03-28T22:51:11.838Z (about 1 year ago)
- Language: TypeScript
- Size: 285 KB
- Stars: 2
- Watchers: 6
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Lab Digital Commercetools Utilities
## CommercetoolsClient
This is a more straight-forward helper to create a commercetools client for
interacting with Commercetools.
```ts
const client = CommercetoolsClient({
host: 'https://api.europe-west1.gcp.commercetools.com/',
projectKey: 'my-project',
auth: {
host: 'https://auth.europe-west1.gcp.commercetools.com/',
credentials: {
clientId: 'my-client-id',
clientSecret: 'my-client-secret',
},
scopes: ['view_orders:my-project', 'view_products:my-project'],
},
});
const projectApi = await client.getProjectApi();
projectApi.products().get().execute()
```
When the options are not passed it will read it from the ENV vars:
- `CT_PROJECT_KEY`
- `CT_API_URL`
- `CT_AUTH_URL`
- `CT_CLIENT_ID`
- `CT_CLIENT_SECRET`
- `CT_SCOPES`
```ts
const client = CommercetoolsClient();
client.getProjectApi();
```
You can also pass a callable to `auth` which returns a token to be used.
```ts
const client = createClient({
host: process.env.CT_API_URL,
projectKey,
auth: async () => {
return 'mytoken';
},
});
client.getProjectApi();
```
Note that the ApiRoot object is cached on the client instance for 900 seconds
### Pass custom middleware to the Commercetools client
```ts
const client = CommercetoolsClient({
host: 'https://api.europe-west1.gcp.commercetools.com/',
projectKey: 'my-project',
auth: {
host: 'https://auth.europe-west1.gcp.commercetools.com/',
credentials: {
clientId: 'my-client-id',
clientSecret: 'my-client-secret',
},
scopes: ['view_orders:my-project', 'view_products:my-project'],
},
middlewares: [yourMiddleware]
});
```
See the [commercetools sdk docs](https://docs.commercetools.com/sdk/js-sdk-middleware#custom-middleware) for an example of custom middleware or check the tests in `client.test.ts`.