better-call 2.0.2 → 2.0.3

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.
@@ -1 +1 @@
1
- {"version":3,"file":"endpoint.cjs","names":["BetterCallError","tryCatch","createInternalContext","ValidationError","APIError","isAPIError","toResponse"],"sources":["../src/endpoint.ts"],"sourcesContent":["import { createInternalContext, type EndpointContext } from \"./context\";\nimport type { CookieOptions, CookiePrefixOptions } from \"./cookies\";\nimport {\n\tAPIError,\n\tBetterCallError,\n\ttype Status,\n\ttype statusCodes,\n\tValidationError,\n} from \"./error\";\nimport type { HasRequiredKeys, Prettify } from \"./helper\";\nimport type { Middleware } from \"./middleware\";\nimport type { OpenAPIParameter, OpenAPISchemaType } from \"./openapi\";\nimport type { StandardSchemaV1 } from \"./standard-schema\";\nimport { toResponse } from \"./to-response\";\nimport type {\n\tBodyOption,\n\tHasRequiredInputKeys,\n\tHTTPMethod,\n\tInferUse,\n\tInputContext,\n\tResolveBodyInput,\n\tResolveErrorInput,\n\tResolveMetaInput,\n\tResolveQueryInput,\n} from \"./types\";\nimport { isAPIError, tryCatch } from \"./utils\";\n\nexport type { EndpointContext } from \"./context\";\n\nexport interface EndpointMetadata {\n\t/**\n\t * Open API definition\n\t */\n\topenapi?: {\n\t\tsummary?: string;\n\t\tdescription?: string;\n\t\ttags?: string[];\n\t\toperationId?: string;\n\t\tparameters?: OpenAPIParameter[];\n\t\trequestBody?: {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype?: OpenAPISchemaType;\n\t\t\t\t\t\tproperties?: Record<string, any>;\n\t\t\t\t\t\trequired?: string[];\n\t\t\t\t\t\t$ref?: string;\n\t\t\t\t\t};\n\t\t\t\t};\n\t\t\t};\n\t\t};\n\t\tresponses?: {\n\t\t\t[status: string]: {\n\t\t\t\tdescription: string;\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\t\"text/plain\"?: {\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\t\"text/html\"?: {\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};\n\t};\n\t/**\n\t * Infer body and query type from ts interface\n\t *\n\t * useful for generic and dynamic types\n\t *\n\t * @example\n\t * ```ts\n\t * const endpoint = createEndpoint(\"/path\", {\n\t * \t\tmethod: \"POST\",\n\t * \t\tbody: z.record(z.string()),\n\t * \t\t$Infer: {\n\t * \t\t\tbody: {} as {\n\t * \t\t\t\ttype: InferTypeFromOptions<Option> // custom type inference\n\t * \t\t\t}\n\t * \t\t}\n\t * \t}, async(ctx)=>{\n\t * \t\tconst body = ctx.body\n\t * \t})\n\t * ```\n\t */\n\t$Infer?: {\n\t\t/**\n\t\t * Body\n\t\t */\n\t\tbody?: any;\n\t\t/**\n\t\t * Query\n\t\t */\n\t\tquery?: Record<string, any>;\n\t\t/**\n\t\t * Error\n\t\t */\n\t\terror?: any;\n\t};\n\t/**\n\t * If enabled, endpoint won't be exposed over a router\n\t * @deprecated Use path-less endpoints instead\n\t */\n\tSERVER_ONLY?: boolean;\n\t/**\n\t * If enabled, endpoint won't be exposed as an action to the client\n\t * @deprecated Use path-less endpoints instead\n\t */\n\tisAction?: boolean;\n\t/**\n\t * Defines the places where the endpoint will be available\n\t *\n\t * Possible options:\n\t * - `rpc` - the endpoint is exposed to the router, can be invoked directly and is available to the client\n\t * - `server` - the endpoint is exposed to the router, can be invoked directly, but is not available to the client\n\t * - `http` - the endpoint is only exposed to the router\n\t * @default \"rpc\"\n\t */\n\tscope?: \"rpc\" | \"server\" | \"http\";\n\t/**\n\t * List of allowed media types (MIME types) for the endpoint\n\t *\n\t * if provided, only the media types in the list will be allowed to be passed in the body\n\t *\n\t * @example\n\t * ```ts\n\t * const endpoint = createEndpoint(\"/path\", {\n\t * \t\tmethod: \"POST\",\n\t * \t\tallowedMediaTypes: [\"application/json\", \"application/x-www-form-urlencoded\"],\n\t * \t}, async(ctx)=>{\n\t * \t\tconst body = ctx.body\n\t * \t})\n\t * ```\n\t */\n\tallowedMediaTypes?: string[];\n\t/**\n\t * Extra metadata\n\t */\n\t[key: string]: any;\n}\n\nexport interface EndpointRuntimeOptions {\n\tmethod: string | string[];\n\tbody?: StandardSchemaV1;\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?: EndpointMetadata;\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 thrown or returned\n\t *\n\t * @param e - The API error\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?: (info: {\n\t\tmessage: string;\n\t\tissues: readonly StandardSchemaV1.Issue[];\n\t}) => void | Promise<void>;\n}\n\nexport type Endpoint<\n\tPath extends string = string,\n\tMethod = any,\n\tBody = any,\n\tQuery = any,\n\tUse extends Middleware[] = any,\n\tR = any,\n\tMeta extends EndpointMetadata | undefined = EndpointMetadata | undefined,\n\tError = any,\n> = {\n\t(\n\t\tcontext: InputContext<Path, Method, Body, Query, false, false> & {\n\t\t\tasResponse: true;\n\t\t},\n\t): Promise<Response>;\n\t(\n\t\tcontext: InputContext<Path, Method, Body, Query, false, false> & {\n\t\t\treturnHeaders: true;\n\t\t\treturnStatus: true;\n\t\t},\n\t): Promise<{\n\t\theaders: Headers;\n\t\tstatus: number;\n\t\tresponse: Awaited<R>;\n\t}>;\n\t(\n\t\tcontext: InputContext<Path, Method, Body, Query, false, false> & {\n\t\t\treturnHeaders: true;\n\t\t},\n\t): Promise<{\n\t\theaders: Headers;\n\t\tresponse: Awaited<R>;\n\t}>;\n\t(\n\t\tcontext: InputContext<Path, Method, Body, Query, false, false> & {\n\t\t\treturnStatus: true;\n\t\t},\n\t): Promise<{\n\t\tstatus: number;\n\t\tresponse: Awaited<R>;\n\t}>;\n\t(\n\t\tcontext?: InputContext<Path, Method, Body, Query, false, false>,\n\t): Promise<Awaited<R>>;\n\toptions: Omit<EndpointRuntimeOptions, \"method\" | \"metadata\"> & {\n\t\tmethod: Method;\n\t\tmetadata?: Meta;\n\t};\n\tpath: Path;\n};\n\n// Path + options + handler overload\nexport function createEndpoint<\n\tPath extends string,\n\tMethod extends HTTPMethod | HTTPMethod[] | \"*\",\n\tBodySchema extends object | undefined = undefined,\n\tQuerySchema extends object | undefined = undefined,\n\tUse extends Middleware[] = [],\n\tReqHeaders extends boolean = false,\n\tReqRequest extends boolean = false,\n\tR = unknown,\n\tMeta extends EndpointMetadata | undefined = undefined,\n\tErrorSchema extends StandardSchemaV1 | undefined = undefined,\n>(\n\tpath: Path,\n\toptions: { method: Method } & BodyOption<Method, BodySchema> & {\n\t\t\tquery?: QuerySchema;\n\t\t\tuse?: [...Use];\n\t\t\trequireHeaders?: ReqHeaders;\n\t\t\trequireRequest?: ReqRequest;\n\t\t\terror?: ErrorSchema;\n\t\t\tcloneRequest?: boolean;\n\t\t\tdisableBody?: boolean;\n\t\t\tmetadata?: Meta;\n\t\t\tonAPIError?: (e: APIError) => void | Promise<void>;\n\t\t\tonValidationError?: (info: {\n\t\t\t\tmessage: string;\n\t\t\t\tissues: readonly StandardSchemaV1.Issue[];\n\t\t\t}) => void | Promise<void>;\n\t\t\t[key: string]: any;\n\t\t},\n\thandler: (\n\t\tctx: EndpointContext<\n\t\t\tPath,\n\t\t\tMethod,\n\t\t\tBodySchema,\n\t\t\tQuerySchema,\n\t\t\tUse,\n\t\t\tReqHeaders,\n\t\t\tReqRequest,\n\t\t\tInferUse<Use>,\n\t\t\tMeta\n\t\t>,\n\t) => Promise<R>,\n): Endpoint<\n\tPath,\n\tMethod,\n\tResolveBodyInput<BodySchema, Meta>,\n\tResolveQueryInput<QuerySchema, Meta>,\n\tUse,\n\tR,\n\tResolveMetaInput<Meta>,\n\tResolveErrorInput<ErrorSchema, Meta>\n>;\n\n// Options-only (virtual/path-less) overload\nexport function createEndpoint<\n\tMethod extends HTTPMethod | HTTPMethod[] | \"*\",\n\tBodySchema extends object | undefined = undefined,\n\tQuerySchema extends object | undefined = undefined,\n\tUse extends Middleware[] = [],\n\tReqHeaders extends boolean = false,\n\tReqRequest extends boolean = false,\n\tR = unknown,\n\tMeta extends EndpointMetadata | undefined = undefined,\n\tErrorSchema extends StandardSchemaV1 | undefined = undefined,\n>(\n\toptions: { method: Method } & BodyOption<Method, BodySchema> & {\n\t\t\tpath?: never;\n\t\t\tquery?: QuerySchema;\n\t\t\tuse?: [...Use];\n\t\t\trequireHeaders?: ReqHeaders;\n\t\t\trequireRequest?: ReqRequest;\n\t\t\terror?: ErrorSchema;\n\t\t\tcloneRequest?: boolean;\n\t\t\tdisableBody?: boolean;\n\t\t\tmetadata?: Meta;\n\t\t\tonAPIError?: (e: APIError) => void | Promise<void>;\n\t\t\tonValidationError?: (info: {\n\t\t\t\tmessage: string;\n\t\t\t\tissues: readonly StandardSchemaV1.Issue[];\n\t\t\t}) => void | Promise<void>;\n\t\t\t[key: string]: any;\n\t\t},\n\thandler: (\n\t\tctx: EndpointContext<\n\t\t\tnever,\n\t\t\tMethod,\n\t\t\tBodySchema,\n\t\t\tQuerySchema,\n\t\t\tUse,\n\t\t\tReqHeaders,\n\t\t\tReqRequest,\n\t\t\tInferUse<Use>,\n\t\t\tMeta\n\t\t>,\n\t) => Promise<R>,\n): Endpoint<\n\tnever,\n\tMethod,\n\tResolveBodyInput<BodySchema, Meta>,\n\tResolveQueryInput<QuerySchema, Meta>,\n\tUse,\n\tR,\n\tResolveMetaInput<Meta>,\n\tResolveErrorInput<ErrorSchema, Meta>\n>;\n\n// Implementation\nexport function createEndpoint(\n\tpathOrOptions: any,\n\thandlerOrOptions: any,\n\thandlerOrNever?: any,\n): any {\n\tconst path: string | undefined =\n\t\ttypeof pathOrOptions === \"string\" ? pathOrOptions : undefined;\n\tconst options: any =\n\t\ttypeof handlerOrOptions === \"object\" ? handlerOrOptions : pathOrOptions;\n\tconst handler: any =\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\n\tconst runtimeOptions: EndpointRuntimeOptions = {\n\t\tmethod: options.method,\n\t\tbody: options.body,\n\t\tquery: options.query,\n\t\terror: options.error,\n\t\trequireHeaders: options.requireHeaders,\n\t\trequireRequest: options.requireRequest,\n\t\tcloneRequest: options.cloneRequest,\n\t\tdisableBody: options.disableBody,\n\t\tmetadata: options.metadata,\n\t\tuse: options.use,\n\t\tonAPIError: options.onAPIError,\n\t\tonValidationError: options.onValidationError,\n\t};\n\n\tconst internalHandler = async (...inputCtx: any[]): Promise<any> => {\n\t\tconst context = (inputCtx[0] || {}) as Record<string, any>;\n\t\tconst { data: internalContext, error: validationError } = await tryCatch(\n\t\t\tcreateInternalContext(context, {\n\t\t\t\toptions: runtimeOptions,\n\t\t\t\tpath,\n\t\t\t}),\n\t\t);\n\n\t\tif (validationError) {\n\t\t\tif (!(validationError instanceof ValidationError)) throw validationError;\n\n\t\t\tif (options.onValidationError) {\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\n\t\tconst response = await handler(internalContext as any).catch(\n\t\t\tasync (e: any) => {\n\t\t\t\tif (isAPIError(e)) {\n\t\t\t\t\tconst onAPIError = options.onAPIError;\n\t\t\t\t\tif (onAPIError) {\n\t\t\t\t\t\tawait onAPIError(e);\n\t\t\t\t\t}\n\t\t\t\t\tif (context.asResponse) {\n\t\t\t\t\t\treturn e;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tthrow e;\n\t\t\t},\n\t\t);\n\n\t\tconst responseHeaders = internalContext.responseHeaders;\n\t\tconst status = internalContext.responseStatus;\n\n\t\treturn context.asResponse\n\t\t\t? toResponse(response, {\n\t\t\t\t\theaders: responseHeaders,\n\t\t\t\t\tstatus,\n\t\t\t\t})\n\t\t\t: context.returnHeaders\n\t\t\t\t? context.returnStatus\n\t\t\t\t\t? {\n\t\t\t\t\t\t\theaders: responseHeaders,\n\t\t\t\t\t\t\tresponse,\n\t\t\t\t\t\t\tstatus,\n\t\t\t\t\t\t}\n\t\t\t\t\t: {\n\t\t\t\t\t\t\theaders: responseHeaders,\n\t\t\t\t\t\t\tresponse,\n\t\t\t\t\t\t}\n\t\t\t\t: context.returnStatus\n\t\t\t\t\t? { response, status }\n\t\t\t\t\t: response;\n\t};\n\n\tinternalHandler.options = runtimeOptions;\n\tinternalHandler.path = path;\n\treturn internalHandler as any;\n}\n\ncreateEndpoint.create = <E extends { use?: Middleware[] }>(opts?: E) => {\n\treturn <\n\t\tPath extends string,\n\t\tMethod extends HTTPMethod | HTTPMethod[] | \"*\",\n\t\tBodySchema extends object | undefined = undefined,\n\t\tQuerySchema extends object | undefined = undefined,\n\t\tUse extends Middleware[] = [],\n\t\tReqHeaders extends boolean = false,\n\t\tReqRequest extends boolean = false,\n\t\tR = unknown,\n\t\tMeta extends EndpointMetadata | undefined = undefined,\n\t\tErrorSchema extends StandardSchemaV1 | undefined = undefined,\n\t>(\n\t\tpath: Path,\n\t\toptions: { method: Method } & BodyOption<Method, BodySchema> & {\n\t\t\t\tquery?: QuerySchema;\n\t\t\t\tuse?: [...Use];\n\t\t\t\trequireHeaders?: ReqHeaders;\n\t\t\t\trequireRequest?: ReqRequest;\n\t\t\t\terror?: ErrorSchema;\n\t\t\t\tcloneRequest?: boolean;\n\t\t\t\tdisableBody?: boolean;\n\t\t\t\tmetadata?: Meta;\n\t\t\t\tonAPIError?: (e: APIError) => void | Promise<void>;\n\t\t\t\tonValidationError?: (info: {\n\t\t\t\t\tmessage: string;\n\t\t\t\t\tissues: readonly StandardSchemaV1.Issue[];\n\t\t\t\t}) => void | Promise<void>;\n\t\t\t\t[key: string]: any;\n\t\t\t},\n\t\thandler: (\n\t\t\tctx: EndpointContext<\n\t\t\t\tPath,\n\t\t\t\tMethod,\n\t\t\t\tBodySchema,\n\t\t\t\tQuerySchema,\n\t\t\t\tUse,\n\t\t\t\tReqHeaders,\n\t\t\t\tReqRequest,\n\t\t\t\tInferUse<E[\"use\"]>,\n\t\t\t\tMeta\n\t\t\t>,\n\t\t) => Promise<R>,\n\t): Endpoint<\n\t\tPath,\n\t\tMethod,\n\t\tResolveBodyInput<BodySchema, Meta>,\n\t\tResolveQueryInput<QuerySchema, Meta>,\n\t\tUse,\n\t\tAwaited<R>,\n\t\tResolveMetaInput<Meta>,\n\t\tResolveErrorInput<ErrorSchema, Meta>\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} as any,\n\t\t\thandler as any,\n\t\t) as any;\n\t};\n};\n"],"mappings":";;;;;;AAiXA,SAAgB,eACf,eACA,kBACA,gBACM;CACN,MAAM,OACL,OAAO,kBAAkB,WAAW,gBAAgB;CACrD,MAAM,UACL,OAAO,qBAAqB,WAAW,mBAAmB;CAC3D,MAAM,UACL,OAAO,qBAAqB,aAAa,mBAAmB;AAE7D,MAAK,QAAQ,WAAW,SAAS,QAAQ,WAAW,WAAW,QAAQ,KACtE,OAAM,IAAIA,8BAAgB,+CAA+C;AAG1E,KAAI,QAAQ,SAAS,KAAK,KAAK,CAC9B,OAAM,IAAIA,8BAAgB,0CAA0C;CAGrE,MAAM,iBAAyC;EAC9C,QAAQ,QAAQ;EAChB,MAAM,QAAQ;EACd,OAAO,QAAQ;EACf,OAAO,QAAQ;EACf,gBAAgB,QAAQ;EACxB,gBAAgB,QAAQ;EACxB,cAAc,QAAQ;EACtB,aAAa,QAAQ;EACrB,UAAU,QAAQ;EAClB,KAAK,QAAQ;EACb,YAAY,QAAQ;EACpB,mBAAmB,QAAQ;EAC3B;CAED,MAAM,kBAAkB,OAAO,GAAG,aAAkC;EACnE,MAAM,UAAW,SAAS,MAAM,EAAE;EAClC,MAAM,EAAE,MAAM,iBAAiB,OAAO,oBAAoB,MAAMC,uBAC/DC,sCAAsB,SAAS;GAC9B,SAAS;GACT;GACA,CAAC,CACF;AAED,MAAI,iBAAiB;AACpB,OAAI,EAAE,2BAA2BC,+BAAkB,OAAM;AAEzD,OAAI,QAAQ,kBACX,OAAM,QAAQ,kBAAkB;IAC/B,SAAS,gBAAgB;IACzB,QAAQ,gBAAgB;IACxB,CAAC;AAGH,SAAM,IAAIC,uBAAS,KAAK;IACvB,SAAS,gBAAgB;IACzB,MAAM;IACN,CAAC;;EAGH,MAAM,WAAW,MAAM,QAAQ,gBAAuB,CAAC,MACtD,OAAO,MAAW;AACjB,OAAIC,yBAAW,EAAE,EAAE;IAClB,MAAM,aAAa,QAAQ;AAC3B,QAAI,WACH,OAAM,WAAW,EAAE;AAEpB,QAAI,QAAQ,WACX,QAAO;;AAGT,SAAM;IAEP;EAED,MAAM,kBAAkB,gBAAgB;EACxC,MAAM,SAAS,gBAAgB;AAE/B,SAAO,QAAQ,aACZC,+BAAW,UAAU;GACrB,SAAS;GACT;GACA,CAAC,GACD,QAAQ,gBACP,QAAQ,eACP;GACA,SAAS;GACT;GACA;GACA,GACA;GACA,SAAS;GACT;GACA,GACD,QAAQ,eACP;GAAE;GAAU;GAAQ,GACpB;;AAGN,iBAAgB,UAAU;AAC1B,iBAAgB,OAAO;AACvB,QAAO;;AAGR,eAAe,UAA4C,SAAa;AACvE,SAYC,MACA,SAgBA,YAsBI;AACJ,SAAO,eACN,MACA;GACC,GAAG;GACH,KAAK,CAAC,GAAI,SAAS,OAAO,EAAE,EAAG,GAAI,MAAM,OAAO,EAAE,CAAE;GACpD,EACD,QACA"}
1
+ {"version":3,"file":"endpoint.cjs","names":["BetterCallError","tryCatch","createInternalContext","ValidationError","APIError","isAPIError","toResponse"],"sources":["../src/endpoint.ts"],"sourcesContent":["import { createInternalContext, type EndpointContext } from \"./context\";\nimport type { CookieOptions, CookiePrefixOptions } from \"./cookies\";\nimport {\n\tAPIError,\n\tBetterCallError,\n\ttype Status,\n\ttype statusCodes,\n\tValidationError,\n} from \"./error\";\nimport type { HasRequiredKeys, Prettify } from \"./helper\";\nimport type { Middleware } from \"./middleware\";\nimport type { OpenAPIParameter, OpenAPISchemaType } from \"./openapi\";\nimport type { StandardSchemaV1 } from \"./standard-schema\";\nimport { toResponse } from \"./to-response\";\nimport type {\n\tBodyOption,\n\tHasRequiredInputKeys,\n\tHTTPMethod,\n\tInferUse,\n\tInputContext,\n\tResolveBodyInput,\n\tResolveErrorInput,\n\tResolveMetaInput,\n\tResolveQueryInput,\n} from \"./types\";\nimport { isAPIError, tryCatch } from \"./utils\";\n\nexport type { EndpointContext } from \"./context\";\n\nexport interface EndpointMetadata {\n\t/**\n\t * Open API definition\n\t */\n\topenapi?: {\n\t\tsummary?: string;\n\t\tdescription?: string;\n\t\ttags?: string[];\n\t\toperationId?: string;\n\t\tparameters?: OpenAPIParameter[];\n\t\trequestBody?: {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype?: OpenAPISchemaType;\n\t\t\t\t\t\tproperties?: Record<string, any>;\n\t\t\t\t\t\trequired?: string[];\n\t\t\t\t\t\t$ref?: string;\n\t\t\t\t\t};\n\t\t\t\t};\n\t\t\t};\n\t\t};\n\t\tresponses?: {\n\t\t\t[status: string]: {\n\t\t\t\tdescription: string;\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\t\"text/plain\"?: {\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\t\"text/html\"?: {\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};\n\t};\n\t/**\n\t * Infer body and query type from ts interface\n\t *\n\t * useful for generic and dynamic types\n\t *\n\t * @example\n\t * ```ts\n\t * const endpoint = createEndpoint(\"/path\", {\n\t * \t\tmethod: \"POST\",\n\t * \t\tbody: z.record(z.string()),\n\t * \t\t$Infer: {\n\t * \t\t\tbody: {} as {\n\t * \t\t\t\ttype: InferTypeFromOptions<Option> // custom type inference\n\t * \t\t\t}\n\t * \t\t}\n\t * \t}, async(ctx)=>{\n\t * \t\tconst body = ctx.body\n\t * \t})\n\t * ```\n\t */\n\t$Infer?: {\n\t\t/**\n\t\t * Body\n\t\t */\n\t\tbody?: any;\n\t\t/**\n\t\t * Query\n\t\t */\n\t\tquery?: Record<string, any>;\n\t\t/**\n\t\t * Error\n\t\t */\n\t\terror?: any;\n\t};\n\t/**\n\t * If enabled, endpoint won't be exposed over a router\n\t * @deprecated Use path-less endpoints instead\n\t */\n\tSERVER_ONLY?: boolean;\n\t/**\n\t * If enabled, endpoint won't be exposed as an action to the client\n\t * @deprecated Use path-less endpoints instead\n\t */\n\tisAction?: boolean;\n\t/**\n\t * Defines the places where the endpoint will be available\n\t *\n\t * Possible options:\n\t * - `rpc` - the endpoint is exposed to the router, can be invoked directly and is available to the client\n\t * - `server` - the endpoint is exposed to the router, can be invoked directly, but is not available to the client\n\t * - `http` - the endpoint is only exposed to the router\n\t * @default \"rpc\"\n\t */\n\tscope?: \"rpc\" | \"server\" | \"http\";\n\t/**\n\t * List of allowed media types (MIME types) for the endpoint\n\t *\n\t * if provided, only the media types in the list will be allowed to be passed in the body\n\t *\n\t * @example\n\t * ```ts\n\t * const endpoint = createEndpoint(\"/path\", {\n\t * \t\tmethod: \"POST\",\n\t * \t\tallowedMediaTypes: [\"application/json\", \"application/x-www-form-urlencoded\"],\n\t * \t}, async(ctx)=>{\n\t * \t\tconst body = ctx.body\n\t * \t})\n\t * ```\n\t */\n\tallowedMediaTypes?: string[];\n\t/**\n\t * Extra metadata\n\t */\n\t[key: string]: any;\n}\n\nexport interface EndpointRuntimeOptions {\n\tmethod: string | string[];\n\tbody?: StandardSchemaV1;\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?: EndpointMetadata;\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 thrown or returned\n\t *\n\t * @param e - The API error\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?: (info: {\n\t\tmessage: string;\n\t\tissues: readonly StandardSchemaV1.Issue[];\n\t}) => void | Promise<void>;\n}\n\nexport type Endpoint<\n\tPath extends string = string,\n\tMethod = any,\n\tBody = any,\n\tQuery = any,\n\tUse extends Middleware[] = any,\n\tR = any,\n\tMeta extends EndpointMetadata | undefined = EndpointMetadata | undefined,\n\tError = any,\n> = {\n\t(\n\t\tcontext: InputContext<Path, Method, Body, Query, false, false> & {\n\t\t\tasResponse: true;\n\t\t},\n\t): Promise<Response>;\n\t(\n\t\tcontext: InputContext<Path, Method, Body, Query, false, false> & {\n\t\t\treturnHeaders: true;\n\t\t\treturnStatus: true;\n\t\t},\n\t): Promise<{\n\t\theaders: Headers;\n\t\tstatus: number;\n\t\tresponse: Awaited<R>;\n\t}>;\n\t(\n\t\tcontext: InputContext<Path, Method, Body, Query, false, false> & {\n\t\t\treturnHeaders: true;\n\t\t},\n\t): Promise<{\n\t\theaders: Headers;\n\t\tresponse: Awaited<R>;\n\t}>;\n\t(\n\t\tcontext: InputContext<Path, Method, Body, Query, false, false> & {\n\t\t\treturnStatus: true;\n\t\t},\n\t): Promise<{\n\t\tstatus: number;\n\t\tresponse: Awaited<R>;\n\t}>;\n\t(\n\t\tcontext?: InputContext<Path, Method, Body, Query, false, false>,\n\t): Promise<Awaited<R>>;\n\treadonly options: Omit<EndpointRuntimeOptions, \"method\" | \"metadata\"> & {\n\t\treadonly method: Method;\n\t\treadonly metadata?: Meta;\n\t};\n\treadonly path: Path;\n};\n\n// Path + options + handler overload\nexport function createEndpoint<\n\tPath extends string,\n\tMethod extends HTTPMethod | HTTPMethod[] | \"*\",\n\tBodySchema extends object | undefined = undefined,\n\tQuerySchema extends object | undefined = undefined,\n\tUse extends Middleware[] = [],\n\tReqHeaders extends boolean = false,\n\tReqRequest extends boolean = false,\n\tR = unknown,\n\tMeta extends EndpointMetadata | undefined = undefined,\n\tErrorSchema extends StandardSchemaV1 | undefined = undefined,\n>(\n\tpath: Path,\n\toptions: { method: Method } & BodyOption<Method, BodySchema> & {\n\t\t\tquery?: QuerySchema;\n\t\t\tuse?: [...Use];\n\t\t\trequireHeaders?: ReqHeaders;\n\t\t\trequireRequest?: ReqRequest;\n\t\t\terror?: ErrorSchema;\n\t\t\tcloneRequest?: boolean;\n\t\t\tdisableBody?: boolean;\n\t\t\tmetadata?: Meta;\n\t\t\tonAPIError?: (e: APIError) => void | Promise<void>;\n\t\t\tonValidationError?: (info: {\n\t\t\t\tmessage: string;\n\t\t\t\tissues: readonly StandardSchemaV1.Issue[];\n\t\t\t}) => void | Promise<void>;\n\t\t\t[key: string]: any;\n\t\t},\n\thandler: (\n\t\tctx: EndpointContext<\n\t\t\tPath,\n\t\t\tMethod,\n\t\t\tBodySchema,\n\t\t\tQuerySchema,\n\t\t\tUse,\n\t\t\tReqHeaders,\n\t\t\tReqRequest,\n\t\t\tInferUse<Use>,\n\t\t\tMeta\n\t\t>,\n\t) => Promise<R>,\n): Endpoint<\n\tPath,\n\tMethod,\n\tResolveBodyInput<BodySchema, Meta>,\n\tResolveQueryInput<QuerySchema, Meta>,\n\tUse,\n\tR,\n\tResolveMetaInput<Meta>,\n\tResolveErrorInput<ErrorSchema, Meta>\n>;\n\n// Options-only (virtual/path-less) overload\nexport function createEndpoint<\n\tMethod extends HTTPMethod | HTTPMethod[] | \"*\",\n\tBodySchema extends object | undefined = undefined,\n\tQuerySchema extends object | undefined = undefined,\n\tUse extends Middleware[] = [],\n\tReqHeaders extends boolean = false,\n\tReqRequest extends boolean = false,\n\tR = unknown,\n\tMeta extends EndpointMetadata | undefined = undefined,\n\tErrorSchema extends StandardSchemaV1 | undefined = undefined,\n>(\n\toptions: { method: Method } & BodyOption<Method, BodySchema> & {\n\t\t\tpath?: never;\n\t\t\tquery?: QuerySchema;\n\t\t\tuse?: [...Use];\n\t\t\trequireHeaders?: ReqHeaders;\n\t\t\trequireRequest?: ReqRequest;\n\t\t\terror?: ErrorSchema;\n\t\t\tcloneRequest?: boolean;\n\t\t\tdisableBody?: boolean;\n\t\t\tmetadata?: Meta;\n\t\t\tonAPIError?: (e: APIError) => void | Promise<void>;\n\t\t\tonValidationError?: (info: {\n\t\t\t\tmessage: string;\n\t\t\t\tissues: readonly StandardSchemaV1.Issue[];\n\t\t\t}) => void | Promise<void>;\n\t\t\t[key: string]: any;\n\t\t},\n\thandler: (\n\t\tctx: EndpointContext<\n\t\t\tnever,\n\t\t\tMethod,\n\t\t\tBodySchema,\n\t\t\tQuerySchema,\n\t\t\tUse,\n\t\t\tReqHeaders,\n\t\t\tReqRequest,\n\t\t\tInferUse<Use>,\n\t\t\tMeta\n\t\t>,\n\t) => Promise<R>,\n): Endpoint<\n\tnever,\n\tMethod,\n\tResolveBodyInput<BodySchema, Meta>,\n\tResolveQueryInput<QuerySchema, Meta>,\n\tUse,\n\tR,\n\tResolveMetaInput<Meta>,\n\tResolveErrorInput<ErrorSchema, Meta>\n>;\n\n// Implementation\nexport function createEndpoint(\n\tpathOrOptions: any,\n\thandlerOrOptions: any,\n\thandlerOrNever?: any,\n): any {\n\tconst path: string | undefined =\n\t\ttypeof pathOrOptions === \"string\" ? pathOrOptions : undefined;\n\tconst options: any =\n\t\ttypeof handlerOrOptions === \"object\" ? handlerOrOptions : pathOrOptions;\n\tconst handler: any =\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\n\tconst runtimeOptions: EndpointRuntimeOptions = {\n\t\tmethod: options.method,\n\t\tbody: options.body,\n\t\tquery: options.query,\n\t\terror: options.error,\n\t\trequireHeaders: options.requireHeaders,\n\t\trequireRequest: options.requireRequest,\n\t\tcloneRequest: options.cloneRequest,\n\t\tdisableBody: options.disableBody,\n\t\tmetadata: options.metadata,\n\t\tuse: options.use,\n\t\tonAPIError: options.onAPIError,\n\t\tonValidationError: options.onValidationError,\n\t};\n\n\tconst internalHandler = async (...inputCtx: any[]): Promise<any> => {\n\t\tconst context = (inputCtx[0] || {}) as Record<string, any>;\n\t\tconst { data: internalContext, error: validationError } = await tryCatch(\n\t\t\tcreateInternalContext(context, {\n\t\t\t\toptions: runtimeOptions,\n\t\t\t\tpath,\n\t\t\t}),\n\t\t);\n\n\t\tif (validationError) {\n\t\t\tif (!(validationError instanceof ValidationError)) throw validationError;\n\n\t\t\tif (options.onValidationError) {\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\n\t\tconst response = await handler(internalContext as any).catch(\n\t\t\tasync (e: any) => {\n\t\t\t\tif (isAPIError(e)) {\n\t\t\t\t\tconst onAPIError = options.onAPIError;\n\t\t\t\t\tif (onAPIError) {\n\t\t\t\t\t\tawait onAPIError(e);\n\t\t\t\t\t}\n\t\t\t\t\tif (context.asResponse) {\n\t\t\t\t\t\treturn e;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tthrow e;\n\t\t\t},\n\t\t);\n\n\t\tconst responseHeaders = internalContext.responseHeaders;\n\t\tconst status = internalContext.responseStatus;\n\n\t\treturn context.asResponse\n\t\t\t? toResponse(response, {\n\t\t\t\t\theaders: responseHeaders,\n\t\t\t\t\tstatus,\n\t\t\t\t})\n\t\t\t: context.returnHeaders\n\t\t\t\t? context.returnStatus\n\t\t\t\t\t? {\n\t\t\t\t\t\t\theaders: responseHeaders,\n\t\t\t\t\t\t\tresponse,\n\t\t\t\t\t\t\tstatus,\n\t\t\t\t\t\t}\n\t\t\t\t\t: {\n\t\t\t\t\t\t\theaders: responseHeaders,\n\t\t\t\t\t\t\tresponse,\n\t\t\t\t\t\t}\n\t\t\t\t: context.returnStatus\n\t\t\t\t\t? { response, status }\n\t\t\t\t\t: response;\n\t};\n\n\tinternalHandler.options = runtimeOptions;\n\tinternalHandler.path = path;\n\treturn internalHandler as any;\n}\n\ncreateEndpoint.create = <E extends { use?: Middleware[] }>(opts?: E) => {\n\treturn <\n\t\tPath extends string,\n\t\tMethod extends HTTPMethod | HTTPMethod[] | \"*\",\n\t\tBodySchema extends object | undefined = undefined,\n\t\tQuerySchema extends object | undefined = undefined,\n\t\tUse extends Middleware[] = [],\n\t\tReqHeaders extends boolean = false,\n\t\tReqRequest extends boolean = false,\n\t\tR = unknown,\n\t\tMeta extends EndpointMetadata | undefined = undefined,\n\t\tErrorSchema extends StandardSchemaV1 | undefined = undefined,\n\t>(\n\t\tpath: Path,\n\t\toptions: { method: Method } & BodyOption<Method, BodySchema> & {\n\t\t\t\tquery?: QuerySchema;\n\t\t\t\tuse?: [...Use];\n\t\t\t\trequireHeaders?: ReqHeaders;\n\t\t\t\trequireRequest?: ReqRequest;\n\t\t\t\terror?: ErrorSchema;\n\t\t\t\tcloneRequest?: boolean;\n\t\t\t\tdisableBody?: boolean;\n\t\t\t\tmetadata?: Meta;\n\t\t\t\tonAPIError?: (e: APIError) => void | Promise<void>;\n\t\t\t\tonValidationError?: (info: {\n\t\t\t\t\tmessage: string;\n\t\t\t\t\tissues: readonly StandardSchemaV1.Issue[];\n\t\t\t\t}) => void | Promise<void>;\n\t\t\t\t[key: string]: any;\n\t\t\t},\n\t\thandler: (\n\t\t\tctx: EndpointContext<\n\t\t\t\tPath,\n\t\t\t\tMethod,\n\t\t\t\tBodySchema,\n\t\t\t\tQuerySchema,\n\t\t\t\tUse,\n\t\t\t\tReqHeaders,\n\t\t\t\tReqRequest,\n\t\t\t\tInferUse<E[\"use\"]>,\n\t\t\t\tMeta\n\t\t\t>,\n\t\t) => Promise<R>,\n\t): Endpoint<\n\t\tPath,\n\t\tMethod,\n\t\tResolveBodyInput<BodySchema, Meta>,\n\t\tResolveQueryInput<QuerySchema, Meta>,\n\t\tUse,\n\t\tAwaited<R>,\n\t\tResolveMetaInput<Meta>,\n\t\tResolveErrorInput<ErrorSchema, Meta>\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} as any,\n\t\t\thandler as any,\n\t\t) as any;\n\t};\n};\n"],"mappings":";;;;;;AAiXA,SAAgB,eACf,eACA,kBACA,gBACM;CACN,MAAM,OACL,OAAO,kBAAkB,WAAW,gBAAgB;CACrD,MAAM,UACL,OAAO,qBAAqB,WAAW,mBAAmB;CAC3D,MAAM,UACL,OAAO,qBAAqB,aAAa,mBAAmB;AAE7D,MAAK,QAAQ,WAAW,S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AAS;GACT;GACA,GACD,QAAQ,eACP;GAAE;GAAU;GAAQ,GACpB;;AAGN,iBAAgB,UAAU;AAC1B,iBAAgB,OAAO;AACvB,QAAO;;AAGR,eAAe,UAA4C,SAAa;AACvE,SAYC,MACA,SAgBA,YAsBI;AACJ,SAAO,eACN,MACA;GACC,GAAG;GACH,KAAK,CAAC,GAAI,SAAS,OAAO,EAAE,EAAG,GAAI,MAAM,OAAO,EAAE,CAAE;GACpD,EACD,QACA"}
@@ -210,11 +210,11 @@ type Endpoint<Path extends string = string, Method = any, Body = any, Query = an
210
210
  response: Awaited<R>;
211
211
  }>;
212
212
  (context?: InputContext<Path, Method, Body, Query, false, false>): Promise<Awaited<R>>;
213
- options: Omit<EndpointRuntimeOptions, "method" | "metadata"> & {
214
- method: Method;
215
- metadata?: Meta;
213
+ readonly options: Omit<EndpointRuntimeOptions, "method" | "metadata"> & {
214
+ readonly method: Method;
215
+ readonly metadata?: Meta;
216
216
  };
217
- path: Path;
217
+ readonly path: Path;
218
218
  };
219
219
  declare function createEndpoint<Path extends string, Method extends HTTPMethod | HTTPMethod[] | "*", BodySchema extends object | undefined = undefined, QuerySchema extends object | undefined = undefined, Use extends Middleware[] = [], ReqHeaders extends boolean = false, ReqRequest extends boolean = false, R = unknown, Meta extends EndpointMetadata | undefined = undefined, ErrorSchema extends StandardSchemaV1 | undefined = undefined>(path: Path, options: {
220
220
  method: Method;
@@ -210,11 +210,11 @@ type Endpoint<Path extends string = string, Method = any, Body = any, Query = an
210
210
  response: Awaited<R>;
211
211
  }>;
212
212
  (context?: InputContext<Path, Method, Body, Query, false, false>): Promise<Awaited<R>>;
213
- options: Omit<EndpointRuntimeOptions, "method" | "metadata"> & {
214
- method: Method;
215
- metadata?: Meta;
213
+ readonly options: Omit<EndpointRuntimeOptions, "method" | "metadata"> & {
214
+ readonly method: Method;
215
+ readonly metadata?: Meta;
216
216
  };
217
- path: Path;
217
+ readonly path: Path;
218
218
  };
219
219
  declare function createEndpoint<Path extends string, Method extends HTTPMethod | HTTPMethod[] | "*", BodySchema extends object | undefined = undefined, QuerySchema extends object | undefined = undefined, Use extends Middleware[] = [], ReqHeaders extends boolean = false, ReqRequest extends boolean = false, R = unknown, Meta extends EndpointMetadata | undefined = undefined, ErrorSchema extends StandardSchemaV1 | undefined = undefined>(path: Path, options: {
220
220
  method: Method;
@@ -1 +1 @@
1
- {"version":3,"file":"endpoint.mjs","names":[],"sources":["../src/endpoint.ts"],"sourcesContent":["import { createInternalContext, type EndpointContext } from \"./context\";\nimport type { CookieOptions, CookiePrefixOptions } from \"./cookies\";\nimport {\n\tAPIError,\n\tBetterCallError,\n\ttype Status,\n\ttype statusCodes,\n\tValidationError,\n} from \"./error\";\nimport type { HasRequiredKeys, Prettify } from \"./helper\";\nimport type { Middleware } from \"./middleware\";\nimport type { OpenAPIParameter, OpenAPISchemaType } from \"./openapi\";\nimport type { StandardSchemaV1 } from \"./standard-schema\";\nimport { toResponse } from \"./to-response\";\nimport type {\n\tBodyOption,\n\tHasRequiredInputKeys,\n\tHTTPMethod,\n\tInferUse,\n\tInputContext,\n\tResolveBodyInput,\n\tResolveErrorInput,\n\tResolveMetaInput,\n\tResolveQueryInput,\n} from \"./types\";\nimport { isAPIError, tryCatch } from \"./utils\";\n\nexport type { EndpointContext } from \"./context\";\n\nexport interface EndpointMetadata {\n\t/**\n\t * Open API definition\n\t */\n\topenapi?: {\n\t\tsummary?: string;\n\t\tdescription?: string;\n\t\ttags?: string[];\n\t\toperationId?: string;\n\t\tparameters?: OpenAPIParameter[];\n\t\trequestBody?: {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype?: OpenAPISchemaType;\n\t\t\t\t\t\tproperties?: Record<string, any>;\n\t\t\t\t\t\trequired?: string[];\n\t\t\t\t\t\t$ref?: string;\n\t\t\t\t\t};\n\t\t\t\t};\n\t\t\t};\n\t\t};\n\t\tresponses?: {\n\t\t\t[status: string]: {\n\t\t\t\tdescription: string;\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\t\"text/plain\"?: {\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\t\"text/html\"?: {\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};\n\t};\n\t/**\n\t * Infer body and query type from ts interface\n\t *\n\t * useful for generic and dynamic types\n\t *\n\t * @example\n\t * ```ts\n\t * const endpoint = createEndpoint(\"/path\", {\n\t * \t\tmethod: \"POST\",\n\t * \t\tbody: z.record(z.string()),\n\t * \t\t$Infer: {\n\t * \t\t\tbody: {} as {\n\t * \t\t\t\ttype: InferTypeFromOptions<Option> // custom type inference\n\t * \t\t\t}\n\t * \t\t}\n\t * \t}, async(ctx)=>{\n\t * \t\tconst body = ctx.body\n\t * \t})\n\t * ```\n\t */\n\t$Infer?: {\n\t\t/**\n\t\t * Body\n\t\t */\n\t\tbody?: any;\n\t\t/**\n\t\t * Query\n\t\t */\n\t\tquery?: Record<string, any>;\n\t\t/**\n\t\t * Error\n\t\t */\n\t\terror?: any;\n\t};\n\t/**\n\t * If enabled, endpoint won't be exposed over a router\n\t * @deprecated Use path-less endpoints instead\n\t */\n\tSERVER_ONLY?: boolean;\n\t/**\n\t * If enabled, endpoint won't be exposed as an action to the client\n\t * @deprecated Use path-less endpoints instead\n\t */\n\tisAction?: boolean;\n\t/**\n\t * Defines the places where the endpoint will be available\n\t *\n\t * Possible options:\n\t * - `rpc` - the endpoint is exposed to the router, can be invoked directly and is available to the client\n\t * - `server` - the endpoint is exposed to the router, can be invoked directly, but is not available to the client\n\t * - `http` - the endpoint is only exposed to the router\n\t * @default \"rpc\"\n\t */\n\tscope?: \"rpc\" | \"server\" | \"http\";\n\t/**\n\t * List of allowed media types (MIME types) for the endpoint\n\t *\n\t * if provided, only the media types in the list will be allowed to be passed in the body\n\t *\n\t * @example\n\t * ```ts\n\t * const endpoint = createEndpoint(\"/path\", {\n\t * \t\tmethod: \"POST\",\n\t * \t\tallowedMediaTypes: [\"application/json\", \"application/x-www-form-urlencoded\"],\n\t * \t}, async(ctx)=>{\n\t * \t\tconst body = ctx.body\n\t * \t})\n\t * ```\n\t */\n\tallowedMediaTypes?: string[];\n\t/**\n\t * Extra metadata\n\t */\n\t[key: string]: any;\n}\n\nexport interface EndpointRuntimeOptions {\n\tmethod: string | string[];\n\tbody?: StandardSchemaV1;\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?: EndpointMetadata;\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 thrown or returned\n\t *\n\t * @param e - The API error\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?: (info: {\n\t\tmessage: string;\n\t\tissues: readonly StandardSchemaV1.Issue[];\n\t}) => void | Promise<void>;\n}\n\nexport type Endpoint<\n\tPath extends string = string,\n\tMethod = any,\n\tBody = any,\n\tQuery = any,\n\tUse extends Middleware[] = any,\n\tR = any,\n\tMeta extends EndpointMetadata | undefined = EndpointMetadata | undefined,\n\tError = any,\n> = {\n\t(\n\t\tcontext: InputContext<Path, Method, Body, Query, false, false> & {\n\t\t\tasResponse: true;\n\t\t},\n\t): Promise<Response>;\n\t(\n\t\tcontext: InputContext<Path, Method, Body, Query, false, false> & {\n\t\t\treturnHeaders: true;\n\t\t\treturnStatus: true;\n\t\t},\n\t): Promise<{\n\t\theaders: Headers;\n\t\tstatus: number;\n\t\tresponse: Awaited<R>;\n\t}>;\n\t(\n\t\tcontext: InputContext<Path, Method, Body, Query, false, false> & {\n\t\t\treturnHeaders: true;\n\t\t},\n\t): Promise<{\n\t\theaders: Headers;\n\t\tresponse: Awaited<R>;\n\t}>;\n\t(\n\t\tcontext: InputContext<Path, Method, Body, Query, false, false> & {\n\t\t\treturnStatus: true;\n\t\t},\n\t): Promise<{\n\t\tstatus: number;\n\t\tresponse: Awaited<R>;\n\t}>;\n\t(\n\t\tcontext?: InputContext<Path, Method, Body, Query, false, false>,\n\t): Promise<Awaited<R>>;\n\toptions: Omit<EndpointRuntimeOptions, \"method\" | \"metadata\"> & {\n\t\tmethod: Method;\n\t\tmetadata?: Meta;\n\t};\n\tpath: Path;\n};\n\n// Path + options + handler overload\nexport function createEndpoint<\n\tPath extends string,\n\tMethod extends HTTPMethod | HTTPMethod[] | \"*\",\n\tBodySchema extends object | undefined = undefined,\n\tQuerySchema extends object | undefined = undefined,\n\tUse extends Middleware[] = [],\n\tReqHeaders extends boolean = false,\n\tReqRequest extends boolean = false,\n\tR = unknown,\n\tMeta extends EndpointMetadata | undefined = undefined,\n\tErrorSchema extends StandardSchemaV1 | undefined = undefined,\n>(\n\tpath: Path,\n\toptions: { method: Method } & BodyOption<Method, BodySchema> & {\n\t\t\tquery?: QuerySchema;\n\t\t\tuse?: [...Use];\n\t\t\trequireHeaders?: ReqHeaders;\n\t\t\trequireRequest?: ReqRequest;\n\t\t\terror?: ErrorSchema;\n\t\t\tcloneRequest?: boolean;\n\t\t\tdisableBody?: boolean;\n\t\t\tmetadata?: Meta;\n\t\t\tonAPIError?: (e: APIError) => void | Promise<void>;\n\t\t\tonValidationError?: (info: {\n\t\t\t\tmessage: string;\n\t\t\t\tissues: readonly StandardSchemaV1.Issue[];\n\t\t\t}) => void | Promise<void>;\n\t\t\t[key: string]: any;\n\t\t},\n\thandler: (\n\t\tctx: EndpointContext<\n\t\t\tPath,\n\t\t\tMethod,\n\t\t\tBodySchema,\n\t\t\tQuerySchema,\n\t\t\tUse,\n\t\t\tReqHeaders,\n\t\t\tReqRequest,\n\t\t\tInferUse<Use>,\n\t\t\tMeta\n\t\t>,\n\t) => Promise<R>,\n): Endpoint<\n\tPath,\n\tMethod,\n\tResolveBodyInput<BodySchema, Meta>,\n\tResolveQueryInput<QuerySchema, Meta>,\n\tUse,\n\tR,\n\tResolveMetaInput<Meta>,\n\tResolveErrorInput<ErrorSchema, Meta>\n>;\n\n// Options-only (virtual/path-less) overload\nexport function createEndpoint<\n\tMethod extends HTTPMethod | HTTPMethod[] | \"*\",\n\tBodySchema extends object | undefined = undefined,\n\tQuerySchema extends object | undefined = undefined,\n\tUse extends Middleware[] = [],\n\tReqHeaders extends boolean = false,\n\tReqRequest extends boolean = false,\n\tR = unknown,\n\tMeta extends EndpointMetadata | undefined = undefined,\n\tErrorSchema extends StandardSchemaV1 | undefined = undefined,\n>(\n\toptions: { method: Method } & BodyOption<Method, BodySchema> & {\n\t\t\tpath?: never;\n\t\t\tquery?: QuerySchema;\n\t\t\tuse?: [...Use];\n\t\t\trequireHeaders?: ReqHeaders;\n\t\t\trequireRequest?: ReqRequest;\n\t\t\terror?: ErrorSchema;\n\t\t\tcloneRequest?: boolean;\n\t\t\tdisableBody?: boolean;\n\t\t\tmetadata?: Meta;\n\t\t\tonAPIError?: (e: APIError) => void | Promise<void>;\n\t\t\tonValidationError?: (info: {\n\t\t\t\tmessage: string;\n\t\t\t\tissues: readonly StandardSchemaV1.Issue[];\n\t\t\t}) => void | Promise<void>;\n\t\t\t[key: string]: any;\n\t\t},\n\thandler: (\n\t\tctx: EndpointContext<\n\t\t\tnever,\n\t\t\tMethod,\n\t\t\tBodySchema,\n\t\t\tQuerySchema,\n\t\t\tUse,\n\t\t\tReqHeaders,\n\t\t\tReqRequest,\n\t\t\tInferUse<Use>,\n\t\t\tMeta\n\t\t>,\n\t) => Promise<R>,\n): Endpoint<\n\tnever,\n\tMethod,\n\tResolveBodyInput<BodySchema, Meta>,\n\tResolveQueryInput<QuerySchema, Meta>,\n\tUse,\n\tR,\n\tResolveMetaInput<Meta>,\n\tResolveErrorInput<ErrorSchema, Meta>\n>;\n\n// Implementation\nexport function createEndpoint(\n\tpathOrOptions: any,\n\thandlerOrOptions: any,\n\thandlerOrNever?: any,\n): any {\n\tconst path: string | undefined =\n\t\ttypeof pathOrOptions === \"string\" ? pathOrOptions : undefined;\n\tconst options: any =\n\t\ttypeof handlerOrOptions === \"object\" ? handlerOrOptions : pathOrOptions;\n\tconst handler: any =\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\n\tconst runtimeOptions: EndpointRuntimeOptions = {\n\t\tmethod: options.method,\n\t\tbody: options.body,\n\t\tquery: options.query,\n\t\terror: options.error,\n\t\trequireHeaders: options.requireHeaders,\n\t\trequireRequest: options.requireRequest,\n\t\tcloneRequest: options.cloneRequest,\n\t\tdisableBody: options.disableBody,\n\t\tmetadata: options.metadata,\n\t\tuse: options.use,\n\t\tonAPIError: options.onAPIError,\n\t\tonValidationError: options.onValidationError,\n\t};\n\n\tconst internalHandler = async (...inputCtx: any[]): Promise<any> => {\n\t\tconst context = (inputCtx[0] || {}) as Record<string, any>;\n\t\tconst { data: internalContext, error: validationError } = await tryCatch(\n\t\t\tcreateInternalContext(context, {\n\t\t\t\toptions: runtimeOptions,\n\t\t\t\tpath,\n\t\t\t}),\n\t\t);\n\n\t\tif (validationError) {\n\t\t\tif (!(validationError instanceof ValidationError)) throw validationError;\n\n\t\t\tif (options.onValidationError) {\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\n\t\tconst response = await handler(internalContext as any).catch(\n\t\t\tasync (e: any) => {\n\t\t\t\tif (isAPIError(e)) {\n\t\t\t\t\tconst onAPIError = options.onAPIError;\n\t\t\t\t\tif (onAPIError) {\n\t\t\t\t\t\tawait onAPIError(e);\n\t\t\t\t\t}\n\t\t\t\t\tif (context.asResponse) {\n\t\t\t\t\t\treturn e;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tthrow e;\n\t\t\t},\n\t\t);\n\n\t\tconst responseHeaders = internalContext.responseHeaders;\n\t\tconst status = internalContext.responseStatus;\n\n\t\treturn context.asResponse\n\t\t\t? toResponse(response, {\n\t\t\t\t\theaders: responseHeaders,\n\t\t\t\t\tstatus,\n\t\t\t\t})\n\t\t\t: context.returnHeaders\n\t\t\t\t? context.returnStatus\n\t\t\t\t\t? {\n\t\t\t\t\t\t\theaders: responseHeaders,\n\t\t\t\t\t\t\tresponse,\n\t\t\t\t\t\t\tstatus,\n\t\t\t\t\t\t}\n\t\t\t\t\t: {\n\t\t\t\t\t\t\theaders: responseHeaders,\n\t\t\t\t\t\t\tresponse,\n\t\t\t\t\t\t}\n\t\t\t\t: context.returnStatus\n\t\t\t\t\t? { response, status }\n\t\t\t\t\t: response;\n\t};\n\n\tinternalHandler.options = runtimeOptions;\n\tinternalHandler.path = path;\n\treturn internalHandler as any;\n}\n\ncreateEndpoint.create = <E extends { use?: Middleware[] }>(opts?: E) => {\n\treturn <\n\t\tPath extends string,\n\t\tMethod extends HTTPMethod | HTTPMethod[] | \"*\",\n\t\tBodySchema extends object | undefined = undefined,\n\t\tQuerySchema extends object | undefined = undefined,\n\t\tUse extends Middleware[] = [],\n\t\tReqHeaders extends boolean = false,\n\t\tReqRequest extends boolean = false,\n\t\tR = unknown,\n\t\tMeta extends EndpointMetadata | undefined = undefined,\n\t\tErrorSchema extends StandardSchemaV1 | undefined = undefined,\n\t>(\n\t\tpath: Path,\n\t\toptions: { method: Method } & BodyOption<Method, BodySchema> & {\n\t\t\t\tquery?: QuerySchema;\n\t\t\t\tuse?: [...Use];\n\t\t\t\trequireHeaders?: ReqHeaders;\n\t\t\t\trequireRequest?: ReqRequest;\n\t\t\t\terror?: ErrorSchema;\n\t\t\t\tcloneRequest?: boolean;\n\t\t\t\tdisableBody?: boolean;\n\t\t\t\tmetadata?: Meta;\n\t\t\t\tonAPIError?: (e: APIError) => void | Promise<void>;\n\t\t\t\tonValidationError?: (info: {\n\t\t\t\t\tmessage: string;\n\t\t\t\t\tissues: readonly StandardSchemaV1.Issue[];\n\t\t\t\t}) => void | Promise<void>;\n\t\t\t\t[key: string]: any;\n\t\t\t},\n\t\thandler: (\n\t\t\tctx: EndpointContext<\n\t\t\t\tPath,\n\t\t\t\tMethod,\n\t\t\t\tBodySchema,\n\t\t\t\tQuerySchema,\n\t\t\t\tUse,\n\t\t\t\tReqHeaders,\n\t\t\t\tReqRequest,\n\t\t\t\tInferUse<E[\"use\"]>,\n\t\t\t\tMeta\n\t\t\t>,\n\t\t) => Promise<R>,\n\t): Endpoint<\n\t\tPath,\n\t\tMethod,\n\t\tResolveBodyInput<BodySchema, Meta>,\n\t\tResolveQueryInput<QuerySchema, Meta>,\n\t\tUse,\n\t\tAwaited<R>,\n\t\tResolveMetaInput<Meta>,\n\t\tResolveErrorInput<ErrorSchema, Meta>\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} as any,\n\t\t\thandler as any,\n\t\t) as any;\n\t};\n};\n"],"mappings":";;;;;;AAiXA,SAAgB,eACf,eACA,kBACA,gBACM;CACN,MAAM,OACL,OAAO,kBAAkB,WAAW,gBAAgB;CACrD,MAAM,UACL,OAAO,qBAAqB,WAAW,mBAAmB;CAC3D,MAAM,UACL,OAAO,qBAAqB,aAAa,mBAAmB;AAE7D,MAAK,QAAQ,WAAW,SAAS,QAAQ,WAAW,WAAW,QAAQ,KACtE,OAAM,IAAI,gBAAgB,+CAA+C;AAG1E,KAAI,QAAQ,SAAS,KAAK,KAAK,CAC9B,OAAM,IAAI,gBAAgB,0CAA0C;CAGrE,MAAM,iBAAyC;EAC9C,QAAQ,QAAQ;EAChB,MAAM,QAAQ;EACd,OAAO,QAAQ;EACf,OAAO,QAAQ;EACf,gBAAgB,QAAQ;EACxB,gBAAgB,QAAQ;EACxB,cAAc,QAAQ;EACtB,aAAa,QAAQ;EACrB,UAAU,QAAQ;EAClB,KAAK,QAAQ;EACb,YAAY,QAAQ;EACpB,mBAAmB,QAAQ;EAC3B;CAED,MAAM,kBAAkB,OAAO,GAAG,aAAkC;EACnE,MAAM,UAAW,SAAS,MAAM,EAAE;EAClC,MAAM,EAAE,MAAM,iBAAiB,OAAO,oBAAoB,MAAM,SAC/D,sBAAsB,SAAS;GAC9B,SAAS;GACT;GACA,CAAC,CACF;AAED,MAAI,iBAAiB;AACpB,OAAI,EAAE,2BAA2B,iBAAkB,OAAM;AAEzD,OAAI,QAAQ,kBACX,OAAM,QAAQ,kBAAkB;IAC/B,SAAS,gBAAgB;IACzB,QAAQ,gBAAgB;IACxB,CAAC;AAGH,SAAM,IAAI,SAAS,KAAK;IACvB,SAAS,gBAAgB;IACzB,MAAM;IACN,CAAC;;EAGH,MAAM,WAAW,MAAM,QAAQ,gBAAuB,CAAC,MACtD,OAAO,MAAW;AACjB,OAAI,WAAW,EAAE,EAAE;IAClB,MAAM,aAAa,QAAQ;AAC3B,QAAI,WACH,OAAM,WAAW,EAAE;AAEpB,QAAI,QAAQ,WACX,QAAO;;AAGT,SAAM;IAEP;EAED,MAAM,kBAAkB,gBAAgB;EACxC,MAAM,SAAS,gBAAgB;AAE/B,SAAO,QAAQ,aACZ,WAAW,UAAU;GACrB,SAAS;GACT;GACA,CAAC,GACD,QAAQ,gBACP,QAAQ,eACP;GACA,SAAS;GACT;GACA;GACA,GACA;GACA,SAAS;GACT;GACA,GACD,QAAQ,eACP;GAAE;GAAU;GAAQ,GACpB;;AAGN,iBAAgB,UAAU;AAC1B,iBAAgB,OAAO;AACvB,QAAO;;AAGR,eAAe,UAA4C,SAAa;AACvE,SAYC,MACA,SAgBA,YAsBI;AACJ,SAAO,eACN,MACA;GACC,GAAG;GACH,KAAK,CAAC,GAAI,SAAS,OAAO,EAAE,EAAG,GAAI,MAAM,OAAO,EAAE,CAAE;GACpD,EACD,QACA"}
1
+ {"version":3,"file":"endpoint.mjs","names":[],"sources":["../src/endpoint.ts"],"sourcesContent":["import { createInternalContext, type EndpointContext } from \"./context\";\nimport type { CookieOptions, CookiePrefixOptions } from \"./cookies\";\nimport {\n\tAPIError,\n\tBetterCallError,\n\ttype Status,\n\ttype statusCodes,\n\tValidationError,\n} from \"./error\";\nimport type { HasRequiredKeys, Prettify } from \"./helper\";\nimport type { Middleware } from \"./middleware\";\nimport type { OpenAPIParameter, OpenAPISchemaType } from \"./openapi\";\nimport type { StandardSchemaV1 } from \"./standard-schema\";\nimport { toResponse } from \"./to-response\";\nimport type {\n\tBodyOption,\n\tHasRequiredInputKeys,\n\tHTTPMethod,\n\tInferUse,\n\tInputContext,\n\tResolveBodyInput,\n\tResolveErrorInput,\n\tResolveMetaInput,\n\tResolveQueryInput,\n} from \"./types\";\nimport { isAPIError, tryCatch } from \"./utils\";\n\nexport type { EndpointContext } from \"./context\";\n\nexport interface EndpointMetadata {\n\t/**\n\t * Open API definition\n\t */\n\topenapi?: {\n\t\tsummary?: string;\n\t\tdescription?: string;\n\t\ttags?: string[];\n\t\toperationId?: string;\n\t\tparameters?: OpenAPIParameter[];\n\t\trequestBody?: {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype?: OpenAPISchemaType;\n\t\t\t\t\t\tproperties?: Record<string, any>;\n\t\t\t\t\t\trequired?: string[];\n\t\t\t\t\t\t$ref?: string;\n\t\t\t\t\t};\n\t\t\t\t};\n\t\t\t};\n\t\t};\n\t\tresponses?: {\n\t\t\t[status: string]: {\n\t\t\t\tdescription: string;\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\t\"text/plain\"?: {\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\t\"text/html\"?: {\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};\n\t};\n\t/**\n\t * Infer body and query type from ts interface\n\t *\n\t * useful for generic and dynamic types\n\t *\n\t * @example\n\t * ```ts\n\t * const endpoint = createEndpoint(\"/path\", {\n\t * \t\tmethod: \"POST\",\n\t * \t\tbody: z.record(z.string()),\n\t * \t\t$Infer: {\n\t * \t\t\tbody: {} as {\n\t * \t\t\t\ttype: InferTypeFromOptions<Option> // custom type inference\n\t * \t\t\t}\n\t * \t\t}\n\t * \t}, async(ctx)=>{\n\t * \t\tconst body = ctx.body\n\t * \t})\n\t * ```\n\t */\n\t$Infer?: {\n\t\t/**\n\t\t * Body\n\t\t */\n\t\tbody?: any;\n\t\t/**\n\t\t * Query\n\t\t */\n\t\tquery?: Record<string, any>;\n\t\t/**\n\t\t * Error\n\t\t */\n\t\terror?: any;\n\t};\n\t/**\n\t * If enabled, endpoint won't be exposed over a router\n\t * @deprecated Use path-less endpoints instead\n\t */\n\tSERVER_ONLY?: boolean;\n\t/**\n\t * If enabled, endpoint won't be exposed as an action to the client\n\t * @deprecated Use path-less endpoints instead\n\t */\n\tisAction?: boolean;\n\t/**\n\t * Defines the places where the endpoint will be available\n\t *\n\t * Possible options:\n\t * - `rpc` - the endpoint is exposed to the router, can be invoked directly and is available to the client\n\t * - `server` - the endpoint is exposed to the router, can be invoked directly, but is not available to the client\n\t * - `http` - the endpoint is only exposed to the router\n\t * @default \"rpc\"\n\t */\n\tscope?: \"rpc\" | \"server\" | \"http\";\n\t/**\n\t * List of allowed media types (MIME types) for the endpoint\n\t *\n\t * if provided, only the media types in the list will be allowed to be passed in the body\n\t *\n\t * @example\n\t * ```ts\n\t * const endpoint = createEndpoint(\"/path\", {\n\t * \t\tmethod: \"POST\",\n\t * \t\tallowedMediaTypes: [\"application/json\", \"application/x-www-form-urlencoded\"],\n\t * \t}, async(ctx)=>{\n\t * \t\tconst body = ctx.body\n\t * \t})\n\t * ```\n\t */\n\tallowedMediaTypes?: string[];\n\t/**\n\t * Extra metadata\n\t */\n\t[key: string]: any;\n}\n\nexport interface EndpointRuntimeOptions {\n\tmethod: string | string[];\n\tbody?: StandardSchemaV1;\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?: EndpointMetadata;\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 thrown or returned\n\t *\n\t * @param e - The API error\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?: (info: {\n\t\tmessage: string;\n\t\tissues: readonly StandardSchemaV1.Issue[];\n\t}) => void | Promise<void>;\n}\n\nexport type Endpoint<\n\tPath extends string = string,\n\tMethod = any,\n\tBody = any,\n\tQuery = any,\n\tUse extends Middleware[] = any,\n\tR = any,\n\tMeta extends EndpointMetadata | undefined = EndpointMetadata | undefined,\n\tError = any,\n> = {\n\t(\n\t\tcontext: InputContext<Path, Method, Body, Query, false, false> & {\n\t\t\tasResponse: true;\n\t\t},\n\t): Promise<Response>;\n\t(\n\t\tcontext: InputContext<Path, Method, Body, Query, false, false> & {\n\t\t\treturnHeaders: true;\n\t\t\treturnStatus: true;\n\t\t},\n\t): Promise<{\n\t\theaders: Headers;\n\t\tstatus: number;\n\t\tresponse: Awaited<R>;\n\t}>;\n\t(\n\t\tcontext: InputContext<Path, Method, Body, Query, false, false> & {\n\t\t\treturnHeaders: true;\n\t\t},\n\t): Promise<{\n\t\theaders: Headers;\n\t\tresponse: Awaited<R>;\n\t}>;\n\t(\n\t\tcontext: InputContext<Path, Method, Body, Query, false, false> & {\n\t\t\treturnStatus: true;\n\t\t},\n\t): Promise<{\n\t\tstatus: number;\n\t\tresponse: Awaited<R>;\n\t}>;\n\t(\n\t\tcontext?: InputContext<Path, Method, Body, Query, false, false>,\n\t): Promise<Awaited<R>>;\n\treadonly options: Omit<EndpointRuntimeOptions, \"method\" | \"metadata\"> & {\n\t\treadonly method: Method;\n\t\treadonly metadata?: Meta;\n\t};\n\treadonly path: Path;\n};\n\n// Path + options + handler overload\nexport function createEndpoint<\n\tPath extends string,\n\tMethod extends HTTPMethod | HTTPMethod[] | \"*\",\n\tBodySchema extends object | undefined = undefined,\n\tQuerySchema extends object | undefined = undefined,\n\tUse extends Middleware[] = [],\n\tReqHeaders extends boolean = false,\n\tReqRequest extends boolean = false,\n\tR = unknown,\n\tMeta extends EndpointMetadata | undefined = undefined,\n\tErrorSchema extends StandardSchemaV1 | undefined = undefined,\n>(\n\tpath: Path,\n\toptions: { method: Method } & BodyOption<Method, BodySchema> & {\n\t\t\tquery?: QuerySchema;\n\t\t\tuse?: [...Use];\n\t\t\trequireHeaders?: ReqHeaders;\n\t\t\trequireRequest?: ReqRequest;\n\t\t\terror?: ErrorSchema;\n\t\t\tcloneRequest?: boolean;\n\t\t\tdisableBody?: boolean;\n\t\t\tmetadata?: Meta;\n\t\t\tonAPIError?: (e: APIError) => void | Promise<void>;\n\t\t\tonValidationError?: (info: {\n\t\t\t\tmessage: string;\n\t\t\t\tissues: readonly StandardSchemaV1.Issue[];\n\t\t\t}) => void | Promise<void>;\n\t\t\t[key: string]: any;\n\t\t},\n\thandler: (\n\t\tctx: EndpointContext<\n\t\t\tPath,\n\t\t\tMethod,\n\t\t\tBodySchema,\n\t\t\tQuerySchema,\n\t\t\tUse,\n\t\t\tReqHeaders,\n\t\t\tReqRequest,\n\t\t\tInferUse<Use>,\n\t\t\tMeta\n\t\t>,\n\t) => Promise<R>,\n): Endpoint<\n\tPath,\n\tMethod,\n\tResolveBodyInput<BodySchema, Meta>,\n\tResolveQueryInput<QuerySchema, Meta>,\n\tUse,\n\tR,\n\tResolveMetaInput<Meta>,\n\tResolveErrorInput<ErrorSchema, Meta>\n>;\n\n// Options-only (virtual/path-less) overload\nexport function createEndpoint<\n\tMethod extends HTTPMethod | HTTPMethod[] | \"*\",\n\tBodySchema extends object | undefined = undefined,\n\tQuerySchema extends object | undefined = undefined,\n\tUse extends Middleware[] = [],\n\tReqHeaders extends boolean = false,\n\tReqRequest extends boolean = false,\n\tR = unknown,\n\tMeta extends EndpointMetadata | undefined = undefined,\n\tErrorSchema extends StandardSchemaV1 | undefined = undefined,\n>(\n\toptions: { method: Method } & BodyOption<Method, BodySchema> & {\n\t\t\tpath?: never;\n\t\t\tquery?: QuerySchema;\n\t\t\tuse?: [...Use];\n\t\t\trequireHeaders?: ReqHeaders;\n\t\t\trequireRequest?: ReqRequest;\n\t\t\terror?: ErrorSchema;\n\t\t\tcloneRequest?: boolean;\n\t\t\tdisableBody?: boolean;\n\t\t\tmetadata?: Meta;\n\t\t\tonAPIError?: (e: APIError) => void | Promise<void>;\n\t\t\tonValidationError?: (info: {\n\t\t\t\tmessage: string;\n\t\t\t\tissues: readonly StandardSchemaV1.Issue[];\n\t\t\t}) => void | Promise<void>;\n\t\t\t[key: string]: any;\n\t\t},\n\thandler: (\n\t\tctx: EndpointContext<\n\t\t\tnever,\n\t\t\tMethod,\n\t\t\tBodySchema,\n\t\t\tQuerySchema,\n\t\t\tUse,\n\t\t\tReqHeaders,\n\t\t\tReqRequest,\n\t\t\tInferUse<Use>,\n\t\t\tMeta\n\t\t>,\n\t) => Promise<R>,\n): Endpoint<\n\tnever,\n\tMethod,\n\tResolveBodyInput<BodySchema, Meta>,\n\tResolveQueryInput<QuerySchema, Meta>,\n\tUse,\n\tR,\n\tResolveMetaInput<Meta>,\n\tResolveErrorInput<ErrorSchema, Meta>\n>;\n\n// Implementation\nexport function createEndpoint(\n\tpathOrOptions: any,\n\thandlerOrOptions: any,\n\thandlerOrNever?: any,\n): any {\n\tconst path: string | undefined =\n\t\ttypeof pathOrOptions === \"string\" ? pathOrOptions : undefined;\n\tconst options: any =\n\t\ttypeof handlerOrOptions === \"object\" ? handlerOrOptions : pathOrOptions;\n\tconst handler: any =\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\n\tconst runtimeOptions: EndpointRuntimeOptions = {\n\t\tmethod: options.method,\n\t\tbody: options.body,\n\t\tquery: options.query,\n\t\terror: options.error,\n\t\trequireHeaders: options.requireHeaders,\n\t\trequireRequest: options.requireRequest,\n\t\tcloneRequest: options.cloneRequest,\n\t\tdisableBody: options.disableBody,\n\t\tmetadata: options.metadata,\n\t\tuse: options.use,\n\t\tonAPIError: options.onAPIError,\n\t\tonValidationError: options.onValidationError,\n\t};\n\n\tconst internalHandler = async (...inputCtx: any[]): Promise<any> => {\n\t\tconst context = (inputCtx[0] || {}) as Record<string, any>;\n\t\tconst { data: internalContext, error: validationError } = await tryCatch(\n\t\t\tcreateInternalContext(context, {\n\t\t\t\toptions: runtimeOptions,\n\t\t\t\tpath,\n\t\t\t}),\n\t\t);\n\n\t\tif (validationError) {\n\t\t\tif (!(validationError instanceof ValidationError)) throw validationError;\n\n\t\t\tif (options.onValidationError) {\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\n\t\tconst response = await handler(internalContext as any).catch(\n\t\t\tasync (e: any) => {\n\t\t\t\tif (isAPIError(e)) {\n\t\t\t\t\tconst onAPIError = options.onAPIError;\n\t\t\t\t\tif (onAPIError) {\n\t\t\t\t\t\tawait onAPIError(e);\n\t\t\t\t\t}\n\t\t\t\t\tif (context.asResponse) {\n\t\t\t\t\t\treturn e;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tthrow e;\n\t\t\t},\n\t\t);\n\n\t\tconst responseHeaders = internalContext.responseHeaders;\n\t\tconst status = internalContext.responseStatus;\n\n\t\treturn context.asResponse\n\t\t\t? toResponse(response, {\n\t\t\t\t\theaders: responseHeaders,\n\t\t\t\t\tstatus,\n\t\t\t\t})\n\t\t\t: context.returnHeaders\n\t\t\t\t? context.returnStatus\n\t\t\t\t\t? {\n\t\t\t\t\t\t\theaders: responseHeaders,\n\t\t\t\t\t\t\tresponse,\n\t\t\t\t\t\t\tstatus,\n\t\t\t\t\t\t}\n\t\t\t\t\t: {\n\t\t\t\t\t\t\theaders: responseHeaders,\n\t\t\t\t\t\t\tresponse,\n\t\t\t\t\t\t}\n\t\t\t\t: context.returnStatus\n\t\t\t\t\t? { response, status }\n\t\t\t\t\t: response;\n\t};\n\n\tinternalHandler.options = runtimeOptions;\n\tinternalHandler.path = path;\n\treturn internalHandler as any;\n}\n\ncreateEndpoint.create = <E extends { use?: Middleware[] }>(opts?: E) => {\n\treturn <\n\t\tPath extends string,\n\t\tMethod extends HTTPMethod | HTTPMethod[] | \"*\",\n\t\tBodySchema extends object | undefined = undefined,\n\t\tQuerySchema extends object | undefined = undefined,\n\t\tUse extends Middleware[] = [],\n\t\tReqHeaders extends boolean = false,\n\t\tReqRequest extends boolean = false,\n\t\tR = unknown,\n\t\tMeta extends EndpointMetadata | undefined = undefined,\n\t\tErrorSchema extends StandardSchemaV1 | undefined = undefined,\n\t>(\n\t\tpath: Path,\n\t\toptions: { method: Method } & BodyOption<Method, BodySchema> & {\n\t\t\t\tquery?: QuerySchema;\n\t\t\t\tuse?: [...Use];\n\t\t\t\trequireHeaders?: ReqHeaders;\n\t\t\t\trequireRequest?: ReqRequest;\n\t\t\t\terror?: ErrorSchema;\n\t\t\t\tcloneRequest?: boolean;\n\t\t\t\tdisableBody?: boolean;\n\t\t\t\tmetadata?: Meta;\n\t\t\t\tonAPIError?: (e: APIError) => void | Promise<void>;\n\t\t\t\tonValidationError?: (info: {\n\t\t\t\t\tmessage: string;\n\t\t\t\t\tissues: readonly StandardSchemaV1.Issue[];\n\t\t\t\t}) => void | Promise<void>;\n\t\t\t\t[key: string]: any;\n\t\t\t},\n\t\thandler: (\n\t\t\tctx: EndpointContext<\n\t\t\t\tPath,\n\t\t\t\tMethod,\n\t\t\t\tBodySchema,\n\t\t\t\tQuerySchema,\n\t\t\t\tUse,\n\t\t\t\tReqHeaders,\n\t\t\t\tReqRequest,\n\t\t\t\tInferUse<E[\"use\"]>,\n\t\t\t\tMeta\n\t\t\t>,\n\t\t) => Promise<R>,\n\t): Endpoint<\n\t\tPath,\n\t\tMethod,\n\t\tResolveBodyInput<BodySchema, Meta>,\n\t\tResolveQueryInput<QuerySchema, Meta>,\n\t\tUse,\n\t\tAwaited<R>,\n\t\tResolveMetaInput<Meta>,\n\t\tResolveErrorInput<ErrorSchema, Meta>\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} as any,\n\t\t\thandler as any,\n\t\t) as any;\n\t};\n};\n"],"mappings":";;;;;;AAiXA,SAAgB,eACf,eACA,kBACA,gBACM;CACN,MAAM,OACL,OAAO,kBAAkB,WAAW,gBAAgB;CACrD,MAAM,UACL,OAAO,qBAAqB,WAAW,mBAAmB;CAC3D,MAAM,UACL,OAAO,qBAAqB,aAAa,mBAAmB;AAE7D,MAAK,QAAQ,WAAW,SA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GACD,QAAQ,eACP;GAAE;GAAU;GAAQ,GACpB;;AAGN,iBAAgB,UAAU;AAC1B,iBAAgB,OAAO;AACvB,QAAO;;AAGR,eAAe,UAA4C,SAAa;AACvE,SAYC,MACA,SAgBA,YAsBI;AACJ,SAAO,eACN,MACA;GACC,GAAG;GACH,KAAK,CAAC,GAAI,SAAS,OAAO,EAAE,EAAG,GAAI,MAAM,OAAO,EAAE,CAAE;GACpD,EACD,QACA"}
@@ -1 +1 @@
1
- {"version":3,"file":"openapi.cjs","names":["ZodObject","ZodOptional"],"sources":["../src/openapi.ts"],"sourcesContent":["import { ZodObject, ZodOptional, type ZodType } from \"zod\";\nimport type { Endpoint, EndpointRuntimeOptions } from \"./endpoint\";\n\nexport type OpenAPISchemaType =\n\t| \"string\"\n\t| \"number\"\n\t| \"integer\"\n\t| \"boolean\"\n\t| \"array\"\n\t| \"object\";\n\nexport interface OpenAPIParameter {\n\tin: \"query\" | \"path\" | \"header\" | \"cookie\";\n\tname?: string;\n\tdescription?: string;\n\trequired?: boolean;\n\tschema?: {\n\t\ttype: OpenAPISchemaType;\n\t\tformat?: string;\n\t\titems?: {\n\t\t\ttype: OpenAPISchemaType;\n\t\t};\n\t\tenum?: string[];\n\t\tminLength?: number;\n\t\tdescription?: string;\n\t\tdefault?: string;\n\t\texample?: string;\n\t};\n}\n\ninterface Path {\n\tget?: {\n\t\ttags?: string[];\n\t\toperationId?: string;\n\t\tdescription?: string;\n\t\tsecurity?: [{ bearerAuth: string[] }];\n\t\tparameters?: OpenAPIParameter[];\n\t\tresponses?: {\n\t\t\t[key in string]: {\n\t\t\t\tdescription?: string;\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};\n\t};\n\tpost?: {\n\t\ttags?: string[];\n\t\toperationId?: string;\n\t\tdescription?: string;\n\t\tsecurity?: [{ bearerAuth: string[] }];\n\t\tparameters?: OpenAPIParameter[];\n\t\trequestBody?: {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype?: OpenAPISchemaType;\n\t\t\t\t\t\tproperties?: Record<string, any>;\n\t\t\t\t\t\trequired?: string[];\n\t\t\t\t\t\t$ref?: string;\n\t\t\t\t\t};\n\t\t\t\t};\n\t\t\t};\n\t\t};\n\t\tresponses?: {\n\t\t\t[key in string]: {\n\t\t\t\tdescription?: string;\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};\n\t};\n}\nconst paths: Record<string, Path> = {};\n\nfunction getTypeFromZodType(zodType: ZodType<any>) {\n\tswitch (zodType.constructor.name) {\n\t\tcase \"ZodString\":\n\t\t\treturn \"string\";\n\t\tcase \"ZodNumber\":\n\t\t\treturn \"number\";\n\t\tcase \"ZodBoolean\":\n\t\t\treturn \"boolean\";\n\t\tcase \"ZodObject\":\n\t\t\treturn \"object\";\n\t\tcase \"ZodArray\":\n\t\t\treturn \"array\";\n\t\tdefault:\n\t\t\treturn \"string\";\n\t}\n}\n\nfunction getParameters(options: EndpointRuntimeOptions) {\n\tconst parameters: OpenAPIParameter[] = [];\n\tif (options.metadata?.openapi?.parameters) {\n\t\tparameters.push(...options.metadata.openapi.parameters);\n\t\treturn parameters;\n\t}\n\tif (options.query instanceof ZodObject) {\n\t\tObject.entries(options.query.shape).forEach(([key, value]) => {\n\t\t\tif (value instanceof ZodObject) {\n\t\t\t\tparameters.push({\n\t\t\t\t\tname: key,\n\t\t\t\t\tin: \"query\",\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: getTypeFromZodType(value),\n\t\t\t\t\t\t...(\"minLength\" in value && value.minLength\n\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\tminLength: value.minLength as number,\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t: {}),\n\t\t\t\t\t\tdescription: value.description,\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t}\n\t\t});\n\t}\n\treturn parameters;\n}\n\nfunction getRequestBody(options: EndpointRuntimeOptions): any {\n\tif (options.metadata?.openapi?.requestBody) {\n\t\treturn options.metadata.openapi.requestBody;\n\t}\n\tif (!options.body) return undefined;\n\tif (\n\t\toptions.body instanceof ZodObject ||\n\t\toptions.body instanceof ZodOptional\n\t) {\n\t\t// @ts-expect-error\n\t\tconst shape = options.body.shape;\n\t\tif (!shape) return undefined;\n\t\tconst properties: Record<string, any> = {};\n\t\tconst required: string[] = [];\n\t\tObject.entries(shape).forEach(([key, value]) => {\n\t\t\tif (value instanceof ZodObject) {\n\t\t\t\tproperties[key] = {\n\t\t\t\t\ttype: getTypeFromZodType(value),\n\t\t\t\t\tdescription: value.description,\n\t\t\t\t};\n\t\t\t\tif (!(value instanceof ZodOptional)) {\n\t\t\t\t\trequired.push(key);\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\t\treturn {\n\t\t\trequired:\n\t\t\t\toptions.body instanceof ZodOptional\n\t\t\t\t\t? false\n\t\t\t\t\t: options.body\n\t\t\t\t\t\t? true\n\t\t\t\t\t\t: false,\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties,\n\t\t\t\t\t\trequired,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t};\n\t}\n\treturn undefined;\n}\n\nfunction getResponse(responses?: Record<string, any>) {\n\treturn {\n\t\t\"400\": {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tmessage: {\n\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\trequired: [\"message\"],\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t\tdescription:\n\t\t\t\t\"Bad Request. Usually due to missing parameters, or invalid parameters.\",\n\t\t},\n\t\t\"401\": {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tmessage: {\n\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\trequired: [\"message\"],\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t\tdescription: \"Unauthorized. Due to missing or invalid authentication.\",\n\t\t},\n\t\t\"403\": {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tmessage: {\n\t\t\t\t\t\t\t\ttype: \"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\tdescription:\n\t\t\t\t\"Forbidden. You do not have permission to access this resource or to perform this action.\",\n\t\t},\n\t\t\"404\": {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tmessage: {\n\t\t\t\t\t\t\t\ttype: \"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\tdescription: \"Not Found. The requested resource was not found.\",\n\t\t},\n\t\t\"429\": {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tmessage: {\n\t\t\t\t\t\t\t\ttype: \"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\tdescription:\n\t\t\t\t\"Too Many Requests. You have exceeded the rate limit. Try again later.\",\n\t\t},\n\t\t\"500\": {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tmessage: {\n\t\t\t\t\t\t\t\ttype: \"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\tdescription:\n\t\t\t\t\"Internal Server Error. This is a problem with the server that you cannot fix.\",\n\t\t},\n\t\t...responses,\n\t} as any;\n}\n\nexport async function generator(\n\tendpoints: Record<string, Endpoint>,\n\tconfig?: {\n\t\turl: string;\n\t},\n) {\n\tconst components = {\n\t\tschemas: {},\n\t};\n\n\tObject.entries(endpoints).forEach(([_, value]) => {\n\t\tconst options = value.options as EndpointRuntimeOptions;\n\t\tif (!value.path || options.metadata?.SERVER_ONLY) return;\n\t\tif (options.method === \"GET\") {\n\t\t\tpaths[value.path] = {\n\t\t\t\tget: {\n\t\t\t\t\ttags: [\"Default\", ...(options.metadata?.openapi?.tags || [])],\n\t\t\t\t\tdescription: options.metadata?.openapi?.description,\n\t\t\t\t\toperationId: options.metadata?.openapi?.operationId,\n\t\t\t\t\tsecurity: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tbearerAuth: [],\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\tparameters: getParameters(options),\n\t\t\t\t\tresponses: getResponse(options.metadata?.openapi?.responses),\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\n\t\tif (options.method === \"POST\") {\n\t\t\tconst body = getRequestBody(options);\n\t\t\tpaths[value.path] = {\n\t\t\t\tpost: {\n\t\t\t\t\ttags: [\"Default\", ...(options.metadata?.openapi?.tags || [])],\n\t\t\t\t\tdescription: options.metadata?.openapi?.description,\n\t\t\t\t\toperationId: options.metadata?.openapi?.operationId,\n\t\t\t\t\tsecurity: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tbearerAuth: [],\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\tparameters: getParameters(options),\n\t\t\t\t\t...(body\n\t\t\t\t\t\t? { requestBody: body }\n\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\trequestBody: {\n\t\t\t\t\t\t\t\t\t//set body none\n\t\t\t\t\t\t\t\t\tcontent: {\n\t\t\t\t\t\t\t\t\t\t\"application/json\": {\n\t\t\t\t\t\t\t\t\t\t\tschema: {\n\t\t\t\t\t\t\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\t\t\t\t\t\t\tproperties: {},\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t}),\n\t\t\t\t\tresponses: getResponse(options.metadata?.openapi?.responses),\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\t});\n\n\tconst res = {\n\t\topenapi: \"3.1.1\",\n\t\tinfo: {\n\t\t\ttitle: \"Better Auth\",\n\t\t\tdescription: \"API Reference for your Better Auth Instance\",\n\t\t\tversion: \"1.1.0\",\n\t\t},\n\t\tcomponents,\n\t\tsecurity: [\n\t\t\t{\n\t\t\t\tapiKeyCookie: [],\n\t\t\t},\n\t\t],\n\t\tservers: [\n\t\t\t{\n\t\t\t\turl: config?.url,\n\t\t\t},\n\t\t],\n\t\ttags: [\n\t\t\t{\n\t\t\t\tname: \"Default\",\n\t\t\t\tdescription:\n\t\t\t\t\t\"Default endpoints that are included with Better Auth by default. These endpoints are not part of any plugin.\",\n\t\t\t},\n\t\t],\n\t\tpaths,\n\t};\n\treturn res;\n}\n\nexport const getHTML = (\n\tapiReference: Record<string, any>,\n\tconfig?: {\n\t\tlogo?: string;\n\t\ttheme?: string;\n\t\ttitle?: string;\n\t\tdescription?: string;\n\t},\n) => `<!doctype html>\n<html>\n <head>\n <title>Scalar API Reference</title>\n <meta charset=\"utf-8\" />\n <meta\n name=\"viewport\"\n content=\"width=device-width, initial-scale=1\" />\n </head>\n <body>\n <script\n id=\"api-reference\"\n type=\"application/json\">\n ${JSON.stringify(apiReference)}\n </script>\n\t <script>\n var configuration = {\n\t \tfavicon: ${config?.logo ? `data:image/svg+xml;utf8,${encodeURIComponent(config.logo)}` : undefined} ,\n\t \ttheme: ${config?.theme || \"saturn\"},\n metaData: {\n\t\t\ttitle: ${config?.title || \"Open API Reference\"},\n\t\t\tdescription: ${config?.description || \"Better Call Open API\"},\n\t\t}\n }\n document.getElementById('api-reference').dataset.configuration =\n JSON.stringify(configuration)\n </script>\n\t <script src=\"https://cdn.jsdelivr.net/npm/@scalar/api-reference\"></script>\n </body>\n</html>`;\n"],"mappings":";;;;AAwFA,MAAM,QAA8B,EAAE;AAEtC,SAAS,mBAAmB,SAAuB;AAClD,SAAQ,QAAQ,YAAY,MAA5B;EACC,KAAK,YACJ,QAAO;EACR,KAAK,YACJ,QAAO;EACR,KAAK,aACJ,QAAO;EACR,KAAK,YACJ,QAAO;EACR,KAAK,WACJ,QAAO;EACR,QACC,QAAO;;;AAIV,SAAS,cAAc,SAAiC;CACvD,MAAM,aAAiC,EAAE;AACzC,KAAI,QAAQ,UAAU,SAAS,YAAY;AAC1C,aAAW,KAAK,GAAG,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AC9B,MAAM,OAAO,eAAe,QAAQ;AACpC,SAAM,MAAM,QAAQ,EACnB,MAAM;IACL,MAAM,CAAC,WAAW,GAAI,QAAQ,UAAU,SAAS,QAAQ,EAAE,CAAE;IAC7D,aAAa,QAAQ,UAAU,SAAS;IACxC,aAAa,QAAQ,UAAU,SAAS;IACxC,UAAU,CACT,EACC,YAAY,EAAE,EACd,CACD;IACD,YAAY,cAAc,QAAQ;IAClC,GAAI,OACD,EAAE,aAAa,MAAM,GACrB,EACA,aAAa,EAEZ,SAAS,EACR,oBAAoB,EACnB,QAAQ;KACP,MAAM;KACN,YAAY,EAAE;KACd,EACD,EACD,EACD,EACD;IACH,WAAW,YAAY,QAAQ,UAAU,SAAS,UAAU;IAC5D,EACD;;GAED;AA6BF,QA3BY;EACX,SAAS;EACT,MAAM;GACL,OAAO;GACP,aAAa;GACb,SAAS;GACT;EACD;EACA,UAAU,CACT,EACC,cAAc,EAAE,EAChB,CACD;EACD,SAAS,CACR,EACC,KAAK,QAAQ,KACb,CACD;EACD,MAAM,CACL;GACC,MAAM;GACN,aACC;GACD,CACD;EACD;EACA;;AAIF,MAAa,WACZ,cACA,WAMI;;;;;;;;;;;;;MAaC,KAAK,UAAU,aAAa,CAAC;;;;eAIpB,QAAQ,OAAO,2BAA2B,mBAAmB,OAAO,KAAK,KAAK,OAAU;cACzF,QAAQ,SAAS,SAAS;;YAE5B,QAAQ,SAAS,qBAAqB;kBAChC,QAAQ,eAAe,uBAAuB"}
1
+ {"version":3,"file":"openapi.cjs","names":["ZodObject","ZodOptional"],"sources":["../src/openapi.ts"],"sourcesContent":["import { ZodObject, ZodOptional, type ZodType } from \"zod\";\nimport type { Endpoint, EndpointRuntimeOptions } from \"./endpoint\";\n\nexport type OpenAPISchemaType =\n\t| \"string\"\n\t| \"number\"\n\t| \"integer\"\n\t| \"boolean\"\n\t| \"array\"\n\t| \"object\";\n\nexport interface OpenAPIParameter {\n\tin: \"query\" | \"path\" | \"header\" | \"cookie\";\n\tname?: string;\n\tdescription?: string;\n\trequired?: boolean;\n\tschema?: {\n\t\ttype: OpenAPISchemaType;\n\t\tformat?: string | undefined;\n\t\titems?: {\n\t\t\ttype: OpenAPISchemaType;\n\t\t};\n\t\tenum?: string[];\n\t\tminLength?: number;\n\t\tdescription?: string | undefined;\n\t\tdefault?: string | undefined;\n\t\texample?: string | undefined;\n\t};\n}\n\ninterface Path {\n\tget?: {\n\t\ttags?: string[];\n\t\toperationId?: string;\n\t\tdescription?: string;\n\t\tsecurity?: [{ bearerAuth: string[] }];\n\t\tparameters?: OpenAPIParameter[];\n\t\tresponses?: {\n\t\t\t[key in string]: {\n\t\t\t\tdescription?: string;\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};\n\t};\n\tpost?: {\n\t\ttags?: string[];\n\t\toperationId?: string;\n\t\tdescription?: string;\n\t\tsecurity?: [{ bearerAuth: string[] }];\n\t\tparameters?: OpenAPIParameter[];\n\t\trequestBody?: {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype?: OpenAPISchemaType;\n\t\t\t\t\t\tproperties?: Record<string, any>;\n\t\t\t\t\t\trequired?: string[];\n\t\t\t\t\t\t$ref?: string;\n\t\t\t\t\t};\n\t\t\t\t};\n\t\t\t};\n\t\t};\n\t\tresponses?: {\n\t\t\t[key in string]: {\n\t\t\t\tdescription?: string;\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};\n\t};\n}\nconst paths: Record<string, Path> = {};\n\nfunction getTypeFromZodType(zodType: ZodType<any>) {\n\tswitch (zodType.constructor.name) {\n\t\tcase \"ZodString\":\n\t\t\treturn \"string\";\n\t\tcase \"ZodNumber\":\n\t\t\treturn \"number\";\n\t\tcase \"ZodBoolean\":\n\t\t\treturn \"boolean\";\n\t\tcase \"ZodObject\":\n\t\t\treturn \"object\";\n\t\tcase \"ZodArray\":\n\t\t\treturn \"array\";\n\t\tdefault:\n\t\t\treturn \"string\";\n\t}\n}\n\nfunction getParameters(options: EndpointRuntimeOptions) {\n\tconst parameters: OpenAPIParameter[] = [];\n\tif (options.metadata?.openapi?.parameters) {\n\t\tparameters.push(...options.metadata.openapi.parameters);\n\t\treturn parameters;\n\t}\n\tif (options.query instanceof ZodObject) {\n\t\tObject.entries(options.query.shape).forEach(([key, value]) => {\n\t\t\tif (value instanceof ZodObject) {\n\t\t\t\tparameters.push({\n\t\t\t\t\tname: key,\n\t\t\t\t\tin: \"query\",\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: getTypeFromZodType(value),\n\t\t\t\t\t\t...(\"minLength\" in value && value.minLength\n\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\tminLength: value.minLength as number,\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t: {}),\n\t\t\t\t\t\tdescription: value.description,\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t}\n\t\t});\n\t}\n\treturn parameters;\n}\n\nfunction getRequestBody(options: EndpointRuntimeOptions): any {\n\tif (options.metadata?.openapi?.requestBody) {\n\t\treturn options.metadata.openapi.requestBody;\n\t}\n\tif (!options.body) return undefined;\n\tif (\n\t\toptions.body instanceof ZodObject ||\n\t\toptions.body instanceof ZodOptional\n\t) {\n\t\t// @ts-expect-error\n\t\tconst shape = options.body.shape;\n\t\tif (!shape) return undefined;\n\t\tconst properties: Record<string, any> = {};\n\t\tconst required: string[] = [];\n\t\tObject.entries(shape).forEach(([key, value]) => {\n\t\t\tif (value instanceof ZodObject) {\n\t\t\t\tproperties[key] = {\n\t\t\t\t\ttype: getTypeFromZodType(value),\n\t\t\t\t\tdescription: value.description,\n\t\t\t\t};\n\t\t\t\tif (!(value instanceof ZodOptional)) {\n\t\t\t\t\trequired.push(key);\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\t\treturn {\n\t\t\trequired:\n\t\t\t\toptions.body instanceof ZodOptional\n\t\t\t\t\t? false\n\t\t\t\t\t: options.body\n\t\t\t\t\t\t? true\n\t\t\t\t\t\t: false,\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties,\n\t\t\t\t\t\trequired,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t};\n\t}\n\treturn undefined;\n}\n\nfunction getResponse(responses?: Record<string, any>) {\n\treturn {\n\t\t\"400\": {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tmessage: {\n\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\trequired: [\"message\"],\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t\tdescription:\n\t\t\t\t\"Bad Request. Usually due to missing parameters, or invalid parameters.\",\n\t\t},\n\t\t\"401\": {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tmessage: {\n\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\trequired: [\"message\"],\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t\tdescription: \"Unauthorized. Due to missing or invalid authentication.\",\n\t\t},\n\t\t\"403\": {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tmessage: {\n\t\t\t\t\t\t\t\ttype: \"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\tdescription:\n\t\t\t\t\"Forbidden. You do not have permission to access this resource or to perform this action.\",\n\t\t},\n\t\t\"404\": {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tmessage: {\n\t\t\t\t\t\t\t\ttype: \"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\tdescription: \"Not Found. The requested resource was not found.\",\n\t\t},\n\t\t\"429\": {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tmessage: {\n\t\t\t\t\t\t\t\ttype: \"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\tdescription:\n\t\t\t\t\"Too Many Requests. You have exceeded the rate limit. Try again later.\",\n\t\t},\n\t\t\"500\": {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tmessage: {\n\t\t\t\t\t\t\t\ttype: \"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\tdescription:\n\t\t\t\t\"Internal Server Error. This is a problem with the server that you cannot fix.\",\n\t\t},\n\t\t...responses,\n\t} as any;\n}\n\nexport async function generator(\n\tendpoints: Record<string, Endpoint>,\n\tconfig?: {\n\t\turl: string;\n\t},\n) {\n\tconst components = {\n\t\tschemas: {},\n\t};\n\n\tObject.entries(endpoints).forEach(([_, value]) => {\n\t\tconst options = value.options as EndpointRuntimeOptions;\n\t\tif (!value.path || options.metadata?.SERVER_ONLY) return;\n\t\tif (options.method === \"GET\") {\n\t\t\tpaths[value.path] = {\n\t\t\t\tget: {\n\t\t\t\t\ttags: [\"Default\", ...(options.metadata?.openapi?.tags || [])],\n\t\t\t\t\tdescription: options.metadata?.openapi?.description,\n\t\t\t\t\toperationId: options.metadata?.openapi?.operationId,\n\t\t\t\t\tsecurity: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tbearerAuth: [],\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\tparameters: getParameters(options),\n\t\t\t\t\tresponses: getResponse(options.metadata?.openapi?.responses),\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\n\t\tif (options.method === \"POST\") {\n\t\t\tconst body = getRequestBody(options);\n\t\t\tpaths[value.path] = {\n\t\t\t\tpost: {\n\t\t\t\t\ttags: [\"Default\", ...(options.metadata?.openapi?.tags || [])],\n\t\t\t\t\tdescription: options.metadata?.openapi?.description,\n\t\t\t\t\toperationId: options.metadata?.openapi?.operationId,\n\t\t\t\t\tsecurity: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tbearerAuth: [],\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\tparameters: getParameters(options),\n\t\t\t\t\t...(body\n\t\t\t\t\t\t? { requestBody: body }\n\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\trequestBody: {\n\t\t\t\t\t\t\t\t\t//set body none\n\t\t\t\t\t\t\t\t\tcontent: {\n\t\t\t\t\t\t\t\t\t\t\"application/json\": {\n\t\t\t\t\t\t\t\t\t\t\tschema: {\n\t\t\t\t\t\t\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\t\t\t\t\t\t\tproperties: {},\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t}),\n\t\t\t\t\tresponses: getResponse(options.metadata?.openapi?.responses),\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\t});\n\n\tconst res = {\n\t\topenapi: \"3.1.1\",\n\t\tinfo: {\n\t\t\ttitle: \"Better Auth\",\n\t\t\tdescription: \"API Reference for your Better Auth Instance\",\n\t\t\tversion: \"1.1.0\",\n\t\t},\n\t\tcomponents,\n\t\tsecurity: [\n\t\t\t{\n\t\t\t\tapiKeyCookie: [],\n\t\t\t},\n\t\t],\n\t\tservers: [\n\t\t\t{\n\t\t\t\turl: config?.url,\n\t\t\t},\n\t\t],\n\t\ttags: [\n\t\t\t{\n\t\t\t\tname: \"Default\",\n\t\t\t\tdescription:\n\t\t\t\t\t\"Default endpoints that are included with Better Auth by default. These endpoints are not part of any plugin.\",\n\t\t\t},\n\t\t],\n\t\tpaths,\n\t};\n\treturn res;\n}\n\nexport const getHTML = (\n\tapiReference: Record<string, any>,\n\tconfig?: {\n\t\tlogo?: string;\n\t\ttheme?: string;\n\t\ttitle?: string;\n\t\tdescription?: string;\n\t},\n) => `<!doctype html>\n<html>\n <head>\n <title>Scalar API Reference</title>\n <meta charset=\"utf-8\" />\n <meta\n name=\"viewport\"\n content=\"width=device-width, initial-scale=1\" />\n </head>\n <body>\n <script\n id=\"api-reference\"\n type=\"application/json\">\n ${JSON.stringify(apiReference)}\n </script>\n\t <script>\n var configuration = {\n\t \tfavicon: ${config?.logo ? `data:image/svg+xml;utf8,${encodeURIComponent(config.logo)}` : undefined} ,\n\t \ttheme: ${config?.theme || \"saturn\"},\n metaData: {\n\t\t\ttitle: ${config?.title || \"Open API Reference\"},\n\t\t\tdescription: ${config?.description || \"Better Call Open API\"},\n\t\t}\n }\n document.getElementById('api-reference').dataset.configuration =\n JSON.stringify(configuration)\n </script>\n\t <script src=\"https://cdn.jsdelivr.net/npm/@scalar/api-reference\"></script>\n </body>\n</html>`;\n"],"mappings":";;;;AAwFA,MAAM,QAA8B,EAAE;AAEtC,SAAS,mBAAmB,SAAuB;AAClD,SAAQ,QAAQ,YAAY,MAA5B;EACC,KAAK,YACJ,QAAO;EACR,KAAK,YACJ,QAAO;EACR,KAAK,aACJ,QAAO;EACR,KAAK,YACJ,QAAO;EACR,KAAK,WACJ,QAAO;EACR,QACC,QAAO;;;AAIV,SAAS,cAAc,SAAiC;CACvD,MAAM,aAAiC,EAAE;AACzC,KAAI,QAAQ,UAAU,SAAS,YAAY;AAC1C,aAAW,KAAK,GAAG,QAAQ,SAAS,QAAQ,WAAW;AACvD,SAAO;;AAER,KAAI,QAAQ,iBAAiBA,cAC5B,QAAO,QAAQ,QAAQ,MAAM,MAAM,CAAC,SAAS,CAAC,KAAK,WAAW;AAC7D,MAAI,iBAAiBA,cACpB,YAAW,KAAK;GACf,MAAM;GACN,IAAI;GACJ,QAAQ;IACP,MAAM,mBAAmB,MAAM;IAC/B,GAAI,eAAe,SAAS,MAAM,YAC/B,EACA,WAAW,MAAM,WACjB,GACA,EAAE;IACL,aAAa,MAAM;IACnB;GACD,CAAC;GAEF;AAEH,QAAO;;AAGR,SAAS,eAAe,SAAsC;AAC7D,KAAI,QAAQ,UAAU,SAAS,YAC9B,QAAO,QAAQ,SAAS,QAAQ;AAEjC,KAAI,CAAC,QAAQ,KAAM,QAAO;AAC1B,KACC,QAAQ,gBAAgBA,iBACxB,QAAQ,gBAAgBC,iBACvB;EAED,MAAM,QAAQ,QAAQ,KAAK;AAC3B,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EACD,SAAS,CACR,EACC,KAAK,QAAQ,KACb,CACD;EACD,MAAM,CACL;GACC,MAAM;GACN,aACC;GACD,CACD;EACD;EACA;;AAIF,MAAa,WACZ,cACA,WAMI;;;;;;;;;;;;;MAaC,KAAK,UAAU,aAAa,CAAC;;;;eAIpB,QAAQ,OAAO,2BAA2B,mBAAmB,OAAO,KAAK,KAAK,OAAU;cACzF,QAAQ,SAAS,SAAS;;YAE5B,QAAQ,SAAS,qBAAqB;kBAChC,QAAQ,eAAe,uBAAuB"}
@@ -9,15 +9,15 @@ interface OpenAPIParameter {
9
9
  required?: boolean;
10
10
  schema?: {
11
11
  type: OpenAPISchemaType;
12
- format?: string;
12
+ format?: string | undefined;
13
13
  items?: {
14
14
  type: OpenAPISchemaType;
15
15
  };
16
16
  enum?: string[];
17
17
  minLength?: number;
18
- description?: string;
19
- default?: string;
20
- example?: string;
18
+ description?: string | undefined;
19
+ default?: string | undefined;
20
+ example?: string | undefined;
21
21
  };
22
22
  }
23
23
  interface Path {
@@ -9,15 +9,15 @@ interface OpenAPIParameter {
9
9
  required?: boolean;
10
10
  schema?: {
11
11
  type: OpenAPISchemaType;
12
- format?: string;
12
+ format?: string | undefined;
13
13
  items?: {
14
14
  type: OpenAPISchemaType;
15
15
  };
16
16
  enum?: string[];
17
17
  minLength?: number;
18
- description?: string;
19
- default?: string;
20
- example?: string;
18
+ description?: string | undefined;
19
+ default?: string | undefined;
20
+ example?: string | undefined;
21
21
  };
22
22
  }
23
23
  interface Path {
@@ -1 +1 @@
1
- {"version":3,"file":"openapi.mjs","names":[],"sources":["../src/openapi.ts"],"sourcesContent":["import { ZodObject, ZodOptional, type ZodType } from \"zod\";\nimport type { Endpoint, EndpointRuntimeOptions } from \"./endpoint\";\n\nexport type OpenAPISchemaType =\n\t| \"string\"\n\t| \"number\"\n\t| \"integer\"\n\t| \"boolean\"\n\t| \"array\"\n\t| \"object\";\n\nexport interface OpenAPIParameter {\n\tin: \"query\" | \"path\" | \"header\" | \"cookie\";\n\tname?: string;\n\tdescription?: string;\n\trequired?: boolean;\n\tschema?: {\n\t\ttype: OpenAPISchemaType;\n\t\tformat?: string;\n\t\titems?: {\n\t\t\ttype: OpenAPISchemaType;\n\t\t};\n\t\tenum?: string[];\n\t\tminLength?: number;\n\t\tdescription?: string;\n\t\tdefault?: string;\n\t\texample?: string;\n\t};\n}\n\ninterface Path {\n\tget?: {\n\t\ttags?: string[];\n\t\toperationId?: string;\n\t\tdescription?: string;\n\t\tsecurity?: [{ bearerAuth: string[] }];\n\t\tparameters?: OpenAPIParameter[];\n\t\tresponses?: {\n\t\t\t[key in string]: {\n\t\t\t\tdescription?: string;\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};\n\t};\n\tpost?: {\n\t\ttags?: string[];\n\t\toperationId?: string;\n\t\tdescription?: string;\n\t\tsecurity?: [{ bearerAuth: string[] }];\n\t\tparameters?: OpenAPIParameter[];\n\t\trequestBody?: {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype?: OpenAPISchemaType;\n\t\t\t\t\t\tproperties?: Record<string, any>;\n\t\t\t\t\t\trequired?: string[];\n\t\t\t\t\t\t$ref?: string;\n\t\t\t\t\t};\n\t\t\t\t};\n\t\t\t};\n\t\t};\n\t\tresponses?: {\n\t\t\t[key in string]: {\n\t\t\t\tdescription?: string;\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};\n\t};\n}\nconst paths: Record<string, Path> = {};\n\nfunction getTypeFromZodType(zodType: ZodType<any>) {\n\tswitch (zodType.constructor.name) {\n\t\tcase \"ZodString\":\n\t\t\treturn \"string\";\n\t\tcase \"ZodNumber\":\n\t\t\treturn \"number\";\n\t\tcase \"ZodBoolean\":\n\t\t\treturn \"boolean\";\n\t\tcase \"ZodObject\":\n\t\t\treturn \"object\";\n\t\tcase \"ZodArray\":\n\t\t\treturn \"array\";\n\t\tdefault:\n\t\t\treturn \"string\";\n\t}\n}\n\nfunction getParameters(options: EndpointRuntimeOptions) {\n\tconst parameters: OpenAPIParameter[] = [];\n\tif (options.metadata?.openapi?.parameters) {\n\t\tparameters.push(...options.metadata.openapi.parameters);\n\t\treturn parameters;\n\t}\n\tif (options.query instanceof ZodObject) {\n\t\tObject.entries(options.query.shape).forEach(([key, value]) => {\n\t\t\tif (value instanceof ZodObject) {\n\t\t\t\tparameters.push({\n\t\t\t\t\tname: key,\n\t\t\t\t\tin: \"query\",\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: getTypeFromZodType(value),\n\t\t\t\t\t\t...(\"minLength\" in value && value.minLength\n\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\tminLength: value.minLength as number,\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t: {}),\n\t\t\t\t\t\tdescription: value.description,\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t}\n\t\t});\n\t}\n\treturn parameters;\n}\n\nfunction getRequestBody(options: EndpointRuntimeOptions): any {\n\tif (options.metadata?.openapi?.requestBody) {\n\t\treturn options.metadata.openapi.requestBody;\n\t}\n\tif (!options.body) return undefined;\n\tif (\n\t\toptions.body instanceof ZodObject ||\n\t\toptions.body instanceof ZodOptional\n\t) {\n\t\t// @ts-expect-error\n\t\tconst shape = options.body.shape;\n\t\tif (!shape) return undefined;\n\t\tconst properties: Record<string, any> = {};\n\t\tconst required: string[] = [];\n\t\tObject.entries(shape).forEach(([key, value]) => {\n\t\t\tif (value instanceof ZodObject) {\n\t\t\t\tproperties[key] = {\n\t\t\t\t\ttype: getTypeFromZodType(value),\n\t\t\t\t\tdescription: value.description,\n\t\t\t\t};\n\t\t\t\tif (!(value instanceof ZodOptional)) {\n\t\t\t\t\trequired.push(key);\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\t\treturn {\n\t\t\trequired:\n\t\t\t\toptions.body instanceof ZodOptional\n\t\t\t\t\t? false\n\t\t\t\t\t: options.body\n\t\t\t\t\t\t? true\n\t\t\t\t\t\t: false,\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties,\n\t\t\t\t\t\trequired,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t};\n\t}\n\treturn undefined;\n}\n\nfunction getResponse(responses?: Record<string, any>) {\n\treturn {\n\t\t\"400\": {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tmessage: {\n\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\trequired: [\"message\"],\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t\tdescription:\n\t\t\t\t\"Bad Request. Usually due to missing parameters, or invalid parameters.\",\n\t\t},\n\t\t\"401\": {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tmessage: {\n\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\trequired: [\"message\"],\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t\tdescription: \"Unauthorized. Due to missing or invalid authentication.\",\n\t\t},\n\t\t\"403\": {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tmessage: {\n\t\t\t\t\t\t\t\ttype: \"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\tdescription:\n\t\t\t\t\"Forbidden. You do not have permission to access this resource or to perform this action.\",\n\t\t},\n\t\t\"404\": {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tmessage: {\n\t\t\t\t\t\t\t\ttype: \"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\tdescription: \"Not Found. The requested resource was not found.\",\n\t\t},\n\t\t\"429\": {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tmessage: {\n\t\t\t\t\t\t\t\ttype: \"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\tdescription:\n\t\t\t\t\"Too Many Requests. You have exceeded the rate limit. Try again later.\",\n\t\t},\n\t\t\"500\": {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tmessage: {\n\t\t\t\t\t\t\t\ttype: \"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\tdescription:\n\t\t\t\t\"Internal Server Error. This is a problem with the server that you cannot fix.\",\n\t\t},\n\t\t...responses,\n\t} as any;\n}\n\nexport async function generator(\n\tendpoints: Record<string, Endpoint>,\n\tconfig?: {\n\t\turl: string;\n\t},\n) {\n\tconst components = {\n\t\tschemas: {},\n\t};\n\n\tObject.entries(endpoints).forEach(([_, value]) => {\n\t\tconst options = value.options as EndpointRuntimeOptions;\n\t\tif (!value.path || options.metadata?.SERVER_ONLY) return;\n\t\tif (options.method === \"GET\") {\n\t\t\tpaths[value.path] = {\n\t\t\t\tget: {\n\t\t\t\t\ttags: [\"Default\", ...(options.metadata?.openapi?.tags || [])],\n\t\t\t\t\tdescription: options.metadata?.openapi?.description,\n\t\t\t\t\toperationId: options.metadata?.openapi?.operationId,\n\t\t\t\t\tsecurity: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tbearerAuth: [],\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\tparameters: getParameters(options),\n\t\t\t\t\tresponses: getResponse(options.metadata?.openapi?.responses),\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\n\t\tif (options.method === \"POST\") {\n\t\t\tconst body = getRequestBody(options);\n\t\t\tpaths[value.path] = {\n\t\t\t\tpost: {\n\t\t\t\t\ttags: [\"Default\", ...(options.metadata?.openapi?.tags || [])],\n\t\t\t\t\tdescription: options.metadata?.openapi?.description,\n\t\t\t\t\toperationId: options.metadata?.openapi?.operationId,\n\t\t\t\t\tsecurity: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tbearerAuth: [],\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\tparameters: getParameters(options),\n\t\t\t\t\t...(body\n\t\t\t\t\t\t? { requestBody: body }\n\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\trequestBody: {\n\t\t\t\t\t\t\t\t\t//set body none\n\t\t\t\t\t\t\t\t\tcontent: {\n\t\t\t\t\t\t\t\t\t\t\"application/json\": {\n\t\t\t\t\t\t\t\t\t\t\tschema: {\n\t\t\t\t\t\t\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\t\t\t\t\t\t\tproperties: {},\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t}),\n\t\t\t\t\tresponses: getResponse(options.metadata?.openapi?.responses),\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\t});\n\n\tconst res = {\n\t\topenapi: \"3.1.1\",\n\t\tinfo: {\n\t\t\ttitle: \"Better Auth\",\n\t\t\tdescription: \"API Reference for your Better Auth Instance\",\n\t\t\tversion: \"1.1.0\",\n\t\t},\n\t\tcomponents,\n\t\tsecurity: [\n\t\t\t{\n\t\t\t\tapiKeyCookie: [],\n\t\t\t},\n\t\t],\n\t\tservers: [\n\t\t\t{\n\t\t\t\turl: config?.url,\n\t\t\t},\n\t\t],\n\t\ttags: [\n\t\t\t{\n\t\t\t\tname: \"Default\",\n\t\t\t\tdescription:\n\t\t\t\t\t\"Default endpoints that are included with Better Auth by default. These endpoints are not part of any plugin.\",\n\t\t\t},\n\t\t],\n\t\tpaths,\n\t};\n\treturn res;\n}\n\nexport const getHTML = (\n\tapiReference: Record<string, any>,\n\tconfig?: {\n\t\tlogo?: string;\n\t\ttheme?: string;\n\t\ttitle?: string;\n\t\tdescription?: string;\n\t},\n) => `<!doctype html>\n<html>\n <head>\n <title>Scalar API Reference</title>\n <meta charset=\"utf-8\" />\n <meta\n name=\"viewport\"\n content=\"width=device-width, initial-scale=1\" />\n </head>\n <body>\n <script\n id=\"api-reference\"\n type=\"application/json\">\n ${JSON.stringify(apiReference)}\n </script>\n\t <script>\n var configuration = {\n\t \tfavicon: ${config?.logo ? `data:image/svg+xml;utf8,${encodeURIComponent(config.logo)}` : undefined} ,\n\t \ttheme: ${config?.theme || \"saturn\"},\n metaData: {\n\t\t\ttitle: ${config?.title || \"Open API Reference\"},\n\t\t\tdescription: ${config?.description || \"Better Call Open API\"},\n\t\t}\n }\n document.getElementById('api-reference').dataset.configuration =\n JSON.stringify(configuration)\n </script>\n\t <script src=\"https://cdn.jsdelivr.net/npm/@scalar/api-reference\"></script>\n </body>\n</html>`;\n"],"mappings":";;;AAwFA,MAAM,QAA8B,EAAE;AAEtC,SAAS,mBAAmB,SAAuB;AAClD,SAAQ,QAAQ,YAAY,MAA5B;EACC,KAAK,YACJ,QAAO;EACR,KAAK,YACJ,QAAO;EACR,KAAK,aACJ,QAAO;EACR,KAAK,YACJ,QAAO;EACR,KAAK,WACJ,QAAO;EACR,QACC,QAAO;;;AAIV,SAAS,cAAc,SAAiC;CACvD,MAAM,aAAiC,EAAE;AACzC,KAAI,QAAQ,UAAU,SAAS,YAAY;AAC1C,aAAW,KAAK,GAAG,QAAQ,SAAS,QAAQ,WAAW;AACvD,SAAO;;AAER,KAAI,QAAQ,iBAAiB,UAC5B,QAAO,QAAQ,QAAQ,MAAM,MAAM,CAAC,SAAS,CAAC,KAAK,WAAW;AAC7D,MAAI,iBAAiB,UACpB,YAAW,KAAK;GACf,MAAM;GACN,IAAI;GACJ,QAAQ;IACP,MAAM,mBAAmB,MAAM;IAC/B,GAAI,eAAe,SAAS,MAAM,YAC/B,EACA,WAAW,MAAM,WACjB,GACA,EAAE;IACL,aAAa,MAAM;IACnB;GACD,CAAC;GAEF;AAEH,QAAO;;AAGR,SAAS,eAAe,SAAsC;AAC7D,KAAI,QAAQ,UAAU,SAAS,YAC9B,QAAO,QAAQ,SAAS,QAAQ;AAEjC,KAAI,CAAC,QAAQ,KAAM,QAAO;AAC1B,KACC,QAAQ,gBAAgB,aACxB,QAAQ,gBAAgB,aACvB;EAED,MAAM,QAAQ,QAAQ,KAAK;AAC3B,MAAI,CAAC,MAAO,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K,QAAQ,KACb,CACD;EACD,MAAM,CACL;GACC,MAAM;GACN,aACC;GACD,CACD;EACD;EACA;;AAIF,MAAa,WACZ,cACA,WAMI;;;;;;;;;;;;;MAaC,KAAK,UAAU,aAAa,CAAC;;;;eAIpB,QAAQ,OAAO,2BAA2B,mBAAmB,OAAO,KAAK,KAAK,OAAU;cACzF,QAAQ,SAAS,SAAS;;YAE5B,QAAQ,SAAS,qBAAqB;kBAChC,QAAQ,eAAe,uBAAuB"}
1
+ {"version":3,"file":"openapi.mjs","names":[],"sources":["../src/openapi.ts"],"sourcesContent":["import { ZodObject, ZodOptional, type ZodType } from \"zod\";\nimport type { Endpoint, EndpointRuntimeOptions } from \"./endpoint\";\n\nexport type OpenAPISchemaType =\n\t| \"string\"\n\t| \"number\"\n\t| \"integer\"\n\t| \"boolean\"\n\t| \"array\"\n\t| \"object\";\n\nexport interface OpenAPIParameter {\n\tin: \"query\" | \"path\" | \"header\" | \"cookie\";\n\tname?: string;\n\tdescription?: string;\n\trequired?: boolean;\n\tschema?: {\n\t\ttype: OpenAPISchemaType;\n\t\tformat?: string | undefined;\n\t\titems?: {\n\t\t\ttype: OpenAPISchemaType;\n\t\t};\n\t\tenum?: string[];\n\t\tminLength?: number;\n\t\tdescription?: string | undefined;\n\t\tdefault?: string | undefined;\n\t\texample?: string | undefined;\n\t};\n}\n\ninterface Path {\n\tget?: {\n\t\ttags?: string[];\n\t\toperationId?: string;\n\t\tdescription?: string;\n\t\tsecurity?: [{ bearerAuth: string[] }];\n\t\tparameters?: OpenAPIParameter[];\n\t\tresponses?: {\n\t\t\t[key in string]: {\n\t\t\t\tdescription?: string;\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};\n\t};\n\tpost?: {\n\t\ttags?: string[];\n\t\toperationId?: string;\n\t\tdescription?: string;\n\t\tsecurity?: [{ bearerAuth: string[] }];\n\t\tparameters?: OpenAPIParameter[];\n\t\trequestBody?: {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype?: OpenAPISchemaType;\n\t\t\t\t\t\tproperties?: Record<string, any>;\n\t\t\t\t\t\trequired?: string[];\n\t\t\t\t\t\t$ref?: string;\n\t\t\t\t\t};\n\t\t\t\t};\n\t\t\t};\n\t\t};\n\t\tresponses?: {\n\t\t\t[key in string]: {\n\t\t\t\tdescription?: string;\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};\n\t};\n}\nconst paths: Record<string, Path> = {};\n\nfunction getTypeFromZodType(zodType: ZodType<any>) {\n\tswitch (zodType.constructor.name) {\n\t\tcase \"ZodString\":\n\t\t\treturn \"string\";\n\t\tcase \"ZodNumber\":\n\t\t\treturn \"number\";\n\t\tcase \"ZodBoolean\":\n\t\t\treturn \"boolean\";\n\t\tcase \"ZodObject\":\n\t\t\treturn \"object\";\n\t\tcase \"ZodArray\":\n\t\t\treturn \"array\";\n\t\tdefault:\n\t\t\treturn \"string\";\n\t}\n}\n\nfunction getParameters(options: EndpointRuntimeOptions) {\n\tconst parameters: OpenAPIParameter[] = [];\n\tif (options.metadata?.openapi?.parameters) {\n\t\tparameters.push(...options.metadata.openapi.parameters);\n\t\treturn parameters;\n\t}\n\tif (options.query instanceof ZodObject) {\n\t\tObject.entries(options.query.shape).forEach(([key, value]) => {\n\t\t\tif (value instanceof ZodObject) {\n\t\t\t\tparameters.push({\n\t\t\t\t\tname: key,\n\t\t\t\t\tin: \"query\",\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: getTypeFromZodType(value),\n\t\t\t\t\t\t...(\"minLength\" in value && value.minLength\n\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\tminLength: value.minLength as number,\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t: {}),\n\t\t\t\t\t\tdescription: value.description,\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t}\n\t\t});\n\t}\n\treturn parameters;\n}\n\nfunction getRequestBody(options: EndpointRuntimeOptions): any {\n\tif (options.metadata?.openapi?.requestBody) {\n\t\treturn options.metadata.openapi.requestBody;\n\t}\n\tif (!options.body) return undefined;\n\tif (\n\t\toptions.body instanceof ZodObject ||\n\t\toptions.body instanceof ZodOptional\n\t) {\n\t\t// @ts-expect-error\n\t\tconst shape = options.body.shape;\n\t\tif (!shape) return undefined;\n\t\tconst properties: Record<string, any> = {};\n\t\tconst required: string[] = [];\n\t\tObject.entries(shape).forEach(([key, value]) => {\n\t\t\tif (value instanceof ZodObject) {\n\t\t\t\tproperties[key] = {\n\t\t\t\t\ttype: getTypeFromZodType(value),\n\t\t\t\t\tdescription: value.description,\n\t\t\t\t};\n\t\t\t\tif (!(value instanceof ZodOptional)) {\n\t\t\t\t\trequired.push(key);\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\t\treturn {\n\t\t\trequired:\n\t\t\t\toptions.body instanceof ZodOptional\n\t\t\t\t\t? false\n\t\t\t\t\t: options.body\n\t\t\t\t\t\t? true\n\t\t\t\t\t\t: false,\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties,\n\t\t\t\t\t\trequired,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t};\n\t}\n\treturn undefined;\n}\n\nfunction getResponse(responses?: Record<string, any>) {\n\treturn {\n\t\t\"400\": {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tmessage: {\n\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\trequired: [\"message\"],\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t\tdescription:\n\t\t\t\t\"Bad Request. Usually due to missing parameters, or invalid parameters.\",\n\t\t},\n\t\t\"401\": {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tmessage: {\n\t\t\t\t\t\t\t\ttype: \"string\",\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t\trequired: [\"message\"],\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t\tdescription: \"Unauthorized. Due to missing or invalid authentication.\",\n\t\t},\n\t\t\"403\": {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tmessage: {\n\t\t\t\t\t\t\t\ttype: \"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\tdescription:\n\t\t\t\t\"Forbidden. You do not have permission to access this resource or to perform this action.\",\n\t\t},\n\t\t\"404\": {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tmessage: {\n\t\t\t\t\t\t\t\ttype: \"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\tdescription: \"Not Found. The requested resource was not found.\",\n\t\t},\n\t\t\"429\": {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tmessage: {\n\t\t\t\t\t\t\t\ttype: \"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\tdescription:\n\t\t\t\t\"Too Many Requests. You have exceeded the rate limit. Try again later.\",\n\t\t},\n\t\t\"500\": {\n\t\t\tcontent: {\n\t\t\t\t\"application/json\": {\n\t\t\t\t\tschema: {\n\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\tmessage: {\n\t\t\t\t\t\t\t\ttype: \"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\tdescription:\n\t\t\t\t\"Internal Server Error. This is a problem with the server that you cannot fix.\",\n\t\t},\n\t\t...responses,\n\t} as any;\n}\n\nexport async function generator(\n\tendpoints: Record<string, Endpoint>,\n\tconfig?: {\n\t\turl: string;\n\t},\n) {\n\tconst components = {\n\t\tschemas: {},\n\t};\n\n\tObject.entries(endpoints).forEach(([_, value]) => {\n\t\tconst options = value.options as EndpointRuntimeOptions;\n\t\tif (!value.path || options.metadata?.SERVER_ONLY) return;\n\t\tif (options.method === \"GET\") {\n\t\t\tpaths[value.path] = {\n\t\t\t\tget: {\n\t\t\t\t\ttags: [\"Default\", ...(options.metadata?.openapi?.tags || [])],\n\t\t\t\t\tdescription: options.metadata?.openapi?.description,\n\t\t\t\t\toperationId: options.metadata?.openapi?.operationId,\n\t\t\t\t\tsecurity: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tbearerAuth: [],\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\tparameters: getParameters(options),\n\t\t\t\t\tresponses: getResponse(options.metadata?.openapi?.responses),\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\n\t\tif (options.method === \"POST\") {\n\t\t\tconst body = getRequestBody(options);\n\t\t\tpaths[value.path] = {\n\t\t\t\tpost: {\n\t\t\t\t\ttags: [\"Default\", ...(options.metadata?.openapi?.tags || [])],\n\t\t\t\t\tdescription: options.metadata?.openapi?.description,\n\t\t\t\t\toperationId: options.metadata?.openapi?.operationId,\n\t\t\t\t\tsecurity: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tbearerAuth: [],\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t\tparameters: getParameters(options),\n\t\t\t\t\t...(body\n\t\t\t\t\t\t? { requestBody: body }\n\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\trequestBody: {\n\t\t\t\t\t\t\t\t\t//set body none\n\t\t\t\t\t\t\t\t\tcontent: {\n\t\t\t\t\t\t\t\t\t\t\"application/json\": {\n\t\t\t\t\t\t\t\t\t\t\tschema: {\n\t\t\t\t\t\t\t\t\t\t\t\ttype: \"object\",\n\t\t\t\t\t\t\t\t\t\t\t\tproperties: {},\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t}),\n\t\t\t\t\tresponses: getResponse(options.metadata?.openapi?.responses),\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\t});\n\n\tconst res = {\n\t\topenapi: \"3.1.1\",\n\t\tinfo: {\n\t\t\ttitle: \"Better Auth\",\n\t\t\tdescription: \"API Reference for your Better Auth Instance\",\n\t\t\tversion: \"1.1.0\",\n\t\t},\n\t\tcomponents,\n\t\tsecurity: [\n\t\t\t{\n\t\t\t\tapiKeyCookie: [],\n\t\t\t},\n\t\t],\n\t\tservers: [\n\t\t\t{\n\t\t\t\turl: config?.url,\n\t\t\t},\n\t\t],\n\t\ttags: [\n\t\t\t{\n\t\t\t\tname: \"Default\",\n\t\t\t\tdescription:\n\t\t\t\t\t\"Default endpoints that are included with Better Auth by default. These endpoints are not part of any plugin.\",\n\t\t\t},\n\t\t],\n\t\tpaths,\n\t};\n\treturn res;\n}\n\nexport const getHTML = (\n\tapiReference: Record<string, any>,\n\tconfig?: {\n\t\tlogo?: string;\n\t\ttheme?: string;\n\t\ttitle?: string;\n\t\tdescription?: string;\n\t},\n) => `<!doctype html>\n<html>\n <head>\n <title>Scalar API Reference</title>\n <meta charset=\"utf-8\" />\n <meta\n name=\"viewport\"\n content=\"width=device-width, initial-scale=1\" />\n </head>\n <body>\n <script\n id=\"api-reference\"\n type=\"application/json\">\n ${JSON.stringify(apiReference)}\n </script>\n\t <script>\n var configuration = {\n\t \tfavicon: ${config?.logo ? `data:image/svg+xml;utf8,${encodeURIComponent(config.logo)}` : undefined} ,\n\t \ttheme: ${config?.theme || \"saturn\"},\n metaData: {\n\t\t\ttitle: ${config?.title || \"Open API Reference\"},\n\t\t\tdescription: ${config?.description || \"Better Call Open API\"},\n\t\t}\n }\n document.getElementById('api-reference').dataset.configuration =\n JSON.stringify(configuration)\n </script>\n\t <script src=\"https://cdn.jsdelivr.net/npm/@scalar/api-reference\"></script>\n </body>\n</html>`;\n"],"mappings":";;;AAwFA,MAAM,QAA8B,EAAE;AAEtC,SAAS,mBAAmB,SAAuB;AAClD,SAAQ,QAAQ,YAAY,MAA5B;EACC,KAAK,YACJ,QAAO;EACR,KAAK,YACJ,QAAO;EACR,KAAK,aACJ,QAAO;EACR,KAAK,YACJ,QAAO;EACR,KAAK,WACJ,QAAO;EACR,QACC,QAAO;;;AAIV,SAAS,cAAc,SAAiC;CACvD,MAAM,aAAiC,EAAE;AACzC,KAAI,QAAQ,UAAU,SAAS,YAAY;AAC1C,aAAW,KAAK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MAAM,OAAO,eAAe,QAAQ;AACpC,SAAM,MAAM,QAAQ,EACnB,MAAM;IACL,MAAM,CAAC,WAAW,GAAI,QAAQ,UAAU,SAAS,QAAQ,EAAE,CAAE;IAC7D,aAAa,QAAQ,UAAU,SAAS;IACxC,aAAa,QAAQ,UAAU,SAAS;IACxC,UAAU,CACT,EACC,YAAY,EAAE,EACd,CACD;IACD,YAAY,cAAc,QAAQ;IAClC,GAAI,OACD,EAAE,aAAa,MAAM,GACrB,EACA,aAAa,EAEZ,SAAS,EACR,oBAAoB,EACnB,QAAQ;KACP,MAAM;KACN,YAAY,EAAE;KACd,EACD,EACD,EACD,EACD;IACH,WAAW,YAAY,QAAQ,UAAU,SAAS,UAAU;IAC5D,EACD;;GAED;AA6BF,QA3BY;EACX,SAAS;EACT,MAAM;GACL,OAAO;GACP,aAAa;GACb,SAAS;GACT;EACD;EACA,UAAU,CACT,EACC,cAAc,EAAE,EAChB,CACD;EACD,SAAS,CACR,EACC,KAAK,QAAQ,KACb,CACD;EACD,MAAM,CACL;GACC,MAAM;GACN,aACC;GACD,CACD;EACD;EACA;;AAIF,MAAa,WACZ,cACA,WAMI;;;;;;;;;;;;;MAaC,KAAK,UAAU,aAAa,CAAC;;;;eAIpB,QAAQ,OAAO,2BAA2B,mBAAmB,OAAO,KAAK,KAAK,OAAU;cACzF,QAAQ,SAAS,SAAS;;YAE5B,QAAQ,SAAS,qBAAqB;kBAChC,QAAQ,eAAe,uBAAuB"}
@@ -29,11 +29,63 @@ function isJSONResponse(value) {
29
29
  if (!value || typeof value !== "object") return false;
30
30
  return "_flag" in value && value._flag === "json";
31
31
  }
32
+ /**
33
+ * Headers that MUST be stripped when building an HTTP response from
34
+ * arbitrary header input. These are request-only, hop-by-hop, or
35
+ * transport-managed headers that cause protocol violations when present
36
+ * on responses (e.g. Content-Length mismatch → net::ERR_CONTENT_LENGTH_MISMATCH).
37
+ *
38
+ * Sources:
39
+ * - RFC 9110 §10.1 (Request Context Fields)
40
+ * - RFC 9110 §7.6.1 (Connection / hop-by-hop)
41
+ * - RFC 9110 §11.6-7 (Authentication credentials)
42
+ * - RFC 9110 §12.5 (Content negotiation)
43
+ * - RFC 9110 §13.1 (Conditional request headers)
44
+ * - RFC 9110 §14.2 (Range requests)
45
+ * - RFC 6265 §5.4 (Cookie)
46
+ * - RFC 6454 (Origin)
47
+ */
48
+ const REQUEST_ONLY_HEADERS = new Set([
49
+ "host",
50
+ "user-agent",
51
+ "referer",
52
+ "from",
53
+ "expect",
54
+ "authorization",
55
+ "proxy-authorization",
56
+ "cookie",
57
+ "origin",
58
+ "accept-charset",
59
+ "accept-encoding",
60
+ "accept-language",
61
+ "if-match",
62
+ "if-none-match",
63
+ "if-modified-since",
64
+ "if-unmodified-since",
65
+ "if-range",
66
+ "range",
67
+ "max-forwards",
68
+ "connection",
69
+ "keep-alive",
70
+ "transfer-encoding",
71
+ "te",
72
+ "upgrade",
73
+ "trailer",
74
+ "proxy-connection",
75
+ "content-length"
76
+ ]);
77
+ function stripRequestOnlyHeaders(headers) {
78
+ for (const name of REQUEST_ONLY_HEADERS) headers.delete(name);
79
+ }
32
80
  function toResponse(data, init) {
33
81
  if (data instanceof Response) {
34
- if (init?.headers instanceof Headers) init.headers.forEach((value, key) => {
35
- data.headers.set(key, value);
36
- });
82
+ if (init?.headers) {
83
+ const safeHeaders = new Headers(init.headers);
84
+ stripRequestOnlyHeaders(safeHeaders);
85
+ safeHeaders.forEach((value, key) => {
86
+ data.headers.set(key, value);
87
+ });
88
+ }
37
89
  return data;
38
90
  }
39
91
  if (isJSONResponse(data)) {
@@ -46,7 +98,11 @@ function toResponse(data, init) {
46
98
  for (const [key, value] of headers.entries()) headers.set(key, value);
47
99
  }
48
100
  if (data.headers) for (const [key, value] of new Headers(data.headers).entries()) headers.set(key, value);
49
- if (init?.headers) for (const [key, value] of new Headers(init.headers).entries()) headers.set(key, value);
101
+ if (init?.headers) {
102
+ const safeHeaders = new Headers(init.headers);
103
+ stripRequestOnlyHeaders(safeHeaders);
104
+ for (const [key, value] of safeHeaders.entries()) headers.set(key, value);
105
+ }
50
106
  headers.set("Content-Type", "application/json");
51
107
  return new Response(JSON.stringify(body), {
52
108
  ...routerResponse,
@@ -62,6 +118,7 @@ function toResponse(data, init) {
62
118
  });
63
119
  let body = data;
64
120
  const headers = new Headers(init?.headers);
121
+ stripRequestOnlyHeaders(headers);
65
122
  if (!data) {
66
123
  if (data === null) body = JSON.stringify(null);
67
124
  headers.set("content-type", "application/json");
@@ -1 +1 @@
1
- {"version":3,"file":"to-response.cjs","names":["isAPIError"],"sources":["../src/to-response.ts"],"sourcesContent":["import { APIError } from \"./error\";\nimport { isAPIError } from \"./utils\";\n\nfunction isJSONSerializable(value: any) {\n\tif (value === undefined) {\n\t\treturn false;\n\t}\n\tconst t = typeof value;\n\tif (t === \"string\" || t === \"number\" || t === \"boolean\" || t === null) {\n\t\treturn true;\n\t}\n\tif (t !== \"object\") {\n\t\treturn false;\n\t}\n\tif (Array.isArray(value)) {\n\t\treturn true;\n\t}\n\tif (value.buffer) {\n\t\treturn false;\n\t}\n\treturn (\n\t\t(value.constructor && value.constructor.name === \"Object\") ||\n\t\ttypeof value.toJSON === \"function\"\n\t);\n}\n\nfunction safeStringify(\n\tobj: any,\n\treplacer?: (key: string, value: any) => any,\n\tspace?: string | number,\n): string {\n\tlet id = 0;\n\tconst seen = new WeakMap<object, number>(); // ref -> counter\n\n\tconst safeReplacer = (key: string, value: any) => {\n\t\t// Handle bigint first\n\t\tif (typeof value === \"bigint\") {\n\t\t\treturn value.toString();\n\t\t}\n\n\t\t// Then handle circular references\n\t\tif (typeof value === \"object\" && value !== null) {\n\t\t\tif (seen.has(value)) {\n\t\t\t\treturn `[Circular ref-${seen.get(value)}]`;\n\t\t\t}\n\t\t\tseen.set(value, id++);\n\t\t}\n\n\t\t// Finally apply any custom replacer\n\t\tif (replacer) {\n\t\t\treturn replacer(key, value);\n\t\t}\n\n\t\treturn value;\n\t};\n\n\treturn JSON.stringify(obj, safeReplacer, space);\n}\n\nexport type JSONResponse = {\n\tbody: Record<string, any>;\n\trouterResponse: ResponseInit | undefined;\n\tstatus?: number;\n\theaders?: Record<string, string> | Headers;\n\t_flag: \"json\";\n};\n\nfunction isJSONResponse(value: any): value is JSONResponse {\n\tif (!value || typeof value !== \"object\") {\n\t\treturn false;\n\t}\n\treturn \"_flag\" in value && value._flag === \"json\";\n}\n\nexport function toResponse(data?: any, init?: ResponseInit): Response {\n\tif (data instanceof Response) {\n\t\tif (init?.headers instanceof Headers) {\n\t\t\tinit.headers.forEach((value, key) => {\n\t\t\t\tdata.headers.set(key, value);\n\t\t\t});\n\t\t}\n\t\treturn data;\n\t}\n\tconst isJSON = isJSONResponse(data);\n\tif (isJSON) {\n\t\tconst body = data.body;\n\t\tconst routerResponse = data.routerResponse;\n\t\tif (routerResponse instanceof Response) {\n\t\t\treturn routerResponse;\n\t\t}\n\t\tconst headers = new Headers();\n\t\tif (routerResponse?.headers) {\n\t\t\tconst headers = new Headers(routerResponse.headers);\n\t\t\tfor (const [key, value] of headers.entries()) {\n\t\t\t\theaders.set(key, value);\n\t\t\t}\n\t\t}\n\t\tif (data.headers) {\n\t\t\tfor (const [key, value] of new Headers(data.headers).entries()) {\n\t\t\t\theaders.set(key, value);\n\t\t\t}\n\t\t}\n\t\tif (init?.headers) {\n\t\t\tfor (const [key, value] of new Headers(init.headers).entries()) {\n\t\t\t\theaders.set(key, value);\n\t\t\t}\n\t\t}\n\n\t\theaders.set(\"Content-Type\", \"application/json\");\n\t\treturn new Response(JSON.stringify(body), {\n\t\t\t...routerResponse,\n\t\t\theaders,\n\t\t\tstatus: data.status ?? init?.status ?? routerResponse?.status,\n\t\t\tstatusText: init?.statusText ?? routerResponse?.statusText,\n\t\t});\n\t}\n\tif (isAPIError(data)) {\n\t\treturn toResponse(data.body, {\n\t\t\tstatus: init?.status ?? data.statusCode,\n\t\t\tstatusText: data.status.toString(),\n\t\t\theaders: init?.headers || data.headers,\n\t\t});\n\t}\n\tlet body = data;\n\tconst headers = new Headers(init?.headers);\n\tif (!data) {\n\t\tif (data === null) {\n\t\t\tbody = JSON.stringify(null);\n\t\t}\n\t\theaders.set(\"content-type\", \"application/json\");\n\t} else if (typeof data === \"string\") {\n\t\tbody = data;\n\t\theaders.set(\"Content-Type\", \"text/plain\");\n\t} else if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {\n\t\tbody = data;\n\t\theaders.set(\"Content-Type\", \"application/octet-stream\");\n\t} else if (data instanceof Blob) {\n\t\tbody = data;\n\t\theaders.set(\"Content-Type\", data.type || \"application/octet-stream\");\n\t} else if (data instanceof FormData) {\n\t\tbody = data;\n\t} else if (data instanceof URLSearchParams) {\n\t\tbody = data;\n\t\theaders.set(\"Content-Type\", \"application/x-www-form-urlencoded\");\n\t} else if (data instanceof ReadableStream) {\n\t\tbody = data;\n\t\theaders.set(\"Content-Type\", \"application/octet-stream\");\n\t} else if (isJSONSerializable(data)) {\n\t\tbody = safeStringify(data);\n\t\theaders.set(\"Content-Type\", \"application/json\");\n\t}\n\n\treturn new Response(body, {\n\t\t...init,\n\t\theaders,\n\t});\n}\n"],"mappings":";;;;AAGA,SAAS,mBAAmB,OAAY;AACvC,KAAI,UAAU,OACb,QAAO;CAER,MAAM,IAAI,OAAO;AACjB,KAAI,MAAM,YAAY,MAAM,YAAY,MAAM,aAAa,MAAM,KAChE,QAAO;AAER,KAAI,MAAM,SACT,QAAO;AAER,KAAI,MAAM,QAAQ,MAAM,CACvB,QAAO;AAER,KAAI,MAAM,OACT,QAAO;AAER,QACE,MAAM,eAAe,MAAM,YAAY,SAAS,YACjD,OAAO,MAAM,WAAW;;AAI1B,SAAS,cACR,KACA,UACA,OACS;CACT,IAAI,KAAK;CACT,MAAM,uBAAO,IAAI,SAAyB;CAE1C,MAAM,gBAAgB,KAAa,UAAe;AAEjD,MAAI,OAAO,UAAU,SACpB,QAAO,MAAM,UAAU;AAIxB,MAAI,OAAO,UAAU,YAAY,UAAU,MAAM;AAChD,O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CAAC;;AAEH,KAAIA,yBAAW,KAAK,CACnB,QAAO,WAAW,KAAK,MAAM;EAC5B,QAAQ,MAAM,UAAU,KAAK;EAC7B,YAAY,KAAK,OAAO,UAAU;EAClC,SAAS,MAAM,WAAW,KAAK;EAC/B,CAAC;CAEH,IAAI,OAAO;CACX,MAAM,UAAU,IAAI,QAAQ,MAAM,QAAQ;AAC1C,KAAI,CAAC,MAAM;AACV,MAAI,SAAS,KACZ,QAAO,KAAK,UAAU,KAAK;AAE5B,UAAQ,IAAI,gBAAgB,mBAAmB;YACrC,OAAO,SAAS,UAAU;AACpC,SAAO;AACP,UAAQ,IAAI,gBAAgB,aAAa;YAC/B,gBAAgB,eAAe,YAAY,OAAO,KAAK,EAAE;AACnE,SAAO;AACP,UAAQ,IAAI,gBAAgB,2BAA2B;YAC7C,gBAAgB,MAAM;AAChC,SAAO;AACP,UAAQ,IAAI,gBAAgB,KAAK,QAAQ,2BAA2B;YAC1D,gBAAgB,SAC1B,QAAO;UACG,gBAAgB,iBAAiB;AAC3C,SAAO;AACP,UAAQ,IAAI,gBAAgB,oCAAoC;YACtD,gBAAgB,gBAAgB;AAC1C,SAAO;AACP,UAAQ,IAAI,gBAAgB,2BAA2B;YAC7C,mBAAmB,KAAK,EAAE;AACpC,SAAO,cAAc,KAAK;AAC1B,UAAQ,IAAI,gBAAgB,mBAAmB;;AAGhD,QAAO,IAAI,SAAS,MAAM;EACzB,GAAG;EACH;EACA,CAAC"}
1
+ {"version":3,"file":"to-response.cjs","names":["isAPIError"],"sources":["../src/to-response.ts"],"sourcesContent":["import { APIError } from \"./error\";\nimport { isAPIError } from \"./utils\";\n\nfunction isJSONSerializable(value: any) {\n\tif (value === undefined) {\n\t\treturn false;\n\t}\n\tconst t = typeof value;\n\tif (t === \"string\" || t === \"number\" || t === \"boolean\" || t === null) {\n\t\treturn true;\n\t}\n\tif (t !== \"object\") {\n\t\treturn false;\n\t}\n\tif (Array.isArray(value)) {\n\t\treturn true;\n\t}\n\tif (value.buffer) {\n\t\treturn false;\n\t}\n\treturn (\n\t\t(value.constructor && value.constructor.name === \"Object\") ||\n\t\ttypeof value.toJSON === \"function\"\n\t);\n}\n\nfunction safeStringify(\n\tobj: any,\n\treplacer?: (key: string, value: any) => any,\n\tspace?: string | number,\n): string {\n\tlet id = 0;\n\tconst seen = new WeakMap<object, number>(); // ref -> counter\n\n\tconst safeReplacer = (key: string, value: any) => {\n\t\t// Handle bigint first\n\t\tif (typeof value === \"bigint\") {\n\t\t\treturn value.toString();\n\t\t}\n\n\t\t// Then handle circular references\n\t\tif (typeof value === \"object\" && value !== null) {\n\t\t\tif (seen.has(value)) {\n\t\t\t\treturn `[Circular ref-${seen.get(value)}]`;\n\t\t\t}\n\t\t\tseen.set(value, id++);\n\t\t}\n\n\t\t// Finally apply any custom replacer\n\t\tif (replacer) {\n\t\t\treturn replacer(key, value);\n\t\t}\n\n\t\treturn value;\n\t};\n\n\treturn JSON.stringify(obj, safeReplacer, space);\n}\n\nexport type JSONResponse = {\n\tbody: Record<string, any>;\n\trouterResponse: ResponseInit | undefined;\n\tstatus?: number;\n\theaders?: Record<string, string> | Headers;\n\t_flag: \"json\";\n};\n\nfunction isJSONResponse(value: any): value is JSONResponse {\n\tif (!value || typeof value !== \"object\") {\n\t\treturn false;\n\t}\n\treturn \"_flag\" in value && value._flag === \"json\";\n}\n\n/**\n * Headers that MUST be stripped when building an HTTP response from\n * arbitrary header input. These are request-only, hop-by-hop, or\n * transport-managed headers that cause protocol violations when present\n * on responses (e.g. Content-Length mismatch → net::ERR_CONTENT_LENGTH_MISMATCH).\n *\n * Sources:\n * - RFC 9110 §10.1 (Request Context Fields)\n * - RFC 9110 §7.6.1 (Connection / hop-by-hop)\n * - RFC 9110 §11.6-7 (Authentication credentials)\n * - RFC 9110 §12.5 (Content negotiation)\n * - RFC 9110 §13.1 (Conditional request headers)\n * - RFC 9110 §14.2 (Range requests)\n * - RFC 6265 §5.4 (Cookie)\n * - RFC 6454 (Origin)\n */\nconst REQUEST_ONLY_HEADERS = new Set([\n\t// Request context (RFC 9110 §10.1)\n\t\"host\", // §7.2\n\t\"user-agent\", // §10.1.5\n\t\"referer\", // §10.1.3\n\t\"from\", // §10.1.2\n\t\"expect\", // §10.1.1\n\n\t// Authentication credentials (RFC 9110 §11.6-7)\n\t\"authorization\", // §11.6.2\n\t\"proxy-authorization\", // §11.7.2\n\t\"cookie\", // RFC 6265 §5.4\n\t\"origin\", // RFC 6454\n\n\t// Content negotiation (RFC 9110 §12.5)\n\t\"accept-charset\", // §12.5.2 (deprecated)\n\t\"accept-encoding\", // §12.5.3\n\t\"accept-language\", // §12.5.4\n\n\t// Conditional requests (RFC 9110 §13.1)\n\t\"if-match\", // §13.1.1\n\t\"if-none-match\", // §13.1.2\n\t\"if-modified-since\", // §13.1.3\n\t\"if-unmodified-since\", // §13.1.4\n\t\"if-range\", // §13.1.5\n\n\t// Range requests (RFC 9110 §14.2)\n\t\"range\", // §14.2\n\n\t// Forwarding control (RFC 9110 §7.6)\n\t\"max-forwards\", // §7.6.2\n\n\t// Hop-by-hop (RFC 9110 §7.6.1)\n\t\"connection\", // §7.6.1\n\t\"keep-alive\",\n\t\"transfer-encoding\",\n\t\"te\", // §10.1.4\n\t\"upgrade\",\n\t\"trailer\",\n\t\"proxy-connection\", // non-standard\n\n\t// Valid on responses but WRONG if copied from request (RFC 9110 §8.6)\n\t\"content-length\",\n]);\n\nfunction stripRequestOnlyHeaders(headers: Headers): void {\n\tfor (const name of REQUEST_ONLY_HEADERS) {\n\t\theaders.delete(name);\n\t}\n}\n\nexport function toResponse(data?: any, init?: ResponseInit): Response {\n\tif (data instanceof Response) {\n\t\tif (init?.headers) {\n\t\t\tconst safeHeaders = new Headers(init.headers);\n\t\t\tstripRequestOnlyHeaders(safeHeaders);\n\t\t\tsafeHeaders.forEach((value, key) => {\n\t\t\t\tdata.headers.set(key, value);\n\t\t\t});\n\t\t}\n\t\treturn data;\n\t}\n\tconst isJSON = isJSONResponse(data);\n\tif (isJSON) {\n\t\tconst body = data.body;\n\t\tconst routerResponse = data.routerResponse;\n\t\tif (routerResponse instanceof Response) {\n\t\t\treturn routerResponse;\n\t\t}\n\t\tconst headers = new Headers();\n\t\tif (routerResponse?.headers) {\n\t\t\tconst headers = new Headers(routerResponse.headers);\n\t\t\tfor (const [key, value] of headers.entries()) {\n\t\t\t\theaders.set(key, value);\n\t\t\t}\n\t\t}\n\t\tif (data.headers) {\n\t\t\tfor (const [key, value] of new Headers(data.headers).entries()) {\n\t\t\t\theaders.set(key, value);\n\t\t\t}\n\t\t}\n\t\tif (init?.headers) {\n\t\t\tconst safeHeaders = new Headers(init.headers);\n\t\t\tstripRequestOnlyHeaders(safeHeaders);\n\t\t\tfor (const [key, value] of safeHeaders.entries()) {\n\t\t\t\theaders.set(key, value);\n\t\t\t}\n\t\t}\n\n\t\theaders.set(\"Content-Type\", \"application/json\");\n\t\treturn new Response(JSON.stringify(body), {\n\t\t\t...routerResponse,\n\t\t\theaders,\n\t\t\tstatus: data.status ?? init?.status ?? routerResponse?.status,\n\t\t\tstatusText: init?.statusText ?? routerResponse?.statusText,\n\t\t});\n\t}\n\tif (isAPIError(data)) {\n\t\treturn toResponse(data.body, {\n\t\t\tstatus: init?.status ?? data.statusCode,\n\t\t\tstatusText: data.status.toString(),\n\t\t\theaders: init?.headers || data.headers,\n\t\t});\n\t}\n\tlet body = data;\n\tconst headers = new Headers(init?.headers);\n\tstripRequestOnlyHeaders(headers);\n\tif (!data) {\n\t\tif (data === null) {\n\t\t\tbody = JSON.stringify(null);\n\t\t}\n\t\theaders.set(\"content-type\", \"application/json\");\n\t} else if (typeof data === \"string\") {\n\t\tbody = data;\n\t\theaders.set(\"Content-Type\", \"text/plain\");\n\t} else if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {\n\t\tbody = data;\n\t\theaders.set(\"Content-Type\", \"application/octet-stream\");\n\t} else if (data instanceof Blob) {\n\t\tbody = data;\n\t\theaders.set(\"Content-Type\", data.type || \"application/octet-stream\");\n\t} else if (data instanceof FormData) {\n\t\tbody = data;\n\t} else if (data instanceof URLSearchParams) {\n\t\tbody = data;\n\t\theaders.set(\"Content-Type\", \"application/x-www-form-urlencoded\");\n\t} else if (data instanceof ReadableStream) {\n\t\tbody = data;\n\t\theaders.set(\"Content-Type\", \"application/octet-stream\");\n\t} else if (isJSONSerializable(data)) {\n\t\tbody = safeStringify(data);\n\t\theaders.set(\"Content-Type\", \"application/json\");\n\t}\n\n\treturn new Response(body, {\n\t\t...init,\n\t\theaders,\n\t});\n}\n"],"mappings":";;;;AAGA,SAAS,mBAAmB,OAAY;AACvC,KAAI,UAAU,OACb,QAAO;CAER,MAAM,IAAI,OAAO;AACjB,KAAI,MAAM,YAAY,MAAM,YAAY,MAAM,aAAa,MAAM,KAChE,QAAO;AAER,KAAI,MAAM,SACT,QAAO;AAER,KAAI,MAAM,QAAQ,MAAM,CACvB,QAAO;AAER,KAAI,MAAM,OACT,QAAO;AAER,QACE,MAAM,eAAe,MAAM,YAAY,SAAS,YACjD,OAAO,MAAM,WAAW;;AAI1B,SAAS,cACR,KACA,UACA,OACS;CACT,IAAI,KAAK;CACT,MAAM,uBAAO,IAAI,SAAyB;CAE1C,MAAM,gBAAgB,KAAa,UAAe;AAEjD,MAAI,OAAO,UAAU,SACpB,QAAO,MAAM,UAAU;AAIxB,MAAI,OAAO,UAAU,YAAY,UAAU,MAAM;AAChD,OAAI,KAAK,IAAI,MAAM,CAClB,QAAO,iBAAiB,KAAK,IAAI,MAAM,CAAC;AAEzC,QAAK,IAAI,OAAO,KAAK;;AAItB,MAAI,SACH,QAAO,SAAS,KAAK,MAAM;AAG5B,SAAO;;AAGR,QAAO,KAAK,UAAU,KAAK,cAAc,MAAM;;AAWhD,SAAS,eAAe,OAAmC;AAC1D,KAAI,CAAC,SAAS,OAAO,UAAU,SAC9B,QAAO;AAER,QAAO,WAAW,SAAS,MAAM,UAAU;;;;;;;;;;;;;;;;;;AAmB5C,MAAM,uBAAuB,IAAI,IAA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MAAM,cAAc,gBAAgB;GAChD,CAAC;;AAEH,KAAIA,yBAAW,KAAK,CACnB,QAAO,WAAW,KAAK,MAAM;EAC5B,QAAQ,MAAM,UAAU,KAAK;EAC7B,YAAY,KAAK,OAAO,UAAU;EAClC,SAAS,MAAM,WAAW,KAAK;EAC/B,CAAC;CAEH,IAAI,OAAO;CACX,MAAM,UAAU,IAAI,QAAQ,MAAM,QAAQ;AAC1C,yBAAwB,QAAQ;AAChC,KAAI,CAAC,MAAM;AACV,MAAI,SAAS,KACZ,QAAO,KAAK,UAAU,KAAK;AAE5B,UAAQ,IAAI,gBAAgB,mBAAmB;YACrC,OAAO,SAAS,UAAU;AACpC,SAAO;AACP,UAAQ,IAAI,gBAAgB,aAAa;YAC/B,gBAAgB,eAAe,YAAY,OAAO,KAAK,EAAE;AACnE,SAAO;AACP,UAAQ,IAAI,gBAAgB,2BAA2B;YAC7C,gBAAgB,MAAM;AAChC,SAAO;AACP,UAAQ,IAAI,gBAAgB,KAAK,QAAQ,2BAA2B;YAC1D,gBAAgB,SAC1B,QAAO;UACG,gBAAgB,iBAAiB;AAC3C,SAAO;AACP,UAAQ,IAAI,gBAAgB,oCAAoC;YACtD,gBAAgB,gBAAgB;AAC1C,SAAO;AACP,UAAQ,IAAI,gBAAgB,2BAA2B;YAC7C,mBAAmB,KAAK,EAAE;AACpC,SAAO,cAAc,KAAK;AAC1B,UAAQ,IAAI,gBAAgB,mBAAmB;;AAGhD,QAAO,IAAI,SAAS,MAAM;EACzB,GAAG;EACH;EACA,CAAC"}
@@ -29,11 +29,63 @@ function isJSONResponse(value) {
29
29
  if (!value || typeof value !== "object") return false;
30
30
  return "_flag" in value && value._flag === "json";
31
31
  }
32
+ /**
33
+ * Headers that MUST be stripped when building an HTTP response from
34
+ * arbitrary header input. These are request-only, hop-by-hop, or
35
+ * transport-managed headers that cause protocol violations when present
36
+ * on responses (e.g. Content-Length mismatch → net::ERR_CONTENT_LENGTH_MISMATCH).
37
+ *
38
+ * Sources:
39
+ * - RFC 9110 §10.1 (Request Context Fields)
40
+ * - RFC 9110 §7.6.1 (Connection / hop-by-hop)
41
+ * - RFC 9110 §11.6-7 (Authentication credentials)
42
+ * - RFC 9110 §12.5 (Content negotiation)
43
+ * - RFC 9110 §13.1 (Conditional request headers)
44
+ * - RFC 9110 §14.2 (Range requests)
45
+ * - RFC 6265 §5.4 (Cookie)
46
+ * - RFC 6454 (Origin)
47
+ */
48
+ const REQUEST_ONLY_HEADERS = new Set([
49
+ "host",
50
+ "user-agent",
51
+ "referer",
52
+ "from",
53
+ "expect",
54
+ "authorization",
55
+ "proxy-authorization",
56
+ "cookie",
57
+ "origin",
58
+ "accept-charset",
59
+ "accept-encoding",
60
+ "accept-language",
61
+ "if-match",
62
+ "if-none-match",
63
+ "if-modified-since",
64
+ "if-unmodified-since",
65
+ "if-range",
66
+ "range",
67
+ "max-forwards",
68
+ "connection",
69
+ "keep-alive",
70
+ "transfer-encoding",
71
+ "te",
72
+ "upgrade",
73
+ "trailer",
74
+ "proxy-connection",
75
+ "content-length"
76
+ ]);
77
+ function stripRequestOnlyHeaders(headers) {
78
+ for (const name of REQUEST_ONLY_HEADERS) headers.delete(name);
79
+ }
32
80
  function toResponse(data, init) {
33
81
  if (data instanceof Response) {
34
- if (init?.headers instanceof Headers) init.headers.forEach((value, key) => {
35
- data.headers.set(key, value);
36
- });
82
+ if (init?.headers) {
83
+ const safeHeaders = new Headers(init.headers);
84
+ stripRequestOnlyHeaders(safeHeaders);
85
+ safeHeaders.forEach((value, key) => {
86
+ data.headers.set(key, value);
87
+ });
88
+ }
37
89
  return data;
38
90
  }
39
91
  if (isJSONResponse(data)) {
@@ -46,7 +98,11 @@ function toResponse(data, init) {
46
98
  for (const [key, value] of headers.entries()) headers.set(key, value);
47
99
  }
48
100
  if (data.headers) for (const [key, value] of new Headers(data.headers).entries()) headers.set(key, value);
49
- if (init?.headers) for (const [key, value] of new Headers(init.headers).entries()) headers.set(key, value);
101
+ if (init?.headers) {
102
+ const safeHeaders = new Headers(init.headers);
103
+ stripRequestOnlyHeaders(safeHeaders);
104
+ for (const [key, value] of safeHeaders.entries()) headers.set(key, value);
105
+ }
50
106
  headers.set("Content-Type", "application/json");
51
107
  return new Response(JSON.stringify(body), {
52
108
  ...routerResponse,
@@ -62,6 +118,7 @@ function toResponse(data, init) {
62
118
  });
63
119
  let body = data;
64
120
  const headers = new Headers(init?.headers);
121
+ stripRequestOnlyHeaders(headers);
65
122
  if (!data) {
66
123
  if (data === null) body = JSON.stringify(null);
67
124
  headers.set("content-type", "application/json");
@@ -1 +1 @@
1
- {"version":3,"file":"to-response.mjs","names":[],"sources":["../src/to-response.ts"],"sourcesContent":["import { APIError } from \"./error\";\nimport { isAPIError } from \"./utils\";\n\nfunction isJSONSerializable(value: any) {\n\tif (value === undefined) {\n\t\treturn false;\n\t}\n\tconst t = typeof value;\n\tif (t === \"string\" || t === \"number\" || t === \"boolean\" || t === null) {\n\t\treturn true;\n\t}\n\tif (t !== \"object\") {\n\t\treturn false;\n\t}\n\tif (Array.isArray(value)) {\n\t\treturn true;\n\t}\n\tif (value.buffer) {\n\t\treturn false;\n\t}\n\treturn (\n\t\t(value.constructor && value.constructor.name === \"Object\") ||\n\t\ttypeof value.toJSON === \"function\"\n\t);\n}\n\nfunction safeStringify(\n\tobj: any,\n\treplacer?: (key: string, value: any) => any,\n\tspace?: string | number,\n): string {\n\tlet id = 0;\n\tconst seen = new WeakMap<object, number>(); // ref -> counter\n\n\tconst safeReplacer = (key: string, value: any) => {\n\t\t// Handle bigint first\n\t\tif (typeof value === \"bigint\") {\n\t\t\treturn value.toString();\n\t\t}\n\n\t\t// Then handle circular references\n\t\tif (typeof value === \"object\" && value !== null) {\n\t\t\tif (seen.has(value)) {\n\t\t\t\treturn `[Circular ref-${seen.get(value)}]`;\n\t\t\t}\n\t\t\tseen.set(value, id++);\n\t\t}\n\n\t\t// Finally apply any custom replacer\n\t\tif (replacer) {\n\t\t\treturn replacer(key, value);\n\t\t}\n\n\t\treturn value;\n\t};\n\n\treturn JSON.stringify(obj, safeReplacer, space);\n}\n\nexport type JSONResponse = {\n\tbody: Record<string, any>;\n\trouterResponse: ResponseInit | undefined;\n\tstatus?: number;\n\theaders?: Record<string, string> | Headers;\n\t_flag: \"json\";\n};\n\nfunction isJSONResponse(value: any): value is JSONResponse {\n\tif (!value || typeof value !== \"object\") {\n\t\treturn false;\n\t}\n\treturn \"_flag\" in value && value._flag === \"json\";\n}\n\nexport function toResponse(data?: any, init?: ResponseInit): Response {\n\tif (data instanceof Response) {\n\t\tif (init?.headers instanceof Headers) {\n\t\t\tinit.headers.forEach((value, key) => {\n\t\t\t\tdata.headers.set(key, value);\n\t\t\t});\n\t\t}\n\t\treturn data;\n\t}\n\tconst isJSON = isJSONResponse(data);\n\tif (isJSON) {\n\t\tconst body = data.body;\n\t\tconst routerResponse = data.routerResponse;\n\t\tif (routerResponse instanceof Response) {\n\t\t\treturn routerResponse;\n\t\t}\n\t\tconst headers = new Headers();\n\t\tif (routerResponse?.headers) {\n\t\t\tconst headers = new Headers(routerResponse.headers);\n\t\t\tfor (const [key, value] of headers.entries()) {\n\t\t\t\theaders.set(key, value);\n\t\t\t}\n\t\t}\n\t\tif (data.headers) {\n\t\t\tfor (const [key, value] of new Headers(data.headers).entries()) {\n\t\t\t\theaders.set(key, value);\n\t\t\t}\n\t\t}\n\t\tif (init?.headers) {\n\t\t\tfor (const [key, value] of new Headers(init.headers).entries()) {\n\t\t\t\theaders.set(key, value);\n\t\t\t}\n\t\t}\n\n\t\theaders.set(\"Content-Type\", \"application/json\");\n\t\treturn new Response(JSON.stringify(body), {\n\t\t\t...routerResponse,\n\t\t\theaders,\n\t\t\tstatus: data.status ?? init?.status ?? routerResponse?.status,\n\t\t\tstatusText: init?.statusText ?? routerResponse?.statusText,\n\t\t});\n\t}\n\tif (isAPIError(data)) {\n\t\treturn toResponse(data.body, {\n\t\t\tstatus: init?.status ?? data.statusCode,\n\t\t\tstatusText: data.status.toString(),\n\t\t\theaders: init?.headers || data.headers,\n\t\t});\n\t}\n\tlet body = data;\n\tconst headers = new Headers(init?.headers);\n\tif (!data) {\n\t\tif (data === null) {\n\t\t\tbody = JSON.stringify(null);\n\t\t}\n\t\theaders.set(\"content-type\", \"application/json\");\n\t} else if (typeof data === \"string\") {\n\t\tbody = data;\n\t\theaders.set(\"Content-Type\", \"text/plain\");\n\t} else if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {\n\t\tbody = data;\n\t\theaders.set(\"Content-Type\", \"application/octet-stream\");\n\t} else if (data instanceof Blob) {\n\t\tbody = data;\n\t\theaders.set(\"Content-Type\", data.type || \"application/octet-stream\");\n\t} else if (data instanceof FormData) {\n\t\tbody = data;\n\t} else if (data instanceof URLSearchParams) {\n\t\tbody = data;\n\t\theaders.set(\"Content-Type\", \"application/x-www-form-urlencoded\");\n\t} else if (data instanceof ReadableStream) {\n\t\tbody = data;\n\t\theaders.set(\"Content-Type\", \"application/octet-stream\");\n\t} else if (isJSONSerializable(data)) {\n\t\tbody = safeStringify(data);\n\t\theaders.set(\"Content-Type\", \"application/json\");\n\t}\n\n\treturn new Response(body, {\n\t\t...init,\n\t\theaders,\n\t});\n}\n"],"mappings":";;;;AAGA,SAAS,mBAAmB,OAAY;AACvC,KAAI,UAAU,OACb,QAAO;CAER,MAAM,IAAI,OAAO;AACjB,KAAI,MAAM,YAAY,MAAM,YAAY,MAAM,aAAa,MAAM,KAChE,QAAO;AAER,KAAI,MAAM,SACT,QAAO;AAER,KAAI,MAAM,QAAQ,MAAM,CACvB,QAAO;AAER,KAAI,MAAM,OACT,QAAO;AAER,QACE,MAAM,eAAe,MAAM,YAAY,SAAS,YACjD,OAAO,MAAM,WAAW;;AAI1B,SAAS,cACR,KACA,UACA,OACS;CACT,IAAI,KAAK;CACT,MAAM,uBAAO,IAAI,SAAyB;CAE1C,MAAM,gBAAgB,KAAa,UAAe;AAEjD,MAAI,OAAO,UAAU,SACpB,QAAO,MAAM,UAAU;AAIxB,MAAI,O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vD,YAAY,MAAM,cAAc,gBAAgB;GAChD,CAAC;;AAEH,KAAI,WAAW,KAAK,CACnB,QAAO,WAAW,KAAK,MAAM;EAC5B,QAAQ,MAAM,UAAU,KAAK;EAC7B,YAAY,KAAK,OAAO,UAAU;EAClC,SAAS,MAAM,WAAW,KAAK;EAC/B,CAAC;CAEH,IAAI,OAAO;CACX,MAAM,UAAU,IAAI,QAAQ,MAAM,QAAQ;AAC1C,KAAI,CAAC,MAAM;AACV,MAAI,SAAS,KACZ,QAAO,KAAK,UAAU,KAAK;AAE5B,UAAQ,IAAI,gBAAgB,mBAAmB;YACrC,OAAO,SAAS,UAAU;AACpC,SAAO;AACP,UAAQ,IAAI,gBAAgB,aAAa;YAC/B,gBAAgB,eAAe,YAAY,OAAO,KAAK,EAAE;AACnE,SAAO;AACP,UAAQ,IAAI,gBAAgB,2BAA2B;YAC7C,gBAAgB,MAAM;AAChC,SAAO;AACP,UAAQ,IAAI,gBAAgB,KAAK,QAAQ,2BAA2B;YAC1D,gBAAgB,SAC1B,QAAO;UACG,gBAAgB,iBAAiB;AAC3C,SAAO;AACP,UAAQ,IAAI,gBAAgB,oCAAoC;YACtD,gBAAgB,gBAAgB;AAC1C,SAAO;AACP,UAAQ,IAAI,gBAAgB,2BAA2B;YAC7C,mBAAmB,KAAK,EAAE;AACpC,SAAO,cAAc,KAAK;AAC1B,UAAQ,IAAI,gBAAgB,mBAAmB;;AAGhD,QAAO,IAAI,SAAS,MAAM;EACzB,GAAG;EACH;EACA,CAAC"}
1
+ {"version":3,"file":"to-response.mjs","names":[],"sources":["../src/to-response.ts"],"sourcesContent":["import { APIError } from \"./error\";\nimport { isAPIError } from \"./utils\";\n\nfunction isJSONSerializable(value: any) {\n\tif (value === undefined) {\n\t\treturn false;\n\t}\n\tconst t = typeof value;\n\tif (t === \"string\" || t === \"number\" || t === \"boolean\" || t === null) {\n\t\treturn true;\n\t}\n\tif (t !== \"object\") {\n\t\treturn false;\n\t}\n\tif (Array.isArray(value)) {\n\t\treturn true;\n\t}\n\tif (value.buffer) {\n\t\treturn false;\n\t}\n\treturn (\n\t\t(value.constructor && value.constructor.name === \"Object\") ||\n\t\ttypeof value.toJSON === \"function\"\n\t);\n}\n\nfunction safeStringify(\n\tobj: any,\n\treplacer?: (key: string, value: any) => any,\n\tspace?: string | number,\n): string {\n\tlet id = 0;\n\tconst seen = new WeakMap<object, number>(); // ref -> counter\n\n\tconst safeReplacer = (key: string, value: any) => {\n\t\t// Handle bigint first\n\t\tif (typeof value === \"bigint\") {\n\t\t\treturn value.toString();\n\t\t}\n\n\t\t// Then handle circular references\n\t\tif (typeof value === \"object\" && value !== null) {\n\t\t\tif (seen.has(value)) {\n\t\t\t\treturn `[Circular ref-${seen.get(value)}]`;\n\t\t\t}\n\t\t\tseen.set(value, id++);\n\t\t}\n\n\t\t// Finally apply any custom replacer\n\t\tif (replacer) {\n\t\t\treturn replacer(key, value);\n\t\t}\n\n\t\treturn value;\n\t};\n\n\treturn JSON.stringify(obj, safeReplacer, space);\n}\n\nexport type JSONResponse = {\n\tbody: Record<string, any>;\n\trouterResponse: ResponseInit | undefined;\n\tstatus?: number;\n\theaders?: Record<string, string> | Headers;\n\t_flag: \"json\";\n};\n\nfunction isJSONResponse(value: any): value is JSONResponse {\n\tif (!value || typeof value !== \"object\") {\n\t\treturn false;\n\t}\n\treturn \"_flag\" in value && value._flag === \"json\";\n}\n\n/**\n * Headers that MUST be stripped when building an HTTP response from\n * arbitrary header input. These are request-only, hop-by-hop, or\n * transport-managed headers that cause protocol violations when present\n * on responses (e.g. Content-Length mismatch → net::ERR_CONTENT_LENGTH_MISMATCH).\n *\n * Sources:\n * - RFC 9110 §10.1 (Request Context Fields)\n * - RFC 9110 §7.6.1 (Connection / hop-by-hop)\n * - RFC 9110 §11.6-7 (Authentication credentials)\n * - RFC 9110 §12.5 (Content negotiation)\n * - RFC 9110 §13.1 (Conditional request headers)\n * - RFC 9110 §14.2 (Range requests)\n * - RFC 6265 §5.4 (Cookie)\n * - RFC 6454 (Origin)\n */\nconst REQUEST_ONLY_HEADERS = new Set([\n\t// Request context (RFC 9110 §10.1)\n\t\"host\", // §7.2\n\t\"user-agent\", // §10.1.5\n\t\"referer\", // §10.1.3\n\t\"from\", // §10.1.2\n\t\"expect\", // §10.1.1\n\n\t// Authentication credentials (RFC 9110 §11.6-7)\n\t\"authorization\", // §11.6.2\n\t\"proxy-authorization\", // §11.7.2\n\t\"cookie\", // RFC 6265 §5.4\n\t\"origin\", // RFC 6454\n\n\t// Content negotiation (RFC 9110 §12.5)\n\t\"accept-charset\", // §12.5.2 (deprecated)\n\t\"accept-encoding\", // §12.5.3\n\t\"accept-language\", // §12.5.4\n\n\t// Conditional requests (RFC 9110 §13.1)\n\t\"if-match\", // §13.1.1\n\t\"if-none-match\", // §13.1.2\n\t\"if-modified-since\", // §13.1.3\n\t\"if-unmodified-since\", // §13.1.4\n\t\"if-range\", // §13.1.5\n\n\t// Range requests (RFC 9110 §14.2)\n\t\"range\", // §14.2\n\n\t// Forwarding control (RFC 9110 §7.6)\n\t\"max-forwards\", // §7.6.2\n\n\t// Hop-by-hop (RFC 9110 §7.6.1)\n\t\"connection\", // §7.6.1\n\t\"keep-alive\",\n\t\"transfer-encoding\",\n\t\"te\", // §10.1.4\n\t\"upgrade\",\n\t\"trailer\",\n\t\"proxy-connection\", // non-standard\n\n\t// Valid on responses but WRONG if copied from request (RFC 9110 §8.6)\n\t\"content-length\",\n]);\n\nfunction stripRequestOnlyHeaders(headers: Headers): void {\n\tfor (const name of REQUEST_ONLY_HEADERS) {\n\t\theaders.delete(name);\n\t}\n}\n\nexport function toResponse(data?: any, init?: ResponseInit): Response {\n\tif (data instanceof Response) {\n\t\tif (init?.headers) {\n\t\t\tconst safeHeaders = new Headers(init.headers);\n\t\t\tstripRequestOnlyHeaders(safeHeaders);\n\t\t\tsafeHeaders.forEach((value, key) => {\n\t\t\t\tdata.headers.set(key, value);\n\t\t\t});\n\t\t}\n\t\treturn data;\n\t}\n\tconst isJSON = isJSONResponse(data);\n\tif (isJSON) {\n\t\tconst body = data.body;\n\t\tconst routerResponse = data.routerResponse;\n\t\tif (routerResponse instanceof Response) {\n\t\t\treturn routerResponse;\n\t\t}\n\t\tconst headers = new Headers();\n\t\tif (routerResponse?.headers) {\n\t\t\tconst headers = new Headers(routerResponse.headers);\n\t\t\tfor (const [key, value] of headers.entries()) {\n\t\t\t\theaders.set(key, value);\n\t\t\t}\n\t\t}\n\t\tif (data.headers) {\n\t\t\tfor (const [key, value] of new Headers(data.headers).entries()) {\n\t\t\t\theaders.set(key, value);\n\t\t\t}\n\t\t}\n\t\tif (init?.headers) {\n\t\t\tconst safeHeaders = new Headers(init.headers);\n\t\t\tstripRequestOnlyHeaders(safeHeaders);\n\t\t\tfor (const [key, value] of safeHeaders.entries()) {\n\t\t\t\theaders.set(key, value);\n\t\t\t}\n\t\t}\n\n\t\theaders.set(\"Content-Type\", \"application/json\");\n\t\treturn new Response(JSON.stringify(body), {\n\t\t\t...routerResponse,\n\t\t\theaders,\n\t\t\tstatus: data.status ?? init?.status ?? routerResponse?.status,\n\t\t\tstatusText: init?.statusText ?? routerResponse?.statusText,\n\t\t});\n\t}\n\tif (isAPIError(data)) {\n\t\treturn toResponse(data.body, {\n\t\t\tstatus: init?.status ?? data.statusCode,\n\t\t\tstatusText: data.status.toString(),\n\t\t\theaders: init?.headers || data.headers,\n\t\t});\n\t}\n\tlet body = data;\n\tconst headers = new Headers(init?.headers);\n\tstripRequestOnlyHeaders(headers);\n\tif (!data) {\n\t\tif (data === null) {\n\t\t\tbody = JSON.stringify(null);\n\t\t}\n\t\theaders.set(\"content-type\", \"application/json\");\n\t} else if (typeof data === \"string\") {\n\t\tbody = data;\n\t\theaders.set(\"Content-Type\", \"text/plain\");\n\t} else if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {\n\t\tbody = data;\n\t\theaders.set(\"Content-Type\", \"application/octet-stream\");\n\t} else if (data instanceof Blob) {\n\t\tbody = data;\n\t\theaders.set(\"Content-Type\", data.type || \"application/octet-stream\");\n\t} else if (data instanceof FormData) {\n\t\tbody = data;\n\t} else if (data instanceof URLSearchParams) {\n\t\tbody = data;\n\t\theaders.set(\"Content-Type\", \"application/x-www-form-urlencoded\");\n\t} else if (data instanceof ReadableStream) {\n\t\tbody = data;\n\t\theaders.set(\"Content-Type\", \"application/octet-stream\");\n\t} else if (isJSONSerializable(data)) {\n\t\tbody = safeStringify(data);\n\t\theaders.set(\"Content-Type\", \"application/json\");\n\t}\n\n\treturn new Response(body, {\n\t\t...init,\n\t\theaders,\n\t});\n}\n"],"mappings":";;;;AAGA,SAAS,mBAAmB,OAAY;AACvC,KAAI,UAAU,OACb,QAAO;CAER,MAAM,IAAI,OAAO;AACjB,KAAI,MAAM,YAAY,MAAM,YAAY,MAAM,aAAa,MAAM,KAChE,QAAO;AAER,KAAI,MAAM,SACT,QAAO;AAER,KAAI,MAAM,QAAQ,MAAM,CACvB,QAAO;AAER,KAAI,MAAM,OACT,QAAO;AAER,QACE,MAAM,eAAe,MAAM,YAAY,SAAS,YACjD,OAAO,MAAM,WAAW;;AAI1B,SAAS,cACR,KACA,UACA,OACS;CACT,IAAI,KAAK;CACT,MAAM,uBAAO,IAAI,SAAyB;CAE1C,MAAM,gBAAgB,KAAa,UAAe;AAEjD,MAAI,OAAO,UAAU,SACpB,QAAO,MAAM,UAAU;AAIxB,MAAI,OAAO,UAAU,YAAY,UAAU,MAAM;AAChD,OAAI,KAAK,IAAI,MAAM,CAClB,QAAO,iBAAiB,KAAK,IAAI,MAAM,CAAC;AAEzC,QAAK,IAAI,OAAO,KAAK;;AAItB,MAAI,SACH,QAAO,SAAS,KAAK,MAAM;AAG5B,SAAO;;AAGR,QAAO,KAAK,UAAU,KAAK,cAAc,MAAM;;AAWhD,SAAS,eAAe,OAAmC;AAC1D,KAAI,CAAC,SAAS,OAAO,UAAU,SAC9B,QAAO;AAER,QAAO,WAAW,SAAS,MAAM,UAAU;;;;;;;;;;;;;;;;;;AAmB5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UAAU,gBAAgB;GACvD,YAAY,MAAM,cAAc,gBAAgB;GAChD,CAAC;;AAEH,KAAI,WAAW,KAAK,CACnB,QAAO,WAAW,KAAK,MAAM;EAC5B,QAAQ,MAAM,UAAU,KAAK;EAC7B,YAAY,KAAK,OAAO,UAAU;EAClC,SAAS,MAAM,WAAW,KAAK;EAC/B,CAAC;CAEH,IAAI,OAAO;CACX,MAAM,UAAU,IAAI,QAAQ,MAAM,QAAQ;AAC1C,yBAAwB,QAAQ;AAChC,KAAI,CAAC,MAAM;AACV,MAAI,SAAS,KACZ,QAAO,KAAK,UAAU,KAAK;AAE5B,UAAQ,IAAI,gBAAgB,mBAAmB;YACrC,OAAO,SAAS,UAAU;AACpC,SAAO;AACP,UAAQ,IAAI,gBAAgB,aAAa;YAC/B,gBAAgB,eAAe,YAAY,OAAO,KAAK,EAAE;AACnE,SAAO;AACP,UAAQ,IAAI,gBAAgB,2BAA2B;YAC7C,gBAAgB,MAAM;AAChC,SAAO;AACP,UAAQ,IAAI,gBAAgB,KAAK,QAAQ,2BAA2B;YAC1D,gBAAgB,SAC1B,QAAO;UACG,gBAAgB,iBAAiB;AAC3C,SAAO;AACP,UAAQ,IAAI,gBAAgB,oCAAoC;YACtD,gBAAgB,gBAAgB;AAC1C,SAAO;AACP,UAAQ,IAAI,gBAAgB,2BAA2B;YAC7C,mBAAmB,KAAK,EAAE;AACpC,SAAO,cAAc,KAAK;AAC1B,UAAQ,IAAI,gBAAgB,mBAAmB;;AAGhD,QAAO,IAAI,SAAS,MAAM;EACzB,GAAG;EACH;EACA,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "better-call",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -22,7 +22,7 @@
22
22
  "supertest": "^7.1.4"
23
23
  },
24
24
  "dependencies": {
25
- "@better-auth/utils": "^0.3.1",
25
+ "@better-auth/utils": "^0.4.0",
26
26
  "@better-fetch/fetch": "^1.1.21",
27
27
  "rou3": "^0.7.12",
28
28
  "set-cookie-parser": "^3.0.1"