Skip to content

Commit deef3fe

Browse files
authored
feat: Attach fetch response to SWR data/error
It is useful to have the original fetch response available in the hook result. For instance to distinguish the HTTP status code in case of an `error`. Since SWR only returns `error` / `data` properties, this provides an escape hatch to be able to access to original `fetch` response, as returned by the `openapi-fetch` client.
1 parent 47e4b5e commit deef3fe

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

packages/swr-openapi/src/query-base.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import type { Fetcher, SWRHook } from "swr";
44
import type { TypesForGetRequest } from "./types.js";
55
import { useCallback, useDebugValue, useMemo } from "react";
66

7+
export const RESPONSE = Symbol.for('response');
8+
79
/**
810
* @private
911
*/
@@ -35,8 +37,18 @@ export function configureBaseQueryHook(useHook: SWRHook) {
3537
// @ts-expect-error TODO: Improve internal init types
3638
const res = await client.GET(path, init);
3739
if (res.error) {
40+
Object.defineProperty(res.error, RESPONSE, {
41+
value: res.response,
42+
enumerable: false,
43+
});
3844
throw res.error;
3945
}
46+
if (res.data) {
47+
Object.defineProperty(res.data, RESPONSE, {
48+
value: res.response,
49+
enumerable: false,
50+
});
51+
}
4052
return res.data as Data;
4153
},
4254
[client],

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy