@shopware/api-client 0.0.3 → 0.2.0
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 +160 -5
- package/api-types/apiSchema-6.5.3.0.json +12154 -0
- package/api-types/apiTypes-6.4.19.0.d.ts +1 -1
- package/api-types/apiTypes-6.4.20.0.d.ts +1 -1
- package/api-types/apiTypes-6.5.0.0.d.ts +1 -1
- package/api-types/apiTypes-6.5.2.0.d.ts +1 -1
- package/api-types/apiTypes-6.5.3.0.d.ts +7738 -0
- package/api-types/index.d.ts +1 -1
- package/dist/index.d.ts +207 -161
- package/dist/index.mjs +40 -8
- package/package.json +11 -8
package/README.md
CHANGED
|
@@ -1,10 +1,144 @@
|
|
|
1
1
|
# shopware/frontends - api-client
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://npmjs.com/package/@shopware/api-client)
|
|
4
|
+
[](https://github.com/shopware/frontends/tree/main/packages/api-client-next)
|
|
5
|
+

|
|
6
|
+
[](https://github.com/shopware/frontends/issues?q=is%3Aopen+is%3Aissue+label%3Aapi-client)
|
|
4
7
|
|
|
5
|
-
|
|
8
|
+
Dynamic and fully typed API Client for Shopware 6. Usable in any JavaScript an TypeScript project.
|
|
9
|
+
You can use types generated from your custom API instance to have autocompletion and type safety.
|
|
6
10
|
|
|
7
|
-
|
|
11
|
+
## Setup
|
|
12
|
+
|
|
13
|
+
Install npm package:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
# Using pnpm
|
|
17
|
+
pnpm add @shopware/api-client
|
|
18
|
+
|
|
19
|
+
# Using yarn
|
|
20
|
+
yarn add @shopware/api-client
|
|
21
|
+
|
|
22
|
+
# Using npm
|
|
23
|
+
npm i @shopware/api-client
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Recommended practice is to create separate module file. For example `src/apiClient.ts`, and import it whenever you need to use API Client.
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { createAPIClient } from "@shopware/api-client";
|
|
30
|
+
|
|
31
|
+
// You can pick types of your current API version, the default one:
|
|
32
|
+
import {
|
|
33
|
+
operationPaths,
|
|
34
|
+
operations,
|
|
35
|
+
components,
|
|
36
|
+
} from "@shopware/api-client/api-types";
|
|
37
|
+
// or (specific version):
|
|
38
|
+
import {
|
|
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";
|
|
45
|
+
|
|
46
|
+
// you can pick cookies library of your choice
|
|
47
|
+
import Cookies from "js-cookie";
|
|
48
|
+
|
|
49
|
+
export const apiClient = createAPIClient<operations, operationPaths>({
|
|
50
|
+
baseURL: "https://demo-frontends.shopware.store/store-api",
|
|
51
|
+
accessToken: "SWSCBHFSNTVMAWNZDNFKSHLAYW",
|
|
52
|
+
contextToken: Cookies.get("sw-context-token"),
|
|
53
|
+
apiType: "store-api",
|
|
54
|
+
onContextChanged(newContextToken) {
|
|
55
|
+
Cookies.set("sw-context-token", newContextToken, {
|
|
56
|
+
expires: 365, // days
|
|
57
|
+
path: "/",
|
|
58
|
+
sameSite: "lax",
|
|
59
|
+
});
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// reimport schemas to use it in application
|
|
64
|
+
export type ApiSchemas = components["schemas"];
|
|
65
|
+
// reimport operations request parameters to use it in application
|
|
66
|
+
export type ApiRequestParams<OPERATION_NAME extends keyof operations> =
|
|
67
|
+
RequestParameters<OPERATION_NAME, operations>;
|
|
68
|
+
// reimport operations return types to use it in application
|
|
69
|
+
export type ApiReturnType<OPERATION_NAME extends keyof operations> =
|
|
70
|
+
RequestReturnType<OPERATION_NAME, operations>;
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Basic usage
|
|
74
|
+
|
|
75
|
+
Take a look at [example project using API Client](https://stackblitz.com/github/shopware/frontends/tree/main/examples/new-api-client).
|
|
76
|
+
|
|
77
|
+
### Simple invocation
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
import { apiClient, ApiReturnType } from "./apiClient";
|
|
81
|
+
|
|
82
|
+
// could be reactive value, you can use ApiReturnType to type it properly
|
|
83
|
+
let productsResponse: ApiReturnType<"readProduct">;
|
|
84
|
+
|
|
85
|
+
async function loadProducts() {
|
|
86
|
+
productsResponse = await apiClient.invoke("readProduct post /product", {
|
|
87
|
+
limit: 2,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Predefining methods
|
|
93
|
+
|
|
94
|
+
If you prefer to add another layer of abstraction you can use created previously types to define your own concept of methods.
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
// add for example into apiClient.ts file
|
|
98
|
+
const readNavigation = (params: ApiRequestParams<"readNavigation">) =>
|
|
99
|
+
apiClient.invoke(
|
|
100
|
+
"readNavigation post /navigation/{activeId}/{rootId} sw-include-seo-urls",
|
|
101
|
+
{
|
|
102
|
+
depth: 2,
|
|
103
|
+
...params,
|
|
104
|
+
},
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
// in another file you can use it, and depth property will be set to 2 by default
|
|
108
|
+
import { readNavigation } from "./apiClient";
|
|
109
|
+
|
|
110
|
+
async function loadMainNavigation() {
|
|
111
|
+
const navigation = await readNavigation({
|
|
112
|
+
activeId: "main-navigation",
|
|
113
|
+
rootId: "main-navigation",
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Error handling
|
|
119
|
+
|
|
120
|
+
Client is throwing `ApiClientError` with detailed information returned from the API. It will display clear message in the console or you can access `details` property to get raw information from the response.
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
import { ApiClientError } from "@shopware/api-client";
|
|
124
|
+
|
|
125
|
+
try {
|
|
126
|
+
// ... your request
|
|
127
|
+
} catch (error) {
|
|
128
|
+
if (error instanceof ApiClientError) {
|
|
129
|
+
console.error(error); // This prints message summary
|
|
130
|
+
console.error("Details:", error.details); // Raw response from API
|
|
131
|
+
} else {
|
|
132
|
+
console.error("==>", error); // Another type of error, not recognized by API client
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Links
|
|
138
|
+
|
|
139
|
+
- [📘 Documentation](https://frontends.shopware.com)
|
|
140
|
+
|
|
141
|
+
- [👥 Community Slack](https://shopwarecommunity.slack.com) (`#shopware-frontends` channel)
|
|
8
142
|
|
|
9
143
|
<!-- AUTO GENERATED CHANGELOG -->
|
|
10
144
|
|
|
@@ -12,8 +146,29 @@ Documentation specific for this package: [api-client](https://frontends.shopware
|
|
|
12
146
|
|
|
13
147
|
Full changelog for stable version is available [here](https://github.com/shopware/frontends/blob/main/packages/api-client-next/CHANGELOG.md)
|
|
14
148
|
|
|
15
|
-
### Latest changes: 0.0
|
|
149
|
+
### Latest changes: 0.2.0
|
|
150
|
+
|
|
151
|
+
### Minor Changes
|
|
152
|
+
|
|
153
|
+
- [#316](https://github.com/shopware/frontends/pull/316) [`589c09c`](https://github.com/shopware/frontends/commit/589c09cdd9dee0db172c371afc5ecd740bdb4723) Thanks [@patzick](https://github.com/patzick)! - Improved error handling. Api client now throws `ApiClientError` with detailed information about what went wrong with request.
|
|
154
|
+
|
|
155
|
+
example:
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
import { ApiClientError } from "@shopware/api-client";
|
|
159
|
+
|
|
160
|
+
try {
|
|
161
|
+
// ... your request
|
|
162
|
+
} catch (error) {
|
|
163
|
+
if (error instanceof ApiClientError) {
|
|
164
|
+
console.error(error); // This prints message summary
|
|
165
|
+
console.error("Details:", error.details); // Raw response from API
|
|
166
|
+
} else {
|
|
167
|
+
console.error("==>", error); // Another type of error, not recognized by API client
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
```
|
|
16
171
|
|
|
17
172
|
### Patch Changes
|
|
18
173
|
|
|
19
|
-
- [#
|
|
174
|
+
- [#303](https://github.com/shopware/frontends/pull/303) [`aeb639a`](https://github.com/shopware/frontends/commit/aeb639a3244f812c275145345618e5bc0045be0d) Thanks [@patzick](https://github.com/patzick)! - Improved linting in packages. Types should be more reliable
|