@shopware/api-client 0.1.0 → 0.2.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 CHANGED
@@ -101,7 +101,7 @@ const readNavigation = (params: ApiRequestParams<"readNavigation">) =>
101
101
  {
102
102
  depth: 2,
103
103
  ...params,
104
- }
104
+ },
105
105
  );
106
106
 
107
107
  // in another file you can use it, and depth property will be set to 2 by default
@@ -115,6 +115,25 @@ async function loadMainNavigation() {
115
115
  }
116
116
  ```
117
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
+
118
137
  ## Links
119
138
 
120
139
  - [📘 Documentation](https://frontends.shopware.com)
@@ -127,17 +146,11 @@ async function loadMainNavigation() {
127
146
 
128
147
  Full changelog for stable version is available [here](https://github.com/shopware/frontends/blob/main/packages/api-client-next/CHANGELOG.md)
129
148
 
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
- ```
149
+ ### Latest changes: 0.2.1
140
150
 
141
151
  ### Patch Changes
142
152
 
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
153
+ - [#339](https://github.com/shopware/frontends/pull/339) [`b2fe2bc`](https://github.com/shopware/frontends/commit/b2fe2bc84bc4f3381bc16b9216a935f3c317b0d4) Thanks [@patzick](https://github.com/patzick)! - Query param arrays. This fixes the way how query params are serialized. Previously, array query params were serialized as `?ids=1&ids=2`, now they are serialized as `?ids[]=1&ids[]=2`. This is the proper way of serialization in the Shopware API.
154
+ The definition of the endpoints hasn't changed, so you don't need to change anything in your code.
155
+
156
+ - [#320](https://github.com/shopware/frontends/pull/320) [`8e499e3`](https://github.com/shopware/frontends/commit/8e499e35b3a1a7dc4d1382f8f99b8fc3426e4ac9) Thanks [@mkucmus](https://github.com/mkucmus)! - Prevent setting "null" or "undefined" as token on session init
@@ -1888,7 +1888,7 @@ export type components = {
1888
1888
  */
1889
1889
  href: string;
1890
1890
  meta?: components["schemas"]["meta"];
1891
- }
1891
+ },
1892
1892
  ]
1893
1893
  >;
1894
1894
  /** The "type" and "id" to non-empty members. */
@@ -1888,7 +1888,7 @@ export type components = {
1888
1888
  */
1889
1889
  href: string;
1890
1890
  meta?: components["schemas"]["meta"];
1891
- }
1891
+ },
1892
1892
  ]
1893
1893
  >;
1894
1894
  /** The "type" and "id" to non-empty members. */
@@ -1875,7 +1875,7 @@ export type components = {
1875
1875
  */
1876
1876
  href: string;
1877
1877
  meta?: components["schemas"]["meta"];
1878
- }
1878
+ },
1879
1879
  ]
1880
1880
  >;
1881
1881
  /** The "type" and "id" to non-empty members. */
@@ -2006,7 +2006,7 @@ export type components = {
2006
2006
  */
2007
2007
  href: string;
2008
2008
  meta?: components["schemas"]["meta"];
2009
- }
2009
+ },
2010
2010
  ]
2011
2011
  >;
2012
2012
  /** The "type" and "id" to non-empty members. */
@@ -2006,7 +2006,7 @@ export type components = {
2006
2006
  */
2007
2007
  href: string;
2008
2008
  meta?: components["schemas"]["meta"];
2009
- }
2009
+ },
2010
2010
  ]
2011
2011
  >;
2012
2012
  /** The "type" and "id" to non-empty members. */
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { FetchResponse } from 'ofetch';
2
+
1
3
  /**
2
4
  * This file was auto-generated by openapi-typescript.
3
5
  * Do not make direct changes to the file.
@@ -2004,7 +2006,7 @@ type components = {
2004
2006
  */
2005
2007
  href: string;
2006
2008
  meta?: components["schemas"]["meta"];
2007
- }
2009
+ },
2008
2010
  ]
2009
2011
  >;
2010
2012
  /** The "type" and "id" to non-empty members. */
@@ -7662,6 +7664,48 @@ type operations = {
7662
7664
  };
7663
7665
  };
7664
7666
 
7667
+ type ApiError = {
7668
+ title?: string;
7669
+ detail?: string;
7670
+ code?: string;
7671
+ status?: string;
7672
+ source?: {
7673
+ pointer?: string;
7674
+ };
7675
+ meta?: {
7676
+ parameters?: Record<string, string>;
7677
+ };
7678
+ };
7679
+ declare class ApiClientError<T extends {
7680
+ errors: Array<ApiError>;
7681
+ }> extends Error {
7682
+ /**
7683
+ * Flag to indicate if the request was successful.
7684
+ */
7685
+ ok: boolean;
7686
+ /**
7687
+ * HTTP status code of the response.
7688
+ */
7689
+ status: number;
7690
+ /**
7691
+ * HTTP status text of the response.
7692
+ */
7693
+ statusText?: string;
7694
+ /**
7695
+ * URL of the request.
7696
+ */
7697
+ url: string;
7698
+ /**
7699
+ * Details of the error.
7700
+ */
7701
+ details: T;
7702
+ /**
7703
+ * Headers of the response.
7704
+ */
7705
+ headers: Headers;
7706
+ constructor(response: FetchResponse<T>);
7707
+ }
7708
+
7665
7709
  type Operations = Record<string, unknown>;
7666
7710
  type HttpMethod = "post" | "get" | "put" | "delete" | "patch";
7667
7711
  type GetInferKey<T, NAME extends string> = T extends {
@@ -7706,4 +7750,4 @@ declare function transformPathToQuery<T extends Record<string, unknown>>(path: s
7706
7750
  }
7707
7751
  ];
7708
7752
 
7709
- export { RequestParameters, RequestReturnType, createAPIClient, transformPathToQuery };
7753
+ export { ApiClientError, RequestParameters, RequestReturnType, createAPIClient, transformPathToQuery };
package/dist/index.mjs CHANGED
@@ -1,10 +1,39 @@
1
1
  import { ofetch } from 'ofetch';
2
2
 
3
+ class ApiClientError extends Error {
4
+ constructor(response) {
5
+ let message = "Failed request";
6
+ message += response._data?.errors.reduce((message2, error) => {
7
+ let pointer = "";
8
+ if (error.source?.pointer) {
9
+ pointer = `[${error.source.pointer}]`;
10
+ }
11
+ return `${message2}
12
+ - [${error.title}]${pointer} ${error.detail}`;
13
+ }, "");
14
+ super(message);
15
+ this.name = "ApiClientError";
16
+ this.details = response._data || {
17
+ errors: [{ title: "Unknown error", detail: "" }]
18
+ };
19
+ this.ok = response.ok;
20
+ this.status = response.status;
21
+ this.statusText = response.statusText;
22
+ this.url = response.url;
23
+ this.headers = response.headers;
24
+ }
25
+ }
26
+ function errorInterceptor(response) {
27
+ throw new ApiClientError(response);
28
+ }
29
+
3
30
  function createAPIClient(params) {
4
31
  const defaultHeaders = {
5
- "sw-access-key": params.accessToken,
6
- "sw-context-token": params.contextToken
32
+ "sw-access-key": params.accessToken
7
33
  };
34
+ if (params.contextToken) {
35
+ defaultHeaders["sw-context-token"] = params.contextToken;
36
+ }
8
37
  const apiFetch = ofetch.create({
9
38
  baseURL: params.baseURL,
10
39
  // async onRequest({ request, options }) {},
@@ -15,8 +44,10 @@ function createAPIClient(params) {
15
44
  defaultHeaders["sw-context-token"] = newContextToken;
16
45
  params.onContextChanged?.(newContextToken);
17
46
  }
47
+ },
48
+ async onResponseError({ request, response, options }) {
49
+ errorInterceptor(response);
18
50
  }
19
- // async onResponseError({ request, response, options }) {},
20
51
  });
21
52
  async function invoke(pathParam, params2) {
22
53
  const [requestPath, options] = transformPathToQuery(
@@ -53,7 +84,11 @@ function transformPathToQuery(path, params) {
53
84
  });
54
85
  const query = {};
55
86
  queryParamNames.forEach((paramName) => {
56
- query[paramName] = params[paramName];
87
+ let queryParamName = paramName;
88
+ if (Array.isArray(params[paramName]) && !queryParamName.includes("[]")) {
89
+ queryParamName += "[]";
90
+ }
91
+ query[queryParamName] = params[paramName];
57
92
  });
58
93
  const returnOptions = {
59
94
  method: method.toUpperCase(),
@@ -69,4 +104,4 @@ function transformPathToQuery(path, params) {
69
104
  return [requestPathWithParams, returnOptions];
70
105
  }
71
106
 
72
- export { createAPIClient, transformPathToQuery };
107
+ export { ApiClientError, createAPIClient, transformPathToQuery };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shopware/api-client",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
4
  "description": "Shopware client for API connection.",
5
5
  "author": "Shopware",
6
6
  "repository": {
@@ -27,12 +27,12 @@
27
27
  "import": "./dist/index.mjs"
28
28
  },
29
29
  "devDependencies": {
30
- "@types/prettier": "^2.7.3",
31
- "@vitest/coverage-c8": "^0.32.2",
32
- "prettier": "^2.8.8",
33
- "vitest": "^0.32.2",
34
- "eslint-config-shopware": "0.0.3",
35
- "@shopware/api-gen": "0.0.4",
30
+ "@types/prettier": "^3.0.0",
31
+ "@vitest/coverage-c8": "^0.33.0",
32
+ "prettier": "^3.0.0",
33
+ "vitest": "^0.33.0",
34
+ "@shopware/api-gen": "0.0.6",
35
+ "eslint-config-shopware": "0.0.5",
36
36
  "tsconfig": "0.0.0"
37
37
  },
38
38
  "dependencies": {