better-call 1.1.5 → 1.1.7

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 (98) hide show
  1. package/README.md +35 -0
  2. package/dist/_virtual/rolldown_runtime.cjs +29 -0
  3. package/dist/adapters/node/request.cjs +125 -0
  4. package/dist/adapters/node/request.cjs.map +1 -0
  5. package/dist/{node.d.ts → adapters/node/request.d.cts} +2 -6
  6. package/dist/adapters/node/request.d.mts +16 -0
  7. package/dist/{node.js → adapters/node/request.mjs} +2 -13
  8. package/dist/adapters/node/request.mjs.map +1 -0
  9. package/dist/client.cjs +8 -1
  10. package/dist/client.cjs.map +1 -1
  11. package/dist/client.d.cts +12 -13
  12. package/dist/client.d.mts +53 -0
  13. package/dist/client.mjs +14 -0
  14. package/dist/client.mjs.map +1 -0
  15. package/dist/context.cjs +102 -0
  16. package/dist/context.cjs.map +1 -0
  17. package/dist/context.d.cts +340 -0
  18. package/dist/context.d.mts +340 -0
  19. package/dist/context.mjs +102 -0
  20. package/dist/context.mjs.map +1 -0
  21. package/dist/cookies.cjs +87 -0
  22. package/dist/cookies.cjs.map +1 -0
  23. package/dist/cookies.d.cts +103 -0
  24. package/dist/cookies.d.mts +103 -0
  25. package/dist/cookies.mjs +84 -0
  26. package/dist/cookies.mjs.map +1 -0
  27. package/dist/crypto.cjs +39 -0
  28. package/dist/crypto.cjs.map +1 -0
  29. package/dist/crypto.mjs +36 -0
  30. package/dist/crypto.mjs.map +1 -0
  31. package/dist/endpoint.cjs +70 -0
  32. package/dist/endpoint.cjs.map +1 -0
  33. package/dist/endpoint.d.cts +428 -0
  34. package/dist/endpoint.d.mts +428 -0
  35. package/dist/endpoint.mjs +70 -0
  36. package/dist/endpoint.mjs.map +1 -0
  37. package/dist/error.cjs +141 -0
  38. package/dist/error.cjs.map +1 -0
  39. package/dist/error.d.cts +103 -0
  40. package/dist/error.d.mts +103 -0
  41. package/dist/error.mjs +135 -0
  42. package/dist/error.mjs.map +1 -0
  43. package/dist/helper.d.cts +12 -0
  44. package/dist/helper.d.mts +12 -0
  45. package/dist/index.cjs +26 -968
  46. package/dist/index.d.cts +11 -14
  47. package/dist/index.d.mts +11 -0
  48. package/dist/index.mjs +10 -0
  49. package/dist/middleware.cjs +39 -0
  50. package/dist/middleware.cjs.map +1 -0
  51. package/dist/middleware.d.cts +123 -0
  52. package/dist/middleware.d.mts +123 -0
  53. package/dist/middleware.mjs +39 -0
  54. package/dist/middleware.mjs.map +1 -0
  55. package/dist/node.cjs +4 -151
  56. package/dist/node.cjs.map +1 -1
  57. package/dist/node.d.cts +2 -13
  58. package/dist/node.d.mts +9 -0
  59. package/dist/node.mjs +15 -0
  60. package/dist/node.mjs.map +1 -0
  61. package/dist/openapi.cjs +191 -0
  62. package/dist/openapi.cjs.map +1 -0
  63. package/dist/openapi.d.cts +113 -0
  64. package/dist/openapi.d.mts +113 -0
  65. package/dist/openapi.mjs +189 -0
  66. package/dist/openapi.mjs.map +1 -0
  67. package/dist/router.cjs +117 -0
  68. package/dist/router.cjs.map +1 -0
  69. package/dist/router.d.cts +4 -1242
  70. package/dist/router.d.mts +97 -0
  71. package/dist/router.mjs +116 -0
  72. package/dist/router.mjs.map +1 -0
  73. package/dist/standard-schema.d.cts +59 -0
  74. package/dist/standard-schema.d.mts +59 -0
  75. package/dist/to-response.cjs +96 -0
  76. package/dist/to-response.cjs.map +1 -0
  77. package/dist/to-response.d.cts +12 -0
  78. package/dist/to-response.d.mts +12 -0
  79. package/dist/to-response.mjs +96 -0
  80. package/dist/to-response.mjs.map +1 -0
  81. package/dist/utils.cjs +77 -0
  82. package/dist/utils.cjs.map +1 -0
  83. package/dist/utils.mjs +74 -0
  84. package/dist/utils.mjs.map +1 -0
  85. package/dist/validator.cjs +58 -0
  86. package/dist/validator.cjs.map +1 -0
  87. package/dist/validator.mjs +57 -0
  88. package/dist/validator.mjs.map +1 -0
  89. package/package.json +23 -13
  90. package/dist/client.d.ts +0 -54
  91. package/dist/client.js +0 -13
  92. package/dist/client.js.map +0 -1
  93. package/dist/index.cjs.map +0 -1
  94. package/dist/index.d.ts +0 -14
  95. package/dist/index.js +0 -951
  96. package/dist/index.js.map +0 -1
  97. package/dist/node.js.map +0 -1
  98. package/dist/router.d.ts +0 -1335
@@ -0,0 +1,70 @@
1
+ const require_error = require('./error.cjs');
2
+ const require_utils = require('./utils.cjs');
3
+ const require_to_response = require('./to-response.cjs');
4
+ const require_context = require('./context.cjs');
5
+
6
+ //#region src/endpoint.ts
7
+ function createEndpoint(pathOrOptions, handlerOrOptions, handlerOrNever) {
8
+ const path = typeof pathOrOptions === "string" ? pathOrOptions : void 0;
9
+ const options = typeof handlerOrOptions === "object" ? handlerOrOptions : pathOrOptions;
10
+ const handler = typeof handlerOrOptions === "function" ? handlerOrOptions : handlerOrNever;
11
+ if ((options.method === "GET" || options.method === "HEAD") && options.body) throw new require_error.BetterCallError("Body is not allowed with GET or HEAD methods");
12
+ if (path && /\/{2,}/.test(path)) throw new require_error.BetterCallError("Path cannot contain consecutive slashes");
13
+ const internalHandler = async (...inputCtx) => {
14
+ const context = inputCtx[0] || {};
15
+ const { data: internalContext, error: validationError } = await require_utils.tryCatch(require_context.createInternalContext(context, {
16
+ options,
17
+ path
18
+ }));
19
+ if (validationError) {
20
+ if (!(validationError instanceof require_error.ValidationError)) throw validationError;
21
+ if (options.onValidationError) await options.onValidationError({
22
+ message: validationError.message,
23
+ issues: validationError.issues
24
+ });
25
+ throw new require_error.APIError(400, {
26
+ message: validationError.message,
27
+ code: "VALIDATION_ERROR"
28
+ });
29
+ }
30
+ const response = await handler(internalContext).catch(async (e) => {
31
+ if (require_utils.isAPIError(e)) {
32
+ const onAPIError = options.onAPIError;
33
+ if (onAPIError) await onAPIError(e);
34
+ if (context.asResponse) return e;
35
+ }
36
+ throw e;
37
+ });
38
+ const headers = internalContext.responseHeaders;
39
+ const status = internalContext.responseStatus;
40
+ return context.asResponse ? require_to_response.toResponse(response, {
41
+ headers,
42
+ status
43
+ }) : context.returnHeaders ? context.returnStatus ? {
44
+ headers,
45
+ response,
46
+ status
47
+ } : {
48
+ headers,
49
+ response
50
+ } : context.returnStatus ? {
51
+ response,
52
+ status
53
+ } : response;
54
+ };
55
+ internalHandler.options = options;
56
+ internalHandler.path = path;
57
+ return internalHandler;
58
+ }
59
+ createEndpoint.create = (opts) => {
60
+ return (path, options, handler) => {
61
+ return createEndpoint(path, {
62
+ ...options,
63
+ use: [...options?.use || [], ...opts?.use || []]
64
+ }, handler);
65
+ };
66
+ };
67
+
68
+ //#endregion
69
+ exports.createEndpoint = createEndpoint;
70
+ //# sourceMappingURL=endpoint.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"endpoint.cjs","names":["path: string | undefined","options: Options","handler: EndpointHandler<Path, Options, R>","BetterCallError","tryCatch","createInternalContext","ValidationError","APIError","isAPIError","toResponse"],"sources":["../src/endpoint.ts"],"sourcesContent":["import type { HasRequiredKeys, Prettify } from \"./helper\";\nimport { toResponse } from \"./to-response\";\nimport type { Middleware } from \"./middleware\";\nimport {\n\tcreateInternalContext,\n\ttype InferBody,\n\ttype InferHeaders,\n\ttype InferMethod,\n\ttype InferParam,\n\ttype InferQuery,\n\ttype InferRequest,\n\ttype InferUse,\n\ttype InputContext,\n\ttype Method,\n} from \"./context\";\nimport type { CookieOptions, CookiePrefixOptions } from \"./cookies\";\nimport { APIError, ValidationError, type statusCodes, type Status, BetterCallError } from \"./error\";\nimport type { OpenAPIParameter, OpenAPISchemaType } from \"./openapi\";\nimport type { StandardSchemaV1 } from \"./standard-schema\";\nimport { isAPIError, tryCatch } from \"./utils\";\n\nexport interface EndpointBaseOptions {\n\t/**\n\t * Query Schema\n\t */\n\tquery?: StandardSchemaV1;\n\t/**\n\t * Error Schema\n\t */\n\terror?: StandardSchemaV1;\n\t/**\n\t * If true headers will be required to be passed in the context\n\t */\n\trequireHeaders?: boolean;\n\t/**\n\t * If true request object will be required\n\t */\n\trequireRequest?: boolean;\n\t/**\n\t * Clone the request object from the router\n\t */\n\tcloneRequest?: boolean;\n\t/**\n\t * If true the body will be undefined\n\t */\n\tdisableBody?: boolean;\n\t/**\n\t * Endpoint metadata\n\t */\n\tmetadata?: {\n\t\t/**\n\t\t * Open API definition\n\t\t */\n\t\topenapi?: {\n\t\t\tsummary?: string;\n\t\t\tdescription?: string;\n\t\t\ttags?: string[];\n\t\t\toperationId?: string;\n\t\t\tparameters?: OpenAPIParameter[];\n\t\t\trequestBody?: {\n\t\t\t\tcontent: {\n\t\t\t\t\t\"application/json\": {\n\t\t\t\t\t\tschema: {\n\t\t\t\t\t\t\ttype?: OpenAPISchemaType;\n\t\t\t\t\t\t\tproperties?: Record<string, any>;\n\t\t\t\t\t\t\trequired?: string[];\n\t\t\t\t\t\t\t$ref?: string;\n\t\t\t\t\t\t};\n\t\t\t\t\t};\n\t\t\t\t};\n\t\t\t};\n\t\t\tresponses?: {\n\t\t\t\t[status: string]: {\n\t\t\t\t\tdescription: string;\n\t\t\t\t\tcontent?: {\n\t\t\t\t\t\t\"application/json\"?: {\n\t\t\t\t\t\t\tschema: {\n\t\t\t\t\t\t\t\ttype?: OpenAPISchemaType;\n\t\t\t\t\t\t\t\tproperties?: Record<string, any>;\n\t\t\t\t\t\t\t\trequired?: string[];\n\t\t\t\t\t\t\t\t$ref?: string;\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t};\n\t\t\t\t\t\t\"text/plain\"?: {\n\t\t\t\t\t\t\tschema?: {\n\t\t\t\t\t\t\t\ttype?: OpenAPISchemaType;\n\t\t\t\t\t\t\t\tproperties?: Record<string, any>;\n\t\t\t\t\t\t\t\trequired?: string[];\n\t\t\t\t\t\t\t\t$ref?: string;\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t};\n\t\t\t\t\t\t\"text/html\"?: {\n\t\t\t\t\t\t\tschema?: {\n\t\t\t\t\t\t\t\ttype?: OpenAPISchemaType;\n\t\t\t\t\t\t\t\tproperties?: Record<string, any>;\n\t\t\t\t\t\t\t\trequired?: string[];\n\t\t\t\t\t\t\t\t$ref?: string;\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t};\n\t\t\t\t\t};\n\t\t\t\t};\n\t\t\t};\n\t\t};\n\t\t/**\n\t\t * Infer body and query type from ts interface\n\t\t *\n\t\t * useful for generic and dynamic types\n\t\t *\n\t\t * @example\n\t\t * ```ts\n\t\t * const endpoint = createEndpoint(\"/path\", {\n\t\t * \t\tmethod: \"POST\",\n\t\t * \t\tbody: z.record(z.string()),\n\t\t * \t\t$Infer: {\n\t\t * \t\t\tbody: {} as {\n\t\t * \t\t\t\ttype: InferTypeFromOptions<Option> // custom type inference\n\t\t * \t\t\t}\n\t\t * \t\t}\n\t\t * \t}, async(ctx)=>{\n\t\t * \t\tconst body = ctx.body\n\t\t * \t})\n\t\t * ```\n\t\t */\n\t\t$Infer?: {\n\t\t\t/**\n\t\t\t * Body\n\t\t\t */\n\t\t\tbody?: any;\n\t\t\t/**\n\t\t\t * Query\n\t\t\t */\n\t\t\tquery?: Record<string, any>;\n\t\t};\n\t\t/**\n\t\t * If enabled, endpoint won't be exposed over a router\n\t\t * @deprecated Use path-less endpoints instead\n\t\t */\n\t\tSERVER_ONLY?: boolean;\n\t\t/**\n\t\t * If enabled, endpoint won't be exposed as an action to the client\n\t\t * @deprecated Use path-less endpoints instead\n\t\t */\n\t\tisAction?: boolean;\n\t\t/**\n\t\t * Defines the places where the endpoint will be available\n\t\t *\n\t\t * Possible options:\n\t\t * - `rpc` - the endpoint is exposed to the router, can be invoked directly and is available to the client\n\t\t * - `server` - the endpoint is exposed to the router, can be invoked directly, but is not available to the client\n\t\t * - `http` - the endpoint is only exposed to the router\n\t\t * @default \"rpc\"\n\t\t */\n\t\tscope?: \"rpc\" | \"server\" | \"http\";\n\t\t/**\n\t\t * List of allowed media types (MIME types) for the endpoint\n\t\t *\n\t\t * if provided, only the media types in the list will be allowed to be passed in the body\n\t\t *\n\t\t * @example\n\t\t * ```ts\n\t\t * const endpoint = createEndpoint(\"/path\", {\n\t\t * \t\tmethod: \"POST\",\n\t\t * \t\tallowedMediaTypes: [\"application/json\", \"application/x-www-form-urlencoded\"],\n\t\t * \t}, async(ctx)=>{\n\t\t * \t\tconst body = ctx.body\n\t\t * \t})\n\t\t * ```\n\t\t */\n\t\tallowedMediaTypes?: string[];\n\t\t/**\n\t\t * Extra metadata\n\t\t */\n\t\t[key: string]: any;\n\t};\n\t/**\n\t * List of middlewares to use\n\t */\n\tuse?: Middleware[];\n\t/**\n\t * A callback to run before any API error is throw or returned\n\t *\n\t * @param e - The API error\n\t * @returns - The response to return\n\t */\n\tonAPIError?: (e: APIError) => void | Promise<void>;\n\t/**\n\t * A callback to run before a validation error is thrown\n\t * You can customize the validation error message by throwing your own APIError\n\t */\n\tonValidationError?: ({\n\t\tissues,\n\t\tmessage,\n\t}: { message: string; issues: readonly StandardSchemaV1.Issue[] }) => void | Promise<void>;\n}\n\nexport type EndpointBodyMethodOptions =\n\t| {\n\t\t\t/**\n\t\t\t * Request Method\n\t\t\t */\n\t\t\tmethod: \"POST\" | \"PUT\" | \"DELETE\" | \"PATCH\" | (\"POST\" | \"PUT\" | \"DELETE\" | \"PATCH\")[];\n\t\t\t/**\n\t\t\t * Body Schema\n\t\t\t */\n\t\t\tbody?: StandardSchemaV1;\n\t }\n\t| {\n\t\t\t/**\n\t\t\t * Request Method\n\t\t\t */\n\t\t\tmethod: \"GET\" | \"HEAD\" | (\"GET\" | \"HEAD\")[];\n\t\t\t/**\n\t\t\t * Body Schema\n\t\t\t */\n\t\t\tbody?: never;\n\t }\n\t| {\n\t\t\t/**\n\t\t\t * Request Method\n\t\t\t */\n\t\t\tmethod: \"*\";\n\t\t\t/**\n\t\t\t * Body Schema\n\t\t\t */\n\t\t\tbody?: StandardSchemaV1;\n\t }\n\t| {\n\t\t\t/**\n\t\t\t * Request Method\n\t\t\t */\n\t\t\tmethod: (\"POST\" | \"PUT\" | \"DELETE\" | \"PATCH\" | \"GET\" | \"HEAD\")[];\n\t\t\t/**\n\t\t\t * Body Schema\n\t\t\t */\n\t\t\tbody?: StandardSchemaV1;\n\t };\n\nexport type EndpointOptions = EndpointBaseOptions & EndpointBodyMethodOptions;\n\nexport type EndpointContext<Path extends string, Options extends EndpointOptions, Context = {}> = {\n\t/**\n\t * Method\n\t *\n\t * The request method\n\t */\n\tmethod: InferMethod<Options>;\n\t/**\n\t * Path\n\t *\n\t * The path of the endpoint\n\t */\n\tpath: Path;\n\t/**\n\t * Body\n\t *\n\t * The body object will be the parsed JSON from the request and validated\n\t * against the body schema if it exists.\n\t */\n\tbody: InferBody<Options>;\n\t/**\n\t * Query\n\t *\n\t * The query object will be the parsed query string from the request\n\t * and validated against the query schema if it exists\n\t */\n\tquery: InferQuery<Options>;\n\t/**\n\t * Params\n\t *\n\t * If the path is `/user/:id` and the request is `/user/1` then the params will\n\t * be `{ id: \"1\" }` and if the path includes a wildcard like `/user/*` then the\n\t * params will be `{ _: \"1\" }` where `_` is the wildcard key. If the wildcard\n\t * is named like `/user/**:name` then the params will be `{ name: string }`\n\t */\n\tparams: InferParam<Path>;\n\t/**\n\t * Request object\n\t *\n\t * If `requireRequest` is set to true in the endpoint options this will be\n\t * required\n\t */\n\trequest: InferRequest<Options>;\n\t/**\n\t * Headers\n\t *\n\t * If `requireHeaders` is set to true in the endpoint options this will be\n\t * required\n\t */\n\theaders: InferHeaders<Options>;\n\t/**\n\t * Set header\n\t *\n\t * If it's called outside of a request it will just be ignored.\n\t */\n\tsetHeader: (key: string, value: string) => void;\n\t/**\n\t * Set the response status code\n\t */\n\tsetStatus: (status: Status) => void;\n\t/**\n\t * Get header\n\t *\n\t * If it's called outside of a request it will just return null\n\t *\n\t * @param key - The key of the header\n\t * @returns\n\t */\n\tgetHeader: (key: string) => string | null;\n\t/**\n\t * Get a cookie value from the request\n\t *\n\t * @param key - The key of the cookie\n\t * @param prefix - The prefix of the cookie between `__Secure-` and `__Host-`\n\t * @returns - The value of the cookie\n\t */\n\tgetCookie: (key: string, prefix?: CookiePrefixOptions) => string | null;\n\t/**\n\t * Get a signed cookie value from the request\n\t *\n\t * @param key - The key of the cookie\n\t * @param secret - The secret of the signed cookie\n\t * @param prefix - The prefix of the cookie between `__Secure-` and `__Host-`\n\t * @returns - The value of the cookie or null if the cookie is not found or false if the signature is invalid\n\t */\n\tgetSignedCookie: (\n\t\tkey: string,\n\t\tsecret: string,\n\t\tprefix?: CookiePrefixOptions,\n\t) => Promise<string | null | false>;\n\t/**\n\t * Set a cookie value in the response\n\t *\n\t * @param key - The key of the cookie\n\t * @param value - The value to set\n\t * @param options - The options of the cookie\n\t * @returns - The cookie string\n\t */\n\tsetCookie: (key: string, value: string, options?: CookieOptions) => string;\n\t/**\n\t * Set signed cookie\n\t *\n\t * @param key - The key of the cookie\n\t * @param value - The value to set\n\t * @param secret - The secret to sign the cookie with\n\t * @param options - The options of the cookie\n\t * @returns - The cookie string\n\t */\n\tsetSignedCookie: (\n\t\tkey: string,\n\t\tvalue: string,\n\t\tsecret: string,\n\t\toptions?: CookieOptions,\n\t) => Promise<string>;\n\t/**\n\t * JSON\n\t *\n\t * a helper function to create a JSON response with\n\t * the correct headers\n\t * and status code. If `asResponse` is set to true in\n\t * the context then\n\t * it will return a Response object instead of the\n\t * JSON object.\n\t *\n\t * @param json - The JSON object to return\n\t * @param routerResponse - The response object to\n\t * return if `asResponse` is\n\t * true in the context this will take precedence\n\t */\n\tjson: <R extends Record<string, any> | null>(\n\t\tjson: R,\n\t\trouterResponse?:\n\t\t\t| {\n\t\t\t\t\tstatus?: number;\n\t\t\t\t\theaders?: Record<string, string>;\n\t\t\t\t\tresponse?: Response;\n\t\t\t\t\tbody?: Record<string, string>;\n\t\t\t }\n\t\t\t| Response,\n\t) => Promise<R>;\n\t/**\n\t * Middleware context\n\t */\n\tcontext: Prettify<Context & InferUse<Options[\"use\"]>>;\n\t/**\n\t * Redirect to a new URL\n\t */\n\tredirect: (url: string) => APIError;\n\t/**\n\t * Return error\n\t */\n\terror: (\n\t\tstatus: keyof typeof statusCodes | Status,\n\t\tbody?: {\n\t\t\tmessage?: string;\n\t\t\tcode?: string;\n\t\t} & Record<string, any>,\n\t\theaders?: HeadersInit,\n\t) => APIError;\n};\n\ntype EndpointHandler<Path extends string, Options extends EndpointOptions, R> = (\n\tcontext: EndpointContext<Path, Options>,\n) => Promise<R>;\n\nexport function createEndpoint<Path extends string, Options extends EndpointOptions, R>(\n\tpath: Path,\n\toptions: Options,\n\thandler: EndpointHandler<Path, Options, R>,\n): StrictEndpoint<Path, Options, R>;\n\nexport function createEndpoint<Options extends EndpointOptions, R>(\n\toptions: Options,\n\thandler: EndpointHandler<never, Options, R>,\n): StrictEndpoint<never, Options, R>;\n\nexport function createEndpoint<Path extends string, Options extends EndpointOptions, R>(\n\tpathOrOptions: Path | Options,\n\thandlerOrOptions: EndpointHandler<Path, Options, R> | Options,\n\thandlerOrNever?: any,\n): StrictEndpoint<Path, Options, R> {\n\tconst path: string | undefined = typeof pathOrOptions === \"string\" ? pathOrOptions : undefined;\n\tconst options: Options =\n\t\ttypeof handlerOrOptions === \"object\" ? handlerOrOptions : (pathOrOptions as Options);\n\tconst handler: EndpointHandler<Path, Options, R> =\n\t\ttypeof handlerOrOptions === \"function\" ? handlerOrOptions : handlerOrNever;\n\n\tif ((options.method === \"GET\" || options.method === \"HEAD\") && options.body) {\n\t\tthrow new BetterCallError(\"Body is not allowed with GET or HEAD methods\");\n\t}\n\n\tif (path && /\\/{2,}/.test(path)) {\n\t\tthrow new BetterCallError(\"Path cannot contain consecutive slashes\");\n\t}\n\ttype Context = InputContext<Path, Options>;\n\n\ttype ResultType<\n\t\tAsResponse extends boolean,\n\t\tReturnHeaders extends boolean,\n\t\tReturnStatus extends boolean,\n\t> = AsResponse extends true\n\t\t? Response\n\t\t: ReturnHeaders extends true\n\t\t\t? ReturnStatus extends true\n\t\t\t\t? {\n\t\t\t\t\t\theaders: Headers;\n\t\t\t\t\t\tstatus: number;\n\t\t\t\t\t\tresponse: Awaited<R>;\n\t\t\t\t\t}\n\t\t\t\t: {\n\t\t\t\t\t\theaders: Headers;\n\t\t\t\t\t\tresponse: Awaited<R>;\n\t\t\t\t\t}\n\t\t\t: ReturnStatus extends true\n\t\t\t\t? {\n\t\t\t\t\t\tstatus: number;\n\t\t\t\t\t\tresponse: Awaited<R>;\n\t\t\t\t\t}\n\t\t\t\t: Awaited<R>;\n\n\tconst internalHandler = async <\n\t\tAsResponse extends boolean = false,\n\t\tReturnHeaders extends boolean = false,\n\t\tReturnStatus extends boolean = false,\n\t>(\n\t\t...inputCtx: HasRequiredKeys<Context> extends true\n\t\t\t? [\n\t\t\t\t\tContext & {\n\t\t\t\t\t\tasResponse?: AsResponse;\n\t\t\t\t\t\treturnHeaders?: ReturnHeaders;\n\t\t\t\t\t\treturnStatus?: ReturnStatus;\n\t\t\t\t\t},\n\t\t\t\t]\n\t\t\t: [\n\t\t\t\t\t(Context & {\n\t\t\t\t\t\tasResponse?: AsResponse;\n\t\t\t\t\t\treturnHeaders?: ReturnHeaders;\n\t\t\t\t\t\treturnStatus?: ReturnStatus;\n\t\t\t\t\t})?,\n\t\t\t\t]\n\t): Promise<ResultType<AsResponse, ReturnHeaders, ReturnStatus>> => {\n\t\tconst context = (inputCtx[0] || {}) as InputContext<any, any>;\n\t\tconst { data: internalContext, error: validationError } = await tryCatch(\n\t\t\tcreateInternalContext(context, {\n\t\t\t\toptions,\n\t\t\t\tpath,\n\t\t\t}),\n\t\t);\n\n\t\tif (validationError) {\n\t\t\t// If it's not a validation error, we throw it\n\t\t\tif (!(validationError instanceof ValidationError)) throw validationError;\n\n\t\t\t// Check if the endpoint has a custom onValidationError callback\n\t\t\tif (options.onValidationError) {\n\t\t\t\t// This can possibly throw an APIError in order to customize the validation error message\n\t\t\t\tawait options.onValidationError({\n\t\t\t\t\tmessage: validationError.message,\n\t\t\t\t\tissues: validationError.issues,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tthrow new APIError(400, {\n\t\t\t\tmessage: validationError.message,\n\t\t\t\tcode: \"VALIDATION_ERROR\",\n\t\t\t});\n\t\t}\n\t\tconst response = await handler(internalContext as any).catch(async (e) => {\n\t\t\tif (isAPIError(e)) {\n\t\t\t\tconst onAPIError = options.onAPIError;\n\t\t\t\tif (onAPIError) {\n\t\t\t\t\tawait onAPIError(e);\n\t\t\t\t}\n\t\t\t\tif (context.asResponse) {\n\t\t\t\t\treturn e;\n\t\t\t\t}\n\t\t\t}\n\t\t\tthrow e;\n\t\t});\n\t\tconst headers = internalContext.responseHeaders;\n\t\tconst status = internalContext.responseStatus;\n\n\t\treturn (\n\t\t\tcontext.asResponse\n\t\t\t\t? toResponse(response, {\n\t\t\t\t\t\theaders,\n\t\t\t\t\t\tstatus,\n\t\t\t\t\t})\n\t\t\t\t: context.returnHeaders\n\t\t\t\t\t? context.returnStatus\n\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\theaders,\n\t\t\t\t\t\t\t\tresponse,\n\t\t\t\t\t\t\t\tstatus,\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\theaders,\n\t\t\t\t\t\t\t\tresponse,\n\t\t\t\t\t\t\t}\n\t\t\t\t\t: context.returnStatus\n\t\t\t\t\t\t? { response, status }\n\t\t\t\t\t\t: response\n\t\t) as ResultType<AsResponse, ReturnHeaders, ReturnStatus>;\n\t};\n\tinternalHandler.options = options;\n\tinternalHandler.path = path;\n\treturn internalHandler as unknown as StrictEndpoint<Path, Options, R>;\n}\n\ncreateEndpoint.create = <E extends { use?: Middleware[] }>(opts?: E) => {\n\treturn <Path extends string, Opts extends EndpointOptions, R extends Promise<any>>(\n\t\tpath: Path,\n\t\toptions: Opts,\n\t\thandler: (ctx: EndpointContext<Path, Opts, InferUse<E[\"use\"]>>) => R,\n\t) => {\n\t\treturn createEndpoint(\n\t\t\tpath,\n\t\t\t{\n\t\t\t\t...options,\n\t\t\t\tuse: [...(options?.use || []), ...(opts?.use || [])],\n\t\t\t},\n\t\t\thandler,\n\t\t);\n\t};\n};\n\nexport type StrictEndpoint<Path extends string, Options extends EndpointOptions, R = any> = {\n\t// asResponse cases\n\t(context: InputContext<Path, Options> & { asResponse: true }): Promise<Response>;\n\n\t// returnHeaders & returnStatus cases\n\t(\n\t\tcontext: InputContext<Path, Options> & { returnHeaders: true; returnStatus: true },\n\t): Promise<{ headers: Headers; status: number; response: Awaited<R> }>;\n\t(\n\t\tcontext: InputContext<Path, Options> & { returnHeaders: true; returnStatus: false },\n\t): Promise<{ headers: Headers; response: Awaited<R> }>;\n\t(\n\t\tcontext: InputContext<Path, Options> & { returnHeaders: false; returnStatus: true },\n\t): Promise<{ status: number; response: Awaited<R> }>;\n\t(\n\t\tcontext: InputContext<Path, Options> & { returnHeaders: false; returnStatus: false },\n\t): Promise<R>;\n\n\t// individual flag cases\n\t(\n\t\tcontext: InputContext<Path, Options> & { returnHeaders: true },\n\t): Promise<{ headers: Headers; response: Awaited<R> }>;\n\t(\n\t\tcontext: InputContext<Path, Options> & { returnStatus: true },\n\t): Promise<{ status: number; response: Awaited<R> }>;\n\n\t// default case\n\t(context?: InputContext<Path, Options>): Promise<R>;\n\n\toptions: Options;\n\tpath: Path;\n};\n\nexport type Endpoint<\n\tPath extends string = string,\n\tOptions extends EndpointOptions = EndpointOptions,\n\tHandler extends (inputCtx: any) => Promise<any> = (inputCtx: any) => Promise<any>,\n> = Handler & {\n\toptions: Options;\n\tpath: Path;\n};\n"],"mappings":";;;;;;AA+ZA,SAAgB,eACf,eACA,kBACA,gBACmC;CACnC,MAAMA,OAA2B,OAAO,kBAAkB,WAAW,gBAAgB;CACrF,MAAMC,UACL,OAAO,qBAAqB,WAAW,mBAAoB;CAC5D,MAAMC,UACL,OAAO,qBAAqB,aAAa,mBAAmB;AAE7D,MAAK,QAAQ,WAAW,SAAS,QAAQ,WAAW,WAAW,QAAQ,KACtE,OAAM,IAAIC,8BAAgB,+CAA+C;AAG1E,KAAI,QAAQ,SAAS,KAAK,KAAK,CAC9B,OAAM,IAAIA,8BAAgB,0CAA0C;CA4BrE,MAAM,kBAAkB,OAKvB,GAAG,aAe+D;EAClE,MAAM,UAAW,SAAS,MAAM,EAAE;EAClC,MAAM,EAAE,MAAM,iBAAiB,OAAO,oBAAoB,MAAMC,uBAC/DC,sCAAsB,SAAS;GAC9B;GACA;GACA,CAAC,CACF;AAED,MAAI,iBAAiB;AAEpB,OAAI,EAAE,2BAA2BC,+BAAkB,OAAM;AAGzD,OAAI,QAAQ,kBAEX,OAAM,QAAQ,kBAAkB;IAC/B,SAAS,gBAAgB;IACzB,QAAQ,gBAAgB;IACxB,CAAC;AAGH,SAAM,IAAIC,uBAAS,KAAK;IACvB,SAAS,gBAAgB;IACzB,MAAM;IACN,CAAC;;EAEH,MAAM,WAAW,MAAM,QAAQ,gBAAuB,CAAC,MAAM,OAAO,MAAM;AACzE,OAAIC,yBAAW,EAAE,EAAE;IAClB,MAAM,aAAa,QAAQ;AAC3B,QAAI,WACH,OAAM,WAAW,EAAE;AAEpB,QAAI,QAAQ,WACX,QAAO;;AAGT,SAAM;IACL;EACF,MAAM,UAAU,gBAAgB;EAChC,MAAM,SAAS,gBAAgB;AAE/B,SACC,QAAQ,aACLC,+BAAW,UAAU;GACrB;GACA;GACA,CAAC,GACD,QAAQ,gBACP,QAAQ,eACP;GACA;GACA;GACA;GACA,GACA;GACA;GACA;GACA,GACD,QAAQ,eACP;GAAE;GAAU;GAAQ,GACpB;;AAGP,iBAAgB,UAAU;AAC1B,iBAAgB,OAAO;AACvB,QAAO;;AAGR,eAAe,UAA4C,SAAa;AACvE,SACC,MACA,SACA,YACI;AACJ,SAAO,eACN,MACA;GACC,GAAG;GACH,KAAK,CAAC,GAAI,SAAS,OAAO,EAAE,EAAG,GAAI,MAAM,OAAO,EAAE,CAAE;GACpD,EACD,QACA"}
@@ -0,0 +1,428 @@
1
+ import { Prettify } from "./helper.cjs";
2
+ import { StandardSchemaV1 } from "./standard-schema.cjs";
3
+ import { APIError, Status, statusCodes } from "./error.cjs";
4
+ import { CookieOptions, CookiePrefixOptions } from "./cookies.cjs";
5
+ import { InferBody, InferHeaders, InferMethod, InferParam, InferQuery, InferRequest, InferUse, InputContext } from "./context.cjs";
6
+ import { Middleware } from "./middleware.cjs";
7
+ import { OpenAPIParameter, OpenAPISchemaType } from "./openapi.cjs";
8
+
9
+ //#region src/endpoint.d.ts
10
+ interface EndpointBaseOptions {
11
+ /**
12
+ * Query Schema
13
+ */
14
+ query?: StandardSchemaV1;
15
+ /**
16
+ * Error Schema
17
+ */
18
+ error?: StandardSchemaV1;
19
+ /**
20
+ * If true headers will be required to be passed in the context
21
+ */
22
+ requireHeaders?: boolean;
23
+ /**
24
+ * If true request object will be required
25
+ */
26
+ requireRequest?: boolean;
27
+ /**
28
+ * Clone the request object from the router
29
+ */
30
+ cloneRequest?: boolean;
31
+ /**
32
+ * If true the body will be undefined
33
+ */
34
+ disableBody?: boolean;
35
+ /**
36
+ * Endpoint metadata
37
+ */
38
+ metadata?: {
39
+ /**
40
+ * Open API definition
41
+ */
42
+ openapi?: {
43
+ summary?: string;
44
+ description?: string;
45
+ tags?: string[];
46
+ operationId?: string;
47
+ parameters?: OpenAPIParameter[];
48
+ requestBody?: {
49
+ content: {
50
+ "application/json": {
51
+ schema: {
52
+ type?: OpenAPISchemaType;
53
+ properties?: Record<string, any>;
54
+ required?: string[];
55
+ $ref?: string;
56
+ };
57
+ };
58
+ };
59
+ };
60
+ responses?: {
61
+ [status: string]: {
62
+ description: string;
63
+ content?: {
64
+ "application/json"?: {
65
+ schema: {
66
+ type?: OpenAPISchemaType;
67
+ properties?: Record<string, any>;
68
+ required?: string[];
69
+ $ref?: string;
70
+ };
71
+ };
72
+ "text/plain"?: {
73
+ schema?: {
74
+ type?: OpenAPISchemaType;
75
+ properties?: Record<string, any>;
76
+ required?: string[];
77
+ $ref?: string;
78
+ };
79
+ };
80
+ "text/html"?: {
81
+ schema?: {
82
+ type?: OpenAPISchemaType;
83
+ properties?: Record<string, any>;
84
+ required?: string[];
85
+ $ref?: string;
86
+ };
87
+ };
88
+ };
89
+ };
90
+ };
91
+ };
92
+ /**
93
+ * Infer body and query type from ts interface
94
+ *
95
+ * useful for generic and dynamic types
96
+ *
97
+ * @example
98
+ * ```ts
99
+ * const endpoint = createEndpoint("/path", {
100
+ * method: "POST",
101
+ * body: z.record(z.string()),
102
+ * $Infer: {
103
+ * body: {} as {
104
+ * type: InferTypeFromOptions<Option> // custom type inference
105
+ * }
106
+ * }
107
+ * }, async(ctx)=>{
108
+ * const body = ctx.body
109
+ * })
110
+ * ```
111
+ */
112
+ $Infer?: {
113
+ /**
114
+ * Body
115
+ */
116
+ body?: any;
117
+ /**
118
+ * Query
119
+ */
120
+ query?: Record<string, any>;
121
+ };
122
+ /**
123
+ * If enabled, endpoint won't be exposed over a router
124
+ * @deprecated Use path-less endpoints instead
125
+ */
126
+ SERVER_ONLY?: boolean;
127
+ /**
128
+ * If enabled, endpoint won't be exposed as an action to the client
129
+ * @deprecated Use path-less endpoints instead
130
+ */
131
+ isAction?: boolean;
132
+ /**
133
+ * Defines the places where the endpoint will be available
134
+ *
135
+ * Possible options:
136
+ * - `rpc` - the endpoint is exposed to the router, can be invoked directly and is available to the client
137
+ * - `server` - the endpoint is exposed to the router, can be invoked directly, but is not available to the client
138
+ * - `http` - the endpoint is only exposed to the router
139
+ * @default "rpc"
140
+ */
141
+ scope?: "rpc" | "server" | "http";
142
+ /**
143
+ * List of allowed media types (MIME types) for the endpoint
144
+ *
145
+ * if provided, only the media types in the list will be allowed to be passed in the body
146
+ *
147
+ * @example
148
+ * ```ts
149
+ * const endpoint = createEndpoint("/path", {
150
+ * method: "POST",
151
+ * allowedMediaTypes: ["application/json", "application/x-www-form-urlencoded"],
152
+ * }, async(ctx)=>{
153
+ * const body = ctx.body
154
+ * })
155
+ * ```
156
+ */
157
+ allowedMediaTypes?: string[];
158
+ /**
159
+ * Extra metadata
160
+ */
161
+ [key: string]: any;
162
+ };
163
+ /**
164
+ * List of middlewares to use
165
+ */
166
+ use?: Middleware[];
167
+ /**
168
+ * A callback to run before any API error is throw or returned
169
+ *
170
+ * @param e - The API error
171
+ * @returns - The response to return
172
+ */
173
+ onAPIError?: (e: APIError) => void | Promise<void>;
174
+ /**
175
+ * A callback to run before a validation error is thrown
176
+ * You can customize the validation error message by throwing your own APIError
177
+ */
178
+ onValidationError?: ({
179
+ issues,
180
+ message
181
+ }: {
182
+ message: string;
183
+ issues: readonly StandardSchemaV1.Issue[];
184
+ }) => void | Promise<void>;
185
+ }
186
+ type EndpointBodyMethodOptions = {
187
+ /**
188
+ * Request Method
189
+ */
190
+ method: "POST" | "PUT" | "DELETE" | "PATCH" | ("POST" | "PUT" | "DELETE" | "PATCH")[];
191
+ /**
192
+ * Body Schema
193
+ */
194
+ body?: StandardSchemaV1;
195
+ } | {
196
+ /**
197
+ * Request Method
198
+ */
199
+ method: "GET" | "HEAD" | ("GET" | "HEAD")[];
200
+ /**
201
+ * Body Schema
202
+ */
203
+ body?: never;
204
+ } | {
205
+ /**
206
+ * Request Method
207
+ */
208
+ method: "*";
209
+ /**
210
+ * Body Schema
211
+ */
212
+ body?: StandardSchemaV1;
213
+ } | {
214
+ /**
215
+ * Request Method
216
+ */
217
+ method: ("POST" | "PUT" | "DELETE" | "PATCH" | "GET" | "HEAD")[];
218
+ /**
219
+ * Body Schema
220
+ */
221
+ body?: StandardSchemaV1;
222
+ };
223
+ type EndpointOptions = EndpointBaseOptions & EndpointBodyMethodOptions;
224
+ type EndpointContext<Path extends string, Options extends EndpointOptions, Context = {}> = {
225
+ /**
226
+ * Method
227
+ *
228
+ * The request method
229
+ */
230
+ method: InferMethod<Options>;
231
+ /**
232
+ * Path
233
+ *
234
+ * The path of the endpoint
235
+ */
236
+ path: Path;
237
+ /**
238
+ * Body
239
+ *
240
+ * The body object will be the parsed JSON from the request and validated
241
+ * against the body schema if it exists.
242
+ */
243
+ body: InferBody<Options>;
244
+ /**
245
+ * Query
246
+ *
247
+ * The query object will be the parsed query string from the request
248
+ * and validated against the query schema if it exists
249
+ */
250
+ query: InferQuery<Options>;
251
+ /**
252
+ * Params
253
+ *
254
+ * If the path is `/user/:id` and the request is `/user/1` then the params will
255
+ * be `{ id: "1" }` and if the path includes a wildcard like `/user/*` then the
256
+ * params will be `{ _: "1" }` where `_` is the wildcard key. If the wildcard
257
+ * is named like `/user/**:name` then the params will be `{ name: string }`
258
+ */
259
+ params: InferParam<Path>;
260
+ /**
261
+ * Request object
262
+ *
263
+ * If `requireRequest` is set to true in the endpoint options this will be
264
+ * required
265
+ */
266
+ request: InferRequest<Options>;
267
+ /**
268
+ * Headers
269
+ *
270
+ * If `requireHeaders` is set to true in the endpoint options this will be
271
+ * required
272
+ */
273
+ headers: InferHeaders<Options>;
274
+ /**
275
+ * Set header
276
+ *
277
+ * If it's called outside of a request it will just be ignored.
278
+ */
279
+ setHeader: (key: string, value: string) => void;
280
+ /**
281
+ * Set the response status code
282
+ */
283
+ setStatus: (status: Status) => void;
284
+ /**
285
+ * Get header
286
+ *
287
+ * If it's called outside of a request it will just return null
288
+ *
289
+ * @param key - The key of the header
290
+ * @returns
291
+ */
292
+ getHeader: (key: string) => string | null;
293
+ /**
294
+ * Get a cookie value from the request
295
+ *
296
+ * @param key - The key of the cookie
297
+ * @param prefix - The prefix of the cookie between `__Secure-` and `__Host-`
298
+ * @returns - The value of the cookie
299
+ */
300
+ getCookie: (key: string, prefix?: CookiePrefixOptions) => string | null;
301
+ /**
302
+ * Get a signed cookie value from the request
303
+ *
304
+ * @param key - The key of the cookie
305
+ * @param secret - The secret of the signed cookie
306
+ * @param prefix - The prefix of the cookie between `__Secure-` and `__Host-`
307
+ * @returns - The value of the cookie or null if the cookie is not found or false if the signature is invalid
308
+ */
309
+ getSignedCookie: (key: string, secret: string, prefix?: CookiePrefixOptions) => Promise<string | null | false>;
310
+ /**
311
+ * Set a cookie value in the response
312
+ *
313
+ * @param key - The key of the cookie
314
+ * @param value - The value to set
315
+ * @param options - The options of the cookie
316
+ * @returns - The cookie string
317
+ */
318
+ setCookie: (key: string, value: string, options?: CookieOptions) => string;
319
+ /**
320
+ * Set signed cookie
321
+ *
322
+ * @param key - The key of the cookie
323
+ * @param value - The value to set
324
+ * @param secret - The secret to sign the cookie with
325
+ * @param options - The options of the cookie
326
+ * @returns - The cookie string
327
+ */
328
+ setSignedCookie: (key: string, value: string, secret: string, options?: CookieOptions) => Promise<string>;
329
+ /**
330
+ * JSON
331
+ *
332
+ * a helper function to create a JSON response with
333
+ * the correct headers
334
+ * and status code. If `asResponse` is set to true in
335
+ * the context then
336
+ * it will return a Response object instead of the
337
+ * JSON object.
338
+ *
339
+ * @param json - The JSON object to return
340
+ * @param routerResponse - The response object to
341
+ * return if `asResponse` is
342
+ * true in the context this will take precedence
343
+ */
344
+ json: <R extends Record<string, any> | null>(json: R, routerResponse?: {
345
+ status?: number;
346
+ headers?: Record<string, string>;
347
+ response?: Response;
348
+ body?: Record<string, string>;
349
+ } | Response) => Promise<R>;
350
+ /**
351
+ * Middleware context
352
+ */
353
+ context: Prettify<Context & InferUse<Options["use"]>>;
354
+ /**
355
+ * Redirect to a new URL
356
+ */
357
+ redirect: (url: string) => APIError;
358
+ /**
359
+ * Return error
360
+ */
361
+ error: (status: keyof typeof statusCodes | Status, body?: {
362
+ message?: string;
363
+ code?: string;
364
+ } & Record<string, any>, headers?: HeadersInit) => APIError;
365
+ };
366
+ type EndpointHandler<Path extends string, Options extends EndpointOptions, R> = (context: EndpointContext<Path, Options>) => Promise<R>;
367
+ declare function createEndpoint<Path extends string, Options extends EndpointOptions, R>(path: Path, options: Options, handler: EndpointHandler<Path, Options, R>): StrictEndpoint<Path, Options, R>;
368
+ declare function createEndpoint<Options extends EndpointOptions, R>(options: Options, handler: EndpointHandler<never, Options, R>): StrictEndpoint<never, Options, R>;
369
+ declare namespace createEndpoint {
370
+ var create: <E extends {
371
+ use?: Middleware[];
372
+ }>(opts?: E) => <Path extends string, Opts extends EndpointOptions, R extends Promise<any>>(path: Path, options: Opts, handler: (ctx: EndpointContext<Path, Opts, InferUse<E["use"]>>) => R) => StrictEndpoint<Path, Opts & {
373
+ use: any[];
374
+ }, any>;
375
+ }
376
+ type StrictEndpoint<Path extends string, Options extends EndpointOptions, R = any> = {
377
+ (context: InputContext<Path, Options> & {
378
+ asResponse: true;
379
+ }): Promise<Response>;
380
+ (context: InputContext<Path, Options> & {
381
+ returnHeaders: true;
382
+ returnStatus: true;
383
+ }): Promise<{
384
+ headers: Headers;
385
+ status: number;
386
+ response: Awaited<R>;
387
+ }>;
388
+ (context: InputContext<Path, Options> & {
389
+ returnHeaders: true;
390
+ returnStatus: false;
391
+ }): Promise<{
392
+ headers: Headers;
393
+ response: Awaited<R>;
394
+ }>;
395
+ (context: InputContext<Path, Options> & {
396
+ returnHeaders: false;
397
+ returnStatus: true;
398
+ }): Promise<{
399
+ status: number;
400
+ response: Awaited<R>;
401
+ }>;
402
+ (context: InputContext<Path, Options> & {
403
+ returnHeaders: false;
404
+ returnStatus: false;
405
+ }): Promise<R>;
406
+ (context: InputContext<Path, Options> & {
407
+ returnHeaders: true;
408
+ }): Promise<{
409
+ headers: Headers;
410
+ response: Awaited<R>;
411
+ }>;
412
+ (context: InputContext<Path, Options> & {
413
+ returnStatus: true;
414
+ }): Promise<{
415
+ status: number;
416
+ response: Awaited<R>;
417
+ }>;
418
+ (context?: InputContext<Path, Options>): Promise<R>;
419
+ options: Options;
420
+ path: Path;
421
+ };
422
+ type Endpoint<Path extends string = string, Options extends EndpointOptions = EndpointOptions, Handler extends (inputCtx: any) => Promise<any> = (inputCtx: any) => Promise<any>> = Handler & {
423
+ options: Options;
424
+ path: Path;
425
+ };
426
+ //#endregion
427
+ export { Endpoint, EndpointBaseOptions, EndpointBodyMethodOptions, EndpointContext, EndpointOptions, StrictEndpoint, createEndpoint };
428
+ //# sourceMappingURL=endpoint.d.cts.map