@commercetools/ts-client 0.0.0-beta.0 → 0.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +121 -2
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,3 +1,122 @@
1
- # Proof of Concept
1
+ # Commercetools Composable Commerce (Improved) TypeScript SDK client (beta)
2
2
 
3
- A Chain of Responsibility _(CoR)_ in TypeScript
3
+ This is the new and improved Typescript SDK client.
4
+
5
+ ## Usage examples
6
+
7
+ ```bash
8
+ npm install --save @commercetools/ts-client
9
+ npm install --save @commercetools/platform-sdk
10
+
11
+ or
12
+
13
+ yarn add @commercetools/ts-client
14
+ yarn add @commercetools/platform-sdk
15
+ ```
16
+
17
+ ```ts
18
+ import {
19
+ type Next
20
+ type HttpMiddlewareOptions,
21
+ type AuthMiddlewareBaseOptions
22
+ type ClientRequest,
23
+ type MiddlewareRequest,
24
+ type MiddlewareResponse,
25
+ type Client
26
+
27
+ ClientBuilder,
28
+ } from '@commercetools/ts-client'
29
+ import { createApiBuilderFromCtpClient } from '@commercetools/platform-sdk'
30
+ import fetch from 'node-fetch'
31
+
32
+ const projectKey = 'mc-project-key'
33
+ const authMiddlewareOptions = {
34
+ host: 'https://auth.europe-west1.gcp.commercetools.com',
35
+ projectKey,
36
+ credentials: {
37
+ clientId: 'mc-client-id',
38
+ clientSecret: 'mc-client-secrets',
39
+ },
40
+ oauthUri: '/oauth/token', // - optional: custom oauthUri
41
+ scopes: [`manage_project:${projectKey}`],
42
+ httpClient: fetch,
43
+ }
44
+
45
+ const httpMiddlewareOptions = {
46
+ host: 'https://api.europe-west1.gcp.commercetools.com',
47
+ httpClient: fetch,
48
+ }
49
+
50
+ const retryOptions = {
51
+ maxRetries: 3,
52
+ retryDelay: 200,
53
+ backoff: true,
54
+ retryCodes: [200]
55
+ }
56
+
57
+ // custom middleware
58
+ function middleware(options) {
59
+ return (next: Next) =>
60
+ async (request: MiddlewareRequest): Promise<MiddlewareResponse> => {
61
+ const { response, ...rest } = request
62
+
63
+ // other actions can also be carried out here e.g logging,
64
+ // error handling, injecting custom headers to http requests etc.
65
+ console.log({ response, rest })
66
+ return next({ ...request })
67
+ }
68
+ }
69
+
70
+ const client: Client = new ClientBuilder()
71
+ .withPasswordFlow(authMiddlewareOptions)
72
+ .withLoggerMiddleware({ includeOriginalRequest: false, includeResponseHeaders: false })
73
+ .withCorrelationIdMiddleware({ generate: () => 'fake-correlation-id' + Math.floor(Math.random() + 2) })
74
+ .withHttpMiddleware(httpMiddlewareOptions)
75
+ .withRetryMiddleware(retryOptions)
76
+ .withMiddleware(middleware({})) // <<<------------------- add the custom middleware here
77
+ .withErrorMiddleware({})
78
+ .build()
79
+
80
+ const apiRoot = createApiBuilderFromCtpClient(client)
81
+
82
+ // calling the Composable Commerce `api` functions
83
+ // get project details
84
+ apiRoot
85
+ .withProjectKey({ projectKey })
86
+ .get()
87
+ .execute()
88
+ .then((x) => {
89
+ /*...*/
90
+ })
91
+
92
+ // create a productType
93
+ apiRoot
94
+ .withProjectKey({ projectKey })
95
+ .productTypes()
96
+ .post({
97
+ body: { name: 'product-type-name', description: 'some description' },
98
+ })
99
+ .execute()
100
+ .then((x) => {
101
+ /*...*/
102
+ })
103
+
104
+ // create a product
105
+ apiRoot
106
+ .withProjectKey({ projectKey })
107
+ .products()
108
+ .post({
109
+ body: {
110
+ name: { en: 'our-great-product-name' },
111
+ productType: {
112
+ typeId: 'product-type',
113
+ id: 'some-product-type-id',
114
+ },
115
+ slug: { en: 'some-slug' },
116
+ },
117
+ })
118
+ .execute()
119
+ .then((x) => {
120
+ /*...*/
121
+ })
122
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commercetools/ts-client",
3
- "version": "0.0.0-beta.0",
3
+ "version": "0.0.0-beta.1",
4
4
  "engines": {
5
5
  "node": ">=14"
6
6
  },