-
Notifications
You must be signed in to change notification settings - Fork 418
Expand file tree
/
Copy pathindex.ts
More file actions
253 lines (236 loc) · 8.96 KB
/
index.ts
File metadata and controls
253 lines (236 loc) · 8.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import type { H3Event, HTTPEvent, InferEventInput } from "h3";
import * as h3 from "h3";
import { getRequestEvent } from "solid-js/web";
function _setContext(event: H3Event, key: string, value: any) {
event.context[key] = value;
}
function _getContext(event: H3Event, key: string) {
return event.context[key];
}
function getEvent() {
return getRequestEvent()!.nativeEvent;
}
export function getWebRequest(): Request {
return getEvent().req;
}
export const HTTPEventSymbol = Symbol("$HTTPEvent");
export function isEvent(
obj: any,
): obj is h3.H3Event | { [HTTPEventSymbol]: h3.H3Event } {
return (
typeof obj === "object" &&
(obj instanceof h3.H3Event ||
obj?.[HTTPEventSymbol] instanceof h3.H3Event ||
obj?.__is_event__ === true)
);
// Implement logic to check if obj is an H3Event
}
type Tail<T> = T extends [any, ...infer U] ? U : never;
type PrependOverload<
TOriginal extends (...args: Array<any>) => any,
TOverload extends (...args: Array<any>) => any,
> = TOverload & TOriginal;
// add an overload to the function without the event argument
type WrapFunction<TFn extends (...args: Array<any>) => any> = PrependOverload<
TFn,
(
...args: Parameters<TFn> extends [
h3.HTTPEvent<any> | h3.H3Event<any>,
...infer TArgs,
]
? TArgs
: Parameters<TFn>
) => ReturnType<TFn>
>;
function createWrapperFunction<TFn extends (...args: Array<any>) => any>(
h3Function: TFn,
): WrapFunction<TFn> {
return ((...args: Array<any>) => {
const event = args[0];
if (!isEvent(event)) {
args.unshift(getEvent());
} else {
args[0] =
event instanceof h3.H3Event || (event as any).__is_event__
? event
: event[HTTPEventSymbol];
}
return (h3Function as any)(...args);
}) as any;
}
// Creating wrappers for each utility and exporting them with their original names
// readRawBody => getWebRequest().text()/.arrayBuffer()
type WrappedReadBody = <T, TEventInput = InferEventInput<"body", H3Event, T>>(
...args: Tail<Parameters<typeof h3.readBody<T, H3Event, TEventInput>>>
) => ReturnType<typeof h3.readBody<T, h3.H3Event, TEventInput>>;
export const readBody = createWrapperFunction(h3.readBody) as PrependOverload<
typeof h3.readBody,
WrappedReadBody
>;
type WrappedGetQuery = <
T,
TEventInput = Exclude<InferEventInput<"query", H3Event, T>, undefined>,
>(
...args: Tail<Parameters<typeof h3.getQuery<T, H3Event, TEventInput>>>
) => ReturnType<typeof h3.getQuery<T, H3Event, TEventInput>>;
export const getQuery = createWrapperFunction(h3.getQuery) as PrependOverload<
typeof h3.getQuery,
WrappedGetQuery
>;
export const isMethod = createWrapperFunction(h3.isMethod);
export const isPreflightRequest = createWrapperFunction(h3.isPreflightRequest);
type WrappedGetValidatedQuery = <
T extends HTTPEvent,
TEventInput = InferEventInput<"query", H3Event, T>,
>(
...args: Tail<
Parameters<typeof h3.getValidatedQuery<T, H3Event, TEventInput>>
>
) => ReturnType<typeof h3.getValidatedQuery<T, H3Event, TEventInput>>;
export const getValidatedQuery = createWrapperFunction(
h3.getValidatedQuery,
) as PrependOverload<typeof h3.getValidatedQuery, WrappedGetValidatedQuery>;
export const getRouterParams = createWrapperFunction(h3.getRouterParams);
export const getRouterParam = createWrapperFunction(h3.getRouterParam);
type WrappedGetValidatedRouterParams = <
T extends HTTPEvent,
TEventInput = InferEventInput<"routerParams", H3Event, T>,
>(
...args: Tail<
Parameters<typeof h3.getValidatedRouterParams<T, H3Event, TEventInput>>
>
) => ReturnType<typeof h3.getValidatedRouterParams<T, H3Event, TEventInput>>;
export const getValidatedRouterParams = createWrapperFunction(
h3.getValidatedRouterParams,
) as PrependOverload<
typeof h3.getValidatedRouterParams,
WrappedGetValidatedRouterParams
>;
export const assertMethod = createWrapperFunction(h3.assertMethod);
export const getRequestHeaders = createWrapperFunction(h3.getRequestHeaders);
export const getRequestHeader = createWrapperFunction(h3.getRequestHeader);
export const getRequestURL = createWrapperFunction(h3.getRequestURL);
export const getRequestHost = createWrapperFunction(h3.getRequestHost);
export const getRequestProtocol = createWrapperFunction(h3.getRequestProtocol);
export const getRequestIP = createWrapperFunction(h3.getRequestIP);
export const setResponseStatus = (code?: number, text?: string) => {
const e = getEvent();
if (e.res.status !== undefined) e.res.status = code;
if (e.res.statusText !== undefined) e.res.statusText = text;
};
export const getResponseStatus = () => getEvent().res.status;
export const getResponseStatusText = () => getEvent().res.statusText;
export const getResponseHeaders = () =>
Object.fromEntries(getEvent().res.headers.entries());
export const getResponseHeader = (name: string) =>
getEvent().res.headers.get(name);
export const setResponseHeaders = (values: Record<string, string>) => {
const headers = getEvent().res.headers;
for (const [name, value] of Object.entries(values)) {
headers.set(name, value);
}
};
export const setResponseHeader = (name: string, value: string | string[]) => {
const headers = getEvent().res.headers;
(Array.isArray(value) ? value : [value]).forEach((value) => {
headers.set(name, value);
});
};
export const appendResponseHeaders = (values: Record<string, string>) => {
const headers = getEvent().res.headers;
for (const [name, value] of Object.entries(values)) {
headers.append(name, value);
}
};
export const appendResponseHeader = (
name: string,
value: string | string[],
) => {
const headers = getEvent().res.headers;
(Array.isArray(value) ? value : [value]).forEach((value) => {
headers.append(name, value);
});
};
export const defaultContentType = (type: string) =>
getEvent().res.headers.set("content-type", type);
export const proxyRequest = createWrapperFunction(h3.proxyRequest);
export const fetchWithEvent = createWrapperFunction(h3.fetchWithEvent);
export const getProxyRequestHeaders = createWrapperFunction(
h3.getProxyRequestHeaders,
);
export const parseCookies = createWrapperFunction(h3.parseCookies);
export const getCookie = createWrapperFunction(h3.getCookie);
export const setCookie = createWrapperFunction(h3.setCookie);
export const deleteCookie = createWrapperFunction(h3.deleteCookie);
// not exported :(
type SessionDataT = Record<string, any>;
type WrappedUseSession = <T extends SessionDataT>(
...args: Tail<Parameters<typeof h3.useSession<T>>>
) => ReturnType<typeof h3.useSession<T>>;
export const useSession: WrappedUseSession = createWrapperFunction(
h3.useSession,
);
type WrappedGetSession = <T extends SessionDataT>(
...args: Tail<Parameters<typeof h3.getSession<T>>>
) => ReturnType<typeof h3.getSession<T>>;
export const getSession: WrappedGetSession = createWrapperFunction(
h3.getSession,
);
type WrappedUpdateSession = <T extends SessionDataT>(
...args: Tail<Parameters<typeof h3.updateSession<T>>>
) => ReturnType<typeof h3.updateSession<T>>;
export const updateSession: WrappedUpdateSession = createWrapperFunction(
h3.updateSession,
);
type WrappedSealSession = <T extends SessionDataT>(
...args: Tail<Parameters<typeof h3.sealSession<T>>>
) => ReturnType<typeof h3.sealSession<T>>;
export const sealSession: WrappedSealSession = createWrapperFunction(
h3.sealSession,
);
export const unsealSession = createWrapperFunction(h3.unsealSession);
export const clearSession = createWrapperFunction(h3.clearSession);
export const handleCacheHeaders = createWrapperFunction(h3.handleCacheHeaders);
export const handleCors = createWrapperFunction(h3.handleCors);
export const appendCorsHeaders = createWrapperFunction(h3.appendCorsHeaders);
export const appendCorsPreflightHeaders = createWrapperFunction(
h3.appendCorsPreflightHeaders,
);
export const appendHeader = appendResponseHeader;
export const appendHeaders = appendResponseHeaders;
export const setHeader = setResponseHeader;
export const setHeaders = setResponseHeaders;
export const getHeader = getRequestHeader;
export const getHeaders = getRequestHeaders;
export const getRequestFingerprint = createWrapperFunction(
h3.getRequestFingerprint,
);
export const getRequestWebStream = () => getEvent().req.body;
export const readFormData = () => getEvent().req.formData();
type WrappedReadValidatedBody = <
T extends HTTPEvent,
TEventInput = InferEventInput<"body", H3Event, T>,
>(
...args: Tail<
Parameters<typeof h3.readValidatedBody<T, H3Event, TEventInput>>
>
) => ReturnType<typeof h3.readValidatedBody<T, H3Event, TEventInput>>;
export const readValidatedBody = createWrapperFunction(
h3.readValidatedBody,
) as PrependOverload<typeof h3.readValidatedBody, WrappedReadValidatedBody>;
export const getContext = createWrapperFunction(_getContext);
export const setContext = createWrapperFunction(_setContext);
export const removeResponseHeader = (name: string) =>
getEvent().res.headers.delete(name);
export const clearResponseHeaders = (headerNames?: string[]) => {
const headers = getEvent().res.headers;
if (headerNames && headerNames.length > 0) {
for (const name of headerNames) {
headers.delete(name);
}
} else {
for (const name of headers.keys()) {
headers.delete(name);
}
}
};