@sapphire/plugin-api 7.0.0-next.d27dc1c → 7.0.0-next.fc8e636

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.
Files changed (148) hide show
  1. package/dist/cjs/index.cjs +22 -8
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs/index.d.cts +273 -149
  4. package/dist/cjs/lib/structures/MediaParser.cjs.map +1 -1
  5. package/dist/cjs/lib/structures/Middleware.cjs.map +1 -1
  6. package/dist/cjs/lib/structures/MiddlewareStore.cjs +2 -2
  7. package/dist/cjs/lib/structures/MiddlewareStore.cjs.map +1 -1
  8. package/dist/cjs/lib/structures/Route.cjs +17 -34
  9. package/dist/cjs/lib/structures/Route.cjs.map +1 -1
  10. package/dist/cjs/lib/structures/RouteLoaderStrategy.cjs +20 -0
  11. package/dist/cjs/lib/structures/RouteLoaderStrategy.cjs.map +1 -0
  12. package/dist/cjs/lib/structures/RouteStore.cjs +4 -42
  13. package/dist/cjs/lib/structures/RouteStore.cjs.map +1 -1
  14. package/dist/cjs/lib/structures/api/ApiRequest.cjs +26 -0
  15. package/dist/cjs/lib/structures/api/ApiRequest.cjs.map +1 -1
  16. package/dist/cjs/lib/structures/api/ApiResponse.cjs +6 -0
  17. package/dist/cjs/lib/structures/api/ApiResponse.cjs.map +1 -1
  18. package/dist/cjs/lib/structures/api/CookieStore.cjs +3 -3
  19. package/dist/cjs/lib/structures/api/CookieStore.cjs.map +1 -1
  20. package/dist/cjs/lib/structures/http/HttpMethods.cjs +37 -6
  21. package/dist/cjs/lib/structures/http/HttpMethods.cjs.map +1 -1
  22. package/dist/cjs/lib/structures/http/Server.cjs +15 -14
  23. package/dist/cjs/lib/structures/http/Server.cjs.map +1 -1
  24. package/dist/cjs/lib/structures/router/RouterBranch.cjs +184 -0
  25. package/dist/cjs/lib/structures/router/RouterBranch.cjs.map +1 -0
  26. package/dist/cjs/lib/structures/router/RouterNode.cjs +67 -0
  27. package/dist/cjs/lib/structures/router/RouterNode.cjs.map +1 -0
  28. package/dist/cjs/lib/structures/router/RouterRoot.cjs +71 -0
  29. package/dist/cjs/lib/structures/router/RouterRoot.cjs.map +1 -0
  30. package/dist/cjs/listeners/PluginRouteError.cjs +2 -2
  31. package/dist/cjs/listeners/PluginRouteError.cjs.map +1 -1
  32. package/dist/cjs/listeners/PluginServerMiddlewareError.cjs +2 -2
  33. package/dist/cjs/listeners/PluginServerMiddlewareError.cjs.map +1 -1
  34. package/dist/cjs/listeners/PluginServerMiddlewareSuccess.cjs +4 -4
  35. package/dist/cjs/listeners/PluginServerMiddlewareSuccess.cjs.map +1 -1
  36. package/dist/cjs/listeners/PluginServerRequest.cjs +41 -7
  37. package/dist/cjs/listeners/PluginServerRequest.cjs.map +1 -1
  38. package/dist/cjs/listeners/{PluginServerMatch.cjs → PluginServerRouterBranchMethodNotAllowed.cjs} +4 -9
  39. package/dist/cjs/listeners/PluginServerRouterBranchMethodNotAllowed.cjs.map +1 -0
  40. package/dist/cjs/listeners/{PluginServerNoMatch.cjs → PluginServerRouterBranchNotFound.cjs} +2 -2
  41. package/dist/cjs/listeners/PluginServerRouterBranchNotFound.cjs.map +1 -0
  42. package/dist/cjs/listeners/PluginServerRouterFound.cjs +21 -0
  43. package/dist/cjs/listeners/PluginServerRouterFound.cjs.map +1 -0
  44. package/dist/cjs/listeners/_load.cjs +6 -4
  45. package/dist/cjs/listeners/_load.cjs.map +1 -1
  46. package/dist/cjs/mediaParsers/applicationJson.cjs.map +1 -1
  47. package/dist/cjs/mediaParsers/textPlain.cjs.map +1 -1
  48. package/dist/cjs/middlewares/auth.cjs.map +1 -1
  49. package/dist/cjs/middlewares/body.cjs +4 -3
  50. package/dist/cjs/middlewares/body.cjs.map +1 -1
  51. package/dist/cjs/middlewares/cookies.cjs.map +1 -1
  52. package/dist/cjs/middlewares/headers.cjs +18 -8
  53. package/dist/cjs/middlewares/headers.cjs.map +1 -1
  54. package/dist/cjs/routes/_load.cjs +4 -4
  55. package/dist/cjs/routes/_load.cjs.map +1 -1
  56. package/dist/cjs/routes/oauth/{callback.cjs → callback.post.cjs} +3 -4
  57. package/dist/cjs/routes/oauth/callback.post.cjs.map +1 -0
  58. package/dist/cjs/routes/oauth/{logout.cjs → logout.post.cjs} +5 -7
  59. package/dist/cjs/routes/oauth/logout.post.cjs.map +1 -0
  60. package/dist/esm/{chunk-JTFKMR4I.mjs → chunk-S573YWRP.mjs} +3 -2
  61. package/dist/esm/index.d.mts +273 -149
  62. package/dist/esm/index.mjs +5 -3
  63. package/dist/esm/index.mjs.map +1 -1
  64. package/dist/esm/lib/structures/MediaParser.mjs +1 -1
  65. package/dist/esm/lib/structures/MediaParser.mjs.map +1 -1
  66. package/dist/esm/lib/structures/MediaParserStore.mjs +1 -1
  67. package/dist/esm/lib/structures/Middleware.mjs +1 -1
  68. package/dist/esm/lib/structures/Middleware.mjs.map +1 -1
  69. package/dist/esm/lib/structures/MiddlewareStore.mjs +3 -3
  70. package/dist/esm/lib/structures/MiddlewareStore.mjs.map +1 -1
  71. package/dist/esm/lib/structures/Route.mjs +18 -35
  72. package/dist/esm/lib/structures/Route.mjs.map +1 -1
  73. package/dist/esm/lib/structures/RouteLoaderStrategy.mjs +17 -0
  74. package/dist/esm/lib/structures/RouteLoaderStrategy.mjs.map +1 -0
  75. package/dist/esm/lib/structures/RouteStore.mjs +5 -43
  76. package/dist/esm/lib/structures/RouteStore.mjs.map +1 -1
  77. package/dist/esm/lib/structures/api/ApiRequest.mjs +27 -1
  78. package/dist/esm/lib/structures/api/ApiRequest.mjs.map +1 -1
  79. package/dist/esm/lib/structures/api/ApiResponse.mjs +7 -1
  80. package/dist/esm/lib/structures/api/ApiResponse.mjs.map +1 -1
  81. package/dist/esm/lib/structures/api/CookieStore.mjs +4 -4
  82. package/dist/esm/lib/structures/api/CookieStore.mjs.map +1 -1
  83. package/dist/esm/lib/structures/http/Auth.mjs +1 -1
  84. package/dist/esm/lib/structures/http/HttpCodes.mjs +1 -1
  85. package/dist/esm/lib/structures/http/HttpMethods.mjs +39 -5
  86. package/dist/esm/lib/structures/http/HttpMethods.mjs.map +1 -1
  87. package/dist/esm/lib/structures/http/Server.mjs +16 -15
  88. package/dist/esm/lib/structures/http/Server.mjs.map +1 -1
  89. package/dist/esm/lib/structures/router/RouterBranch.mjs +179 -0
  90. package/dist/esm/lib/structures/router/RouterBranch.mjs.map +1 -0
  91. package/dist/esm/lib/structures/router/RouterNode.mjs +56 -0
  92. package/dist/esm/lib/structures/router/RouterNode.mjs.map +1 -0
  93. package/dist/esm/lib/structures/router/RouterRoot.mjs +68 -0
  94. package/dist/esm/lib/structures/router/RouterRoot.mjs.map +1 -0
  95. package/dist/esm/lib/utils/MimeTypes.mjs +1 -1
  96. package/dist/esm/listeners/PluginRouteError.mjs +4 -4
  97. package/dist/esm/listeners/PluginRouteError.mjs.map +1 -1
  98. package/dist/esm/listeners/PluginServerMiddlewareError.mjs +4 -4
  99. package/dist/esm/listeners/PluginServerMiddlewareError.mjs.map +1 -1
  100. package/dist/esm/listeners/PluginServerMiddlewareSuccess.mjs +6 -6
  101. package/dist/esm/listeners/PluginServerMiddlewareSuccess.mjs.map +1 -1
  102. package/dist/esm/listeners/PluginServerRequest.mjs +37 -9
  103. package/dist/esm/listeners/PluginServerRequest.mjs.map +1 -1
  104. package/dist/esm/listeners/PluginServerRouterBranchMethodNotAllowed.mjs +18 -0
  105. package/dist/esm/listeners/PluginServerRouterBranchMethodNotAllowed.mjs.map +1 -0
  106. package/dist/esm/listeners/{PluginServerNoMatch.mjs → PluginServerRouterBranchNotFound.mjs} +4 -4
  107. package/dist/esm/listeners/PluginServerRouterBranchNotFound.mjs.map +1 -0
  108. package/dist/esm/listeners/PluginServerRouterFound.mjs +18 -0
  109. package/dist/esm/listeners/PluginServerRouterFound.mjs.map +1 -0
  110. package/dist/esm/listeners/_load.mjs +13 -11
  111. package/dist/esm/listeners/_load.mjs.map +1 -1
  112. package/dist/esm/mediaParsers/_load.mjs +1 -1
  113. package/dist/esm/mediaParsers/applicationFormUrlEncoded.mjs +1 -1
  114. package/dist/esm/mediaParsers/applicationJson.mjs +1 -1
  115. package/dist/esm/mediaParsers/applicationJson.mjs.map +1 -1
  116. package/dist/esm/mediaParsers/textPlain.mjs +1 -1
  117. package/dist/esm/mediaParsers/textPlain.mjs.map +1 -1
  118. package/dist/esm/middlewares/_load.mjs +1 -1
  119. package/dist/esm/middlewares/auth.mjs +1 -1
  120. package/dist/esm/middlewares/auth.mjs.map +1 -1
  121. package/dist/esm/middlewares/body.mjs +5 -4
  122. package/dist/esm/middlewares/body.mjs.map +1 -1
  123. package/dist/esm/middlewares/cookies.mjs +1 -1
  124. package/dist/esm/middlewares/cookies.mjs.map +1 -1
  125. package/dist/esm/middlewares/headers.mjs +19 -9
  126. package/dist/esm/middlewares/headers.mjs.map +1 -1
  127. package/dist/esm/register.mjs +1 -1
  128. package/dist/esm/routes/_load.mjs +3 -3
  129. package/dist/esm/routes/_load.mjs.map +1 -1
  130. package/dist/esm/routes/oauth/{callback.mjs → callback.post.mjs} +4 -5
  131. package/dist/esm/routes/oauth/callback.post.mjs.map +1 -0
  132. package/dist/esm/routes/oauth/{logout.mjs → logout.post.mjs} +5 -7
  133. package/dist/esm/routes/oauth/logout.post.mjs.map +1 -0
  134. package/package.json +4 -3
  135. package/dist/cjs/lib/utils/RouteData.cjs +0 -62
  136. package/dist/cjs/lib/utils/RouteData.cjs.map +0 -1
  137. package/dist/cjs/listeners/PluginServerMatch.cjs.map +0 -1
  138. package/dist/cjs/listeners/PluginServerNoMatch.cjs.map +0 -1
  139. package/dist/cjs/routes/oauth/callback.cjs.map +0 -1
  140. package/dist/cjs/routes/oauth/logout.cjs.map +0 -1
  141. package/dist/esm/lib/utils/RouteData.mjs +0 -56
  142. package/dist/esm/lib/utils/RouteData.mjs.map +0 -1
  143. package/dist/esm/listeners/PluginServerMatch.mjs +0 -23
  144. package/dist/esm/listeners/PluginServerMatch.mjs.map +0 -1
  145. package/dist/esm/listeners/PluginServerNoMatch.mjs.map +0 -1
  146. package/dist/esm/routes/oauth/callback.mjs.map +0 -1
  147. package/dist/esm/routes/oauth/logout.mjs.map +0 -1
  148. /package/dist/esm/{chunk-JTFKMR4I.mjs.map → chunk-S573YWRP.mjs.map} +0 -0
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/lib/structures/Middleware.ts"],"names":[],"mappings":";;;;;;AAAA,SAAS,aAAa;AASf,IAAe,cAAf,MAAe,oBAA4E,MAA8B;AAAA,EAaxH,YAAY,SAAmC,UAAmB,CAAC,GAAc;AACvF,UAAM,SAAS,OAAO;AAHvB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAAgB;AAIf,SAAK,WAAW,QAAQ,YAAY;AAAA,EACrC;AASD;AAzBgI;AAAzH,IAAe,aAAf","sourcesContent":["import { Piece } from '@sapphire/pieces';\nimport type { Awaitable } from '@sapphire/utilities';\nimport type { Route } from './Route';\nimport type { ApiRequest } from './api/ApiRequest';\nimport type { ApiResponse } from './api/ApiResponse';\n\n/**\n * @since 1.0.0\n */\nexport abstract class Middleware<Options extends Middleware.Options = Middleware.Options> extends Piece<Options, 'middlewares'> {\n\t/**\n\t * The position the middleware has. The {@link MiddlewareStore} will run all middlewares with lower position than\n\t * this one.\n\t *\n\t * The built-in middlewares follow the following positions:\n\t * - headers: 10\n\t * - body: 20\n\t * - cookies: 30\n\t * - auth: 40\n\t */\n\tpublic readonly position: number;\n\n\tpublic constructor(context: Middleware.LoaderContext, options: Options = {} as Options) {\n\t\tsuper(context, options);\n\t\tthis.position = options.position ?? 1000;\n\t}\n\n\t/**\n\t * The method to be overridden by other middlewares.\n\t * @param request The client's request.\n\t * @param response The server's response.\n\t * @param route The route that matched this request, will be `null` if none matched.\n\t */\n\tpublic abstract run(request: ApiRequest, response: ApiResponse, route: Route | null): Awaitable<unknown>;\n}\n\n/**\n * The options for all middlewares.\n */\nexport interface MiddlewareOptions extends Piece.Options {\n\t/**\n\t * The position to insert the middleware at.\n\t * @see Middleware#position\n\t * @default 1000\n\t */\n\tposition?: number;\n}\n\nexport namespace Middleware {\n\t/** @deprecated Use {@linkcode LoaderContext} instead. */\n\texport type Context = LoaderContext;\n\texport type LoaderContext = Piece.LoaderContext<'middlewares'>;\n\texport type Options = MiddlewareOptions;\n\texport type JSON = Piece.JSON;\n\texport type LocationJSON = Piece.LocationJSON;\n}\n"]}
1
+ {"version":3,"sources":["../../../../src/lib/structures/Middleware.ts"],"names":[],"mappings":";;;;;;AAAA,SAAS,aAAa;AAQf,IAAe,cAAf,MAAe,oBAA4E,MAA8B;AAAA,EAaxH,YAAY,SAAmC,UAAmB,CAAC,GAAc;AACvF,UAAM,SAAS,OAAO;AAHvB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAAgB;AAIf,SAAK,WAAW,QAAQ,YAAY;AAAA,EACrC;AASD;AAzBgI;AAAzH,IAAe,aAAf","sourcesContent":["import { Piece } from '@sapphire/pieces';\nimport type { Awaitable } from '@sapphire/utilities';\nimport type { ApiRequest } from './api/ApiRequest';\nimport type { ApiResponse } from './api/ApiResponse';\n\n/**\n * @since 1.0.0\n */\nexport abstract class Middleware<Options extends Middleware.Options = Middleware.Options> extends Piece<Options, 'middlewares'> {\n\t/**\n\t * The position the middleware has. The {@link MiddlewareStore} will run all middlewares with lower position than\n\t * this one.\n\t *\n\t * The built-in middlewares follow the following positions:\n\t * - headers: 10\n\t * - body: 20\n\t * - cookies: 30\n\t * - auth: 40\n\t */\n\tpublic readonly position: number;\n\n\tpublic constructor(context: Middleware.LoaderContext, options: Options = {} as Options) {\n\t\tsuper(context, options);\n\t\tthis.position = options.position ?? 1000;\n\t}\n\n\t/**\n\t * The method to be overridden by other middlewares.\n\t * @param request The client's request.\n\t * @param response The server's response.\n\t * @param route The route that matched this request, will be `null` if none matched.\n\t */\n\tpublic abstract run(request: Middleware.Request, response: Middleware.Response): Awaitable<unknown>;\n}\n\n/**\n * The options for all middlewares.\n */\nexport interface MiddlewareOptions extends Piece.Options {\n\t/**\n\t * The position to insert the middleware at.\n\t * @see Middleware#position\n\t * @default 1000\n\t */\n\tposition?: number;\n}\n\nexport namespace Middleware {\n\t/** @deprecated Use {@linkcode LoaderContext} instead. */\n\texport type Context = LoaderContext;\n\texport type LoaderContext = Piece.LoaderContext<'middlewares'>;\n\texport type Options = MiddlewareOptions;\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"]}
@@ -15,10 +15,10 @@ var _MiddlewareStore = class _MiddlewareStore extends pieces.Store {
15
15
  */
16
16
  __publicField(this, "sortedMiddlewares", []);
17
17
  }
18
- async run(request, response, route) {
18
+ async run(request, response) {
19
19
  for (const middleware of this.sortedMiddlewares) {
20
20
  if (response.writableEnded) return;
21
- if (middleware.enabled) await middleware.run(request, response, route);
21
+ if (middleware.enabled) await middleware.run(request, response);
22
22
  }
23
23
  }
24
24
  set(key, value) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/lib/structures/MiddlewareStore.ts"],"names":[],"mappings":";;;;;;AAAA,SAAS,aAAa;AACtB,SAAS,kBAAkB;AAQpB,IAAM,mBAAN,MAAM,yBAAwB,MAAiC;AAAA,EAM9D,cAAc;AACpB,UAAM,YAAY,EAAE,MAAM,cAAc,CAAC;AAH1C;AAAA;AAAA;AAAA,wBAAgB,qBAAkC,CAAC;AAAA,EAInD;AAAA,EAEA,MAAa,IAAI,SAAqB,UAAuB,OAAoC;AAChG,eAAW,cAAc,KAAK,mBAAmB;AAChD,UAAI,SAAS,cAAe;AAC5B,UAAI,WAAW,QAAS,OAAM,WAAW,IAAI,SAAS,UAAU,KAAK;AAAA,IACtE;AAAA,EACD;AAAA,EAEgB,IAAI,KAAa,OAAyB;AACzD,UAAM,QAAQ,KAAK,kBAAkB,UAAU,CAAC,eAAe,WAAW,YAAY,MAAM,QAAQ;AAGpG,QAAI,UAAU,GAAI,MAAK,kBAAkB,KAAK,KAAK;AAAA,QAC9C,MAAK,kBAAkB,OAAO,OAAO,GAAG,KAAK;AAElD,WAAO,MAAM,IAAI,KAAK,KAAK;AAAA,EAC5B;AAAA,EAEgB,OAAO,KAAsB;AAC5C,UAAM,QAAQ,KAAK,kBAAkB,UAAU,CAAC,eAAe,WAAW,SAAS,GAAG;AAGtF,QAAI,UAAU,GAAI,MAAK,kBAAkB,OAAO,OAAO,CAAC;AAExD,WAAO,MAAM,OAAO,GAAG;AAAA,EACxB;AAAA,EAEgB,QAAc;AAC7B,SAAK,kBAAkB,SAAS;AAChC,WAAO,MAAM,MAAM;AAAA,EACpB;AACD;AAxCsE;AAA/D,IAAM,kBAAN","sourcesContent":["import { Store } from '@sapphire/pieces';\nimport { Middleware } from './Middleware';\nimport type { Route } from './Route';\nimport type { ApiRequest } from './api/ApiRequest';\nimport type { ApiResponse } from './api/ApiResponse';\n\n/**\n * @since 1.0.0\n */\nexport class MiddlewareStore extends Store<Middleware, 'middlewares'> {\n\t/**\n\t * The sorted middlewares, in ascending order of see {@link Middleware.position}.\n\t */\n\tpublic readonly sortedMiddlewares: Middleware[] = [];\n\n\tpublic constructor() {\n\t\tsuper(Middleware, { name: 'middlewares' });\n\t}\n\n\tpublic async run(request: ApiRequest, response: ApiResponse, route: Route | null): Promise<void> {\n\t\tfor (const middleware of this.sortedMiddlewares) {\n\t\t\tif (response.writableEnded) return;\n\t\t\tif (middleware.enabled) await middleware.run(request, response, route);\n\t\t}\n\t}\n\n\tpublic override set(key: string, value: Middleware): this {\n\t\tconst index = this.sortedMiddlewares.findIndex((middleware) => middleware.position >= value.position);\n\n\t\t// If a middleware with lower priority wasn't found, push to the end of the array\n\t\tif (index === -1) this.sortedMiddlewares.push(value);\n\t\telse this.sortedMiddlewares.splice(index, 0, value);\n\n\t\treturn super.set(key, value);\n\t}\n\n\tpublic override delete(key: string): boolean {\n\t\tconst index = this.sortedMiddlewares.findIndex((middleware) => middleware.name === key);\n\n\t\t// If the middleware was found, remove it\n\t\tif (index !== -1) this.sortedMiddlewares.splice(index, 1);\n\n\t\treturn super.delete(key);\n\t}\n\n\tpublic override clear(): void {\n\t\tthis.sortedMiddlewares.length = 0;\n\t\treturn super.clear();\n\t}\n}\n"]}
1
+ {"version":3,"sources":["../../../../src/lib/structures/MiddlewareStore.ts"],"names":[],"mappings":";;;;;;AAAA,SAAS,aAAa;AACtB,SAAS,kBAAkB;AAKpB,IAAM,mBAAN,MAAM,yBAAwB,MAAiC;AAAA,EAM9D,cAAc;AACpB,UAAM,YAAY,EAAE,MAAM,cAAc,CAAC;AAH1C;AAAA;AAAA;AAAA,wBAAgB,qBAAkC,CAAC;AAAA,EAInD;AAAA,EAEA,MAAa,IAAI,SAA6B,UAA8C;AAC3F,eAAW,cAAc,KAAK,mBAAmB;AAChD,UAAI,SAAS,cAAe;AAC5B,UAAI,WAAW,QAAS,OAAM,WAAW,IAAI,SAAS,QAAQ;AAAA,IAC/D;AAAA,EACD;AAAA,EAEgB,IAAI,KAAa,OAAyB;AACzD,UAAM,QAAQ,KAAK,kBAAkB,UAAU,CAAC,eAAe,WAAW,YAAY,MAAM,QAAQ;AAGpG,QAAI,UAAU,GAAI,MAAK,kBAAkB,KAAK,KAAK;AAAA,QAC9C,MAAK,kBAAkB,OAAO,OAAO,GAAG,KAAK;AAElD,WAAO,MAAM,IAAI,KAAK,KAAK;AAAA,EAC5B;AAAA,EAEgB,OAAO,KAAsB;AAC5C,UAAM,QAAQ,KAAK,kBAAkB,UAAU,CAAC,eAAe,WAAW,SAAS,GAAG;AAGtF,QAAI,UAAU,GAAI,MAAK,kBAAkB,OAAO,OAAO,CAAC;AAExD,WAAO,MAAM,OAAO,GAAG;AAAA,EACxB;AAAA,EAEgB,QAAc;AAC7B,SAAK,kBAAkB,SAAS;AAChC,WAAO,MAAM,MAAM;AAAA,EACpB;AACD;AAxCsE;AAA/D,IAAM,kBAAN","sourcesContent":["import { Store } from '@sapphire/pieces';\nimport { Middleware } from './Middleware';\n\n/**\n * @since 1.0.0\n */\nexport class MiddlewareStore extends Store<Middleware, 'middlewares'> {\n\t/**\n\t * The sorted middlewares, in ascending order of see {@link Middleware.position}.\n\t */\n\tpublic readonly sortedMiddlewares: Middleware[] = [];\n\n\tpublic constructor() {\n\t\tsuper(Middleware, { name: 'middlewares' });\n\t}\n\n\tpublic async run(request: Middleware.Request, response: Middleware.Response): Promise<void> {\n\t\tfor (const middleware of this.sortedMiddlewares) {\n\t\t\tif (response.writableEnded) return;\n\t\t\tif (middleware.enabled) await middleware.run(request, response);\n\t\t}\n\t}\n\n\tpublic override set(key: string, value: Middleware): this {\n\t\tconst index = this.sortedMiddlewares.findIndex((middleware) => middleware.position >= value.position);\n\n\t\t// If a middleware with lower priority wasn't found, push to the end of the array\n\t\tif (index === -1) this.sortedMiddlewares.push(value);\n\t\telse this.sortedMiddlewares.splice(index, 0, value);\n\n\t\treturn super.set(key, value);\n\t}\n\n\tpublic override delete(key: string): boolean {\n\t\tconst index = this.sortedMiddlewares.findIndex((middleware) => middleware.name === key);\n\n\t\t// If the middleware was found, remove it\n\t\tif (index !== -1) this.sortedMiddlewares.splice(index, 1);\n\n\t\treturn super.delete(key);\n\t}\n\n\tpublic override clear(): void {\n\t\tthis.sortedMiddlewares.length = 0;\n\t\treturn super.clear();\n\t}\n}\n"]}
@@ -1,9 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  var pieces = require('@sapphire/pieces');
4
- var discord_js = require('discord.js');
5
- var RouteData_cjs = require('../utils/RouteData.cjs');
6
- var HttpMethods_cjs = require('./http/HttpMethods.cjs');
4
+ var utilities = require('@sapphire/utilities');
5
+ var RouterRoot_cjs = require('./router/RouterRoot.cjs');
7
6
 
8
7
  var __defProp = Object.defineProperty;
9
8
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
@@ -21,46 +20,30 @@ var _Route = class _Route extends pieces.Piece {
21
20
  */
22
21
  __publicField(this, "acceptedContentMimeTypes");
23
22
  /**
24
- * The route information.
23
+ * The path this route represents.
25
24
  */
26
- __publicField(this, "router");
25
+ __publicField(this, "path");
27
26
  /**
28
27
  * The methods this route accepts.
29
28
  */
30
- __publicField(this, "methods", new discord_js.Collection());
29
+ __publicField(this, "methods");
31
30
  const api = this.container.server.options;
32
- const prefix = api.prefix ? api.prefix.endsWith("/") ? api.prefix : `${api.prefix}/` : "";
33
- const path = options.route ?? (this.location.virtual ? this.name : this.location.directories.concat(this.name).join("/"));
34
- this.router = new RouteData_cjs.RouteData(`${prefix}${path}`);
35
- for (const [method, symbol] of HttpMethods_cjs.methodEntries) {
36
- const value = Reflect.get(this, symbol);
37
- if (typeof value === "function") this.methods.set(method, value);
31
+ const path = [].concat(
32
+ RouterRoot_cjs.RouterRoot.normalize(api.prefix),
33
+ RouterRoot_cjs.RouterRoot.normalize(options.route ?? (this.location.virtual ? this.name : this.location.directories.concat(this.name).join("/")))
34
+ );
35
+ const methods = new Set(options.methods);
36
+ const implied = RouterRoot_cjs.RouterRoot.extractMethod(path);
37
+ if (!utilities.isNullish(implied)) {
38
+ const lastIndex = path.length - 1;
39
+ path[lastIndex] = path[lastIndex].slice(0, path[lastIndex].length - implied.length - 1);
40
+ methods.add(implied);
38
41
  }
42
+ this.path = path;
43
+ this.methods = methods;
39
44
  this.maximumBodyLength = options.maximumBodyLength ?? api.maximumBodyLength ?? 1024 * 1024 * 50;
40
45
  this.acceptedContentMimeTypes = options.acceptedContentMimeTypes ?? api.acceptedContentMimeTypes ?? null;
41
46
  }
42
- /**
43
- * Per-piece listener that is called when the piece is loaded into the store.
44
- * Useful to set-up asynchronous initialization tasks.
45
- */
46
- onLoad() {
47
- const store = this.store;
48
- for (const [method, cb] of this.methods) {
49
- store.table.get(method).set(this, cb.bind(this));
50
- }
51
- return void 0;
52
- }
53
- /**
54
- * Per-piece listener that is called when the piece is unloaded from the store.
55
- * Useful to set-up clean-up tasks.
56
- */
57
- onUnload() {
58
- const store = this.store;
59
- for (const [method] of this.methods) {
60
- store.table.get(method).delete(this);
61
- }
62
- return void 0;
63
- }
64
47
  };
65
48
  __name(_Route, "Route");
66
49
  var Route = _Route;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/lib/structures/Route.ts"],"names":[],"mappings":";;;;;;AAAA,SAAS,aAAa;AAEtB,SAAS,kBAAkB;AAC3B,SAAS,iBAAiB;AAI1B,SAAS,qBAAmC;AAMrC,IAAe,SAAf,MAAe,eAA6D,MAAyB;AAAA,EAqBpG,YAAY,SAA8B,UAAmB,CAAC,GAAc;AAClF,UAAM,SAAS,OAAO;AAlBvB;AAAA;AAAA;AAAA,wBAAgB;AAKhB;AAAA;AAAA;AAAA,wBAAgB;AAKhB;AAAA;AAAA;AAAA,wBAAgB;AAKhB;AAAA;AAAA;AAAA,wBAAgB,WAAU,IAAI,WAAoC;AAKjE,UAAM,MAAM,KAAK,UAAU,OAAO;AAElC,UAAM,SAAS,IAAI,SAAU,IAAI,OAAO,SAAS,GAAG,IAAI,IAAI,SAAS,GAAG,IAAI,MAAM,MAAO;AAIzF,UAAM,OAAO,QAAQ,UAAU,KAAK,SAAS,UAAU,KAAK,OAAO,KAAK,SAAS,YAAY,OAAO,KAAK,IAAI,EAAE,KAAK,GAAG;AACvH,SAAK,SAAS,IAAI,UAAU,GAAG,MAAM,GAAG,IAAI,EAAE;AAE9C,eAAW,CAAC,QAAQ,MAAM,KAAK,eAAe;AAC7C,YAAM,QAAQ,QAAQ,IAAI,MAAM,MAAM;AACtC,UAAI,OAAO,UAAU,WAAY,MAAK,QAAQ,IAAI,QAAQ,KAAK;AAAA,IAChE;AAEA,SAAK,oBAAoB,QAAQ,qBAAqB,IAAI,qBAAqB,OAAO,OAAO;AAC7F,SAAK,2BAA2B,QAAQ,4BAA4B,IAAI,4BAA4B;AAAA,EACrG;AAAA;AAAA;AAAA;AAAA;AAAA,EAMgB,SAA6B;AAC5C,UAAM,QAAQ,KAAK;AAEnB,eAAW,CAAC,QAAQ,EAAE,KAAK,KAAK,SAAS;AACxC,YAAM,MAAM,IAAI,MAAM,EAAG,IAAI,MAAM,GAAG,KAAK,IAAI,CAAC;AAAA,IACjD;AAEA,WAAO;AAAA,EACR;AAAA;AAAA;AAAA;AAAA;AAAA,EAMgB,WAA+B;AAC9C,UAAM,QAAQ,KAAK;AAEnB,eAAW,CAAC,MAAM,KAAK,KAAK,SAAS;AACpC,YAAM,MAAM,IAAI,MAAM,EAAG,OAAO,IAAI;AAAA,IACrC;AAEA,WAAO;AAAA,EACR;AACD;AArE4G;AAArG,IAAe,QAAf","sourcesContent":["import { Piece } from '@sapphire/pieces';\nimport type { Awaitable } from '@sapphire/utilities';\nimport { Collection } from 'discord.js';\nimport { RouteData } from '../utils/RouteData';\nimport type { MethodCallback, RouteStore } from './RouteStore';\nimport type { ApiRequest } from './api/ApiRequest';\nimport type { ApiResponse } from './api/ApiResponse';\nimport { methodEntries, type Methods } from './http/HttpMethods';\nimport type { MimeTypeWithoutParameters } from './http/Server';\n\n/**\n * @since 1.0.0\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 accepted content types.\n\t */\n\tpublic readonly acceptedContentMimeTypes: readonly MimeTypeWithoutParameters[] | null;\n\n\t/**\n\t * The route information.\n\t */\n\tpublic readonly router: RouteData;\n\n\t/**\n\t * The methods this route accepts.\n\t */\n\tpublic readonly methods = new Collection<Methods, MethodCallback>();\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\t// Concat a `/` to the prefix if it does not end with it\n\t\tconst prefix = api.prefix ? (api.prefix.endsWith('/') ? api.prefix : `${api.prefix}/`) : '';\n\t\t// Use the defined route, otherwise:\n\t\t// - If the location is virtual, use the name.\n\t\t// - Otherwise, use the directories and the name.\n\t\tconst path = options.route ?? (this.location.virtual ? this.name : this.location.directories.concat(this.name).join('/'));\n\t\tthis.router = new RouteData(`${prefix}${path}`);\n\n\t\tfor (const [method, symbol] of methodEntries) {\n\t\t\tconst value = Reflect.get(this, symbol) as MethodCallback;\n\t\t\tif (typeof value === 'function') this.methods.set(method, value);\n\t\t}\n\n\t\tthis.maximumBodyLength = options.maximumBodyLength ?? api.maximumBodyLength ?? 1024 * 1024 * 50;\n\t\tthis.acceptedContentMimeTypes = options.acceptedContentMimeTypes ?? api.acceptedContentMimeTypes ?? null;\n\t}\n\n\t/**\n\t * Per-piece listener that is called when the piece is loaded into the store.\n\t * Useful to set-up asynchronous initialization tasks.\n\t */\n\tpublic override onLoad(): Awaitable<unknown> {\n\t\tconst store = this.store as unknown as RouteStore;\n\n\t\tfor (const [method, cb] of this.methods) {\n\t\t\tstore.table.get(method)!.set(this, cb.bind(this));\n\t\t}\n\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Per-piece listener that is called when the piece is unloaded from the store.\n\t * Useful to set-up clean-up tasks.\n\t */\n\tpublic override onUnload(): Awaitable<unknown> {\n\t\tconst store = this.store as unknown as RouteStore;\n\n\t\tfor (const [method] of this.methods) {\n\t\t\tstore.table.get(method)!.delete(this);\n\t\t}\n\n\t\treturn undefined;\n\t}\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 * @default ''\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 * @default this.context.server.options.maximumBodyLength ?? 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 this.context.server.options.acceptedContentMimeTypes ?? null\n\t */\n\tacceptedContentMimeTypes?: MimeTypeWithoutParameters[] | null;\n}\n\nexport namespace Route {\n\t/** @deprecated Use {@linkcode LoaderContext} instead. */\n\texport type Context = LoaderContext;\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
+ {"version":3,"sources":["../../../../src/lib/structures/Route.ts"],"names":[],"mappings":";;;;;;AAAA,SAAS,aAAa;AACtB,SAAS,iBAAiC;AAK1C,SAAS,kBAAkB;AAuCpB,IAAe,SAAf,MAAe,eAA6D,MAAyB;AAAA,EAqBpG,YAAY,SAA8B,UAAmB,CAAC,GAAc;AAClF,UAAM,SAAS,OAAO;AAlBvB;AAAA;AAAA;AAAA,wBAAgB;AAKhB;AAAA;AAAA;AAAA,wBAAgB;AAKhB;AAAA;AAAA;AAAA,wBAAgB;AAKhB;AAAA;AAAA;AAAA,wBAAgB;AAKf,UAAM,MAAM,KAAK,UAAU,OAAO;AAClC,UAAM,OAAQ,CAAC,EAAe;AAAA,MAC7B,WAAW,UAAU,IAAI,MAAM;AAAA,MAC/B,WAAW,UAAU,QAAQ,UAAU,KAAK,SAAS,UAAU,KAAK,OAAO,KAAK,SAAS,YAAY,OAAO,KAAK,IAAI,EAAE,KAAK,GAAG,EAAE;AAAA,IAClI;AAEA,UAAM,UAAU,IAAI,IAAI,QAAQ,OAAO;AACvC,UAAM,UAAU,WAAW,cAAc,IAAI;AAC7C,QAAI,CAAC,UAAU,OAAO,GAAG;AACxB,YAAM,YAAY,KAAK,SAAS;AAChC,WAAK,SAAS,IAAI,KAAK,SAAS,EAAE,MAAM,GAAG,KAAK,SAAS,EAAE,SAAS,QAAQ,SAAS,CAAC;AACtF,cAAQ,IAAI,OAAqB;AAAA,IAClC;AAEA,SAAK,OAAO;AACZ,SAAK,UAAU;AACf,SAAK,oBAAoB,QAAQ,qBAAqB,IAAI,qBAAqB,OAAO,OAAO;AAC7F,SAAK,2BAA2B,QAAQ,4BAA4B,IAAI,4BAA4B;AAAA,EACrG;AAGD;AA7C4G;AAArG,IAAe,QAAf","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 type { MimeTypeWithoutParameters } from './http/Server';\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 accepted content types.\n\t */\n\tpublic readonly acceptedContentMimeTypes: readonly MimeTypeWithoutParameters[] | null;\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 = ([] as string[]).concat(\n\t\t\tRouterRoot.normalize(api.prefix),\n\t\t\tRouterRoot.normalize(options.route ?? (this.location.virtual ? this.name : this.location.directories.concat(this.name).join('/')))\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 as MethodName);\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\tthis.acceptedContentMimeTypes = options.acceptedContentMimeTypes ?? api.acceptedContentMimeTypes ?? null;\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 accepted content types for this route. If set to null, the route will accept any data.\n\t * @since 1.3.0\n\t *\n\t * @defaultValue this.context.server.options.acceptedContentMimeTypes ?? null\n\t */\n\tacceptedContentMimeTypes?: readonly MimeTypeWithoutParameters[] | null;\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;\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"]}
@@ -0,0 +1,20 @@
1
+ 'use strict';
2
+
3
+ var pieces = require('@sapphire/pieces');
4
+
5
+ var __defProp = Object.defineProperty;
6
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
+ var _RouteLoaderStrategy = class _RouteLoaderStrategy extends pieces.LoaderStrategy {
8
+ onLoad(store, piece) {
9
+ store.router.add(piece);
10
+ }
11
+ onUnload(store, piece) {
12
+ store.router.remove(piece);
13
+ }
14
+ };
15
+ __name(_RouteLoaderStrategy, "RouteLoaderStrategy");
16
+ var RouteLoaderStrategy = _RouteLoaderStrategy;
17
+
18
+ exports.RouteLoaderStrategy = RouteLoaderStrategy;
19
+ //# sourceMappingURL=out.js.map
20
+ //# sourceMappingURL=RouteLoaderStrategy.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/lib/structures/RouteLoaderStrategy.ts"],"names":[],"mappings":";;;;AAAA,SAAS,sBAAsB;AAIxB,IAAM,uBAAN,MAAM,6BAA4B,eAAsB;AAAA,EAC9C,OAAO,OAAmB,OAAoB;AAC7D,UAAM,OAAO,IAAI,KAAK;AAAA,EACvB;AAAA,EAEgB,SAAS,OAAmB,OAAoB;AAC/D,UAAM,OAAO,OAAO,KAAK;AAAA,EAC1B;AACD;AAR+D;AAAxD,IAAM,sBAAN","sourcesContent":["import { LoaderStrategy } from '@sapphire/pieces';\nimport type { Route } from './Route';\nimport type { RouteStore } from './RouteStore';\n\nexport class RouteLoaderStrategy extends LoaderStrategy<Route> {\n\tpublic override onLoad(store: RouteStore, piece: Route): void {\n\t\tstore.router.add(piece);\n\t}\n\n\tpublic override onUnload(store: RouteStore, piece: Route): void {\n\t\tstore.router.remove(piece);\n\t}\n}\n"]}
@@ -1,56 +1,18 @@
1
1
  'use strict';
2
2
 
3
3
  var pieces = require('@sapphire/pieces');
4
- var discord_js = require('discord.js');
5
- var url = require('url');
6
- var HttpMethods_cjs = require('./http/HttpMethods.cjs');
7
4
  var Route_cjs = require('./Route.cjs');
5
+ var RouteLoaderStrategy_cjs = require('./RouteLoaderStrategy.cjs');
6
+ var RouterRoot_cjs = require('./router/RouterRoot.cjs');
8
7
 
9
8
  var __defProp = Object.defineProperty;
10
9
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
10
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
12
11
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
13
- var slash = "/".charCodeAt(0);
14
12
  var _RouteStore = class _RouteStore extends pieces.Store {
15
13
  constructor() {
16
- super(Route_cjs.Route, { name: "routes" });
17
- __publicField(this, "table", new discord_js.Collection());
18
- for (const [method] of HttpMethods_cjs.methodEntries) this.table.set(method, new discord_js.Collection());
19
- }
20
- match(request) {
21
- const { method } = request;
22
- if (typeof method === "undefined") {
23
- return null;
24
- }
25
- const methodTable = this.table.get(method);
26
- if (typeof methodTable === "undefined") {
27
- return null;
28
- }
29
- const { splits, querystring } = this.parseURL(request.url);
30
- for (const [route, cb] of methodTable.entries()) {
31
- const result = route.router.match(splits);
32
- if (result === null) continue;
33
- request.params = result;
34
- request.query = Object.fromEntries(new url.URLSearchParams(querystring).entries());
35
- return { route, cb };
36
- }
37
- return null;
38
- }
39
- parseURL(url = "") {
40
- const index = url.indexOf("?");
41
- let pathname;
42
- let querystring;
43
- if (index === -1) {
44
- pathname = url;
45
- querystring = "";
46
- } else {
47
- pathname = url.substring(0, index);
48
- querystring = url.substring(index + 1);
49
- }
50
- if (pathname.charCodeAt(0) === slash) pathname = pathname.substring(1);
51
- if (pathname.length > 0 && pathname.charCodeAt(pathname.length - 1) === slash) pathname = pathname.substring(0, pathname.length - 1);
52
- const splits = pathname.split("/");
53
- return { splits, querystring };
14
+ super(Route_cjs.Route, { name: "routes", strategy: new RouteLoaderStrategy_cjs.RouteLoaderStrategy() });
15
+ __publicField(this, "router", new RouterRoot_cjs.RouterRoot());
54
16
  }
55
17
  };
56
18
  __name(_RouteStore, "RouteStore");
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/lib/structures/RouteStore.ts"],"names":[],"mappings":";;;;;;AAAA,SAAS,aAAa;AACtB,SAAS,kBAAkB;AAC3B,SAAS,uBAAuB;AAGhC,SAAS,qBAAmC;AAC5C,SAAS,aAAa;AAEtB,IAAM,QAAQ,IAAI,WAAW,CAAC;AAcvB,IAAM,cAAN,MAAM,oBAAmB,MAAuB;AAAA,EAG/C,cAAc;AACpB,UAAM,OAAO,EAAE,MAAM,SAAS,CAAC;AAHhC,wBAAgB,SAAQ,IAAI,WAAuD;AAKlF,eAAW,CAAC,MAAM,KAAK,cAAe,MAAK,MAAM,IAAI,QAAQ,IAAI,WAAW,CAAC;AAAA,EAC9E;AAAA,EAEO,MAAM,SAAwC;AACpD,UAAM,EAAE,OAAO,IAAI;AAGnB,QAAI,OAAO,WAAW,aAAa;AAClC,aAAO;AAAA,IACR;AAGA,UAAM,cAAc,KAAK,MAAM,IAAI,MAAiB;AAGpD,QAAI,OAAO,gBAAgB,aAAa;AACvC,aAAO;AAAA,IACR;AAEA,UAAM,EAAE,QAAQ,YAAY,IAAI,KAAK,SAAS,QAAQ,GAAG;AAEzD,eAAW,CAAC,OAAO,EAAE,KAAK,YAAY,QAAQ,GAAG;AAChD,YAAM,SAAS,MAAM,OAAO,MAAM,MAAM;AACxC,UAAI,WAAW,KAAM;AAErB,cAAQ,SAAS;AACjB,cAAQ,QAAQ,OAAO,YAAY,IAAI,gBAAgB,WAAW,EAAE,QAAQ,CAAC;AAE7E,aAAO,EAAE,OAAO,GAAG;AAAA,IACpB;AAEA,WAAO;AAAA,EACR;AAAA,EAEQ,SAAS,MAAM,IAAI;AAC1B,UAAM,QAAQ,IAAI,QAAQ,GAAG;AAG7B,QAAI;AACJ,QAAI;AAEJ,QAAI,UAAU,IAAI;AACjB,iBAAW;AACX,oBAAc;AAAA,IACf,OAAO;AACN,iBAAW,IAAI,UAAU,GAAG,KAAK;AACjC,oBAAc,IAAI,UAAU,QAAQ,CAAC;AAAA,IACtC;AAEA,QAAI,SAAS,WAAW,CAAC,MAAM,MAAO,YAAW,SAAS,UAAU,CAAC;AACrE,QAAI,SAAS,SAAS,KAAK,SAAS,WAAW,SAAS,SAAS,CAAC,MAAM,MAAO,YAAW,SAAS,UAAU,GAAG,SAAS,SAAS,CAAC;AAEnI,UAAM,SAAS,SAAS,MAAM,GAAG;AAEjC,WAAO,EAAE,QAAQ,YAAY;AAAA,EAC9B;AACD;AA9DuD;AAAhD,IAAM,aAAN","sourcesContent":["import { Store } from '@sapphire/pieces';\nimport { Collection } from 'discord.js';\nimport { URLSearchParams } from 'url';\nimport type { ApiRequest } from './api/ApiRequest';\nimport type { ApiResponse } from './api/ApiResponse';\nimport { methodEntries, type Methods } from './http/HttpMethods';\nimport { Route } from './Route';\n\nconst slash = '/'.charCodeAt(0);\n\nexport interface MethodCallback {\n\t(request: ApiRequest, response: ApiResponse): unknown;\n}\n\nexport interface RouteMatch {\n\troute: Route;\n\tcb: MethodCallback;\n}\n\n/**\n * @since 1.0.0\n */\nexport class RouteStore extends Store<Route, 'routes'> {\n\tpublic readonly table = new Collection<Methods, Collection<Route, MethodCallback>>();\n\n\tpublic constructor() {\n\t\tsuper(Route, { name: 'routes' });\n\n\t\tfor (const [method] of methodEntries) this.table.set(method, new Collection());\n\t}\n\n\tpublic match(request: ApiRequest): RouteMatch | null {\n\t\tconst { method } = request;\n\n\t\t// If there is no method, we can't match a route so return null\n\t\tif (typeof method === 'undefined') {\n\t\t\treturn null;\n\t\t}\n\n\t\t// We get all the methods that are tied to the provided method to have a smaller list to filter through\n\t\tconst methodTable = this.table.get(method as Methods);\n\n\t\t// If there are no methods of the provided type then we won't find any route so we return null\n\t\tif (typeof methodTable === 'undefined') {\n\t\t\treturn null;\n\t\t}\n\n\t\tconst { splits, querystring } = this.parseURL(request.url);\n\n\t\tfor (const [route, cb] of methodTable.entries()) {\n\t\t\tconst result = route.router.match(splits);\n\t\t\tif (result === null) continue;\n\n\t\t\trequest.params = result;\n\t\t\trequest.query = Object.fromEntries(new URLSearchParams(querystring).entries());\n\n\t\t\treturn { route, cb };\n\t\t}\n\n\t\treturn null;\n\t}\n\n\tprivate parseURL(url = '') {\n\t\tconst index = url.indexOf('?');\n\n\t\t/* eslint-disable @typescript-eslint/init-declarations */\n\t\tlet pathname: string;\n\t\tlet querystring: string;\n\t\t/* eslint-enable @typescript-eslint/init-declarations */\n\t\tif (index === -1) {\n\t\t\tpathname = url;\n\t\t\tquerystring = '';\n\t\t} else {\n\t\t\tpathname = url.substring(0, index);\n\t\t\tquerystring = url.substring(index + 1);\n\t\t}\n\n\t\tif (pathname.charCodeAt(0) === slash) pathname = pathname.substring(1);\n\t\tif (pathname.length > 0 && pathname.charCodeAt(pathname.length - 1) === slash) pathname = pathname.substring(0, pathname.length - 1);\n\n\t\tconst splits = pathname.split('/');\n\n\t\treturn { splits, querystring };\n\t}\n}\n"]}
1
+ {"version":3,"sources":["../../../../src/lib/structures/RouteStore.ts"],"names":[],"mappings":";;;;;;AAAA,SAAS,aAAa;AACtB,SAAS,aAAa;AACtB,SAAS,2BAA2B;AACpC,SAAS,kBAAkB;AAKpB,IAAM,cAAN,MAAM,oBAAmB,MAAuB;AAAA,EAG/C,cAAc;AACpB,UAAM,OAAO,EAAE,MAAM,UAAU,UAAU,IAAI,oBAAoB,EAAE,CAAC;AAHrE,wBAAgB,UAAS,IAAI,WAAW;AAAA,EAIxC;AACD;AANuD;AAAhD,IAAM,aAAN","sourcesContent":["import { Store } from '@sapphire/pieces';\nimport { Route } from './Route';\nimport { RouteLoaderStrategy } from './RouteLoaderStrategy';\nimport { RouterRoot } from './router/RouterRoot';\n\n/**\n * @since 1.0.0\n */\nexport class RouteStore extends Store<Route, 'routes'> {\n\tpublic readonly router = new RouterRoot();\n\n\tpublic constructor() {\n\t\tsuper(Route, { name: 'routes', strategy: new RouteLoaderStrategy() });\n\t}\n}\n"]}
@@ -29,6 +29,32 @@ var _ApiRequest = class _ApiRequest extends http.IncomingMessage {
29
29
  * - `AuthData`: The user is authorized.
30
30
  */
31
31
  __publicField(this, "auth");
32
+ /**
33
+ * The router node that matched the request. The field indicates three
34
+ * possible values:
35
+ *
36
+ * - `undefined`: The router handler has not been executed yet.
37
+ * - `null`: The router handler has been executed, but no node matched the
38
+ * request.
39
+ * - `RouterNode`: The router handler has been executed and a node matched
40
+ * the request.
41
+ *
42
+ * @since 7.0.0
43
+ */
44
+ __publicField(this, "routerNode");
45
+ /**
46
+ * The route that matched the request. The field indicates three possible
47
+ * values:
48
+ *
49
+ * - `undefined`: The router handler has not been executed yet.
50
+ * - `null`: The router handler has been executed, but no route matched the
51
+ * request.
52
+ * - `Route`: The router handler has been executed and a route matched the
53
+ * request.
54
+ *
55
+ * @since 7.0.0
56
+ */
57
+ __publicField(this, "route");
32
58
  }
33
59
  };
34
60
  __name(_ApiRequest, "ApiRequest");
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../src/lib/structures/api/ApiRequest.ts"],"names":[],"mappings":";;;;;;AAAA,SAAS,uBAAuB;AAGzB,IAAM,cAAN,MAAM,oBAAmB,gBAAgB;AAAA,EAAzC;AAAA;AAIN;AAAA;AAAA;AAAA,wBAAO,SAA2C,CAAC;AAKnD;AAAA;AAAA;AAAA,wBAAO,UAAiC,CAAC;AAKzC;AAAA;AAAA;AAAA,wBAAO;AASP;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAAO;AAAA;AACR;AAxBgD;AAAzC,IAAM,aAAN","sourcesContent":["import { IncomingMessage } from 'node:http';\nimport type { AuthData } from '../http/Auth';\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 body that was sent by the user.\n\t */\n\tpublic body?: unknown;\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"]}
1
+ {"version":3,"sources":["../../../../../src/lib/structures/api/ApiRequest.ts"],"names":[],"mappings":";;;;;;AAAA,SAAS,uBAAuB;AAKzB,IAAM,cAAN,MAAM,oBAAmB,gBAAgB;AAAA,EAAzC;AAAA;AAIN;AAAA;AAAA;AAAA,wBAAO,SAA2C,CAAC;AAKnD;AAAA;AAAA;AAAA,wBAAO,UAAiC,CAAC;AAKzC;AAAA;AAAA;AAAA,wBAAO;AASP;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAAO;AAcP;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAAO;AAcP;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAAO;AAAA;AACR;AApDgD;AAAzC,IAAM,aAAN","sourcesContent":["import { IncomingMessage } from 'node:http';\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 body that was sent by the user.\n\t */\n\tpublic body?: unknown;\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"]}
@@ -63,6 +63,12 @@ var _ApiResponse = class _ApiResponse extends http.ServerResponse {
63
63
  notFound(data) {
64
64
  return this.error(HttpCodes_cjs.HttpCodes.NotFound, data);
65
65
  }
66
+ /**
67
+ * @since 7.0.0
68
+ */
69
+ methodNotAllowed(data) {
70
+ return this.error(HttpCodes_cjs.HttpCodes.MethodNotAllowed, data);
71
+ }
66
72
  /**
67
73
  * @since 1.0.0
68
74
  */
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../src/lib/structures/api/ApiResponse.ts"],"names":[],"mappings":";;;;;;AAAA,SAA0B,gBAAgB,oBAAoB;AAC9D,SAAS,gBAAgB;AACzB,OAA+B;AAC/B,SAAS,iBAAiB;AAC1B,SAAS,iBAAiB;AAMnB,IAAM,eAAN,MAAM,qBAAuE,eAAwB;AAAA,EAArG;AAAA;AAIN;AAAA;AAAA;AAAA,wBAAO;AAAA;AAAA;AAAA;AAAA;AAAA,EAKA,GAAG,OAAgB,aAAa,UAAU,EAAE,GAAS;AAC3D,SAAK,OAAO,UAAU,EAAE;AACxB,WAAO,KAAK,QAAQ,IAAI;AAAA,EACzB;AAAA;AAAA;AAAA;AAAA,EAKO,QAAQ,OAAgB,aAAa,UAAU,OAAO,GAAS;AACrE,SAAK,OAAO,UAAU,OAAO;AAC7B,WAAO,KAAK,QAAQ,IAAI;AAAA,EACzB;AAAA;AAAA;AAAA;AAAA,EAKO,UAAU,OAAgB,aAAa,UAAU,SAAS,GAAS;AACzE,SAAK,OAAO,UAAU,SAAS;AAC/B,WAAO,KAAK,QAAQ,IAAI;AAAA,EACzB;AAAA;AAAA;AAAA;AAAA,EAKO,WAAW,MAAsB;AACvC,WAAO,KAAK,MAAM,UAAU,YAAY,IAAI;AAAA,EAC7C;AAAA;AAAA;AAAA;AAAA,EAKO,aAAa,MAAsB;AACzC,WAAO,KAAK,MAAM,UAAU,cAAc,IAAI;AAAA,EAC/C;AAAA;AAAA;AAAA;AAAA,EAKO,UAAU,MAAsB;AACtC,WAAO,KAAK,MAAM,UAAU,WAAW,IAAI;AAAA,EAC5C;AAAA;AAAA;AAAA;AAAA,EAKO,SAAS,MAAsB;AACrC,WAAO,KAAK,MAAM,UAAU,UAAU,IAAI;AAAA,EAC3C;AAAA;AAAA;AAAA;AAAA,EAKO,SAAS,MAAgB;AAC/B,WAAO,KAAK,MAAM,UAAU,UAAU,IAAI;AAAA,EAC3C;AAAA;AAAA;AAAA;AAAA,EAKO,MAAM,OAAwB,MAAsB;AAC1D,QAAI,OAAO,UAAU,UAAU;AAC9B,aAAO,KAAK,OAAO,UAAU,mBAAmB,EAAE,KAAK,EAAE,MAAM,CAAC;AAAA,IACjE;AAEA,WAAO,KAAK,OAAO,KAAK,EAAE,KAAK,EAAE,OAAO,QAAQ,aAAa,KAAK,EAAE,CAAC;AAAA,EACtE;AAAA;AAAA;AAAA;AAAA,EAKO,QAAQ,MAAqB;AACnC,WAAO,OAAO,SAAS,WAAW,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,IAAI;AAAA,EACnE;AAAA;AAAA;AAAA;AAAA,EAKO,OAAO,MAAoB;AACjC,SAAK,aAAa;AAClB,WAAO;AAAA,EACR;AAAA;AAAA;AAAA;AAAA,EAKO,KAAK,MAAiB;AAC5B,SAAK,eAAe,UAAU,eAAe,EAAE,IAAI,KAAK,UAAU,IAAI,CAAC;AAAA,EACxE;AAAA;AAAA;AAAA;AAAA,EAKO,KAAK,MAAoB;AAC/B,SAAK,eAAe,UAAU,SAAS,EAAE,IAAI,IAAI;AAAA,EAClD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUO,MACN,MACA,MACO;AACP,QAAI,gBAAgB,UAAU;AAC7B,WAAK,eAAe,IAAI;AACxB,WAAK,KAAK,IAAI;AAAA,IACf,OAAO;AACN,WAAK,eAAe,IAAI,EAAE,IAAI,IAAI;AAAA,IACnC;AAAA,EACD;AAAA;AAAA;AAAA;AAAA,EAKO,KAAK,MAAc,MAAoB;AAC7C,SAAK,eAAe,UAAU,QAAQ,EAAE,OAAO,IAAI,EAAE,IAAI,IAAI;AAAA,EAC9D;AAAA;AAAA;AAAA;AAAA,EAKO,eAAe,aAA8B;AACnD,SAAK,UAAU,gBAAgB,WAAW;AAC1C,WAAO;AAAA,EACR;AACD;AA3I4G;AAArG,IAAM,cAAN","sourcesContent":["import { IncomingMessage, ServerResponse, STATUS_CODES } from 'node:http';\nimport { Readable } from 'node:stream';\nimport { ReadableStream } from 'node:stream/web';\nimport { MimeTypes } from '../../utils/MimeTypes';\nimport { HttpCodes } from '../http/HttpCodes';\nimport type { CookieStore } from './CookieStore';\n\n/**\n * @since 1.0.0\n */\nexport class ApiResponse<Request extends IncomingMessage = IncomingMessage> extends ServerResponse<Request> {\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic cookies!: CookieStore;\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic ok(data: unknown = STATUS_CODES[HttpCodes.OK]): void {\n\t\tthis.status(HttpCodes.OK);\n\t\treturn this.respond(data);\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic created(data: unknown = STATUS_CODES[HttpCodes.Created]): void {\n\t\tthis.status(HttpCodes.Created);\n\t\treturn this.respond(data);\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic noContent(data: unknown = STATUS_CODES[HttpCodes.NoContent]): void {\n\t\tthis.status(HttpCodes.NoContent);\n\t\treturn this.respond(data);\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic badRequest(data?: unknown): void {\n\t\treturn this.error(HttpCodes.BadRequest, data);\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic unauthorized(data?: unknown): void {\n\t\treturn this.error(HttpCodes.Unauthorized, data);\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic forbidden(data?: unknown): void {\n\t\treturn this.error(HttpCodes.Forbidden, data);\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic notFound(data?: unknown): void {\n\t\treturn this.error(HttpCodes.NotFound, data);\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic conflict(data?: unknown) {\n\t\treturn this.error(HttpCodes.Conflict, data);\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic error(error: number | string, data?: unknown): void {\n\t\tif (typeof error === 'string') {\n\t\t\treturn this.status(HttpCodes.InternalServerError).json({ error });\n\t\t}\n\n\t\treturn this.status(error).json({ error: data ?? STATUS_CODES[error] });\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic respond(data: unknown): void {\n\t\treturn typeof data === 'string' ? this.text(data) : this.json(data);\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic status(code: number): this {\n\t\tthis.statusCode = code;\n\t\treturn this;\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic json(data: any): void {\n\t\tthis.setContentType(MimeTypes.ApplicationJson).end(JSON.stringify(data));\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic text(data: string): void {\n\t\tthis.setContentType(MimeTypes.TextPlain).end(data);\n\t}\n\n\t/**\n\t * @since 6.1.0\n\t *\n\t * Sets the image content type and sends the image data in the response.\n\t *\n\t * @param type - The MIME type of the image (e.g., {@link MimeTypes.ImagePng}).\n\t * @param data - The image data as a `string`, {@link Buffer}, {@link Uint8Array}, or {@link ReadableStream}.\n\t */\n\tpublic image(\n\t\ttype: MimeTypes.ImageGif | MimeTypes.ImageJpg | MimeTypes.ImagePng | MimeTypes.ImageWebp | MimeTypes.ImageXIcon,\n\t\tdata: string | Buffer | Uint8Array | Readable\n\t): void {\n\t\tif (data instanceof Readable) {\n\t\t\tthis.setContentType(type);\n\t\t\tdata.pipe(this);\n\t\t} else {\n\t\t\tthis.setContentType(type).end(data);\n\t\t}\n\t}\n\n\t/**\n\t * @since 5.1.0\n\t */\n\tpublic html(code: number, data: string): void {\n\t\tthis.setContentType(MimeTypes.TextHtml).status(code).end(data);\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic setContentType(contentType: MimeTypes): this {\n\t\tthis.setHeader('Content-Type', contentType);\n\t\treturn this;\n\t}\n}\n"]}
1
+ {"version":3,"sources":["../../../../../src/lib/structures/api/ApiResponse.ts"],"names":[],"mappings":"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","sourcesContent":["import { IncomingMessage, ServerResponse, STATUS_CODES } from 'node:http';\nimport { Readable } from 'node:stream';\nimport { ReadableStream } from 'node:stream/web';\nimport { MimeTypes } from '../../utils/MimeTypes';\nimport { HttpCodes } from '../http/HttpCodes';\nimport type { CookieStore } from './CookieStore';\n\n/**\n * @since 1.0.0\n */\nexport class ApiResponse<Request extends IncomingMessage = IncomingMessage> extends ServerResponse<Request> {\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic cookies!: CookieStore;\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic ok(data: unknown = STATUS_CODES[HttpCodes.OK]): void {\n\t\tthis.status(HttpCodes.OK);\n\t\treturn this.respond(data);\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic created(data: unknown = STATUS_CODES[HttpCodes.Created]): void {\n\t\tthis.status(HttpCodes.Created);\n\t\treturn this.respond(data);\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic noContent(data: unknown = STATUS_CODES[HttpCodes.NoContent]): void {\n\t\tthis.status(HttpCodes.NoContent);\n\t\treturn this.respond(data);\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic badRequest(data?: unknown): void {\n\t\treturn this.error(HttpCodes.BadRequest, data);\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic unauthorized(data?: unknown): void {\n\t\treturn this.error(HttpCodes.Unauthorized, data);\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic forbidden(data?: unknown): void {\n\t\treturn this.error(HttpCodes.Forbidden, data);\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic notFound(data?: unknown): void {\n\t\treturn this.error(HttpCodes.NotFound, data);\n\t}\n\n\t/**\n\t * @since 7.0.0\n\t */\n\tpublic methodNotAllowed(data?: unknown): void {\n\t\treturn this.error(HttpCodes.MethodNotAllowed, data);\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic conflict(data?: unknown) {\n\t\treturn this.error(HttpCodes.Conflict, data);\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic error(error: number | string, data?: unknown): void {\n\t\tif (typeof error === 'string') {\n\t\t\treturn this.status(HttpCodes.InternalServerError).json({ error });\n\t\t}\n\n\t\treturn this.status(error).json({ error: data ?? STATUS_CODES[error] });\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic respond(data: unknown): void {\n\t\treturn typeof data === 'string' ? this.text(data) : this.json(data);\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic status(code: number): this {\n\t\tthis.statusCode = code;\n\t\treturn this;\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic json(data: any): void {\n\t\tthis.setContentType(MimeTypes.ApplicationJson).end(JSON.stringify(data));\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic text(data: string): void {\n\t\tthis.setContentType(MimeTypes.TextPlain).end(data);\n\t}\n\n\t/**\n\t * @since 6.1.0\n\t *\n\t * Sets the image content type and sends the image data in the response.\n\t *\n\t * @param type - The MIME type of the image (e.g., {@link MimeTypes.ImagePng}).\n\t * @param data - The image data as a `string`, {@link Buffer}, {@link Uint8Array}, or {@link ReadableStream}.\n\t */\n\tpublic image(\n\t\ttype: MimeTypes.ImageGif | MimeTypes.ImageJpg | MimeTypes.ImagePng | MimeTypes.ImageWebp | MimeTypes.ImageXIcon,\n\t\tdata: string | Buffer | Uint8Array | Readable\n\t): void {\n\t\tif (data instanceof Readable) {\n\t\t\tthis.setContentType(type);\n\t\t\tdata.pipe(this);\n\t\t} else {\n\t\t\tthis.setContentType(type).end(data);\n\t\t}\n\t}\n\n\t/**\n\t * @since 5.1.0\n\t */\n\tpublic html(code: number, data: string): void {\n\t\tthis.setContentType(MimeTypes.TextHtml).status(code).end(data);\n\t}\n\n\t/**\n\t * @since 1.0.0\n\t */\n\tpublic setContentType(contentType: MimeTypes): this {\n\t\tthis.setHeader('Content-Type', contentType);\n\t\treturn this;\n\t}\n}\n"]}
@@ -20,8 +20,8 @@ var _CookieStore = class _CookieStore extends Map {
20
20
  for (const pair of pairs) {
21
21
  const index = pair.indexOf("=");
22
22
  if (index === -1) continue;
23
- const key = decodeURIComponent(pair.substr(0, index).trim());
24
- const value = decodeURIComponent(pair.substr(index + 1).trim());
23
+ const key = decodeURIComponent(pair.slice(0, index).trim());
24
+ const value = decodeURIComponent(pair.slice(index + 1).trim());
25
25
  this.set(key, value);
26
26
  }
27
27
  const [splitHost] = this.request.headers.host?.split(":") ?? [""];
@@ -44,7 +44,7 @@ var _CookieStore = class _CookieStore extends Map {
44
44
  } else if (!Array.isArray(set)) {
45
45
  set = [set.toString()];
46
46
  }
47
- set = set.filter((i) => i.substr(0, i.indexOf("=")) !== name);
47
+ set = set.filter((i) => i.slice(0, i.indexOf("=")) !== name);
48
48
  set.push(entry);
49
49
  this.response.setHeader("Set-Cookie", set);
50
50
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../src/lib/structures/api/CookieStore.ts"],"names":[],"mappings":"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","sourcesContent":["// Copyright (c) 2018 Stanislav Woodger. All rights reserved. MIT license.\n// Source: https://github.com/woodger/cookie-httponly\n\nimport { getDomain } from 'tldts';\nimport type { ApiRequest } from './ApiRequest';\nimport type { ApiResponse } from './ApiResponse';\n\nexport class CookieStore extends Map<string, string> {\n\tprotected request: ApiRequest;\n\tprotected response: ApiResponse;\n\tprivate domain: string;\n\tprivate secure: boolean;\n\n\tpublic constructor(request: ApiRequest, response: ApiResponse, secure: boolean, domainOverwrite?: string | null) {\n\t\tsuper();\n\n\t\tthis.request = request;\n\t\tthis.response = response;\n\n\t\t// Read cookies\n\t\tconst { cookie = '' } = request.headers;\n\t\tconst pairs = cookie.split(';');\n\n\t\tfor (const pair of pairs) {\n\t\t\tconst index = pair.indexOf('=');\n\t\t\tif (index === -1) continue;\n\n\t\t\tconst key = decodeURIComponent(pair.substr(0, index).trim());\n\t\t\tconst value = decodeURIComponent(pair.substr(index + 1).trim());\n\t\t\tthis.set(key, value);\n\t\t}\n\n\t\tconst [splitHost] = this.request.headers.host?.split(':') ?? [''];\n\n\t\tthis.domain = domainOverwrite ?? this.getHostDomain(splitHost);\n\n\t\tif (this.request.socket.remoteAddress === this.domain) {\n\t\t\tthrow new Error('The connection must be established from the domain name (i.e., not an IP address)');\n\t\t}\n\n\t\t// RFC 6265 4.1.2.5. The Secure Attribute\n\t\tthis.secure = secure;\n\t}\n\n\tpublic add(name: string, value: string, options?: SecureCookieStoreSetOptions) {\n\t\tthis.insert(name, this.prepare(name, value, options));\n\t}\n\n\tpublic remove(name: string) {\n\t\tthis.add(name, '', { expires: new Date(0) });\n\t}\n\n\tprotected insert(name: string, entry: string) {\n\t\tlet set = this.response.getHeader('Set-Cookie');\n\n\t\tif (set === undefined) {\n\t\t\tset = [];\n\t\t} else if (!Array.isArray(set)) {\n\t\t\tset = [set.toString()];\n\t\t}\n\n\t\tset = set.filter((i) => i.substr(0, i.indexOf('=')) !== name);\n\t\tset.push(entry);\n\n\t\tthis.response.setHeader('Set-Cookie', set);\n\t}\n\n\tprotected prepare(name: string, value: string, { expires, maxAge, domain, path, httpOnly }: SecureCookieStoreSetOptions = {}) {\n\t\tconst now = new Date();\n\n\t\tif (expires === undefined) {\n\t\t\texpires = now;\n\t\t}\n\n\t\t// RFC 6265 4.1.1. Syntax\n\t\tname = CookieStore.encodeCookieOctet(name);\n\t\tvalue = CookieStore.encodeCookieOctet(value);\n\n\t\tlet entry = `${name}=${value}`;\n\n\t\tif (expires !== now) {\n\t\t\tentry += `; Expires=${expires.toUTCString()}`;\n\t\t} else if (maxAge) {\n\t\t\tentry += `; Max-Age=${maxAge}`;\n\t\t}\n\n\t\t// RFC 6265 5.1.3 Domain Matching\n\t\tdomain = (domain ?? this.domain).toLowerCase();\n\n\t\tentry += `; Domain=${domain}`;\n\t\tentry += `; Path=${path ?? '/'}`;\n\n\t\tif (this.secure) {\n\t\t\tentry += `; Secure`;\n\t\t}\n\n\t\tif (httpOnly ?? true) {\n\t\t\tentry += `; HttpOnly`;\n\t\t}\n\n\t\treturn entry;\n\t}\n\n\t/**\n\t * Parses a host using the {@linkplain https://github.com/remusao/tldts tldts} library to extract the domain.\n\t * This is used for the domain of the cookie\n\t * @param host The hot to parse\n\t * @returns Either the host in all lower case or the parsed domain, ready for use on cookies\n\t */\n\tprivate getHostDomain(host: string): string {\n\t\t// Transform the host to lower case\n\t\tconst lowercaseHost = host.toLowerCase();\n\n\t\t// Try parsing the host with tldts\n\t\tconst tldParsedInfo = getDomain(lowercaseHost);\n\n\t\t// If the domain is not defined then return the host in lowercase\n\t\tif (!tldParsedInfo) return lowercaseHost;\n\n\t\t// If the domain was found from parsing then prefix it with a . for a cookie that works with subdomains and return it\n\t\treturn `.${tldParsedInfo}`;\n\t}\n\n\t// RFC 6265 4.1.1. Syntax\n\tprivate static readonly octetRegExp = /[^\\x21\\x23-\\x2B\\x2D-\\x3A\\x3C-\\x5B\\x5D-\\x7E]/g;\n\n\tprivate static encodeCookieOctet(value: string) {\n\t\tif (CookieStore.octetRegExp.test(value)) {\n\t\t\tthrow new Error(`Invalid character in value`);\n\t\t}\n\n\t\treturn encodeURIComponent(value);\n\t}\n}\n\nexport interface SecureCookieStoreSetOptions {\n\texpires?: Date;\n\tmaxAge?: number;\n\tdomain?: string;\n\tpath?: string;\n\thttpOnly?: boolean;\n}\n"]}
1
+ {"version":3,"sources":["../../../../../src/lib/structures/api/CookieStore.ts"],"names":[],"mappings":"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","sourcesContent":["// Copyright (c) 2018 Stanislav Woodger. All rights reserved. MIT license.\n// Source: https://github.com/woodger/cookie-httponly\n\nimport { getDomain } from 'tldts';\nimport type { ApiRequest } from './ApiRequest';\nimport type { ApiResponse } from './ApiResponse';\n\nexport class CookieStore extends Map<string, string> {\n\tprotected request: ApiRequest;\n\tprotected response: ApiResponse;\n\tprivate domain: string;\n\tprivate secure: boolean;\n\n\tpublic constructor(request: ApiRequest, response: ApiResponse, secure: boolean, domainOverwrite?: string | null) {\n\t\tsuper();\n\n\t\tthis.request = request;\n\t\tthis.response = response;\n\n\t\t// Read cookies\n\t\tconst { cookie = '' } = request.headers;\n\t\tconst pairs = cookie.split(';');\n\n\t\tfor (const pair of pairs) {\n\t\t\tconst index = pair.indexOf('=');\n\t\t\tif (index === -1) continue;\n\n\t\t\tconst key = decodeURIComponent(pair.slice(0, index).trim());\n\t\t\tconst value = decodeURIComponent(pair.slice(index + 1).trim());\n\t\t\tthis.set(key, value);\n\t\t}\n\n\t\tconst [splitHost] = this.request.headers.host?.split(':') ?? [''];\n\n\t\tthis.domain = domainOverwrite ?? this.getHostDomain(splitHost);\n\n\t\tif (this.request.socket.remoteAddress === this.domain) {\n\t\t\tthrow new Error('The connection must be established from the domain name (i.e., not an IP address)');\n\t\t}\n\n\t\t// RFC 6265 4.1.2.5. The Secure Attribute\n\t\tthis.secure = secure;\n\t}\n\n\tpublic add(name: string, value: string, options?: SecureCookieStoreSetOptions) {\n\t\tthis.insert(name, this.prepare(name, value, options));\n\t}\n\n\tpublic remove(name: string) {\n\t\tthis.add(name, '', { expires: new Date(0) });\n\t}\n\n\tprotected insert(name: string, entry: string) {\n\t\tlet set = this.response.getHeader('Set-Cookie');\n\n\t\tif (set === undefined) {\n\t\t\tset = [];\n\t\t} else if (!Array.isArray(set)) {\n\t\t\tset = [set.toString()];\n\t\t}\n\n\t\tset = set.filter((i) => i.slice(0, i.indexOf('=')) !== name);\n\t\tset.push(entry);\n\n\t\tthis.response.setHeader('Set-Cookie', set);\n\t}\n\n\tprotected prepare(name: string, value: string, { expires, maxAge, domain, path, httpOnly }: SecureCookieStoreSetOptions = {}) {\n\t\tconst now = new Date();\n\n\t\tif (expires === undefined) {\n\t\t\texpires = now;\n\t\t}\n\n\t\t// RFC 6265 4.1.1. Syntax\n\t\tname = CookieStore.encodeCookieOctet(name);\n\t\tvalue = CookieStore.encodeCookieOctet(value);\n\n\t\tlet entry = `${name}=${value}`;\n\n\t\tif (expires !== now) {\n\t\t\tentry += `; Expires=${expires.toUTCString()}`;\n\t\t} else if (maxAge) {\n\t\t\tentry += `; Max-Age=${maxAge}`;\n\t\t}\n\n\t\t// RFC 6265 5.1.3 Domain Matching\n\t\tdomain = (domain ?? this.domain).toLowerCase();\n\n\t\tentry += `; Domain=${domain}`;\n\t\tentry += `; Path=${path ?? '/'}`;\n\n\t\tif (this.secure) {\n\t\t\tentry += `; Secure`;\n\t\t}\n\n\t\tif (httpOnly ?? true) {\n\t\t\tentry += `; HttpOnly`;\n\t\t}\n\n\t\treturn entry;\n\t}\n\n\t/**\n\t * Parses a host using the {@linkplain https://github.com/remusao/tldts tldts} library to extract the domain.\n\t * This is used for the domain of the cookie\n\t * @param host The hot to parse\n\t * @returns Either the host in all lower case or the parsed domain, ready for use on cookies\n\t */\n\tprivate getHostDomain(host: string): string {\n\t\t// Transform the host to lower case\n\t\tconst lowercaseHost = host.toLowerCase();\n\n\t\t// Try parsing the host with tldts\n\t\tconst tldParsedInfo = getDomain(lowercaseHost);\n\n\t\t// If the domain is not defined then return the host in lowercase\n\t\tif (!tldParsedInfo) return lowercaseHost;\n\n\t\t// If the domain was found from parsing then prefix it with a . for a cookie that works with subdomains and return it\n\t\treturn `.${tldParsedInfo}`;\n\t}\n\n\t// RFC 6265 4.1.1. Syntax\n\tprivate static readonly octetRegExp = /[^\\x21\\x23-\\x2B\\x2D-\\x3A\\x3C-\\x5B\\x5D-\\x7E]/g;\n\n\tprivate static encodeCookieOctet(value: string) {\n\t\tif (CookieStore.octetRegExp.test(value)) {\n\t\t\tthrow new Error(`Invalid character in value`);\n\t\t}\n\n\t\treturn encodeURIComponent(value);\n\t}\n}\n\nexport interface SecureCookieStoreSetOptions {\n\texpires?: Date;\n\tmaxAge?: number;\n\tdomain?: string;\n\tpath?: string;\n\thttpOnly?: boolean;\n}\n"]}
@@ -1,12 +1,43 @@
1
1
  'use strict';
2
2
 
3
- var http = require('http');
4
-
5
3
  // src/lib/structures/http/HttpMethods.ts
6
- var methods = Object.fromEntries(http.METHODS.map((method) => [method, Symbol(`HTTP-${method}`)]));
7
- var methodEntries = Object.entries(methods);
4
+ var MethodNames = [
5
+ "ACL",
6
+ "BIND",
7
+ "CHECKOUT",
8
+ "CONNECT",
9
+ "COPY",
10
+ "DELETE",
11
+ "GET",
12
+ "HEAD",
13
+ "LINK",
14
+ "LOCK",
15
+ "M-SEARCH",
16
+ "MERGE",
17
+ "MKACTIVITY",
18
+ "MKCALENDAR",
19
+ "MKCOL",
20
+ "MOVE",
21
+ "NOTIFY",
22
+ "OPTIONS",
23
+ "PATCH",
24
+ "POST",
25
+ "PROPFIND",
26
+ "PROPPATCH",
27
+ "PURGE",
28
+ "PUT",
29
+ "REBIND",
30
+ "REPORT",
31
+ "SEARCH",
32
+ "SOURCE",
33
+ "SUBSCRIBE",
34
+ "TRACE",
35
+ "UNBIND",
36
+ "UNLINK",
37
+ "UNLOCK",
38
+ "UNSUBSCRIBE"
39
+ ];
8
40
 
9
- exports.methodEntries = methodEntries;
10
- exports.methods = methods;
41
+ exports.MethodNames = MethodNames;
11
42
  //# sourceMappingURL=out.js.map
12
43
  //# sourceMappingURL=HttpMethods.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../src/lib/structures/http/HttpMethods.ts"],"names":[],"mappings":";AAAA,SAAS,eAAe;AAuCjB,IAAM,UAAU,OAAO,YAAY,QAAQ,IAAI,CAAC,WAAW,CAAC,QAAmB,OAAO,QAAQ,MAAM,EAAE,CAAC,CAAC,CAAC;AACzG,IAAM,gBAAgB,OAAO,QAAQ,OAAO","sourcesContent":["import { METHODS } from 'node:http';\n\nexport type Methods =\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| 'PRI'\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\nexport const methods = Object.fromEntries(METHODS.map((method) => [method as Methods, Symbol(`HTTP-${method}`)])) as Record<Methods, symbol>;\nexport const methodEntries = Object.entries(methods) as readonly [Methods, symbol][];\n"]}
1
+ {"version":3,"sources":["../../../../../src/lib/structures/http/HttpMethods.ts"],"names":[],"mappings":";AAEO,IAAM,cAAc;AAAA,EAC1B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD","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,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var pieces = require('@sapphire/pieces');
4
- var events = require('events');
4
+ var async_event_emitter = require('@vladfrangu/async_event_emitter');
5
5
  var http = require('http');
6
6
  var MediaParserStore_cjs = require('../MediaParserStore.cjs');
7
7
  var MiddlewareStore_cjs = require('../MiddlewareStore.cjs');
@@ -14,18 +14,19 @@ var __defProp = Object.defineProperty;
14
14
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
15
15
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
16
16
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
17
- var ServerEvents = /* @__PURE__ */ ((ServerEvents2) => {
18
- ServerEvents2["Error"] = "error";
19
- ServerEvents2["Request"] = "request";
20
- ServerEvents2["Match"] = "match";
21
- ServerEvents2["NoMatch"] = "noMatch";
22
- ServerEvents2["RouteError"] = "routeError";
23
- ServerEvents2["MiddlewareFailure"] = "middlewareFailure";
24
- ServerEvents2["MiddlewareError"] = "middlewareError";
25
- ServerEvents2["MiddlewareSuccess"] = "middlewareSuccess";
26
- return ServerEvents2;
27
- })(ServerEvents || {});
28
- var _Server = class _Server extends events.EventEmitter {
17
+ var ServerEvent = /* @__PURE__ */ ((ServerEvent2) => {
18
+ ServerEvent2["Error"] = "error";
19
+ ServerEvent2["Request"] = "request";
20
+ ServerEvent2["RouterBranchNotFound"] = "routerBranchNotFound";
21
+ ServerEvent2["RouterBranchMethodNotAllowed"] = "routerBranchMethodNotAllowed";
22
+ ServerEvent2["RouterFound"] = "routerFound";
23
+ ServerEvent2["RouteError"] = "routeError";
24
+ ServerEvent2["MiddlewareFailure"] = "middlewareFailure";
25
+ ServerEvent2["MiddlewareError"] = "middlewareError";
26
+ ServerEvent2["MiddlewareSuccess"] = "middlewareSuccess";
27
+ return ServerEvent2;
28
+ })(ServerEvent || {});
29
+ var _Server = class _Server extends async_event_emitter.AsyncEventEmitter {
29
30
  /**
30
31
  * @since 1.0.0
31
32
  * @param options The options for this server
@@ -119,6 +120,6 @@ __name(_Server, "Server");
119
120
  var Server = _Server;
120
121
 
121
122
  exports.Server = Server;
122
- exports.ServerEvents = ServerEvents;
123
+ exports.ServerEvent = ServerEvent;
123
124
  //# sourceMappingURL=out.js.map
124
125
  //# sourceMappingURL=Server.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../src/lib/structures/http/Server.ts"],"names":["ServerEvents","error"],"mappings":";;;;;;AAAA,SAAS,iBAAiB;AAC1B,SAAS,oBAAoB;AAC7B,SAA+B,gBAAgB,wBAA2D;AAE1G,SAAS,wBAAwB;AACjC,SAAS,uBAAuB;AAChC,SAAS,kBAAmC;AAC5C,SAAS,kBAAkB;AAC3B,SAAS,mBAAmB;AAC5B,SAAS,YAAoC;AAEtC,IAAK,eAAL,kBAAKA,kBAAL;AACN,EAAAA,cAAA,WAAQ;AACR,EAAAA,cAAA,aAAU;AACV,EAAAA,cAAA,WAAQ;AACR,EAAAA,cAAA,aAAU;AACV,EAAAA,cAAA,gBAAa;AACb,EAAAA,cAAA,uBAAoB;AACpB,EAAAA,cAAA,qBAAkB;AAClB,EAAAA,cAAA,uBAAoB;AART,SAAAA;AAAA,GAAA;AAcL,IAAM,UAAN,MAAM,gBAAe,aAAa;AAAA;AAAA;AAAA;AAAA;AAAA,EAyCjC,YAAY,EAAE,MAAM,GAAG,QAAQ,IAAmB,CAAC,GAAG;AAC5D,UAAM;AArCP;AAAA;AAAA;AAAA;AAAA,wBAAgB;AAMhB;AAAA;AAAA;AAAA;AAAA,wBAAgB;AAMhB;AAAA;AAAA;AAAA;AAAA,wBAAgB;AAMhB;AAAA;AAAA;AAAA;AAAA,wBAAgB;AAMhB;AAAA;AAAA;AAAA;AAAA,wBAAgB;AAMhB;AAAA;AAAA;AAAA;AAAA,wBAAgB;AASf,cAAU,SAAS;AAEnB,SAAK,UAAU;AACf,SAAK,SAAS,iBAAiB;AAAA,MAC9B,iBAAiB;AAAA,MACjB,gBAAgB;AAAA,MAChB,GAAI,QAAQ,UAAU,CAAC;AAAA,IACxB,CAAC;AACD,SAAK,SAAS,IAAI,WAAW;AAC7B,SAAK,cAAc,IAAI,gBAAgB;AACvC,SAAK,eAAe,IAAI,iBAAiB;AACzC,SAAK,OAAO,KAAK,OAAO,IAAI;AAC5B,SAAK,OAAO,GAAG,SAAS,KAAK,KAAK,KAAK,MAAM,mBAAkB,CAAC;AAChE,SAAK,OAAO,GAAG,WAAW,KAAK,KAAK,KAAK,MAAM,uBAAoB,CAAC;AAAA,EACrE;AAAA,EAEO,UAAU;AAChB,UAAM,EAAE,OAAO,IAAI;AACnB,WAAO,OAAO;AAAA,MACb,MAAM;AAAA,MACN,GAAI,KAAK,QAAQ,iBAAiB,CAAC;AAAA,IACpC,CAAC;AAED,WAAO,IAAI,QAAc,CAAC,SAAS,WAAW;AAC7C,eAAS,YAAY;AACpB,gBAAQ;AACR,gBAAQ;AAAA,MACT;AAHS;AAKT,eAAS,MAAMC,QAAc;AAC5B,gBAAQ;AACR,eAAOA,MAAK;AAAA,MACb;AAHS;AAKT,eAAS,QAAQ;AAChB,gBAAQ;AACR,eAAO,IAAI,MAAM,sBAAsB,CAAC;AAAA,MACzC;AAHS;AAKT,eAAS,UAAU;AAClB,eAAO,IAAI,aAAa,SAAS;AACjC,eAAO,IAAI,SAAS,KAAK;AACzB,eAAO,IAAI,SAAS,KAAK;AAAA,MAC1B;AAJS;AAMT,aAAO,GAAG,aAAa,SAAS;AAChC,aAAO,GAAG,SAAS,KAAK;AACxB,aAAO,GAAG,SAAS,KAAK;AAAA,IACzB,CAAC;AAAA,EACF;AAAA,EAEO,aAAa;AACnB,WAAO,IAAI,QAAc,CAAC,SAAS,WAAW;AAC7C,WAAK,OAAO,MAAM,CAAC,UAAW,QAAQ,QAAQ,IAAI,OAAO,KAAK,CAAE;AAAA,IACjE,CAAC;AAAA,EACF;AACD;AApGyC;AAAlC,IAAM,SAAN","sourcesContent":["import { container } from '@sapphire/pieces';\nimport { EventEmitter } from 'node:events';\nimport { Server as HttpServer, createServer as httpCreateServer, type ServerOptions as HttpOptions } from 'node:http';\nimport type { ListenOptions } from 'node:net';\nimport { MediaParserStore } from '../MediaParserStore';\nimport { MiddlewareStore } from '../MiddlewareStore';\nimport { RouteStore, type RouteMatch } from '../RouteStore';\nimport { ApiRequest } from '../api/ApiRequest';\nimport { ApiResponse } from '../api/ApiResponse';\nimport { Auth, type ServerOptionsAuth } from './Auth';\n\nexport enum ServerEvents {\n\tError = 'error',\n\tRequest = 'request',\n\tMatch = 'match',\n\tNoMatch = 'noMatch',\n\tRouteError = 'routeError',\n\tMiddlewareFailure = 'middlewareFailure',\n\tMiddlewareError = 'middlewareError',\n\tMiddlewareSuccess = 'middlewareSuccess'\n}\n\n/**\n * @since 1.0.0\n */\nexport class Server extends EventEmitter {\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, ServerEvents.Error));\n\t\tthis.server.on('request', this.emit.bind(this, ServerEvents.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 */\nexport type MimeTypeWithoutParameters = `${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 1.3.0\n */\nexport type MimeType = `${MimeTypeWithoutParameters}${'' | 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?: MimeTypeWithoutParameters[] | 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\n/**\n * The context sent in the error events.\n * @since 1.2.0\n */\nexport interface MiddlewareErrorContext {\n\t/**\n\t * The erroneous request.\n\t * @since 1.2.0\n\t */\n\trequest: ApiRequest;\n\n\t/**\n\t * The server's response.\n\t * @since 1.2.0\n\t */\n\tresponse: ApiResponse;\n\n\t/**\n\t * The route match.\n\t * @since 1.2.0\n\t */\n\tmatch: RouteMatch;\n}\n"]}
1
+ {"version":3,"sources":["../../../../../src/lib/structures/http/Server.ts"],"names":["ServerEvent","error"],"mappings":";;;;;;AAAA,SAAS,iBAAiB;AAC1B,SAAS,yBAAyB;AAClC,SAA+B,gBAAgB,wBAA2D;AAE1G,SAAS,wBAAwB;AACjC,SAAS,uBAAuB;AAEhC,SAAS,kBAAkB;AAC3B,SAAS,kBAAkB;AAC3B,SAAS,mBAAmB;AAE5B,SAAS,YAAoC;AAEtC,IAAK,cAAL,kBAAKA,iBAAL;AACN,EAAAA,aAAA,WAAQ;AACR,EAAAA,aAAA,aAAU;AACV,EAAAA,aAAA,0BAAuB;AACvB,EAAAA,aAAA,kCAA+B;AAC/B,EAAAA,aAAA,iBAAc;AACd,EAAAA,aAAA,gBAAa;AACb,EAAAA,aAAA,uBAAoB;AACpB,EAAAA,aAAA,qBAAkB;AAClB,EAAAA,aAAA,uBAAoB;AATT,SAAAA;AAAA,GAAA;AA2BL,IAAM,UAAN,MAAM,gBAAe,kBAAgC;AAAA;AAAA;AAAA;AAAA;AAAA,EAyCpD,YAAY,EAAE,MAAM,GAAG,QAAQ,IAAmB,CAAC,GAAG;AAC5D,UAAM;AArCP;AAAA;AAAA;AAAA;AAAA,wBAAgB;AAMhB;AAAA;AAAA;AAAA;AAAA,wBAAgB;AAMhB;AAAA;AAAA;AAAA;AAAA,wBAAgB;AAMhB;AAAA;AAAA;AAAA;AAAA,wBAAgB;AAMhB;AAAA;AAAA;AAAA;AAAA,wBAAgB;AAMhB;AAAA;AAAA;AAAA;AAAA,wBAAgB;AASf,cAAU,SAAS;AAEnB,SAAK,UAAU;AACf,SAAK,SAAS,iBAAiB;AAAA,MAC9B,iBAAiB;AAAA,MACjB,gBAAgB;AAAA,MAChB,GAAI,QAAQ,UAAU,CAAC;AAAA,IACxB,CAAC;AACD,SAAK,SAAS,IAAI,WAAW;AAC7B,SAAK,cAAc,IAAI,gBAAgB;AACvC,SAAK,eAAe,IAAI,iBAAiB;AACzC,SAAK,OAAO,KAAK,OAAO,IAAI;AAC5B,SAAK,OAAO,GAAG,SAAS,KAAK,KAAK,KAAK,MAAM,mBAAiB,CAAC;AAC/D,SAAK,OAAO,GAAG,WAAW,KAAK,KAAK,KAAK,MAAM,uBAAmB,CAAC;AAAA,EACpE;AAAA,EAEO,UAAU;AAChB,UAAM,EAAE,OAAO,IAAI;AACnB,WAAO,OAAO;AAAA,MACb,MAAM;AAAA,MACN,GAAI,KAAK,QAAQ,iBAAiB,CAAC;AAAA,IACpC,CAAC;AAED,WAAO,IAAI,QAAc,CAAC,SAAS,WAAW;AAC7C,eAAS,YAAY;AACpB,gBAAQ;AACR,gBAAQ;AAAA,MACT;AAHS;AAKT,eAAS,MAAMC,QAAc;AAC5B,gBAAQ;AACR,eAAOA,MAAK;AAAA,MACb;AAHS;AAKT,eAAS,QAAQ;AAChB,gBAAQ;AACR,eAAO,IAAI,MAAM,sBAAsB,CAAC;AAAA,MACzC;AAHS;AAKT,eAAS,UAAU;AAClB,eAAO,IAAI,aAAa,SAAS;AACjC,eAAO,IAAI,SAAS,KAAK;AACzB,eAAO,IAAI,SAAS,KAAK;AAAA,MAC1B;AAJS;AAMT,aAAO,GAAG,aAAa,SAAS;AAChC,aAAO,GAAG,SAAS,KAAK;AACxB,aAAO,GAAG,SAAS,KAAK;AAAA,IACzB,CAAC;AAAA,EACF;AAAA,EAEO,aAAa;AACnB,WAAO,IAAI,QAAc,CAAC,SAAS,WAAW;AAC7C,WAAK,OAAO,MAAM,CAAC,UAAW,QAAQ,QAAQ,IAAI,OAAO,KAAK,CAAE;AAAA,IACjE,CAAC;AAAA,EACF;AACD;AApG4D;AAArD,IAAM,SAAN","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 { 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]: [error: Error, 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 */\nexport type MimeTypeWithoutParameters = `${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 1.3.0\n */\nexport type MimeType = `${MimeTypeWithoutParameters}${'' | 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?: MimeTypeWithoutParameters[] | 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"]}