@commercetools-frontend/sdk 0.0.0-CRAFT-1791-20251006162610
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.
- package/LICENSE +21 -0
- package/README.md +176 -0
- package/dist/commercetools-frontend-sdk.cjs.d.ts +2 -0
- package/dist/commercetools-frontend-sdk.cjs.dev.js +594 -0
- package/dist/commercetools-frontend-sdk.cjs.js +7 -0
- package/dist/commercetools-frontend-sdk.cjs.prod.js +516 -0
- package/dist/commercetools-frontend-sdk.esm.js +568 -0
- package/dist/declarations/src/actions/actions.d.ts +16 -0
- package/dist/declarations/src/actions/index.d.ts +1 -0
- package/dist/declarations/src/components/sdk-get/sdk-get.d.ts +62 -0
- package/dist/declarations/src/hooks/use-async-dispatch.d.ts +4 -0
- package/dist/declarations/src/index.d.ts +26 -0
- package/dist/declarations/src/middleware/index.d.ts +1 -0
- package/dist/declarations/src/middleware/middleware.d.ts +9 -0
- package/dist/declarations/src/test-utils/index.d.ts +1 -0
- package/dist/declarations/src/test-utils/test-utils.d.ts +38 -0
- package/dist/declarations/src/types.d.ts +74 -0
- package/dist/declarations/src/version.d.ts +2 -0
- package/package.json +74 -0
- package/test-utils/dist/commercetools-frontend-sdk-test-utils.cjs.d.ts +2 -0
- package/test-utils/dist/commercetools-frontend-sdk-test-utils.cjs.dev.js +63 -0
- package/test-utils/dist/commercetools-frontend-sdk-test-utils.cjs.js +7 -0
- package/test-utils/dist/commercetools-frontend-sdk-test-utils.cjs.prod.js +63 -0
- package/test-utils/dist/commercetools-frontend-sdk-test-utils.esm.js +51 -0
- package/test-utils/package.json +4 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) commercetools GmbH
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# @commercetools-frontend/sdk
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<a href="https://www.npmjs.com/package/@commercetools-frontend/sdk"><img src="https://badgen.net/npm/v/@commercetools-frontend/sdk" alt="Latest release (latest dist-tag)" /></a> <a href="https://www.npmjs.com/package/@commercetools-frontend/sdk"><img src="https://badgen.net/npm/v/@commercetools-frontend/sdk/next" alt="Latest release (next dist-tag)" /></a> <a href="https://bundlephobia.com/result?p=@commercetools-frontend/sdk"><img src="https://badgen.net/bundlephobia/minzip/@commercetools-frontend/sdk" alt="Minified + GZipped size" /></a> <a href="https://github.com/commercetools/merchant-center-application-kit/blob/main/LICENSE"><img src="https://badgen.net/github/license/commercetools/merchant-center-application-kit" alt="GitHub license" /></a>
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
Tools for declarative fetching.
|
|
8
|
+
|
|
9
|
+
# Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
$ npm install --save @commercetools-frontend/sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
# Declarative Fetching
|
|
16
|
+
|
|
17
|
+
There are two sides to declarative fetching:
|
|
18
|
+
|
|
19
|
+
- describing the data we need to fetch declaratively
|
|
20
|
+
- fetching by rendering a component instead of triggering the fetch imperatively
|
|
21
|
+
|
|
22
|
+
This module aims to provide the necessary parts for both.
|
|
23
|
+
|
|
24
|
+
## Declaring the fetch using a component
|
|
25
|
+
|
|
26
|
+
> **⚠️ Deprecated** This component will likely not get developed any further. Use regular redux-style action creators for now. In the future we plan to use Apollo to replace this completely.
|
|
27
|
+
|
|
28
|
+
The provided `Sdk.Get` component can be rendered to fetch data. It uses the
|
|
29
|
+
middleware behind the scenes but adds some features on top. See
|
|
30
|
+
[components/sdk-fetch/README.md](./components/sdk-fetch/README.md) for details.
|
|
31
|
+
|
|
32
|
+
## Describing data declaratively
|
|
33
|
+
|
|
34
|
+
The provided middleware takes an object which describes what data should be
|
|
35
|
+
fetched. The middleware transforms that description into a promise and resolves
|
|
36
|
+
the promise. It passes the response back to the callee.
|
|
37
|
+
|
|
38
|
+
# Usage
|
|
39
|
+
|
|
40
|
+
The middleware is a thin wrapper around [`sdk-client`](https://commercetools.github.io/nodejs/sdk/api/sdkClient.html). It offers a way to declaratively describe the data requirements.
|
|
41
|
+
|
|
42
|
+
A Redux action using one of the action creators below needs to be dispatched. It contains the description of what to get/post/delete. The `sdk` middleware then turns the declarative description into imperative API calls on `sdk-client`. The dispatched action resolves with the result of `sdk-client`.
|
|
43
|
+
|
|
44
|
+
## Action creators
|
|
45
|
+
|
|
46
|
+
The action creators can be imported as
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
import { actions as sdkActions } from '@commercetools-frontend/sdk';
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Methods
|
|
53
|
+
|
|
54
|
+
The supported action creators are:
|
|
55
|
+
|
|
56
|
+
- `sdkActions.get(config)`: sends a HTTP `GET`
|
|
57
|
+
- `sdkActions.post(config)`: sends a HTTP `POST`
|
|
58
|
+
- `sdkActions.del(config)`: sends a HTTP `DELETE`
|
|
59
|
+
- `sdkActions.head(config)`: sends a HTTP `HEAD`
|
|
60
|
+
|
|
61
|
+
### Specifying an endpoint
|
|
62
|
+
|
|
63
|
+
There are two ways to describe an endpoint:
|
|
64
|
+
|
|
65
|
+
- by `uri`: pass the URI as string
|
|
66
|
+
- by `service`: uses the `@commercetools/api-request-builder` to build the URI. You can pass `config.options` to supply the necessary request parameters
|
|
67
|
+
|
|
68
|
+
The `post` action creator additionally requires a `config.payload` object or string, containing the request payload.
|
|
69
|
+
|
|
70
|
+
> The `mcApiProxyTarget` values are exposed from the `@commercetools-frontend/constants` package, as `MC_API_PROXY_TARGETS`. The value will be used to build a prefix to the `uri` as `/proxy/<mcApiProxyTarget>/<uri>`.
|
|
71
|
+
|
|
72
|
+
**Usage with `uri`**
|
|
73
|
+
|
|
74
|
+
```js
|
|
75
|
+
{
|
|
76
|
+
uri: String,
|
|
77
|
+
mcApiProxyTarget?: ApiProxyTarget,
|
|
78
|
+
payload?: Object | String
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
- `uri` can be relative or absolute. It gets passed to [`sdk-client`](https://commercetools.github.io/nodejs/sdk/api/sdkClient.html) as-is
|
|
83
|
+
|
|
84
|
+
This approach must be used when querying something other than the CTP API. In case the CTP API is queried it is recommended to use `service` and `options` since that is easier to test. It is totally valid to provide `uri` only as well though.
|
|
85
|
+
|
|
86
|
+
When both, `uri` and `options` (or `service`) are present, the `uri` takes precedence.
|
|
87
|
+
|
|
88
|
+
**Usage with `service` and `options`**
|
|
89
|
+
|
|
90
|
+
```js
|
|
91
|
+
{
|
|
92
|
+
service: String,
|
|
93
|
+
options: Object,
|
|
94
|
+
mcApiProxyTarget?: ApiProxyTarget,
|
|
95
|
+
payload?: Object | String
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Before using the `sdk-client` the `sdk` middleware combines `service` and `options` using `api-request-builder`'s `parse` method to form a `uri`. It then makes the exact same request as when specifying `uri` directly.
|
|
100
|
+
|
|
101
|
+
The supported `options` can be found in the `api-request-builder`'s documentation under the [Declarative Usage](https://commercetools.github.io/nodejs/sdk/api/apiRequestBuilder.html#declarative-usage) section.
|
|
102
|
+
|
|
103
|
+
## Action creators for external API usage
|
|
104
|
+
|
|
105
|
+
By default, all requests with the SDK are configured to be sent to the MC API.
|
|
106
|
+
However, Custom Applications using the [Proxy to external API](https://docs.commercetools.com/merchant-center-customizations/concepts/integrate-with-your-own-api) need to configure the request a bit differently, and send additional headers.
|
|
107
|
+
|
|
108
|
+
To make it easier to make requests to the proxy endpoint using the SDK, there is a new action creator wrapper that comes with built-in configuration options.
|
|
109
|
+
|
|
110
|
+
The exported action creators have a new export `forwardTo`, which is an object containing wrappers around the normal action creators.
|
|
111
|
+
|
|
112
|
+
```js
|
|
113
|
+
actions.forwardTo.get(options);
|
|
114
|
+
actions.forwardTo.del(options);
|
|
115
|
+
actions.forwardTo.head(options);
|
|
116
|
+
actions.forwardTo.post(options);
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
The options for the action creators are the same as the **Usage with `uri`**, except that the `uri` value needs to be the URL to the external API (see `X-Forward-To` header).
|
|
120
|
+
|
|
121
|
+
The `forwardTo` action creators additionally set the following headers:
|
|
122
|
+
|
|
123
|
+
- `Accept-version`
|
|
124
|
+
- `X-Forward-To`
|
|
125
|
+
- `X-Forward-To-Audience-Policy`
|
|
126
|
+
|
|
127
|
+
For more information, check the [Proxy to external API](https://docs.commercetools.com/merchant-center-customizations/concepts/integrate-with-your-own-api) documentation.
|
|
128
|
+
|
|
129
|
+
## Error handling
|
|
130
|
+
|
|
131
|
+
Failed requests will result in a rejected promise. The `sdk-client`'s error handling applies, so network errors and CTP API errors on the content itself result in a rejected promise.
|
|
132
|
+
|
|
133
|
+
The `sdk` package does not provide any error handling out of the box. It's the application's responsibility to handle errors (e.g. show a notification, track the error).
|
|
134
|
+
|
|
135
|
+
The Merchant Center has a `handleActionError` function which is what we currently use for error handling. It logs the error to the tracking tool (Sentry) and shows a notification to the client. This should be used whenever a more special error handling is not necessary.
|
|
136
|
+
|
|
137
|
+
## Example
|
|
138
|
+
|
|
139
|
+
```js
|
|
140
|
+
import { actions as sdkActions } from '@commercetools-frontend/sdk';
|
|
141
|
+
|
|
142
|
+
const fetchProductById = (productId) =>
|
|
143
|
+
sdkActions.get({
|
|
144
|
+
service: 'products',
|
|
145
|
+
options: { id: productId },
|
|
146
|
+
});
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
```js
|
|
150
|
+
import * as globalActions from '@commercetools-frontend/actions-global';
|
|
151
|
+
|
|
152
|
+
class ProductPage extends React.Component {
|
|
153
|
+
state = { product: null };
|
|
154
|
+
componentDidMount() {
|
|
155
|
+
this.props.fetchProductById(this.props.productId).then(
|
|
156
|
+
(product) => {
|
|
157
|
+
this.setState({ product });
|
|
158
|
+
},
|
|
159
|
+
(error) => {
|
|
160
|
+
this.props.onActionError(error, 'ProductPage/fetchProductById');
|
|
161
|
+
}
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
render() {
|
|
165
|
+
if (!this.state.product) return <LoadingSpinner />;
|
|
166
|
+
|
|
167
|
+
return <div>{JSON.stringify(this.state.product)}</div>;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// and finally we need to pass the bound action creator to the component using plain old redux
|
|
172
|
+
export default connect(null, {
|
|
173
|
+
fetchProductById: productsActions.fetchProductById,
|
|
174
|
+
onActionError: globalActions.handleActionError,
|
|
175
|
+
})(ProductPage);
|
|
176
|
+
```
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export * from "./declarations/src/index.js";
|
|
2
|
+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29tbWVyY2V0b29scy1mcm9udGVuZC1zZGsuY2pzLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuL2RlY2xhcmF0aW9ucy9zcmMvaW5kZXguZC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9
|