@parallelworks/client 7.12.7 → 7.13.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/dist/index.js +24 -1
- package/dist/swr/index.d.ts +13 -0
- package/dist/swr/index.js +15 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -236,7 +236,7 @@ export class Client {
|
|
|
236
236
|
* @returns Configured openapi-fetch client instance
|
|
237
237
|
*/
|
|
238
238
|
build() {
|
|
239
|
-
|
|
239
|
+
const client = createClient({
|
|
240
240
|
baseUrl: this.baseUrl,
|
|
241
241
|
...this.options,
|
|
242
242
|
headers: {
|
|
@@ -244,6 +244,29 @@ export class Client {
|
|
|
244
244
|
...(this.authHeader && { Authorization: this.authHeader }),
|
|
245
245
|
},
|
|
246
246
|
});
|
|
247
|
+
// Attach HTTP status code to error response bodies so consumers
|
|
248
|
+
// (e.g. SWR hooks) can distinguish 404s from other errors.
|
|
249
|
+
// openapi-fetch parses the body after middleware, so we replace the
|
|
250
|
+
// response with one whose body includes the status field.
|
|
251
|
+
const statusMiddleware = {
|
|
252
|
+
async onResponse({ response }) {
|
|
253
|
+
if (!response.ok) {
|
|
254
|
+
const body = await response.json().catch(() => ({}));
|
|
255
|
+
if (body && typeof body === 'object') {
|
|
256
|
+
;
|
|
257
|
+
body.status = response.status;
|
|
258
|
+
}
|
|
259
|
+
return new Response(JSON.stringify(body), {
|
|
260
|
+
status: response.status,
|
|
261
|
+
statusText: response.statusText,
|
|
262
|
+
headers: response.headers,
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
return response;
|
|
266
|
+
},
|
|
267
|
+
};
|
|
268
|
+
client.use(statusMiddleware);
|
|
269
|
+
return client;
|
|
247
270
|
}
|
|
248
271
|
}
|
|
249
272
|
export default Client;
|
package/dist/swr/index.d.ts
CHANGED
|
@@ -3,6 +3,19 @@ export interface SwrHooksOptions {
|
|
|
3
3
|
/** Key prefix for SWR cache (default: 'api') */
|
|
4
4
|
prefix?: string;
|
|
5
5
|
}
|
|
6
|
+
/**
|
|
7
|
+
* HTTP status code injected by the client middleware into error responses.
|
|
8
|
+
* Use this to check the status code of API errors from SWR hooks.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```tsx
|
|
12
|
+
* const { error } = useQuery('/api/resource')
|
|
13
|
+
* if (error && hasStatus(error, 404)) {
|
|
14
|
+
* return <NotFound />
|
|
15
|
+
* }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function hasStatus(error: Record<string, unknown>, status: number): boolean;
|
|
6
19
|
type AuthenticatedClient = ReturnType<Client['withApiKey']>;
|
|
7
20
|
/**
|
|
8
21
|
* Create SWR hooks from an authenticated Parallel Works client
|
package/dist/swr/index.js
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import { createImmutableHook, createInfiniteHook, createQueryHook, } from 'swr-openapi';
|
|
3
|
+
/**
|
|
4
|
+
* HTTP status code injected by the client middleware into error responses.
|
|
5
|
+
* Use this to check the status code of API errors from SWR hooks.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```tsx
|
|
9
|
+
* const { error } = useQuery('/api/resource')
|
|
10
|
+
* if (error && hasStatus(error, 404)) {
|
|
11
|
+
* return <NotFound />
|
|
12
|
+
* }
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export function hasStatus(error, status) {
|
|
16
|
+
return error.status === status;
|
|
17
|
+
}
|
|
3
18
|
/**
|
|
4
19
|
* Create SWR hooks from an authenticated Parallel Works client
|
|
5
20
|
*
|