@wolfstar/plugin-api 0.0.0-next-20260816113531
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/LICENSE +202 -0
- package/README.md +114 -0
- package/dist/esm/index-D-l34LxR.d.ts +314 -0
- package/dist/esm/index-D-l34LxR.d.ts.map +1 -0
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/register.d.ts +20 -0
- package/dist/esm/register.d.ts.map +1 -0
- package/dist/esm/register.js +32 -0
- package/dist/esm/register.js.map +1 -0
- package/dist/esm/src-CscMIChC.js +664 -0
- package/dist/esm/src-CscMIChC.js.map +1 -0
- package/package.json +66 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# @wolfstar/plugin-api
|
|
2
|
+
|
|
3
|
+
A plugin for [`@wolfstar/http-framework`](https://www.npmjs.com/package/@wolfstar/http-framework),
|
|
4
|
+
ported from [`@sapphire/plugin-api`](https://github.com/sapphiredev/plugins/tree/main/packages/api).
|
|
5
|
+
It exposes a **standalone** REST API server — for health checks, dashboards, or webhooks from other
|
|
6
|
+
services — built on the same `@sapphire/pieces` `Route`/`Middleware` conventions.
|
|
7
|
+
|
|
8
|
+
It is intentionally independent from `Client#server` (the interactions webhook): that server already
|
|
9
|
+
claims every path on its port and 404s anything that doesn't match, so this plugin binds its own
|
|
10
|
+
`ApiServer` on a separate port instead of trying to share the same listener.
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pnpm add @wolfstar/plugin-api
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
Import the side-effecting `register` entrypoint **before** you create your `Client`:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import "@wolfstar/plugin-api/register";
|
|
24
|
+
import { Client } from "@wolfstar/http-framework";
|
|
25
|
+
|
|
26
|
+
const client = new Client({
|
|
27
|
+
api: {
|
|
28
|
+
listenOptions: { port: 4000 },
|
|
29
|
+
origin: "*",
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
await client.load();
|
|
34
|
+
await client.listen({ port: 8080 }); // interactions webhook; the API server starts right after
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Writing a route
|
|
38
|
+
|
|
39
|
+
Routes are `Route` pieces loaded from a `routes` directory, exactly like `commands`/`listeners`:
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
// src/routes/health.get.ts
|
|
43
|
+
import { HttpCodes } from "@wolfstar/http-framework";
|
|
44
|
+
import { Route } from "@wolfstar/plugin-api";
|
|
45
|
+
|
|
46
|
+
export class HealthRoute extends Route {
|
|
47
|
+
public run(_request: Route.Request, response: Route.Response) {
|
|
48
|
+
response.json({ status: "ok" }, HttpCodes.OK);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
The route's path and method are inferred from its location: `src/routes/health.get.ts` registers
|
|
54
|
+
`GET /health`. A folder named `[id]` becomes a dynamic segment (`request.params.id`), and a
|
|
55
|
+
`(group)`-style folder is skipped when building the path. Both can be overridden explicitly:
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
export class HealthRoute extends Route {
|
|
59
|
+
public constructor(context: Route.LoaderContext) {
|
|
60
|
+
super(context, { route: "/status", methods: ["GET", "HEAD"] });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public run(request: Route.Request, response: Route.Response) {
|
|
64
|
+
response.json({ status: "ok", method: request.method });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Writing a middleware
|
|
70
|
+
|
|
71
|
+
Middlewares are `Middleware` pieces loaded from a `middlewares` directory, run in ascending
|
|
72
|
+
`position` order before route dispatch; a middleware stops the chain by ending the response:
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
// src/middlewares/requestId.ts
|
|
76
|
+
import { Middleware } from "@wolfstar/plugin-api";
|
|
77
|
+
import { randomUUID } from "node:crypto";
|
|
78
|
+
|
|
79
|
+
export class RequestIdMiddleware extends Middleware {
|
|
80
|
+
public constructor(context: Middleware.LoaderContext) {
|
|
81
|
+
super(context, { position: 15 });
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public run(request: Middleware.Request, response: Middleware.Response) {
|
|
85
|
+
response.setHeader("X-Request-Id", randomUUID());
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Built-in middlewares: `headers` (position 10, sets CORS headers and short-circuits `OPTIONS`
|
|
91
|
+
pre-flight requests) and `body` (position 20, rejects requests whose `Content-Length` exceeds
|
|
92
|
+
`maximumBodyLength`).
|
|
93
|
+
|
|
94
|
+
## `ApiServerOptions`
|
|
95
|
+
|
|
96
|
+
| Option | Default | Description |
|
|
97
|
+
| ---------------------- | ------------------ | -------------------------------------------------------- |
|
|
98
|
+
| `prefix` | `undefined` | Path segment prefix applied to every route. |
|
|
99
|
+
| `origin` | `'*'` | `Access-Control-Allow-Origin` header value. |
|
|
100
|
+
| `maximumBodyLength` | `1024 * 1024 * 50` | Maximum accepted `Content-Length`, in bytes. |
|
|
101
|
+
| `server` | `undefined` | Raw options forwarded to `node:http`'s `createServer`. |
|
|
102
|
+
| `listenOptions` | `{ port: 4000 }` | Raw options forwarded to `server.listen()`. |
|
|
103
|
+
| `automaticallyConnect` | `true` | Whether to start listening during the `postListen` hook. |
|
|
104
|
+
|
|
105
|
+
## Scope
|
|
106
|
+
|
|
107
|
+
Ported faithfully: the `Route`/`RouteStore` piece conventions, the filesystem-routing/dynamic-segment
|
|
108
|
+
trie router (`RouterRoot`/`RouterBranch`/`RouterNode`), the position-ordered `Middleware`/`MiddlewareStore`
|
|
109
|
+
chain, and the listener-driven request dispatch pipeline (`request` → middlewares → `routerFound` /
|
|
110
|
+
`routerBranchNotFound` / `routerBranchMethodNotAllowed` → route `run`).
|
|
111
|
+
|
|
112
|
+
**Not ported** (out of scope for this webhook-only framework, kept for a future release): cookies,
|
|
113
|
+
OAuth2/session auth, and the built-in `oauth/callback`, `oauth/logout` routes from the original
|
|
114
|
+
`@sapphire/plugin-api`. Add your own auth middleware/routes as needed.
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import { IncomingMessage, Server, ServerOptions, ServerResponse } from "node:http";
|
|
2
|
+
import { AsyncEventEmitter } from "@vladfrangu/async_event_emitter";
|
|
3
|
+
import { Piece, PieceOptions, Store } from "@sapphire/pieces";
|
|
4
|
+
import { ListenOptions } from "node:net";
|
|
5
|
+
//#region src/lib/http/ApiResponse.d.ts
|
|
6
|
+
declare class ApiResponse extends ServerResponse<ApiRequest> {
|
|
7
|
+
status(statusCode: number): this;
|
|
8
|
+
json(body: unknown, statusCode?: number): this;
|
|
9
|
+
text(body: string, statusCode?: number): this;
|
|
10
|
+
noContent(): this;
|
|
11
|
+
notFound(body?: unknown): this;
|
|
12
|
+
methodNotAllowed(body?: unknown): this;
|
|
13
|
+
badRequest(body?: unknown): this;
|
|
14
|
+
error(statusCode?: number, body?: unknown): this;
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region src/lib/http/HttpMethod.d.ts
|
|
18
|
+
/**
|
|
19
|
+
* The HTTP methods that a {@link Route} can be registered for.
|
|
20
|
+
*/
|
|
21
|
+
declare const HttpMethods: readonly ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"];
|
|
22
|
+
type HttpMethod = (typeof HttpMethods)[number];
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region src/lib/structures/Route.d.ts
|
|
25
|
+
type Awaitable<T> = T | PromiseLike<T>;
|
|
26
|
+
/**
|
|
27
|
+
* A single HTTP endpoint. The route's path and methods are either given explicitly via
|
|
28
|
+
* {@link Route.Options.route}/{@link Route.Options.methods}, or inferred from the piece's file
|
|
29
|
+
* system location: directories become path segments (`(group)`-style directories are skipped,
|
|
30
|
+
* `index` collapses into its parent), `[param]` segments become dynamic, and a `.<method>`
|
|
31
|
+
* filename suffix (e.g. `hello.post.ts`) implies that HTTP method.
|
|
32
|
+
*/
|
|
33
|
+
declare abstract class Route<Options extends Route.Options = Route.Options> extends Piece<Options, "routes"> {
|
|
34
|
+
/**
|
|
35
|
+
* The normalized path segments this route is registered under, e.g. `['users', '[id]']`.
|
|
36
|
+
*/
|
|
37
|
+
readonly path: readonly string[];
|
|
38
|
+
/**
|
|
39
|
+
* The HTTP methods this route responds to.
|
|
40
|
+
*/
|
|
41
|
+
readonly methods: ReadonlySet<HttpMethod>;
|
|
42
|
+
constructor(context: Route.LoaderContext, options: Options);
|
|
43
|
+
/**
|
|
44
|
+
* Handles every method this route was registered for. Branch on `request.method` when a route
|
|
45
|
+
* needs to support multiple methods.
|
|
46
|
+
*/
|
|
47
|
+
abstract run(request: ApiRequest, response: ApiResponse): Awaitable<unknown>;
|
|
48
|
+
}
|
|
49
|
+
declare namespace Route {
|
|
50
|
+
type LoaderContext = Piece.LoaderContext<"routes">;
|
|
51
|
+
interface Options extends PieceOptions {
|
|
52
|
+
/**
|
|
53
|
+
* An explicit route path, e.g. `/users/[id]`. If omitted, the path is inferred from the
|
|
54
|
+
* piece's directory structure and file name.
|
|
55
|
+
*/
|
|
56
|
+
route?: `/${string}`;
|
|
57
|
+
/**
|
|
58
|
+
* The HTTP methods this route responds to. If omitted, inferred from a `.<method>` filename
|
|
59
|
+
* suffix (e.g. `hello.post.ts`), defaulting to `GET`.
|
|
60
|
+
*/
|
|
61
|
+
methods?: readonly HttpMethod[];
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/lib/structures/router/RouterNode.d.ts
|
|
66
|
+
/**
|
|
67
|
+
* A single path segment's registered routes, keyed by HTTP method.
|
|
68
|
+
*/
|
|
69
|
+
declare class RouterNode extends Map<HttpMethod, Route> {
|
|
70
|
+
readonly branch: RouterBranch;
|
|
71
|
+
constructor(branch: RouterBranch);
|
|
72
|
+
/**
|
|
73
|
+
* Walks up the branch chain collecting the values captured by dynamic (`[param]`) segments.
|
|
74
|
+
* @param parts The path segments of the matched request, in the same order used to reach this node.
|
|
75
|
+
*/
|
|
76
|
+
extractParameters(parts: readonly string[]): Record<string, string>;
|
|
77
|
+
}
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region src/lib/structures/router/RouterBranch.d.ts
|
|
80
|
+
/**
|
|
81
|
+
* A single segment of the route trie. Each branch owns exactly one {@link RouterNode} (its
|
|
82
|
+
* method-to-route map) and may have any number of static children plus at most one dynamic
|
|
83
|
+
* (`[param]`) child, which is only checked after every static child fails to match.
|
|
84
|
+
*/
|
|
85
|
+
declare class RouterBranch {
|
|
86
|
+
readonly name: string;
|
|
87
|
+
readonly dynamic: boolean;
|
|
88
|
+
readonly parent: RouterBranch | null;
|
|
89
|
+
readonly node: RouterNode;
|
|
90
|
+
private readonly staticChildren;
|
|
91
|
+
private dynamicChild;
|
|
92
|
+
constructor(name: string, dynamic: boolean, parent: RouterBranch | null);
|
|
93
|
+
matches(part: string): boolean;
|
|
94
|
+
protected insertAt(parts: readonly string[], index: number, route: Route): RouterNode;
|
|
95
|
+
protected removeAt(parts: readonly string[], index: number, route: Route): boolean;
|
|
96
|
+
protected findAt(parts: readonly string[], index: number): RouterBranch | null;
|
|
97
|
+
}
|
|
98
|
+
//#endregion
|
|
99
|
+
//#region src/lib/http/ApiRequest.d.ts
|
|
100
|
+
declare class ApiRequest extends IncomingMessage {
|
|
101
|
+
/**
|
|
102
|
+
* The parsed query string parameters.
|
|
103
|
+
*/
|
|
104
|
+
query: URLSearchParams;
|
|
105
|
+
/**
|
|
106
|
+
* The extracted dynamic (`[param]`) path segment values.
|
|
107
|
+
*/
|
|
108
|
+
params: Record<string, string>;
|
|
109
|
+
/**
|
|
110
|
+
* The matched router branch for this request's pathname, if any.
|
|
111
|
+
*/
|
|
112
|
+
routerNode: RouterBranch | null;
|
|
113
|
+
/**
|
|
114
|
+
* The matched route for this request's pathname and method, if any.
|
|
115
|
+
*/
|
|
116
|
+
route: Route | null;
|
|
117
|
+
/**
|
|
118
|
+
* Reads the full request body as a UTF-8 string.
|
|
119
|
+
* @param limit The maximum number of bytes to read before rejecting. Defaults to 1 MiB.
|
|
120
|
+
*/
|
|
121
|
+
readBodyText(limit?: number): Promise<string>;
|
|
122
|
+
/**
|
|
123
|
+
* Reads and parses the request body as JSON.
|
|
124
|
+
* @param limit The maximum number of bytes to read before rejecting. Defaults to 1 MiB.
|
|
125
|
+
*/
|
|
126
|
+
readBodyJson<T = unknown>(limit?: number): Promise<T>;
|
|
127
|
+
}
|
|
128
|
+
//#endregion
|
|
129
|
+
//#region src/lib/structures/Middleware.d.ts
|
|
130
|
+
/**
|
|
131
|
+
* A piece that runs on every incoming request, in ascending {@link Middleware.position} order,
|
|
132
|
+
* before route dispatch. A middleware stops the chain implicitly by ending the response
|
|
133
|
+
* (`response.writableEnded`); there is no explicit `next()` callback.
|
|
134
|
+
*/
|
|
135
|
+
declare abstract class Middleware<Options extends Middleware.Options = Middleware.Options> extends Piece<Options, "middlewares"> {
|
|
136
|
+
/**
|
|
137
|
+
* The built-in middlewares use the following positions:
|
|
138
|
+
* - `headers`: 10 (CORS headers, 404/405 short-circuit)
|
|
139
|
+
* - `body`: 20 (`Content-Length` validation)
|
|
140
|
+
*/
|
|
141
|
+
readonly position: number;
|
|
142
|
+
constructor(context: Middleware.LoaderContext, options: Options);
|
|
143
|
+
abstract run(request: ApiRequest, response: ApiResponse): Awaitable<unknown>;
|
|
144
|
+
}
|
|
145
|
+
declare namespace Middleware {
|
|
146
|
+
type LoaderContext = Piece.LoaderContext<"middlewares">;
|
|
147
|
+
interface Options extends PieceOptions {
|
|
148
|
+
/**
|
|
149
|
+
* Middlewares run in ascending order of this value.
|
|
150
|
+
* @default 1000
|
|
151
|
+
*/
|
|
152
|
+
position?: number;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
//#endregion
|
|
156
|
+
//#region src/lib/structures/MiddlewareStore.d.ts
|
|
157
|
+
declare class MiddlewareStore extends Store<Middleware, "middlewares"> {
|
|
158
|
+
/**
|
|
159
|
+
* Enabled middlewares, kept sorted in ascending {@link Middleware.position} order.
|
|
160
|
+
*/
|
|
161
|
+
readonly sortedMiddlewares: Middleware[];
|
|
162
|
+
constructor();
|
|
163
|
+
/**
|
|
164
|
+
* Runs every enabled middleware in order, stopping early once the response has ended.
|
|
165
|
+
*/
|
|
166
|
+
run(request: ApiRequest, response: ApiResponse): Promise<void>;
|
|
167
|
+
set(key: string, value: Middleware): this;
|
|
168
|
+
delete(key: string): boolean;
|
|
169
|
+
clear(): void;
|
|
170
|
+
}
|
|
171
|
+
//#endregion
|
|
172
|
+
//#region src/lib/structures/router/RouterRoot.d.ts
|
|
173
|
+
/**
|
|
174
|
+
* The root of the route trie, owned by {@link RouteStore.router}. Provides the public
|
|
175
|
+
* add/remove/find API plus the static path-parsing helpers used by {@link Route}'s constructor.
|
|
176
|
+
*/
|
|
177
|
+
declare class RouterRoot extends RouterBranch {
|
|
178
|
+
constructor();
|
|
179
|
+
add(route: Route): RouterNode;
|
|
180
|
+
remove(route: Route): boolean;
|
|
181
|
+
find(parts: readonly string[]): RouterBranch | null;
|
|
182
|
+
/**
|
|
183
|
+
* Splits a `/`-delimited path into its non-empty segments.
|
|
184
|
+
*/
|
|
185
|
+
static normalize(path?: string | null): string[];
|
|
186
|
+
/**
|
|
187
|
+
* Builds a route path from a piece's directory structure and file name: `(group)`-style
|
|
188
|
+
* directories are skipped, and a file named `index` collapses into its parent directory.
|
|
189
|
+
*/
|
|
190
|
+
static makeRoutePathForPiece(directories: readonly string[], name: string): string;
|
|
191
|
+
/**
|
|
192
|
+
* Extracts a trailing `.<method>` suffix from a piece name, e.g. `hello.post` → `POST`.
|
|
193
|
+
*/
|
|
194
|
+
static extractMethod(name: string): HttpMethod | null;
|
|
195
|
+
}
|
|
196
|
+
//#endregion
|
|
197
|
+
//#region src/lib/structures/RouteStore.d.ts
|
|
198
|
+
declare class RouteStore extends Store<Route, "routes"> {
|
|
199
|
+
/**
|
|
200
|
+
* The trie used to match an incoming request's pathname to a registered {@link Route}.
|
|
201
|
+
*/
|
|
202
|
+
readonly router: RouterRoot;
|
|
203
|
+
constructor();
|
|
204
|
+
}
|
|
205
|
+
//#endregion
|
|
206
|
+
//#region src/lib/http/ApiServer.d.ts
|
|
207
|
+
declare enum ApiServerEvent {
|
|
208
|
+
Error = "error",
|
|
209
|
+
Request = "request",
|
|
210
|
+
RouterBranchNotFound = "routerBranchNotFound",
|
|
211
|
+
RouterBranchMethodNotAllowed = "routerBranchMethodNotAllowed",
|
|
212
|
+
RouterFound = "routerFound",
|
|
213
|
+
RouteError = "routeError",
|
|
214
|
+
MiddlewareError = "middlewareError"
|
|
215
|
+
}
|
|
216
|
+
interface ApiServerEvents {
|
|
217
|
+
[ApiServerEvent.Error]: [error: Error];
|
|
218
|
+
[ApiServerEvent.Request]: [request: ApiRequest, response: ApiResponse];
|
|
219
|
+
[ApiServerEvent.RouterBranchNotFound]: [request: ApiRequest, response: ApiResponse];
|
|
220
|
+
[ApiServerEvent.RouterBranchMethodNotAllowed]: [request: ApiRequest, response: ApiResponse];
|
|
221
|
+
[ApiServerEvent.RouterFound]: [request: ApiRequest, response: ApiResponse];
|
|
222
|
+
[ApiServerEvent.RouteError]: [error: unknown, request: ApiRequest, response: ApiResponse];
|
|
223
|
+
[ApiServerEvent.MiddlewareError]: [error: unknown, request: ApiRequest, response: ApiResponse];
|
|
224
|
+
}
|
|
225
|
+
interface ApiServerOptions {
|
|
226
|
+
/**
|
|
227
|
+
* A path segment prefix applied to every route, e.g. `/api`.
|
|
228
|
+
*/
|
|
229
|
+
prefix?: string;
|
|
230
|
+
/**
|
|
231
|
+
* The value of the `Access-Control-Allow-Origin` header set by the built-in `headers` middleware.
|
|
232
|
+
* @default '*'
|
|
233
|
+
*/
|
|
234
|
+
origin?: string;
|
|
235
|
+
/**
|
|
236
|
+
* The maximum request body size in bytes, enforced by the built-in `body` middleware.
|
|
237
|
+
* @default 1024 * 1024 * 50
|
|
238
|
+
*/
|
|
239
|
+
maximumBodyLength?: number;
|
|
240
|
+
/**
|
|
241
|
+
* Raw options forwarded to `node:http`'s `createServer`.
|
|
242
|
+
*/
|
|
243
|
+
server?: ServerOptions;
|
|
244
|
+
/**
|
|
245
|
+
* Raw options forwarded to `Server#listen`.
|
|
246
|
+
* @default { port: 4000 }
|
|
247
|
+
*/
|
|
248
|
+
listenOptions?: ListenOptions;
|
|
249
|
+
/**
|
|
250
|
+
* Whether to start listening automatically once the interaction webhook is up (during the
|
|
251
|
+
* `postListen` plugin hook).
|
|
252
|
+
* @default true
|
|
253
|
+
*/
|
|
254
|
+
automaticallyConnect?: boolean;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* A standalone HTTP server for auxiliary REST routes (health checks, dashboards, webhooks from
|
|
258
|
+
* other services, etc). It is deliberately independent from {@link Client.server}, which is
|
|
259
|
+
* reserved for the Discord interactions webhook.
|
|
260
|
+
*/
|
|
261
|
+
declare class ApiServer extends AsyncEventEmitter<ApiServerEvents> {
|
|
262
|
+
readonly routes: RouteStore;
|
|
263
|
+
readonly middlewares: MiddlewareStore;
|
|
264
|
+
readonly server: Server<typeof ApiRequest, typeof ApiResponse>;
|
|
265
|
+
readonly options: ApiServerOptions;
|
|
266
|
+
constructor(options?: ApiServerOptions);
|
|
267
|
+
/**
|
|
268
|
+
* Starts listening for requests.
|
|
269
|
+
*/
|
|
270
|
+
connect(): Promise<void>;
|
|
271
|
+
/**
|
|
272
|
+
* Stops the server from accepting new connections.
|
|
273
|
+
*/
|
|
274
|
+
disconnect(): Promise<void>;
|
|
275
|
+
}
|
|
276
|
+
//#endregion
|
|
277
|
+
//#region src/listeners/_load.d.ts
|
|
278
|
+
/**
|
|
279
|
+
* Registers the built-in dispatch-pipeline listeners into the framework's existing listener
|
|
280
|
+
* store, targeting the `server` container entry (see {@link ApiServer}).
|
|
281
|
+
*/
|
|
282
|
+
declare function loadListeners(): Promise<void>;
|
|
283
|
+
//#endregion
|
|
284
|
+
//#region src/middlewares/_load.d.ts
|
|
285
|
+
/**
|
|
286
|
+
* Registers the built-in middlewares (`headers`, `body`) into {@link ApiServer.middlewares}.
|
|
287
|
+
*/
|
|
288
|
+
declare function loadMiddlewares(): Promise<void>;
|
|
289
|
+
//#endregion
|
|
290
|
+
//#region src/index.d.ts
|
|
291
|
+
declare module "@wolfstar/http-framework" {
|
|
292
|
+
interface ClientOptions {
|
|
293
|
+
/**
|
|
294
|
+
* Options for the auxiliary REST API server registered by `@wolfstar/plugin-api`.
|
|
295
|
+
*/
|
|
296
|
+
api?: ApiServerOptions;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
declare module "@sapphire/pieces" {
|
|
300
|
+
interface StoreRegistryEntries {
|
|
301
|
+
routes: RouteStore;
|
|
302
|
+
middlewares: MiddlewareStore;
|
|
303
|
+
}
|
|
304
|
+
interface Container {
|
|
305
|
+
/**
|
|
306
|
+
* The auxiliary REST API server registered by `@wolfstar/plugin-api`. Independent from the
|
|
307
|
+
* Discord interactions webhook server (`Client#server`).
|
|
308
|
+
*/
|
|
309
|
+
server: ApiServer;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
//#endregion
|
|
313
|
+
export { HttpMethods as _, ApiServerEvents as a, RouterRoot as c, ApiRequest as d, RouterBranch as f, HttpMethod as g, Route as h, ApiServerEvent as i, MiddlewareStore as l, Awaitable as m, loadListeners as n, ApiServerOptions as o, RouterNode as p, ApiServer as r, RouteStore as s, loadMiddlewares as t, Middleware as u, ApiResponse as v };
|
|
314
|
+
//# sourceMappingURL=index-D-l34LxR.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index-D-l34LxR.d.ts","names":[],"sources":["../../src/lib/http/ApiResponse.ts","../../src/lib/http/HttpMethod.ts","../../src/lib/structures/Route.ts","../../src/lib/structures/router/RouterNode.ts","../../src/lib/structures/router/RouterBranch.ts","../../src/lib/http/ApiRequest.ts","../../src/lib/structures/Middleware.ts","../../src/lib/structures/MiddlewareStore.ts","../../src/lib/structures/router/RouterRoot.ts","../../src/lib/structures/RouteStore.ts","../../src/lib/http/ApiServer.ts","../../src/listeners/_load.ts","../../src/middlewares/_load.ts","../../src/index.ts"],"mappings":";;;;;cAIa,oBAAoB,eAAe;EACvC,OAAO;EAKP,KAAK,eAAe;EAQpB,KAAK,cAAc;EAQnB;EAMA,SAAS;EAIT,iBAAiB;EAIjB,WAAW;EAIX,MACL,qBACA;;;;;;;cC3CS;KAED,qBAAqB;;;KCCrB,UAAU,KAAK,IAAI,YAAY;;;;;;;;uBASrB,MAAM,gBAAgB,MAAM,UAAU,MAAM,iBAAiB,MACjF;;;;WAMgB;;;;WAKA,SAAS,YAAY;EAElB,YAAA,SAAS,MAAM,eAAe,SAAS;;;;;WA6B1C,IAAI,SAAS,YAAY,UAAU,cAAc;;kBAGlD;OACH,gBAAgB,MAAM;YAEjB,gBAAgB;;;;;IAK/B;;;;;IAMA,mBAAmB;;;;;;;;cCpEV,mBAAmB,IAAI,YAAY;WAC9B,QAAQ;EAEL,YAAA,QAAQ;;;;;EASpB,kBAAkB,2BAA2B;;;;;;;;;cCXzC;WACK;WACA;WACA,QAAQ;WACR,MAAM;mBAEL;UACT;EAEW,YAAA,cAAc,kBAAkB,QAAQ;EAOpD,QAAQ;YAIL,SAAS,0BAA0B,eAAe,OAAO,QAAQ;YAyBjE,SAAS,0BAA0B,eAAe,OAAO;YAczD,OAAO,0BAA0B,gBAAgB;;;;cC/DhD,mBAAmB;;;;EAIvB,OAAO;;;;EAKP,QAAQ;;;;EAKR,YAAY;;;;EAKZ,OAAO;;;;;EAMD,aAAa,iBAAsB;;;;;EAiBnC,aAAa,aAAa,iBAAiB,QAAQ;;;;;;;;;uBCpC5C,WACpB,gBAAgB,WAAW,UAAU,WAAW,iBACxC,MAAM;;;;;;WAME;EAEG,YAAA,SAAS,WAAW,eAAe,SAAS;WAK/C,IAAI,SAAS,YAAY,UAAU,cAAc;;kBAGlD;OACH,gBAAgB,MAAM;YAEjB,gBAAgB;;;;;IAK/B;;;;;cC/BS,wBAAwB,MAAM;;;;WAIzB,mBAAmB;;;;;EAStB,IAAI,SAAS,YAAY,UAAU,cAAc;EAO9C,IAAI,aAAa,OAAO;EAUxB,OAAO;EAOP;;;;;;;;cC/BL,mBAAmB;;EAKvB,IAAI,OAAO,QAAQ;EAInB,OAAO,OAAO;EAId,KAAK,2BAA2B;;;;SAOzB,UAAU;;;;;SASV,sBAAsB,gCAAgC;;;;SAmBtD,cAAc,eAAe;;;;cCtDhC,mBAAmB,MAAM;;;;WAIpB,QAAM;;;;;aCIZ;EACV;EACA;EACA;EACA;EACA;EACA;EACA;;UAGe;GACd,eAAe,SAAS,OAAO;GAC/B,eAAe,WAAW,SAAS,YAAY,UAAU;GACzD,eAAe,wBAAwB,SAAS,YAAY,UAAU;GACtE,eAAe,gCAAgC,SAAS,YAAY,UAAU;GAC9E,eAAe,eAAe,SAAS,YAAY,UAAU;GAC7D,eAAe,cAAc,gBAAgB,SAAS,YAAY,UAAU;GAC5E,eAAe,mBAAmB,gBAAgB,SAAS,YAAY,UAAU;;UAGnE;;;;EAIf;;;;;EAMA;;;;;EAMA;;;;EAKA,SAAS;;;;;EAMT,gBAAgB;;;;;;EAOhB;;;;;;;cAQW,kBAAkB,kBAAkB;WAC/B,QAAQ;WACR,aAAa;WACb,QAAQ,cAAsB,mBAAmB;WACjD,SAAS;EAEN,YAAA,UAAS;;;;EA0BrB,WAAW;;;;EAoBX,cAAc;;;;;;;;iBCnHD,iBAAiB;;;;;;iBCLjB,mBAAmB;;;;YCa7B;;;;IAIR,MAAM;;;;YAKE;IACR,QAAQ;IACR,aAAa;;YAGL;;;;;IAKR,QAAQ"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { _ as HttpMethods, a as ApiServerEvents, c as RouterRoot, d as ApiRequest, f as RouterBranch, g as HttpMethod, h as Route, i as ApiServerEvent, l as MiddlewareStore, m as Awaitable, n as loadListeners, o as ApiServerOptions, p as RouterNode, r as ApiServer, s as RouteStore, t as loadMiddlewares, u as Middleware, v as ApiResponse } from "./index-D-l34LxR.js";
|
|
2
|
+
export { ApiRequest, ApiResponse, ApiServer, ApiServerEvent, ApiServerEvents, ApiServerOptions, Awaitable, HttpMethod, HttpMethods, Middleware, MiddlewareStore, Route, RouteStore, RouterBranch, RouterNode, RouterRoot, loadListeners, loadMiddlewares };
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { a as RouteStore, c as RouterBranch, d as MiddlewareStore, f as Middleware, i as ApiServerEvent, l as RouterNode, m as ApiRequest, n as loadListeners, o as Route, p as ApiResponse, r as ApiServer, s as RouterRoot, t as loadMiddlewares, u as HttpMethods } from "./src-CscMIChC.js";
|
|
2
|
+
|
|
3
|
+
export { ApiRequest, ApiResponse, ApiServer, ApiServerEvent, HttpMethods, Middleware, MiddlewareStore, Route, RouteStore, RouterBranch, RouterNode, RouterRoot, loadListeners, loadMiddlewares };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import "./index-D-l34LxR.js";
|
|
2
|
+
import { Client, ClientOptions, Plugin, postInitialization, postListen } from "@wolfstar/http-framework";
|
|
3
|
+
//#region src/register.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Registers a standalone {@link ApiServer} for auxiliary REST routes (health checks, dashboards,
|
|
6
|
+
* webhooks from other services, etc), independent from the Discord interactions webhook server.
|
|
7
|
+
*
|
|
8
|
+
* Activate by importing the side-effecting entrypoint before creating the client:
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import '@wolfstar/plugin-api/register';
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
declare class ApiPlugin extends Plugin {
|
|
15
|
+
static [postInitialization](this: Client, options: ClientOptions): void;
|
|
16
|
+
static [postListen](this: Client, options: ClientOptions): Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
export { ApiPlugin };
|
|
20
|
+
//# sourceMappingURL=register.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"register.d.ts","names":[],"sources":["../../src/register.ts"],"mappings":";;;;;;;;;;;;;cAuBa,kBAAkB;UACd,oBAAoB,MAAM,QAAQ,SAAS;UAerC,YAAY,MAAM,QAAQ,SAAS,gBAAgB"}
|