Skip to content

Commit 4d08944

Browse files
Benoit NgoNgob
authored andcommitted
chore(appFetch): upgrade to ofetch.create
1 parent 54cfe43 commit 4d08944

File tree

1 file changed

+16
-74
lines changed

1 file changed

+16
-74
lines changed

apps/front/src/plugins/appFetch.ts

Lines changed: 16 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -12,80 +12,22 @@ export default defineNuxtPlugin(() => {
1212
} = useRequestHeaders(["cookie"]) as {
1313
[key: string]: string;
1414
};
15-
16-
const handleException = (e: any) => {
17-
logger.error("An error hapenned during an API call");
18-
logger.error(e);
19-
// Check if 401 so remove auth info
20-
if (e && e.response && e.response.status === 401 && store.isAuthenticated) {
21-
logger.error("401 error, removing authentication informations");
22-
store.resetAuth();
23-
}
24-
// HERE the bug
25-
const cookies = e.response.headers.get("set-cookie") || "";
26-
if (process.server && cookies) {
27-
event.res.setHeader("set-cookie", cookies);
28-
}
29-
throw e;
30-
};
31-
const fetchRaw = async <T>(
32-
request: NitroFetchRequest,
33-
opts?: NitroFetchOptions<"json">
34-
) => {
35-
const res = await $fetch.raw<T>(request, {
36-
headers,
37-
baseURL: API_URL,
38-
...opts,
39-
});
40-
41-
const cookies = res.headers.get("set-cookie") || "";
42-
if (process.server && cookies) {
43-
event.res.setHeader("set-cookie", cookies);
44-
}
45-
return res;
46-
};
47-
48-
const fetchNative = async <T>(
49-
request: NitroFetchRequest,
50-
opts?: NitroFetchOptions<"json">
51-
) => {
52-
const res = await $fetch.raw<T>(request, {
53-
headers,
54-
baseURL: API_URL,
55-
...opts,
56-
});
57-
58-
const cookies = res.headers.get("set-cookie") || "";
59-
if (process.server && cookies) {
60-
event.res.setHeader("set-cookie", cookies);
61-
}
62-
return res._data;
63-
};
64-
65-
const _appFetchRaw = async <T>(
66-
request: NitroFetchRequest,
67-
opts?: NitroFetchOptions<"json">
68-
) => {
69-
try {
70-
return await fetchRaw<T>(request, opts);
71-
} catch (e: any) {
72-
handleException(e);
73-
}
74-
};
75-
const appFetch = async <T>(
76-
request: NitroFetchRequest,
77-
opts?: NitroFetchOptions<"json">
78-
) => {
79-
try {
80-
return await fetchNative<T>(request, opts);
81-
} catch (e: any) {
82-
handleException(e);
83-
}
84-
};
85-
appFetch.raw = _appFetchRaw;
86-
// When you use appFetch.create, you should ensure that you dont need to bridge cookies
87-
appFetch.create = $fetch.create;
88-
15+
const appFetch = $fetch.create({
16+
baseURL: API_URL,
17+
headers,
18+
onResponse(context) {
19+
const res = context.response;
20+
const cookies = res.headers.get("set-cookie") || "";
21+
console.log(res.status);
22+
if (res.status === 401 && store.isAuthenticated) {
23+
logger.error("401 error, removing authentication informations");
24+
store.resetAuth();
25+
}
26+
if (process.server && cookies) {
27+
event.node.res.setHeader("set-cookie", cookies);
28+
}
29+
},
30+
});
8931
return {
9032
provide: {
9133
// // https://nuxt.com/docs/getting-started/data-fetching#example-pass-client-headers-to-the-api

0 commit comments

Comments
 (0)