-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhey-api.ts
More file actions
62 lines (45 loc) · 1.96 KB
/
hey-api.ts
File metadata and controls
62 lines (45 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { isServer } from '@/utils/runtime';
import { ROUTES } from '@/constants/routes';
import { getPublicEnv } from '@/config/process-env';
// import { waitMs } from '@/utils/wait';
import type { CreateClientConfig } from '@/client/client.gen';
const { CLIENT_PROXY } = ROUTES.API;
/** Runtime config. Runs and imported both on server and in browser. */
export const createClientConfig: CreateClientConfig = (config) => {
const { API_URL } = getPublicEnv();
return {
...config,
baseUrl: API_URL,
credentials: 'include',
fetch: isServer() ? serverFetch : clientFetch,
};
};
const serverFetch: typeof fetch = async (input, init = {}) => {
// Note: Dynamic import to avoid bundling 'next/headers' on client
const { cookies } = await import('next/headers');
const cookieStore = await cookies();
const cookieHeader = cookieStore
.getAll()
.map((c) => `${c.name}=${c.value}`)
.join('; ');
// Note: must append auth_cookie like this or content-type header will break in server actions
const headers = new Headers(init.headers);
headers.append('Cookie', cookieHeader);
// test skeletons styling
// await waitMs(3000);
const response = fetch(input, { ...init, headers });
return response;
};
/** Client-side fetch: forwards requests to api/client-proxy/[...path]/route.ts */
const clientFetch: typeof fetch = async (input, init = {}) => {
const { API_URL } = getPublicEnv() as { API_URL: string };
// hey-api sends absolute URL
const url: string = typeof input === 'string' ? input : input.toString();
// Normalize to relative URL
// API_URL.length + 1 - removes leading slash, API_URL guaranteed not to have trailing slash
const relativeUrl = url.startsWith(API_URL) ? url.slice(API_URL.length + 1) : url;
// Build the proxy URL relative to Next.js API
const proxyUrl = `${CLIENT_PROXY}${relativeUrl}`;
const headers = new Headers(init.headers);
return fetch(proxyUrl, { ...init, headers, credentials: 'include' });
};