@sugardarius/anzen 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 AurΓ©lien Dupays Dexemple
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,145 @@
1
+ ## [Unreleased yet]
2
+
3
+ A flexible, framework validation agnostic, type-safe factory for creating Next.JS App Router route handlers.
4
+
5
+ - πŸ”§ Framework validation agnostic, use a validation library of your choice supporting [Standard Schema](https://standardschema.dev/).
6
+ - 🧠 Focused functionalities, use only features you want.
7
+ - 🧹 Clean and flexible API.
8
+ - πŸ”’ Type-safe.
9
+ - 🌱 Dependency free.
10
+
11
+ ## Install
12
+
13
+ ```sh
14
+ npm i @sugardarius/anzen
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```tsx
20
+ import { createSafeRouteHandler } from '@sugardarius/anzen'
21
+ import { auth } from '~/lib/auth'
22
+
23
+ export const GET = createSafeRouteHandler(
24
+ {
25
+ authorize: async ({ req }) => {
26
+ const session = await auth.getSession(req)
27
+ if (!session) {
28
+ return new Response(null, { status: 401 })
29
+ }
30
+
31
+ return { user: session.user }
32
+ },
33
+ },
34
+ async ({ auth }, req): Promise<Response> => {
35
+ return Response.json({ user: auth.user }, { status: 200 })
36
+ }
37
+ )
38
+ ```
39
+
40
+ The example above shows how to use the factory to authorize your requests.
41
+
42
+ ## Framework validation agnostic
43
+
44
+ By design the factory is framework validation agnostic 🌟. When doing your validations you can use whatever you want as framework validation as long as it implements the [Standard Schema](https://github.com/standard-schema/standard-schema) common interface. You can use your favorite validation library like [Zod](https://zod.dev/) or [decoders](https://decoders.cc/).
45
+
46
+ ```tsx
47
+ import { z } from 'zod'
48
+ import { object, string, number } from 'decoders'
49
+
50
+ export const POST = createSafeRouteHandler(
51
+ {
52
+ // `zod` for segments dictionary validation
53
+ segments: { id: z.string() }
54
+ // `decoders` for body validation
55
+ body: object({
56
+ id: number,
57
+ name: string,
58
+ }),
59
+ },
60
+ async ({ body }) => {
61
+ return Response.json({ body })
62
+ }
63
+ )
64
+ ```
65
+
66
+ ## Synchronous validations
67
+
68
+ The factory do not supports async validations. As required by the [Standard Schema](https://github.com/standard-schema/standard-schema) common interface we should avoid it. In the context of a route handler it's not necessary.
69
+
70
+ If you define an async validation then the route handler will throw an error.
71
+
72
+ ## API
73
+
74
+ Check the API and the available options to configure the factory as you wish.
75
+
76
+ ### Error handling
77
+
78
+ By design the factory will catch any error thrown in the route handler will return a simple response with `500` status.
79
+
80
+ You can customize the error response if you want to fine tune error response management.
81
+
82
+ ```tsx
83
+ import { HttpError, DbUnknownError } from '~/lib/errors'
84
+ import { db } from '~/lib/db'
85
+
86
+ export const GET = createSafeRouteHandler(
87
+ {
88
+ onErrorResponse: async (err: unknown): Promise<Response> => {
89
+ if (err instanceof HttpError) {
90
+ return new Response(err.message, { status: err.status })
91
+ } else if (err instanceof DbUnknownError) {
92
+ return new Response(err.message, { status: err.status })
93
+ }
94
+
95
+ return new Response('Internal server error', { status: 500 })
96
+ },
97
+ },
98
+ async (): Promise<Response> => {
99
+ const [data, err] = await db.findUnique({ id: 'liveblocks' })
100
+
101
+ if (err) {
102
+ throw new DbUnknownError(err.message, 500)
103
+ }
104
+
105
+ if (data === null) {
106
+ throw new HttpError(404)
107
+ }
108
+
109
+ return Response.json({ data })
110
+ }
111
+ )
112
+ ```
113
+
114
+ ## Fair use note
115
+
116
+ Please note that if you're not using any of the proposed options in `createSafeRouteHandler` it means you're surely don't need it.
117
+
118
+ ```tsx
119
+ // Calling πŸ‘‡πŸ»
120
+ export const GET = createSafeRouteHandler({}, async () => {
121
+ return new Response(null, { status: 200 })
122
+ })
123
+
124
+ // is equal to declare the route handler this way πŸ‘‡πŸ»
125
+ export function GET() {
126
+ return new Response(null, { status: 200 })
127
+ }
128
+ // excepts `createSafeRouteHandler` will provide by default a native error catching
129
+ // and will return a `500` response. That's the only advantage.
130
+ ```
131
+
132
+ Feel free to open an issue or a PR if you think a relevant option could be added into the factory πŸ™‚
133
+
134
+ ## Contributing
135
+
136
+ All contributions are welcome! πŸ™‚ Feel free to open an issue if you find a bug or create a pull request if you have a feature request.
137
+
138
+ ## Credits
139
+
140
+ - Thanks to [@t3-oss/env-core](https://github.com/t3-oss/t3-env) for opening the implementation of `StandardSchemaDictionary` πŸ™πŸ»
141
+ - Thanks to [frimousse](https://github.com/liveblocks/frimousse) for opening the release & publish workflow πŸ™πŸ»
142
+
143
+ ## License
144
+
145
+ This project is licensed under the [MIT License](https://choosealicense.com/licenses/mit/).
package/dist/index.cjs ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }function b(e){return e!==null&&(typeof e=="object"||typeof e=="function")&&typeof e.then=="function"}function T(e,r){if(b(e))throw new Error(r)}function x(e=!1){let r=e||process.env.NODE_ENV!=="production";return{info:(t,...n)=>{r&&console.log(t,...n)},error:(t,...n)=>{r&&console.error(t,...n)},warn:(t,...n)=>{r&&console.warn(t,...n)}}}function D(e,r,t="Validation must be synchronous but schema returned a Promise."){let n=e["~standard"].validate(r);return T(n,t),n}var O=(e,r)=>r in e;function f(e,r){let t={},n=[];for(let u in e){if(!O(e,u))continue;let i=e[u]["~standard"].validate(r[u]);if(T(i,`Validation must be synchronous, but ${u} returned a Promise.`),i.issues){n.push(...i.issues.map(p=>({...p,path:[u,..._nullishCoalesce(p.path, () => ([]))]})));continue}t[u]=i.value}return n.length>0?{issues:n}:{value:t}}var V="[unknown:route:handler]",k=async e=>{if(_optionalChain([e, 'access', _ => _.headers, 'access', _2 => _2.get, 'call', _3 => _3("content-type"), 'optionalAccess', _4 => _4.startsWith, 'call', _5 => _5("application/json")]))return await e.json();let t=await e.text();return JSON.parse(t)};function A(e,r){if(e.body&&e.formData)throw new Error("You cannot use both `body` and `formData` in the same route handler. They are both mutually exclusive.");let t=x(e.debug),n=_nullishCoalesce(e.id, () => (V)),u=_nullishCoalesce(e.onErrorResponse, () => ((a=>(t.error(`\u{1F6D1} Unexpected error in route handler '${n}'`,a),new Response("Internal server error",{status:500}))))),i=_nullishCoalesce(e.onSegmentsValidationErrorResponse, () => ((a=>(t.error(`\u{1F6D1} Invalid segments for route handler '${n}':`,a),new Response("Invalid segments",{status:400}))))),p=_nullishCoalesce(e.onSearchParamsValidationErrorResponse, () => ((a=>(t.error(`\u{1F6D1} Invalid search params for route handler '${n}':`,a),new Response("Invalid search params",{status:400}))))),I=_nullishCoalesce(e.onBodyValidationErrorResponse, () => ((a=>(t.error(`\u{1F6D1} Invalid body for route handler '${n}':`,a),new Response("Invalid body",{status:400}))))),g=_nullishCoalesce(e.onFormDataValidationErrorResponse, () => ((a=>(t.error(`\u{1F6D1} Invalid form data for route handler '${n}':`,a),new Response("Invalid form data",{status:400}))))),P=_nullishCoalesce(e.authorize, () => ((async()=>{})));return async function(a,w){t.info(`\u{1F504} Running route handler '${n}'`),t.info(`\u{1F449}\u{1F3FB} Request url: ${a.url}`);let m=new URL(a.url),l=await P({req:a,url:m});if(l instanceof Response)return t.error(`\u{1F6D1} Request not authorized for route handler '${n}'`),l;let y;if(e.segments){if(w.params===void 0)return new Response("No segments provided",{status:400});let s=await w.params,o=f(e.segments,s);if(o.issues)return await i(o.issues);y=o.value}let h;if(e.searchParams){let s=[...m.searchParams.keys()].map(d=>{let c=m.searchParams.getAll(d);return[d,c.length>1?c:c[0]]}),o=f(e.searchParams,Object.fromEntries(s));if(o.issues)return await p(o.issues);h=o.value}let S;if(e.body){if(!["POST","PUT","PATCH"].includes(a.method))return new Response("Invalid method for request body",{status:405});let s;try{s=await k(a)}catch(d){return await u(d)}let o=D(e.body,s,"Request body validation must be synchronous");if(o.issues)return await I(o.issues);S=o.value}let R;if(e.formData){if(!["POST","PUT","PATCH"].includes(a.method))return new Response("Invalid method for request form data",{status:405});let s=a.headers.get("content-type");if(!_optionalChain([s, 'optionalAccess', _6 => _6.startsWith, 'call', _7 => _7("multipart/form-data")])&&!_optionalChain([s, 'optionalAccess', _8 => _8.startsWith, 'call', _9 => _9("application/x-www-form-urlencoded")]))return new Response("Invalid content type for request form data",{status:415});let o;try{o=await a.formData()}catch(c){return await u(c)}let d=f(e.formData,Object.fromEntries(o.entries()));if(d.issues)return await g(d.issues);R=d.value}let v={id:n,url:m,...l?{auth:l}:{},...y?{segments:y}:{},...h?{searchParams:h}:{},...S?{body:S}:{},...R?{formData:R}:{}};try{return await r(v,a)}catch(s){return await u(s)}}}exports.createSafeRouteHandler = A;
2
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/utils.ts","../src/standard-schema.ts","../src/create-safe-route-handler.ts"],"names":["isPromise","value","assertsSyncOperation","message","createLogger","debug","shouldLog","rest","validateWithSchema","schema","errSyncMsg","result","hasDictKey","obj","key","parseWithDictionary","dictionary","issues","propResult","issue","DEFAULT_ID","readRequestBodyAsJson","req","text","createSafeRouteHandler","options","handlerFn","log","id","onErrorResponse","err","onSegmentsValidationErrorResponse","onSearchParamsValidationErrorResponse","onBodyValidationErrorResponse","onFormDataValidationErrorResponse","authorize","extras"],"mappings":"AAAO,0rBAASA,CAAAA,CACdC,CAAAA,CACsC,CACtC,OACEA,CAAAA,GAAU,IAAA,EAAA,CACT,OAAOA,CAAAA,EAAU,QAAA,EAAY,OAAOA,CAAAA,EAAU,UAAA,CAAA,EAE/C,OAAQA,CAAAA,CAAc,IAAA,EAAS,UAEnC,CAEO,SAASC,CAAAA,CACdD,CAAAA,CACAE,CAAAA,CACoB,CACpB,EAAA,CAAIH,CAAAA,CAAaC,CAAK,CAAA,CACpB,MAAM,IAAI,KAAA,CAAME,CAAO,CAE3B,CAEO,SAASC,CAAAA,CAAaC,CAAAA,CAAiB,CAAA,CAAA,CAAO,CACnD,IAAMC,CAAAA,CAAYD,CAAAA,EAAS,OAAA,CAAQ,GAAA,CAAI,QAAA,GAAa,YAAA,CACpD,MAAO,CACL,IAAA,CAAM,CAACF,CAAAA,CAAAA,GAAoBI,CAAAA,CAAAA,EAA0B,CAC/CD,CAAAA,EACF,OAAA,CAAQ,GAAA,CAAIH,CAAAA,CAAS,GAAGI,CAAI,CAEhC,CAAA,CACA,KAAA,CAAO,CAACJ,CAAAA,CAAAA,GAAoBI,CAAAA,CAAAA,EAA0B,CAChDD,CAAAA,EACF,OAAA,CAAQ,KAAA,CAAMH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AAA,CAAA","file":"/home/runner/work/anzen/anzen/dist/index.cjs","sourcesContent":["export function isPromise<T>(\n value: unknown\n): value is Promise<T> | PromiseLike<T> {\n return (\n value !== null &&\n (typeof value === 'object' || typeof value === 'function') &&\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n typeof (value as any).then === 'function'\n )\n}\n\nexport function assertsSyncOperation<T>(\n value: T | Promise<T> | PromiseLike<T>,\n message: string\n): asserts value is T {\n if (isPromise<T>(value)) {\n throw new Error(message)\n }\n}\n\nexport function createLogger(debug: boolean = false) {\n const shouldLog = debug || process.env.NODE_ENV !== 'production'\n return {\n info: (message: string, ...rest: unknown[]): void => {\n if (shouldLog) {\n console.log(message, ...rest)\n }\n },\n error: (message: string, ...rest: unknown[]): void => {\n if (shouldLog) {\n console.error(message, ...rest)\n }\n },\n warn: (message: string, ...rest: unknown[]): void => {\n if (shouldLog) {\n console.warn(message, ...rest)\n }\n },\n }\n}\n","import { assertsSyncOperation } from './utils'\n\n/** The Standard Schema interface. */\nexport interface StandardSchemaV1<Input = unknown, Output = Input> {\n /** The Standard Schema properties. */\n readonly '~standard': StandardSchemaV1.Props<Input, Output>\n}\n\nexport declare namespace StandardSchemaV1 {\n /** The Standard Schema properties interface. */\n export interface Props<Input = unknown, Output = Input> {\n /** The version number of the standard. */\n readonly version: 1\n /** The vendor name of the schema library. */\n readonly vendor: string\n /** Validates unknown input values. */\n readonly validate: (\n value: unknown\n ) => Result<Output> | Promise<Result<Output>>\n /** Inferred types associated with the schema. */\n readonly types?: Types<Input, Output> | undefined\n }\n\n /** The result interface of the validate function. */\n export type Result<Output> = SuccessResult<Output> | FailureResult\n\n /** The result interface if validation succeeds. */\n export interface SuccessResult<Output> {\n /** The typed output value. */\n readonly value: Output\n /** The non-existent issues. */\n readonly issues?: undefined\n }\n\n /** The result interface if validation fails. */\n export interface FailureResult {\n /** The issues of failed validation. */\n readonly issues: ReadonlyArray<Issue>\n }\n\n /** The issue interface of the failure output. */\n export interface Issue {\n /** The error message of the issue. */\n readonly message: string\n /** The path of the issue, if any. */\n readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined\n }\n\n /** The path segment interface of the issue. */\n export interface PathSegment {\n /** The key representing a path segment. */\n readonly key: PropertyKey\n }\n\n /** The Standard Schema types interface. */\n export interface Types<Input = unknown, Output = Input> {\n /** The input type of the schema. */\n readonly input: Input\n /** The output type of the schema. */\n readonly output: Output\n }\n\n /** Infers the input type of a Standard Schema. */\n export type InferInput<Schema extends StandardSchemaV1> = NonNullable<\n Schema['~standard']['types']\n >['input']\n\n /** Infers the output type of a Standard Schema. */\n export type InferOutput<Schema extends StandardSchemaV1> = NonNullable<\n Schema['~standard']['types']\n >['output']\n}\n\nexport function validateWithSchema<TSchema extends StandardSchemaV1>(\n schema: TSchema,\n value: unknown,\n errSyncMsg = 'Validation must be synchronous but schema returned a Promise.'\n): StandardSchemaV1.Result<StandardSchemaV1.InferOutput<TSchema>> {\n const result = schema['~standard'].validate(value)\n assertsSyncOperation(result, errSyncMsg)\n\n return result\n}\n\n// Thanks to `@t3-env/core` (πŸ‘‰πŸ» https://github.com/t3-oss/t3-env/blob/main/packages/core/src/standard.ts)\n// for this awesome dictionary schema.\nexport type StandardSchemaDictionary<\n Input = Record<string, unknown>,\n Output extends Record<keyof Input, unknown> = Input,\n> = {\n [K in keyof Input]-?: StandardSchemaV1<Input[K], Output[K]>\n}\n\nexport namespace StandardSchemaDictionary {\n export type InferInput<T extends StandardSchemaDictionary> = {\n [K in keyof T]: StandardSchemaV1.InferInput<T[K]>\n }\n export type InferOutput<T extends StandardSchemaDictionary> = {\n [K in keyof T]: StandardSchemaV1.InferOutput<T[K]>\n }\n}\n\nconst hasDictKey = <T extends object, K extends PropertyKey>(\n obj: T,\n key: K\n): obj is T & Record<typeof key, unknown> => {\n return key in obj\n}\n\nexport function parseWithDictionary<TDict extends StandardSchemaDictionary>(\n dictionary: TDict,\n value: Record<string, unknown>\n): StandardSchemaV1.Result<StandardSchemaDictionary.InferOutput<TDict>> {\n const result: Record<string, unknown> = {}\n const issues: StandardSchemaV1.Issue[] = []\n\n for (const key in dictionary) {\n if (!hasDictKey(dictionary, key)) {\n continue\n }\n // NOTE: safe to assert as we're ensuring just before key isn't undefined\n const propResult = dictionary[key]!['~standard'].validate(value[key])\n\n assertsSyncOperation(\n propResult,\n `Validation must be synchronous, but ${key} returned a Promise.`\n )\n\n if (propResult.issues) {\n issues.push(\n ...propResult.issues.map((issue) => ({\n ...issue,\n path: [key, ...(issue.path ?? [])],\n }))\n )\n continue\n }\n result[key] = propResult.value\n }\n\n if (issues.length > 0) {\n return { issues }\n }\n\n return { value: result as never }\n}\n","import { createLogger } from './utils'\nimport {\n parseWithDictionary,\n validateWithSchema,\n type StandardSchemaV1,\n} from './standard-schema'\nimport type {\n Awaitable,\n AuthContext,\n TSegmentsDict,\n TSearchParamsDict,\n TBodySchema,\n TFormDataDict,\n RequestExtras,\n CreateSafeRouteHandlerOptions,\n CreateSafeRouteHandlerReturnType,\n SafeRouteHandler,\n SafeRouteHandlerContext,\n} from './types'\n\n/** @internal exported for testing only */\nexport const DEFAULT_ID = '[unknown:route:handler]'\n\n/**\n * @internal\n *\n * Reads the request body as JSON.\n * If it fails, it calls the `onErrorResponse` callback.\n */\nconst readRequestBodyAsJson = async (req: Request): Promise<unknown> => {\n const contentType = req.headers.get('content-type')\n if (contentType?.startsWith('application/json')) {\n return await req.json()\n }\n\n const text = await req.text()\n return JSON.parse(text)\n}\n\n/**\n * Creates a safe route handler with data validation and error handling\n * for Next.js (>= 14) API route handlers.\n *\n * @param options - Options to configure the route handler.\n * @param handlerFn - The route handler function.\n *\n * @returns A Next.js API route handler function.\n *\n * @example\n * ```ts\n * import { string } from 'decoders'\n *import { createSafeRouteHandler } from '@sugardarius/anzen'\n * import { auth } from '~/lib/auth'\n *\n * export const GET = createSafeRouteHandler(\n * {\n * id: 'my-safe-route-handler',\n * authorize: async ({ req }) => {\n * const session = await auth.getSession(req)\n * if (!session) {\n * return new Response(null, { status: 401 })\n * }\n *\n * return { user: session.user }\n * },\n * segments: {\n * id: string,\n * },\n * },\n * async ({ auth, segments, }, req): Promise<Response> => {\n * return Response.json({ user: auth.user, segments }, { status: 200 })\n * }\n *)\n * ```\n */\nexport function createSafeRouteHandler<\n AC extends AuthContext | undefined = undefined,\n TRouteDynamicSegments extends TSegmentsDict | undefined = undefined,\n TSearchParams extends TSearchParamsDict | undefined = undefined,\n TBody extends TBodySchema | undefined = undefined,\n TFormData extends TFormDataDict | undefined = undefined,\n>(\n options: CreateSafeRouteHandlerOptions<\n AC,\n TRouteDynamicSegments,\n TSearchParams,\n TBody,\n TFormData\n >,\n handlerFn: SafeRouteHandler<\n AC,\n TRouteDynamicSegments,\n TSearchParams,\n TBody,\n TFormData\n >\n): CreateSafeRouteHandlerReturnType {\n // NOTE: `body` and `formData` options are mutually exclusive 🎭\n if (options.body && options.formData) {\n throw new Error(\n 'You cannot use both `body` and `formData` in the same route handler. They are both mutually exclusive.'\n )\n }\n\n const log = createLogger(options.debug)\n const id = options.id ?? DEFAULT_ID\n\n const onErrorResponse =\n options.onErrorResponse ??\n ((err: unknown): Awaitable<Response> => {\n log.error(`πŸ›‘ Unexpected error in route handler '${id}'`, err)\n return new Response('Internal server error', {\n status: 500,\n })\n })\n\n const onSegmentsValidationErrorResponse =\n options.onSegmentsValidationErrorResponse ??\n ((issues: readonly StandardSchemaV1.Issue[]): Awaitable<Response> => {\n log.error(`πŸ›‘ Invalid segments for route handler '${id}':`, issues)\n return new Response('Invalid segments', {\n status: 400,\n })\n })\n\n const onSearchParamsValidationErrorResponse =\n options.onSearchParamsValidationErrorResponse ??\n ((issues: readonly StandardSchemaV1.Issue[]): Awaitable<Response> => {\n log.error(`πŸ›‘ Invalid search params for route handler '${id}':`, issues)\n return new Response('Invalid search params', {\n status: 400,\n })\n })\n\n const onBodyValidationErrorResponse =\n options.onBodyValidationErrorResponse ??\n ((issues: readonly StandardSchemaV1.Issue[]): Awaitable<Response> => {\n log.error(`πŸ›‘ Invalid body for route handler '${id}':`, issues)\n return new Response('Invalid body', {\n status: 400,\n })\n })\n\n const onFormDataValidationErrorResponse =\n options.onFormDataValidationErrorResponse ??\n ((issues: readonly StandardSchemaV1.Issue[]): Awaitable<Response> => {\n log.error(`πŸ›‘ Invalid form data for route handler '${id}':`, issues)\n return new Response('Invalid form data', {\n status: 400,\n })\n })\n\n const authorize = options.authorize ?? (async () => undefined)\n\n // Next.js API Route handler declaration\n return async function (\n req: Request,\n extras: RequestExtras\n ): Promise<Response> {\n log.info(`πŸ”„ Running route handler '${id}'`)\n log.info(`πŸ‘‰πŸ» Request url: ${req.url}`)\n\n const url = new URL(req.url)\n\n const authOrResponse = await authorize({ req, url })\n if (authOrResponse instanceof Response) {\n log.error(`πŸ›‘ Request not authorized for route handler '${id}'`)\n return authOrResponse\n }\n\n let segments = undefined\n if (options.segments) {\n if (extras.params === undefined) {\n return new Response('No segments provided', { status: 400 })\n }\n\n const params = await extras.params\n const parsedSegments = parseWithDictionary(options.segments, params)\n\n if (parsedSegments.issues) {\n return await onSegmentsValidationErrorResponse(parsedSegments.issues)\n }\n\n segments = parsedSegments.value\n }\n\n let searchParams = undefined\n if (options.searchParams) {\n const queryParams_unsafe = [...url.searchParams.keys()].map((k) => {\n const values = url.searchParams.getAll(k)\n return [k, values.length > 1 ? values : values[0]]\n })\n\n const parsedSearchParams = parseWithDictionary(\n options.searchParams,\n Object.fromEntries(queryParams_unsafe)\n )\n\n if (parsedSearchParams.issues) {\n return await onSearchParamsValidationErrorResponse(\n parsedSearchParams.issues\n )\n }\n\n searchParams = parsedSearchParams.value\n }\n\n let body = undefined\n if (options.body) {\n if (!['POST', 'PUT', 'PATCH'].includes(req.method)) {\n return new Response('Invalid method for request body', {\n status: 405,\n })\n }\n\n let body_unsafe: unknown\n try {\n body_unsafe = await readRequestBodyAsJson(req)\n } catch (err) {\n return await onErrorResponse(err)\n }\n\n const parsedBody = validateWithSchema(\n options.body,\n body_unsafe,\n 'Request body validation must be synchronous'\n )\n\n if (parsedBody.issues) {\n return await onBodyValidationErrorResponse(parsedBody.issues)\n }\n\n body = parsedBody.value\n }\n\n let formData = undefined\n if (options.formData) {\n if (!['POST', 'PUT', 'PATCH'].includes(req.method)) {\n return new Response('Invalid method for request form data', {\n status: 405,\n })\n }\n\n const contentType = req.headers.get('content-type')\n if (\n !contentType?.startsWith('multipart/form-data') &&\n !contentType?.startsWith('application/x-www-form-urlencoded')\n ) {\n return new Response('Invalid content type for request form data', {\n status: 415,\n })\n }\n\n let formData_unsafe: FormData\n try {\n formData_unsafe = await req.formData() // NOTE: πŸ€” maybe find a better way to counted the deprecation warning?\n } catch (err) {\n return await onErrorResponse(err)\n }\n\n const parsedFormData = parseWithDictionary(\n options.formData,\n Object.fromEntries(formData_unsafe.entries())\n )\n\n if (parsedFormData.issues) {\n return await onFormDataValidationErrorResponse(parsedFormData.issues)\n }\n\n formData = parsedFormData.value\n }\n\n // Build safe route handler context\n const ctx = {\n id,\n url,\n ...(authOrResponse ? { auth: authOrResponse } : {}),\n ...(segments ? { segments } : {}),\n ...(searchParams ? { searchParams } : {}),\n ...(body ? { body } : {}),\n ...(formData ? { formData } : {}),\n } as SafeRouteHandlerContext<\n AC,\n TRouteDynamicSegments,\n TSearchParams,\n TBody,\n TFormData\n >\n\n // Let's catch any error that might happen in the handler\n try {\n return await handlerFn(ctx, req)\n } catch (err) {\n return await onErrorResponse(err)\n }\n }\n}\n"]}
@@ -0,0 +1,256 @@
1
+ /** The Standard Schema interface. */
2
+ interface StandardSchemaV1<Input = unknown, Output = Input> {
3
+ /** The Standard Schema properties. */
4
+ readonly '~standard': StandardSchemaV1.Props<Input, Output>;
5
+ }
6
+ declare namespace StandardSchemaV1 {
7
+ /** The Standard Schema properties interface. */
8
+ interface Props<Input = unknown, Output = Input> {
9
+ /** The version number of the standard. */
10
+ readonly version: 1;
11
+ /** The vendor name of the schema library. */
12
+ readonly vendor: string;
13
+ /** Validates unknown input values. */
14
+ readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>;
15
+ /** Inferred types associated with the schema. */
16
+ readonly types?: Types<Input, Output> | undefined;
17
+ }
18
+ /** The result interface of the validate function. */
19
+ type Result<Output> = SuccessResult<Output> | FailureResult;
20
+ /** The result interface if validation succeeds. */
21
+ interface SuccessResult<Output> {
22
+ /** The typed output value. */
23
+ readonly value: Output;
24
+ /** The non-existent issues. */
25
+ readonly issues?: undefined;
26
+ }
27
+ /** The result interface if validation fails. */
28
+ interface FailureResult {
29
+ /** The issues of failed validation. */
30
+ readonly issues: ReadonlyArray<Issue>;
31
+ }
32
+ /** The issue interface of the failure output. */
33
+ interface Issue {
34
+ /** The error message of the issue. */
35
+ readonly message: string;
36
+ /** The path of the issue, if any. */
37
+ readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
38
+ }
39
+ /** The path segment interface of the issue. */
40
+ interface PathSegment {
41
+ /** The key representing a path segment. */
42
+ readonly key: PropertyKey;
43
+ }
44
+ /** The Standard Schema types interface. */
45
+ interface Types<Input = unknown, Output = Input> {
46
+ /** The input type of the schema. */
47
+ readonly input: Input;
48
+ /** The output type of the schema. */
49
+ readonly output: Output;
50
+ }
51
+ /** Infers the input type of a Standard Schema. */
52
+ type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema['~standard']['types']>['input'];
53
+ /** Infers the output type of a Standard Schema. */
54
+ type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema['~standard']['types']>['output'];
55
+ }
56
+ type StandardSchemaDictionary<Input = Record<string, unknown>, Output extends Record<keyof Input, unknown> = Input> = {
57
+ [K in keyof Input]-?: StandardSchemaV1<Input[K], Output[K]>;
58
+ };
59
+ declare namespace StandardSchemaDictionary {
60
+ type InferInput<T extends StandardSchemaDictionary> = {
61
+ [K in keyof T]: StandardSchemaV1.InferInput<T[K]>;
62
+ };
63
+ type InferOutput<T extends StandardSchemaDictionary> = {
64
+ [K in keyof T]: StandardSchemaV1.InferOutput<T[K]>;
65
+ };
66
+ }
67
+
68
+ type EmptyObjectType = {};
69
+ type UnwrapReadonlyObject<T> = T extends Readonly<infer U> ? U : T;
70
+ type Awaitable<T> = T | PromiseLike<T>;
71
+ type AuthContext = Record<string, unknown>;
72
+ type TSegmentsDict = StandardSchemaDictionary;
73
+ type TSearchParamsDict = StandardSchemaDictionary;
74
+ type TBodySchema = StandardSchemaV1;
75
+ type TFormDataDict = StandardSchemaDictionary;
76
+ type AuthFunction<AC extends AuthContext | undefined> = (input: {
77
+ /**
78
+ * Original request
79
+ */
80
+ readonly req: Request;
81
+ /**
82
+ * Parsed request url
83
+ */
84
+ readonly url: URL;
85
+ }) => Awaitable<AC | Response>;
86
+ type BaseOptions<AC extends AuthContext | undefined> = {
87
+ /**
88
+ * ID for the route handler.
89
+ * Used when logging in development or when `debug` is enabled.
90
+ *
91
+ * You can also use it in the route handler definition to add extra logging
92
+ * or monitoring.
93
+ */
94
+ id?: string;
95
+ /**
96
+ * Function to use to authorize the request.
97
+ * By default it always authorize the request.
98
+ *
99
+ * When returning a response, it will be used as the response for the request.
100
+ * Return a response when the request is not authorized.
101
+ */
102
+ authorize?: AuthFunction<AC>;
103
+ /**
104
+ * Callback triggered when the request fails.
105
+ * By default it returns a simple `500` response and the error is logged into the console.
106
+ *
107
+ * Use it if your handler use custom errors and
108
+ * you want to manage them properly by returning a proper response.
109
+ */
110
+ onErrorResponse?: (err: unknown) => Awaitable<Response>;
111
+ /**
112
+ * Use this options to enable debug mode.
113
+ * It will add logs in the handler to help you debug the request.
114
+ *
115
+ * By default it's `false` for production builds.
116
+ * In development builds, it will be `true` if `NODE_ENV` is not set to `production`.
117
+ */
118
+ debug?: boolean;
119
+ };
120
+ type OnValidationErrorResponse = (issues: readonly StandardSchemaV1.Issue[]) => Awaitable<Response>;
121
+ type CreateSafeRouteHandlerOptions<AC extends AuthContext | undefined, TSegments extends TSegmentsDict | undefined, TSearchParams extends TSearchParamsDict | undefined, TBody extends TBodySchema | undefined, TFormData extends TFormDataDict | undefined> = {
122
+ /**
123
+ * Dynamic route segments used in the route handler path.
124
+ */
125
+ segments?: TSegments;
126
+ /**
127
+ * Callback triggered when dynamic segments validations returned issues.
128
+ * By default it returns a simple `400` response and issues are logged into the console.
129
+ */
130
+ onSegmentsValidationErrorResponse?: OnValidationErrorResponse;
131
+ /**
132
+ * Search params used in the route.
133
+ */
134
+ searchParams?: TSearchParams;
135
+ /**
136
+ * Callback triggered when search params validations returned issues.
137
+ * By default it returns a simple `400` response and issues are logged into the console.
138
+ */
139
+ onSearchParamsValidationErrorResponse?: OnValidationErrorResponse;
140
+ /**
141
+ * Request body.
142
+ *
143
+ * Returns a `405` response if the request method is not `POST`, 'PUT' or 'PATCH'.
144
+ * Returns a `415`response if the request does not explicitly set the `Content-Type` to `application/json`.
145
+ *
146
+ * IMPORTANT: The body is parsed as JSON, so it must be a valid JSON object!
147
+ * IMPORTANT: body shouldn't be used with `formData` at the same time. They are exclusive.
148
+ * Why making the distinction? `formData` is used as a `StandardSchemaDictionary` whereas `body` is used as a `StandardSchemaV1`.
149
+ */
150
+ body?: TBody;
151
+ /**
152
+ * Callback triggered when body validation returned issues.
153
+ * By default it returns a simple `400` response and issues are logged into the console.
154
+ */
155
+ onBodyValidationErrorResponse?: OnValidationErrorResponse;
156
+ /**
157
+ * Request form data.
158
+ *
159
+ * Returns a `405` response if the request method is not `POST`, 'PUT' or 'PATCH'.
160
+ * Returns a `415`response if the request does not explicitly set the `Content-Type` to `multipart/form-data`
161
+ * or to `application/x-www-form-urlencoded`.
162
+ *
163
+ * IMPORTANT: formData shouldn't be used with `body` at the same time. They are exclusive.
164
+ * Why making the distinction? `formData` is used as a `StandardSchemaDictionary` whereas `body` is used as a `StandardSchemaV1`.
165
+ */
166
+ formData?: TFormData;
167
+ /**
168
+ * Callback triggered when form data validation returned issues.
169
+ * By default it returns a simple `400` response and issues are logged into the console.
170
+ */
171
+ onFormDataValidationErrorResponse?: OnValidationErrorResponse;
172
+ } & BaseOptions<AC>;
173
+ type RequestExtras = {
174
+ /**
175
+ * Route dynamic segments as params
176
+ */
177
+ params: Awaitable<any> | undefined;
178
+ };
179
+ type CreateSafeRouteHandlerReturnType = (
180
+ /**
181
+ * Original request
182
+ */
183
+ req: Request,
184
+ /**
185
+ * Extras added by Next.js itself
186
+ */
187
+ extras: RequestExtras) => Promise<Response>;
188
+ type SafeRouteHandlerContext<AC extends AuthContext | undefined, TSegments extends TSegmentsDict | undefined, TSearchParams extends TSearchParamsDict | undefined, TBody extends TBodySchema | undefined, TFormData extends TFormDataDict | undefined> = {
189
+ /**
190
+ * Route handler ID
191
+ */
192
+ readonly id: string;
193
+ /**
194
+ * Parsed request url
195
+ */
196
+ readonly url: URL;
197
+ } & (AC extends AuthContext ? {
198
+ readonly auth: AC;
199
+ } : EmptyObjectType) & (TSegments extends TSegmentsDict ? {
200
+ readonly segments: UnwrapReadonlyObject<StandardSchemaDictionary.InferOutput<TSegments>>;
201
+ } : EmptyObjectType) & (TSearchParams extends TSearchParamsDict ? {
202
+ readonly searchParams: UnwrapReadonlyObject<StandardSchemaDictionary.InferOutput<TSearchParams>>;
203
+ } : EmptyObjectType) & (TBody extends TBodySchema ? {
204
+ readonly body: StandardSchemaV1.InferOutput<TBody>;
205
+ } : EmptyObjectType) & (TFormData extends TFormDataDict ? {
206
+ readonly formData: UnwrapReadonlyObject<StandardSchemaDictionary.InferOutput<TFormData>>;
207
+ } : EmptyObjectType);
208
+ type SafeRouteHandler<AC extends AuthContext | undefined, TSegments extends TSegmentsDict | undefined, TSearchParams extends TSearchParamsDict | undefined, TBody extends TBodySchema | undefined, TFormData extends TFormDataDict | undefined> = (
209
+ /**
210
+ * Safe route handler context
211
+ */
212
+ ctx: SafeRouteHandlerContext<AC, TSegments, TSearchParams, TBody, TFormData>,
213
+ /**
214
+ * Original request
215
+ */
216
+ req: Request) => Promise<Response>;
217
+
218
+ /**
219
+ * Creates a safe route handler with data validation and error handling
220
+ * for Next.js (>= 14) API route handlers.
221
+ *
222
+ * @param options - Options to configure the route handler.
223
+ * @param handlerFn - The route handler function.
224
+ *
225
+ * @returns A Next.js API route handler function.
226
+ *
227
+ * @example
228
+ * ```ts
229
+ * import { string } from 'decoders'
230
+ *import { createSafeRouteHandler } from '@sugardarius/anzen'
231
+ * import { auth } from '~/lib/auth'
232
+ *
233
+ * export const GET = createSafeRouteHandler(
234
+ * {
235
+ * id: 'my-safe-route-handler',
236
+ * authorize: async ({ req }) => {
237
+ * const session = await auth.getSession(req)
238
+ * if (!session) {
239
+ * return new Response(null, { status: 401 })
240
+ * }
241
+ *
242
+ * return { user: session.user }
243
+ * },
244
+ * segments: {
245
+ * id: string,
246
+ * },
247
+ * },
248
+ * async ({ auth, segments, }, req): Promise<Response> => {
249
+ * return Response.json({ user: auth.user, segments }, { status: 200 })
250
+ * }
251
+ *)
252
+ * ```
253
+ */
254
+ declare function createSafeRouteHandler<AC extends AuthContext | undefined = undefined, TRouteDynamicSegments extends TSegmentsDict | undefined = undefined, TSearchParams extends TSearchParamsDict | undefined = undefined, TBody extends TBodySchema | undefined = undefined, TFormData extends TFormDataDict | undefined = undefined>(options: CreateSafeRouteHandlerOptions<AC, TRouteDynamicSegments, TSearchParams, TBody, TFormData>, handlerFn: SafeRouteHandler<AC, TRouteDynamicSegments, TSearchParams, TBody, TFormData>): CreateSafeRouteHandlerReturnType;
255
+
256
+ export { type AuthContext, type AuthFunction, type Awaitable, type BaseOptions, type CreateSafeRouteHandlerOptions, type CreateSafeRouteHandlerReturnType, type OnValidationErrorResponse, type RequestExtras, type SafeRouteHandler, type SafeRouteHandlerContext, type TBodySchema, type TFormDataDict, type TSearchParamsDict, type TSegmentsDict, createSafeRouteHandler };
@@ -0,0 +1,256 @@
1
+ /** The Standard Schema interface. */
2
+ interface StandardSchemaV1<Input = unknown, Output = Input> {
3
+ /** The Standard Schema properties. */
4
+ readonly '~standard': StandardSchemaV1.Props<Input, Output>;
5
+ }
6
+ declare namespace StandardSchemaV1 {
7
+ /** The Standard Schema properties interface. */
8
+ interface Props<Input = unknown, Output = Input> {
9
+ /** The version number of the standard. */
10
+ readonly version: 1;
11
+ /** The vendor name of the schema library. */
12
+ readonly vendor: string;
13
+ /** Validates unknown input values. */
14
+ readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>;
15
+ /** Inferred types associated with the schema. */
16
+ readonly types?: Types<Input, Output> | undefined;
17
+ }
18
+ /** The result interface of the validate function. */
19
+ type Result<Output> = SuccessResult<Output> | FailureResult;
20
+ /** The result interface if validation succeeds. */
21
+ interface SuccessResult<Output> {
22
+ /** The typed output value. */
23
+ readonly value: Output;
24
+ /** The non-existent issues. */
25
+ readonly issues?: undefined;
26
+ }
27
+ /** The result interface if validation fails. */
28
+ interface FailureResult {
29
+ /** The issues of failed validation. */
30
+ readonly issues: ReadonlyArray<Issue>;
31
+ }
32
+ /** The issue interface of the failure output. */
33
+ interface Issue {
34
+ /** The error message of the issue. */
35
+ readonly message: string;
36
+ /** The path of the issue, if any. */
37
+ readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
38
+ }
39
+ /** The path segment interface of the issue. */
40
+ interface PathSegment {
41
+ /** The key representing a path segment. */
42
+ readonly key: PropertyKey;
43
+ }
44
+ /** The Standard Schema types interface. */
45
+ interface Types<Input = unknown, Output = Input> {
46
+ /** The input type of the schema. */
47
+ readonly input: Input;
48
+ /** The output type of the schema. */
49
+ readonly output: Output;
50
+ }
51
+ /** Infers the input type of a Standard Schema. */
52
+ type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema['~standard']['types']>['input'];
53
+ /** Infers the output type of a Standard Schema. */
54
+ type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema['~standard']['types']>['output'];
55
+ }
56
+ type StandardSchemaDictionary<Input = Record<string, unknown>, Output extends Record<keyof Input, unknown> = Input> = {
57
+ [K in keyof Input]-?: StandardSchemaV1<Input[K], Output[K]>;
58
+ };
59
+ declare namespace StandardSchemaDictionary {
60
+ type InferInput<T extends StandardSchemaDictionary> = {
61
+ [K in keyof T]: StandardSchemaV1.InferInput<T[K]>;
62
+ };
63
+ type InferOutput<T extends StandardSchemaDictionary> = {
64
+ [K in keyof T]: StandardSchemaV1.InferOutput<T[K]>;
65
+ };
66
+ }
67
+
68
+ type EmptyObjectType = {};
69
+ type UnwrapReadonlyObject<T> = T extends Readonly<infer U> ? U : T;
70
+ type Awaitable<T> = T | PromiseLike<T>;
71
+ type AuthContext = Record<string, unknown>;
72
+ type TSegmentsDict = StandardSchemaDictionary;
73
+ type TSearchParamsDict = StandardSchemaDictionary;
74
+ type TBodySchema = StandardSchemaV1;
75
+ type TFormDataDict = StandardSchemaDictionary;
76
+ type AuthFunction<AC extends AuthContext | undefined> = (input: {
77
+ /**
78
+ * Original request
79
+ */
80
+ readonly req: Request;
81
+ /**
82
+ * Parsed request url
83
+ */
84
+ readonly url: URL;
85
+ }) => Awaitable<AC | Response>;
86
+ type BaseOptions<AC extends AuthContext | undefined> = {
87
+ /**
88
+ * ID for the route handler.
89
+ * Used when logging in development or when `debug` is enabled.
90
+ *
91
+ * You can also use it in the route handler definition to add extra logging
92
+ * or monitoring.
93
+ */
94
+ id?: string;
95
+ /**
96
+ * Function to use to authorize the request.
97
+ * By default it always authorize the request.
98
+ *
99
+ * When returning a response, it will be used as the response for the request.
100
+ * Return a response when the request is not authorized.
101
+ */
102
+ authorize?: AuthFunction<AC>;
103
+ /**
104
+ * Callback triggered when the request fails.
105
+ * By default it returns a simple `500` response and the error is logged into the console.
106
+ *
107
+ * Use it if your handler use custom errors and
108
+ * you want to manage them properly by returning a proper response.
109
+ */
110
+ onErrorResponse?: (err: unknown) => Awaitable<Response>;
111
+ /**
112
+ * Use this options to enable debug mode.
113
+ * It will add logs in the handler to help you debug the request.
114
+ *
115
+ * By default it's `false` for production builds.
116
+ * In development builds, it will be `true` if `NODE_ENV` is not set to `production`.
117
+ */
118
+ debug?: boolean;
119
+ };
120
+ type OnValidationErrorResponse = (issues: readonly StandardSchemaV1.Issue[]) => Awaitable<Response>;
121
+ type CreateSafeRouteHandlerOptions<AC extends AuthContext | undefined, TSegments extends TSegmentsDict | undefined, TSearchParams extends TSearchParamsDict | undefined, TBody extends TBodySchema | undefined, TFormData extends TFormDataDict | undefined> = {
122
+ /**
123
+ * Dynamic route segments used in the route handler path.
124
+ */
125
+ segments?: TSegments;
126
+ /**
127
+ * Callback triggered when dynamic segments validations returned issues.
128
+ * By default it returns a simple `400` response and issues are logged into the console.
129
+ */
130
+ onSegmentsValidationErrorResponse?: OnValidationErrorResponse;
131
+ /**
132
+ * Search params used in the route.
133
+ */
134
+ searchParams?: TSearchParams;
135
+ /**
136
+ * Callback triggered when search params validations returned issues.
137
+ * By default it returns a simple `400` response and issues are logged into the console.
138
+ */
139
+ onSearchParamsValidationErrorResponse?: OnValidationErrorResponse;
140
+ /**
141
+ * Request body.
142
+ *
143
+ * Returns a `405` response if the request method is not `POST`, 'PUT' or 'PATCH'.
144
+ * Returns a `415`response if the request does not explicitly set the `Content-Type` to `application/json`.
145
+ *
146
+ * IMPORTANT: The body is parsed as JSON, so it must be a valid JSON object!
147
+ * IMPORTANT: body shouldn't be used with `formData` at the same time. They are exclusive.
148
+ * Why making the distinction? `formData` is used as a `StandardSchemaDictionary` whereas `body` is used as a `StandardSchemaV1`.
149
+ */
150
+ body?: TBody;
151
+ /**
152
+ * Callback triggered when body validation returned issues.
153
+ * By default it returns a simple `400` response and issues are logged into the console.
154
+ */
155
+ onBodyValidationErrorResponse?: OnValidationErrorResponse;
156
+ /**
157
+ * Request form data.
158
+ *
159
+ * Returns a `405` response if the request method is not `POST`, 'PUT' or 'PATCH'.
160
+ * Returns a `415`response if the request does not explicitly set the `Content-Type` to `multipart/form-data`
161
+ * or to `application/x-www-form-urlencoded`.
162
+ *
163
+ * IMPORTANT: formData shouldn't be used with `body` at the same time. They are exclusive.
164
+ * Why making the distinction? `formData` is used as a `StandardSchemaDictionary` whereas `body` is used as a `StandardSchemaV1`.
165
+ */
166
+ formData?: TFormData;
167
+ /**
168
+ * Callback triggered when form data validation returned issues.
169
+ * By default it returns a simple `400` response and issues are logged into the console.
170
+ */
171
+ onFormDataValidationErrorResponse?: OnValidationErrorResponse;
172
+ } & BaseOptions<AC>;
173
+ type RequestExtras = {
174
+ /**
175
+ * Route dynamic segments as params
176
+ */
177
+ params: Awaitable<any> | undefined;
178
+ };
179
+ type CreateSafeRouteHandlerReturnType = (
180
+ /**
181
+ * Original request
182
+ */
183
+ req: Request,
184
+ /**
185
+ * Extras added by Next.js itself
186
+ */
187
+ extras: RequestExtras) => Promise<Response>;
188
+ type SafeRouteHandlerContext<AC extends AuthContext | undefined, TSegments extends TSegmentsDict | undefined, TSearchParams extends TSearchParamsDict | undefined, TBody extends TBodySchema | undefined, TFormData extends TFormDataDict | undefined> = {
189
+ /**
190
+ * Route handler ID
191
+ */
192
+ readonly id: string;
193
+ /**
194
+ * Parsed request url
195
+ */
196
+ readonly url: URL;
197
+ } & (AC extends AuthContext ? {
198
+ readonly auth: AC;
199
+ } : EmptyObjectType) & (TSegments extends TSegmentsDict ? {
200
+ readonly segments: UnwrapReadonlyObject<StandardSchemaDictionary.InferOutput<TSegments>>;
201
+ } : EmptyObjectType) & (TSearchParams extends TSearchParamsDict ? {
202
+ readonly searchParams: UnwrapReadonlyObject<StandardSchemaDictionary.InferOutput<TSearchParams>>;
203
+ } : EmptyObjectType) & (TBody extends TBodySchema ? {
204
+ readonly body: StandardSchemaV1.InferOutput<TBody>;
205
+ } : EmptyObjectType) & (TFormData extends TFormDataDict ? {
206
+ readonly formData: UnwrapReadonlyObject<StandardSchemaDictionary.InferOutput<TFormData>>;
207
+ } : EmptyObjectType);
208
+ type SafeRouteHandler<AC extends AuthContext | undefined, TSegments extends TSegmentsDict | undefined, TSearchParams extends TSearchParamsDict | undefined, TBody extends TBodySchema | undefined, TFormData extends TFormDataDict | undefined> = (
209
+ /**
210
+ * Safe route handler context
211
+ */
212
+ ctx: SafeRouteHandlerContext<AC, TSegments, TSearchParams, TBody, TFormData>,
213
+ /**
214
+ * Original request
215
+ */
216
+ req: Request) => Promise<Response>;
217
+
218
+ /**
219
+ * Creates a safe route handler with data validation and error handling
220
+ * for Next.js (>= 14) API route handlers.
221
+ *
222
+ * @param options - Options to configure the route handler.
223
+ * @param handlerFn - The route handler function.
224
+ *
225
+ * @returns A Next.js API route handler function.
226
+ *
227
+ * @example
228
+ * ```ts
229
+ * import { string } from 'decoders'
230
+ *import { createSafeRouteHandler } from '@sugardarius/anzen'
231
+ * import { auth } from '~/lib/auth'
232
+ *
233
+ * export const GET = createSafeRouteHandler(
234
+ * {
235
+ * id: 'my-safe-route-handler',
236
+ * authorize: async ({ req }) => {
237
+ * const session = await auth.getSession(req)
238
+ * if (!session) {
239
+ * return new Response(null, { status: 401 })
240
+ * }
241
+ *
242
+ * return { user: session.user }
243
+ * },
244
+ * segments: {
245
+ * id: string,
246
+ * },
247
+ * },
248
+ * async ({ auth, segments, }, req): Promise<Response> => {
249
+ * return Response.json({ user: auth.user, segments }, { status: 200 })
250
+ * }
251
+ *)
252
+ * ```
253
+ */
254
+ declare function createSafeRouteHandler<AC extends AuthContext | undefined = undefined, TRouteDynamicSegments extends TSegmentsDict | undefined = undefined, TSearchParams extends TSearchParamsDict | undefined = undefined, TBody extends TBodySchema | undefined = undefined, TFormData extends TFormDataDict | undefined = undefined>(options: CreateSafeRouteHandlerOptions<AC, TRouteDynamicSegments, TSearchParams, TBody, TFormData>, handlerFn: SafeRouteHandler<AC, TRouteDynamicSegments, TSearchParams, TBody, TFormData>): CreateSafeRouteHandlerReturnType;
255
+
256
+ export { type AuthContext, type AuthFunction, type Awaitable, type BaseOptions, type CreateSafeRouteHandlerOptions, type CreateSafeRouteHandlerReturnType, type OnValidationErrorResponse, type RequestExtras, type SafeRouteHandler, type SafeRouteHandlerContext, type TBodySchema, type TFormDataDict, type TSearchParamsDict, type TSegmentsDict, createSafeRouteHandler };
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ function b(e){return e!==null&&(typeof e=="object"||typeof e=="function")&&typeof e.then=="function"}function T(e,r){if(b(e))throw new Error(r)}function x(e=!1){let r=e||process.env.NODE_ENV!=="production";return{info:(t,...n)=>{r&&console.log(t,...n)},error:(t,...n)=>{r&&console.error(t,...n)},warn:(t,...n)=>{r&&console.warn(t,...n)}}}function D(e,r,t="Validation must be synchronous but schema returned a Promise."){let n=e["~standard"].validate(r);return T(n,t),n}var O=(e,r)=>r in e;function f(e,r){let t={},n=[];for(let u in e){if(!O(e,u))continue;let i=e[u]["~standard"].validate(r[u]);if(T(i,`Validation must be synchronous, but ${u} returned a Promise.`),i.issues){n.push(...i.issues.map(p=>({...p,path:[u,...p.path??[]]})));continue}t[u]=i.value}return n.length>0?{issues:n}:{value:t}}var V="[unknown:route:handler]",k=async e=>{if(e.headers.get("content-type")?.startsWith("application/json"))return await e.json();let t=await e.text();return JSON.parse(t)};function A(e,r){if(e.body&&e.formData)throw new Error("You cannot use both `body` and `formData` in the same route handler. They are both mutually exclusive.");let t=x(e.debug),n=e.id??V,u=e.onErrorResponse??(a=>(t.error(`\u{1F6D1} Unexpected error in route handler '${n}'`,a),new Response("Internal server error",{status:500}))),i=e.onSegmentsValidationErrorResponse??(a=>(t.error(`\u{1F6D1} Invalid segments for route handler '${n}':`,a),new Response("Invalid segments",{status:400}))),p=e.onSearchParamsValidationErrorResponse??(a=>(t.error(`\u{1F6D1} Invalid search params for route handler '${n}':`,a),new Response("Invalid search params",{status:400}))),I=e.onBodyValidationErrorResponse??(a=>(t.error(`\u{1F6D1} Invalid body for route handler '${n}':`,a),new Response("Invalid body",{status:400}))),g=e.onFormDataValidationErrorResponse??(a=>(t.error(`\u{1F6D1} Invalid form data for route handler '${n}':`,a),new Response("Invalid form data",{status:400}))),P=e.authorize??(async()=>{});return async function(a,w){t.info(`\u{1F504} Running route handler '${n}'`),t.info(`\u{1F449}\u{1F3FB} Request url: ${a.url}`);let m=new URL(a.url),l=await P({req:a,url:m});if(l instanceof Response)return t.error(`\u{1F6D1} Request not authorized for route handler '${n}'`),l;let y;if(e.segments){if(w.params===void 0)return new Response("No segments provided",{status:400});let s=await w.params,o=f(e.segments,s);if(o.issues)return await i(o.issues);y=o.value}let h;if(e.searchParams){let s=[...m.searchParams.keys()].map(d=>{let c=m.searchParams.getAll(d);return[d,c.length>1?c:c[0]]}),o=f(e.searchParams,Object.fromEntries(s));if(o.issues)return await p(o.issues);h=o.value}let S;if(e.body){if(!["POST","PUT","PATCH"].includes(a.method))return new Response("Invalid method for request body",{status:405});let s;try{s=await k(a)}catch(d){return await u(d)}let o=D(e.body,s,"Request body validation must be synchronous");if(o.issues)return await I(o.issues);S=o.value}let R;if(e.formData){if(!["POST","PUT","PATCH"].includes(a.method))return new Response("Invalid method for request form data",{status:405});let s=a.headers.get("content-type");if(!s?.startsWith("multipart/form-data")&&!s?.startsWith("application/x-www-form-urlencoded"))return new Response("Invalid content type for request form data",{status:415});let o;try{o=await a.formData()}catch(c){return await u(c)}let d=f(e.formData,Object.fromEntries(o.entries()));if(d.issues)return await g(d.issues);R=d.value}let v={id:n,url:m,...l?{auth:l}:{},...y?{segments:y}:{},...h?{searchParams:h}:{},...S?{body:S}:{},...R?{formData:R}:{}};try{return await r(v,a)}catch(s){return await u(s)}}}export{A as createSafeRouteHandler};
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/utils.ts","../src/standard-schema.ts","../src/create-safe-route-handler.ts"],"sourcesContent":["export function isPromise<T>(\n value: unknown\n): value is Promise<T> | PromiseLike<T> {\n return (\n value !== null &&\n (typeof value === 'object' || typeof value === 'function') &&\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n typeof (value as any).then === 'function'\n )\n}\n\nexport function assertsSyncOperation<T>(\n value: T | Promise<T> | PromiseLike<T>,\n message: string\n): asserts value is T {\n if (isPromise<T>(value)) {\n throw new Error(message)\n }\n}\n\nexport function createLogger(debug: boolean = false) {\n const shouldLog = debug || process.env.NODE_ENV !== 'production'\n return {\n info: (message: string, ...rest: unknown[]): void => {\n if (shouldLog) {\n console.log(message, ...rest)\n }\n },\n error: (message: string, ...rest: unknown[]): void => {\n if (shouldLog) {\n console.error(message, ...rest)\n }\n },\n warn: (message: string, ...rest: unknown[]): void => {\n if (shouldLog) {\n console.warn(message, ...rest)\n }\n },\n }\n}\n","import { assertsSyncOperation } from './utils'\n\n/** The Standard Schema interface. */\nexport interface StandardSchemaV1<Input = unknown, Output = Input> {\n /** The Standard Schema properties. */\n readonly '~standard': StandardSchemaV1.Props<Input, Output>\n}\n\nexport declare namespace StandardSchemaV1 {\n /** The Standard Schema properties interface. */\n export interface Props<Input = unknown, Output = Input> {\n /** The version number of the standard. */\n readonly version: 1\n /** The vendor name of the schema library. */\n readonly vendor: string\n /** Validates unknown input values. */\n readonly validate: (\n value: unknown\n ) => Result<Output> | Promise<Result<Output>>\n /** Inferred types associated with the schema. */\n readonly types?: Types<Input, Output> | undefined\n }\n\n /** The result interface of the validate function. */\n export type Result<Output> = SuccessResult<Output> | FailureResult\n\n /** The result interface if validation succeeds. */\n export interface SuccessResult<Output> {\n /** The typed output value. */\n readonly value: Output\n /** The non-existent issues. */\n readonly issues?: undefined\n }\n\n /** The result interface if validation fails. */\n export interface FailureResult {\n /** The issues of failed validation. */\n readonly issues: ReadonlyArray<Issue>\n }\n\n /** The issue interface of the failure output. */\n export interface Issue {\n /** The error message of the issue. */\n readonly message: string\n /** The path of the issue, if any. */\n readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined\n }\n\n /** The path segment interface of the issue. */\n export interface PathSegment {\n /** The key representing a path segment. */\n readonly key: PropertyKey\n }\n\n /** The Standard Schema types interface. */\n export interface Types<Input = unknown, Output = Input> {\n /** The input type of the schema. */\n readonly input: Input\n /** The output type of the schema. */\n readonly output: Output\n }\n\n /** Infers the input type of a Standard Schema. */\n export type InferInput<Schema extends StandardSchemaV1> = NonNullable<\n Schema['~standard']['types']\n >['input']\n\n /** Infers the output type of a Standard Schema. */\n export type InferOutput<Schema extends StandardSchemaV1> = NonNullable<\n Schema['~standard']['types']\n >['output']\n}\n\nexport function validateWithSchema<TSchema extends StandardSchemaV1>(\n schema: TSchema,\n value: unknown,\n errSyncMsg = 'Validation must be synchronous but schema returned a Promise.'\n): StandardSchemaV1.Result<StandardSchemaV1.InferOutput<TSchema>> {\n const result = schema['~standard'].validate(value)\n assertsSyncOperation(result, errSyncMsg)\n\n return result\n}\n\n// Thanks to `@t3-env/core` (πŸ‘‰πŸ» https://github.com/t3-oss/t3-env/blob/main/packages/core/src/standard.ts)\n// for this awesome dictionary schema.\nexport type StandardSchemaDictionary<\n Input = Record<string, unknown>,\n Output extends Record<keyof Input, unknown> = Input,\n> = {\n [K in keyof Input]-?: StandardSchemaV1<Input[K], Output[K]>\n}\n\nexport namespace StandardSchemaDictionary {\n export type InferInput<T extends StandardSchemaDictionary> = {\n [K in keyof T]: StandardSchemaV1.InferInput<T[K]>\n }\n export type InferOutput<T extends StandardSchemaDictionary> = {\n [K in keyof T]: StandardSchemaV1.InferOutput<T[K]>\n }\n}\n\nconst hasDictKey = <T extends object, K extends PropertyKey>(\n obj: T,\n key: K\n): obj is T & Record<typeof key, unknown> => {\n return key in obj\n}\n\nexport function parseWithDictionary<TDict extends StandardSchemaDictionary>(\n dictionary: TDict,\n value: Record<string, unknown>\n): StandardSchemaV1.Result<StandardSchemaDictionary.InferOutput<TDict>> {\n const result: Record<string, unknown> = {}\n const issues: StandardSchemaV1.Issue[] = []\n\n for (const key in dictionary) {\n if (!hasDictKey(dictionary, key)) {\n continue\n }\n // NOTE: safe to assert as we're ensuring just before key isn't undefined\n const propResult = dictionary[key]!['~standard'].validate(value[key])\n\n assertsSyncOperation(\n propResult,\n `Validation must be synchronous, but ${key} returned a Promise.`\n )\n\n if (propResult.issues) {\n issues.push(\n ...propResult.issues.map((issue) => ({\n ...issue,\n path: [key, ...(issue.path ?? [])],\n }))\n )\n continue\n }\n result[key] = propResult.value\n }\n\n if (issues.length > 0) {\n return { issues }\n }\n\n return { value: result as never }\n}\n","import { createLogger } from './utils'\nimport {\n parseWithDictionary,\n validateWithSchema,\n type StandardSchemaV1,\n} from './standard-schema'\nimport type {\n Awaitable,\n AuthContext,\n TSegmentsDict,\n TSearchParamsDict,\n TBodySchema,\n TFormDataDict,\n RequestExtras,\n CreateSafeRouteHandlerOptions,\n CreateSafeRouteHandlerReturnType,\n SafeRouteHandler,\n SafeRouteHandlerContext,\n} from './types'\n\n/** @internal exported for testing only */\nexport const DEFAULT_ID = '[unknown:route:handler]'\n\n/**\n * @internal\n *\n * Reads the request body as JSON.\n * If it fails, it calls the `onErrorResponse` callback.\n */\nconst readRequestBodyAsJson = async (req: Request): Promise<unknown> => {\n const contentType = req.headers.get('content-type')\n if (contentType?.startsWith('application/json')) {\n return await req.json()\n }\n\n const text = await req.text()\n return JSON.parse(text)\n}\n\n/**\n * Creates a safe route handler with data validation and error handling\n * for Next.js (>= 14) API route handlers.\n *\n * @param options - Options to configure the route handler.\n * @param handlerFn - The route handler function.\n *\n * @returns A Next.js API route handler function.\n *\n * @example\n * ```ts\n * import { string } from 'decoders'\n *import { createSafeRouteHandler } from '@sugardarius/anzen'\n * import { auth } from '~/lib/auth'\n *\n * export const GET = createSafeRouteHandler(\n * {\n * id: 'my-safe-route-handler',\n * authorize: async ({ req }) => {\n * const session = await auth.getSession(req)\n * if (!session) {\n * return new Response(null, { status: 401 })\n * }\n *\n * return { user: session.user }\n * },\n * segments: {\n * id: string,\n * },\n * },\n * async ({ auth, segments, }, req): Promise<Response> => {\n * return Response.json({ user: auth.user, segments }, { status: 200 })\n * }\n *)\n * ```\n */\nexport function createSafeRouteHandler<\n AC extends AuthContext | undefined = undefined,\n TRouteDynamicSegments extends TSegmentsDict | undefined = undefined,\n TSearchParams extends TSearchParamsDict | undefined = undefined,\n TBody extends TBodySchema | undefined = undefined,\n TFormData extends TFormDataDict | undefined = undefined,\n>(\n options: CreateSafeRouteHandlerOptions<\n AC,\n TRouteDynamicSegments,\n TSearchParams,\n TBody,\n TFormData\n >,\n handlerFn: SafeRouteHandler<\n AC,\n TRouteDynamicSegments,\n TSearchParams,\n TBody,\n TFormData\n >\n): CreateSafeRouteHandlerReturnType {\n // NOTE: `body` and `formData` options are mutually exclusive 🎭\n if (options.body && options.formData) {\n throw new Error(\n 'You cannot use both `body` and `formData` in the same route handler. They are both mutually exclusive.'\n )\n }\n\n const log = createLogger(options.debug)\n const id = options.id ?? DEFAULT_ID\n\n const onErrorResponse =\n options.onErrorResponse ??\n ((err: unknown): Awaitable<Response> => {\n log.error(`πŸ›‘ Unexpected error in route handler '${id}'`, err)\n return new Response('Internal server error', {\n status: 500,\n })\n })\n\n const onSegmentsValidationErrorResponse =\n options.onSegmentsValidationErrorResponse ??\n ((issues: readonly StandardSchemaV1.Issue[]): Awaitable<Response> => {\n log.error(`πŸ›‘ Invalid segments for route handler '${id}':`, issues)\n return new Response('Invalid segments', {\n status: 400,\n })\n })\n\n const onSearchParamsValidationErrorResponse =\n options.onSearchParamsValidationErrorResponse ??\n ((issues: readonly StandardSchemaV1.Issue[]): Awaitable<Response> => {\n log.error(`πŸ›‘ Invalid search params for route handler '${id}':`, issues)\n return new Response('Invalid search params', {\n status: 400,\n })\n })\n\n const onBodyValidationErrorResponse =\n options.onBodyValidationErrorResponse ??\n ((issues: readonly StandardSchemaV1.Issue[]): Awaitable<Response> => {\n log.error(`πŸ›‘ Invalid body for route handler '${id}':`, issues)\n return new Response('Invalid body', {\n status: 400,\n })\n })\n\n const onFormDataValidationErrorResponse =\n options.onFormDataValidationErrorResponse ??\n ((issues: readonly StandardSchemaV1.Issue[]): Awaitable<Response> => {\n log.error(`πŸ›‘ Invalid form data for route handler '${id}':`, issues)\n return new Response('Invalid form data', {\n status: 400,\n })\n })\n\n const authorize = options.authorize ?? (async () => undefined)\n\n // Next.js API Route handler declaration\n return async function (\n req: Request,\n extras: RequestExtras\n ): Promise<Response> {\n log.info(`πŸ”„ Running route handler '${id}'`)\n log.info(`πŸ‘‰πŸ» Request url: ${req.url}`)\n\n const url = new URL(req.url)\n\n const authOrResponse = await authorize({ req, url })\n if (authOrResponse instanceof Response) {\n log.error(`πŸ›‘ Request not authorized for route handler '${id}'`)\n return authOrResponse\n }\n\n let segments = undefined\n if (options.segments) {\n if (extras.params === undefined) {\n return new Response('No segments provided', { status: 400 })\n }\n\n const params = await extras.params\n const parsedSegments = parseWithDictionary(options.segments, params)\n\n if (parsedSegments.issues) {\n return await onSegmentsValidationErrorResponse(parsedSegments.issues)\n }\n\n segments = parsedSegments.value\n }\n\n let searchParams = undefined\n if (options.searchParams) {\n const queryParams_unsafe = [...url.searchParams.keys()].map((k) => {\n const values = url.searchParams.getAll(k)\n return [k, values.length > 1 ? values : values[0]]\n })\n\n const parsedSearchParams = parseWithDictionary(\n options.searchParams,\n Object.fromEntries(queryParams_unsafe)\n )\n\n if (parsedSearchParams.issues) {\n return await onSearchParamsValidationErrorResponse(\n parsedSearchParams.issues\n )\n }\n\n searchParams = parsedSearchParams.value\n }\n\n let body = undefined\n if (options.body) {\n if (!['POST', 'PUT', 'PATCH'].includes(req.method)) {\n return new Response('Invalid method for request body', {\n status: 405,\n })\n }\n\n let body_unsafe: unknown\n try {\n body_unsafe = await readRequestBodyAsJson(req)\n } catch (err) {\n return await onErrorResponse(err)\n }\n\n const parsedBody = validateWithSchema(\n options.body,\n body_unsafe,\n 'Request body validation must be synchronous'\n )\n\n if (parsedBody.issues) {\n return await onBodyValidationErrorResponse(parsedBody.issues)\n }\n\n body = parsedBody.value\n }\n\n let formData = undefined\n if (options.formData) {\n if (!['POST', 'PUT', 'PATCH'].includes(req.method)) {\n return new Response('Invalid method for request form data', {\n status: 405,\n })\n }\n\n const contentType = req.headers.get('content-type')\n if (\n !contentType?.startsWith('multipart/form-data') &&\n !contentType?.startsWith('application/x-www-form-urlencoded')\n ) {\n return new Response('Invalid content type for request form data', {\n status: 415,\n })\n }\n\n let formData_unsafe: FormData\n try {\n formData_unsafe = await req.formData() // NOTE: πŸ€” maybe find a better way to counted the deprecation warning?\n } catch (err) {\n return await onErrorResponse(err)\n }\n\n const parsedFormData = parseWithDictionary(\n options.formData,\n Object.fromEntries(formData_unsafe.entries())\n )\n\n if (parsedFormData.issues) {\n return await onFormDataValidationErrorResponse(parsedFormData.issues)\n }\n\n formData = parsedFormData.value\n }\n\n // Build safe route handler context\n const ctx = {\n id,\n url,\n ...(authOrResponse ? { auth: authOrResponse } : {}),\n ...(segments ? { segments } : {}),\n ...(searchParams ? { searchParams } : {}),\n ...(body ? { body } : {}),\n ...(formData ? { formData } : {}),\n } as SafeRouteHandlerContext<\n AC,\n TRouteDynamicSegments,\n TSearchParams,\n TBody,\n TFormData\n >\n\n // Let's catch any error that might happen in the handler\n try {\n return await handlerFn(ctx, req)\n } catch (err) {\n return await onErrorResponse(err)\n }\n }\n}\n"],"mappings":"AAAO,SAASA,EACdC,EACsC,CACtC,OACEA,IAAU,OACT,OAAOA,GAAU,UAAY,OAAOA,GAAU,aAE/C,OAAQA,EAAc,MAAS,UAEnC,CAEO,SAASC,EACdD,EACAE,EACoB,CACpB,GAAIH,EAAaC,CAAK,EACpB,MAAM,IAAI,MAAME,CAAO,CAE3B,CAEO,SAASC,EAAaC,EAAiB,GAAO,CACnD,IAAMC,EAAYD,GAAS,QAAQ,IAAI,WAAa,aACpD,MAAO,CACL,KAAM,CAACF,KAAoBI,IAA0B,CAC/CD,GACF,QAAQ,IAAIH,EAAS,GAAGI,CAAI,CAEhC,EACA,MAAO,CAACJ,KAAoBI,IAA0B,CAChDD,GACF,QAAQ,MAAMH,EAAS,GAAGI,CAAI,CAElC,EACA,KAAM,CAACJ,KAAoBI,IAA0B,CAC/CD,GACF,QAAQ,KAAKH,EAAS,GAAGI,CAAI,CAEjC,CACF,CACF,CCkCO,SAASC,EACdC,EACAC,EACAC,EAAa,gEACmD,CAChE,IAAMC,EAASH,EAAO,WAAW,EAAE,SAASC,CAAK,EACjD,OAAAG,EAAqBD,EAAQD,CAAU,EAEhCC,CACT,CAoBA,IAAME,EAAa,CACjBC,EACAC,IAEOA,KAA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AC5B,CAGA,IAAMC,EAAM,CACV,GAAA7B,EACA,IAAAU,EACA,GAAIC,EAAiB,CAAE,KAAMA,CAAe,EAAI,CAAC,EACjD,GAAIC,EAAW,CAAE,SAAAA,CAAS,EAAI,CAAC,EAC/B,GAAII,EAAe,CAAE,aAAAA,CAAa,EAAI,CAAC,EACvC,GAAIK,EAAO,CAAE,KAAAA,CAAK,EAAI,CAAC,EACvB,GAAII,EAAW,CAAE,SAAAA,CAAS,EAAI,CAAC,CACjC,EASA,GAAI,CACF,OAAO,MAAM5B,EAAUgC,EAAKpC,CAAG,CACjC,OAASS,EAAK,CACZ,OAAO,MAAMD,EAAgBC,CAAG,CAClC,CACF,CACF","names":["isPromise","value","assertsSyncOperation","message","createLogger","debug","shouldLog","rest","validateWithSchema","schema","value","errSyncMsg","result","assertsSyncOperation","hasDictKey","obj","key","parseWithDictionary","dictionary","issues","propResult","issue","DEFAULT_ID","readRequestBodyAsJson","req","text","createSafeRouteHandler","options","handlerFn","log","createLogger","id","onErrorResponse","err","onSegmentsValidationErrorResponse","issues","onSearchParamsValidationErrorResponse","onBodyValidationErrorResponse","onFormDataValidationErrorResponse","authorize","extras","url","authOrResponse","segments","params","parsedSegments","parseWithDictionary","searchParams","queryParams_unsafe","k","values","parsedSearchParams","body","body_unsafe","parsedBody","validateWithSchema","formData","contentType","formData_unsafe","parsedFormData","ctx"]}
package/package.json ADDED
@@ -0,0 +1,84 @@
1
+ {
2
+ "name": "@sugardarius/anzen",
3
+ "version": "0.1.1",
4
+ "description": "A fast, framework validation agnostic, type-safe factory for creating Next.JS App Router route handlers.",
5
+ "license": "MIT",
6
+ "packageManager": "npm@11.3.0",
7
+ "workspaces": [
8
+ ".",
9
+ "www"
10
+ ],
11
+ "type": "module",
12
+ "sideEffects": false,
13
+ "main": "./dist/index.cjs",
14
+ "types": "./dist/index.d.cts",
15
+ "exports": {
16
+ ".": {
17
+ "import": {
18
+ "types": "./dist/index.d.ts",
19
+ "default": "./dist/index.js"
20
+ },
21
+ "require": {
22
+ "types": "./dist/index.d.cts",
23
+ "module": "./dist/index.js",
24
+ "default": "./dist/index.cjs"
25
+ }
26
+ }
27
+ },
28
+ "files": [
29
+ "dist/**",
30
+ "README.md",
31
+ "LICENSE"
32
+ ],
33
+ "author": {
34
+ "name": "AurΓ©lien Dupays Dexemple",
35
+ "url": "https://aureliendupaysdexemple.com"
36
+ },
37
+ "repository": {
38
+ "type": "git",
39
+ "url": "git+https://github.com/sugardarius/anzen.git"
40
+ },
41
+ "bugs": {
42
+ "url": "https://github.com/sugardarius/anzen/issues"
43
+ },
44
+ "keywords": [
45
+ "nextjs",
46
+ "safe route handlers",
47
+ "framework agnostic validation"
48
+ ],
49
+ "publishConfig": {
50
+ "access": "public"
51
+ },
52
+ "scripts": {
53
+ "dev": "tsup --watch",
54
+ "build": "tsup --minify",
55
+ "dev:www": "turbo run dev --filter=www",
56
+ "build:www": "turbo run build --filter=www",
57
+ "test": "vitest run --silent",
58
+ "lint": "npm run lint:eslint && npm run lint:tsc && npm run lint:package",
59
+ "lint:eslint": "eslint src/",
60
+ "lint:tsc": "tsc --noEmit",
61
+ "lint:package": "publint --strict && attw --pack",
62
+ "format": "prettier --write src/",
63
+ "release": "release-it"
64
+ },
65
+ "peerDependencies": {
66
+ "next": "^14 || ^15",
67
+ "typescript": "^5"
68
+ },
69
+ "devDependencies": {
70
+ "@arethetypeswrong/cli": "^0.17.4",
71
+ "@eslint/js": "^9.25.1",
72
+ "@release-it/keep-a-changelog": "^7.0.0",
73
+ "@types/node": "^22.15.3",
74
+ "eslint": "^9.25.1",
75
+ "prettier": "^3.5.3",
76
+ "publint": "^0.3.12",
77
+ "release-it": "^19.0.1",
78
+ "tsup": "^8.4.0",
79
+ "turbo": "^2.5.2",
80
+ "typescript": "^5.8.3",
81
+ "typescript-eslint": "^8.31.1",
82
+ "vitest": "^3.1.2"
83
+ }
84
+ }