@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 CHANGED
@@ -1,10 +1,144 @@
1
1
  # shopware/frontends - api-client
2
2
 
3
- Welcome to `@shopware/api-client` package.
3
+ [![](https://img.shields.io/npm/v/@shopware/api-client?color=blue&logo=data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTIiIGhlaWdodD0iMTIiIHZpZXdCb3g9IjAgMCA0ODggNTUzIiBmaWxsPSJub25lIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgo8cGF0aCBkPSJNNDM5LjA0MSAxMjkuNTkzTDI1OC43NjkgMzEuMzA3NkMyNDQuOTE1IDIzLjc1NDEgMjI4LjExNiAyNC4wMDkzIDIxNC40OTcgMzEuOTgwMkw0Ny4yNjkgMTI5Ljg1OEMzMy40NzYzIDEzNy45MzEgMjUgMTUyLjcxMyAyNSAxNjguNjk1VjM4OC40NjZDMjUgNDA0LjczMiAzMy43Nzg1IDQxOS43MzIgNDcuOTYwMiA0MjcuNjk5TDIxNS4xNzggNTIxLjYzNkMyMjguNDUxIDUyOS4wOTIgMjQ0LjU5MyA1MjkuMzMyIDI1OC4wODIgNTIyLjI3NEw0MzguMzY0IDQyNy45MzRDNDUzLjIwMSA0MjAuMTcgNDYyLjUgNDA0LjgwOSA0NjIuNSAzODguMDYzVjE2OS4xMDJDNDYyLjUgMTUyLjYzMiA0NTMuNTAyIDEzNy40NzcgNDM5LjA0MSAxMjkuNTkzWiIgc3Ryb2tlPSJ1cmwoI3BhaW50MF9saW5lYXJfMTUzXzY5MjY1KSIgc3Ryb2tlLXdpZHRoPSI1MCIgc3Ryb2tlLWxpbmVqb2luPSJyb3VuZCIvPgo8ZGVmcz4KPGxpbmVhckdyYWRpZW50IGlkPSJwYWludDBfbGluZWFyXzE1M182OTI2NSIgeDE9Ii0xNi4yOTg5IiB5MT0iMTY1LjM0OSIgeDI9IjI3Ni40MTIiIHkyPSItODkuMzIzNCIgZ3JhZGllbnRVbml0cz0idXNlclNwYWNlT25Vc2UiPgo8c3RvcCBzdG9wLWNvbG9yPSIjMDA4NUZGIi8+CjxzdG9wIG9mZnNldD0iMSIgc3RvcC1jb2xvcj0iI0MwRTJGNSIvPgo8L2xpbmVhckdyYWRpZW50Pgo8L2RlZnM+Cjwvc3ZnPg==)](https://npmjs.com/package/@shopware/api-client)
4
+ [![](https://img.shields.io/github/package-json/v/shopware/frontends?color=blue&filename=packages%2Fapi-client-next%2Fpackage.json&label=frontends/api-client&logo=github)](https://github.com/shopware/frontends/tree/main/packages/api-client-next)
5
+ ![](https://img.shields.io/github/license/shopware/frontends?color=blue)
6
+ [![](https://img.shields.io/github/issues/shopware/frontends/api-client?label=package%20issues&logo=github)](https://github.com/shopware/frontends/issues?q=is%3Aopen+is%3Aissue+label%3Aapi-client)
4
7
 
5
- For getting started documentation visit [https://frontends.shopware.com/](https://frontends.shopware.com/)
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
- Documentation specific for this package: [api-client](https://frontends.shopware.com/packages/api-client.html)
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.3
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
- - [#290](https://github.com/shopware/frontends/pull/290) [`9562a8a`](https://github.com/shopware/frontends/commit/9562a8add35751093d766017abba474f0ad578f8) Thanks [@patzick](https://github.com/patzick)! - ship api-types with package
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