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

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 (183) hide show
  1. package/dist/cjs/index.cjs +23 -9
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs/index.d.cts +273 -149
  4. package/dist/cjs/lib/structures/Augmentations.d.cjs +1 -1
  5. package/dist/cjs/lib/structures/Augmentations.d.cjs.map +1 -1
  6. package/dist/cjs/lib/structures/MediaParser.cjs +1 -1
  7. package/dist/cjs/lib/structures/MediaParser.cjs.map +1 -1
  8. package/dist/cjs/lib/structures/MediaParserStore.cjs +1 -1
  9. package/dist/cjs/lib/structures/MediaParserStore.cjs.map +1 -1
  10. package/dist/cjs/lib/structures/Middleware.cjs +2 -2
  11. package/dist/cjs/lib/structures/Middleware.cjs.map +1 -1
  12. package/dist/cjs/lib/structures/MiddlewareStore.cjs +4 -4
  13. package/dist/cjs/lib/structures/MiddlewareStore.cjs.map +1 -1
  14. package/dist/cjs/lib/structures/Route.cjs +18 -35
  15. package/dist/cjs/lib/structures/Route.cjs.map +1 -1
  16. package/dist/cjs/lib/structures/RouteLoaderStrategy.cjs +20 -0
  17. package/dist/cjs/lib/structures/RouteLoaderStrategy.cjs.map +1 -0
  18. package/dist/cjs/lib/structures/RouteStore.cjs +6 -44
  19. package/dist/cjs/lib/structures/RouteStore.cjs.map +1 -1
  20. package/dist/cjs/lib/structures/api/ApiRequest.cjs +27 -1
  21. package/dist/cjs/lib/structures/api/ApiRequest.cjs.map +1 -1
  22. package/dist/cjs/lib/structures/api/ApiResponse.cjs +8 -2
  23. package/dist/cjs/lib/structures/api/ApiResponse.cjs.map +1 -1
  24. package/dist/cjs/lib/structures/api/CookieStore.cjs +4 -4
  25. package/dist/cjs/lib/structures/api/CookieStore.cjs.map +1 -1
  26. package/dist/cjs/lib/structures/http/Auth.cjs +2 -2
  27. package/dist/cjs/lib/structures/http/Auth.cjs.map +1 -1
  28. package/dist/cjs/lib/structures/http/HttpCodes.cjs +1 -1
  29. package/dist/cjs/lib/structures/http/HttpCodes.cjs.map +1 -1
  30. package/dist/cjs/lib/structures/http/HttpMethods.cjs +38 -7
  31. package/dist/cjs/lib/structures/http/HttpMethods.cjs.map +1 -1
  32. package/dist/cjs/lib/structures/http/Server.cjs +16 -15
  33. package/dist/cjs/lib/structures/http/Server.cjs.map +1 -1
  34. package/dist/cjs/lib/structures/router/RouterBranch.cjs +184 -0
  35. package/dist/cjs/lib/structures/router/RouterBranch.cjs.map +1 -0
  36. package/dist/cjs/lib/structures/router/RouterNode.cjs +67 -0
  37. package/dist/cjs/lib/structures/router/RouterNode.cjs.map +1 -0
  38. package/dist/cjs/lib/structures/router/RouterRoot.cjs +71 -0
  39. package/dist/cjs/lib/structures/router/RouterRoot.cjs.map +1 -0
  40. package/dist/cjs/lib/utils/MimeTypes.cjs +1 -1
  41. package/dist/cjs/lib/utils/MimeTypes.cjs.map +1 -1
  42. package/dist/cjs/listeners/PluginRouteError.cjs +3 -3
  43. package/dist/cjs/listeners/PluginRouteError.cjs.map +1 -1
  44. package/dist/cjs/listeners/PluginServerMiddlewareError.cjs +3 -3
  45. package/dist/cjs/listeners/PluginServerMiddlewareError.cjs.map +1 -1
  46. package/dist/cjs/listeners/PluginServerMiddlewareSuccess.cjs +5 -5
  47. package/dist/cjs/listeners/PluginServerMiddlewareSuccess.cjs.map +1 -1
  48. package/dist/cjs/listeners/PluginServerRequest.cjs +42 -8
  49. package/dist/cjs/listeners/PluginServerRequest.cjs.map +1 -1
  50. package/dist/cjs/listeners/{PluginServerMatch.cjs → PluginServerRouterBranchMethodNotAllowed.cjs} +5 -10
  51. package/dist/cjs/listeners/PluginServerRouterBranchMethodNotAllowed.cjs.map +1 -0
  52. package/dist/cjs/listeners/{PluginServerNoMatch.cjs → PluginServerRouterBranchNotFound.cjs} +3 -3
  53. package/dist/cjs/listeners/PluginServerRouterBranchNotFound.cjs.map +1 -0
  54. package/dist/cjs/listeners/PluginServerRouterFound.cjs +22 -0
  55. package/dist/cjs/listeners/PluginServerRouterFound.cjs.map +1 -0
  56. package/dist/cjs/listeners/_load.cjs +7 -5
  57. package/dist/cjs/listeners/_load.cjs.map +1 -1
  58. package/dist/cjs/mediaParsers/_load.cjs +1 -1
  59. package/dist/cjs/mediaParsers/_load.cjs.map +1 -1
  60. package/dist/cjs/mediaParsers/applicationFormUrlEncoded.cjs +1 -1
  61. package/dist/cjs/mediaParsers/applicationFormUrlEncoded.cjs.map +1 -1
  62. package/dist/cjs/mediaParsers/applicationJson.cjs +1 -1
  63. package/dist/cjs/mediaParsers/applicationJson.cjs.map +1 -1
  64. package/dist/cjs/mediaParsers/textPlain.cjs +1 -1
  65. package/dist/cjs/mediaParsers/textPlain.cjs.map +1 -1
  66. package/dist/cjs/middlewares/_load.cjs +1 -1
  67. package/dist/cjs/middlewares/_load.cjs.map +1 -1
  68. package/dist/cjs/middlewares/auth.cjs +2 -2
  69. package/dist/cjs/middlewares/auth.cjs.map +1 -1
  70. package/dist/cjs/middlewares/body.cjs +6 -5
  71. package/dist/cjs/middlewares/body.cjs.map +1 -1
  72. package/dist/cjs/middlewares/cookies.cjs +1 -1
  73. package/dist/cjs/middlewares/cookies.cjs.map +1 -1
  74. package/dist/cjs/middlewares/headers.cjs +19 -9
  75. package/dist/cjs/middlewares/headers.cjs.map +1 -1
  76. package/dist/cjs/register.cjs +1 -1
  77. package/dist/cjs/register.cjs.map +1 -1
  78. package/dist/cjs/routes/_load.cjs +5 -5
  79. package/dist/cjs/routes/_load.cjs.map +1 -1
  80. package/dist/cjs/routes/oauth/{callback.cjs → callback.post.cjs} +5 -6
  81. package/dist/cjs/routes/oauth/callback.post.cjs.map +1 -0
  82. package/dist/cjs/routes/oauth/{logout.cjs → logout.post.cjs} +6 -8
  83. package/dist/cjs/routes/oauth/logout.post.cjs.map +1 -0
  84. package/dist/esm/{chunk-JTFKMR4I.mjs → chunk-S573YWRP.mjs} +4 -3
  85. package/dist/esm/chunk-S573YWRP.mjs.map +1 -0
  86. package/dist/esm/index.d.mts +273 -149
  87. package/dist/esm/index.mjs +6 -4
  88. package/dist/esm/index.mjs.map +1 -1
  89. package/dist/esm/lib/structures/Augmentations.d.mjs +1 -1
  90. package/dist/esm/lib/structures/Augmentations.d.mjs.map +1 -1
  91. package/dist/esm/lib/structures/MediaParser.mjs +2 -2
  92. package/dist/esm/lib/structures/MediaParser.mjs.map +1 -1
  93. package/dist/esm/lib/structures/MediaParserStore.mjs +2 -2
  94. package/dist/esm/lib/structures/MediaParserStore.mjs.map +1 -1
  95. package/dist/esm/lib/structures/Middleware.mjs +2 -2
  96. package/dist/esm/lib/structures/Middleware.mjs.map +1 -1
  97. package/dist/esm/lib/structures/MiddlewareStore.mjs +4 -4
  98. package/dist/esm/lib/structures/MiddlewareStore.mjs.map +1 -1
  99. package/dist/esm/lib/structures/Route.mjs +19 -36
  100. package/dist/esm/lib/structures/Route.mjs.map +1 -1
  101. package/dist/esm/lib/structures/RouteLoaderStrategy.mjs +17 -0
  102. package/dist/esm/lib/structures/RouteLoaderStrategy.mjs.map +1 -0
  103. package/dist/esm/lib/structures/RouteStore.mjs +6 -44
  104. package/dist/esm/lib/structures/RouteStore.mjs.map +1 -1
  105. package/dist/esm/lib/structures/api/ApiRequest.mjs +28 -2
  106. package/dist/esm/lib/structures/api/ApiRequest.mjs.map +1 -1
  107. package/dist/esm/lib/structures/api/ApiResponse.mjs +8 -2
  108. package/dist/esm/lib/structures/api/ApiResponse.mjs.map +1 -1
  109. package/dist/esm/lib/structures/api/CookieStore.mjs +5 -5
  110. package/dist/esm/lib/structures/api/CookieStore.mjs.map +1 -1
  111. package/dist/esm/lib/structures/http/Auth.mjs +2 -2
  112. package/dist/esm/lib/structures/http/Auth.mjs.map +1 -1
  113. package/dist/esm/lib/structures/http/HttpCodes.mjs +2 -2
  114. package/dist/esm/lib/structures/http/HttpCodes.mjs.map +1 -1
  115. package/dist/esm/lib/structures/http/HttpMethods.mjs +40 -6
  116. package/dist/esm/lib/structures/http/HttpMethods.mjs.map +1 -1
  117. package/dist/esm/lib/structures/http/Server.mjs +17 -16
  118. package/dist/esm/lib/structures/http/Server.mjs.map +1 -1
  119. package/dist/esm/lib/structures/router/RouterBranch.mjs +179 -0
  120. package/dist/esm/lib/structures/router/RouterBranch.mjs.map +1 -0
  121. package/dist/esm/lib/structures/router/RouterNode.mjs +56 -0
  122. package/dist/esm/lib/structures/router/RouterNode.mjs.map +1 -0
  123. package/dist/esm/lib/structures/router/RouterRoot.mjs +68 -0
  124. package/dist/esm/lib/structures/router/RouterRoot.mjs.map +1 -0
  125. package/dist/esm/lib/utils/MimeTypes.mjs +2 -2
  126. package/dist/esm/lib/utils/MimeTypes.mjs.map +1 -1
  127. package/dist/esm/listeners/PluginRouteError.mjs +5 -5
  128. package/dist/esm/listeners/PluginRouteError.mjs.map +1 -1
  129. package/dist/esm/listeners/PluginServerMiddlewareError.mjs +5 -5
  130. package/dist/esm/listeners/PluginServerMiddlewareError.mjs.map +1 -1
  131. package/dist/esm/listeners/PluginServerMiddlewareSuccess.mjs +7 -7
  132. package/dist/esm/listeners/PluginServerMiddlewareSuccess.mjs.map +1 -1
  133. package/dist/esm/listeners/PluginServerRequest.mjs +38 -10
  134. package/dist/esm/listeners/PluginServerRequest.mjs.map +1 -1
  135. package/dist/esm/listeners/PluginServerRouterBranchMethodNotAllowed.mjs +18 -0
  136. package/dist/esm/listeners/PluginServerRouterBranchMethodNotAllowed.mjs.map +1 -0
  137. package/dist/esm/listeners/{PluginServerNoMatch.mjs → PluginServerRouterBranchNotFound.mjs} +5 -5
  138. package/dist/esm/listeners/PluginServerRouterBranchNotFound.mjs.map +1 -0
  139. package/dist/esm/listeners/PluginServerRouterFound.mjs +19 -0
  140. package/dist/esm/listeners/PluginServerRouterFound.mjs.map +1 -0
  141. package/dist/esm/listeners/_load.mjs +14 -12
  142. package/dist/esm/listeners/_load.mjs.map +1 -1
  143. package/dist/esm/mediaParsers/_load.mjs +2 -2
  144. package/dist/esm/mediaParsers/_load.mjs.map +1 -1
  145. package/dist/esm/mediaParsers/applicationFormUrlEncoded.mjs +2 -2
  146. package/dist/esm/mediaParsers/applicationFormUrlEncoded.mjs.map +1 -1
  147. package/dist/esm/mediaParsers/applicationJson.mjs +2 -2
  148. package/dist/esm/mediaParsers/applicationJson.mjs.map +1 -1
  149. package/dist/esm/mediaParsers/textPlain.mjs +2 -2
  150. package/dist/esm/mediaParsers/textPlain.mjs.map +1 -1
  151. package/dist/esm/middlewares/_load.mjs +2 -2
  152. package/dist/esm/middlewares/_load.mjs.map +1 -1
  153. package/dist/esm/middlewares/auth.mjs +2 -2
  154. package/dist/esm/middlewares/auth.mjs.map +1 -1
  155. package/dist/esm/middlewares/body.mjs +6 -5
  156. package/dist/esm/middlewares/body.mjs.map +1 -1
  157. package/dist/esm/middlewares/cookies.mjs +2 -2
  158. package/dist/esm/middlewares/cookies.mjs.map +1 -1
  159. package/dist/esm/middlewares/headers.mjs +20 -10
  160. package/dist/esm/middlewares/headers.mjs.map +1 -1
  161. package/dist/esm/register.mjs +2 -2
  162. package/dist/esm/register.mjs.map +1 -1
  163. package/dist/esm/routes/_load.mjs +4 -4
  164. package/dist/esm/routes/_load.mjs.map +1 -1
  165. package/dist/esm/routes/oauth/{callback.mjs → callback.post.mjs} +5 -6
  166. package/dist/esm/routes/oauth/callback.post.mjs.map +1 -0
  167. package/dist/esm/routes/oauth/{logout.mjs → logout.post.mjs} +6 -8
  168. package/dist/esm/routes/oauth/logout.post.mjs.map +1 -0
  169. package/package.json +6 -5
  170. package/dist/cjs/lib/utils/RouteData.cjs +0 -62
  171. package/dist/cjs/lib/utils/RouteData.cjs.map +0 -1
  172. package/dist/cjs/listeners/PluginServerMatch.cjs.map +0 -1
  173. package/dist/cjs/listeners/PluginServerNoMatch.cjs.map +0 -1
  174. package/dist/cjs/routes/oauth/callback.cjs.map +0 -1
  175. package/dist/cjs/routes/oauth/logout.cjs.map +0 -1
  176. package/dist/esm/chunk-JTFKMR4I.mjs.map +0 -1
  177. package/dist/esm/lib/utils/RouteData.mjs +0 -56
  178. package/dist/esm/lib/utils/RouteData.mjs.map +0 -1
  179. package/dist/esm/listeners/PluginServerMatch.mjs +0 -23
  180. package/dist/esm/listeners/PluginServerMatch.mjs.map +0 -1
  181. package/dist/esm/listeners/PluginServerNoMatch.mjs.map +0 -1
  182. package/dist/esm/routes/oauth/callback.mjs.map +0 -1
  183. package/dist/esm/routes/oauth/logout.mjs.map +0 -1
@@ -1,4 +1,4 @@
1
- import { __name } from '../../chunk-JTFKMR4I.mjs';
1
+ import { __name } from '../../chunk-S573YWRP.mjs';
2
2
  import { Store } from '@sapphire/pieces';
3
3
  import { MediaParser } from './MediaParser.mjs';
4
4
 
@@ -20,5 +20,5 @@ __name(_MediaParserStore, "MediaParserStore");
20
20
  var MediaParserStore = _MediaParserStore;
21
21
 
22
22
  export { MediaParserStore };
23
- //# sourceMappingURL=out.js.map
23
+ //# sourceMappingURL=MediaParserStore.mjs.map
24
24
  //# sourceMappingURL=MediaParserStore.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/lib/structures/MediaParserStore.ts"],"names":[],"mappings":";;;;;AAAA,SAAS,aAAa;AACtB,SAAS,mBAAmB;AAKrB,IAAM,oBAAN,MAAM,0BAAyB,MAAmC;AAAA,EACjE,cAAc;AACpB,UAAM,aAAa,EAAE,MAAM,eAAe,CAAC;AAAA,EAC5C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOO,iBAAiB,aAA6B;AACpD,UAAM,QAAQ,YAAY,QAAQ,GAAG;AACrC,WAAO,UAAU,KAAK,cAAc,YAAY,MAAM,GAAG,KAAK;AAAA,EAC/D;AACD;AAdyE;AAAlE,IAAM,mBAAN","sourcesContent":["import { Store } from '@sapphire/pieces';\nimport { MediaParser } from './MediaParser';\n\n/**\n * @since 1.3.0\n */\nexport class MediaParserStore extends Store<MediaParser, 'mediaParsers'> {\n\tpublic constructor() {\n\t\tsuper(MediaParser, { name: 'mediaParsers' });\n\t}\n\n\t/**\n\t * Parses a content type by getting the relevant information inside.\n\t * @since 1.3.0\n\t * @param contentType The content type to parse.\n\t */\n\tpublic parseContentType(contentType: string): string {\n\t\tconst index = contentType.indexOf(';');\n\t\treturn index === -1 ? contentType : contentType.slice(0, index);\n\t}\n}\n"]}
1
+ {"version":3,"sources":["../../../../src/lib/structures/MediaParserStore.ts"],"names":[],"mappings":";;;;AAMO,IAAM,iBAAA,GAAN,MAAM,iBAAA,SAAyB,KAAmC,CAAA;AAAA,EACjE,WAAc,GAAA;AACpB,IAAA,KAAA,CAAM,WAAa,EAAA,EAAE,IAAM,EAAA,cAAA,EAAgB,CAAA,CAAA;AAAA,GAC5C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOO,iBAAiB,WAA6B,EAAA;AACpD,IAAM,MAAA,KAAA,GAAQ,WAAY,CAAA,OAAA,CAAQ,GAAG,CAAA,CAAA;AACrC,IAAA,OAAO,UAAU,CAAK,CAAA,GAAA,WAAA,GAAc,WAAY,CAAA,KAAA,CAAM,GAAG,KAAK,CAAA,CAAA;AAAA,GAC/D;AACD,CAAA,CAAA;AAdyE,MAAA,CAAA,iBAAA,EAAA,kBAAA,CAAA,CAAA;AAAlE,IAAM,gBAAN,GAAA","file":"MediaParserStore.mjs","sourcesContent":["import { Store } from '@sapphire/pieces';\nimport { MediaParser } from './MediaParser';\n\n/**\n * @since 1.3.0\n */\nexport class MediaParserStore extends Store<MediaParser, 'mediaParsers'> {\n\tpublic constructor() {\n\t\tsuper(MediaParser, { name: 'mediaParsers' });\n\t}\n\n\t/**\n\t * Parses a content type by getting the relevant information inside.\n\t * @since 1.3.0\n\t * @param contentType The content type to parse.\n\t */\n\tpublic parseContentType(contentType: string): string {\n\t\tconst index = contentType.indexOf(';');\n\t\treturn index === -1 ? contentType : contentType.slice(0, index);\n\t}\n}\n"]}
@@ -1,4 +1,4 @@
1
- import { __name, __publicField } from '../../chunk-JTFKMR4I.mjs';
1
+ import { __name, __publicField } from '../../chunk-S573YWRP.mjs';
2
2
  import { Piece } from '@sapphire/pieces';
3
3
 
4
4
  var _Middleware = class _Middleware extends Piece {
@@ -22,5 +22,5 @@ __name(_Middleware, "Middleware");
22
22
  var Middleware = _Middleware;
23
23
 
24
24
  export { Middleware };
25
- //# sourceMappingURL=out.js.map
25
+ //# sourceMappingURL=Middleware.mjs.map
26
26
  //# sourceMappingURL=Middleware.mjs.map
@@ -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":";;;AAQO,IAAe,WAAA,GAAf,MAAe,WAAA,SAA4E,KAA8B,CAAA;AAAA,EAaxH,WAAY,CAAA,OAAA,EAAmC,OAAmB,GAAA,EAAe,EAAA;AACvF,IAAA,KAAA,CAAM,SAAS,OAAO,CAAA,CAAA;AAHvB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAgB,aAAA,CAAA,IAAA,EAAA,UAAA,CAAA,CAAA;AAIf,IAAK,IAAA,CAAA,QAAA,GAAW,QAAQ,QAAY,IAAA,GAAA,CAAA;AAAA,GACrC;AASD,CAAA,CAAA;AAzBgI,MAAA,CAAA,WAAA,EAAA,YAAA,CAAA,CAAA;AAAzH,IAAe,UAAf,GAAA","file":"Middleware.mjs","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"]}
@@ -1,4 +1,4 @@
1
- import { __name, __publicField } from '../../chunk-JTFKMR4I.mjs';
1
+ import { __name, __publicField } from '../../chunk-S573YWRP.mjs';
2
2
  import { Store } from '@sapphire/pieces';
3
3
  import { Middleware } from './Middleware.mjs';
4
4
 
@@ -10,10 +10,10 @@ var _MiddlewareStore = class _MiddlewareStore extends Store {
10
10
  */
11
11
  __publicField(this, "sortedMiddlewares", []);
12
12
  }
13
- async run(request, response, route) {
13
+ async run(request, response) {
14
14
  for (const middleware of this.sortedMiddlewares) {
15
15
  if (response.writableEnded) return;
16
- if (middleware.enabled) await middleware.run(request, response, route);
16
+ if (middleware.enabled) await middleware.run(request, response);
17
17
  }
18
18
  }
19
19
  set(key, value) {
@@ -36,5 +36,5 @@ __name(_MiddlewareStore, "MiddlewareStore");
36
36
  var MiddlewareStore = _MiddlewareStore;
37
37
 
38
38
  export { MiddlewareStore };
39
- //# sourceMappingURL=out.js.map
39
+ //# sourceMappingURL=MiddlewareStore.mjs.map
40
40
  //# sourceMappingURL=MiddlewareStore.mjs.map
@@ -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":";;;;AAMO,IAAM,gBAAA,GAAN,MAAM,gBAAA,SAAwB,KAAiC,CAAA;AAAA,EAM9D,WAAc,GAAA;AACpB,IAAA,KAAA,CAAM,UAAY,EAAA,EAAE,IAAM,EAAA,aAAA,EAAe,CAAA,CAAA;AAH1C;AAAA;AAAA;AAAA,IAAA,aAAA,CAAA,IAAA,EAAgB,qBAAkC,EAAC,CAAA,CAAA;AAAA,GAInD;AAAA,EAEA,MAAa,GAAI,CAAA,OAAA,EAA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AAN,GAAA","file":"MiddlewareStore.mjs","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,8 +1,7 @@
1
- import { __name, __publicField } from '../../chunk-JTFKMR4I.mjs';
1
+ import { __name, __publicField } from '../../chunk-S573YWRP.mjs';
2
2
  import { Piece } from '@sapphire/pieces';
3
- import { Collection } from 'discord.js';
4
- import { RouteData } from '../utils/RouteData.mjs';
5
- import { methodEntries } from './http/HttpMethods.mjs';
3
+ import { isNullish } from '@sapphire/utilities';
4
+ import { RouterRoot } from './router/RouterRoot.mjs';
6
5
 
7
6
  var _Route = class _Route extends Piece {
8
7
  constructor(context, options = {}) {
@@ -16,50 +15,34 @@ var _Route = class _Route extends Piece {
16
15
  */
17
16
  __publicField(this, "acceptedContentMimeTypes");
18
17
  /**
19
- * The route information.
18
+ * The path this route represents.
20
19
  */
21
- __publicField(this, "router");
20
+ __publicField(this, "path");
22
21
  /**
23
22
  * The methods this route accepts.
24
23
  */
25
- __publicField(this, "methods", new Collection());
24
+ __publicField(this, "methods");
26
25
  const api = this.container.server.options;
27
- const prefix = api.prefix ? api.prefix.endsWith("/") ? api.prefix : `${api.prefix}/` : "";
28
- const path = options.route ?? (this.location.virtual ? this.name : this.location.directories.concat(this.name).join("/"));
29
- this.router = new RouteData(`${prefix}${path}`);
30
- for (const [method, symbol] of methodEntries) {
31
- const value = Reflect.get(this, symbol);
32
- if (typeof value === "function") this.methods.set(method, value);
26
+ const path = [].concat(
27
+ RouterRoot.normalize(api.prefix),
28
+ RouterRoot.normalize(options.route ?? (this.location.virtual ? this.name : this.location.directories.concat(this.name).join("/")))
29
+ );
30
+ const methods = new Set(options.methods);
31
+ const implied = RouterRoot.extractMethod(path);
32
+ if (!isNullish(implied)) {
33
+ const lastIndex = path.length - 1;
34
+ path[lastIndex] = path[lastIndex].slice(0, path[lastIndex].length - implied.length - 1);
35
+ methods.add(implied);
33
36
  }
37
+ this.path = path;
38
+ this.methods = methods;
34
39
  this.maximumBodyLength = options.maximumBodyLength ?? api.maximumBodyLength ?? 1024 * 1024 * 50;
35
40
  this.acceptedContentMimeTypes = options.acceptedContentMimeTypes ?? api.acceptedContentMimeTypes ?? null;
36
41
  }
37
- /**
38
- * Per-piece listener that is called when the piece is loaded into the store.
39
- * Useful to set-up asynchronous initialization tasks.
40
- */
41
- onLoad() {
42
- const store = this.store;
43
- for (const [method, cb] of this.methods) {
44
- store.table.get(method).set(this, cb.bind(this));
45
- }
46
- return void 0;
47
- }
48
- /**
49
- * Per-piece listener that is called when the piece is unloaded from the store.
50
- * Useful to set-up clean-up tasks.
51
- */
52
- onUnload() {
53
- const store = this.store;
54
- for (const [method] of this.methods) {
55
- store.table.get(method).delete(this);
56
- }
57
- return void 0;
58
- }
59
42
  };
60
43
  __name(_Route, "Route");
61
44
  var Route = _Route;
62
45
 
63
46
  export { Route };
64
- //# sourceMappingURL=out.js.map
47
+ //# sourceMappingURL=Route.mjs.map
65
48
  //# sourceMappingURL=Route.mjs.map
@@ -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":";;;;;AA6CO,IAAe,MAAA,GAAf,MAAe,MAAA,SAA6D,KAAyB,CAAA;AAAA,EAqBpG,WAAY,CAAA,OAAA,EAA8B,OAAmB,GAAA,EAAe,EAAA;AAClF,IAAA,KAAA,CAAM,SAAS,OAAO,CAAA,CAAA;AAlBvB;AAAA;AAAA;AAAA,IAAgB,aAAA,CAAA,IAAA,EAAA,mBAAA,CAAA,CAAA;AAKhB;AAAA;AAAA;AAAA,IAAgB,aAAA,CAAA,IAAA,EAAA,0BAAA,CAAA,CAAA;AAKhB;AAAA;AAAA;AAAA,IAAgB,aAAA,CAAA,IAAA,EAAA,MAAA,CAAA,CAAA;AAKhB;AAAA;AAAA;AAAA,IAAgB,aAAA,CAAA,IAAA,EAAA,SAAA,CAAA,CAAA;AAKf,IAAM,MAAA,GAAA,GAAM,IAAK,CAAA,SAAA,CAAU,MAAO,CAAA,OAAA,CAAA;AAClC,IAAM,MAAA,IAAA,GAAQ,EAAgB,CAAA,MAAA;AAAA,MAC7B,UAAA,CAAW,SAAU,CAAA,GAAA,CAAI,MAAM,CAAA;AAAA,MAC/B,WAAW,SAAU,CAAA,OAAA,CAAQ,UAAU,IAAK,CAAA,QAAA,CAAS,UAAU,IAAK,CAAA,IAAA,GAAO,IAAK,CAAA,QAAA,CAAS,YAAY,MAAO,CAAA,IAAA,CAAK,IAAI,CAAE,CAAA,IAAA,CAAK,GAAG,CAAE,CAAA,CAAA;AAAA,KAClI,CAAA;AAEA,IAAA,MAAM,OAAU,GAAA,IAAI,GAAI,CAAA,OAAA,CAAQ,OAAO,CAAA,CAAA;AACvC,IAAM,MAAA,OAAA,GAAU,UAAW,CAAA,aAAA,CAAc,IAAI,CAAA,CAAA;AAC7C,IAAI,IAAA,CAAC,SAAU,CAAA,OAAO,CAAG,EAAA;AACxB,MAAM,MAAA,SAAA,GAAY,KAAK,MAAS,GAAA,CAAA,CAAA;AAChC,MAAA,IAAA,CAAK,SAAS,CAAA,GAAI,IAAK,CAAA,SAAS,CAAE,CAAA,KAAA,CAAM,CAAG,EAAA,IAAA,CAAK,SAAS,CAAA,CAAE,MAAS,GAAA,OAAA,CAAQ,SAAS,CAAC,CAAA,CAAA;AACtF,MAAA,OAAA,CAAQ,IAAI,OAAqB,CAAA,CAAA;AAAA,KAClC;AAEA,IAAA,IAAA,CAAK,IAAO,GAAA,IAAA,CAAA;AACZ,IAAA,IAAA,CAAK,OAAU,GAAA,OAAA,CAAA;AACf,IAAA,IAAA,CAAK,oBAAoB,OAAQ,CAAA,iBAAA,IAAqB,GAAI,CAAA,iBAAA,IAAqB,OAAO,IAAO,GAAA,EAAA,CAAA;AAC7F,IAAA,IAAA,CAAK,wBAA2B,GAAA,OAAA,CAAQ,wBAA4B,IAAA,GAAA,CAAI,wBAA4B,IAAA,IAAA,CAAA;AAAA,GACrG;AAGD,CAAA,CAAA;AA7C4G,MAAA,CAAA,MAAA,EAAA,OAAA,CAAA,CAAA;AAArG,IAAe,KAAf,GAAA","file":"Route.mjs","sourcesContent":["import { Piece } from '@sapphire/pieces';\nimport { isNullish, type Awaitable } from '@sapphire/utilities';\nimport type { ApiRequest } from './api/ApiRequest';\nimport type { ApiResponse } from './api/ApiResponse';\nimport type { MethodName } from './http/HttpMethods';\nimport 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,17 @@
1
+ import { __name } from '../../chunk-S573YWRP.mjs';
2
+ import { LoaderStrategy } from '@sapphire/pieces';
3
+
4
+ var _RouteLoaderStrategy = class _RouteLoaderStrategy extends LoaderStrategy {
5
+ onLoad(store, piece) {
6
+ store.router.add(piece);
7
+ }
8
+ onUnload(store, piece) {
9
+ store.router.remove(piece);
10
+ }
11
+ };
12
+ __name(_RouteLoaderStrategy, "RouteLoaderStrategy");
13
+ var RouteLoaderStrategy = _RouteLoaderStrategy;
14
+
15
+ export { RouteLoaderStrategy };
16
+ //# sourceMappingURL=RouteLoaderStrategy.mjs.map
17
+ //# sourceMappingURL=RouteLoaderStrategy.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/lib/structures/RouteLoaderStrategy.ts"],"names":[],"mappings":";;;AAIO,IAAM,oBAAA,GAAN,MAAM,oBAAA,SAA4B,cAAsB,CAAA;AAAA,EAC9C,MAAA,CAAO,OAAmB,KAAoB,EAAA;AAC7D,IAAM,KAAA,CAAA,MAAA,CAAO,IAAI,KAAK,CAAA,CAAA;AAAA,GACvB;AAAA,EAEgB,QAAA,CAAS,OAAmB,KAAoB,EAAA;AAC/D,IAAM,KAAA,CAAA,MAAA,CAAO,OAAO,KAAK,CAAA,CAAA;AAAA,GAC1B;AACD,CAAA,CAAA;AAR+D,MAAA,CAAA,oBAAA,EAAA,qBAAA,CAAA,CAAA;AAAxD,IAAM,mBAAN,GAAA","file":"RouteLoaderStrategy.mjs","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
- import { __name, __publicField } from '../../chunk-JTFKMR4I.mjs';
1
+ import { __name, __publicField } from '../../chunk-S573YWRP.mjs';
2
2
  import { Store } from '@sapphire/pieces';
3
- import { Collection } from 'discord.js';
4
- import { URLSearchParams } from 'url';
5
- import { methodEntries } from './http/HttpMethods.mjs';
6
3
  import { Route } from './Route.mjs';
4
+ import { RouteLoaderStrategy } from './RouteLoaderStrategy.mjs';
5
+ import { RouterRoot } from './router/RouterRoot.mjs';
7
6
 
8
- var slash = "/".charCodeAt(0);
9
7
  var _RouteStore = class _RouteStore extends Store {
10
8
  constructor() {
11
- super(Route, { name: "routes" });
12
- __publicField(this, "table", new Collection());
13
- for (const [method] of methodEntries) this.table.set(method, new Collection());
14
- }
15
- match(request) {
16
- const { method } = request;
17
- if (typeof method === "undefined") {
18
- return null;
19
- }
20
- const methodTable = this.table.get(method);
21
- if (typeof methodTable === "undefined") {
22
- return null;
23
- }
24
- const { splits, querystring } = this.parseURL(request.url);
25
- for (const [route, cb] of methodTable.entries()) {
26
- const result = route.router.match(splits);
27
- if (result === null) continue;
28
- request.params = result;
29
- request.query = Object.fromEntries(new URLSearchParams(querystring).entries());
30
- return { route, cb };
31
- }
32
- return null;
33
- }
34
- parseURL(url = "") {
35
- const index = url.indexOf("?");
36
- let pathname;
37
- let querystring;
38
- if (index === -1) {
39
- pathname = url;
40
- querystring = "";
41
- } else {
42
- pathname = url.substring(0, index);
43
- querystring = url.substring(index + 1);
44
- }
45
- if (pathname.charCodeAt(0) === slash) pathname = pathname.substring(1);
46
- if (pathname.length > 0 && pathname.charCodeAt(pathname.length - 1) === slash) pathname = pathname.substring(0, pathname.length - 1);
47
- const splits = pathname.split("/");
48
- return { splits, querystring };
9
+ super(Route, { name: "routes", strategy: new RouteLoaderStrategy() });
10
+ __publicField(this, "router", new RouterRoot());
49
11
  }
50
12
  };
51
13
  __name(_RouteStore, "RouteStore");
52
14
  var RouteStore = _RouteStore;
53
15
 
54
16
  export { RouteStore };
55
- //# sourceMappingURL=out.js.map
17
+ //# sourceMappingURL=Rou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U,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":";;;;;;AAQO,IAAM,WAAA,GAAN,MAAM,WAAA,SAAmB,KAAuB,CAAA;AAAA,EAG/C,WAAc,GAAA;AACpB,IAAM,KAAA,CAAA,KAAA,EAAO,EAAE,IAAM,EAAA,QAAA,EAAU,UAAU,IAAI,mBAAA,IAAuB,CAAA,CAAA;AAHrE,IAAgB,aAAA,CAAA,IAAA,EAAA,QAAA,EAAS,IAAI,UAAW,EAAA,CAAA,CAAA;AAAA,GAIxC;AACD,CAAA,CAAA;AANuD,MAAA,CAAA,WAAA,EAAA,YAAA,CAAA,CAAA;AAAhD,IAAM,UAAN,GAAA","file":"RouteStore.mjs","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"]}
@@ -1,4 +1,4 @@
1
- import { __name, __publicField } from '../../../chunk-JTFKMR4I.mjs';
1
+ import { __name, __publicField } from '../../../chunk-S573YWRP.mjs';
2
2
  import { IncomingMessage } from 'node:http';
3
3
 
4
4
  var _ApiRequest = class _ApiRequest extends IncomingMessage {
@@ -24,11 +24,37 @@ var _ApiRequest = class _ApiRequest extends IncomingMessage {
24
24
  * - `AuthData`: The user is authorized.
25
25
  */
26
26
  __publicField(this, "auth");
27
+ /**
28
+ * The router node that matched the request. The field indicates three
29
+ * possible values:
30
+ *
31
+ * - `undefined`: The router handler has not been executed yet.
32
+ * - `null`: The router handler has been executed, but no node matched the
33
+ * request.
34
+ * - `RouterNode`: The router handler has been executed and a node matched
35
+ * the request.
36
+ *
37
+ * @since 7.0.0
38
+ */
39
+ __publicField(this, "routerNode");
40
+ /**
41
+ * The route that matched the request. The field indicates three possible
42
+ * values:
43
+ *
44
+ * - `undefined`: The router handler has not been executed yet.
45
+ * - `null`: The router handler has been executed, but no route matched the
46
+ * request.
47
+ * - `Route`: The router handler has been executed and a route matched the
48
+ * request.
49
+ *
50
+ * @since 7.0.0
51
+ */
52
+ __publicField(this, "route");
27
53
  }
28
54
  };
29
55
  __name(_ApiRequest, "ApiRequest");
30
56
  var ApiRequest = _ApiRequest;
31
57
 
32
58
  export { ApiRequest };
33
- //# sourceMappingURL=out.js.map
59
+ //# sourceMappingURL=ApiRequest.mjs.map
34
60
  //# sourceMappingURL=ApiRequest.mjs.map
@@ -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":";;;AAKO,IAAM,WAAA,GAAN,MAAM,WAAA,SAAmB,eAAgB,CAAA;AAAA,EAAzC,WAAA,GAAA;AAAA,IAAA,KAAA,CAAA,GAAA,SAAA,CAAA,CAAA;AAIN;AAAA;AAAA;AAAA,IAAA,aAAA,CAAA,IAAA,EAAO,SAA2C,EAAC,CAAA,CAAA;AAKnD;AAAA;AAAA;AAAA,IAAA,aAAA,CAAA,IAAA,EAAO,UAAiC,EAAC,CAAA,CAAA;AAKzC;AAAA;AAAA;AAAA,IAAO,aAAA,CAAA,IAAA,EAAA,MAAA,CAAA,CAAA;AASP;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAO,aAAA,CAAA,IAAA,EAAA,MAAA,CAAA,CAAA;AAcP;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAO,aAAA,CAAA,IAAA,EAAA,YAAA,CAAA,CAAA;AAcP;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAO,aAAA,CAAA,IAAA,EAAA,OAAA,CAAA,CAAA;AAAA,GAAA;AACR,CAAA,CAAA;AApDgD,MAAA,CAAA,WAAA,EAAA,YAAA,CAAA,CAAA;AAAzC,IAAM,UAAN,GAAA","file":"ApiRequest.mjs","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"]}
@@ -1,4 +1,4 @@
1
- import { __name, __publicField } from '../../../chunk-JTFKMR4I.mjs';
1
+ import { __name, __publicField } from '../../../chunk-S573YWRP.mjs';
2
2
  import { ServerResponse, STATUS_CODES } from 'node:http';
3
3
  import { Readable } from 'node:stream';
4
4
  import 'node:stream/web';
@@ -58,6 +58,12 @@ var _ApiResponse = class _ApiResponse extends ServerResponse {
58
58
  notFound(data) {
59
59
  return this.error(HttpCodes.NotFound, data);
60
60
  }
61
+ /**
62
+ * @since 7.0.0
63
+ */
64
+ methodNotAllowed(data) {
65
+ return this.error(HttpCodes.MethodNotAllowed, data);
66
+ }
61
67
  /**
62
68
  * @since 1.0.0
63
69
  */
@@ -132,5 +138,5 @@ __name(_ApiResponse, "ApiResponse");
132
138
  var ApiResponse = _ApiResponse;
133
139
 
134
140
  export { ApiResponse };
135
- //# sourceMappingURL=out.js.map
141
+ //# sourceMappingURL=ApiResponse.mjs.map
136
142
  //# sourceMappingURL=ApiResponse.mjs.map
@@ -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,M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;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":";;;;;;;AAUO,IAAM,YAAA,GAAN,MAAM,YAAA,SAAuE,cAAwB,CAAA;AAAA,EAArG,WAAA,GAAA;AAAA,IAAA,KAAA,CAAA,GAAA,SAAA,CAAA,CAAA;AAIN;AAAA;AAAA;AAAA,IAAO,aAAA,CAAA,IAAA,EAAA,SAAA,CAAA,CAAA;AAAA,GAAA;AAAA;AAAA;AAAA;AAAA,EAKA,EAAG,CAAA,IAAA,GAAgB,YAAa,CAAA,SAAA,CAAU,EAAE,CAAS,EAAA;AAC3D,IAAK,IAAA,CAAA,MAAA,CAAO,UAAU,EAAE,CAAA,CAAA;AACxB,IAAO,OAAA,IAAA,CAAK,QAAQ,IAAI,CAAA,CAAA;AAAA,GACzB;AAAA;AAAA;AAAA;AAAA,EAKO,OAAQ,CAAA,IAAA,GAAgB,YAAa,CAAA,SAAA,CAAU,OAAO,CAAS,EAAA;AACrE,IAAK,IAAA,CAAA,MAAA,CAAO,UAAU,OAAO,CAAA,CAAA;AAC7B,IAAO,OAAA,IAAA,CAAK,QAAQ,IAAI,CAAA,CAAA;AAAA,GACzB;AAAA;AAAA;AAAA;AAAA,EAKO,SAAU,CAAA,IAAA,GAAgB,YAAa,CAAA,SAAA,CAAU,SAAS,CAAS,EAAA;AACzE,IAAK,IAAA,CAAA,MAAA,CAAO,UAAU,SAAS,CAAA,CAAA;AAC/B,IAAO,OAAA,IAAA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AA,CAAA;AAAA,GAC9D;AAAA;AAAA;AAAA;AAAA,EAKO,eAAe,WAA8B,EAAA;AACnD,IAAK,IAAA,CAAA,SAAA,CAAU,gBAAgB,WAAW,CAAA,CAAA;AAC1C,IAAO,OAAA,IAAA,CAAA;AAAA,GACR;AACD,CAAA,CAAA;AAlJ4G,MAAA,CAAA,YAAA,EAAA,aAAA,CAAA,CAAA;AAArG,IAAM,WAAN,GAAA","file":"ApiResponse.mjs","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"]}
@@ -1,4 +1,4 @@
1
- import { __name, __publicField } from '../../../chunk-JTFKMR4I.mjs';
1
+ import { __name, __publicField } from '../../../chunk-S573YWRP.mjs';
2
2
  import { getDomain } from 'tldts';
3
3
 
4
4
  var _CookieStore = class _CookieStore extends Map {
@@ -15,8 +15,8 @@ var _CookieStore = class _CookieStore extends Map {
15
15
  for (const pair of pairs) {
16
16
  const index = pair.indexOf("=");
17
17
  if (index === -1) continue;
18
- const key = decodeURIComponent(pair.substr(0, index).trim());
19
- const value = decodeURIComponent(pair.substr(index + 1).trim());
18
+ const key = decodeURIComponent(pair.slice(0, index).trim());
19
+ const value = decodeURIComponent(pair.slice(index + 1).trim());
20
20
  this.set(key, value);
21
21
  }
22
22
  const [splitHost] = this.request.headers.host?.split(":") ?? [""];
@@ -39,7 +39,7 @@ var _CookieStore = class _CookieStore extends Map {
39
39
  } else if (!Array.isArray(set)) {
40
40
  set = [set.toString()];
41
41
  }
42
- set = set.filter((i) => i.substr(0, i.indexOf("=")) !== name);
42
+ set = set.filter((i) => i.slice(0, i.indexOf("=")) !== name);
43
43
  set.push(entry);
44
44
  this.response.setHeader("Set-Cookie", set);
45
45
  }
@@ -92,5 +92,5 @@ __publicField(_CookieStore, "octetRegExp", /[^\x21\x23-\x2B\x2D-\x3A\x3C-\x5B\x5
92
92
  var CookieStore = _CookieStore;
93
93
 
94
94
  export { CookieStore };
95
- //# sourceMappingURL=out.js.map
95
+ //# sourceMappingURL=CookieStore.mjs.map
96
96
  //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AAAA,IAC7C;AAEA,WAAO,mBAAmB,KAAK;AAAA,EAChC;AACD;AA9HqD;AAAA;AAqHpD,cArHY,cAqHY,eAAc;AArHhC,IAAM,cAAN","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":";;;AAOO,IAAM,YAAA,GAAN,MAAM,YAAA,SAAoB,GAAoB,CAAA;AAAA,EAM7C,WAAY,CAAA,OAAA,EAAqB,QAAuB,EAAA,MAAA,EAAiB,eAAiC,EAAA;AAChH,IAAM,KAAA,EAAA,CAAA;AANP,IAAU,aAAA,CAAA,IAAA,EAAA,SAAA,CAAA,CAAA;AACV,IAAU,aAAA,CAAA,IAAA,EAAA,UAAA,CAAA,CAAA;AACV,IAAQ,aAAA,CAAA,IAAA,EAAA,QAAA,CAAA,CAAA;AACR,IAAQ,aAAA,CAAA,IAAA,EAAA,QAAA,CAAA,CAAA;AAKP,IAAA,IAAA,CAAK,OAAU,GAAA,OAAA,CAAA;AACf,IAAA,IAAA,CAAK,QAAW,GAAA,QAAA,CAAA;AAGhB,IAAA,MAAM,EAAE,MAAA,GAAS,EAAG,EAAA,GAAI,OAAQ,CAAA,OAAA,CAAA;AAChC,IAAM,MAAA,KAAA,GAAQ,MAAO,CAAA,KAAA,CAAM,GAAG,CAAA,CAAA;AAE9B,IAAA,KAAA,MAAW,QAAQ,KAAO,EAAA;AACzB,MAAM,MAAA,KAAA,GAAQ,IAAK,CAAA,OAAA,CAAQ,GAAG,CAAA,CAAA;AAC9B,MAAA,IAAI,UAAU,CAAI,CAAA,EAAA,SAAA;AAElB,MAAM,MAAA,GAAA,GAAM,mBAAmB,IAAK,CAAA,K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,MAAA,EAAQ,WAAY,EAAA,CAAA;AAE7C,IAAA,KAAA,IAAS,YAAY,MAAM,CAAA,CAAA,CAAA;AAC3B,IAAS,KAAA,IAAA,CAAA,OAAA,EAAU,QAAQ,GAAG,CAAA,CAAA,CAAA;AAE9B,IAAA,IAAI,KAAK,MAAQ,EAAA;AAChB,MAAS,KAAA,IAAA,CAAA,QAAA,CAAA,CAAA;AAAA,KACV;AAEA,IAAA,IAAI,YAAY,IAAM,EAAA;AACrB,MAAS,KAAA,IAAA,CAAA,UAAA,CAAA,CAAA;AAAA,KACV;AAEA,IAAO,OAAA,KAAA,CAAA;AAAA,GACR;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQQ,cAAc,IAAsB,EAAA;AAE3C,IAAM,MAAA,aAAA,GAAgB,KAAK,WAAY,EAAA,CAAA;AAGvC,IAAM,MAAA,aAAA,GAAgB,UAAU,aAAa,CAAA,CAAA;AAG7C,IAAI,IAAA,CAAC,eAAsB,OAAA,aAAA,CAAA;AAG3B,IAAA,OAAO,IAAI,aAAa,CAAA,CAAA,CAAA;AAAA,GACzB;AAAA,EAKA,OAAe,kBAAkB,KAAe,EAAA;AAC/C,IAAA,IAAI,YAAY,CAAA,WAAA,CAAY,IAAK,CAAA,KAAK,CAAG,EAAA;AACxC,MAAM,MAAA,IAAI,MAAM,CAA4B,0BAAA,CAAA,CAAA,CAAA;AAAA,KAC7C;AAEA,IAAA,OAAO,mBAAmB,KAAK,CAAA,CAAA;AAAA,GAChC;AACD,CAAA,CAAA;AA9HqD,MAAA,CAAA,YAAA,EAAA,aAAA,CAAA,CAAA;AAAA;AAqHpD,aAAA,CArHY,cAqHY,aAAc,EAAA,8CAAA,CAAA,CAAA;AArHhC,IAAM,WAAN,GAAA","file":"CookieStore.mjs","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,4 +1,4 @@
1
- import { __name, __publicField, __privateAdd, __privateSet, __privateGet } from '../../../chunk-JTFKMR4I.mjs';
1
+ import { __name, __publicField, __privateAdd, __privateSet, __privateGet } from '../../../chunk-S573YWRP.mjs';
2
2
  import { isThenable } from '@sapphire/utilities';
3
3
  import { randomBytes, createCipheriv, createDecipheriv } from 'crypto';
4
4
  import { OAuth2Scopes, RouteBases, Routes } from 'discord.js';
@@ -116,5 +116,5 @@ __name(_Auth, "Auth");
116
116
  var Auth = _Auth;
117
117
 
118
118
  export { Auth };
119
- //# sourceMappingURL=out.js.map
119
+ //# sourceMappingURL=Auth.mjs.map
120
120
  //# sourceMappingURL=Auth.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../src/lib/structures/http/Auth.ts"],"names":[],"mappings":";;;;;;;;;AAAA,SAAS,kBAAkC;AAC3C,SAAS,gBAAgB,kBAAkB,mBAAmB;AAC9D;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,OAKM;AACP,SAAS,aAAa;AAXtB;AAaO,IAAM,QAAN,MAAM,MAAK;AAAA,EAmCT,YAAY,SAA4B;AA9BhD;AAAA;AAAA;AAAA;AAAA,wBAAO;AAMP;AAAA;AAAA;AAAA;AAAA,wBAAO;AAMP;AAAA;AAAA;AAAA;AAAA,wBAAO;AAMP;AAAA;AAAA;AAAA;AAAA,wBAAO;AAMP;AAAA;AAAA;AAAA;AAAA,wBAAO;AAEP,wBAAO,mBAAiC;AAExC;AAGC,SAAK,KAAK,QAAQ;AAClB,SAAK,SAAS,QAAQ,UAAU;AAChC,SAAK,SAAS,QAAQ,UAAU,CAAC,aAAa,QAAQ;AACtD,SAAK,WAAW,QAAQ;AACxB,uBAAK,SAAU,QAAQ;AACvB,SAAK,eAAe,QAAQ,gBAAgB,CAAC;AAC7C,SAAK,kBAAkB,QAAQ,mBAAmB;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MAAM,QAAQ,YAAY;AAClD,eAAW,eAAe,KAAK,cAAc;AAC5C,YAAM,SAAS,YAAY,IAAI;AAC/B,UAAI,WAAW,MAAM,EAAG,QAAO,MAAM;AAAA,UAChC,QAAO;AAAA,IACb;AAEA,WAAO;AAAA,EACR;AAAA,EAEA,MAAc,iBAAoB,OAAqB,OAAe,KAA4C;AACjH,QAAI,CAAC,KAAK,OAAO,SAAS,KAAK,EAAG,QAAO;AAEzC,UAAM,SAAS,MAAM,MAAM,KAAK;AAAA,MAC/B,SAAS;AAAA,QACR,eAAe,UAAU,KAAK;AAAA,MAC/B;AAAA,IACD,CAAC;AAED,WAAO,OAAO,KAAO,MAAM,OAAO,KAAK,IAAW;AAAA,EACnD;AAAA,EAEA,OAAc,OAAO,SAA0C;AAC9D,QAAI,CAAC,SAAS,UAAU,CAAC,QAAQ,GAAI,QAAO;AAC5C,WAAO,IAAI,MAAK,OAAO;AAAA,EACxB;AACD;AA7FC;AAjCiB;AAAX,IAAM,OAAN","sourcesContent":["import { isThenable, type Awaitable } from '@sapphire/utilities';\nimport { createCipheriv, createDecipheriv, randomBytes } from 'crypto';\nimport {\n\tOAuth2Scopes,\n\tRouteBases,\n\tRoutes,\n\ttype RESTGetAPICurrentUserConnectionsResult,\n\ttype RESTGetAPICurrentUserGuildsResult,\n\ttype RESTGetAPICurrentUserResult,\n\ttype Snowflake\n} from 'discord.js';\nimport { fetch } from 'undici';\n\nexport class Auth {\n\t/**\n\t * The client's application id, this can be retrieved in Discord Developer Portal at https://discord.com/developers/applications.\n\t * @since 1.0.0\n\t */\n\tpublic id: Snowflake;\n\n\t/**\n\t * The name for the cookie, this will be used to identify a Secure HttpOnly cookie.\n\t * @since 1.0.0\n\t */\n\tpublic cookie: string;\n\n\t/**\n\t * The scopes defined at https://discord.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes.\n\t * @since 1.0.0\n\t */\n\tpublic scopes: readonly OAuth2Scopes[];\n\n\t/**\n\t * The redirect uri.\n\t * @since 1.0.0\n\t */\n\tpublic redirect: string | undefined;\n\n\t/**\n\t * The transformers used for {@link Auth.fetchData}.\n\t * @since 1.4.0\n\t */\n\tpublic transformers: LoginDataTransformer[];\n\n\tpublic domainOverwrite: string | null = null;\n\n\t#secret: string;\n\n\tprivate constructor(options: ServerOptionsAuth) {\n\t\tthis.id = options.id as Snowflake;\n\t\tthis.cookie = options.cookie ?? 'SAPPHIRE_AUTH';\n\t\tthis.scopes = options.scopes ?? [OAuth2Scopes.Identify];\n\t\tthis.redirect = options.redirect;\n\t\tthis.#secret = options.secret;\n\t\tthis.transformers = options.transformers ?? [];\n\t\tthis.domainOverwrite = options.domainOverwrite ?? null;\n\t}\n\n\t/**\n\t * The client secret, this can be retrieved in Discord Developer Portal at https://discord.com/developers/applications.\n\t * @since 1.0.0\n\t */\n\tpublic get secret() {\n\t\treturn this.#secret;\n\t}\n\n\t/**\n\t * Encrypts an object with aes-256-cbc to use as a token.\n\t * @since 1.0.0\n\t * @param data An object to encrypt\n\t */\n\tpublic encrypt(data: AuthData): string {\n\t\tconst iv = randomBytes(16);\n\t\tconst cipher = createCipheriv('aes-256-cbc', this.#secret, iv);\n\t\treturn `${cipher.update(JSON.stringify(data), 'utf8', 'base64') + cipher.final('base64')}.${iv.toString('base64')}`;\n\t}\n\n\t/**\n\t * Decrypts an object with aes-256-cbc to use as a token.\n\t * @since 1.0.0\n\t * @param token An data to decrypt\n\t */\n\tpublic decrypt(token: string): AuthData | null {\n\t\tconst [data, iv] = token.split('.');\n\t\tconst decipher = createDecipheriv('aes-256-cbc', this.#secret, Buffer.from(iv, 'base64'));\n\n\t\ttry {\n\t\t\tconst parsed = JSON.parse(decipher.update(data, 'base64', 'utf8') + decipher.final('utf8')) as AuthData;\n\t\t\t// If the token expired, return null:\n\t\t\treturn parsed.expires >= Date.now() ? parsed : null;\n\t\t} catch {\n\t\t\treturn null;\n\t\t}\n\t}\n\n\t/**\n\t * Retrieves the data for a specific user.\n\t * @since 1.4.0\n\t * @param token The access token from the user.\n\t */\n\tpublic async fetchData(token: string): Promise<LoginData> {\n\t\t// Fetch the information:\n\t\tconst [user, guilds, connections] = await Promise.all([\n\t\t\tthis.fetchInformation<RESTGetAPICurrentUserResult>(OAuth2Scopes.Identify, token, `${RouteBases.api}${Routes.user()}`),\n\t\t\tthis.fetchInformation<RESTGetAPICurrentUserGuildsResult>(OAuth2Scopes.Guilds, token, `${RouteBases.api}${Routes.userGuilds()}`),\n\t\t\tthis.fetchInformation<RESTGetAPICurrentUserConnectionsResult>(\n\t\t\t\tOAuth2Scopes.Connections,\n\t\t\t\ttoken,\n\t\t\t\t`${RouteBases.api}${Routes.userConnections()}`\n\t\t\t)\n\t\t]);\n\n\t\t// Transform the information:\n\t\tlet data: LoginData = { user, guilds, connections };\n\t\tfor (const transformer of this.transformers) {\n\t\t\tconst result = transformer(data);\n\t\t\tif (isThenable(result)) data = await result;\n\t\t\telse data = result as LoginData;\n\t\t}\n\n\t\treturn data;\n\t}\n\n\tprivate async fetchInformation<T>(scope: OAuth2Scopes, token: string, url: string): Promise<T | null | undefined> {\n\t\tif (!this.scopes.includes(scope)) return undefined;\n\n\t\tconst result = await fetch(url, {\n\t\t\theaders: {\n\t\t\t\tauthorization: `Bearer ${token}`\n\t\t\t}\n\t\t});\n\n\t\treturn result.ok ? ((await result.json()) as T) : null;\n\t}\n\n\tpublic static create(options?: ServerOptionsAuth): Auth | null {\n\t\tif (!options?.secret || !options.id) return null;\n\t\treturn new Auth(options);\n\t}\n}\n\n/**\n * Defines the authentication data, this is to be encrypted and decrypted by the server.\n * @since 1.0.0\n */\nexport interface AuthData {\n\t/**\n\t * The user ID.\n\t * @since 1.0.0\n\t */\n\tid: string;\n\n\t/**\n\t * The timestamp at which the token expires.\n\t * @since 1.0.0\n\t */\n\texpires: number;\n\n\t/**\n\t * The refresh token.\n\t * @since 1.0.0\n\t */\n\trefresh: string;\n\n\t/**\n\t * The access token.\n\t * @since 1.0.0\n\t */\n\ttoken: string;\n}\n\n/**\n * Defines the authentication options.\n * @since 1.0.0\n */\nexport interface ServerOptionsAuth {\n\t/**\n\t * The client's application id, this can be retrieved in Discord Developer Portal at https://discord.com/developers/applications.\n\t * @since 1.0.0\n\t */\n\tid: string;\n\n\t/**\n\t * The name for the cookie, this will be used to identify a Secure HttpOnly cookie.\n\t * @since 1.0.0\n\t * @default 'SAPPHIRE_AUTH'\n\t */\n\tcookie?: string;\n\n\t/**\n\t * The client secret, this can be retrieved in Discord Developer Portal at https://discord.com/developers/applications.\n\t * @since 1.0.0\n\t */\n\tsecret: string;\n\n\t/**\n\t * The scopes defined at https://discord.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes.\n\t * @since 1.0.0\n\t * @default [OAuth2Scopes.Identify]\n\t */\n\tscopes?: OAuth2Scopes[];\n\n\t/**\n\t * The redirect uri. This will default to {@link OAuth2BodyData.redirectUri} if missing.\n\t * @since 1.0.0\n\t */\n\tredirect?: string;\n\n\t/**\n\t * The login data transformers used for {@link Auth.fetchData}.\n\t * @since 1.4.0\n\t * @default []\n\t */\n\ttransformers?: LoginDataTransformer[];\n\t/**\n\t * The domain that should be used for the cookie. This overwrites the automatic detection of the domain.\n\t * @remark if you want to support subdomains (`one.example.two` and `two.example.com`) then you need to use prefix your domain with a `.`, for example `.example.com`\n\t * @since 2.1.0\n\t * @default undefined\n\t */\n\tdomainOverwrite?: string;\n}\n\n/**\n * The login data sent when fetching data from a user.\n * @since 1.4.0\n */\nexport interface LoginData {\n\t/**\n\t * The user data, defined when the `'identify'` scope is defined.\n\t * @since 1.4.0\n\t */\n\tuser?: RESTGetAPICurrentUserResult | null;\n\n\t/**\n\t * The guilds data, defined when the `'guilds'` scope is defined.\n\t * @since 1.4.0\n\t */\n\tguilds?: RESTGetAPICurrentUserGuildsResult | null;\n\n\t/**\n\t * The connections data, defined when the `'connections'` scope is defined.\n\t * @since 1.4.0\n\t */\n\tconnections?: RESTGetAPICurrentUserConnectionsResult | null;\n}\n\n/**\n * A login data transformer.\n * @since 1.4.0\n */\nexport interface LoginDataTransformer<T extends LoginData = LoginData> {\n\t/**\n\t * Transforms the object by mutating its properties or adding new ones.\n\t * @since 1.4.0\n\t */\n\t(data: LoginData): Awaitable<T>;\n}\n"]}
1
+ {"version":3,"sources":["../../../../../src/lib/structures/http/Auth.ts"],"names":[],"mappings":";;;;;;AAAA,IAAA,OAAA,CAAA;AAaO,IAAM,KAAA,GAAN,MAAM,KAAK,CAAA;AAAA,EAmCT,YAAY,OAA4B,EAAA;AA9BhD;AAAA;AAAA;AAAA;AAAA,IAAO,aAAA,CAAA,IAAA,EAAA,IAAA,CAAA,CAAA;AAMP;AAAA;AAAA;AAAA;AAAA,IAAO,aAAA,CAAA,IAAA,EAAA,QAAA,CAAA,CAAA;AAMP;AAAA;AAAA;AAAA;AAAA,IAAO,aAAA,CAAA,IAAA,EAAA,QAAA,CAAA,CAAA;AAMP;AAAA;AAAA;AAAA;AAAA,IAAO,aAAA,CAAA,IAAA,EAAA,UAAA,CAAA,CAAA;AAMP;AAAA;AAAA;AAAA;AAAA,IAAO,aAAA,CAAA,IAAA,EAAA,cAAA,CAAA,CAAA;AAEP,IAAA,aAAA,CAAA,IAAA,EAAO,iBAAiC,EAAA,IAAA,CAAA,CAAA;AAExC,IAAA,YAAA,CAAA,IAAA,EAAA,OAAA,CAAA,CAAA;AAGC,IAAA,IAAA,CAAK,KAAK,OAAQ,CAAA,EAAA,CAAA;AAClB,IAAK,IAAA,CAAA,MAAA,GAAS,QAAQ,MAAU,IAAA,eAAA,CAAA;AAChC,IAAA,IAAA,CAAK,MAAS,GAAA,OAAA,CAAQ,MAAU,IAAA,CAAC,aAAa,QAAQ,CAAA,CAAA;AACtD,IAAA,IAAA,CAAK,WAAW,OAAQ,CAAA,QAAA,CAAA;AACxB,IAAA,YAAA,CAAA,IAAA,EAAK,SAAU,OAAQ,CAAA,MAAA,CAAA,CAAA;AACvB,IAAK,IAAA,CAAA,YAAA,GAAe,OAAQ,CAAA,YAAA,IAAgB,EAAC,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AK,GAAU,OAAA,KAAA,CAAA,CAAA;AAEzC,IAAM,MAAA,MAAA,GAAS,MAAM,KAAA,CAAM,GAAK,EAAA;AAAA,MAC/B,OAAS,EAAA;AAAA,QACR,aAAA,EAAe,UAAU,KAAK,CAAA,CAAA;AAAA,OAC/B;AAAA,KACA,CAAA,CAAA;AAED,IAAA,OAAO,MAAO,CAAA,EAAA,GAAO,MAAM,MAAA,CAAO,MAAgB,GAAA,IAAA,CAAA;AAAA,GACnD;AAAA,EAEA,OAAc,OAAO,OAA0C,EAAA;AAC9D,IAAA,IAAI,CAAC,OAAS,EAAA,MAAA,IAAU,CAAC,OAAA,CAAQ,IAAW,OAAA,IAAA,CAAA;AAC5C,IAAO,OAAA,IAAI,MAAK,OAAO,CAAA,CAAA;AAAA,GACxB;AACD,CAAA,CAAA;AA7FC,OAAA,GAAA,IAAA,OAAA,EAAA,CAAA;AAjCiB,MAAA,CAAA,KAAA,EAAA,MAAA,CAAA,CAAA;AAAX,IAAM,IAAN,GAAA","file":"Auth.mjs","sourcesContent":["import { isThenable, type Awaitable } from '@sapphire/utilities';\nimport { createCipheriv, createDecipheriv, randomBytes } from 'crypto';\nimport {\n\tOAuth2Scopes,\n\tRouteBases,\n\tRoutes,\n\ttype RESTGetAPICurrentUserConnectionsResult,\n\ttype RESTGetAPICurrentUserGuildsResult,\n\ttype RESTGetAPICurrentUserResult,\n\ttype Snowflake\n} from 'discord.js';\nimport { fetch } from 'undici';\n\nexport class Auth {\n\t/**\n\t * The client's application id, this can be retrieved in Discord Developer Portal at https://discord.com/developers/applications.\n\t * @since 1.0.0\n\t */\n\tpublic id: Snowflake;\n\n\t/**\n\t * The name for the cookie, this will be used to identify a Secure HttpOnly cookie.\n\t * @since 1.0.0\n\t */\n\tpublic cookie: string;\n\n\t/**\n\t * The scopes defined at https://discord.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes.\n\t * @since 1.0.0\n\t */\n\tpublic scopes: readonly OAuth2Scopes[];\n\n\t/**\n\t * The redirect uri.\n\t * @since 1.0.0\n\t */\n\tpublic redirect: string | undefined;\n\n\t/**\n\t * The transformers used for {@link Auth.fetchData}.\n\t * @since 1.4.0\n\t */\n\tpublic transformers: LoginDataTransformer[];\n\n\tpublic domainOverwrite: string | null = null;\n\n\t#secret: string;\n\n\tprivate constructor(options: ServerOptionsAuth) {\n\t\tthis.id = options.id as Snowflake;\n\t\tthis.cookie = options.cookie ?? 'SAPPHIRE_AUTH';\n\t\tthis.scopes = options.scopes ?? [OAuth2Scopes.Identify];\n\t\tthis.redirect = options.redirect;\n\t\tthis.#secret = options.secret;\n\t\tthis.transformers = options.transformers ?? [];\n\t\tthis.domainOverwrite = options.domainOverwrite ?? null;\n\t}\n\n\t/**\n\t * The client secret, this can be retrieved in Discord Developer Portal at https://discord.com/developers/applications.\n\t * @since 1.0.0\n\t */\n\tpublic get secret() {\n\t\treturn this.#secret;\n\t}\n\n\t/**\n\t * Encrypts an object with aes-256-cbc to use as a token.\n\t * @since 1.0.0\n\t * @param data An object to encrypt\n\t */\n\tpublic encrypt(data: AuthData): string {\n\t\tconst iv = randomBytes(16);\n\t\tconst cipher = createCipheriv('aes-256-cbc', this.#secret, iv);\n\t\treturn `${cipher.update(JSON.stringify(data), 'utf8', 'base64') + cipher.final('base64')}.${iv.toString('base64')}`;\n\t}\n\n\t/**\n\t * Decrypts an object with aes-256-cbc to use as a token.\n\t * @since 1.0.0\n\t * @param token An data to decrypt\n\t */\n\tpublic decrypt(token: string): AuthData | null {\n\t\tconst [data, iv] = token.split('.');\n\t\tconst decipher = createDecipheriv('aes-256-cbc', this.#secret, Buffer.from(iv, 'base64'));\n\n\t\ttry {\n\t\t\tconst parsed = JSON.parse(decipher.update(data, 'base64', 'utf8') + decipher.final('utf8')) as AuthData;\n\t\t\t// If the token expired, return null:\n\t\t\treturn parsed.expires >= Date.now() ? parsed : null;\n\t\t} catch {\n\t\t\treturn null;\n\t\t}\n\t}\n\n\t/**\n\t * Retrieves the data for a specific user.\n\t * @since 1.4.0\n\t * @param token The access token from the user.\n\t */\n\tpublic async fetchData(token: string): Promise<LoginData> {\n\t\t// Fetch the information:\n\t\tconst [user, guilds, connections] = await Promise.all([\n\t\t\tthis.fetchInformation<RESTGetAPICurrentUserResult>(OAuth2Scopes.Identify, token, `${RouteBases.api}${Routes.user()}`),\n\t\t\tthis.fetchInformation<RESTGetAPICurrentUserGuildsResult>(OAuth2Scopes.Guilds, token, `${RouteBases.api}${Routes.userGuilds()}`),\n\t\t\tthis.fetchInformation<RESTGetAPICurrentUserConnectionsResult>(\n\t\t\t\tOAuth2Scopes.Connections,\n\t\t\t\ttoken,\n\t\t\t\t`${RouteBases.api}${Routes.userConnections()}`\n\t\t\t)\n\t\t]);\n\n\t\t// Transform the information:\n\t\tlet data: LoginData = { user, guilds, connections };\n\t\tfor (const transformer of this.transformers) {\n\t\t\tconst result = transformer(data);\n\t\t\tif (isThenable(result)) data = await result;\n\t\t\telse data = result as LoginData;\n\t\t}\n\n\t\treturn data;\n\t}\n\n\tprivate async fetchInformation<T>(scope: OAuth2Scopes, token: string, url: string): Promise<T | null | undefined> {\n\t\tif (!this.scopes.includes(scope)) return undefined;\n\n\t\tconst result = await fetch(url, {\n\t\t\theaders: {\n\t\t\t\tauthorization: `Bearer ${token}`\n\t\t\t}\n\t\t});\n\n\t\treturn result.ok ? ((await result.json()) as T) : null;\n\t}\n\n\tpublic static create(options?: ServerOptionsAuth): Auth | null {\n\t\tif (!options?.secret || !options.id) return null;\n\t\treturn new Auth(options);\n\t}\n}\n\n/**\n * Defines the authentication data, this is to be encrypted and decrypted by the server.\n * @since 1.0.0\n */\nexport interface AuthData {\n\t/**\n\t * The user ID.\n\t * @since 1.0.0\n\t */\n\tid: string;\n\n\t/**\n\t * The timestamp at which the token expires.\n\t * @since 1.0.0\n\t */\n\texpires: number;\n\n\t/**\n\t * The refresh token.\n\t * @since 1.0.0\n\t */\n\trefresh: string;\n\n\t/**\n\t * The access token.\n\t * @since 1.0.0\n\t */\n\ttoken: string;\n}\n\n/**\n * Defines the authentication options.\n * @since 1.0.0\n */\nexport interface ServerOptionsAuth {\n\t/**\n\t * The client's application id, this can be retrieved in Discord Developer Portal at https://discord.com/developers/applications.\n\t * @since 1.0.0\n\t */\n\tid: string;\n\n\t/**\n\t * The name for the cookie, this will be used to identify a Secure HttpOnly cookie.\n\t * @since 1.0.0\n\t * @default 'SAPPHIRE_AUTH'\n\t */\n\tcookie?: string;\n\n\t/**\n\t * The client secret, this can be retrieved in Discord Developer Portal at https://discord.com/developers/applications.\n\t * @since 1.0.0\n\t */\n\tsecret: string;\n\n\t/**\n\t * The scopes defined at https://discord.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes.\n\t * @since 1.0.0\n\t * @default [OAuth2Scopes.Identify]\n\t */\n\tscopes?: OAuth2Scopes[];\n\n\t/**\n\t * The redirect uri. This will default to {@link OAuth2BodyData.redirectUri} if missing.\n\t * @since 1.0.0\n\t */\n\tredirect?: string;\n\n\t/**\n\t * The login data transformers used for {@link Auth.fetchData}.\n\t * @since 1.4.0\n\t * @default []\n\t */\n\ttransformers?: LoginDataTransformer[];\n\t/**\n\t * The domain that should be used for the cookie. This overwrites the automatic detection of the domain.\n\t * @remark if you want to support subdomains (`one.example.two` and `two.example.com`) then you need to use prefix your domain with a `.`, for example `.example.com`\n\t * @since 2.1.0\n\t * @default undefined\n\t */\n\tdomainOverwrite?: string;\n}\n\n/**\n * The login data sent when fetching data from a user.\n * @since 1.4.0\n */\nexport interface LoginData {\n\t/**\n\t * The user data, defined when the `'identify'` scope is defined.\n\t * @since 1.4.0\n\t */\n\tuser?: RESTGetAPICurrentUserResult | null;\n\n\t/**\n\t * The guilds data, defined when the `'guilds'` scope is defined.\n\t * @since 1.4.0\n\t */\n\tguilds?: RESTGetAPICurrentUserGuildsResult | null;\n\n\t/**\n\t * The connections data, defined when the `'connections'` scope is defined.\n\t * @since 1.4.0\n\t */\n\tconnections?: RESTGetAPICurrentUserConnectionsResult | null;\n}\n\n/**\n * A login data transformer.\n * @since 1.4.0\n */\nexport interface LoginDataTransformer<T extends LoginData = LoginData> {\n\t/**\n\t * Transforms the object by mutating its properties or adding new ones.\n\t * @since 1.4.0\n\t */\n\t(data: LoginData): Awaitable<T>;\n}\n"]}