@sapphire/plugin-api 7.0.0-next.7ba75bc → 7.0.0-next.821ff70
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/cjs/index.cjs +5 -24
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +79 -82
- package/dist/cjs/lib/structures/Route.cjs +0 -5
- package/dist/cjs/lib/structures/Route.cjs.map +1 -1
- package/dist/cjs/lib/structures/api/ApiRequest.cjs +128 -4
- package/dist/cjs/lib/structures/api/ApiRequest.cjs.map +1 -1
- package/dist/cjs/lib/structures/http/Auth.cjs.map +1 -1
- package/dist/cjs/lib/structures/http/HttpMethods.cjs +1 -0
- package/dist/cjs/lib/structures/http/HttpMethods.cjs.map +1 -1
- package/dist/cjs/lib/structures/http/Server.cjs +1 -8
- package/dist/cjs/lib/structures/http/Server.cjs.map +1 -1
- package/dist/cjs/lib/structures/router/RouterRoot.cjs +0 -1
- package/dist/cjs/lib/structures/router/RouterRoot.cjs.map +1 -1
- package/dist/cjs/lib/utils/_body/RequestHeadersProxy.cjs +100 -0
- package/dist/cjs/lib/utils/_body/RequestHeadersProxy.cjs.map +1 -0
- package/dist/cjs/lib/utils/_body/RequestProxy.cjs +123 -0
- package/dist/cjs/lib/utils/_body/RequestProxy.cjs.map +1 -0
- package/dist/cjs/lib/utils/_body/RequestURLProxy.cjs +179 -0
- package/dist/cjs/lib/utils/_body/RequestURLProxy.cjs.map +1 -0
- package/dist/cjs/lib/utils/constants.cjs +8 -0
- package/dist/cjs/lib/utils/constants.cjs.map +1 -0
- package/dist/cjs/listeners/_load.cjs.map +1 -1
- package/dist/cjs/middlewares/_load.cjs.map +1 -1
- package/dist/cjs/middlewares/body.cjs +1 -17
- package/dist/cjs/middlewares/body.cjs.map +1 -1
- package/dist/cjs/register.cjs +2 -3
- package/dist/cjs/register.cjs.map +1 -1
- package/dist/cjs/register.d.cts +0 -1
- package/dist/cjs/routes/_load.cjs.map +1 -1
- package/dist/cjs/routes/oauth/callback.post.cjs +1 -1
- package/dist/cjs/routes/oauth/callback.post.cjs.map +1 -1
- package/dist/esm/index.d.mts +79 -82
- package/dist/esm/index.mjs +1 -4
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/lib/structures/Route.mjs +0 -5
- package/dist/esm/lib/structures/Route.mjs.map +1 -1
- package/dist/esm/lib/structures/api/ApiRequest.mjs +122 -5
- package/dist/esm/lib/structures/api/ApiRequest.mjs.map +1 -1
- package/dist/esm/lib/structures/http/Auth.mjs.map +1 -1
- package/dist/esm/lib/structures/http/HttpMethods.mjs +1 -0
- package/dist/esm/lib/structures/http/HttpMethods.mjs.map +1 -1
- package/dist/esm/lib/structures/http/Server.mjs +1 -8
- package/dist/esm/lib/structures/http/Server.mjs.map +1 -1
- package/dist/esm/lib/structures/router/RouterRoot.mjs +0 -1
- package/dist/esm/lib/structures/router/RouterRoot.mjs.map +1 -1
- package/dist/esm/lib/utils/_body/RequestHeadersProxy.mjs +95 -0
- package/dist/esm/lib/utils/_body/RequestHeadersProxy.mjs.map +1 -0
- package/dist/esm/lib/utils/_body/RequestProxy.mjs +111 -0
- package/dist/esm/lib/utils/_body/RequestProxy.mjs.map +1 -0
- package/dist/esm/lib/utils/_body/RequestURLProxy.mjs +167 -0
- package/dist/esm/lib/utils/_body/RequestURLProxy.mjs.map +1 -0
- package/dist/esm/lib/utils/constants.mjs +8 -0
- package/dist/esm/lib/utils/constants.mjs.map +1 -0
- package/dist/esm/listeners/_load.mjs.map +1 -1
- package/dist/esm/middlewares/_load.mjs.map +1 -1
- package/dist/esm/middlewares/body.mjs +2 -16
- package/dist/esm/middlewares/body.mjs.map +1 -1
- package/dist/esm/register.d.mts +0 -1
- package/dist/esm/register.mjs +2 -3
- package/dist/esm/register.mjs.map +1 -1
- package/dist/esm/routes/_load.mjs.map +1 -1
- package/dist/esm/routes/oauth/callback.post.mjs +1 -1
- package/dist/esm/routes/oauth/callback.post.mjs.map +1 -1
- package/package.json +3 -2
- package/dist/cjs/lib/structures/MediaParser.cjs +0 -79
- package/dist/cjs/lib/structures/MediaParser.cjs.map +0 -1
- package/dist/cjs/lib/structures/MediaParserStore.cjs +0 -27
- package/dist/cjs/lib/structures/MediaParserStore.cjs.map +0 -1
- package/dist/cjs/mediaParsers/_load.cjs +0 -20
- package/dist/cjs/mediaParsers/_load.cjs.map +0 -1
- package/dist/cjs/mediaParsers/applicationFormUrlEncoded.cjs +0 -22
- package/dist/cjs/mediaParsers/applicationFormUrlEncoded.cjs.map +0 -1
- package/dist/cjs/mediaParsers/applicationJson.cjs +0 -21
- package/dist/cjs/mediaParsers/applicationJson.cjs.map +0 -1
- package/dist/cjs/mediaParsers/textPlain.cjs +0 -21
- package/dist/cjs/mediaParsers/textPlain.cjs.map +0 -1
- package/dist/esm/lib/structures/MediaParser.mjs +0 -76
- package/dist/esm/lib/structures/MediaParser.mjs.map +0 -1
- package/dist/esm/lib/structures/MediaParserStore.mjs +0 -24
- package/dist/esm/lib/structures/MediaParserStore.mjs.map +0 -1
- package/dist/esm/mediaParsers/_load.mjs +0 -17
- package/dist/esm/mediaParsers/_load.mjs.map +0 -1
- package/dist/esm/mediaParsers/applicationFormUrlEncoded.mjs +0 -19
- package/dist/esm/mediaParsers/applicationFormUrlEncoded.mjs.map +0 -1
- package/dist/esm/mediaParsers/applicationJson.mjs +0 -18
- package/dist/esm/mediaParsers/applicationJson.mjs.map +0 -1
- package/dist/esm/mediaParsers/textPlain.mjs +0 -18
- package/dist/esm/mediaParsers/textPlain.mjs.map +0 -1
package/dist/esm/index.mjs
CHANGED
|
@@ -6,8 +6,6 @@ export * from './lib/structures/http/Auth.mjs';
|
|
|
6
6
|
export * from './lib/structures/http/HttpCodes.mjs';
|
|
7
7
|
export * from './lib/structures/http/HttpMethods.mjs';
|
|
8
8
|
export * from './lib/structures/http/Server.mjs';
|
|
9
|
-
export * from './lib/structures/MediaParser.mjs';
|
|
10
|
-
export * from './lib/structures/MediaParserStore.mjs';
|
|
11
9
|
export * from './lib/structures/Middleware.mjs';
|
|
12
10
|
export * from './lib/structures/MiddlewareStore.mjs';
|
|
13
11
|
export * from './lib/structures/Route.mjs';
|
|
@@ -16,11 +14,10 @@ export * from './lib/structures/router/RouterNode.mjs';
|
|
|
16
14
|
export * from './lib/structures/router/RouterRoot.mjs';
|
|
17
15
|
export * from './lib/structures/RouteStore.mjs';
|
|
18
16
|
export { loadListeners } from './listeners/_load.mjs';
|
|
19
|
-
export { loadMediaParsers } from './mediaParsers/_load.mjs';
|
|
20
17
|
export { loadMiddlewares } from './middlewares/_load.mjs';
|
|
21
18
|
export { loadRoutes } from './routes/_load.mjs';
|
|
22
19
|
|
|
23
|
-
var version = "7.0.0-next.
|
|
20
|
+
var version = "7.0.0-next.821ff70";
|
|
24
21
|
|
|
25
22
|
export { version };
|
|
26
23
|
//# sourceMappingURL=index.mjs.map
|
package/dist/esm/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;AAoDO,IAAM,OAAkB,GAAA","file":"index.mjs","sourcesContent":["import type { Server, ServerOptions } from './lib/structures/http/Server';\nimport type { MiddlewareStore } from './lib/structures/MiddlewareStore';\nimport type { RouteStore } from './lib/structures/RouteStore';\n\nexport * from './lib/structures/api/ApiRequest';\nexport * from './lib/structures/api/ApiResponse';\nexport * from './lib/structures/api/CookieStore';\nexport * from './lib/structures/http/Auth';\nexport * from './lib/structures/http/HttpCodes';\nexport * from './lib/structures/http/HttpMethods';\nexport * from './lib/structures/http/Server';\nexport * from './lib/structures/Middleware';\nexport * from './lib/structures/MiddlewareStore';\nexport * from './lib/structures/Route';\nexport * from './lib/structures/router/RouterBranch';\nexport * from './lib/structures/router/RouterNode';\nexport * from './lib/structures/router/RouterRoot';\nexport * from './lib/structures/RouteStore';\nexport type * from './lib/utils/MimeType';\n\nexport { loadListeners } from './listeners/_load';\nexport { loadMiddlewares } from './middlewares/_load';\nexport { loadRoutes } from './routes/_load';\n\ndeclare module 'discord.js' {\n\tinterface Client {\n\t\tserver: Server;\n\t}\n\n\tinterface ClientOptions {\n\t\tapi?: ServerOptions;\n\t}\n}\n\ndeclare module '@sapphire/pieces' {\n\tinterface StoreRegistryEntries {\n\t\troutes: RouteStore;\n\t\tmiddlewares: MiddlewareStore;\n\t}\n\n\tinterface Container {\n\t\tserver: Server;\n\t}\n}\n\n/**\n * The [@sapphire/plugin-api](https://github.com/sapphiredev/plugins/blob/main/packages/api) version that you are currently using.\n * An example use of this is showing it of in a bot information command.\n *\n * Note to Sapphire developers: This needs to explicitly be `string` so it is not typed as the string that gets replaced by esbuild\n */\n// eslint-disable-next-line @typescript-eslint/no-inferrable-types\nexport const version: string = '7.0.0-next.821ff70';\n"]}
|
|
@@ -10,10 +10,6 @@ var _Route = class _Route extends Piece {
|
|
|
10
10
|
* (RFC 7230 3.3.2) The maximum decimal number of octets.
|
|
11
11
|
*/
|
|
12
12
|
__publicField(this, "maximumBodyLength");
|
|
13
|
-
/**
|
|
14
|
-
* The accepted content types.
|
|
15
|
-
*/
|
|
16
|
-
__publicField(this, "acceptedContentMimeTypes");
|
|
17
13
|
/**
|
|
18
14
|
* The path this route represents.
|
|
19
15
|
*/
|
|
@@ -37,7 +33,6 @@ var _Route = class _Route extends Piece {
|
|
|
37
33
|
this.path = path;
|
|
38
34
|
this.methods = methods;
|
|
39
35
|
this.maximumBodyLength = options.maximumBodyLength ?? api.maximumBodyLength ?? 1024 * 1024 * 50;
|
|
40
|
-
this.acceptedContentMimeTypes = options.acceptedContentMimeTypes ?? api.acceptedContentMimeTypes ?? null;
|
|
41
36
|
}
|
|
42
37
|
};
|
|
43
38
|
__name(_Route, "Route");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/lib/structures/Route.ts"],"names":[],"mappings":";;;;;
|
|
1
|
+
{"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AAA,CAAA;AACtF,MAAA,OAAA,CAAQ,IAAI,OAAO,CAAA,CAAA;AAAA,KACpB;AAEA,IAAA,IAAA,CAAK,IAAO,GAAA,IAAA,CAAA;AACZ,IAAA,IAAA,CAAK,OAAU,GAAA,OAAA,CAAA;AACf,IAAA,IAAA,CAAK,oBAAoB,OAAQ,CAAA,iBAAA,IAAqB,GAAI,CAAA,iBAAA,IAAqB,OAAO,IAAO,GAAA,EAAA,CAAA;AAAA,GAC9F;AAGD,CAAA,CAAA;AAvC4G,MAAA,CAAA,MAAA,EAAA,OAAA,CAAA,CAAA;AAArG,IAAe,KAAf,GAAA","file":"Route.mjs","sourcesContent":["import { Piece } from '@sapphire/pieces';\nimport { isNullish, type Awaitable } from '@sapphire/utilities';\nimport type { ApiRequest } from './api/ApiRequest';\nimport type { ApiResponse } from './api/ApiResponse';\nimport type { MethodName } from './http/HttpMethods';\nimport { RouterRoot } from './router/RouterRoot';\n\n/**\n * @since 1.0.0\n *\n * @example A simple GET route that returns a JSON response:\n * ```typescript\n * // hello.get.ts\n * import { Route } from '@sapphire/plugin-api';\n *\n * export class MyRoute extends Route {\n * public run(request: Route.Request, response: Route.Response) {\n * return response.json({ message: 'Hello, World!' });\n * }\n * }\n * ```\n *\n * ```bash\n * $ curl http://localhost:4000/hello\n * {\"message\":\"Hello, World!\"}\n * ```\n *\n * @example A simple POST route that reads the body and returns it:\n * ```typescript\n * // echo.post.ts\n * import { Route } from '@sapphire/plugin-api';\n *\n * export class MyRoute extends Route {\n * public run(request: Route.Request, response: Route.Response) {\n * return response.json(request.params);\n * }\n * }\n * ```\n *\n * ```bash\n * $ curl -X POST -H \"Content-Type: application/json\" -d '{\"hello\":\"world\"}' http://localhost:4000/echo\n * {\"hello\":\"world\"}\n * ```\n */\nexport abstract class Route<Options extends Route.Options = Route.Options> extends Piece<Options, 'routes'> {\n\t/**\n\t * (RFC 7230 3.3.2) The maximum decimal number of octets.\n\t */\n\tpublic readonly maximumBodyLength: number;\n\n\t/**\n\t * The path this route represents.\n\t */\n\tpublic readonly path: readonly string[];\n\n\t/**\n\t * The methods this route accepts.\n\t */\n\tpublic readonly methods: ReadonlySet<MethodName>;\n\n\tpublic constructor(context: Route.LoaderContext, options: Options = {} as Options) {\n\t\tsuper(context, options);\n\n\t\tconst api = this.container.server.options;\n\t\tconst path = [\n\t\t\t...RouterRoot.normalize(api.prefix),\n\t\t\t...RouterRoot.normalize(options.route ?? RouterRoot.makeRoutePathForPiece(this.location.directories, this.name))\n\t\t];\n\n\t\tconst methods = new Set(options.methods);\n\t\tconst implied = RouterRoot.extractMethod(path);\n\t\tif (!isNullish(implied)) {\n\t\t\tconst lastIndex = path.length - 1;\n\t\t\tpath[lastIndex] = path[lastIndex].slice(0, path[lastIndex].length - implied.length - 1);\n\t\t\tmethods.add(implied);\n\t\t}\n\n\t\tthis.path = path;\n\t\tthis.methods = methods;\n\t\tthis.maximumBodyLength = options.maximumBodyLength ?? api.maximumBodyLength ?? 1024 * 1024 * 50;\n\t}\n\n\tpublic abstract run(request: Route.Request, response: Route.Response): Awaitable<unknown>;\n}\n\nexport interface RouteOptions extends Piece.Options {\n\t/**\n\t * The route the piece should represent.\n\t * @since 1.0.0\n\t *\n\t * @defaultValue The filesystem-based path, or the name if the location is virtual.\n\t *\n\t * @example\n\t * ```typescript\n\t * '/users'\n\t * // request.params -> {}\n\t * ```\n\t * @example\n\t * ```typescript\n\t * '/guilds/[guild]/members/[member]'\n\t * // request.params -> { guild: '...', member: '...' }\n\t * ```\n\t */\n\troute?: string;\n\n\t/**\n\t * (RFC 7230 3.3.2) The maximum decimal number of octets.\n\t * @since 1.0.0\n\t *\n\t * @defaultValue this.context.server.options.maximumBodyLength ?? 1024 * 1024 * 50\n\t */\n\tmaximumBodyLength?: number;\n\n\t/**\n\t * The methods this route accepts.\n\t * @since 7.0.0\n\t *\n\t * @defaultValue The method defined in the piece name, or none if not set.\n\t */\n\tmethods?: readonly MethodName[];\n}\n\nexport namespace Route {\n\t/** @deprecated Use {@linkcode LoaderContext} instead. */\n\texport type Context = LoaderContext; // NOSONAR\n\texport type LoaderContext = Piece.LoaderContext<'routes'>;\n\texport type Options = RouteOptions;\n\texport type JSON = Piece.JSON;\n\texport type LocationJSON = Piece.LocationJSON;\n\n\texport type Request = ApiRequest;\n\texport type Response = ApiResponse;\n}\n"]}
|
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import { __name, __publicField } from '../../../chunk-S573YWRP.mjs';
|
|
1
|
+
import { __name, __privateAdd, __publicField, __privateGet, __privateSet } from '../../../chunk-S573YWRP.mjs';
|
|
2
|
+
import { isNullishOrEmpty } from '@sapphire/utilities';
|
|
2
3
|
import { IncomingMessage } from 'node:http';
|
|
4
|
+
import { RequestProxy } from '../../utils/_body/RequestProxy.mjs';
|
|
3
5
|
|
|
6
|
+
var _cachedRequest, _ApiRequest_instances, isFormContentType_get;
|
|
4
7
|
var _ApiRequest = class _ApiRequest extends IncomingMessage {
|
|
5
8
|
constructor() {
|
|
6
9
|
super(...arguments);
|
|
10
|
+
__privateAdd(this, _ApiRequest_instances);
|
|
7
11
|
/**
|
|
8
12
|
* The query parameters.
|
|
9
13
|
*/
|
|
@@ -12,10 +16,6 @@ var _ApiRequest = class _ApiRequest extends IncomingMessage {
|
|
|
12
16
|
* The URI parameters.
|
|
13
17
|
*/
|
|
14
18
|
__publicField(this, "params", {});
|
|
15
|
-
/**
|
|
16
|
-
* The body that was sent by the user.
|
|
17
|
-
*/
|
|
18
|
-
__publicField(this, "body");
|
|
19
19
|
/**
|
|
20
20
|
* The authorization information. This field indicates three possible values:
|
|
21
21
|
*
|
|
@@ -50,8 +50,125 @@ var _ApiRequest = class _ApiRequest extends IncomingMessage {
|
|
|
50
50
|
* @since 7.0.0
|
|
51
51
|
*/
|
|
52
52
|
__publicField(this, "route");
|
|
53
|
+
/**
|
|
54
|
+
* The response object. This field is cached to prevent multiple response
|
|
55
|
+
* objects from being created.
|
|
56
|
+
*/
|
|
57
|
+
__privateAdd(this, _cachedRequest, null);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The response object, used to validate the request's headers and body.
|
|
61
|
+
*/
|
|
62
|
+
asWeb() {
|
|
63
|
+
__privateGet(this, _cachedRequest) ?? __privateSet(this, _cachedRequest, new RequestProxy(this));
|
|
64
|
+
return __privateGet(this, _cachedRequest);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Reads the request body and tries to parse using JSON or form-urlencoded.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* const body = await request.readBody();
|
|
72
|
+
* ```
|
|
73
|
+
*
|
|
74
|
+
* @returns The result of the body parsing
|
|
75
|
+
*/
|
|
76
|
+
readBody() {
|
|
77
|
+
return __privateGet(this, _ApiRequest_instances, isFormContentType_get) ? this.readBodyFormData() : this.readBodyJson();
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Reads the request body as an {@link ArrayBuffer}.
|
|
81
|
+
*
|
|
82
|
+
* @returns The result of the body parsing
|
|
83
|
+
*/
|
|
84
|
+
readBodyArrayBuffer() {
|
|
85
|
+
return this.asWeb().arrayBuffer();
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Reads the request body as a {@link Blob}.
|
|
89
|
+
*
|
|
90
|
+
* @returns The result of the body parsing
|
|
91
|
+
*/
|
|
92
|
+
readBodyBlob() {
|
|
93
|
+
return this.asWeb().blob();
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Reads the request body as a {@link FormData}.
|
|
97
|
+
*
|
|
98
|
+
* @remarks
|
|
99
|
+
*
|
|
100
|
+
* This will throw an error if the content type is not one of the following:
|
|
101
|
+
*
|
|
102
|
+
* - `application/x-www-form-urlencoded`
|
|
103
|
+
* - `multipart/form-data`
|
|
104
|
+
*
|
|
105
|
+
* @returns The result of the body parsing
|
|
106
|
+
*/
|
|
107
|
+
readBodyFormData() {
|
|
108
|
+
return this.asWeb().formData();
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Reads the request body as text, using {@link TextDecoder}. Afterward, it
|
|
112
|
+
* parses the body as JSON with {@link JSON.parse}.
|
|
113
|
+
*
|
|
114
|
+
* @returns The result of the body parsing
|
|
115
|
+
*/
|
|
116
|
+
readBodyJson() {
|
|
117
|
+
return this.asWeb().json();
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Reads the request body as text, using {@link TextDecoder}.
|
|
121
|
+
*
|
|
122
|
+
* @returns The result of the body parsing
|
|
123
|
+
*/
|
|
124
|
+
readBodyText() {
|
|
125
|
+
return this.asWeb().text();
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Identical to {@link ApiRequest.readBody}, but it validates the result.
|
|
129
|
+
*
|
|
130
|
+
* @param validator The validator function to use on the body parsing result
|
|
131
|
+
* @returns The validated body
|
|
132
|
+
*/
|
|
133
|
+
readValidatedBody(validator) {
|
|
134
|
+
return this.readBody().then(validator);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Identical to {@link ApiRequest.readBodyFormData}, but it validates the
|
|
138
|
+
* result.
|
|
139
|
+
*
|
|
140
|
+
* @param validator The validator function to use on the body parsing result
|
|
141
|
+
* @returns The validated body
|
|
142
|
+
*/
|
|
143
|
+
readValidatedBodyFormData(validator) {
|
|
144
|
+
return this.readBodyFormData().then(validator);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Identical to {@link ApiRequest.readBodyJson}, but it validates the result.
|
|
148
|
+
*
|
|
149
|
+
* @param validator The validator function to use on the body parsing result
|
|
150
|
+
* @returns The validated body
|
|
151
|
+
*/
|
|
152
|
+
readValidatedBodyJson(validator) {
|
|
153
|
+
return this.readBodyJson().then(validator);
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Identical to {@link ApiRequest.readBodyText}, but it validates the result.
|
|
157
|
+
*
|
|
158
|
+
* @param validator The validator function to use on the body parsing result
|
|
159
|
+
* @returns The validated body
|
|
160
|
+
*/
|
|
161
|
+
readValidatedBodyText(validator) {
|
|
162
|
+
return this.readBodyText().then(validator);
|
|
53
163
|
}
|
|
54
164
|
};
|
|
165
|
+
_cachedRequest = new WeakMap();
|
|
166
|
+
_ApiRequest_instances = new WeakSet();
|
|
167
|
+
isFormContentType_get = /* @__PURE__ */ __name(function() {
|
|
168
|
+
const contentType = this.asWeb().headers.get("content-type");
|
|
169
|
+
if (isNullishOrEmpty(contentType)) return false;
|
|
170
|
+
return contentType.startsWith("application/x-www-form-urlencoded") || contentType.startsWith("multipart/form-data");
|
|
171
|
+
}, "#isFormContentType");
|
|
55
172
|
__name(_ApiRequest, "ApiRequest");
|
|
56
173
|
var ApiRequest = _ApiRequest;
|
|
57
174
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../src/lib/structures/api/ApiRequest.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../../../../src/lib/structures/api/ApiRequest.ts"],"names":[],"mappings":";;;;;AAAA,IAAA,cAAA,EAAA,qBAAA,EAAA,qBAAA,CAAA;AAQO,IAAM,WAAA,GAAN,MAAM,WAAA,SAAmB,eAAgB,CAAA;AAAA,EAAzC,WAAA,GAAA;AAAA,IAAA,KAAA,CAAA,GAAA,SAAA,CAAA,CAAA;AAAA,IAAA,YAAA,CAAA,IAAA,EAAA,qBAAA,CAAA,CAAA;AAIN;AAAA;AAAA;AAAA,IAAA,aAAA,CAAA,IAAA,EAAO,SAA2C,EAAC,CAAA,CAAA;AAKnD;AAAA;AAAA;AAAA,IAAA,aAAA,CAAA,IAAA,EAAO,UAAiC,EAAC,CAAA,CAAA;AASzC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAO,aAAA,CAAA,IAAA,EAAA,MAAA,CAAA,CAAA;AAcP;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAO,aAAA,CAAA,IAAA,EAAA,Y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AAqB,CAAA,CAAA;AACnH,CARsB,EAAA,oBAAA,CAAA,CAAA;AAtDyB,MAAA,CAAA,WAAA,EAAA,YAAA,CAAA,CAAA;AAAzC,IAAM,UAAN,GAAA","file":"ApiRequest.mjs","sourcesContent":["import { isNullishOrEmpty } from '@sapphire/utilities';\nimport { IncomingMessage } from 'node:http';\nimport type { MimeType } from '../../utils/MimeType';\nimport { RequestProxy } from '../../utils/_body/RequestProxy';\nimport type { Route } from '../Route';\nimport type { AuthData } from '../http/Auth';\nimport type { RouterNode } from '../router/RouterNode';\n\nexport class ApiRequest extends IncomingMessage {\n\t/**\n\t * The query parameters.\n\t */\n\tpublic query: Record<string, string | string[]> = {};\n\n\t/**\n\t * The URI parameters.\n\t */\n\tpublic params: Record<string, string> = {};\n\n\t/**\n\t * The authorization information. This field indicates three possible values:\n\t *\n\t * - `undefined`: The authorization middleware has not been executed yet.\n\t * - `null`: The user is not authorized.\n\t * - `AuthData`: The user is authorized.\n\t */\n\tpublic auth?: AuthData | null;\n\n\t/**\n\t * The router node that matched the request. The field indicates three\n\t * possible values:\n\t *\n\t * - `undefined`: The router handler has not been executed yet.\n\t * - `null`: The router handler has been executed, but no node matched the\n\t * request.\n\t * - `RouterNode`: The router handler has been executed and a node matched\n\t * the request.\n\t *\n\t * @since 7.0.0\n\t */\n\tpublic routerNode?: RouterNode | null;\n\n\t/**\n\t * The route that matched the request. The field indicates three possible\n\t * values:\n\t *\n\t * - `undefined`: The router handler has not been executed yet.\n\t * - `null`: The router handler has been executed, but no route matched the\n\t * request.\n\t * - `Route`: The router handler has been executed and a route matched the\n\t * request.\n\t *\n\t * @since 7.0.0\n\t */\n\tpublic route?: Route | null;\n\n\t/**\n\t * The response object. This field is cached to prevent multiple response\n\t * objects from being created.\n\t */\n\t#cachedRequest: Request | null = null;\n\n\tget #isFormContentType() {\n\t\tconst contentType = this.asWeb().headers.get('content-type') as MimeType | null;\n\n\t\t// If Content-Type isn't sent, we can't assume it's a form.\n\t\tif (isNullishOrEmpty(contentType)) return false;\n\n\t\t// If the Content-Type is application/x-www-form-urlencoded or multipart/form-data, it's a form.\n\t\treturn contentType.startsWith('application/x-www-form-urlencoded') || contentType.startsWith('multipart/form-data');\n\t}\n\n\t/**\n\t * The response object, used to validate the request's headers and body.\n\t */\n\tpublic asWeb(): Request {\n\t\tthis.#cachedRequest ??= new RequestProxy(this);\n\t\treturn this.#cachedRequest;\n\t}\n\n\t/**\n\t * Reads the request body and tries to parse using JSON or form-urlencoded.\n\t *\n\t * @example\n\t * ```typescript\n\t * const body = await request.readBody();\n\t * ```\n\t *\n\t * @returns The result of the body parsing\n\t */\n\tpublic readBody() {\n\t\treturn this.#isFormContentType ? this.readBodyFormData() : this.readBodyJson();\n\t}\n\n\t/**\n\t * Reads the request body as an {@link ArrayBuffer}.\n\t *\n\t * @returns The result of the body parsing\n\t */\n\tpublic readBodyArrayBuffer() {\n\t\treturn this.asWeb().arrayBuffer();\n\t}\n\n\t/**\n\t * Reads the request body as a {@link Blob}.\n\t *\n\t * @returns The result of the body parsing\n\t */\n\tpublic readBodyBlob() {\n\t\treturn this.asWeb().blob();\n\t}\n\n\t/**\n\t * Reads the request body as a {@link FormData}.\n\t *\n\t * @remarks\n\t *\n\t * This will throw an error if the content type is not one of the following:\n\t *\n\t * - `application/x-www-form-urlencoded`\n\t * - `multipart/form-data`\n\t *\n\t * @returns The result of the body parsing\n\t */\n\tpublic readBodyFormData() {\n\t\treturn this.asWeb().formData(); // NOSONAR\n\t}\n\n\t/**\n\t * Reads the request body as text, using {@link TextDecoder}. Afterward, it\n\t * parses the body as JSON with {@link JSON.parse}.\n\t *\n\t * @returns The result of the body parsing\n\t */\n\tpublic readBodyJson() {\n\t\treturn this.asWeb().json();\n\t}\n\n\t/**\n\t * Reads the request body as text, using {@link TextDecoder}.\n\t *\n\t * @returns The result of the body parsing\n\t */\n\tpublic readBodyText() {\n\t\treturn this.asWeb().text();\n\t}\n\n\t/**\n\t * Identical to {@link ApiRequest.readBody}, but it validates the result.\n\t *\n\t * @param validator The validator function to use on the body parsing result\n\t * @returns The validated body\n\t */\n\tpublic readValidatedBody<Type>(validator: ValidatorFunction<unknown, Type>) {\n\t\treturn this.readBody().then(validator);\n\t}\n\n\t/**\n\t * Identical to {@link ApiRequest.readBodyFormData}, but it validates the\n\t * result.\n\t *\n\t * @param validator The validator function to use on the body parsing result\n\t * @returns The validated body\n\t */\n\tpublic readValidatedBodyFormData<Type extends FormData>(validator: ValidatorFunction<FormData, Type>) {\n\t\treturn this.readBodyFormData().then(validator);\n\t}\n\n\t/**\n\t * Identical to {@link ApiRequest.readBodyJson}, but it validates the result.\n\t *\n\t * @param validator The validator function to use on the body parsing result\n\t * @returns The validated body\n\t */\n\tpublic readValidatedBodyJson<Type>(validator: ValidatorFunction<unknown, Type>) {\n\t\treturn this.readBodyJson().then(validator);\n\t}\n\n\t/**\n\t * Identical to {@link ApiRequest.readBodyText}, but it validates the result.\n\t *\n\t * @param validator The validator function to use on the body parsing result\n\t * @returns The validated body\n\t */\n\tpublic readValidatedBodyText<Type extends string>(validator: ValidatorFunction<string, Type>) {\n\t\treturn this.readBodyText().then(validator);\n\t}\n}\n\nexport type ValidatorFunction<Data, Type extends Data> = (data: Data) => Type | ((data: Data) => data is Type);\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../src/lib/structures/http/Auth.ts"],"names":[],"mappings":";;;;;;AAAA,IAAA,OAAA,CAAA;AAaO,IAAM,KAAA,GAAN,MAAM,KAAK,CAAA;AAAA,EAmCT,YAAY,OAA4B,EAAA;AA9BhD;AAAA;AAAA;AAAA;AAAA,IAAO,aAAA,CAAA,IAAA,EAAA,IAAA,CAAA,CAAA;AAMP;AAAA;AAAA;AAAA;AAAA,IAAO,aAAA,CAAA,IAAA,EAAA,QAAA,CAAA,CAAA;AAMP;AAAA;AAAA;AAAA;AAAA,IAAO,aAAA,CAAA,IAAA,EAAA,QAAA,CAAA,CAAA;AAMP;AAAA;AAAA;AA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AAA,CAAA,KAAA,EAAA,MAAA,CAAA,CAAA;AAAX,IAAM,IAAN,GAAA","file":"Auth.mjs","sourcesContent":["import { isThenable, type Awaitable } from '@sapphire/utilities';\nimport { createCipheriv, createDecipheriv, randomBytes } from 'crypto';\nimport {\n\tOAuth2Scopes,\n\tRouteBases,\n\tRoutes,\n\ttype RESTGetAPICurrentUserConnectionsResult,\n\ttype RESTGetAPICurrentUserGuildsResult,\n\ttype RESTGetAPICurrentUserResult,\n\ttype Snowflake\n} from 'discord.js';\nimport { fetch } from 'undici';\n\nexport class Auth {\n\t/**\n\t * The client's application id, this can be retrieved in Discord Developer Portal at https://discord.com/developers/applications.\n\t * @since 1.0.0\n\t */\n\tpublic id: Snowflake;\n\n\t/**\n\t * The name for the cookie, this will be used to identify a Secure HttpOnly cookie.\n\t * @since 1.0.0\n\t */\n\tpublic cookie: string;\n\n\t/**\n\t * The scopes defined at https://discord.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes.\n\t * @since 1.0.0\n\t */\n\tpublic scopes: readonly OAuth2Scopes[];\n\n\t/**\n\t * The redirect uri.\n\t * @since 1.0.0\n\t */\n\tpublic redirect: string | undefined;\n\n\t/**\n\t * The transformers used for {@link Auth.fetchData}.\n\t * @since 1.4.0\n\t */\n\tpublic transformers: LoginDataTransformer[];\n\n\tpublic domainOverwrite: string | null = null;\n\n\t#secret: string;\n\n\tprivate constructor(options: ServerOptionsAuth) {\n\t\tthis.id = options.id as Snowflake;\n\t\tthis.cookie = options.cookie ?? 'SAPPHIRE_AUTH';\n\t\tthis.scopes = options.scopes ?? [OAuth2Scopes.Identify];\n\t\tthis.redirect = options.redirect;\n\t\tthis.#secret = options.secret;\n\t\tthis.transformers = options.transformers ?? [];\n\t\tthis.domainOverwrite = options.domainOverwrite ?? null;\n\t}\n\n\t/**\n\t * The client secret, this can be retrieved in Discord Developer Portal at https://discord.com/developers/applications.\n\t * @since 1.0.0\n\t */\n\tpublic get secret() {\n\t\treturn this.#secret;\n\t}\n\n\t/**\n\t * Encrypts an object with aes-256-cbc to use as a token.\n\t * @since 1.0.0\n\t * @param data An object to encrypt\n\t */\n\tpublic encrypt(data: AuthData): string {\n\t\tconst iv = randomBytes(16);\n\t\tconst cipher = createCipheriv('aes-256-cbc', this.#secret, iv);\n\t\treturn `${cipher.update(JSON.stringify(data), 'utf8', 'base64') + cipher.final('base64')}.${iv.toString('base64')}`;\n\t}\n\n\t/**\n\t * Decrypts an object with aes-256-cbc to use as a token.\n\t * @since 1.0.0\n\t * @param token An data to decrypt\n\t */\n\tpublic decrypt(token: string): AuthData | null {\n\t\tconst [data, iv] = token.split('.');\n\t\tconst decipher = createDecipheriv('aes-256-cbc', this.#secret, Buffer.from(iv, 'base64'));\n\n\t\ttry {\n\t\t\tconst parsed = JSON.parse(decipher.update(data, 'base64', 'utf8') + decipher.final('utf8')) as AuthData;\n\t\t\t// If the token expired, return null:\n\t\t\treturn parsed.expires >= Date.now() ? parsed : null;\n\t\t} catch {\n\t\t\treturn null;\n\t\t}\n\t}\n\n\t/**\n\t * Retrieves the data for a specific user.\n\t * @since 1.4.0\n\t * @param token The access token from the user.\n\t */\n\tpublic async fetchData(token: string): Promise<LoginData> {\n\t\t// Fetch the information:\n\t\tconst [user, guilds, connections] = await Promise.all([\n\t\t\tthis.fetchInformation<RESTGetAPICurrentUserResult>(OAuth2Scopes.Identify, token, `${RouteBases.api}${Routes.user()}`),\n\t\t\tthis.fetchInformation<RESTGetAPICurrentUserGuildsResult>(OAuth2Scopes.Guilds, token, `${RouteBases.api}${Routes.userGuilds()}`),\n\t\t\tthis.fetchInformation<RESTGetAPICurrentUserConnectionsResult>(\n\t\t\t\tOAuth2Scopes.Connections,\n\t\t\t\ttoken,\n\t\t\t\t`${RouteBases.api}${Routes.userConnections()}`\n\t\t\t)\n\t\t]);\n\n\t\t// Transform the information:\n\t\tlet data: LoginData = { user, guilds, connections };\n\t\tfor (const transformer of this.transformers) {\n\t\t\tconst result = transformer(data);\n\t\t\tif (isThenable(result)) data = await result;\n\t\t\telse data = result as LoginData;\n\t\t}\n\n\t\treturn data;\n\t}\n\n\tprivate async fetchInformation<T>(scope: OAuth2Scopes, token: string, url: string): Promise<T | null | undefined> {\n\t\tif (!this.scopes.includes(scope)) return undefined;\n\n\t\tconst result = await fetch(url, {\n\t\t\theaders: {\n\t\t\t\tauthorization: `Bearer ${token}`\n\t\t\t}\n\t\t});\n\n\t\treturn result.ok ? ((await result.json()) as T) : null;\n\t}\n\n\tpublic static create(options?: ServerOptionsAuth): Auth | null {\n\t\tif (!options?.secret || !options.id) return null;\n\t\treturn new Auth(options);\n\t}\n}\n\n/**\n * Defines the authentication data, this is to be encrypted and decrypted by the server.\n * @since 1.0.0\n */\nexport interface AuthData {\n\t/**\n\t * The user ID.\n\t * @since 1.0.0\n\t */\n\tid: string;\n\n\t/**\n\t * The timestamp at which the token expires.\n\t * @since 1.0.0\n\t */\n\texpires: number;\n\n\t/**\n\t * The refresh token.\n\t * @since 1.0.0\n\t */\n\trefresh: string;\n\n\t/**\n\t * The access token.\n\t * @since 1.0.0\n\t */\n\ttoken: string;\n}\n\n/**\n * Defines the authentication options.\n * @since 1.0.0\n */\nexport interface ServerOptionsAuth {\n\t/**\n\t * The client's application id, this can be retrieved in Discord Developer Portal at https://discord.com/developers/applications.\n\t * @since 1.0.0\n\t */\n\tid: string;\n\n\t/**\n\t * The name for the cookie, this will be used to identify a Secure HttpOnly cookie.\n\t * @since 1.0.0\n\t * @default 'SAPPHIRE_AUTH'\n\t */\n\tcookie?: string;\n\n\t/**\n\t * The client secret, this can be retrieved in Discord Developer Portal at https://discord.com/developers/applications.\n\t * @since 1.0.0\n\t */\n\tsecret: string;\n\n\t/**\n\t * The scopes defined at https://discord.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes.\n\t * @since 1.0.0\n\t * @default [OAuth2Scopes.Identify]\n\t */\n\tscopes?: OAuth2Scopes[];\n\n\t/**\n\t * The redirect uri. This will default to {@link OAuth2BodyData.redirectUri} if missing.\n\t * @since 1.0.0\n\t */\n\tredirect?: string;\n\n\t/**\n\t * The login data transformers used for {@link Auth.fetchData}.\n\t * @since 1.4.0\n\t * @default []\n\t */\n\ttransformers?: LoginDataTransformer[];\n\t/**\n\t * The domain that should be used for the cookie. This overwrites the automatic detection of the domain.\n\t * @remark if you want to support subdomains (`one.example.two` and `two.example.com`) then you need to use prefix your domain with a `.`, for example `.example.com`\n\t * @since 2.1.0\n\t * @default undefined\n\t */\n\tdomainOverwrite?: string;\n}\n\n/**\n * The login data sent when fetching data from a user.\n * @since 1.4.0\n */\nexport interface LoginData {\n\t/**\n\t * The user data, defined when the `'identify'` scope is defined.\n\t * @since 1.4.0\n\t */\n\tuser?: RESTGetAPICurrentUserResult | null;\n\n\t/**\n\t * The guilds data, defined when the `'guilds'` scope is defined.\n\t * @since 1.4.0\n\t */\n\tguilds?: RESTGetAPICurrentUserGuildsResult | null;\n\n\t/**\n\t * The connections data, defined when the `'connections'` scope is defined.\n\t * @since 1.4.0\n\t */\n\tconnections?: RESTGetAPICurrentUserConnectionsResult | null;\n}\n\n/**\n * A login data transformer.\n * @since 1.4.0\n */\nexport interface LoginDataTransformer<T extends LoginData = LoginData> {\n\t/**\n\t * Transforms the object by mutating its properties or adding new ones.\n\t * @since 1.4.0\n\t */\n\t(data: LoginData): Awaitable<T>;\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../../../../src/lib/structures/http/Auth.ts"],"names":[],"mappings":";;;;;;AAAA,IAAA,OAAA,CAAA;AAaO,IAAM,KAAA,GAAN,MAAM,KAAK,CAAA;AAAA,EAmCT,YAAY,OAA4B,EAAA;AA9BhD;AAAA;AAAA;AAAA;AAAA,IAAO,aAAA,CAAA,IAAA,EAAA,IAAA,CAAA,CAAA;AAMP;AAAA;AAAA;AAAA;AAAA,IAAO,aAAA,CAAA,IAAA,EAAA,QAAA,CAAA,CAAA;AAMP;AAAA;AAAA;AAAA;AAAA,IAAO,aAAA,CAAA,IAAA,EAAA,QAAA,CAAA,C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AA,EAAA,CAAA;AAjCiB,MAAA,CAAA,KAAA,EAAA,MAAA,CAAA,CAAA;AAAX,IAAM,IAAN,GAAA","file":"Auth.mjs","sourcesContent":["import { isThenable, type Awaitable } from '@sapphire/utilities';\nimport { createCipheriv, createDecipheriv, randomBytes } from 'crypto';\nimport {\n\tOAuth2Scopes,\n\tRouteBases,\n\tRoutes,\n\ttype RESTGetAPICurrentUserConnectionsResult,\n\ttype RESTGetAPICurrentUserGuildsResult,\n\ttype RESTGetAPICurrentUserResult,\n\ttype Snowflake\n} from 'discord.js';\nimport { fetch } from 'undici';\n\nexport class Auth {\n\t/**\n\t * The client's application id, this can be retrieved in Discord Developer Portal at https://discord.com/developers/applications.\n\t * @since 1.0.0\n\t */\n\tpublic id: Snowflake;\n\n\t/**\n\t * The name for the cookie, this will be used to identify a Secure HttpOnly cookie.\n\t * @since 1.0.0\n\t */\n\tpublic cookie: string;\n\n\t/**\n\t * The scopes defined at https://discord.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes.\n\t * @since 1.0.0\n\t */\n\tpublic scopes: readonly OAuth2Scopes[];\n\n\t/**\n\t * The redirect uri.\n\t * @since 1.0.0\n\t */\n\tpublic redirect: string | undefined;\n\n\t/**\n\t * The transformers used for {@link Auth.fetchData}.\n\t * @since 1.4.0\n\t */\n\tpublic transformers: LoginDataTransformer[];\n\n\tpublic domainOverwrite: string | null = null;\n\n\t#secret: string;\n\n\tprivate constructor(options: ServerOptionsAuth) {\n\t\tthis.id = options.id;\n\t\tthis.cookie = options.cookie ?? 'SAPPHIRE_AUTH';\n\t\tthis.scopes = options.scopes ?? [OAuth2Scopes.Identify];\n\t\tthis.redirect = options.redirect;\n\t\tthis.#secret = options.secret;\n\t\tthis.transformers = options.transformers ?? [];\n\t\tthis.domainOverwrite = options.domainOverwrite ?? null;\n\t}\n\n\t/**\n\t * The client secret, this can be retrieved in Discord Developer Portal at https://discord.com/developers/applications.\n\t * @since 1.0.0\n\t */\n\tpublic get secret() {\n\t\treturn this.#secret;\n\t}\n\n\t/**\n\t * Encrypts an object with aes-256-cbc to use as a token.\n\t * @since 1.0.0\n\t * @param data An object to encrypt\n\t */\n\tpublic encrypt(data: AuthData): string {\n\t\tconst iv = randomBytes(16);\n\t\tconst cipher = createCipheriv('aes-256-cbc', this.#secret, iv);\n\t\treturn `${cipher.update(JSON.stringify(data), 'utf8', 'base64') + cipher.final('base64')}.${iv.toString('base64')}`;\n\t}\n\n\t/**\n\t * Decrypts an object with aes-256-cbc to use as a token.\n\t * @since 1.0.0\n\t * @param token An data to decrypt\n\t */\n\tpublic decrypt(token: string): AuthData | null {\n\t\tconst [data, iv] = token.split('.');\n\t\tconst decipher = createDecipheriv('aes-256-cbc', this.#secret, Buffer.from(iv, 'base64'));\n\n\t\ttry {\n\t\t\tconst parsed = JSON.parse(decipher.update(data, 'base64', 'utf8') + decipher.final('utf8')) as AuthData;\n\t\t\t// If the token expired, return null:\n\t\t\treturn parsed.expires >= Date.now() ? parsed : null;\n\t\t} catch {\n\t\t\treturn null;\n\t\t}\n\t}\n\n\t/**\n\t * Retrieves the data for a specific user.\n\t * @since 1.4.0\n\t * @param token The access token from the user.\n\t */\n\tpublic async fetchData(token: string): Promise<LoginData> {\n\t\t// Fetch the information:\n\t\tconst [user, guilds, connections] = await Promise.all([\n\t\t\tthis.fetchInformation<RESTGetAPICurrentUserResult>(OAuth2Scopes.Identify, token, `${RouteBases.api}${Routes.user()}`),\n\t\t\tthis.fetchInformation<RESTGetAPICurrentUserGuildsResult>(OAuth2Scopes.Guilds, token, `${RouteBases.api}${Routes.userGuilds()}`),\n\t\t\tthis.fetchInformation<RESTGetAPICurrentUserConnectionsResult>(\n\t\t\t\tOAuth2Scopes.Connections,\n\t\t\t\ttoken,\n\t\t\t\t`${RouteBases.api}${Routes.userConnections()}`\n\t\t\t)\n\t\t]);\n\n\t\t// Transform the information:\n\t\tlet data: LoginData = { user, guilds, connections };\n\t\tfor (const transformer of this.transformers) {\n\t\t\tconst result = transformer(data);\n\t\t\tif (isThenable(result)) data = await result;\n\t\t\telse data = result as LoginData;\n\t\t}\n\n\t\treturn data;\n\t}\n\n\tprivate async fetchInformation<T>(scope: OAuth2Scopes, token: string, url: string): Promise<T | null | undefined> {\n\t\tif (!this.scopes.includes(scope)) return undefined;\n\n\t\tconst result = await fetch(url, {\n\t\t\theaders: {\n\t\t\t\tauthorization: `Bearer ${token}`\n\t\t\t}\n\t\t});\n\n\t\treturn result.ok ? ((await result.json()) as T) : null;\n\t}\n\n\tpublic static create(options?: ServerOptionsAuth): Auth | null {\n\t\tif (!options?.secret || !options.id) return null;\n\t\treturn new Auth(options);\n\t}\n}\n\n/**\n * Defines the authentication data, this is to be encrypted and decrypted by the server.\n * @since 1.0.0\n */\nexport interface AuthData {\n\t/**\n\t * The user ID.\n\t * @since 1.0.0\n\t */\n\tid: string;\n\n\t/**\n\t * The timestamp at which the token expires.\n\t * @since 1.0.0\n\t */\n\texpires: number;\n\n\t/**\n\t * The refresh token.\n\t * @since 1.0.0\n\t */\n\trefresh: string;\n\n\t/**\n\t * The access token.\n\t * @since 1.0.0\n\t */\n\ttoken: string;\n}\n\n/**\n * Defines the authentication options.\n * @since 1.0.0\n */\nexport interface ServerOptionsAuth {\n\t/**\n\t * The client's application id, this can be retrieved in Discord Developer Portal at https://discord.com/developers/applications.\n\t * @since 1.0.0\n\t */\n\tid: Snowflake;\n\n\t/**\n\t * The name for the cookie, this will be used to identify a Secure HttpOnly cookie.\n\t * @since 1.0.0\n\t * @default 'SAPPHIRE_AUTH'\n\t */\n\tcookie?: string;\n\n\t/**\n\t * The client secret, this can be retrieved in Discord Developer Portal at https://discord.com/developers/applications.\n\t * @since 1.0.0\n\t */\n\tsecret: string;\n\n\t/**\n\t * The scopes defined at https://discord.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes.\n\t * @since 1.0.0\n\t * @default [OAuth2Scopes.Identify]\n\t */\n\tscopes?: OAuth2Scopes[];\n\n\t/**\n\t * The redirect uri. This will default to {@link OAuth2BodyData.redirectUri} if missing.\n\t * @since 1.0.0\n\t */\n\tredirect?: string;\n\n\t/**\n\t * The login data transformers used for {@link Auth.fetchData}.\n\t * @since 1.4.0\n\t * @default []\n\t */\n\ttransformers?: LoginDataTransformer[];\n\t/**\n\t * The domain that should be used for the cookie. This overwrites the automatic detection of the domain.\n\t * @remark if you want to support subdomains (`one.example.two` and `two.example.com`) then you need to use prefix your domain with a `.`, for example `.example.com`\n\t * @since 2.1.0\n\t * @default undefined\n\t */\n\tdomainOverwrite?: string;\n}\n\n/**\n * The login data sent when fetching data from a user.\n * @since 1.4.0\n */\nexport interface LoginData {\n\t/**\n\t * The user data, defined when the `'identify'` scope is defined.\n\t * @since 1.4.0\n\t */\n\tuser?: RESTGetAPICurrentUserResult | null;\n\n\t/**\n\t * The guilds data, defined when the `'guilds'` scope is defined.\n\t * @since 1.4.0\n\t */\n\tguilds?: RESTGetAPICurrentUserGuildsResult | null;\n\n\t/**\n\t * The connections data, defined when the `'connections'` scope is defined.\n\t * @since 1.4.0\n\t */\n\tconnections?: RESTGetAPICurrentUserConnectionsResult | null;\n}\n\n/**\n * A login data transformer.\n * @since 1.4.0\n */\nexport interface LoginDataTransformer<T extends LoginData = LoginData> {\n\t/**\n\t * Transforms the object by mutating its properties or adding new ones.\n\t * @since 1.4.0\n\t */\n\t(data: LoginData): Awaitable<T>;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../src/lib/structures/http/HttpMethods.ts"],"names":[],"mappings":";;;AAEO,IAAM,WAAc,GAAA;AAAA,EAC1B,KAAA;AAAA,EACA,MAAA;AAAA,EACA,UAAA;AAAA,EACA,SAAA;AAAA,EACA,MAAA;AAAA,EACA,QAAA;AAAA,EACA,KAAA;AAAA,EACA,MAAA;AAAA,EACA,MAAA;AAAA,EACA,MAAA;AAAA,EACA,UAAA;AAAA,EACA,OAAA;AAAA,EACA,YAAA;AAAA,EACA,YAAA;AAAA,EACA,OAAA;AAAA,EACA,MAAA;AAAA,EACA,QAAA;AAAA,EACA,SAAA;AAAA,EACA,OAAA;AAAA,EACA,MAAA;AAAA,EACA,UAAA;AAAA,EACA,WAAA;AAAA,EACA,OAAA;AAAA,EACA,KAAA;AAAA,EACA,QAAA;AAAA,EACA,QAAA;AAAA,EACA,QAAA;AAAA,EACA,QAAA;AAAA,EACA,WAAA;AAAA,EACA,OAAA;AAAA,EACA,QAAA;AAAA,EACA,QAAA;AAAA,EACA,QAAA;AAAA,EACA,aAAA;AACD","file":"HttpMethods.mjs","sourcesContent":["export type MethodName = (typeof MethodNames)[number];\n\nexport const MethodNames = [\n\t'ACL',\n\t'BIND',\n\t'CHECKOUT',\n\t'CONNECT',\n\t'COPY',\n\t'DELETE',\n\t'GET',\n\t'HEAD',\n\t'LINK',\n\t'LOCK',\n\t'M-SEARCH',\n\t'MERGE',\n\t'MKACTIVITY',\n\t'MKCALENDAR',\n\t'MKCOL',\n\t'MOVE',\n\t'NOTIFY',\n\t'OPTIONS',\n\t'PATCH',\n\t'POST',\n\t'PROPFIND',\n\t'PROPPATCH',\n\t'PURGE',\n\t'PUT',\n\t'REBIND',\n\t'REPORT',\n\t'SEARCH',\n\t'SOURCE',\n\t'SUBSCRIBE',\n\t'TRACE',\n\t'UNBIND',\n\t'UNLINK',\n\t'UNLOCK',\n\t'UNSUBSCRIBE'\n] as const;\n"]}
|
|
1
|
+
{"version":3,"sources":["../../../../../src/lib/structures/http/HttpMethods.ts"],"names":[],"mappings":";;;AAEO,IAAM,WAAc,GAAA;AAAA,EAC1B,KAAA;AAAA,EACA,MAAA;AAAA,EACA,UAAA;AAAA,EACA,SAAA;AAAA,EACA,MAAA;AAAA,EACA,QAAA;AAAA,EACA,KAAA;AAAA,EACA,MAAA;AAAA,EACA,MAAA;AAAA,EACA,MAAA;AAAA,EACA,UAAA;AAAA,EACA,OAAA;AAAA,EACA,YAAA;AAAA,EACA,YAAA;AAAA,EACA,OAAA;AAAA,EACA,MAAA;AAAA,EACA,QAAA;AAAA,EACA,SAAA;AAAA,EACA,OAAA;AAAA,EACA,MAAA;AAAA,EACA,UAAA;AAAA,EACA,WAAA;AAAA,EACA,OAAA;AAAA,EACA,KAAA;AAAA,EACA,OAAA;AAAA,EACA,QAAA;AAAA,EACA,QAAA;AAAA,EACA,QAAA;AAAA,EACA,QAAA;AAAA,EACA,WAAA;AAAA,EACA,OAAA;AAAA,EACA,QAAA;AAAA,EACA,QAAA;AAAA,EACA,QAAA;AAAA,EACA,aAAA;AACD","file":"HttpMethods.mjs","sourcesContent":["export type MethodName = (typeof MethodNames)[number];\n\nexport const MethodNames = [\n\t'ACL',\n\t'BIND',\n\t'CHECKOUT',\n\t'CONNECT',\n\t'COPY',\n\t'DELETE',\n\t'GET',\n\t'HEAD',\n\t'LINK',\n\t'LOCK',\n\t'M-SEARCH',\n\t'MERGE',\n\t'MKACTIVITY',\n\t'MKCALENDAR',\n\t'MKCOL',\n\t'MOVE',\n\t'NOTIFY',\n\t'OPTIONS',\n\t'PATCH',\n\t'POST',\n\t'PROPFIND',\n\t'PROPPATCH',\n\t'PURGE',\n\t'PUT',\n\t'QUERY',\n\t'REBIND',\n\t'REPORT',\n\t'SEARCH',\n\t'SOURCE',\n\t'SUBSCRIBE',\n\t'TRACE',\n\t'UNBIND',\n\t'UNLINK',\n\t'UNLOCK',\n\t'UNSUBSCRIBE'\n] as const;\n"]}
|
|
@@ -2,7 +2,6 @@ import { __name, __publicField } from '../../../chunk-S573YWRP.mjs';
|
|
|
2
2
|
import { container } from '@sapphire/pieces';
|
|
3
3
|
import { AsyncEventEmitter } from '@vladfrangu/async_event_emitter';
|
|
4
4
|
import { createServer } from 'node:http';
|
|
5
|
-
import { MediaParserStore } from '../MediaParserStore.mjs';
|
|
6
5
|
import { MiddlewareStore } from '../MiddlewareStore.mjs';
|
|
7
6
|
import { RouteStore } from '../RouteStore.mjs';
|
|
8
7
|
import { ApiRequest } from '../api/ApiRequest.mjs';
|
|
@@ -38,11 +37,6 @@ var _Server = class _Server extends AsyncEventEmitter {
|
|
|
38
37
|
* @since 1.0.0
|
|
39
38
|
*/
|
|
40
39
|
__publicField(this, "middlewares");
|
|
41
|
-
/**
|
|
42
|
-
* The media parsers this server holds.
|
|
43
|
-
* @since 1.3.0
|
|
44
|
-
*/
|
|
45
|
-
__publicField(this, "mediaParsers");
|
|
46
40
|
/**
|
|
47
41
|
* The authentication system.
|
|
48
42
|
* @since 1.0.0
|
|
@@ -67,7 +61,6 @@ var _Server = class _Server extends AsyncEventEmitter {
|
|
|
67
61
|
});
|
|
68
62
|
this.routes = new RouteStore();
|
|
69
63
|
this.middlewares = new MiddlewareStore();
|
|
70
|
-
this.mediaParsers = new MediaParserStore();
|
|
71
64
|
this.auth = Auth.create(auth);
|
|
72
65
|
this.server.on("error", this.emit.bind(this, "error" /* Error */));
|
|
73
66
|
this.server.on("request", this.emit.bind(this, "request" /* Request */));
|
|
@@ -107,7 +100,7 @@ var _Server = class _Server extends AsyncEventEmitter {
|
|
|
107
100
|
}
|
|
108
101
|
disconnect() {
|
|
109
102
|
return new Promise((resolve, reject) => {
|
|
110
|
-
this.server.close((error) => error ?
|
|
103
|
+
this.server.close((error) => error ? reject(error) : resolve());
|
|
111
104
|
});
|
|
112
105
|
}
|
|
113
106
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../src/lib/structures/http/Server.ts"],"names":["ServerEvent","httpCreateServer","error"],"mappings":";;;;;;;;;;;AAcY,IAAA,WAAA,qBAAAA,YAAL,KAAA;AACN,EAAAA,aAAA,OAAQ,CAAA,GAAA,OAAA,CAAA;AACR,EAAAA,aAAA,SAAU,CAAA,GAAA,SAAA,CAAA;AACV,EAAAA,aAAA,sBAAuB,CAAA,GAAA,sBAAA,CAAA;AACvB,EAAAA,aAAA,8BAA+B,CAAA,GAAA,8BAAA,CAAA;AAC/B,EAAAA,aAAA,aAAc,CAAA,GAAA,aAAA,CAAA;AACd,EAAAA,aAAA,YAAa,CAAA,GAAA,YA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;AAHS,MAAA,MAAA,CAAA,KAAA,EAAA,OAAA,CAAA,CAAA;AAKT,MAAA,SAAS,OAAU,GAAA;AAClB,QAAO,MAAA,CAAA,GAAA,CAAI,aAAa,SAAS,CAAA,CAAA;AACjC,QAAO,MAAA,CAAA,GAAA,CAAI,SAAS,KAAK,CAAA,CAAA;AACzB,QAAO,MAAA,CAAA,GAAA,CAAI,SAAS,KAAK,CAAA,CAAA;AAAA,OAC1B;AAJS,MAAA,MAAA,CAAA,OAAA,EAAA,SAAA,CAAA,CAAA;AAMT,MAAO,MAAA,CAAA,EAAA,CAAG,aAAa,SAAS,CAAA,CAAA;AAChC,MAAO,MAAA,CAAA,EAAA,CAAG,SAAS,KAAK,CAAA,CAAA;AACxB,MAAO,MAAA,CAAA,EAAA,CAAG,SAAS,KAAK,CAAA,CAAA;AAAA,KACxB,CAAA,CAAA;AAAA,GACF;AAAA,EAEO,UAAa,GAAA;AACnB,IAAA,OAAO,IAAI,OAAA,CAAc,CAAC,OAAA,EAAS,MAAW,KAAA;AAC7C,MAAK,IAAA,CAAA,MAAA,CAAO,MAAM,CAAC,KAAA,KAAW,QAAQ,OAAQ,EAAA,GAAI,MAAO,CAAA,KAAK,CAAE,CAAA,CAAA;AAAA,KAChE,CAAA,CAAA;AAAA,GACF;AACD,CAAA,CAAA;AApG4D,MAAA,CAAA,OAAA,EAAA,QAAA,CAAA,CAAA;AAArD,IAAM,MAAN,GAAA","file":"Server.mjs","sourcesContent":["import { container } from '@sapphire/pieces';\nimport { AsyncEventEmitter } from '@vladfrangu/async_event_emitter';\nimport { Server as HttpServer, createServer as httpCreateServer, type ServerOptions as HttpOptions } from 'node:http';\nimport type { ListenOptions } from 'node:net';\nimport type { MimeType } from '../../utils/MimeType';\nimport { MediaParserStore } from '../MediaParserStore';\nimport { MiddlewareStore } from '../MiddlewareStore';\nimport type { Route } from '../Route';\nimport { RouteStore } from '../RouteStore';\nimport { ApiRequest } from '../api/ApiRequest';\nimport { ApiResponse } from '../api/ApiResponse';\nimport type { RouterBranch } from '../router/RouterBranch';\nimport { Auth, type ServerOptionsAuth } from './Auth';\n\nexport enum ServerEvent {\n\tError = 'error',\n\tRequest = 'request',\n\tRouterBranchNotFound = 'routerBranchNotFound',\n\tRouterBranchMethodNotAllowed = 'routerBranchMethodNotAllowed',\n\tRouterFound = 'routerFound',\n\tRouteError = 'routeError',\n\tMiddlewareFailure = 'middlewareFailure',\n\tMiddlewareError = 'middlewareError',\n\tMiddlewareSuccess = 'middlewareSuccess'\n}\n\nexport interface ServerEvents {\n\t[ServerEvent.Error]: [error: Error, request: ApiRequest, response: ApiResponse];\n\t[ServerEvent.Request]: [request: ApiRequest, response: ApiResponse];\n\t[ServerEvent.RouterBranchNotFound]: [request: ApiRequest, response: ApiResponse];\n\t[ServerEvent.RouterBranchMethodNotAllowed]: [request: ApiRequest, response: ApiResponse, node: RouterBranch];\n\t[ServerEvent.RouterFound]: [request: ApiRequest, response: ApiResponse];\n\t[ServerEvent.RouteError]: [error: Error, request: ApiRequest, response: ApiResponse];\n\t[ServerEvent.MiddlewareFailure]: [request: ApiRequest, response: ApiResponse];\n\t[ServerEvent.MiddlewareSuccess]: [request: Route.Request, response: Route.Response, route: Route];\n\t[ServerEvent.MiddlewareError]: [error: Error, request: ApiRequest, response: ApiResponse];\n}\n\n/**\n * @since 1.0.0\n */\nexport class Server extends AsyncEventEmitter<ServerEvents> {\n\t/**\n\t * The routes this server holds.\n\t * @since 1.0.0\n\t */\n\tpublic readonly routes: RouteStore;\n\n\t/**\n\t * The middlewares this server holds.\n\t * @since 1.0.0\n\t */\n\tpublic readonly middlewares: MiddlewareStore;\n\n\t/**\n\t * The media parsers this server holds.\n\t * @since 1.3.0\n\t */\n\tpublic readonly mediaParsers: MediaParserStore;\n\n\t/**\n\t * The authentication system.\n\t * @since 1.0.0\n\t */\n\tpublic readonly auth: Auth | null;\n\n\t/**\n\t * The http.Server instance that manages the recieved HTTP requests.\n\t * @since 1.0.0\n\t */\n\tpublic readonly server: HttpServer;\n\n\t/**\n\t * The options for this server.\n\t * @since 1.0.0\n\t */\n\tpublic readonly options: AuthLessServerOptions;\n\n\t/**\n\t * @since 1.0.0\n\t * @param options The options for this server\n\t */\n\tpublic constructor({ auth, ...options }: ServerOptions = {}) {\n\t\tsuper();\n\n\t\tcontainer.server = this;\n\n\t\tthis.options = options;\n\t\tthis.server = httpCreateServer({\n\t\t\tIncomingMessage: ApiRequest,\n\t\t\tServerResponse: ApiResponse,\n\t\t\t...(options.server ?? {})\n\t\t});\n\t\tthis.routes = new RouteStore();\n\t\tthis.middlewares = new MiddlewareStore();\n\t\tthis.mediaParsers = new MediaParserStore();\n\t\tthis.auth = Auth.create(auth);\n\t\tthis.server.on('error', this.emit.bind(this, ServerEvent.Error));\n\t\tthis.server.on('request', this.emit.bind(this, ServerEvent.Request));\n\t}\n\n\tpublic connect() {\n\t\tconst { server } = this;\n\t\tserver.listen({\n\t\t\tport: 4000,\n\t\t\t...(this.options.listenOptions ?? {})\n\t\t});\n\n\t\treturn new Promise<void>((resolve, reject) => {\n\t\t\tfunction listening() {\n\t\t\t\tcleanup();\n\t\t\t\tresolve();\n\t\t\t}\n\n\t\t\tfunction error(error: Error) {\n\t\t\t\tcleanup();\n\t\t\t\treject(error);\n\t\t\t}\n\n\t\t\tfunction close() {\n\t\t\t\tcleanup();\n\t\t\t\treject(new Error('Closed unexpectedly.'));\n\t\t\t}\n\n\t\t\tfunction cleanup() {\n\t\t\t\tserver.off('listening', listening);\n\t\t\t\tserver.off('error', error);\n\t\t\t\tserver.off('close', close);\n\t\t\t}\n\n\t\t\tserver.on('listening', listening);\n\t\t\tserver.on('error', error);\n\t\t\tserver.on('close', close);\n\t\t});\n\t}\n\n\tpublic disconnect() {\n\t\treturn new Promise<void>((resolve, reject) => {\n\t\t\tthis.server.close((error) => (error ? resolve() : reject(error)));\n\t\t});\n\t}\n}\n\n/**\n * RFC 1341 4: Defines a Content-Type's type, which follows the following structure:\n *\n * - `type` = `text` | `multipart` | `message` | `image` | `audio` | `video` | `application` | x-token\n * - `x-token` = The two characters \"X-\" followed, with no intervening white space, by any token\n * @since 1.3.0\n */\nexport type ContentTypeType = 'text' | 'multipart' | 'message' | 'image' | 'audio' | 'video' | 'application' | `X-${string}`;\n\n/**\n * RFC 1341 4: Defines a Content-Type's parameter, which follows the following structure:\n *\n * - `parameter` = `attribute` \"=\" `value`\n * - `attribute` = `token`\n * - `value` = `token` / `quoted-string`\n * - `token` = `1*<any CHAR except \"SPACE\", \"CTLs\", or \"tspecials\">`\n * - `tspecials` = `(` | `)` | `<` | `>` | `@` | `,` | `;` | `:` | `\\` | `\"` | `/` | `[` | `]` | `?` | `.` | `=`\n *\n * @note `tspecials` must be in quoted-string, to use within parameter values.\n * @note The definition of `tspecials` is the same as the RFC 822 definition of `specials` with the addition of the\n * three characters `/`, `?`, and `=`.\n * @since 1.3.0\n */\nexport type ContentTypeParameter = `; ${string}=${string}`;\n\n/**\n * RFC 1341 4: Defines the syntax for a Content-Type field without parameters, which follows the following structure:\n * `type \"/\" subtype`.\n * @since 7.0.0\n */\nexport type GenericMimeType = `${ContentTypeType}/${string}`;\n\n/**\n * RFC 1341 4: Defines the syntax for a Content-Type field, which follows the following structure:\n * `type \"/\" subtype *[\";\" parameter]`.\n * @since 7.0.0\n */\nexport type GenericParametrizedMimeType = `${GenericMimeType}${'' | ContentTypeParameter}`;\n\n/**\n * The API options.\n * @since 1.0.0\n */\nexport interface ServerOptions {\n\t/**\n\t * The prefix for all routes, e.g. `v1/`.\n\t * @since 1.0.0\n\t * @default ''\n\t */\n\tprefix?: string;\n\n\t/**\n\t * The origin header to be set on every request at 'Access-Control-Allow-Origin'.\n\t * @since 1.0.0\n\t * @default '*'\n\t */\n\torigin?: string;\n\n\t/**\n\t * (RFC 7230 3.3.2) The maximum decimal number of octets.\n\t * @since 1.0.0\n\t * @default 1024 * 1024 * 50\n\t */\n\tmaximumBodyLength?: number;\n\n\t/**\n\t * The accepted content types for this route. If set to null, the route will accept any data.\n\t * @since 1.3.0\n\t * @default null\n\t */\n\tacceptedContentMimeTypes?: MimeType[] | null;\n\n\t/**\n\t * The HTTP server options.\n\t * @since 1.0.0\n\t * @default {}\n\t */\n\tserver?: HttpOptions;\n\n\t/**\n\t * The HTTP listen options.\n\t * @since 1.0.0\n\t * @default { port: 4000 }\n\t */\n\tlistenOptions?: ListenOptions;\n\n\t/**\n\t * The auth options. If neither `auth` nor `auth.secret` are defined, auth-related routes and middlewares will be\n\t * automatically disabled.\n\t * @since 1.0.0\n\t * @default {}\n\t */\n\tauth?: ServerOptionsAuth;\n\n\t/**\n\t * Whether the server should connect upon being when the plugin is loaded.\n\t * @since 3.2.0\n\t * @default true\n\t */\n\tautomaticallyConnect?: boolean;\n}\n\n/**\n * The {@link ServerOptions} without {@link ServerOptions.auth}.\n * @since 1.0.0\n */\nexport type AuthLessServerOptions = Omit<ServerOptions, 'auth'>;\n"]}
|
|
1
|
+
{"version":3,"sources":["../../../../../src/lib/structures/http/Server.ts"],"names":["ServerEvent","httpCreateServer","error"],"mappings":";;;;;;;;;;AAYY,IAAA,WAAA,qBAAAA,YAAL,KAAA;AACN,EAAAA,aAAA,OAAQ,CAAA,GAAA,OAAA,CAAA;AACR,EAAAA,aAAA,SAAU,CAAA,GAAA,SAAA,CAAA;AACV,EAAAA,aAAA,sBAAuB,CAAA,GAAA,sBAAA,CAAA;AACvB,EAAAA,aAAA,8BAA+B,CAAA,GAAA,8BAAA,CAAA;AAC/B,EAAAA,aAAA,aAAc,CAAA,GAAA,aAAA,CAAA;AACd,EAAAA,aAAA,YAAa,CAAA,GAAA,YAAA,CAAA;AACb,EAAAA,aAAA,mBAAoB,CAAA,GAAA,mBAAA,CAAA;AACpB,EAAAA,aAAA,iBAAkB,CAAA,GAAA,iBAAA,CAAA;AAClB,EAAAA,aAAA,mBAAoB,CAAA,GAAA,mBAAA,CAAA;AATT,EAAAA,OAAAA,YAAAA,CAAAA;AAAA,CAAA,EAAA,WAAA,IAAA,EAAA,EAAA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AA,CAAA;AAChC,MAAO,MAAA,CAAA,EAAA,CAAG,SAAS,KAAK,CAAA,CAAA;AACxB,MAAO,MAAA,CAAA,EAAA,CAAG,SAAS,KAAK,CAAA,CAAA;AAAA,KACxB,CAAA,CAAA;AAAA,GACF;AAAA,EAEO,UAAa,GAAA;AACnB,IAAA,OAAO,IAAI,OAAA,CAAc,CAAC,OAAA,EAAS,MAAW,KAAA;AAC7C,MAAK,IAAA,CAAA,MAAA,CAAO,MAAM,CAAC,KAAA,KAAW,QAAQ,MAAO,CAAA,KAAK,CAAI,GAAA,OAAA,EAAU,CAAA,CAAA;AAAA,KAChE,CAAA,CAAA;AAAA,GACF;AACD,CAAA,CAAA;AA7F4D,MAAA,CAAA,OAAA,EAAA,QAAA,CAAA,CAAA;AAArD,IAAM,MAAN,GAAA","file":"Server.mjs","sourcesContent":["import { container } from '@sapphire/pieces';\nimport { AsyncEventEmitter } from '@vladfrangu/async_event_emitter';\nimport { Server as HttpServer, createServer as httpCreateServer, type ServerOptions as HttpOptions } from 'node:http';\nimport type { ListenOptions } from 'node:net';\nimport { MiddlewareStore } from '../MiddlewareStore';\nimport type { Route } from '../Route';\nimport { RouteStore } from '../RouteStore';\nimport { ApiRequest } from '../api/ApiRequest';\nimport { ApiResponse } from '../api/ApiResponse';\nimport type { RouterBranch } from '../router/RouterBranch';\nimport { Auth, type ServerOptionsAuth } from './Auth';\n\nexport enum ServerEvent {\n\tError = 'error',\n\tRequest = 'request',\n\tRouterBranchNotFound = 'routerBranchNotFound',\n\tRouterBranchMethodNotAllowed = 'routerBranchMethodNotAllowed',\n\tRouterFound = 'routerFound',\n\tRouteError = 'routeError',\n\tMiddlewareFailure = 'middlewareFailure',\n\tMiddlewareError = 'middlewareError',\n\tMiddlewareSuccess = 'middlewareSuccess'\n}\n\nexport interface ServerEvents {\n\t[ServerEvent.Error]: [error: Error, request: ApiRequest, response: ApiResponse];\n\t[ServerEvent.Request]: [request: ApiRequest, response: ApiResponse];\n\t[ServerEvent.RouterBranchNotFound]: [request: ApiRequest, response: ApiResponse];\n\t[ServerEvent.RouterBranchMethodNotAllowed]: [request: ApiRequest, response: ApiResponse, node: RouterBranch];\n\t[ServerEvent.RouterFound]: [request: ApiRequest, response: ApiResponse];\n\t[ServerEvent.RouteError]: [error: Error, request: ApiRequest, response: ApiResponse];\n\t[ServerEvent.MiddlewareFailure]: [request: ApiRequest, response: ApiResponse];\n\t[ServerEvent.MiddlewareSuccess]: [request: Route.Request, response: Route.Response, route: Route];\n\t[ServerEvent.MiddlewareError]: [error: Error, request: ApiRequest, response: ApiResponse];\n}\n\n/**\n * @since 1.0.0\n */\nexport class Server extends AsyncEventEmitter<ServerEvents> {\n\t/**\n\t * The routes this server holds.\n\t * @since 1.0.0\n\t */\n\tpublic readonly routes: RouteStore;\n\n\t/**\n\t * The middlewares this server holds.\n\t * @since 1.0.0\n\t */\n\tpublic readonly middlewares: MiddlewareStore;\n\n\t/**\n\t * The authentication system.\n\t * @since 1.0.0\n\t */\n\tpublic readonly auth: Auth | null;\n\n\t/**\n\t * The http.Server instance that manages the recieved HTTP requests.\n\t * @since 1.0.0\n\t */\n\tpublic readonly server: HttpServer;\n\n\t/**\n\t * The options for this server.\n\t * @since 1.0.0\n\t */\n\tpublic readonly options: AuthLessServerOptions;\n\n\t/**\n\t * @since 1.0.0\n\t * @param options The options for this server\n\t */\n\tpublic constructor({ auth, ...options }: ServerOptions = {}) {\n\t\tsuper();\n\n\t\tcontainer.server = this;\n\n\t\tthis.options = options;\n\t\tthis.server = httpCreateServer({\n\t\t\tIncomingMessage: ApiRequest,\n\t\t\tServerResponse: ApiResponse,\n\t\t\t...(options.server ?? {})\n\t\t});\n\t\tthis.routes = new RouteStore();\n\t\tthis.middlewares = new MiddlewareStore();\n\t\tthis.auth = Auth.create(auth);\n\t\tthis.server.on('error', this.emit.bind(this, ServerEvent.Error));\n\t\tthis.server.on('request', this.emit.bind(this, ServerEvent.Request));\n\t}\n\n\tpublic connect() {\n\t\tconst { server } = this;\n\t\tserver.listen({\n\t\t\tport: 4000,\n\t\t\t...(this.options.listenOptions ?? {})\n\t\t});\n\n\t\treturn new Promise<void>((resolve, reject) => {\n\t\t\tfunction listening() {\n\t\t\t\tcleanup();\n\t\t\t\tresolve();\n\t\t\t}\n\n\t\t\tfunction error(error: Error) {\n\t\t\t\tcleanup();\n\t\t\t\treject(error);\n\t\t\t}\n\n\t\t\tfunction close() {\n\t\t\t\tcleanup();\n\t\t\t\treject(new Error('Closed unexpectedly.'));\n\t\t\t}\n\n\t\t\tfunction cleanup() {\n\t\t\t\tserver.off('listening', listening);\n\t\t\t\tserver.off('error', error);\n\t\t\t\tserver.off('close', close);\n\t\t\t}\n\n\t\t\tserver.on('listening', listening);\n\t\t\tserver.on('error', error);\n\t\t\tserver.on('close', close);\n\t\t});\n\t}\n\n\tpublic disconnect() {\n\t\treturn new Promise<void>((resolve, reject) => {\n\t\t\tthis.server.close((error) => (error ? reject(error) : resolve()));\n\t\t});\n\t}\n}\n\n/**\n * RFC 1341 4: Defines a Content-Type's type, which follows the following structure:\n *\n * - `type` = `text` | `multipart` | `message` | `image` | `audio` | `video` | `application` | x-token\n * - `x-token` = The two characters \"X-\" followed, with no intervening white space, by any token\n * @since 1.3.0\n */\nexport type ContentTypeType = 'text' | 'multipart' | 'message' | 'image' | 'audio' | 'video' | 'application' | `X-${string}`;\n\n/**\n * RFC 1341 4: Defines a Content-Type's parameter, which follows the following structure:\n *\n * - `parameter` = `attribute` \"=\" `value`\n * - `attribute` = `token`\n * - `value` = `token` / `quoted-string`\n * - `token` = `1*<any CHAR except \"SPACE\", \"CTLs\", or \"tspecials\">`\n * - `tspecials` = `(` | `)` | `<` | `>` | `@` | `,` | `;` | `:` | `\\` | `\"` | `/` | `[` | `]` | `?` | `.` | `=`\n *\n * @note `tspecials` must be in quoted-string, to use within parameter values.\n * @note The definition of `tspecials` is the same as the RFC 822 definition of `specials` with the addition of the\n * three characters `/`, `?`, and `=`.\n * @since 1.3.0\n */\nexport type ContentTypeParameter = `; ${string}=${string}`;\n\n/**\n * RFC 1341 4: Defines the syntax for a Content-Type field without parameters, which follows the following structure:\n * `type \"/\" subtype`.\n * @since 7.0.0\n */\nexport type GenericMimeType = `${ContentTypeType}/${string}`;\n\n/**\n * RFC 1341 4: Defines the syntax for a Content-Type field, which follows the following structure:\n * `type \"/\" subtype *[\";\" parameter]`.\n * @since 7.0.0\n */\nexport type GenericParametrizedMimeType = `${GenericMimeType}${'' | ContentTypeParameter}`;\n\n/**\n * The API options.\n * @since 1.0.0\n */\nexport interface ServerOptions {\n\t/**\n\t * The prefix for all routes, e.g. `v1/`.\n\t * @since 1.0.0\n\t * @default ''\n\t */\n\tprefix?: string;\n\n\t/**\n\t * The origin header to be set on every request at 'Access-Control-Allow-Origin'.\n\t * @since 1.0.0\n\t * @default '*'\n\t */\n\torigin?: string;\n\n\t/**\n\t * (RFC 7230 3.3.2) The maximum decimal number of octets.\n\t * @since 1.0.0\n\t * @default 1024 * 1024 * 50\n\t */\n\tmaximumBodyLength?: number;\n\n\t/**\n\t * The HTTP server options.\n\t * @since 1.0.0\n\t * @default {}\n\t */\n\tserver?: HttpOptions;\n\n\t/**\n\t * The HTTP listen options.\n\t * @since 1.0.0\n\t * @default { port: 4000 }\n\t */\n\tlistenOptions?: ListenOptions;\n\n\t/**\n\t * The auth options. If neither `auth` nor `auth.secret` are defined, auth-related routes and middlewares will be\n\t * automatically disabled.\n\t * @since 1.0.0\n\t * @default {}\n\t */\n\tauth?: ServerOptionsAuth;\n\n\t/**\n\t * Whether the server should connect upon being when the plugin is loaded.\n\t * @since 3.2.0\n\t * @default true\n\t */\n\tautomaticallyConnect?: boolean;\n}\n\n/**\n * The {@link ServerOptions} without {@link ServerOptions.auth}.\n * @since 1.0.0\n */\nexport type AuthLessServerOptions = Omit<ServerOptions, 'auth'>;\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../src/lib/structures/router/RouterRoot.ts"],"names":[],"mappings":";;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CAAA,EAAU,OAAA,IAAA,CAAA;AAE9B,IAAM,MAAA,SAAA,GAAY,KAAK,MAAS,GAAA,CAAA,CAAA;AAChC,IAAM,MAAA,IAAA,GAAO,KAAK,SAAS,CAAA,CAAA;AAC3B,IAAM,MAAA,WAAA,GAAc,IAAK,CAAA,WAAA,CAAY,GAAG,CAAA,CAAA;AACxC,IAAA,IAAI,gBAAgB,CAAM,CAAA,IAAA,WAAA,KAAgB,IAAK,CAAA,MAAA,GAAS,GAAU,OAAA,IAAA,CAAA;AAElE,IAAA,OAAO,IAAK,CAAA,KAAA,CAAM,WAAc,GAAA,CAAC,EAAE,WAAY,EAAA,CAAA;AAAA,GAChD;AACD,CAAA,CAAA;AApF6C,MAAA,CAAA,WAAA,EAAA,YAAA,CAAA,CAAA;AAAtC,IAAM,UAAN,GAAA","file":"RouterRoot.mjs","sourcesContent":["import { isNullishOrEmpty } from '@sapphire/utilities';\nimport type { Route } from '../Route';\nimport type { MethodName } from '../http/HttpMethods';\nimport { RouterBranch } from './RouterBranch';\nimport type { RouterNode } from './RouterNode';\n\nexport class RouterRoot extends RouterBranch {\n\tpublic constructor() {\n\t\tsuper('::ROOT::', false, null);\n\t}\n\n\t/**\n\t * Adds a route to the branch\n\t *\n\t * @param route The route to add\n\t * @returns The node the route was added to\n\t */\n\tpublic add(route: Route): RouterNode {\n\t\treturn this._add(route.path, 0, route);\n\t}\n\n\t/**\n\t * Removes a route from the branch\n\t *\n\t * @param route The route to remove\n\t * @returns Whether or not the route was removed\n\t */\n\tpublic remove(route: Route): boolean {\n\t\treturn this._remove(route.path, 0, route);\n\t}\n\n\t// eslint-disable-next-line @typescript-eslint/class-literal-property-style\n\tpublic override get path(): string {\n\t\treturn '';\n\t}\n\n\tpublic override toString(): string {\n\t\treturn '';\n\t}\n\n\tpublic static makeRoutePathForPiece(directories: readonly string[], name: string): string {\n\t\tconst parts: string[] = [];\n\t\tfor (const directory of directories) {\n\t\t\tconst trimmed = directory.trim();\n\n\t\t\t// If empty, skip:\n\t\t\tif (isNullishOrEmpty(trimmed)) continue;\n\t\t\t// If it's a group, skip:\n\t\t\tif (trimmed.startsWith('(') && trimmed.endsWith(')')) continue;\n\n\t\t\tparts.push(trimmed);\n\t\t}\n\n\t\tparts.push(name.trim());\n\t\treturn parts.join('/');\n\t}\n\n\tpublic static normalize(path: string | null | undefined): string[] {\n\t\tconst parts = [] as string[];\n\t\tif (isNullishOrEmpty(path)) return parts;\n\n\t\tlet part = '';\n\t\tfor (const char of path) {\n\t\t\tif (char === '/') {\n\t\t\t\tif (part.length) {\n\t\t\t\t\tparts.push(part);\n\t\t\t\t\tpart = '';\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tpart += char;\n\t\t\t}\n\t\t}\n\n\t\tif (part.length) {\n\t\t\tparts.push(part);\n\t\t}\n\n\t\treturn parts;\n\t}\n\n\tpublic static extractMethod(path: readonly string[]): MethodName | null {\n\t\tif (path.length === 0) return null;\n\n\t\tconst lastIndex = path.length - 1;\n\t\tconst last = path[lastIndex];\n\t\tconst methodIndex = last.lastIndexOf('.');\n\t\tif (methodIndex === -1 || methodIndex === last.length - 1) return null;\n\n\t\treturn last.slice(methodIndex + 1).toUpperCase() as MethodName;\n\t}\n}\n"]}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { __name, __publicField } from '../../../chunk-S573YWRP.mjs';
|
|
2
|
+
import { isNullishOrEmpty } from '@sapphire/utilities';
|
|
3
|
+
import { splitSetCookieString } from 'cookie-es';
|
|
4
|
+
import { NodeUtilInspectSymbol } from '../constants.mjs';
|
|
5
|
+
|
|
6
|
+
var _RequestHeadersProxy = class _RequestHeadersProxy {
|
|
7
|
+
constructor(request) {
|
|
8
|
+
__publicField(this, "request");
|
|
9
|
+
this.request = request;
|
|
10
|
+
}
|
|
11
|
+
append(name, value) {
|
|
12
|
+
const { headers } = this.request;
|
|
13
|
+
const current = headers[name];
|
|
14
|
+
if (current) {
|
|
15
|
+
if (Array.isArray(current)) {
|
|
16
|
+
current.push(value);
|
|
17
|
+
} else {
|
|
18
|
+
headers[name] = [current, value];
|
|
19
|
+
}
|
|
20
|
+
} else {
|
|
21
|
+
headers[name] = value;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
delete(name) {
|
|
25
|
+
this.request.headers[name] = void 0;
|
|
26
|
+
}
|
|
27
|
+
get(name) {
|
|
28
|
+
return normalizeValue(this.request.headers[name]);
|
|
29
|
+
}
|
|
30
|
+
has(name) {
|
|
31
|
+
return !isNullishOrEmpty(this.request.headers[name]);
|
|
32
|
+
}
|
|
33
|
+
set(name, value) {
|
|
34
|
+
this.request.headers[name] = value;
|
|
35
|
+
}
|
|
36
|
+
getSetCookie() {
|
|
37
|
+
const setCookie = this.get("set-cookie");
|
|
38
|
+
return setCookie === null ? [] : splitSetCookieString(setCookie);
|
|
39
|
+
}
|
|
40
|
+
forEach(callbackfn, thisArg) {
|
|
41
|
+
for (const [key, value] of this.entries()) {
|
|
42
|
+
callbackfn.call(thisArg, value, key, this);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
*keys() {
|
|
46
|
+
const { headers } = this.request;
|
|
47
|
+
for (const key of Object.keys(headers)) {
|
|
48
|
+
const value = headers[key];
|
|
49
|
+
if (!isNullishOrEmpty(value)) {
|
|
50
|
+
yield key;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
*values() {
|
|
55
|
+
const { headers } = this.request;
|
|
56
|
+
for (const key of Object.keys(headers)) {
|
|
57
|
+
const value = headers[key];
|
|
58
|
+
if (!isNullishOrEmpty(value)) {
|
|
59
|
+
yield normalizeValue(value);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
*entries() {
|
|
64
|
+
const { headers } = this.request;
|
|
65
|
+
for (const key of Object.keys(headers)) {
|
|
66
|
+
const value = headers[key];
|
|
67
|
+
if (!isNullishOrEmpty(value)) {
|
|
68
|
+
yield [key, normalizeValue(value)];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
[Symbol.iterator]() {
|
|
73
|
+
return this.entries();
|
|
74
|
+
}
|
|
75
|
+
// eslint-disable-next-line @typescript-eslint/class-literal-property-style
|
|
76
|
+
get [Symbol.toStringTag]() {
|
|
77
|
+
return "Headers";
|
|
78
|
+
}
|
|
79
|
+
[NodeUtilInspectSymbol]() {
|
|
80
|
+
return Object.fromEntries(this.entries());
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
__name(_RequestHeadersProxy, "RequestHeadersProxy");
|
|
84
|
+
var RequestHeadersProxy = _RequestHeadersProxy;
|
|
85
|
+
function normalizeValue(value) {
|
|
86
|
+
if (Array.isArray(value)) {
|
|
87
|
+
return value.join(", ");
|
|
88
|
+
}
|
|
89
|
+
return String(value ?? "");
|
|
90
|
+
}
|
|
91
|
+
__name(normalizeValue, "normalizeValue");
|
|
92
|
+
|
|
93
|
+
export { RequestHeadersProxy };
|
|
94
|
+
//# sourceMappingURL=RequestHeadersProxy.mjs.map
|
|
95
|
+
//# sourceMappingURL=RequestHeadersProxy.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../src/lib/utils/_body/RequestHeadersProxy.ts"],"names":[],"mappings":";;;;;AAMO,IAAM,oBAAA,GAAN,MAAM,oBAAuC,CAAA;AAAA,EAG5C,YAAY,OAAqB,EAAA;AAFxC,IAAiB,aAAA,CAAA,IAAA,EAAA,SAAA,CAAA,CAAA;AAGhB,IAAA,IAAA,CAAK,OAAU,GAAA,OAAA,CAAA;AAAA,GAChB;AAAA,EAEO,MAAA,CAAO,MAAc,KAAqB,EAAA;AAChD,IAAM,MAAA,EAAE,OAAQ,EAAA,GAAI,IAAK,CAAA,OAAA,CAAA;AACzB,IAAM,MAAA,OAAA,GAAU,QAAQ,IAAI,CAAA,CAAA;AAC5B,IAAA,IAAI,OAAS,EAAA;AACZ,MAAI,IAAA,KAAA,CAAM,OAAQ,CAAA,OAA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,EAAA,CAAA;AAAA,GACrB;AAAA;AAAA,EAGA,KAAY,MAAO,CAAA,WAAW,CAAI,GAAA;AACjC,IAAO,OAAA,SAAA,CAAA;AAAA,GACR;AAAA,EAEA,CAAQ,qBAAqB,CAAI,GAAA;AAChC,IAAA,OAAO,MAAO,CAAA,WAAA,CAAY,IAAK,CAAA,OAAA,EAAS,CAAA,CAAA;AAAA,GACzC;AACD,CAAA,CAAA;AA7FoD,MAAA,CAAA,oBAAA,EAAA,qBAAA,CAAA,CAAA;AAA7C,IAAM,mBAAN,GAAA,qBAAA;AA+FP,SAAS,eAAe,KAA8C,EAAA;AACrE,EAAI,IAAA,KAAA,CAAM,OAAQ,CAAA,KAAK,CAAG,EAAA;AACzB,IAAO,OAAA,KAAA,CAAM,KAAK,IAAI,CAAA,CAAA;AAAA,GACvB;AAEA,EAAO,OAAA,MAAA,CAAO,SAAS,EAAE,CAAA,CAAA;AAC1B,CAAA;AANS,MAAA,CAAA,cAAA,EAAA,gBAAA,CAAA","file":"RequestHeadersProxy.mjs","sourcesContent":["import { isNullishOrEmpty } from '@sapphire/utilities';\nimport { splitSetCookieString } from 'cookie-es';\nimport type { Headers, SpecIterableIterator } from 'undici-types';\nimport type { ApiRequest } from '../../structures/api/ApiRequest';\nimport { NodeUtilInspectSymbol } from '../constants';\n\nexport class RequestHeadersProxy implements Headers {\n\tprivate readonly request: ApiRequest;\n\n\tpublic constructor(request: ApiRequest) {\n\t\tthis.request = request;\n\t}\n\n\tpublic append(name: string, value: string): void {\n\t\tconst { headers } = this.request;\n\t\tconst current = headers[name];\n\t\tif (current) {\n\t\t\tif (Array.isArray(current)) {\n\t\t\t\tcurrent.push(value);\n\t\t\t} else {\n\t\t\t\theaders[name] = [current, value];\n\t\t\t}\n\t\t} else {\n\t\t\theaders[name] = value;\n\t\t}\n\t}\n\n\tpublic delete(name: string): void {\n\t\tthis.request.headers[name] = undefined;\n\t}\n\n\tpublic get(name: string): string | null {\n\t\treturn normalizeValue(this.request.headers[name]);\n\t}\n\n\tpublic has(name: string): boolean {\n\t\treturn !isNullishOrEmpty(this.request.headers[name]);\n\t}\n\n\tpublic set(name: string, value: string): void {\n\t\tthis.request.headers[name] = value;\n\t}\n\n\tpublic getSetCookie(): string[] {\n\t\tconst setCookie = this.get('set-cookie');\n\t\treturn setCookie === null ? [] : splitSetCookieString(setCookie);\n\t}\n\n\tpublic forEach(callbackfn: (value: string, key: string, iterable: Headers) => void, thisArg?: unknown): void {\n\t\tfor (const [key, value] of this.entries()) {\n\t\t\tcallbackfn.call(thisArg, value, key, this);\n\t\t}\n\t}\n\n\tpublic *keys(): SpecIterableIterator<string> {\n\t\tconst { headers } = this.request;\n\t\tfor (const key of Object.keys(headers)) {\n\t\t\tconst value = headers[key];\n\n\t\t\tif (!isNullishOrEmpty(value)) {\n\t\t\t\tyield key;\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic *values(): SpecIterableIterator<string> {\n\t\tconst { headers } = this.request;\n\t\tfor (const key of Object.keys(headers)) {\n\t\t\tconst value = headers[key];\n\n\t\t\tif (!isNullishOrEmpty(value)) {\n\t\t\t\tyield normalizeValue(value);\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic *entries(): SpecIterableIterator<[string, string]> {\n\t\tconst { headers } = this.request;\n\t\tfor (const key of Object.keys(headers)) {\n\t\t\tconst value = headers[key];\n\n\t\t\tif (!isNullishOrEmpty(value)) {\n\t\t\t\tyield [key, normalizeValue(value)];\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic [Symbol.iterator](): SpecIterableIterator<[string, string]> {\n\t\treturn this.entries();\n\t}\n\n\t// eslint-disable-next-line @typescript-eslint/class-literal-property-style\n\tpublic get [Symbol.toStringTag]() {\n\t\treturn 'Headers';\n\t}\n\n\tpublic [NodeUtilInspectSymbol]() {\n\t\treturn Object.fromEntries(this.entries());\n\t}\n}\n\nfunction normalizeValue(value: string | string[] | undefined): string {\n\tif (Array.isArray(value)) {\n\t\treturn value.join(', ');\n\t}\n\n\treturn String(value ?? '');\n}\n"]}
|