@sapphire/plugin-api 7.0.2-next.80816ca → 7.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/cjs/index.cjs +1 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -1
- package/dist/cjs/lib/structures/Route.cjs +11 -9
- package/dist/cjs/lib/structures/Route.cjs.map +1 -1
- package/dist/cjs/lib/structures/router/RouterRoot.cjs +6 -4
- package/dist/cjs/lib/structures/router/RouterRoot.cjs.map +1 -1
- package/dist/cjs/register.cjs.map +1 -1
- package/dist/esm/index.d.mts +1 -1
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/lib/structures/Route.mjs +11 -9
- package/dist/esm/lib/structures/Route.mjs.map +1 -1
- package/dist/esm/lib/structures/router/RouterRoot.mjs +6 -4
- package/dist/esm/lib/structures/router/RouterRoot.mjs.map +1 -1
- package/dist/esm/register.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
|
|
5
|
+
# [@sapphire/plugin-api@7.0.2](https://github.com/sapphiredev/plugins/compare/@sapphire/plugin-api@7.0.1...@sapphire/plugin-api@7.0.2) - (2024-11-05)
|
|
6
|
+
|
|
7
|
+
## 🐛 Bug Fixes
|
|
8
|
+
|
|
9
|
+
- **route:** Handle `index` case correctly ([954d495](https://github.com/sapphiredev/plugins/commit/954d495d2834f76983a70034624a3d4f5749e97e)) ([#657](https://github.com/sapphiredev/plugins/pull/657) by @kyranet)
|
|
10
|
+
|
|
5
11
|
# [@sapphire/plugin-api@7.0.1](https://github.com/sapphiredev/plugins/compare/@sapphire/plugin-api@7.0.0...@sapphire/plugin-api@7.0.1) - (2024-11-05)
|
|
6
12
|
|
|
7
13
|
## 🏠 Refactor
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -19,7 +19,7 @@ var _load_cjs$1 = require('./middlewares/_load.cjs');
|
|
|
19
19
|
var _load_cjs$2 = require('./routes/_load.cjs');
|
|
20
20
|
|
|
21
21
|
// src/index.ts
|
|
22
|
-
var version = "7.0.2
|
|
22
|
+
var version = "7.0.2";
|
|
23
23
|
|
|
24
24
|
Object.defineProperty(exports, "loadListeners", {
|
|
25
25
|
enumerable: true,
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;AAoDO,IAAM,OAAkB,GAAA","file":"index.cjs","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.2
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;AAoDO,IAAM,OAAkB,GAAA","file":"index.cjs","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.2';\n"]}
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -678,7 +678,7 @@ declare class RouterRoot extends RouterBranch {
|
|
|
678
678
|
toString(): string;
|
|
679
679
|
static makeRoutePathForPiece(directories: readonly string[], name: string): string;
|
|
680
680
|
static normalize(path: string | null | undefined): string[];
|
|
681
|
-
static extractMethod(path: readonly string[]): MethodName | null;
|
|
681
|
+
static extractMethod(path: string | readonly string[]): MethodName | null;
|
|
682
682
|
}
|
|
683
683
|
|
|
684
684
|
/**
|
|
@@ -24,16 +24,18 @@ var _Route = class _Route extends pieces.Piece {
|
|
|
24
24
|
*/
|
|
25
25
|
__publicField(this, "methods");
|
|
26
26
|
const api = this.container.server.options;
|
|
27
|
-
const path =
|
|
28
|
-
...RouterRoot_cjs.RouterRoot.normalize(api.prefix),
|
|
29
|
-
...RouterRoot_cjs.RouterRoot.normalize(options.route ?? RouterRoot_cjs.RouterRoot.makeRoutePathForPiece(this.location.directories, this.name))
|
|
30
|
-
];
|
|
27
|
+
const path = RouterRoot_cjs.RouterRoot.normalize(api.prefix);
|
|
31
28
|
const methods = new Set(options.methods);
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
29
|
+
if (options.route) {
|
|
30
|
+
path.push(...RouterRoot_cjs.RouterRoot.normalize(options.route));
|
|
31
|
+
} else {
|
|
32
|
+
let lastPart = context.name;
|
|
33
|
+
const implied = RouterRoot_cjs.RouterRoot.extractMethod(lastPart);
|
|
34
|
+
if (!utilities.isNullish(implied)) {
|
|
35
|
+
lastPart = lastPart.slice(0, lastPart.length - implied.length - 1);
|
|
36
|
+
methods.add(implied);
|
|
37
|
+
}
|
|
38
|
+
path.push(...RouterRoot_cjs.RouterRoot.normalize(RouterRoot_cjs.RouterRoot.makeRoutePathForPiece(this.location.directories, lastPart)));
|
|
37
39
|
}
|
|
38
40
|
this.path = path;
|
|
39
41
|
this.methods = methods;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/lib/structures/Route.ts"],"names":["Piece","RouterRoot","isNullish"],"mappings":";;;;;;;;;;AA4CO,IAAe,MAAA,GAAf,MAAe,MAAA,SAA6DA,YAAyB,CAAA;AAAA,EAgBpG,WAAY,CAAA,OAAA,EAA8B,OAAmB,GAAA,EAAe,EAAA;AAClF,IAAA,KAAA,CAAM,SAAS,OAAO,CAAA;AAbvB;AAAA;AAAA;AAAA,IAAgB,aAAA,CAAA,IAAA,EAAA,mBAAA,CAAA;AAKhB;AAAA;AAAA;AAAA,IAAgB,aAAA,CAAA,IAAA,EAAA,MAAA,CAAA;AAKhB;AAAA;AAAA;AAAA,IAAgB,aAAA,CAAA,IAAA,EAAA,SAAA,CAAA;AAKf,IAAM,MAAA,GAAA,GAAM,IAAK,CAAA,SAAA,CAAU,MAAO,CAAA,OAAA;
|
|
1
|
+
{"version":3,"sources":["../../../../src/lib/structures/Route.ts"],"names":["Piece","RouterRoot","isNullish"],"mappings":";;;;;;;;;;AA4CO,IAAe,MAAA,GAAf,MAAe,MAAA,SAA6DA,YAAyB,CAAA;AAAA,EAgBpG,WAAY,CAAA,OAAA,EAA8B,OAAmB,GAAA,EAAe,EAAA;AAClF,IAAA,KAAA,CAAM,SAAS,OAAO,CAAA;AAbvB;AAAA;AAAA;AAAA,IAAgB,aAAA,CAAA,IAAA,EAAA,mBAAA,CAAA;AAKhB;AAAA;AAAA;AAAA,IAAgB,aAAA,CAAA,IAAA,EAAA,MAAA,CAAA;AAKhB;AAAA;AAAA;AAAA,IAAgB,aAAA,CAAA,IAAA,EAAA,SAAA,CAAA;AAKf,IAAM,MAAA,GAAA,GAAM,IAAK,CAAA,SAAA,CAAU,MAAO,CAAA,OAAA;AAElC,IAAA,MAAM,IAAO,GAAAC,yBAAA,CAAW,SAAU,CAAA,GAAA,CAAI,MAAM,CAAA;AAC5C,IAAA,MAAM,OAAU,GAAA,IAAI,GAAI,CAAA,OAAA,CAAQ,OAAO,CAAA;AACvC,IAAA,IAAI,QAAQ,KAAO,EAAA;AAElB,MAAA,IAAA,CAAK,KAAK,GAAGA,yBAAA,CAAW,SAAU,CAAA,OAAA,CAAQ,KAAK,CAAC,CAAA;AAAA,KAC1C,MAAA;AAGN,MAAA,IAAI,WAAW,OAAQ,CAAA,IAAA;AACvB,MAAM,MAAA,OAAA,GAAUA,yBAAW,CAAA,aAAA,CAAc,QAAQ,CAAA;AACjD,MAAI,IAAA,CAACC,mBAAU,CAAA,OAAO,CAAG,EAAA;AACxB,QAAA,QAAA,GAAW,SAAS,KAAM,CAAA,CAAA,EAAG,SAAS,MAAS,GAAA,OAAA,CAAQ,SAAS,CAAC,CAAA;AACjE,QAAA,OAAA,CAAQ,IAAI,OAAO,CAAA;AAAA;AAGpB,MAAK,IAAA,CAAA,IAAA,CAAK,GAAGD,yBAAA,CAAW,SAAU,CAAAA,yBAAA,CAAW,qBAAsB,CAAA,IAAA,CAAK,QAAS,CAAA,WAAA,EAAa,QAAQ,CAAC,CAAC,CAAA;AAAA;AAGzG,IAAA,IAAA,CAAK,IAAO,GAAA,IAAA;AACZ,IAAA,IAAA,CAAK,OAAU,GAAA,OAAA;AACf,IAAA,IAAA,CAAK,oBAAoB,OAAQ,CAAA,iBAAA,IAAqB,GAAI,CAAA,iBAAA,IAAqB,OAAO,IAAO,GAAA,EAAA;AAAA;AAI/F,CAAA;AA7C4G,MAAA,CAAA,MAAA,EAAA,OAAA,CAAA;AAArG,IAAe,KAAf,GAAA","file":"Route.cjs","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\n\t\tconst path = RouterRoot.normalize(api.prefix);\n\t\tconst methods = new Set(options.methods);\n\t\tif (options.route) {\n\t\t\t// If a route is specified, no extra processing is made:\n\t\t\tpath.push(...RouterRoot.normalize(options.route));\n\t\t} else {\n\t\t\t// If a route is not specified, extra processing is made to calculate\n\t\t\t// one from the file system if it's possible:\n\t\t\tlet lastPart = context.name;\n\t\t\tconst implied = RouterRoot.extractMethod(lastPart);\n\t\t\tif (!isNullish(implied)) {\n\t\t\t\tlastPart = lastPart.slice(0, lastPart.length - implied.length - 1);\n\t\t\t\tmethods.add(implied);\n\t\t\t}\n\n\t\t\tpath.push(...RouterRoot.normalize(RouterRoot.makeRoutePathForPiece(this.location.directories, lastPart)));\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"]}
|
|
@@ -68,11 +68,13 @@ var _RouterRoot = class _RouterRoot extends RouterBranch_cjs.RouterBranch {
|
|
|
68
68
|
}
|
|
69
69
|
static extractMethod(path) {
|
|
70
70
|
if (path.length === 0) return null;
|
|
71
|
+
if (typeof path === "string") {
|
|
72
|
+
const methodSeparatorPositionIndex = path.lastIndexOf(".");
|
|
73
|
+
if (methodSeparatorPositionIndex === -1 || methodSeparatorPositionIndex === path.length - 1) return null;
|
|
74
|
+
return path.slice(methodSeparatorPositionIndex + 1).toUpperCase();
|
|
75
|
+
}
|
|
71
76
|
const lastIndex = path.length - 1;
|
|
72
|
-
|
|
73
|
-
const methodIndex = last.lastIndexOf(".");
|
|
74
|
-
if (methodIndex === -1 || methodIndex === last.length - 1) return null;
|
|
75
|
-
return last.slice(methodIndex + 1).toUpperCase();
|
|
77
|
+
return _RouterRoot.extractMethod(path[lastIndex]);
|
|
76
78
|
}
|
|
77
79
|
};
|
|
78
80
|
__name(_RouterRoot, "RouterRoot");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../src/lib/structures/router/RouterRoot.ts"],"names":["RouterBranch","isNullishOrEmpty"],"mappings":";;;;;;;AAMO,IAAM,WAAA,GAAN,MAAM,WAAA,SAAmBA,6BAAa,CAAA;AAAA,EACrC,WAAc,GAAA;AACpB,IAAM,KAAA,CAAA,UAAA,EAAY,OAAO,IAAI,CAAA;AAAA;AAC9B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQO,IAAI,KAA0B,EAAA;AACpC,IAAA,OAAO,IAAK,CAAA,IAAA,CAAK,KAAM,CAAA,IAAA,EAAM,GAAG,KAAK,CAAA;AAAA;AACtC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQO,OAAO,KAAuB,EAAA;AACpC,IAAA,OAAO,IAAK,CAAA,OAAA,CAAQ,KAAM,CAAA,IAAA,EAAM,GAAG,KAAK,CAAA;AAAA;AACzC;AAAA,EAGA,IAAoB,IAAe,GAAA;AAClC,IAAO,OAAA,EAAA;AAAA;AACR,EAEgB,QAAmB,GAAA;AAClC,IAAO,OAAA,EAAA;AAAA;AACR,EAEA,OAAc,qBAAsB,CAAA,WAAA,EAAgC,IAAsB,EAAA;AACzF,IAAA,MAAM,QAAkB,EAAC;AACzB,IAAA,KAAA,MAAW,aAAa,WAAa,EAAA;AACpC,MAAM,MAAA,OAAA,GAAU,UAAU,IAAK,EAAA;AA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AACf,UAAO,IAAA,GAAA,EAAA;AAAA;AACR,OACM,MAAA;AACN,QAAQ,IAAA,IAAA,IAAA;AAAA;AACT;AAGD,IAAA,IAAI,KAAK,MAAQ,EAAA;AAChB,MAAA,KAAA,CAAM,KAAK,IAAI,CAAA;AAAA;AAGhB,IAAO,OAAA,KAAA;AAAA;AACR,EAEA,OAAc,cAAc,IAAqD,EAAA;AAChF,IAAI,IAAA,IAAA,CAAK,MAAW,KAAA,CAAA,EAAU,OAAA,IAAA;AAC9B,IAAI,IAAA,OAAO,SAAS,QAAU,EAAA;AAC7B,MAAM,MAAA,4BAAA,GAA+B,IAAK,CAAA,WAAA,CAAY,GAAG,CAAA;AACzD,MAAA,IAAI,iCAAiC,CAAM,CAAA,IAAA,4BAAA,KAAiC,IAAK,CAAA,MAAA,GAAS,GAAU,OAAA,IAAA;AAEpG,MAAA,OAAO,IAAK,CAAA,KAAA,CAAM,4BAA+B,GAAA,CAAC,EAAE,WAAY,EAAA;AAAA;AAGjE,IAAM,MAAA,SAAA,GAAY,KAAK,MAAS,GAAA,CAAA;AAChC,IAAA,OAAO,WAAW,CAAA,aAAA,CAAc,IAAK,CAAA,SAAS,CAAC,CAAA;AAAA;AAEjD,CAAA;AAzF6C,MAAA,CAAA,WAAA,EAAA,YAAA,CAAA;AAAtC,IAAM,UAAN,GAAA","file":"RouterRoot.cjs","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\tif (name !== 'index') {\n\t\t\tparts.push(name.trim());\n\t\t}\n\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: string | readonly string[]): MethodName | null {\n\t\tif (path.length === 0) return null;\n\t\tif (typeof path === 'string') {\n\t\t\tconst methodSeparatorPositionIndex = path.lastIndexOf('.');\n\t\t\tif (methodSeparatorPositionIndex === -1 || methodSeparatorPositionIndex === path.length - 1) return null;\n\n\t\t\treturn path.slice(methodSeparatorPositionIndex + 1).toUpperCase() as MethodName;\n\t\t}\n\n\t\tconst lastIndex = path.length - 1;\n\t\treturn RouterRoot.extractMethod(path[lastIndex]);\n\t}\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/register.ts"],"names":["Plugin","postInitialization","Server","loadListeners","loadMiddlewares","loadRoutes","preLogin","SapphireClient"],"mappings":";;;;;;;AAOO,IAAM,IAAA,GAAN,MAAM,IAAA,SAAYA,gBAAO,CAAA;AAAA;AAAA;AAAA;AAAA,EAI/B,
|
|
1
|
+
{"version":3,"sources":["../../src/register.ts"],"names":["Plugin","postInitialization","Server","loadListeners","loadMiddlewares","loadRoutes","preLogin","SapphireClient"],"mappings":";;;;;;;AAOO,IAAM,IAAA,GAAN,MAAM,IAAA,SAAYA,gBAAO,CAAA;AAAA;AAAA;AAAA;AAAA,EAI/B,QAAwBC,4BAAkB,CAAA,CAAwB,OAA8B,EAAA;AAC/F,IAAA,IAAA,CAAK,MAAS,GAAA,IAAIC,gBAAO,CAAA,OAAA,CAAQ,GAAG,CAAA;AACpC,IAAK,IAAA,CAAA,MAAA,CACH,SAAS,IAAK,CAAA,MAAA,CAAO,MAAM,CAC3B,CAAA,QAAA,CAAS,IAAK,CAAA,MAAA,CAAO,WAAW,CAAA;AAElC,IAAcC,uBAAA,EAAA;AACd,IAAgBC,yBAAA,EAAA;AAChB,IAAWC,oBAAA,EAAA;AAAA;AACZ;AAAA;AAAA;AAAA,EAKA,cAA8BC,kBAAQ,CAAuC,GAAA;AAC5E,IAAA,IAAI,EAAE,IAAA,CAAK,MAAO,CAAA,OAAA,CAAQ,wBAAwB,IAAO,CAAA,EAAA;AACxD,MAAA;AAAA;AAGD,IAAM,MAAA,IAAA,CAAK,OAAO,OAAQ,EAAA;AAAA;AAE5B,CAAA;AAzBgC,MAAA,CAAA,IAAA,EAAA,KAAA,CAAA;AAAzB,IAAM,GAAN,GAAA;AA2BPC,wBAAA,CAAe,OAAQ,CAAA,8BAAA,CAA+B,GAAI,CAAAN,4BAAkB,GAAG,wBAAwB,CAAA;AACvGM,wBAAA,CAAe,OAAQ,CAAA,oBAAA,CAAqB,GAAI,CAAAD,kBAAQ,GAAG,cAAc,CAAA","file":"register.cjs","sourcesContent":["import { Plugin, postInitialization, preLogin, SapphireClient } from '@sapphire/framework';\nimport type { ClientOptions } from 'discord.js';\nimport { loadListeners, loadMiddlewares, loadRoutes, Server } from './index';\n\n/**\n * @since 1.0.0\n */\nexport class Api extends Plugin {\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic static override [postInitialization](this: SapphireClient, options: ClientOptions): void {\n\t\tthis.server = new Server(options.api);\n\t\tthis.stores\n\t\t\t.register(this.server.routes) //\n\t\t\t.register(this.server.middlewares);\n\n\t\tloadListeners();\n\t\tloadMiddlewares();\n\t\tloadRoutes();\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic static override async [preLogin](this: SapphireClient): Promise<void> {\n\t\tif (!(this.server.options.automaticallyConnect ?? true)) {\n\t\t\treturn;\n\t\t}\n\n\t\tawait this.server.connect();\n\t}\n}\n\nSapphireClient.plugins.registerPostInitializationHook(Api[postInitialization], 'API-PostInitialization');\nSapphireClient.plugins.registerPreLoginHook(Api[preLogin], 'API-PreLogin');\n"]}
|
package/dist/esm/index.d.mts
CHANGED
|
@@ -678,7 +678,7 @@ declare class RouterRoot extends RouterBranch {
|
|
|
678
678
|
toString(): string;
|
|
679
679
|
static makeRoutePathForPiece(directories: readonly string[], name: string): string;
|
|
680
680
|
static normalize(path: string | null | undefined): string[];
|
|
681
|
-
static extractMethod(path: readonly string[]): MethodName | null;
|
|
681
|
+
static extractMethod(path: string | readonly string[]): MethodName | null;
|
|
682
682
|
}
|
|
683
683
|
|
|
684
684
|
/**
|
package/dist/esm/index.mjs
CHANGED
|
@@ -17,7 +17,7 @@ export { loadListeners } from './listeners/_load.mjs';
|
|
|
17
17
|
export { loadMiddlewares } from './middlewares/_load.mjs';
|
|
18
18
|
export { loadRoutes } from './routes/_load.mjs';
|
|
19
19
|
|
|
20
|
-
var version = "7.0.2
|
|
20
|
+
var version = "7.0.2";
|
|
21
21
|
|
|
22
22
|
export { version };
|
|
23
23
|
//# sourceMappingURL=index.mjs.map
|
package/dist/esm/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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.2
|
|
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.2';\n"]}
|
|
@@ -19,16 +19,18 @@ var _Route = class _Route extends Piece {
|
|
|
19
19
|
*/
|
|
20
20
|
__publicField(this, "methods");
|
|
21
21
|
const api = this.container.server.options;
|
|
22
|
-
const path =
|
|
23
|
-
...RouterRoot.normalize(api.prefix),
|
|
24
|
-
...RouterRoot.normalize(options.route ?? RouterRoot.makeRoutePathForPiece(this.location.directories, this.name))
|
|
25
|
-
];
|
|
22
|
+
const path = RouterRoot.normalize(api.prefix);
|
|
26
23
|
const methods = new Set(options.methods);
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
24
|
+
if (options.route) {
|
|
25
|
+
path.push(...RouterRoot.normalize(options.route));
|
|
26
|
+
} else {
|
|
27
|
+
let lastPart = context.name;
|
|
28
|
+
const implied = RouterRoot.extractMethod(lastPart);
|
|
29
|
+
if (!isNullish(implied)) {
|
|
30
|
+
lastPart = lastPart.slice(0, lastPart.length - implied.length - 1);
|
|
31
|
+
methods.add(implied);
|
|
32
|
+
}
|
|
33
|
+
path.push(...RouterRoot.normalize(RouterRoot.makeRoutePathForPiece(this.location.directories, lastPart)));
|
|
32
34
|
}
|
|
33
35
|
this.path = path;
|
|
34
36
|
this.methods = methods;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/lib/structures/Route.ts"],"names":[],"mappings":";;;;;AA4CO,IAAe,MAAA,GAAf,MAAe,MAAA,SAA6D,KAAyB,CAAA;AAAA,EAgBpG,WAAY,CAAA,OAAA,EAA8B,OAAmB,GAAA,EAAe,EAAA;AAClF,IAAA,KAAA,CAAM,SAAS,OAAO,CAAA;AAbvB;AAAA;AAAA;AAAA,IAAgB,aAAA,CAAA,IAAA,EAAA,mBAAA,CAAA;AAKhB;AAAA;AAAA;AAAA,IAAgB,aAAA,CAAA,IAAA,EAAA,MAAA,CAAA;AAKhB;AAAA;AAAA;AAAA,IAAgB,aAAA,CAAA,IAAA,EAAA,SAAA,CAAA;AAKf,IAAM,MAAA,GAAA,GAAM,IAAK,CAAA,SAAA,CAAU,MAAO,CAAA,OAAA;
|
|
1
|
+
{"version":3,"sources":["../../../../src/lib/structures/Route.ts"],"names":[],"mappings":";;;;;AA4CO,IAAe,MAAA,GAAf,MAA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IAAA;AACZ,IAAA,IAAA,CAAK,OAAU,GAAA,OAAA;AACf,IAAA,IAAA,CAAK,oBAAoB,OAAQ,CAAA,iBAAA,IAAqB,GAAI,CAAA,iBAAA,IAAqB,OAAO,IAAO,GAAA,EAAA;AAAA;AAI/F,CAAA;AA7C4G,MAAA,CAAA,MAAA,EAAA,OAAA,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\n\t\tconst path = RouterRoot.normalize(api.prefix);\n\t\tconst methods = new Set(options.methods);\n\t\tif (options.route) {\n\t\t\t// If a route is specified, no extra processing is made:\n\t\t\tpath.push(...RouterRoot.normalize(options.route));\n\t\t} else {\n\t\t\t// If a route is not specified, extra processing is made to calculate\n\t\t\t// one from the file system if it's possible:\n\t\t\tlet lastPart = context.name;\n\t\t\tconst implied = RouterRoot.extractMethod(lastPart);\n\t\t\tif (!isNullish(implied)) {\n\t\t\t\tlastPart = lastPart.slice(0, lastPart.length - implied.length - 1);\n\t\t\t\tmethods.add(implied);\n\t\t\t}\n\n\t\t\tpath.push(...RouterRoot.normalize(RouterRoot.makeRoutePathForPiece(this.location.directories, lastPart)));\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"]}
|
|
@@ -65,11 +65,13 @@ var _RouterRoot = class _RouterRoot extends RouterBranch {
|
|
|
65
65
|
}
|
|
66
66
|
static extractMethod(path) {
|
|
67
67
|
if (path.length === 0) return null;
|
|
68
|
+
if (typeof path === "string") {
|
|
69
|
+
const methodSeparatorPositionIndex = path.lastIndexOf(".");
|
|
70
|
+
if (methodSeparatorPositionIndex === -1 || methodSeparatorPositionIndex === path.length - 1) return null;
|
|
71
|
+
return path.slice(methodSeparatorPositionIndex + 1).toUpperCase();
|
|
72
|
+
}
|
|
68
73
|
const lastIndex = path.length - 1;
|
|
69
|
-
|
|
70
|
-
const methodIndex = last.lastIndexOf(".");
|
|
71
|
-
if (methodIndex === -1 || methodIndex === last.length - 1) return null;
|
|
72
|
-
return last.slice(methodIndex + 1).toUpperCase();
|
|
74
|
+
return _RouterRoot.extractMethod(path[lastIndex]);
|
|
73
75
|
}
|
|
74
76
|
};
|
|
75
77
|
__name(_RouterRoot, "RouterRoot");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../src/lib/structures/router/RouterRoot.ts"],"names":[],"mappings":";;;;AAMO,IAAM,WAAA,GAAN,MAAM,WAAA,SAAmB,YAAa,CAAA;AAAA,EACrC,WAAc,GAAA;AACpB,IAAM,KAAA,CAAA,UAAA,EAAY,OAAO,IAAI,CAAA;AAAA;AAC9B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQO,IAAI,KAA0B,EAAA;AACpC,IAAA,OAAO,IAAK,CAAA,IAAA,CAAK,KAAM,CAAA,IAAA,EAAM,GAAG,KAAK,CAAA;AAAA;AACtC;AAAA;AAAA;AAAA;AAAA;AAAA;AAA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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\tif (name !== 'index') {\n\t\t\tparts.push(name.trim());\n\t\t}\n\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: string | readonly string[]): MethodName | null {\n\t\tif (path.length === 0) return null;\n\t\tif (typeof path === 'string') {\n\t\t\tconst methodSeparatorPositionIndex = path.lastIndexOf('.');\n\t\t\tif (methodSeparatorPositionIndex === -1 || methodSeparatorPositionIndex === path.length - 1) return null;\n\n\t\t\treturn path.slice(methodSeparatorPositionIndex + 1).toUpperCase() as MethodName;\n\t\t}\n\n\t\tconst lastIndex = path.length - 1;\n\t\treturn RouterRoot.extractMethod(path[lastIndex]);\n\t}\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/register.ts"],"names":[],"mappings":";;;;AAOO,IAAM,IAAA,GAAN,MAAM,IAAA,SAAY,MAAO,CAAA;AAAA;AAAA;AAAA;AAAA,EAI/B,
|
|
1
|
+
{"version":3,"sources":["../../src/register.ts"],"names":[],"mappings":";;;;AAOO,IAAM,IAAA,GAAN,MAAM,IAAA,SAAY,MAAO,CAAA;AAAA;AAAA;AAAA;AAAA,EAI/B,QAAwB,kBAAkB,CAAA,CAAwB,OAA8B,EAAA;AAC/F,IAAA,IAAA,CAAK,MAAS,GAAA,IAAI,MAAO,CAAA,OAAA,CAAQ,GAAG,CAAA;AACpC,IAAK,IAAA,CAAA,MAAA,CACH,SAAS,IAAK,CAAA,MAAA,CAAO,MAAM,CAC3B,CAAA,QAAA,CAAS,IAAK,CAAA,MAAA,CAAO,WAAW,CAAA;AAElC,IAAc,aAAA,EAAA;AACd,IAAgB,eAAA,EAAA;AAChB,IAAW,UAAA,EAAA;AAAA;AACZ;AAAA;AAAA;AAAA,EAKA,cAA8B,QAAQ,CAAuC,GAAA;AAC5E,IAAA,IAAI,EAAE,IAAA,CAAK,MAAO,CAAA,OAAA,CAAQ,wBAAwB,IAAO,CAAA,EAAA;AACxD,MAAA;AAAA;AAGD,IAAM,MAAA,IAAA,CAAK,OAAO,OAAQ,EAAA;AAAA;AAE5B,CAAA;AAzBgC,MAAA,CAAA,IAAA,EAAA,KAAA,CAAA;AAAzB,IAAM,GAAN,GAAA;AA2BP,cAAA,CAAe,OAAQ,CAAA,8BAAA,CAA+B,GAAI,CAAA,kBAAkB,GAAG,wBAAwB,CAAA;AACvG,cAAA,CAAe,OAAQ,CAAA,oBAAA,CAAqB,GAAI,CAAA,QAAQ,GAAG,cAAc,CAAA","file":"register.mjs","sourcesContent":["import { Plugin, postInitialization, preLogin, SapphireClient } from '@sapphire/framework';\nimport type { ClientOptions } from 'discord.js';\nimport { loadListeners, loadMiddlewares, loadRoutes, Server } from './index';\n\n/**\n * @since 1.0.0\n */\nexport class Api extends Plugin {\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic static override [postInitialization](this: SapphireClient, options: ClientOptions): void {\n\t\tthis.server = new Server(options.api);\n\t\tthis.stores\n\t\t\t.register(this.server.routes) //\n\t\t\t.register(this.server.middlewares);\n\n\t\tloadListeners();\n\t\tloadMiddlewares();\n\t\tloadRoutes();\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic static override async [preLogin](this: SapphireClient): Promise<void> {\n\t\tif (!(this.server.options.automaticallyConnect ?? true)) {\n\t\t\treturn;\n\t\t}\n\n\t\tawait this.server.connect();\n\t}\n}\n\nSapphireClient.plugins.registerPostInitializationHook(Api[postInitialization], 'API-PostInitialization');\nSapphireClient.plugins.registerPreLoginHook(Api[preLogin], 'API-PreLogin');\n"]}
|