@sugardarius/anzen 1.0.1 → 1.1.0

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/README.md CHANGED
@@ -113,6 +113,27 @@ export const VERB = createSafeRouteHandler(
113
113
  ): Promise<Response> => Response.json({}))
114
114
  ```
115
115
 
116
+ ### Using `NextRequest` type
117
+
118
+ By default the factory uses the native `Request` type. If you want to use the `NextRequest` type from [Next.js](https://nextjs.org/), you can do it by passing the `NextRequest` type as a generic argument to the factory.
119
+
120
+ ```tsx
121
+ import { NextRequest } from 'next/server'
122
+ import { createSafeRouteHandler } from '@sugardarius/anzen'
123
+
124
+ // Use `NextRequest` type as first generic argument
125
+ export const GET = createSafeRouteHandler<NextRequest>(
126
+ { id: 'next/request' },
127
+ async (
128
+ ctx,
129
+ req // Request is now of type `NextRequest`
130
+ ) => {
131
+ console.log('pathname', req.nextUrl.pathname)
132
+ return new Response(null, 200)
133
+ }
134
+ )
135
+ ```
136
+
116
137
  ### Base options
117
138
 
118
139
  When creating a safe route handler you can use a bunch of options for helping you achieve different tasks 👇🏻
@@ -125,7 +146,7 @@ By default the id is set to `[unknown:route:handler]`
125
146
  ```tsx
126
147
  export const POST = createSafeRouteHandler(
127
148
  {
128
- id: 'my-safe-route-handler',
149
+ id: 'auth/login',
129
150
  },
130
151
  async ({ id }) => {
131
152
  return Response.json({ id })
@@ -161,6 +182,8 @@ export const GET = createSafeRouteHandler(
161
182
  )
162
183
  ```
163
184
 
185
+ The original is cloned from the incoming request to avoid side effects and to make it consumable in the `authorize` function.
186
+
164
187
  #### `onErrorResponse?: (err: unknown) => Awaitable<Response>`
165
188
 
166
189
  Callback triggered when the request fails.
@@ -287,7 +310,7 @@ export const GET = createSafeRouteHandler(
287
310
 
288
311
  Request body.
289
312
 
290
- Returns a `405` response if the request method is not `POST`, 'PUT' or 'PATCH'.
313
+ Returns a `405` response if the request method is not `POST`, `PUT` or `PATCH`.
291
314
 
292
315
  Returns a `415`response if the request does not explicitly set the `Content-Type` to `application/json`.
293
316
 
@@ -313,6 +336,8 @@ export const POST = createSafeRouteHandler(
313
336
  )
314
337
  ```
315
338
 
339
+ > When validating the body the request is cloned to let you consume the body in the original request (e.g second arguments of handler function).
340
+
316
341
  #### `onBodyValidationErrorResponse?: OnValidationErrorResponse`
317
342
 
318
343
  Callback triggered when body validation returned issues. By default it returns a simple `400` response and issues are logged into the console.
@@ -342,7 +367,7 @@ export const POST = createSafeRouteHandler(
342
367
 
343
368
  Request form data.
344
369
 
345
- Returns a `405` response if the request method is not `POST`, 'PUT' or 'PATCH'.
370
+ Returns a `405` response if the request method is not `POST`, `PUT` or `PATCH`.
346
371
 
347
372
  Returns a `415`response if the request does not explicitly set the `Content-Type` to `multipart/form-data` or to `application/x-www-form-urlencoded`.
348
373
 
@@ -367,6 +392,8 @@ export const POST = createSafeRouteHandler(
367
392
  )
368
393
  ```
369
394
 
395
+ > When validating the form data the request is cloned to let you consume the form data in the original request (e.g second arguments of handler function).
396
+
370
397
  #### `onFormDataValidationErrorResponse?: OnValidationErrorResponse`
371
398
 
372
399
  Callback triggered when form data validation returned issues. By default it returns a simple `400` response and issues are logged into the console.
package/dist/index.cjs CHANGED
@@ -1,2 +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;
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 E(e){return e!==null&&(typeof e=="object"||typeof e=="function")&&typeof e.then=="function"}function w(e,a){if(E(e))throw new Error(a)}function g(e=!1){let a=e||process.env.NODE_ENV!=="production";return{info:(t,...n)=>{a&&console.log(t,...n)},error:(t,...n)=>{a&&console.error(t,...n)},warn:(t,...n)=>{a&&console.warn(t,...n)}}}function D(){let e=null,a=null;return{start:()=>{e=performance.now()},stop:()=>{if(e===null)throw new Error("Execution clock was not started.");a=performance.now()},get:()=>{if(!e||!a)throw new Error("Execution clock has not been started or stopped.");return`${(a-e).toFixed(2)}ms`}}}function I(e,a,t="Validation must be synchronous but schema returned a Promise."){let n=e["~standard"].validate(a);return w(n,t),n}var A=(e,a)=>a in e;function h(e,a){let t={},n=[];for(let u in e){if(!A(e,u))continue;let i=e[u]["~standard"].validate(a[u]);if(w(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 C="[unknown:route:handler]",q=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 F(e,a){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=g(e.debug),n=_nullishCoalesce(e.id, () => (C)),u=_nullishCoalesce(e.onErrorResponse, () => ((r=>(t.error(`\u{1F6D1} Unexpected error in route handler '${n}'`,r),new Response("Internal server error",{status:500}))))),i=_nullishCoalesce(e.onSegmentsValidationErrorResponse, () => ((r=>(t.error(`\u{1F6D1} Invalid segments for route handler '${n}':`,r),new Response("Invalid segments",{status:400}))))),p=_nullishCoalesce(e.onSearchParamsValidationErrorResponse, () => ((r=>(t.error(`\u{1F6D1} Invalid search params for route handler '${n}':`,r),new Response("Invalid search params",{status:400}))))),P=_nullishCoalesce(e.onBodyValidationErrorResponse, () => ((r=>(t.error(`\u{1F6D1} Invalid body for route handler '${n}':`,r),new Response("Invalid body",{status:400}))))),v=_nullishCoalesce(e.onFormDataValidationErrorResponse, () => ((r=>(t.error(`\u{1F6D1} Invalid form data for route handler '${n}':`,r),new Response("Invalid form data",{status:400}))))),b=_nullishCoalesce(e.authorize, () => ((async()=>{})));return async function(r,O){let c=D();c.start(),t.info(`\u{1F504} Running route handler '${n}'`),t.info(`\u{1F449}\u{1F3FB} Request ${r.method} ${r.url}`);let m=new URL(r.url),k=r.clone(),f=await b({req:k,url:m});if(f instanceof Response)return t.error(`\u{1F6D1} Request not authorized for route handler '${n}'`),f;let y;if(e.segments){let o=await O.params;if(o===void 0)return new Response("No segments provided",{status:400});let s=h(e.segments,o);if(s.issues)return await i(s.issues);y=s.value}let S;if(e.searchParams){let o=[...m.searchParams.keys()].map(d=>{let l=m.searchParams.getAll(d);return[d,l.length>1?l:l[0]]}),s=h(e.searchParams,Object.fromEntries(o));if(s.issues)return await p(s.issues);S=s.value}let x=r.clone(),R;if(e.body){if(!["POST","PUT","PATCH"].includes(r.method))return new Response("Invalid method for request body",{status:405});let o;try{o=await q(x)}catch(d){return await u(d)}let s=I(e.body,o,"Request body validation must be synchronous");if(s.issues)return await P(s.issues);R=s.value}let T;if(e.formData){if(!["POST","PUT","PATCH"].includes(r.method))return new Response("Invalid method for request form data",{status:405});let o=r.headers.get("content-type");if(!_optionalChain([o, 'optionalAccess', _6 => _6.startsWith, 'call', _7 => _7("multipart/form-data")])&&!_optionalChain([o, '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 s;try{s=await x.formData()}catch(l){return await u(l)}let d=h(e.formData,Object.fromEntries(s.entries()));if(d.issues)return await v(d.issues);T=d.value}let V={id:n,url:m,...f?{auth:f}:{},...y?{segments:y}:{},...S?{searchParams:S}:{},...R?{body:R}:{},...T?{formData:T}:{}};try{let o=await a(V,r);return c.stop(),t.info(`\u2705 Route handler '${n}' executed successfully in ${c.get()}`),o}catch(o){return c.stop(),t.error(`\u{1F6D1} Route handle '${n} failed to execute after ${c.get()}'`),await u(o)}}}exports.createSafeRouteHandler = F;
2
2
  //# sourceMappingURL=index.cjs.map
@@ -1 +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,E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,MAAA,QAAA,CACLH,CAAAA,CACAc,CAAAA,CACmB,CACnBT,CAAAA,CAAI,IAAA,CAAK,CAAA,iCAAA,EAA6BC,CAAE,CAAA,CAAA,CAAG,CAAA,CAC3CD,CAAAA,CAAI,IAAA,CAAK,CAAA,gCAAA,EAAqBL,CAAAA,CAAI,GAAG,CAAA,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"]}
1
+ {"version":3,"sources":["../src/utils.ts"],"names":["isPromise","value","assertsSyncOperation","message","createLogger","debug","shouldLog","rest","createExecutionClock","startTime","endTime"],"mappings":"AAAO,0rBAASA,CAAAA,CACdC,CAAAA,CACsC,CACtC,OACEA,CAAAA,GAAU,IAAA,EAAA,C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AAA,CAAI,CAC5B,CAAA,CACA,GAAA,CAAK,CAAA,CAAA,EAAc,CACjB,EAAA,CAAI,CAACD,CAAAA,EAAa,CAACC,CAAAA,CACjB,MAAM,IAAI,KAAA,CAAM,kDAAkD,CAAA,CAIpE,MAAO,CAAA,EAAA","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\nexport function createExecutionClock() {\n let startTime: number | null = null\n let endTime: number | null = null\n\n return {\n start: (): void => {\n startTime = performance.now()\n },\n stop: (): void => {\n if (startTime === null) {\n throw new Error('Execution clock was not started.')\n }\n endTime = performance.now()\n },\n get: (): string => {\n if (!startTime || !endTime) {\n throw new Error('Execution clock has not been started or stopped.')\n }\n\n const duration = endTime - startTime\n return `${duration.toFixed(2)}ms`\n },\n }\n}\n"]}
package/dist/index.d.cts CHANGED
@@ -73,17 +73,20 @@ type TSegmentsDict = StandardSchemaDictionary;
73
73
  type TSearchParamsDict = StandardSchemaDictionary;
74
74
  type TBodySchema = StandardSchemaV1;
75
75
  type TFormDataDict = StandardSchemaDictionary;
76
- type AuthFunction<AC extends AuthContext | undefined> = (input: {
76
+ type AuthFunction<TReq extends Request, AC extends AuthContext | undefined> = (input: {
77
77
  /**
78
78
  * Original request
79
+ *
80
+ * Cloned from the incoming request to avoid side effects
81
+ * and to make it consumable in the `authorize` function.
79
82
  */
80
- readonly req: Request;
83
+ readonly req: TReq;
81
84
  /**
82
85
  * Parsed request url
83
86
  */
84
87
  readonly url: URL;
85
88
  }) => Awaitable<AC | Response>;
86
- type BaseOptions<AC extends AuthContext | undefined> = {
89
+ type BaseOptions<TReq extends Request, AC extends AuthContext | undefined> = {
87
90
  /**
88
91
  * ID for the route handler.
89
92
  * Used when logging in development or when `debug` is enabled.
@@ -98,7 +101,7 @@ type BaseOptions<AC extends AuthContext | undefined> = {
98
101
  * When returning a response, it will be used as the response for the request.
99
102
  * Return a response when the request is not authorized.
100
103
  */
101
- authorize?: AuthFunction<AC>;
104
+ authorize?: AuthFunction<TReq, AC>;
102
105
  /**
103
106
  * Callback triggered when the request fails.
104
107
  * By default it returns a simple `500` response and the error is logged into the console.
@@ -117,7 +120,7 @@ type BaseOptions<AC extends AuthContext | undefined> = {
117
120
  debug?: boolean;
118
121
  };
119
122
  type OnValidationErrorResponse = (issues: readonly StandardSchemaV1.Issue[]) => Awaitable<Response>;
120
- type CreateSafeRouteHandlerOptions<AC extends AuthContext | undefined, TSegments extends TSegmentsDict | undefined, TSearchParams extends TSearchParamsDict | undefined, TBody extends TBodySchema | undefined, TFormData extends TFormDataDict | undefined> = {
123
+ type CreateSafeRouteHandlerOptions<TReq extends Request, AC extends AuthContext | undefined, TSegments extends TSegmentsDict | undefined, TSearchParams extends TSearchParamsDict | undefined, TBody extends TBodySchema | undefined, TFormData extends TFormDataDict | undefined> = {
121
124
  /**
122
125
  * Dynamic route segments used for the route handler path.
123
126
  * By design it will handler if the segments are a `Promise` or not.
@@ -173,18 +176,18 @@ type CreateSafeRouteHandlerOptions<AC extends AuthContext | undefined, TSegments
173
176
  * By default it returns a simple `400` response and issues are logged into the console.
174
177
  */
175
178
  onFormDataValidationErrorResponse?: OnValidationErrorResponse;
176
- } & BaseOptions<AC>;
179
+ } & BaseOptions<TReq, AC>;
177
180
  type RequestExtras = {
178
181
  /**
179
182
  * Route dynamic segments as params
180
183
  */
181
184
  params: Awaitable<any> | undefined;
182
185
  };
183
- type CreateSafeRouteHandlerReturnType = (
186
+ type CreateSafeRouteHandlerReturnType<TReq extends Request> = (
184
187
  /**
185
188
  * Original request
186
189
  */
187
- req: Request,
190
+ req: TReq,
188
191
  /**
189
192
  * Extras added by Next.js itself
190
193
  */
@@ -224,7 +227,7 @@ type SafeRouteHandlerContext<AC extends AuthContext | undefined, TSegments exten
224
227
  */
225
228
  readonly formData: UnwrapReadonlyObject<StandardSchemaDictionary.InferOutput<TFormData>>;
226
229
  } : EmptyObjectType);
227
- type SafeRouteHandler<AC extends AuthContext | undefined, TSegments extends TSegmentsDict | undefined, TSearchParams extends TSearchParamsDict | undefined, TBody extends TBodySchema | undefined, TFormData extends TFormDataDict | undefined> = (
230
+ type SafeRouteHandler<TReq extends Request, AC extends AuthContext | undefined, TSegments extends TSegmentsDict | undefined, TSearchParams extends TSearchParamsDict | undefined, TBody extends TBodySchema | undefined, TFormData extends TFormDataDict | undefined> = (
228
231
  /**
229
232
  * Safe route handler context
230
233
  */
@@ -232,7 +235,7 @@ ctx: SafeRouteHandlerContext<AC, TSegments, TSearchParams, TBody, TFormData>,
232
235
  /**
233
236
  * Original request
234
237
  */
235
- req: Request) => Promise<Response>;
238
+ req: TReq) => Promise<Response>;
236
239
 
237
240
  /**
238
241
  * Creates a safe route handler with data validation and error handling
@@ -270,6 +273,6 @@ req: Request) => Promise<Response>;
270
273
  *)
271
274
  * ```
272
275
  */
273
- 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;
276
+ declare function createSafeRouteHandler<TReq extends Request = Request, 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<TReq, AC, TRouteDynamicSegments, TSearchParams, TBody, TFormData>, handlerFn: SafeRouteHandler<TReq, AC, TRouteDynamicSegments, TSearchParams, TBody, TFormData>): CreateSafeRouteHandlerReturnType<TReq>;
274
277
 
275
278
  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.d.ts CHANGED
@@ -73,17 +73,20 @@ type TSegmentsDict = StandardSchemaDictionary;
73
73
  type TSearchParamsDict = StandardSchemaDictionary;
74
74
  type TBodySchema = StandardSchemaV1;
75
75
  type TFormDataDict = StandardSchemaDictionary;
76
- type AuthFunction<AC extends AuthContext | undefined> = (input: {
76
+ type AuthFunction<TReq extends Request, AC extends AuthContext | undefined> = (input: {
77
77
  /**
78
78
  * Original request
79
+ *
80
+ * Cloned from the incoming request to avoid side effects
81
+ * and to make it consumable in the `authorize` function.
79
82
  */
80
- readonly req: Request;
83
+ readonly req: TReq;
81
84
  /**
82
85
  * Parsed request url
83
86
  */
84
87
  readonly url: URL;
85
88
  }) => Awaitable<AC | Response>;
86
- type BaseOptions<AC extends AuthContext | undefined> = {
89
+ type BaseOptions<TReq extends Request, AC extends AuthContext | undefined> = {
87
90
  /**
88
91
  * ID for the route handler.
89
92
  * Used when logging in development or when `debug` is enabled.
@@ -98,7 +101,7 @@ type BaseOptions<AC extends AuthContext | undefined> = {
98
101
  * When returning a response, it will be used as the response for the request.
99
102
  * Return a response when the request is not authorized.
100
103
  */
101
- authorize?: AuthFunction<AC>;
104
+ authorize?: AuthFunction<TReq, AC>;
102
105
  /**
103
106
  * Callback triggered when the request fails.
104
107
  * By default it returns a simple `500` response and the error is logged into the console.
@@ -117,7 +120,7 @@ type BaseOptions<AC extends AuthContext | undefined> = {
117
120
  debug?: boolean;
118
121
  };
119
122
  type OnValidationErrorResponse = (issues: readonly StandardSchemaV1.Issue[]) => Awaitable<Response>;
120
- type CreateSafeRouteHandlerOptions<AC extends AuthContext | undefined, TSegments extends TSegmentsDict | undefined, TSearchParams extends TSearchParamsDict | undefined, TBody extends TBodySchema | undefined, TFormData extends TFormDataDict | undefined> = {
123
+ type CreateSafeRouteHandlerOptions<TReq extends Request, AC extends AuthContext | undefined, TSegments extends TSegmentsDict | undefined, TSearchParams extends TSearchParamsDict | undefined, TBody extends TBodySchema | undefined, TFormData extends TFormDataDict | undefined> = {
121
124
  /**
122
125
  * Dynamic route segments used for the route handler path.
123
126
  * By design it will handler if the segments are a `Promise` or not.
@@ -173,18 +176,18 @@ type CreateSafeRouteHandlerOptions<AC extends AuthContext | undefined, TSegments
173
176
  * By default it returns a simple `400` response and issues are logged into the console.
174
177
  */
175
178
  onFormDataValidationErrorResponse?: OnValidationErrorResponse;
176
- } & BaseOptions<AC>;
179
+ } & BaseOptions<TReq, AC>;
177
180
  type RequestExtras = {
178
181
  /**
179
182
  * Route dynamic segments as params
180
183
  */
181
184
  params: Awaitable<any> | undefined;
182
185
  };
183
- type CreateSafeRouteHandlerReturnType = (
186
+ type CreateSafeRouteHandlerReturnType<TReq extends Request> = (
184
187
  /**
185
188
  * Original request
186
189
  */
187
- req: Request,
190
+ req: TReq,
188
191
  /**
189
192
  * Extras added by Next.js itself
190
193
  */
@@ -224,7 +227,7 @@ type SafeRouteHandlerContext<AC extends AuthContext | undefined, TSegments exten
224
227
  */
225
228
  readonly formData: UnwrapReadonlyObject<StandardSchemaDictionary.InferOutput<TFormData>>;
226
229
  } : EmptyObjectType);
227
- type SafeRouteHandler<AC extends AuthContext | undefined, TSegments extends TSegmentsDict | undefined, TSearchParams extends TSearchParamsDict | undefined, TBody extends TBodySchema | undefined, TFormData extends TFormDataDict | undefined> = (
230
+ type SafeRouteHandler<TReq extends Request, AC extends AuthContext | undefined, TSegments extends TSegmentsDict | undefined, TSearchParams extends TSearchParamsDict | undefined, TBody extends TBodySchema | undefined, TFormData extends TFormDataDict | undefined> = (
228
231
  /**
229
232
  * Safe route handler context
230
233
  */
@@ -232,7 +235,7 @@ ctx: SafeRouteHandlerContext<AC, TSegments, TSearchParams, TBody, TFormData>,
232
235
  /**
233
236
  * Original request
234
237
  */
235
- req: Request) => Promise<Response>;
238
+ req: TReq) => Promise<Response>;
236
239
 
237
240
  /**
238
241
  * Creates a safe route handler with data validation and error handling
@@ -270,6 +273,6 @@ req: Request) => Promise<Response>;
270
273
  *)
271
274
  * ```
272
275
  */
273
- 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;
276
+ declare function createSafeRouteHandler<TReq extends Request = Request, 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<TReq, AC, TRouteDynamicSegments, TSearchParams, TBody, TFormData>, handlerFn: SafeRouteHandler<TReq, AC, TRouteDynamicSegments, TSearchParams, TBody, TFormData>): CreateSafeRouteHandlerReturnType<TReq>;
274
277
 
275
278
  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 CHANGED
@@ -1,2 +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};
1
+ function E(e){return e!==null&&(typeof e=="object"||typeof e=="function")&&typeof e.then=="function"}function w(e,a){if(E(e))throw new Error(a)}function g(e=!1){let a=e||process.env.NODE_ENV!=="production";return{info:(t,...n)=>{a&&console.log(t,...n)},error:(t,...n)=>{a&&console.error(t,...n)},warn:(t,...n)=>{a&&console.warn(t,...n)}}}function D(){let e=null,a=null;return{start:()=>{e=performance.now()},stop:()=>{if(e===null)throw new Error("Execution clock was not started.");a=performance.now()},get:()=>{if(!e||!a)throw new Error("Execution clock has not been started or stopped.");return`${(a-e).toFixed(2)}ms`}}}function I(e,a,t="Validation must be synchronous but schema returned a Promise."){let n=e["~standard"].validate(a);return w(n,t),n}var A=(e,a)=>a in e;function h(e,a){let t={},n=[];for(let u in e){if(!A(e,u))continue;let i=e[u]["~standard"].validate(a[u]);if(w(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 C="[unknown:route:handler]",q=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 F(e,a){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=g(e.debug),n=e.id??C,u=e.onErrorResponse??(r=>(t.error(`\u{1F6D1} Unexpected error in route handler '${n}'`,r),new Response("Internal server error",{status:500}))),i=e.onSegmentsValidationErrorResponse??(r=>(t.error(`\u{1F6D1} Invalid segments for route handler '${n}':`,r),new Response("Invalid segments",{status:400}))),p=e.onSearchParamsValidationErrorResponse??(r=>(t.error(`\u{1F6D1} Invalid search params for route handler '${n}':`,r),new Response("Invalid search params",{status:400}))),P=e.onBodyValidationErrorResponse??(r=>(t.error(`\u{1F6D1} Invalid body for route handler '${n}':`,r),new Response("Invalid body",{status:400}))),v=e.onFormDataValidationErrorResponse??(r=>(t.error(`\u{1F6D1} Invalid form data for route handler '${n}':`,r),new Response("Invalid form data",{status:400}))),b=e.authorize??(async()=>{});return async function(r,O){let c=D();c.start(),t.info(`\u{1F504} Running route handler '${n}'`),t.info(`\u{1F449}\u{1F3FB} Request ${r.method} ${r.url}`);let m=new URL(r.url),k=r.clone(),f=await b({req:k,url:m});if(f instanceof Response)return t.error(`\u{1F6D1} Request not authorized for route handler '${n}'`),f;let y;if(e.segments){let o=await O.params;if(o===void 0)return new Response("No segments provided",{status:400});let s=h(e.segments,o);if(s.issues)return await i(s.issues);y=s.value}let S;if(e.searchParams){let o=[...m.searchParams.keys()].map(d=>{let l=m.searchParams.getAll(d);return[d,l.length>1?l:l[0]]}),s=h(e.searchParams,Object.fromEntries(o));if(s.issues)return await p(s.issues);S=s.value}let x=r.clone(),R;if(e.body){if(!["POST","PUT","PATCH"].includes(r.method))return new Response("Invalid method for request body",{status:405});let o;try{o=await q(x)}catch(d){return await u(d)}let s=I(e.body,o,"Request body validation must be synchronous");if(s.issues)return await P(s.issues);R=s.value}let T;if(e.formData){if(!["POST","PUT","PATCH"].includes(r.method))return new Response("Invalid method for request form data",{status:405});let o=r.headers.get("content-type");if(!o?.startsWith("multipart/form-data")&&!o?.startsWith("application/x-www-form-urlencoded"))return new Response("Invalid content type for request form data",{status:415});let s;try{s=await x.formData()}catch(l){return await u(l)}let d=h(e.formData,Object.fromEntries(s.entries()));if(d.issues)return await v(d.issues);T=d.value}let V={id:n,url:m,...f?{auth:f}:{},...y?{segments:y}:{},...S?{searchParams:S}:{},...R?{body:R}:{},...T?{formData:T}:{}};try{let o=await a(V,r);return c.stop(),t.info(`\u2705 Route handler '${n}' executed successfully in ${c.get()}`),o}catch(o){return c.stop(),t.error(`\u{1F6D1} Route handle '${n} failed to execute after ${c.get()}'`),await u(o)}}}export{F as createSafeRouteHandler};
2
2
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +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,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G,CAClC,CAEA,IAAM0B,EAAiBb,EACrBnB,EAAQ,SACR,OAAO,YAAY+B,EAAgB,QAAQ,CAAC,CAC9C,EAEA,GAAIC,EAAe,OACjB,OAAO,MAAMrB,EAAkCqB,EAAe,MAAM,EAGtEH,EAAWG,EAAe,KAC5B,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"]}
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\nexport function createExecutionClock() {\n let startTime: number | null = null\n let endTime: number | null = null\n\n return {\n start: (): void => {\n startTime = performance.now()\n },\n stop: (): void => {\n if (startTime === null) {\n throw new Error('Execution clock was not started.')\n }\n endTime = performance.now()\n },\n get: (): string => {\n if (!startTime || !endTime) {\n throw new Error('Execution clock has not been started or stopped.')\n }\n\n const duration = endTime - startTime\n return `${duration.toFixed(2)}ms`\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, createExecutionClock } 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 <TReq extends Request>(\n req: TReq\n): 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 TReq extends Request = Request,\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 TReq,\n AC,\n TRouteDynamicSegments,\n TSearchParams,\n TBody,\n TFormData\n >,\n handlerFn: SafeRouteHandler<\n TReq,\n AC,\n TRouteDynamicSegments,\n TSearchParams,\n TBody,\n TFormData\n >\n): CreateSafeRouteHandlerReturnType<TReq> {\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 (req: TReq, extras: RequestExtras): Promise<Response> {\n const executionClock = createExecutionClock()\n executionClock.start()\n\n log.info(`🔄 Running route handler '${id}'`)\n log.info(`👉🏻 Request ${req.method} ${req.url}`)\n\n const url = new URL(req.url)\n\n // Do not mutate / consume the original request\n const clonedReq_forAuth = req.clone() as TReq\n const authOrResponse = await authorize({ req: clonedReq_forAuth, 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 const params = await extras.params\n if (params === undefined) {\n return new Response('No segments provided', { status: 400 })\n }\n\n const parsedSegments = parseWithDictionary(options.segments, params)\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 // Do not mutate / consume the original request\n const clonedReq_forBody = req.clone() as TReq\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(clonedReq_forBody)\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 // NOTE: 🤔 maybe find a better way to counter the deprecation warning?\n formData_unsafe = await clonedReq_forBody.formData()\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 const response = await handlerFn(ctx, req)\n\n executionClock.stop()\n log.info(\n `✅ Route handler '${id}' executed successfully in ${executionClock.get()}`\n )\n\n return response\n } catch (err) {\n executionClock.stop()\n log.error(\n `🛑 Route handle '${id} failed to execute after ${executionClock.get()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AAIC,EAAe,OACjB,OAAO,MAAMzB,EAAkCyB,EAAe,MAAM,EAGtEH,EAAWG,EAAe,KAC5B,CAGA,IAAMC,EAAM,CACV,GAAAjC,EACA,IAAAY,EACA,GAAIE,EAAiB,CAAE,KAAMA,CAAe,EAAI,CAAC,EACjD,GAAIC,EAAW,CAAE,SAAAA,CAAS,EAAI,CAAC,EAC/B,GAAII,EAAe,CAAE,aAAAA,CAAa,EAAI,CAAC,EACvC,GAAIM,EAAO,CAAE,KAAAA,CAAK,EAAI,CAAC,EACvB,GAAII,EAAW,CAAE,SAAAA,CAAS,EAAI,CAAC,CACjC,EASA,GAAI,CACF,IAAMK,EAAW,MAAMrC,EAAUoC,EAAKxC,CAAG,EAEzC,OAAAiB,EAAe,KAAK,EACpBZ,EAAI,KACF,yBAAoBE,CAAE,8BAA8BU,EAAe,IAAI,CAAC,EAC1E,EAEOwB,CACT,OAAShC,EAAK,CACZ,OAAAQ,EAAe,KAAK,EACpBZ,EAAI,MACF,2BAAoBE,CAAE,4BAA4BU,EAAe,IAAI,CAAC,GACxE,EAEO,MAAMT,EAAgBC,CAAG,CAClC,CACF,CACF","names":["isPromise","value","assertsSyncOperation","message","createLogger","debug","shouldLog","rest","createExecutionClock","startTime","endTime","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","executionClock","createExecutionClock","url","clonedReq_forAuth","authOrResponse","segments","params","parsedSegments","parseWithDictionary","searchParams","queryParams_unsafe","k","values","parsedSearchParams","clonedReq_forBody","body","body_unsafe","parsedBody","validateWithSchema","formData","contentType","formData_unsafe","parsedFormData","ctx","response"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sugardarius/anzen",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "A fast, framework validation agnostic, type-safe factory for creating Next.JS App Router route handlers.",
5
5
  "license": "MIT",
6
6
  "packageManager": "npm@11.3.0",
@@ -67,21 +67,19 @@
67
67
  "typescript": "^5"
68
68
  },
69
69
  "devDependencies": {
70
- "@arethetypeswrong/cli": "^0.18.1",
71
- "@eslint/js": "^9.27.0",
70
+ "@arethetypeswrong/cli": "^0.18.2",
71
+ "@eslint/js": "^9.31.0",
72
72
  "@release-it/keep-a-changelog": "^7.0.0",
73
- "@types/node": "^22.15.19",
74
- "@types/react": "^19.1.4",
75
- "@types/react-dom": "^19.1.5",
76
- "eslint": "^9.27.0",
77
- "prettier": "^3.5.3",
73
+ "@types/node": "^24.0.13",
74
+ "eslint": "^9.31.0",
75
+ "prettier": "^3.6.2",
78
76
  "publint": "^0.3.12",
79
- "release-it": "^19.0.2",
77
+ "release-it": "^19.0.3",
80
78
  "tsup": "^8.5.0",
81
- "turbo": "^2.5.3",
79
+ "turbo": "^2.5.4",
82
80
  "typescript": "^5.8.3",
83
- "typescript-eslint": "^8.32.1",
84
- "vitest": "^3.1.4",
85
- "zod": "^3.25.7"
81
+ "typescript-eslint": "^8.37.0",
82
+ "vitest": "^3.2.4",
83
+ "zod": "^4.0.5"
86
84
  }
87
85
  }