@shopware/api-client 0.0.3 → 0.1.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,125 @@
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
+ ## Links
119
+
120
+ - [📘 Documentation](https://frontends.shopware.com)
121
+
122
+ - [👥 Community Slack](https://shopwarecommunity.slack.com) (`#shopware-frontends` channel)
8
123
 
9
124
  <!-- AUTO GENERATED CHANGELOG -->
10
125
 
@@ -12,8 +127,17 @@ Documentation specific for this package: [api-client](https://frontends.shopware
12
127
 
13
128
  Full changelog for stable version is available [here](https://github.com/shopware/frontends/blob/main/packages/api-client-next/CHANGELOG.md)
14
129
 
15
- ### Latest changes: 0.0.3
130
+ ### Latest changes: 0.1.0
131
+
132
+ ### Minor Changes
133
+
134
+ - [#300](https://github.com/shopware/frontends/pull/300) [`da347b5`](https://github.com/shopware/frontends/commit/da347b548aea93afaab1cc9ebab63f732ecdb964) Thanks [@patzick](https://github.com/patzick)! - Predefining methods: exported `RequestReturnType ` and `RequestParameters` types. You can now create predefined methods:
135
+
136
+ ```typescript
137
+ const readCart = (params: RequestParameters<"readCart", operations>) =>
138
+ apiInstance.invoke("readCart get /checkout/cart?name", params);
139
+ ```
16
140
 
17
141
  ### Patch Changes
18
142
 
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
143
+ - [#295](https://github.com/shopware/frontends/pull/295) [`23a0a53`](https://github.com/shopware/frontends/commit/23a0a532410990c0075ea7fff622949ccdecfd49) Thanks [@patzick](https://github.com/patzick)! - bump dependencies