@shopware/api-client 0.1.0 → 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
@@ -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,29 @@ 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
149
+ ### Latest changes: 0.2.0
131
150
 
132
151
  ### Minor Changes
133
152
 
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:
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:
135
156
 
136
157
  ```typescript
137
- const readCart = (params: RequestParameters<"readCart", operations>) =>
138
- apiInstance.invoke("readCart get /checkout/cart?name", params);
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
+ }
139
170
  ```
140
171
 
141
172
  ### Patch Changes
142
173
 
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
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
@@ -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,5 +1,32 @@
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
32
  "sw-access-key": params.accessToken,
@@ -15,8 +42,10 @@ function createAPIClient(params) {
15
42
  defaultHeaders["sw-context-token"] = newContextToken;
16
43
  params.onContextChanged?.(newContextToken);
17
44
  }
45
+ },
46
+ async onResponseError({ request, response, options }) {
47
+ errorInterceptor(response);
18
48
  }
19
- // async onResponseError({ request, response, options }) {},
20
49
  });
21
50
  async function invoke(pathParam, params2) {
22
51
  const [requestPath, options] = transformPathToQuery(
@@ -69,4 +98,4 @@ function transformPathToQuery(path, params) {
69
98
  return [requestPathWithParams, returnOptions];
70
99
  }
71
100
 
72
- export { createAPIClient, transformPathToQuery };
101
+ 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.0",
4
4
  "description": "Shopware client for API connection.",
5
5
  "author": "Shopware",
6
6
  "repository": {
@@ -28,11 +28,11 @@
28
28
  },
29
29
  "devDependencies": {
30
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",
31
+ "@vitest/coverage-c8": "^0.33.0",
32
+ "prettier": "^3.0.0",
33
+ "vitest": "^0.33.0",
34
+ "@shopware/api-gen": "0.0.5",
35
+ "eslint-config-shopware": "0.0.4",
36
36
  "tsconfig": "0.0.0"
37
37
  },
38
38
  "dependencies": {