better-call 1.3.3 → 2.0.0-beta.2
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/client.cjs.map +1 -1
- package/dist/client.d.cts +13 -17
- package/dist/client.d.mts +13 -17
- package/dist/client.mjs.map +1 -1
- package/dist/context.cjs +10 -9
- package/dist/context.cjs.map +1 -1
- package/dist/context.d.cts +129 -323
- package/dist/context.d.mts +129 -323
- package/dist/context.mjs +10 -9
- package/dist/context.mjs.map +1 -1
- package/dist/cookies.cjs +1 -1
- package/dist/cookies.d.cts +1 -12
- package/dist/cookies.d.mts +1 -12
- package/dist/cookies.mjs +1 -1
- package/dist/crypto.cjs.map +1 -1
- package/dist/crypto.mjs.map +1 -1
- package/dist/endpoint.cjs +21 -7
- package/dist/endpoint.cjs.map +1 -1
- package/dist/endpoint.d.cts +197 -392
- package/dist/endpoint.d.mts +197 -392
- package/dist/endpoint.mjs +21 -7
- package/dist/endpoint.mjs.map +1 -1
- package/dist/error.cjs +1 -1
- package/dist/error.cjs.map +1 -1
- package/dist/error.mjs +1 -1
- package/dist/error.mjs.map +1 -1
- package/dist/helper.d.cts +2 -3
- package/dist/helper.d.mts +2 -3
- package/dist/index.cjs +3 -10
- package/dist/index.d.cts +9 -8
- package/dist/index.d.mts +9 -8
- package/dist/index.mjs +3 -4
- package/dist/middleware.cjs +59 -13
- package/dist/middleware.cjs.map +1 -1
- package/dist/middleware.d.cts +85 -42
- package/dist/middleware.d.mts +85 -42
- package/dist/middleware.mjs +59 -13
- package/dist/middleware.mjs.map +1 -1
- package/dist/node.cjs.map +1 -1
- package/dist/node.d.cts +1 -1
- package/dist/node.d.mts +1 -1
- package/dist/node.mjs.map +1 -1
- package/dist/openapi.cjs.map +1 -1
- package/dist/openapi.d.cts +1 -1
- package/dist/openapi.d.mts +1 -1
- package/dist/openapi.mjs.map +1 -1
- package/dist/router.cjs.map +1 -1
- package/dist/router.mjs.map +1 -1
- package/dist/to-response.cjs +1 -1
- package/dist/to-response.cjs.map +1 -1
- package/dist/to-response.mjs +1 -1
- package/dist/to-response.mjs.map +1 -1
- package/dist/types.d.cts +142 -0
- package/dist/types.d.mts +142 -0
- package/dist/validator.cjs +1 -1
- package/dist/validator.cjs.map +1 -1
- package/dist/validator.mjs +1 -1
- package/dist/validator.mjs.map +1 -1
- package/package.json +3 -3
package/dist/middleware.d.cts
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
|
+
import { CookieOptions, CookiePrefixOptions } from "./cookies.cjs";
|
|
2
|
+
import { APIError, Status, statusCodes } from "./error.cjs";
|
|
1
3
|
import { Prettify } from "./helper.cjs";
|
|
2
|
-
import {
|
|
3
|
-
import { EndpointContext, EndpointOptions } from "./endpoint.cjs";
|
|
4
|
+
import { InferUse } from "./types.cjs";
|
|
4
5
|
|
|
5
6
|
//#region src/middleware.d.ts
|
|
6
|
-
|
|
7
|
-
type MiddlewareResponse = null | void | undefined | Record<string, any>;
|
|
8
|
-
type MiddlewareContext<Options extends MiddlewareOptions, Context = {}> = EndpointContext<string, Options & {
|
|
9
|
-
method: "*";
|
|
10
|
-
}> & {
|
|
7
|
+
type MiddlewareContext<Context = {}> = {
|
|
11
8
|
/**
|
|
12
9
|
* Method
|
|
13
10
|
*
|
|
@@ -26,98 +23,144 @@ type MiddlewareContext<Options extends MiddlewareOptions, Context = {}> = Endpoi
|
|
|
26
23
|
* The body object will be the parsed JSON from the request and validated
|
|
27
24
|
* against the body schema if it exists
|
|
28
25
|
*/
|
|
29
|
-
body:
|
|
26
|
+
body: any;
|
|
30
27
|
/**
|
|
31
28
|
* Query
|
|
32
29
|
*
|
|
33
30
|
* The query object will be the parsed query string from the request
|
|
34
31
|
* and validated against the query schema if it exists
|
|
35
32
|
*/
|
|
36
|
-
query:
|
|
33
|
+
query: Record<string, any> | undefined;
|
|
37
34
|
/**
|
|
38
35
|
* Params
|
|
39
36
|
*
|
|
40
37
|
* If the path is `/user/:id` and the request is `/user/1` then the
|
|
41
|
-
* params will
|
|
42
|
-
* be `{
|
|
43
|
-
* then the
|
|
44
|
-
*
|
|
45
|
-
* wildcard
|
|
46
|
-
* is named like `/user/**:name` then the params will be `{ name: string }`
|
|
38
|
+
* params will be `{ id: "1" }` and if the path includes a wildcard like
|
|
39
|
+
* `/user/*` then the params will be `{ _: "1" }` where `_` is the wildcard
|
|
40
|
+
* key. If the wildcard is named like `/user/**:name` then the params will
|
|
41
|
+
* be `{ name: string }`
|
|
47
42
|
*/
|
|
48
|
-
params: string;
|
|
43
|
+
params: Record<string, any> | undefined;
|
|
49
44
|
/**
|
|
50
45
|
* Request object
|
|
51
46
|
*
|
|
52
47
|
* If `requireRequest` is set to true in the endpoint options this will be
|
|
53
48
|
* required
|
|
54
49
|
*/
|
|
55
|
-
request:
|
|
50
|
+
request: Request | undefined;
|
|
56
51
|
/**
|
|
57
52
|
* Headers
|
|
58
53
|
*
|
|
59
54
|
* If `requireHeaders` is set to true in the endpoint options this will be
|
|
60
55
|
* required
|
|
61
56
|
*/
|
|
62
|
-
headers:
|
|
57
|
+
headers: Headers | undefined;
|
|
63
58
|
/**
|
|
64
59
|
* Set header
|
|
65
60
|
*
|
|
66
61
|
* If it's called outside of a request it will just be ignored.
|
|
67
62
|
*/
|
|
68
63
|
setHeader: (key: string, value: string) => void;
|
|
64
|
+
/**
|
|
65
|
+
* Set the response status code
|
|
66
|
+
*/
|
|
67
|
+
setStatus: (status: Status) => void;
|
|
69
68
|
/**
|
|
70
69
|
* Get header
|
|
71
70
|
*
|
|
72
71
|
* If it's called outside of a request it will just return null
|
|
73
72
|
*
|
|
74
|
-
* @param key
|
|
75
|
-
* @returns
|
|
73
|
+
* @param key - The key of the header
|
|
76
74
|
*/
|
|
77
75
|
getHeader: (key: string) => string | null;
|
|
76
|
+
/**
|
|
77
|
+
* Get a cookie value from the request
|
|
78
|
+
*
|
|
79
|
+
* @param key - The key of the cookie
|
|
80
|
+
* @param prefix - The prefix of the cookie between `__Secure-` and `__Host-`
|
|
81
|
+
* @returns The value of the cookie
|
|
82
|
+
*/
|
|
83
|
+
getCookie: (key: string, prefix?: CookiePrefixOptions) => string | null;
|
|
84
|
+
/**
|
|
85
|
+
* Get a signed cookie value from the request
|
|
86
|
+
*
|
|
87
|
+
* @param key - The key of the cookie
|
|
88
|
+
* @param secret - The secret of the signed cookie
|
|
89
|
+
* @param prefix - The prefix of the cookie between `__Secure-` and `__Host-`
|
|
90
|
+
* @returns The value of the cookie or null if the cookie is not found or false if the signature is invalid
|
|
91
|
+
*/
|
|
92
|
+
getSignedCookie: (key: string, secret: string, prefix?: CookiePrefixOptions) => Promise<string | null | false>;
|
|
93
|
+
/**
|
|
94
|
+
* Set a cookie value in the response
|
|
95
|
+
*
|
|
96
|
+
* @param key - The key of the cookie
|
|
97
|
+
* @param value - The value to set
|
|
98
|
+
* @param options - The options of the cookie
|
|
99
|
+
* @returns The cookie string
|
|
100
|
+
*/
|
|
101
|
+
setCookie: (key: string, value: string, options?: CookieOptions) => string;
|
|
102
|
+
/**
|
|
103
|
+
* Set signed cookie
|
|
104
|
+
*
|
|
105
|
+
* @param key - The key of the cookie
|
|
106
|
+
* @param value - The value to set
|
|
107
|
+
* @param secret - The secret to sign the cookie with
|
|
108
|
+
* @param options - The options of the cookie
|
|
109
|
+
* @returns The cookie string
|
|
110
|
+
*/
|
|
111
|
+
setSignedCookie: (key: string, value: string, secret: string, options?: CookieOptions) => Promise<string>;
|
|
78
112
|
/**
|
|
79
113
|
* JSON
|
|
80
114
|
*
|
|
81
|
-
*
|
|
82
|
-
* the
|
|
83
|
-
*
|
|
84
|
-
* the context then
|
|
85
|
-
* it will return a Response object instead of the
|
|
86
|
-
* JSON object.
|
|
115
|
+
* A helper function to create a JSON response with the correct headers
|
|
116
|
+
* and status code. If `asResponse` is set to true in the context then
|
|
117
|
+
* it will return a Response object instead of the JSON object.
|
|
87
118
|
*
|
|
88
119
|
* @param json - The JSON object to return
|
|
89
|
-
* @param routerResponse - The response object to
|
|
90
|
-
* return if `asResponse` is
|
|
120
|
+
* @param routerResponse - The response object to return if `asResponse` is
|
|
91
121
|
* true in the context this will take precedence
|
|
92
122
|
*/
|
|
93
123
|
json: <R extends Record<string, any> | null>(json: R, routerResponse?: {
|
|
94
124
|
status?: number;
|
|
95
125
|
headers?: Record<string, string>;
|
|
96
126
|
response?: Response;
|
|
97
|
-
|
|
127
|
+
body?: Record<string, any>;
|
|
128
|
+
} | Response) => R;
|
|
98
129
|
/**
|
|
99
130
|
* Middleware context
|
|
100
131
|
*/
|
|
101
132
|
context: Prettify<Context>;
|
|
133
|
+
/**
|
|
134
|
+
* Redirect to a new URL
|
|
135
|
+
*/
|
|
136
|
+
redirect: (url: string) => APIError;
|
|
137
|
+
/**
|
|
138
|
+
* Return error
|
|
139
|
+
*/
|
|
140
|
+
error: (status: keyof typeof statusCodes | Status, body?: {
|
|
141
|
+
message?: string;
|
|
142
|
+
code?: string;
|
|
143
|
+
} & Record<string, any>, headers?: HeadersInit) => APIError;
|
|
144
|
+
asResponse?: boolean;
|
|
145
|
+
returnHeaders?: boolean;
|
|
146
|
+
returnStatus?: boolean;
|
|
147
|
+
responseHeaders: Headers;
|
|
148
|
+
};
|
|
149
|
+
type DefaultHandler = (inputCtx: MiddlewareContext<any>) => Promise<any>;
|
|
150
|
+
type Middleware<Handler extends (inputCtx: MiddlewareContext<any>) => Promise<any> = DefaultHandler> = Handler & {
|
|
151
|
+
options: Record<string, any>;
|
|
102
152
|
};
|
|
103
|
-
declare function createMiddleware<
|
|
104
|
-
declare function createMiddleware<Options extends MiddlewareOptions, R>(handler: (context: MiddlewareContext<Options>) => Promise<R>): <InputCtx extends MiddlewareInputContext<Options>>(inputContext: InputCtx) => Promise<R>;
|
|
153
|
+
declare function createMiddleware<Context = {}, R = unknown>(handler: (context: MiddlewareContext<Context>) => Promise<R>): Middleware<(inputContext: Record<string, any>) => Promise<R>>;
|
|
105
154
|
declare namespace createMiddleware {
|
|
106
155
|
var create: <E extends {
|
|
107
156
|
use?: Middleware[];
|
|
108
157
|
}>(opts?: E) => {
|
|
109
|
-
<
|
|
110
|
-
|
|
158
|
+
<R>(options: {
|
|
159
|
+
use?: Middleware[];
|
|
160
|
+
}, handler: (ctx: MiddlewareContext<InferUse<E["use"]>>) => Promise<R>): Middleware<(inputContext: Record<string, any>) => Promise<R>>;
|
|
161
|
+
<R>(handler: (ctx: MiddlewareContext<InferUse<E["use"]>>) => Promise<R>): Middleware<(inputContext: Record<string, any>) => Promise<R>>;
|
|
111
162
|
};
|
|
112
163
|
}
|
|
113
|
-
type MiddlewareInputContext<Options extends MiddlewareOptions> = InferBodyInput<Options> & InferQueryInput<Options> & InferRequestInput<Options> & InferHeadersInput<Options> & {
|
|
114
|
-
asResponse?: boolean;
|
|
115
|
-
returnHeaders?: boolean;
|
|
116
|
-
use?: Middleware[];
|
|
117
|
-
};
|
|
118
|
-
type Middleware<Options extends MiddlewareOptions = MiddlewareOptions, Handler extends (inputCtx: any) => Promise<any> = any> = Handler & {
|
|
119
|
-
options: Options;
|
|
120
|
-
};
|
|
121
164
|
//#endregion
|
|
122
|
-
export { Middleware, MiddlewareContext,
|
|
165
|
+
export { Middleware, MiddlewareContext, createMiddleware };
|
|
123
166
|
//# sourceMappingURL=middleware.d.cts.map
|
package/dist/middleware.d.mts
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
|
+
import { CookieOptions, CookiePrefixOptions } from "./cookies.mjs";
|
|
2
|
+
import { APIError, Status, statusCodes } from "./error.mjs";
|
|
1
3
|
import { Prettify } from "./helper.mjs";
|
|
2
|
-
import {
|
|
3
|
-
import { EndpointContext, EndpointOptions } from "./endpoint.mjs";
|
|
4
|
+
import { InferUse } from "./types.mjs";
|
|
4
5
|
|
|
5
6
|
//#region src/middleware.d.ts
|
|
6
|
-
|
|
7
|
-
type MiddlewareResponse = null | void | undefined | Record<string, any>;
|
|
8
|
-
type MiddlewareContext<Options extends MiddlewareOptions, Context = {}> = EndpointContext<string, Options & {
|
|
9
|
-
method: "*";
|
|
10
|
-
}> & {
|
|
7
|
+
type MiddlewareContext<Context = {}> = {
|
|
11
8
|
/**
|
|
12
9
|
* Method
|
|
13
10
|
*
|
|
@@ -26,98 +23,144 @@ type MiddlewareContext<Options extends MiddlewareOptions, Context = {}> = Endpoi
|
|
|
26
23
|
* The body object will be the parsed JSON from the request and validated
|
|
27
24
|
* against the body schema if it exists
|
|
28
25
|
*/
|
|
29
|
-
body:
|
|
26
|
+
body: any;
|
|
30
27
|
/**
|
|
31
28
|
* Query
|
|
32
29
|
*
|
|
33
30
|
* The query object will be the parsed query string from the request
|
|
34
31
|
* and validated against the query schema if it exists
|
|
35
32
|
*/
|
|
36
|
-
query:
|
|
33
|
+
query: Record<string, any> | undefined;
|
|
37
34
|
/**
|
|
38
35
|
* Params
|
|
39
36
|
*
|
|
40
37
|
* If the path is `/user/:id` and the request is `/user/1` then the
|
|
41
|
-
* params will
|
|
42
|
-
* be `{
|
|
43
|
-
* then the
|
|
44
|
-
*
|
|
45
|
-
* wildcard
|
|
46
|
-
* is named like `/user/**:name` then the params will be `{ name: string }`
|
|
38
|
+
* params will be `{ id: "1" }` and if the path includes a wildcard like
|
|
39
|
+
* `/user/*` then the params will be `{ _: "1" }` where `_` is the wildcard
|
|
40
|
+
* key. If the wildcard is named like `/user/**:name` then the params will
|
|
41
|
+
* be `{ name: string }`
|
|
47
42
|
*/
|
|
48
|
-
params: string;
|
|
43
|
+
params: Record<string, any> | undefined;
|
|
49
44
|
/**
|
|
50
45
|
* Request object
|
|
51
46
|
*
|
|
52
47
|
* If `requireRequest` is set to true in the endpoint options this will be
|
|
53
48
|
* required
|
|
54
49
|
*/
|
|
55
|
-
request:
|
|
50
|
+
request: Request | undefined;
|
|
56
51
|
/**
|
|
57
52
|
* Headers
|
|
58
53
|
*
|
|
59
54
|
* If `requireHeaders` is set to true in the endpoint options this will be
|
|
60
55
|
* required
|
|
61
56
|
*/
|
|
62
|
-
headers:
|
|
57
|
+
headers: Headers | undefined;
|
|
63
58
|
/**
|
|
64
59
|
* Set header
|
|
65
60
|
*
|
|
66
61
|
* If it's called outside of a request it will just be ignored.
|
|
67
62
|
*/
|
|
68
63
|
setHeader: (key: string, value: string) => void;
|
|
64
|
+
/**
|
|
65
|
+
* Set the response status code
|
|
66
|
+
*/
|
|
67
|
+
setStatus: (status: Status) => void;
|
|
69
68
|
/**
|
|
70
69
|
* Get header
|
|
71
70
|
*
|
|
72
71
|
* If it's called outside of a request it will just return null
|
|
73
72
|
*
|
|
74
|
-
* @param key
|
|
75
|
-
* @returns
|
|
73
|
+
* @param key - The key of the header
|
|
76
74
|
*/
|
|
77
75
|
getHeader: (key: string) => string | null;
|
|
76
|
+
/**
|
|
77
|
+
* Get a cookie value from the request
|
|
78
|
+
*
|
|
79
|
+
* @param key - The key of the cookie
|
|
80
|
+
* @param prefix - The prefix of the cookie between `__Secure-` and `__Host-`
|
|
81
|
+
* @returns The value of the cookie
|
|
82
|
+
*/
|
|
83
|
+
getCookie: (key: string, prefix?: CookiePrefixOptions) => string | null;
|
|
84
|
+
/**
|
|
85
|
+
* Get a signed cookie value from the request
|
|
86
|
+
*
|
|
87
|
+
* @param key - The key of the cookie
|
|
88
|
+
* @param secret - The secret of the signed cookie
|
|
89
|
+
* @param prefix - The prefix of the cookie between `__Secure-` and `__Host-`
|
|
90
|
+
* @returns The value of the cookie or null if the cookie is not found or false if the signature is invalid
|
|
91
|
+
*/
|
|
92
|
+
getSignedCookie: (key: string, secret: string, prefix?: CookiePrefixOptions) => Promise<string | null | false>;
|
|
93
|
+
/**
|
|
94
|
+
* Set a cookie value in the response
|
|
95
|
+
*
|
|
96
|
+
* @param key - The key of the cookie
|
|
97
|
+
* @param value - The value to set
|
|
98
|
+
* @param options - The options of the cookie
|
|
99
|
+
* @returns The cookie string
|
|
100
|
+
*/
|
|
101
|
+
setCookie: (key: string, value: string, options?: CookieOptions) => string;
|
|
102
|
+
/**
|
|
103
|
+
* Set signed cookie
|
|
104
|
+
*
|
|
105
|
+
* @param key - The key of the cookie
|
|
106
|
+
* @param value - The value to set
|
|
107
|
+
* @param secret - The secret to sign the cookie with
|
|
108
|
+
* @param options - The options of the cookie
|
|
109
|
+
* @returns The cookie string
|
|
110
|
+
*/
|
|
111
|
+
setSignedCookie: (key: string, value: string, secret: string, options?: CookieOptions) => Promise<string>;
|
|
78
112
|
/**
|
|
79
113
|
* JSON
|
|
80
114
|
*
|
|
81
|
-
*
|
|
82
|
-
* the
|
|
83
|
-
*
|
|
84
|
-
* the context then
|
|
85
|
-
* it will return a Response object instead of the
|
|
86
|
-
* JSON object.
|
|
115
|
+
* A helper function to create a JSON response with the correct headers
|
|
116
|
+
* and status code. If `asResponse` is set to true in the context then
|
|
117
|
+
* it will return a Response object instead of the JSON object.
|
|
87
118
|
*
|
|
88
119
|
* @param json - The JSON object to return
|
|
89
|
-
* @param routerResponse - The response object to
|
|
90
|
-
* return if `asResponse` is
|
|
120
|
+
* @param routerResponse - The response object to return if `asResponse` is
|
|
91
121
|
* true in the context this will take precedence
|
|
92
122
|
*/
|
|
93
123
|
json: <R extends Record<string, any> | null>(json: R, routerResponse?: {
|
|
94
124
|
status?: number;
|
|
95
125
|
headers?: Record<string, string>;
|
|
96
126
|
response?: Response;
|
|
97
|
-
|
|
127
|
+
body?: Record<string, any>;
|
|
128
|
+
} | Response) => R;
|
|
98
129
|
/**
|
|
99
130
|
* Middleware context
|
|
100
131
|
*/
|
|
101
132
|
context: Prettify<Context>;
|
|
133
|
+
/**
|
|
134
|
+
* Redirect to a new URL
|
|
135
|
+
*/
|
|
136
|
+
redirect: (url: string) => APIError;
|
|
137
|
+
/**
|
|
138
|
+
* Return error
|
|
139
|
+
*/
|
|
140
|
+
error: (status: keyof typeof statusCodes | Status, body?: {
|
|
141
|
+
message?: string;
|
|
142
|
+
code?: string;
|
|
143
|
+
} & Record<string, any>, headers?: HeadersInit) => APIError;
|
|
144
|
+
asResponse?: boolean;
|
|
145
|
+
returnHeaders?: boolean;
|
|
146
|
+
returnStatus?: boolean;
|
|
147
|
+
responseHeaders: Headers;
|
|
148
|
+
};
|
|
149
|
+
type DefaultHandler = (inputCtx: MiddlewareContext<any>) => Promise<any>;
|
|
150
|
+
type Middleware<Handler extends (inputCtx: MiddlewareContext<any>) => Promise<any> = DefaultHandler> = Handler & {
|
|
151
|
+
options: Record<string, any>;
|
|
102
152
|
};
|
|
103
|
-
declare function createMiddleware<
|
|
104
|
-
declare function createMiddleware<Options extends MiddlewareOptions, R>(handler: (context: MiddlewareContext<Options>) => Promise<R>): <InputCtx extends MiddlewareInputContext<Options>>(inputContext: InputCtx) => Promise<R>;
|
|
153
|
+
declare function createMiddleware<Context = {}, R = unknown>(handler: (context: MiddlewareContext<Context>) => Promise<R>): Middleware<(inputContext: Record<string, any>) => Promise<R>>;
|
|
105
154
|
declare namespace createMiddleware {
|
|
106
155
|
var create: <E extends {
|
|
107
156
|
use?: Middleware[];
|
|
108
157
|
}>(opts?: E) => {
|
|
109
|
-
<
|
|
110
|
-
|
|
158
|
+
<R>(options: {
|
|
159
|
+
use?: Middleware[];
|
|
160
|
+
}, handler: (ctx: MiddlewareContext<InferUse<E["use"]>>) => Promise<R>): Middleware<(inputContext: Record<string, any>) => Promise<R>>;
|
|
161
|
+
<R>(handler: (ctx: MiddlewareContext<InferUse<E["use"]>>) => Promise<R>): Middleware<(inputContext: Record<string, any>) => Promise<R>>;
|
|
111
162
|
};
|
|
112
163
|
}
|
|
113
|
-
type MiddlewareInputContext<Options extends MiddlewareOptions> = InferBodyInput<Options> & InferQueryInput<Options> & InferRequestInput<Options> & InferHeadersInput<Options> & {
|
|
114
|
-
asResponse?: boolean;
|
|
115
|
-
returnHeaders?: boolean;
|
|
116
|
-
use?: Middleware[];
|
|
117
|
-
};
|
|
118
|
-
type Middleware<Options extends MiddlewareOptions = MiddlewareOptions, Handler extends (inputCtx: any) => Promise<any> = any> = Handler & {
|
|
119
|
-
options: Options;
|
|
120
|
-
};
|
|
121
164
|
//#endregion
|
|
122
|
-
export { Middleware, MiddlewareContext,
|
|
165
|
+
export { Middleware, MiddlewareContext, createMiddleware };
|
|
123
166
|
//# sourceMappingURL=middleware.d.mts.map
|
package/dist/middleware.mjs
CHANGED
|
@@ -1,20 +1,17 @@
|
|
|
1
1
|
import { kAPIErrorHeaderSymbol } from "./error.mjs";
|
|
2
2
|
import { isAPIError } from "./utils.mjs";
|
|
3
3
|
import { createInternalContext } from "./context.mjs";
|
|
4
|
-
import "./endpoint.mjs";
|
|
5
4
|
|
|
6
5
|
//#region src/middleware.ts
|
|
7
|
-
function createMiddleware(
|
|
6
|
+
function createMiddleware(handler) {
|
|
8
7
|
const internalHandler = async (inputCtx) => {
|
|
9
8
|
const context = inputCtx;
|
|
10
|
-
const _handler = typeof optionsOrHandler === "function" ? optionsOrHandler : handler;
|
|
11
9
|
const internalContext = await createInternalContext(context, {
|
|
12
|
-
options:
|
|
10
|
+
options: {},
|
|
13
11
|
path: "/"
|
|
14
12
|
});
|
|
15
|
-
if (!_handler) throw new Error("handler must be defined");
|
|
16
13
|
try {
|
|
17
|
-
const response = await
|
|
14
|
+
const response = await handler(internalContext);
|
|
18
15
|
const headers = internalContext.responseHeaders;
|
|
19
16
|
return context.returnHeaders ? {
|
|
20
17
|
headers,
|
|
@@ -31,18 +28,67 @@ function createMiddleware(optionsOrHandler, handler) {
|
|
|
31
28
|
throw e;
|
|
32
29
|
}
|
|
33
30
|
};
|
|
34
|
-
internalHandler.options =
|
|
31
|
+
internalHandler.options = {};
|
|
35
32
|
return internalHandler;
|
|
36
33
|
}
|
|
37
34
|
createMiddleware.create = (opts) => {
|
|
38
35
|
function fn(optionsOrHandler, handler) {
|
|
39
|
-
if (typeof optionsOrHandler === "function")
|
|
36
|
+
if (typeof optionsOrHandler === "function") {
|
|
37
|
+
const internalHandler = async (inputCtx) => {
|
|
38
|
+
const context = inputCtx;
|
|
39
|
+
const internalContext = await createInternalContext(context, {
|
|
40
|
+
options: { use: opts?.use },
|
|
41
|
+
path: "/"
|
|
42
|
+
});
|
|
43
|
+
try {
|
|
44
|
+
const response = await optionsOrHandler(internalContext);
|
|
45
|
+
const headers = internalContext.responseHeaders;
|
|
46
|
+
return context.returnHeaders ? {
|
|
47
|
+
headers,
|
|
48
|
+
response
|
|
49
|
+
} : response;
|
|
50
|
+
} catch (e) {
|
|
51
|
+
if (isAPIError(e)) Object.defineProperty(e, kAPIErrorHeaderSymbol, {
|
|
52
|
+
enumerable: false,
|
|
53
|
+
configurable: false,
|
|
54
|
+
get() {
|
|
55
|
+
return internalContext.responseHeaders;
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
throw e;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
internalHandler.options = { use: opts?.use };
|
|
62
|
+
return internalHandler;
|
|
63
|
+
}
|
|
40
64
|
if (!handler) throw new Error("Middleware handler is required");
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
65
|
+
const use = [...opts?.use || [], ...optionsOrHandler.use || []];
|
|
66
|
+
const internalHandler = async (inputCtx) => {
|
|
67
|
+
const context = inputCtx;
|
|
68
|
+
const internalContext = await createInternalContext(context, {
|
|
69
|
+
options: { use },
|
|
70
|
+
path: "/"
|
|
71
|
+
});
|
|
72
|
+
try {
|
|
73
|
+
const response = await handler(internalContext);
|
|
74
|
+
const headers = internalContext.responseHeaders;
|
|
75
|
+
return context.returnHeaders ? {
|
|
76
|
+
headers,
|
|
77
|
+
response
|
|
78
|
+
} : response;
|
|
79
|
+
} catch (e) {
|
|
80
|
+
if (isAPIError(e)) Object.defineProperty(e, kAPIErrorHeaderSymbol, {
|
|
81
|
+
enumerable: false,
|
|
82
|
+
configurable: false,
|
|
83
|
+
get() {
|
|
84
|
+
return internalContext.responseHeaders;
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
throw e;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
internalHandler.options = { use };
|
|
91
|
+
return internalHandler;
|
|
46
92
|
}
|
|
47
93
|
return fn;
|
|
48
94
|
};
|
package/dist/middleware.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.mjs","names":[],"sources":["../src/middleware.ts"],"sourcesContent":["import {\n\tcreateEndpoint,\n\ttype Endpoint,\n\ttype EndpointContext,\n\ttype EndpointOptions,\n} from \"./endpoint\";\nimport {\n\tcreateInternalContext,\n\ttype InferBody,\n\ttype InferBodyInput,\n\ttype InferHeaders,\n\ttype InferHeadersInput,\n\ttype InferMiddlewareBody,\n\ttype InferMiddlewareQuery,\n\ttype InferQuery,\n\ttype InferQueryInput,\n\ttype InferRequest,\n\ttype InferRequestInput,\n\ttype InferUse,\n\ttype InputContext,\n} from \"./context\";\nimport type { Prettify } from \"./helper\";\nimport { isAPIError } from \"./utils\";\nimport { kAPIErrorHeaderSymbol } from \"./error\";\n\nexport interface MiddlewareOptions extends Omit<EndpointOptions, \"method\"> {}\n\nexport type MiddlewareResponse = null | void | undefined | Record<string, any>;\n\nexport type MiddlewareContext<\n\tOptions extends MiddlewareOptions,\n\tContext = {},\n> = EndpointContext<\n\tstring,\n\tOptions & {\n\t\tmethod: \"*\";\n\t}\n> & {\n\t/**\n\t * Method\n\t *\n\t * The request method\n\t */\n\tmethod: string;\n\t/**\n\t * Path\n\t *\n\t * The path of the endpoint\n\t */\n\tpath: string;\n\t/**\n\t * Body\n\t *\n\t * The body object will be the parsed JSON from the request and validated\n\t * against the body schema if it exists\n\t */\n\tbody: InferMiddlewareBody<Options>;\n\t/**\n\t * Query\n\t *\n\t * The query object will be the parsed query string from the request\n\t * and validated against the query schema if it exists\n\t */\n\tquery: InferMiddlewareQuery<Options>;\n\t/**\n\t * Params\n\t *\n\t * If the path is `/user/:id` and the request is `/user/1` then the\n\t * params will\n\t * be `{ id: \"1\" }` and if the path includes a wildcard like `/user/*`\n\t * then the\n\t * params will be `{ _: \"1\" }` where `_` is the wildcard key. If the\n\t * wildcard\n\t * is named like `/user/**:name` then the params will be `{ name: string }`\n\t */\n\tparams: string;\n\t/**\n\t * Request object\n\t *\n\t * If `requireRequest` is set to true in the endpoint options this will be\n\t * required\n\t */\n\trequest: InferRequest<Options>;\n\t/**\n\t * Headers\n\t *\n\t * If `requireHeaders` is set to true in the endpoint options this will be\n\t * required\n\t */\n\theaders: InferHeaders<Options>;\n\t/**\n\t * Set header\n\t *\n\t * If it's called outside of a request it will just be ignored.\n\t */\n\tsetHeader: (key: string, value: string) => void;\n\t/**\n\t * Get header\n\t *\n\t * If it's called outside of a request it will just return null\n\t *\n\t * @param key - The key of the header\n\t * @returns\n\t */\n\tgetHeader: (key: string) => string | null;\n\t/**\n\t * JSON\n\t *\n\t * a helper function to create a JSON response with\n\t * the correct headers\n\t * and status code. If `asResponse` is set to true in\n\t * the context then\n\t * it will return a Response object instead of the\n\t * JSON object.\n\t *\n\t * @param json - The JSON object to return\n\t * @param routerResponse - The response object to\n\t * return if `asResponse` is\n\t * true in the context this will take precedence\n\t */\n\tjson: <R extends Record<string, any> | null>(\n\t\tjson: R,\n\t\trouterResponse?:\n\t\t\t| {\n\t\t\t\t\tstatus?: number;\n\t\t\t\t\theaders?: Record<string, string>;\n\t\t\t\t\tresponse?: Response;\n\t\t\t }\n\t\t\t| Response,\n\t) => Promise<R>;\n\t/**\n\t * Middleware context\n\t */\n\tcontext: Prettify<Context>;\n};\n\nexport function createMiddleware<Options extends MiddlewareOptions, R>(\n\toptions: Options,\n\thandler: (context: MiddlewareContext<Options>) => Promise<R>,\n): <InputCtx extends MiddlewareInputContext<Options>>(\n\tinputContext: InputCtx,\n) => Promise<R>;\nexport function createMiddleware<Options extends MiddlewareOptions, R>(\n\thandler: (context: MiddlewareContext<Options>) => Promise<R>,\n): <InputCtx extends MiddlewareInputContext<Options>>(\n\tinputContext: InputCtx,\n) => Promise<R>;\nexport function createMiddleware(optionsOrHandler: any, handler?: any) {\n\tconst internalHandler = async (inputCtx: InputContext<any, any>) => {\n\t\tconst context = inputCtx as InputContext<any, any>;\n\t\tconst _handler =\n\t\t\ttypeof optionsOrHandler === \"function\" ? optionsOrHandler : handler;\n\t\tconst options =\n\t\t\ttypeof optionsOrHandler === \"function\" ? {} : optionsOrHandler;\n\t\tconst internalContext = await createInternalContext(context, {\n\t\t\toptions,\n\t\t\tpath: \"/\",\n\t\t});\n\n\t\tif (!_handler) {\n\t\t\tthrow new Error(\"handler must be defined\");\n\t\t}\n\t\ttry {\n\t\t\tconst response = await _handler(internalContext as any);\n\t\t\tconst headers = internalContext.responseHeaders;\n\t\t\treturn context.returnHeaders\n\t\t\t\t? {\n\t\t\t\t\t\theaders,\n\t\t\t\t\t\tresponse,\n\t\t\t\t\t}\n\t\t\t\t: response;\n\t\t} catch (e) {\n\t\t\t// fixme(alex): this is workaround that set-cookie headers are not accessible when error is thrown from middleware\n\t\t\tif (isAPIError(e)) {\n\t\t\t\tObject.defineProperty(e, kAPIErrorHeaderSymbol, {\n\t\t\t\t\tenumerable: false,\n\t\t\t\t\tconfigurable: false,\n\t\t\t\t\tget() {\n\t\t\t\t\t\treturn internalContext.responseHeaders;\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t}\n\t\t\tthrow e;\n\t\t}\n\t};\n\tinternalHandler.options =\n\t\ttypeof optionsOrHandler === \"function\" ? {} : optionsOrHandler;\n\treturn internalHandler;\n}\n\nexport type MiddlewareInputContext<Options extends MiddlewareOptions> =\n\tInferBodyInput<Options> &\n\t\tInferQueryInput<Options> &\n\t\tInferRequestInput<Options> &\n\t\tInferHeadersInput<Options> & {\n\t\t\tasResponse?: boolean;\n\t\t\treturnHeaders?: boolean;\n\t\t\tuse?: Middleware[];\n\t\t};\n\nexport type Middleware<\n\tOptions extends MiddlewareOptions = MiddlewareOptions,\n\tHandler extends (inputCtx: any) => Promise<any> = any,\n> = Handler & {\n\toptions: Options;\n};\n\ncreateMiddleware.create = <\n\tE extends {\n\t\tuse?: Middleware[];\n\t},\n>(\n\topts?: E,\n) => {\n\ttype InferredContext = InferUse<E[\"use\"]>;\n\tfunction fn<Options extends MiddlewareOptions, R>(\n\t\toptions: Options,\n\t\thandler: (ctx: MiddlewareContext<Options, InferredContext>) => Promise<R>,\n\t): (inputContext: MiddlewareInputContext<Options>) => Promise<R>;\n\tfunction fn<Options extends MiddlewareOptions, R>(\n\t\thandler: (ctx: MiddlewareContext<Options, InferredContext>) => Promise<R>,\n\t): (inputContext: MiddlewareInputContext<Options>) => Promise<R>;\n\tfunction fn(optionsOrHandler: any, handler?: any) {\n\t\tif (typeof optionsOrHandler === \"function\") {\n\t\t\treturn createMiddleware(\n\t\t\t\t{\n\t\t\t\t\tuse: opts?.use,\n\t\t\t\t},\n\t\t\t\toptionsOrHandler,\n\t\t\t);\n\t\t}\n\t\tif (!handler) {\n\t\t\tthrow new Error(\"Middleware handler is required\");\n\t\t}\n\t\tconst middleware = createMiddleware(\n\t\t\t{\n\t\t\t\t...optionsOrHandler,\n\t\t\t\tmethod: \"*\",\n\t\t\t\tuse: [...(opts?.use || []), ...(optionsOrHandler.use || [])],\n\t\t\t},\n\t\t\thandler,\n\t\t);\n\t\treturn middleware as any;\n\t}\n\treturn fn;\n};\n"],"mappings":";;;;;;AAmJA,SAAgB,iBAAiB,kBAAuB,SAAe;CACtE,MAAM,kBAAkB,OAAO,aAAqC;EACnE,MAAM,UAAU;EAChB,MAAM,WACL,OAAO,qBAAqB,aAAa,mBAAmB;EAG7D,MAAM,kBAAkB,MAAM,sBAAsB,SAAS;GAC5D,SAFA,OAAO,qBAAqB,aAAa,EAAE,GAAG;GAG9C,MAAM;GACN,CAAC;AAEF,MAAI,CAAC,SACJ,OAAM,IAAI,MAAM,0BAA0B;AAE3C,MAAI;GACH,MAAM,WAAW,MAAM,SAAS,gBAAuB;GACvD,MAAM,UAAU,gBAAgB;AAChC,UAAO,QAAQ,gBACZ;IACA;IACA;IACA,GACA;WACK,GAAG;AAEX,OAAI,WAAW,EAAE,CAChB,QAAO,eAAe,GAAG,uBAAuB;IAC/C,YAAY;IACZ,cAAc;IACd,MAAM;AACL,YAAO,gBAAgB;;IAExB,CAAC;AAEH,SAAM;;;AAGR,iBAAgB,UACf,OAAO,qBAAqB,aAAa,EAAE,GAAG;AAC/C,QAAO;;AAoBR,iBAAiB,UAKhB,SACI;CASJ,SAAS,GAAG,kBAAuB,SAAe;AACjD,MAAI,OAAO,qBAAqB,WAC/B,QAAO,iBACN,EACC,KAAK,MAAM,KACX,EACD,iBACA;AAEF,MAAI,CAAC,QACJ,OAAM,IAAI,MAAM,iCAAiC;AAUlD,SARmB,iBAClB;GACC,GAAG;GACH,QAAQ;GACR,KAAK,CAAC,GAAI,MAAM,OAAO,EAAE,EAAG,GAAI,iBAAiB,OAAO,EAAE,CAAE;GAC5D,EACD,QACA;;AAGF,QAAO"}
|
|
1
|
+
{"version":3,"file":"middleware.mjs","names":[],"sources":["../src/middleware.ts"],"sourcesContent":["import { createInternalContext } from \"./context\";\nimport type { CookieOptions, CookiePrefixOptions } from \"./cookies\";\nimport type { Status, statusCodes } from \"./error\";\nimport { type APIError, kAPIErrorHeaderSymbol } from \"./error\";\nimport type { Prettify } from \"./helper\";\nimport type { InferUse } from \"./types\";\nimport { isAPIError } from \"./utils\";\n\nexport type MiddlewareContext<Context = {}> = {\n\t/**\n\t * Method\n\t *\n\t * The request method\n\t */\n\tmethod: string;\n\t/**\n\t * Path\n\t *\n\t * The path of the endpoint\n\t */\n\tpath: string;\n\t/**\n\t * Body\n\t *\n\t * The body object will be the parsed JSON from the request and validated\n\t * against the body schema if it exists\n\t */\n\tbody: any;\n\t/**\n\t * Query\n\t *\n\t * The query object will be the parsed query string from the request\n\t * and validated against the query schema if it exists\n\t */\n\tquery: Record<string, any> | undefined;\n\t/**\n\t * Params\n\t *\n\t * If the path is `/user/:id` and the request is `/user/1` then the\n\t * params will be `{ id: \"1\" }` and if the path includes a wildcard like\n\t * `/user/*` then the params will be `{ _: \"1\" }` where `_` is the wildcard\n\t * key. If the wildcard is named like `/user/**:name` then the params will\n\t * be `{ name: string }`\n\t */\n\tparams: Record<string, any> | undefined;\n\t/**\n\t * Request object\n\t *\n\t * If `requireRequest` is set to true in the endpoint options this will be\n\t * required\n\t */\n\trequest: Request | undefined;\n\t/**\n\t * Headers\n\t *\n\t * If `requireHeaders` is set to true in the endpoint options this will be\n\t * required\n\t */\n\theaders: Headers | undefined;\n\t/**\n\t * Set header\n\t *\n\t * If it's called outside of a request it will just be ignored.\n\t */\n\tsetHeader: (key: string, value: string) => void;\n\t/**\n\t * Set the response status code\n\t */\n\tsetStatus: (status: Status) => void;\n\t/**\n\t * Get header\n\t *\n\t * If it's called outside of a request it will just return null\n\t *\n\t * @param key - The key of the header\n\t */\n\tgetHeader: (key: string) => string | null;\n\t/**\n\t * Get a cookie value from the request\n\t *\n\t * @param key - The key of the cookie\n\t * @param prefix - The prefix of the cookie between `__Secure-` and `__Host-`\n\t * @returns The value of the cookie\n\t */\n\tgetCookie: (key: string, prefix?: CookiePrefixOptions) => string | null;\n\t/**\n\t * Get a signed cookie value from the request\n\t *\n\t * @param key - The key of the cookie\n\t * @param secret - The secret of the signed cookie\n\t * @param prefix - The prefix of the cookie between `__Secure-` and `__Host-`\n\t * @returns The value of the cookie or null if the cookie is not found or false if the signature is invalid\n\t */\n\tgetSignedCookie: (\n\t\tkey: string,\n\t\tsecret: string,\n\t\tprefix?: CookiePrefixOptions,\n\t) => Promise<string | null | false>;\n\t/**\n\t * Set a cookie value in the response\n\t *\n\t * @param key - The key of the cookie\n\t * @param value - The value to set\n\t * @param options - The options of the cookie\n\t * @returns The cookie string\n\t */\n\tsetCookie: (key: string, value: string, options?: CookieOptions) => string;\n\t/**\n\t * Set signed cookie\n\t *\n\t * @param key - The key of the cookie\n\t * @param value - The value to set\n\t * @param secret - The secret to sign the cookie with\n\t * @param options - The options of the cookie\n\t * @returns The cookie string\n\t */\n\tsetSignedCookie: (\n\t\tkey: string,\n\t\tvalue: string,\n\t\tsecret: string,\n\t\toptions?: CookieOptions,\n\t) => Promise<string>;\n\t/**\n\t * JSON\n\t *\n\t * A helper function to create a JSON response with the correct headers\n\t * and status code. If `asResponse` is set to true in the context then\n\t * it will return a Response object instead of the JSON object.\n\t *\n\t * @param json - The JSON object to return\n\t * @param routerResponse - The response object to return if `asResponse` is\n\t * true in the context this will take precedence\n\t */\n\tjson: <R extends Record<string, any> | null>(\n\t\tjson: R,\n\t\trouterResponse?:\n\t\t\t| {\n\t\t\t\t\tstatus?: number;\n\t\t\t\t\theaders?: Record<string, string>;\n\t\t\t\t\tresponse?: Response;\n\t\t\t\t\tbody?: Record<string, any>;\n\t\t\t }\n\t\t\t| Response,\n\t) => R;\n\t/**\n\t * Middleware context\n\t */\n\tcontext: Prettify<Context>;\n\t/**\n\t * Redirect to a new URL\n\t */\n\tredirect: (url: string) => APIError;\n\t/**\n\t * Return error\n\t */\n\terror: (\n\t\tstatus: keyof typeof statusCodes | Status,\n\t\tbody?: {\n\t\t\tmessage?: string;\n\t\t\tcode?: string;\n\t\t} & Record<string, any>,\n\t\theaders?: HeadersInit,\n\t) => APIError;\n\tasResponse?: boolean;\n\treturnHeaders?: boolean;\n\treturnStatus?: boolean;\n\tresponseHeaders: Headers;\n};\n\ntype DefaultHandler = (inputCtx: MiddlewareContext<any>) => Promise<any>;\n\nexport type Middleware<\n\tHandler extends (\n\t\tinputCtx: MiddlewareContext<any>,\n\t) => Promise<any> = DefaultHandler,\n> = Handler & {\n\toptions: Record<string, any>;\n};\n\nexport function createMiddleware<Context = {}, R = unknown>(\n\thandler: (context: MiddlewareContext<Context>) => Promise<R>,\n): Middleware<(inputContext: Record<string, any>) => Promise<R>>;\nexport function createMiddleware(handler: any) {\n\tconst internalHandler = async (inputCtx: any) => {\n\t\tconst context = inputCtx as Record<string, any>;\n\t\tconst internalContext = await createInternalContext(context, {\n\t\t\toptions: {},\n\t\t\tpath: \"/\",\n\t\t});\n\n\t\ttry {\n\t\t\tconst response = await handler(internalContext as any);\n\t\t\tconst headers = internalContext.responseHeaders;\n\t\t\treturn context.returnHeaders\n\t\t\t\t? {\n\t\t\t\t\t\theaders,\n\t\t\t\t\t\tresponse,\n\t\t\t\t\t}\n\t\t\t\t: response;\n\t\t} catch (e) {\n\t\t\t// fixme(alex): this is workaround that set-cookie headers are not accessible when error is thrown from middleware\n\t\t\tif (isAPIError(e)) {\n\t\t\t\tObject.defineProperty(e, kAPIErrorHeaderSymbol, {\n\t\t\t\t\tenumerable: false,\n\t\t\t\t\tconfigurable: false,\n\t\t\t\t\tget() {\n\t\t\t\t\t\treturn internalContext.responseHeaders;\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t}\n\t\t\tthrow e;\n\t\t}\n\t};\n\tinternalHandler.options = {};\n\treturn internalHandler;\n}\n\ncreateMiddleware.create = <\n\tE extends {\n\t\tuse?: Middleware[];\n\t},\n>(\n\topts?: E,\n) => {\n\ttype InferredContext = InferUse<E[\"use\"]>;\n\n\tfunction fn<R>(\n\t\toptions: { use?: Middleware[] },\n\t\thandler: (ctx: MiddlewareContext<InferredContext>) => Promise<R>,\n\t): Middleware<(inputContext: Record<string, any>) => Promise<R>>;\n\tfunction fn<R>(\n\t\thandler: (ctx: MiddlewareContext<InferredContext>) => Promise<R>,\n\t): Middleware<(inputContext: Record<string, any>) => Promise<R>>;\n\tfunction fn(optionsOrHandler: any, handler?: any) {\n\t\tif (typeof optionsOrHandler === \"function\") {\n\t\t\tconst internalHandler = async (inputCtx: any) => {\n\t\t\t\tconst context = inputCtx as Record<string, any>;\n\t\t\t\tconst internalContext = await createInternalContext(context, {\n\t\t\t\t\toptions: { use: opts?.use },\n\t\t\t\t\tpath: \"/\",\n\t\t\t\t});\n\n\t\t\t\ttry {\n\t\t\t\t\tconst response = await optionsOrHandler(internalContext as any);\n\t\t\t\t\tconst headers = internalContext.responseHeaders;\n\t\t\t\t\treturn context.returnHeaders ? { headers, response } : response;\n\t\t\t\t} catch (e) {\n\t\t\t\t\tif (isAPIError(e)) {\n\t\t\t\t\t\tObject.defineProperty(e, kAPIErrorHeaderSymbol, {\n\t\t\t\t\t\t\tenumerable: false,\n\t\t\t\t\t\t\tconfigurable: false,\n\t\t\t\t\t\t\tget() {\n\t\t\t\t\t\t\t\treturn internalContext.responseHeaders;\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\tthrow e;\n\t\t\t\t}\n\t\t\t};\n\t\t\tinternalHandler.options = { use: opts?.use };\n\t\t\treturn internalHandler;\n\t\t}\n\t\tif (!handler) {\n\t\t\tthrow new Error(\"Middleware handler is required\");\n\t\t}\n\t\tconst use = [...(opts?.use || []), ...(optionsOrHandler.use || [])];\n\t\tconst internalHandler = async (inputCtx: any) => {\n\t\t\tconst context = inputCtx as Record<string, any>;\n\t\t\tconst internalContext = await createInternalContext(context, {\n\t\t\t\toptions: { use },\n\t\t\t\tpath: \"/\",\n\t\t\t});\n\n\t\t\ttry {\n\t\t\t\tconst response = await handler(internalContext as any);\n\t\t\t\tconst headers = internalContext.responseHeaders;\n\t\t\t\treturn context.returnHeaders ? { headers, response } : response;\n\t\t\t} catch (e) {\n\t\t\t\tif (isAPIError(e)) {\n\t\t\t\t\tObject.defineProperty(e, kAPIErrorHeaderSymbol, {\n\t\t\t\t\t\tenumerable: false,\n\t\t\t\t\t\tconfigurable: false,\n\t\t\t\t\t\tget() {\n\t\t\t\t\t\t\treturn internalContext.responseHeaders;\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tthrow e;\n\t\t\t}\n\t\t};\n\t\tinternalHandler.options = { use };\n\t\treturn internalHandler as any;\n\t}\n\treturn fn;\n};\n"],"mappings":";;;;;AAsLA,SAAgB,iBAAiB,SAAc;CAC9C,MAAM,kBAAkB,OAAO,aAAkB;EAChD,MAAM,UAAU;EAChB,MAAM,kBAAkB,MAAM,sBAAsB,SAAS;GAC5D,SAAS,EAAE;GACX,MAAM;GACN,CAAC;AAEF,MAAI;GACH,MAAM,WAAW,MAAM,QAAQ,gBAAuB;GACtD,MAAM,UAAU,gBAAgB;AAChC,UAAO,QAAQ,gBACZ;IACA;IACA;IACA,GACA;WACK,GAAG;AAEX,OAAI,WAAW,EAAE,CAChB,QAAO,eAAe,GAAG,uBAAuB;IAC/C,YAAY;IACZ,cAAc;IACd,MAAM;AACL,YAAO,gBAAgB;;IAExB,CAAC;AAEH,SAAM;;;AAGR,iBAAgB,UAAU,EAAE;AAC5B,QAAO;;AAGR,iBAAiB,UAKhB,SACI;CAUJ,SAAS,GAAG,kBAAuB,SAAe;AACjD,MAAI,OAAO,qBAAqB,YAAY;GAC3C,MAAM,kBAAkB,OAAO,aAAkB;IAChD,MAAM,UAAU;IAChB,MAAM,kBAAkB,MAAM,sBAAsB,SAAS;KAC5D,SAAS,EAAE,KAAK,MAAM,KAAK;KAC3B,MAAM;KACN,CAAC;AAEF,QAAI;KACH,MAAM,WAAW,MAAM,iBAAiB,gBAAuB;KAC/D,MAAM,UAAU,gBAAgB;AAChC,YAAO,QAAQ,gBAAgB;MAAE;MAAS;MAAU,GAAG;aAC/C,GAAG;AACX,SAAI,WAAW,EAAE,CAChB,QAAO,eAAe,GAAG,uBAAuB;MAC/C,YAAY;MACZ,cAAc;MACd,MAAM;AACL,cAAO,gBAAgB;;MAExB,CAAC;AAEH,WAAM;;;AAGR,mBAAgB,UAAU,EAAE,KAAK,MAAM,KAAK;AAC5C,UAAO;;AAER,MAAI,CAAC,QACJ,OAAM,IAAI,MAAM,iCAAiC;EAElD,MAAM,MAAM,CAAC,GAAI,MAAM,OAAO,EAAE,EAAG,GAAI,iBAAiB,OAAO,EAAE,CAAE;EACnE,MAAM,kBAAkB,OAAO,aAAkB;GAChD,MAAM,UAAU;GAChB,MAAM,kBAAkB,MAAM,sBAAsB,SAAS;IAC5D,SAAS,EAAE,KAAK;IAChB,MAAM;IACN,CAAC;AAEF,OAAI;IACH,MAAM,WAAW,MAAM,QAAQ,gBAAuB;IACtD,MAAM,UAAU,gBAAgB;AAChC,WAAO,QAAQ,gBAAgB;KAAE;KAAS;KAAU,GAAG;YAC/C,GAAG;AACX,QAAI,WAAW,EAAE,CAChB,QAAO,eAAe,GAAG,uBAAuB;KAC/C,YAAY;KACZ,cAAc;KACd,MAAM;AACL,aAAO,gBAAgB;;KAExB,CAAC;AAEH,UAAM;;;AAGR,kBAAgB,UAAU,EAAE,KAAK;AACjC,SAAO;;AAER,QAAO"}
|
package/dist/node.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node.cjs","names":["setResponse","getRequest"],"sources":["../src/adapters/node/index.ts"],"sourcesContent":["import type { IncomingMessage, ServerResponse } from \"node:http\";\
|
|
1
|
+
{"version":3,"file":"node.cjs","names":["setResponse","getRequest"],"sources":["../src/adapters/node/index.ts"],"sourcesContent":["import type { IncomingMessage, ServerResponse } from \"node:http\";\nimport type { Router } from \"../../router.js\";\nimport { getRequest, setResponse } from \"./request\";\n\nexport function toNodeHandler(handler: Router[\"handler\"]) {\n\treturn async (req: IncomingMessage, res: ServerResponse) => {\n\t\tconst protocol =\n\t\t\treq.headers[\"x-forwarded-proto\"] ||\n\t\t\t((req.socket as any).encrypted ? \"https\" : \"http\");\n\t\tconst base = `${protocol}://${req.headers[\":authority\"] || req.headers.host}`;\n\t\tconst response = await handler(getRequest({ base, request: req }));\n\t\treturn setResponse(res, response);\n\t};\n}\n\nexport { getRequest, setResponse };\n"],"mappings":";;;;AAIA,SAAgB,cAAc,SAA4B;AACzD,QAAO,OAAO,KAAsB,QAAwB;AAM3D,SAAOA,4BAAY,KADF,MAAM,QAAQC,2BAAW;GAAE,MAD/B,GAFZ,IAAI,QAAQ,yBACV,IAAI,OAAe,YAAY,UAAU,QACnB,KAAK,IAAI,QAAQ,iBAAiB,IAAI,QAAQ;GACrB,SAAS;GAAK,CAAC,CAAC,CACjC"}
|
package/dist/node.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { getRequest, setResponse } from "./adapters/node/request.cjs";
|
|
2
1
|
import { Router } from "./router.cjs";
|
|
2
|
+
import { getRequest, setResponse } from "./adapters/node/request.cjs";
|
|
3
3
|
import { IncomingMessage, ServerResponse } from "node:http";
|
|
4
4
|
|
|
5
5
|
//#region src/adapters/node/index.d.ts
|
package/dist/node.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { getRequest, setResponse } from "./adapters/node/request.mjs";
|
|
2
1
|
import { Router } from "./router.mjs";
|
|
2
|
+
import { getRequest, setResponse } from "./adapters/node/request.mjs";
|
|
3
3
|
import { IncomingMessage, ServerResponse } from "node:http";
|
|
4
4
|
|
|
5
5
|
//#region src/adapters/node/index.d.ts
|
package/dist/node.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node.mjs","names":[],"sources":["../src/adapters/node/index.ts"],"sourcesContent":["import type { IncomingMessage, ServerResponse } from \"node:http\";\
|
|
1
|
+
{"version":3,"file":"node.mjs","names":[],"sources":["../src/adapters/node/index.ts"],"sourcesContent":["import type { IncomingMessage, ServerResponse } from \"node:http\";\nimport type { Router } from \"../../router.js\";\nimport { getRequest, setResponse } from \"./request\";\n\nexport function toNodeHandler(handler: Router[\"handler\"]) {\n\treturn async (req: IncomingMessage, res: ServerResponse) => {\n\t\tconst protocol =\n\t\t\treq.headers[\"x-forwarded-proto\"] ||\n\t\t\t((req.socket as any).encrypted ? \"https\" : \"http\");\n\t\tconst base = `${protocol}://${req.headers[\":authority\"] || req.headers.host}`;\n\t\tconst response = await handler(getRequest({ base, request: req }));\n\t\treturn setResponse(res, response);\n\t};\n}\n\nexport { getRequest, setResponse };\n"],"mappings":";;;AAIA,SAAgB,cAAc,SAA4B;AACzD,QAAO,OAAO,KAAsB,QAAwB;AAM3D,SAAO,YAAY,KADF,MAAM,QAAQ,WAAW;GAAE,MAD/B,GAFZ,IAAI,QAAQ,yBACV,IAAI,OAAe,YAAY,UAAU,QACnB,KAAK,IAAI,QAAQ,iBAAiB,IAAI,QAAQ;GACrB,SAAS;GAAK,CAAC,CAAC,CACjC"}
|