@shopware/api-client 0.5.0 → 1.0.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.
- package/README.md +154 -93
- package/{admin-api-types/apiSchema-6.5.3.0.json → api-types/adminApiSchema.json} +62470 -25854
- package/api-types/adminApiTypes.d.ts +84322 -0
- package/api-types/{apiSchema-6.4.19.0.json → storeApiSchema.json} +15295 -11433
- package/api-types/storeApiSchema.overrides.json +687 -0
- package/api-types/storeApiTypes.d.ts +8514 -0
- package/api-types/storeApiTypes.overrides.ts +182 -0
- package/dist/index.cjs +169 -106
- package/dist/index.d.cts +89261 -62623
- package/dist/index.d.mts +89261 -62623
- package/dist/index.d.ts +89261 -62623
- package/dist/index.mjs +165 -106
- package/package.json +20 -14
- package/admin-api-types/apiTypes-6.5.3.0.d.ts +0 -59568
- package/admin-api-types/index.d.ts +0 -1
- package/api-types/apiSchema-6.4.20.0.json +0 -13830
- package/api-types/apiSchema-6.5.0.0.json +0 -16669
- package/api-types/apiSchema-6.5.2.0.json +0 -12154
- package/api-types/apiSchema-6.5.3.0.json +0 -12154
- package/api-types/apiTypes-6.4.19.0.d.ts +0 -8170
- package/api-types/apiTypes-6.4.20.0.d.ts +0 -8168
- package/api-types/apiTypes-6.5.0.0.d.ts +0 -7244
- package/api-types/apiTypes-6.5.2.0.d.ts +0 -7738
- package/api-types/apiTypes-6.5.3.0.d.ts +0 -7738
- package/api-types/index.d.ts +0 -1
package/README.md
CHANGED
|
@@ -1,111 +1,137 @@
|
|
|
1
1
|
# shopware/frontends - api-client
|
|
2
2
|
|
|
3
3
|
[](https://npmjs.com/package/@shopware/api-client)
|
|
4
|
-
[
|
|
4
|
+
[](https://github.com/shopware/frontends/tree/main/packages/api-client)
|
|
6
5
|
[](https://github.com/shopware/frontends/issues?q=is%3Aopen+is%3Aissue+label%3Aapi-client)
|
|
6
|
+
[](#)
|
|
7
7
|
|
|
8
|
-
Dynamic and fully typed API Client for Shopware 6. Usable in any JavaScript
|
|
8
|
+
Dynamic and fully typed API Client for Shopware 6. Usable in any JavaScript and TypeScript project.
|
|
9
9
|
You can use types generated from your custom API instance to have autocompletion and type safety.
|
|
10
10
|
|
|
11
|
+
To generate your own types use [@shopware/api-gen](https://www.npmjs.com/package/@shopware/api-gen) CLI.
|
|
12
|
+
|
|
11
13
|
## Setup
|
|
12
14
|
|
|
13
15
|
Install npm package:
|
|
14
16
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
<!-- automd:pm-install name="@shopware/api-client" -->
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
# ✨ Auto-detect
|
|
21
|
+
npx nypm install @shopware/api-client
|
|
18
22
|
|
|
19
|
-
#
|
|
23
|
+
# npm
|
|
24
|
+
npm install @shopware/api-client
|
|
25
|
+
|
|
26
|
+
# yarn
|
|
20
27
|
yarn add @shopware/api-client
|
|
21
28
|
|
|
22
|
-
#
|
|
23
|
-
|
|
29
|
+
# pnpm
|
|
30
|
+
pnpm install @shopware/api-client
|
|
31
|
+
|
|
32
|
+
# bun
|
|
33
|
+
bun install @shopware/api-client
|
|
24
34
|
```
|
|
25
35
|
|
|
26
|
-
|
|
36
|
+
<!-- /automd -->
|
|
37
|
+
|
|
38
|
+
## Store API client setup
|
|
39
|
+
|
|
40
|
+
Recommended practice is to create a separate module file. For example `src/apiClient.ts`, and import it whenever you need to use API Client.
|
|
27
41
|
|
|
28
42
|
```typescript
|
|
29
43
|
import { createAPIClient } from "@shopware/api-client";
|
|
30
44
|
|
|
31
45
|
// You can pick types of your current API version, the default one:
|
|
32
|
-
import type {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
components,
|
|
36
|
-
} from "@shopware/api-client/api-types";
|
|
37
|
-
// or (specific version):
|
|
38
|
-
import type {
|
|
39
|
-
operationPaths,
|
|
40
|
-
operations,
|
|
41
|
-
components,
|
|
42
|
-
} from "@shopware/api-client/api-types/apiTypes-6.4.20.0";
|
|
43
|
-
// or your types generated by @shopware/api-gen CLI:
|
|
44
|
-
import { operationPaths, operations, components } from "./apiTypes";
|
|
46
|
+
import type { operations } from "@shopware/api-client/store-api-types";
|
|
47
|
+
// or - RECOMMENDED - your types generated by [@shopware/api-gen](https://www.npmjs.com/package/@shopware/api-gen) CLI:
|
|
48
|
+
import type { operations } from "./api-types/storeApiTypes";
|
|
45
49
|
|
|
46
50
|
// you can pick cookies library of your choice
|
|
47
51
|
import Cookies from "js-cookie";
|
|
48
52
|
|
|
49
|
-
export const apiClient = createAPIClient<operations
|
|
53
|
+
export const apiClient = createAPIClient<operations>({
|
|
50
54
|
baseURL: "https://demo-frontends.shopware.store/store-api",
|
|
51
55
|
accessToken: "SWSCBHFSNTVMAWNZDNFKSHLAYW",
|
|
52
56
|
contextToken: Cookies.get("sw-context-token"),
|
|
53
|
-
onContextChanged(newContextToken) {
|
|
54
|
-
Cookies.set("sw-context-token", newContextToken, {
|
|
55
|
-
expires: 365, // days
|
|
56
|
-
path: "/",
|
|
57
|
-
sameSite: "lax",
|
|
58
|
-
});
|
|
59
|
-
},
|
|
60
57
|
});
|
|
61
58
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
59
|
+
apiClient.hook("onContextChanged", (newContextToken) => {
|
|
60
|
+
Cookies.set("sw-context-token", newContextToken, {
|
|
61
|
+
expires: 365, // days
|
|
62
|
+
path: "/",
|
|
63
|
+
sameSite: "lax",
|
|
64
|
+
secure: shopwareEndpoint.startsWith("https://"),
|
|
65
|
+
});
|
|
66
|
+
});
|
|
70
67
|
```
|
|
71
68
|
|
|
72
69
|
## Admin API client setup
|
|
73
70
|
|
|
74
|
-
|
|
71
|
+
```typescript
|
|
72
|
+
import { createAdminAPIClient } from "@shopware/api-client";
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
The setup works the same way as `creteAPIClient` function, with few differences
|
|
76
|
+
|
|
77
|
+
### credentials (optional) - Quick scripting or token-based authentication
|
|
78
|
+
|
|
79
|
+
We provide optional `credentials` parameter to `createAdminAPIClient`. Which allows you to use authentication type of your choice whenever you wish to create connection to any endpoint.
|
|
80
|
+
|
|
81
|
+
Example:
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import type {
|
|
85
|
+
operations,
|
|
86
|
+
} from "@shopware/api-client/admin-api-types"; // we take default admin api types from different directory than store-api - use your own types by generating schema with @shopware/api-gen CLI
|
|
87
|
+
import type { operations } from "./api-types/adminApiTypes"; // or use your own types generated by @shopware/api-gen CLI
|
|
88
|
+
|
|
89
|
+
const adminApiClient = createAdminAPIClient<operations>({
|
|
90
|
+
baseURL: `${process.env.SHOP_URL}/api`,
|
|
91
|
+
credentials: {
|
|
92
|
+
grant_type: "password",
|
|
93
|
+
client_id: "administration",
|
|
94
|
+
scopes: "write",
|
|
95
|
+
username: process.env.SHOP_ADMIN_USERNAME,
|
|
96
|
+
password: process.env.SHOP_ADMIN_PASSWORD,
|
|
97
|
+
},
|
|
98
|
+
// credentials: { // or token-based example
|
|
99
|
+
// grant_type: "client_credentials",
|
|
100
|
+
// client_id: "administration",
|
|
101
|
+
// client_secret: process.env.SHOP_ADMIN_TOKEN,
|
|
102
|
+
// },
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
await adminApiClient.invoke(...); // invoke defined endpoint
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### sessionData (optional) - Persistent authentication
|
|
109
|
+
|
|
110
|
+
This parameter is used to store session data in cookies (or other place you want to store it), so you can keep your session persistent.
|
|
111
|
+
|
|
112
|
+
You can combine this option with `credentials` property.
|
|
75
113
|
|
|
76
114
|
```typescript
|
|
77
115
|
// example adminApiClient.ts file
|
|
78
116
|
import { createAdminAPIClient } from "@shopware/api-client"; // we use different function to create admin api client
|
|
79
117
|
|
|
80
|
-
import {
|
|
81
|
-
|
|
82
|
-
RequestReturnType,
|
|
83
|
-
createAdminAPIClient,
|
|
84
|
-
} from "@shopware/api-client";
|
|
85
|
-
import type {
|
|
86
|
-
operationPaths,
|
|
87
|
-
operations,
|
|
88
|
-
components,
|
|
89
|
-
} from "@shopware/api-client/admin-api-types"; // we take default admin api types from different directory than store-api
|
|
118
|
+
import { createAdminAPIClient } from "@shopware/api-client";
|
|
119
|
+
import type { operations, Schemas } from "@shopware/api-client/admin-api-types"; // we take default admin api types from different directory than store-api
|
|
90
120
|
import Cookies from "js-cookie";
|
|
91
121
|
|
|
92
|
-
export const adminApiClient = createAdminAPIClient<operations
|
|
122
|
+
export const adminApiClient = createAdminAPIClient<operations>({
|
|
93
123
|
baseURL: "https://demo-frontends.shopware.store/api",
|
|
94
124
|
sessionData: JSON.parse(Cookies.get("sw-admin-session-data") || "{}"),
|
|
95
|
-
onAuthChange(sessionData) {
|
|
96
|
-
Cookies.set("sw-admin-session-data", JSON.stringify(sessionData), {
|
|
97
|
-
expires: 1, // days
|
|
98
|
-
path: "/",
|
|
99
|
-
sameSite: "lax",
|
|
100
|
-
});
|
|
101
|
-
},
|
|
102
125
|
});
|
|
103
126
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
127
|
+
adminApiClient.hooks("onAuthChange", (sessionData) => {
|
|
128
|
+
Cookies.set("sw-admin-session-data", JSON.stringify(sessionData), {
|
|
129
|
+
expires: 1, // days
|
|
130
|
+
path: "/",
|
|
131
|
+
sameSite: "lax",
|
|
132
|
+
secure: shopwareEndpoint.startsWith("https://"),
|
|
133
|
+
});
|
|
134
|
+
});
|
|
109
135
|
```
|
|
110
136
|
|
|
111
137
|
the rest works the same as store-api client.
|
|
@@ -117,10 +143,10 @@ Take a look at [example project using API Client](https://stackblitz.com/github/
|
|
|
117
143
|
### Simple invocation
|
|
118
144
|
|
|
119
145
|
```typescript
|
|
120
|
-
import { apiClient,
|
|
146
|
+
import { apiClient, RequestReturnType } from "./apiClient";
|
|
121
147
|
|
|
122
148
|
// could be reactive value, you can use ApiReturnType to type it properly
|
|
123
|
-
let productsResponse:
|
|
149
|
+
let productsResponse: RequestReturnType<"readProduct">;
|
|
124
150
|
|
|
125
151
|
async function loadProducts() {
|
|
126
152
|
productsResponse = await apiClient.invoke("readProduct post /product", {
|
|
@@ -129,28 +155,79 @@ async function loadProducts() {
|
|
|
129
155
|
}
|
|
130
156
|
```
|
|
131
157
|
|
|
158
|
+
### Fetch features
|
|
159
|
+
|
|
160
|
+
The new API client is leveraging [ofetch](https://github.com/unjs/ofetch) library, which has built in support for AbortController, timeout and other features.
|
|
161
|
+
|
|
162
|
+
Example usage of AbortController to cancell your request:
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
const controller = new AbortController();
|
|
166
|
+
|
|
167
|
+
const request = client.invoke("readContext get /context", {
|
|
168
|
+
fetchOptions: {
|
|
169
|
+
signal: controller.signal,
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
controller.abort(); // At this point client will throw an error with the information, that the request has been cancelled
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Other example of using `fetchOptions` for setting the timeout:
|
|
177
|
+
|
|
178
|
+
```typescript
|
|
179
|
+
const request = client.invoke("readContext get /context", {
|
|
180
|
+
fetchOptions: {
|
|
181
|
+
timeout: 5000, // 5 seconds
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
All exposed options available under `fetchOptions` are:
|
|
187
|
+
|
|
188
|
+
- `cache`
|
|
189
|
+
- `duplex`
|
|
190
|
+
- `keepalive`
|
|
191
|
+
- `priority`
|
|
192
|
+
- `redirect`
|
|
193
|
+
- `retry`
|
|
194
|
+
- `retryDelay`
|
|
195
|
+
- `retryStatusCodes`
|
|
196
|
+
- `signal`
|
|
197
|
+
- `timeout`
|
|
198
|
+
|
|
132
199
|
### Predefining methods
|
|
133
200
|
|
|
134
201
|
If you prefer to add another layer of abstraction you can use created previously types to define your own concept of methods.
|
|
135
202
|
|
|
136
203
|
```typescript
|
|
137
204
|
// add for example into apiClient.ts file
|
|
138
|
-
const readNavigation = (
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
205
|
+
const readNavigation = ({
|
|
206
|
+
depth,
|
|
207
|
+
type,
|
|
208
|
+
}: {
|
|
209
|
+
depth: number;
|
|
210
|
+
type: "main-navigation";
|
|
211
|
+
}) =>
|
|
212
|
+
apiClient.invoke("readNavigation post /navigation/{activeId}/{rootId}", {
|
|
213
|
+
headers: {
|
|
214
|
+
"sw-include-seo-urls": true,
|
|
215
|
+
},
|
|
216
|
+
pathParams: {
|
|
217
|
+
activeId: type,
|
|
218
|
+
rootId: type,
|
|
219
|
+
},
|
|
220
|
+
body: {
|
|
221
|
+
depth,
|
|
144
222
|
},
|
|
145
|
-
);
|
|
223
|
+
});
|
|
146
224
|
|
|
147
225
|
// in another file you can use it, and depth property will be set to 2 by default
|
|
148
226
|
import { readNavigation } from "./apiClient";
|
|
149
227
|
|
|
150
228
|
async function loadMainNavigation() {
|
|
151
229
|
const navigation = await readNavigation({
|
|
152
|
-
activeId: "main-navigation",
|
|
153
|
-
rootId: "main-navigation",
|
|
230
|
+
body: { activeId: "main-navigation", rootId: "main-navigation" },
|
|
154
231
|
});
|
|
155
232
|
}
|
|
156
233
|
```
|
|
@@ -184,26 +261,10 @@ try {
|
|
|
184
261
|
|
|
185
262
|
## Changelog
|
|
186
263
|
|
|
187
|
-
Full changelog for stable version is available [here](https://github.com/shopware/frontends/blob/main/packages/api-client
|
|
188
|
-
|
|
189
|
-
### Latest changes: 0.5.0
|
|
264
|
+
Full changelog for stable version is available [here](https://github.com/shopware/frontends/blob/main/packages/api-client/CHANGELOG.md)
|
|
190
265
|
|
|
191
|
-
###
|
|
192
|
-
|
|
193
|
-
- [#435](https://github.com/shopware/frontends/pull/435) [`a4483ed8`](https://github.com/shopware/frontends/commit/a4483ed8bf9370e87aedeb81846fe9d31880b3e0) Thanks [@patzick](https://github.com/patzick)! - Changed types imports to `import type {...} from "..."`
|
|
266
|
+
### Latest changes: 1.0.1
|
|
194
267
|
|
|
195
268
|
### Patch Changes
|
|
196
269
|
|
|
197
|
-
- [#
|
|
198
|
-
|
|
199
|
-
Now instead of:
|
|
200
|
-
|
|
201
|
-
```ts
|
|
202
|
-
const result = await apiInstance.invoke("readContext get /context", {});
|
|
203
|
-
```
|
|
204
|
-
|
|
205
|
-
you can do:
|
|
206
|
-
|
|
207
|
-
```ts
|
|
208
|
-
const result = await apiInstance.invoke("readContext get /context");
|
|
209
|
-
```
|
|
270
|
+
- [#1078](https://github.com/shopware/frontends/pull/1078) [`19f2800`](https://github.com/shopware/frontends/commit/19f28003cf937bcb630257cb7cfd2bd131b7cf9d) Thanks [@patzick](https://github.com/patzick)! - Patch for missing `sw-include-seo-url` in OpenAPI schema.
|