@sugardarius/anzen 1.1.2 → 2.0.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 +116 -378
- package/dist/chunk-DT3TEL5X.js +2 -0
- package/dist/chunk-DT3TEL5X.js.map +1 -0
- package/dist/chunk-UDCVGQMH.cjs +2 -0
- package/dist/chunk-UDCVGQMH.cjs.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -240
- package/dist/index.d.ts +3 -240
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/server-components/index.cjs +2 -0
- package/dist/server-components/index.cjs.map +1 -0
- package/dist/server-components/index.d.cts +208 -0
- package/dist/server-components/index.d.ts +208 -0
- package/dist/server-components/index.js +2 -0
- package/dist/server-components/index.js.map +1 -0
- package/dist/types-LPIIICMI.d.cts +266 -0
- package/dist/types-LPIIICMI.d.ts +266 -0
- package/package.json +23 -11
|
@@ -0,0 +1,266 @@
|
|
|
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 AuthFunctionParams<TSegments extends TSegmentsDict | undefined, TSearchParams extends TSearchParamsDict | undefined, TBody extends TBodySchema | undefined, TFormData extends TFormDataDict | undefined> = {
|
|
77
|
+
/**
|
|
78
|
+
* ID for the route handler.
|
|
79
|
+
*/
|
|
80
|
+
readonly id: string;
|
|
81
|
+
/**
|
|
82
|
+
* Parsed request url
|
|
83
|
+
*/
|
|
84
|
+
readonly url: URL;
|
|
85
|
+
/**
|
|
86
|
+
* Original request
|
|
87
|
+
*
|
|
88
|
+
* Cloned from the incoming request to avoid side effects
|
|
89
|
+
* and to make it consumable in the `authorize` function.
|
|
90
|
+
* Due to `NextRequest` limitations as the req is cloned it's always a `Request`
|
|
91
|
+
*/
|
|
92
|
+
req: Request;
|
|
93
|
+
} & (TSegments extends TSegmentsDict ? {
|
|
94
|
+
/**
|
|
95
|
+
* Validated route dynamic segments
|
|
96
|
+
*/
|
|
97
|
+
readonly segments: UnwrapReadonlyObject<StandardSchemaDictionary.InferOutput<TSegments>>;
|
|
98
|
+
} : EmptyObjectType) & (TSearchParams extends TSearchParamsDict ? {
|
|
99
|
+
/**
|
|
100
|
+
* Validated search params
|
|
101
|
+
*/
|
|
102
|
+
readonly searchParams: UnwrapReadonlyObject<StandardSchemaDictionary.InferOutput<TSearchParams>>;
|
|
103
|
+
} : EmptyObjectType) & (TBody extends TBodySchema ? {
|
|
104
|
+
/**
|
|
105
|
+
* Validated request body
|
|
106
|
+
*/
|
|
107
|
+
readonly body: StandardSchemaV1.InferOutput<TBody>;
|
|
108
|
+
} : EmptyObjectType) & (TFormData extends TFormDataDict ? {
|
|
109
|
+
/**
|
|
110
|
+
* Validated form data
|
|
111
|
+
*/
|
|
112
|
+
readonly formData: UnwrapReadonlyObject<StandardSchemaDictionary.InferOutput<TFormData>>;
|
|
113
|
+
} : EmptyObjectType);
|
|
114
|
+
type AuthFunction<AC extends AuthContext | undefined, TSegments extends TSegmentsDict | undefined, TSearchParams extends TSearchParamsDict | undefined, TBody extends TBodySchema | undefined, TFormData extends TFormDataDict | undefined> = (params: AuthFunctionParams<TSegments, TSearchParams, TBody, TFormData>) => Awaitable<AC | Response>;
|
|
115
|
+
type BaseOptions = {
|
|
116
|
+
/**
|
|
117
|
+
* ID for the route handler.
|
|
118
|
+
* Used when logging in development or when `debug` is enabled.
|
|
119
|
+
*
|
|
120
|
+
* You can also use it to add extra logging or monitoring.
|
|
121
|
+
*/
|
|
122
|
+
id?: string;
|
|
123
|
+
/**
|
|
124
|
+
* Callback triggered when the request fails.
|
|
125
|
+
* By default it returns a simple `500` response and the error is logged into the console.
|
|
126
|
+
*
|
|
127
|
+
* Use it if your handler use custom errors and
|
|
128
|
+
* you want to manage them properly by returning a proper response.
|
|
129
|
+
*/
|
|
130
|
+
onErrorResponse?: (err: unknown) => Awaitable<Response>;
|
|
131
|
+
/**
|
|
132
|
+
* Use this options to enable debug mode.
|
|
133
|
+
* It will add logs in the handler to help you debug the request.
|
|
134
|
+
*
|
|
135
|
+
* By default it's set to `false` for production builds.
|
|
136
|
+
* In development builds, it will be `true` if `NODE_ENV` is not set to `production`.
|
|
137
|
+
*/
|
|
138
|
+
debug?: boolean;
|
|
139
|
+
};
|
|
140
|
+
type OnValidationErrorResponse = (issues: readonly StandardSchemaV1.Issue[]) => Awaitable<Response>;
|
|
141
|
+
type CreateSafeRouteHandlerOptions<AC extends AuthContext | undefined, TSegments extends TSegmentsDict | undefined, TSearchParams extends TSearchParamsDict | undefined, TBody extends TBodySchema | undefined, TFormData extends TFormDataDict | undefined> = BaseOptions & {
|
|
142
|
+
/**
|
|
143
|
+
* Dynamic route segments used for the route handler path.
|
|
144
|
+
* By design it will handler if the segments are a `Promise` or not.
|
|
145
|
+
*
|
|
146
|
+
* Please note the expected input is a `StandardSchemaDictionary`.
|
|
147
|
+
*/
|
|
148
|
+
segments?: TSegments;
|
|
149
|
+
/**
|
|
150
|
+
* Callback triggered when dynamic segments validations returned issues.
|
|
151
|
+
* By default it returns a simple `400` response and issues are logged into the console.
|
|
152
|
+
*/
|
|
153
|
+
onSegmentsValidationErrorResponse?: OnValidationErrorResponse;
|
|
154
|
+
/**
|
|
155
|
+
* Search params used in the route.
|
|
156
|
+
*
|
|
157
|
+
* Please note the expected input is a `StandardSchemaDictionary`.
|
|
158
|
+
*/
|
|
159
|
+
searchParams?: TSearchParams;
|
|
160
|
+
/**
|
|
161
|
+
* Callback triggered when search params validations returned issues.
|
|
162
|
+
* By default it returns a simple `400` response and issues are logged into the console.
|
|
163
|
+
*/
|
|
164
|
+
onSearchParamsValidationErrorResponse?: OnValidationErrorResponse;
|
|
165
|
+
/**
|
|
166
|
+
* Request body.
|
|
167
|
+
*
|
|
168
|
+
* Returns a `405` response if the request method is not `POST`, 'PUT' or 'PATCH'.
|
|
169
|
+
* Returns a `415`response if the request does not explicitly set the `Content-Type` to `application/json`.
|
|
170
|
+
*
|
|
171
|
+
* IMPORTANT: The body is parsed as JSON, so it must be a valid JSON object!
|
|
172
|
+
* IMPORTANT: Body shouldn't be used with `formData` at the same time. They are exclusive.
|
|
173
|
+
* Why making the distinction? `formData` is used as a `StandardSchemaDictionary` whereas `body` is used as a `StandardSchemaV1`.
|
|
174
|
+
*/
|
|
175
|
+
body?: TBody;
|
|
176
|
+
/**
|
|
177
|
+
* Callback triggered when body validation returned issues.
|
|
178
|
+
* By default it returns a simple `400` response and issues are logged into the console.
|
|
179
|
+
*/
|
|
180
|
+
onBodyValidationErrorResponse?: OnValidationErrorResponse;
|
|
181
|
+
/**
|
|
182
|
+
* Request form data.
|
|
183
|
+
*
|
|
184
|
+
* Returns a `405` response if the request method is not `POST`, 'PUT' or 'PATCH'.
|
|
185
|
+
* Returns a `415`response if the request does not explicitly set the `Content-Type` to `multipart/form-data`
|
|
186
|
+
* or to `application/x-www-form-urlencoded`.
|
|
187
|
+
*
|
|
188
|
+
* IMPORTANT: formData shouldn't be used with `body` at the same time. They are exclusive.
|
|
189
|
+
* Why making the distinction? `formData` is used as a `StandardSchemaDictionary` whereas `body` is used as a `StandardSchemaV1`.
|
|
190
|
+
*/
|
|
191
|
+
formData?: TFormData;
|
|
192
|
+
/**
|
|
193
|
+
* Callback triggered when form data validation returned issues.
|
|
194
|
+
* By default it returns a simple `400` response and issues are logged into the console.
|
|
195
|
+
*/
|
|
196
|
+
onFormDataValidationErrorResponse?: OnValidationErrorResponse;
|
|
197
|
+
/**
|
|
198
|
+
* Function to use to authorize the request.
|
|
199
|
+
* By default it always authorize the request.
|
|
200
|
+
*
|
|
201
|
+
* When returning a response, it will be used as the response for the request.
|
|
202
|
+
* Return a response when the request is not authorized.
|
|
203
|
+
*/
|
|
204
|
+
authorize?: AuthFunction<AC, TSegments, TSearchParams, TBody, TFormData>;
|
|
205
|
+
};
|
|
206
|
+
type ProvidedRouteContext = {
|
|
207
|
+
/**
|
|
208
|
+
* Route dynamic segments as params
|
|
209
|
+
*/
|
|
210
|
+
params: Awaitable<any> | undefined;
|
|
211
|
+
};
|
|
212
|
+
type CreateSafeRouteHandlerReturnType<TReq extends Request = Request> = (
|
|
213
|
+
/**
|
|
214
|
+
* Original request
|
|
215
|
+
*/
|
|
216
|
+
req: TReq,
|
|
217
|
+
/**
|
|
218
|
+
* Provided context added by Next.js itself
|
|
219
|
+
*/
|
|
220
|
+
providedContext: ProvidedRouteContext) => Promise<Response>;
|
|
221
|
+
type SafeRouteHandlerContext<AC extends AuthContext | undefined, TSegments extends TSegmentsDict | undefined, TSearchParams extends TSearchParamsDict | undefined, TBody extends TBodySchema | undefined, TFormData extends TFormDataDict | undefined> = {
|
|
222
|
+
/**
|
|
223
|
+
* Route handler ID
|
|
224
|
+
*/
|
|
225
|
+
readonly id: string;
|
|
226
|
+
/**
|
|
227
|
+
* Parsed request url
|
|
228
|
+
*/
|
|
229
|
+
readonly url: URL;
|
|
230
|
+
} & (AC extends AuthContext ? {
|
|
231
|
+
/**
|
|
232
|
+
* Auth context
|
|
233
|
+
*/
|
|
234
|
+
readonly auth: AC;
|
|
235
|
+
} : EmptyObjectType) & (TSegments extends TSegmentsDict ? {
|
|
236
|
+
/**
|
|
237
|
+
* Validated route dynamic segments
|
|
238
|
+
*/
|
|
239
|
+
readonly segments: UnwrapReadonlyObject<StandardSchemaDictionary.InferOutput<TSegments>>;
|
|
240
|
+
} : EmptyObjectType) & (TSearchParams extends TSearchParamsDict ? {
|
|
241
|
+
/**
|
|
242
|
+
* Validated search params
|
|
243
|
+
*/
|
|
244
|
+
readonly searchParams: UnwrapReadonlyObject<StandardSchemaDictionary.InferOutput<TSearchParams>>;
|
|
245
|
+
} : EmptyObjectType) & (TBody extends TBodySchema ? {
|
|
246
|
+
/**
|
|
247
|
+
* Validated request body
|
|
248
|
+
*/
|
|
249
|
+
readonly body: StandardSchemaV1.InferOutput<TBody>;
|
|
250
|
+
} : EmptyObjectType) & (TFormData extends TFormDataDict ? {
|
|
251
|
+
/**
|
|
252
|
+
* Validated form data
|
|
253
|
+
*/
|
|
254
|
+
readonly formData: UnwrapReadonlyObject<StandardSchemaDictionary.InferOutput<TFormData>>;
|
|
255
|
+
} : EmptyObjectType);
|
|
256
|
+
type SafeRouteHandler<AC extends AuthContext | undefined, TSegments extends TSegmentsDict | undefined, TSearchParams extends TSearchParamsDict | undefined, TBody extends TBodySchema | undefined, TFormData extends TFormDataDict | undefined, TReq extends Request = Request> = (
|
|
257
|
+
/**
|
|
258
|
+
* Safe route handler context
|
|
259
|
+
*/
|
|
260
|
+
ctx: SafeRouteHandlerContext<AC, TSegments, TSearchParams, TBody, TFormData>,
|
|
261
|
+
/**
|
|
262
|
+
* Original request
|
|
263
|
+
*/
|
|
264
|
+
req: TReq) => Promise<Response>;
|
|
265
|
+
|
|
266
|
+
export { type AuthContext as A, type BaseOptions as B, type CreateSafeRouteHandlerOptions as C, type EmptyObjectType as E, type OnValidationErrorResponse as O, type ProvidedRouteContext as P, type SafeRouteHandler as S, type TSegmentsDict as T, type UnwrapReadonlyObject as U, type TSearchParamsDict as a, type TBodySchema as b, type TFormDataDict as c, type CreateSafeRouteHandlerReturnType as d, type Awaitable as e, type AuthFunction as f, type AuthFunctionParams as g, type SafeRouteHandlerContext as h, StandardSchemaDictionary as i, StandardSchemaV1 as j };
|
|
@@ -0,0 +1,266 @@
|
|
|
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 AuthFunctionParams<TSegments extends TSegmentsDict | undefined, TSearchParams extends TSearchParamsDict | undefined, TBody extends TBodySchema | undefined, TFormData extends TFormDataDict | undefined> = {
|
|
77
|
+
/**
|
|
78
|
+
* ID for the route handler.
|
|
79
|
+
*/
|
|
80
|
+
readonly id: string;
|
|
81
|
+
/**
|
|
82
|
+
* Parsed request url
|
|
83
|
+
*/
|
|
84
|
+
readonly url: URL;
|
|
85
|
+
/**
|
|
86
|
+
* Original request
|
|
87
|
+
*
|
|
88
|
+
* Cloned from the incoming request to avoid side effects
|
|
89
|
+
* and to make it consumable in the `authorize` function.
|
|
90
|
+
* Due to `NextRequest` limitations as the req is cloned it's always a `Request`
|
|
91
|
+
*/
|
|
92
|
+
req: Request;
|
|
93
|
+
} & (TSegments extends TSegmentsDict ? {
|
|
94
|
+
/**
|
|
95
|
+
* Validated route dynamic segments
|
|
96
|
+
*/
|
|
97
|
+
readonly segments: UnwrapReadonlyObject<StandardSchemaDictionary.InferOutput<TSegments>>;
|
|
98
|
+
} : EmptyObjectType) & (TSearchParams extends TSearchParamsDict ? {
|
|
99
|
+
/**
|
|
100
|
+
* Validated search params
|
|
101
|
+
*/
|
|
102
|
+
readonly searchParams: UnwrapReadonlyObject<StandardSchemaDictionary.InferOutput<TSearchParams>>;
|
|
103
|
+
} : EmptyObjectType) & (TBody extends TBodySchema ? {
|
|
104
|
+
/**
|
|
105
|
+
* Validated request body
|
|
106
|
+
*/
|
|
107
|
+
readonly body: StandardSchemaV1.InferOutput<TBody>;
|
|
108
|
+
} : EmptyObjectType) & (TFormData extends TFormDataDict ? {
|
|
109
|
+
/**
|
|
110
|
+
* Validated form data
|
|
111
|
+
*/
|
|
112
|
+
readonly formData: UnwrapReadonlyObject<StandardSchemaDictionary.InferOutput<TFormData>>;
|
|
113
|
+
} : EmptyObjectType);
|
|
114
|
+
type AuthFunction<AC extends AuthContext | undefined, TSegments extends TSegmentsDict | undefined, TSearchParams extends TSearchParamsDict | undefined, TBody extends TBodySchema | undefined, TFormData extends TFormDataDict | undefined> = (params: AuthFunctionParams<TSegments, TSearchParams, TBody, TFormData>) => Awaitable<AC | Response>;
|
|
115
|
+
type BaseOptions = {
|
|
116
|
+
/**
|
|
117
|
+
* ID for the route handler.
|
|
118
|
+
* Used when logging in development or when `debug` is enabled.
|
|
119
|
+
*
|
|
120
|
+
* You can also use it to add extra logging or monitoring.
|
|
121
|
+
*/
|
|
122
|
+
id?: string;
|
|
123
|
+
/**
|
|
124
|
+
* Callback triggered when the request fails.
|
|
125
|
+
* By default it returns a simple `500` response and the error is logged into the console.
|
|
126
|
+
*
|
|
127
|
+
* Use it if your handler use custom errors and
|
|
128
|
+
* you want to manage them properly by returning a proper response.
|
|
129
|
+
*/
|
|
130
|
+
onErrorResponse?: (err: unknown) => Awaitable<Response>;
|
|
131
|
+
/**
|
|
132
|
+
* Use this options to enable debug mode.
|
|
133
|
+
* It will add logs in the handler to help you debug the request.
|
|
134
|
+
*
|
|
135
|
+
* By default it's set to `false` for production builds.
|
|
136
|
+
* In development builds, it will be `true` if `NODE_ENV` is not set to `production`.
|
|
137
|
+
*/
|
|
138
|
+
debug?: boolean;
|
|
139
|
+
};
|
|
140
|
+
type OnValidationErrorResponse = (issues: readonly StandardSchemaV1.Issue[]) => Awaitable<Response>;
|
|
141
|
+
type CreateSafeRouteHandlerOptions<AC extends AuthContext | undefined, TSegments extends TSegmentsDict | undefined, TSearchParams extends TSearchParamsDict | undefined, TBody extends TBodySchema | undefined, TFormData extends TFormDataDict | undefined> = BaseOptions & {
|
|
142
|
+
/**
|
|
143
|
+
* Dynamic route segments used for the route handler path.
|
|
144
|
+
* By design it will handler if the segments are a `Promise` or not.
|
|
145
|
+
*
|
|
146
|
+
* Please note the expected input is a `StandardSchemaDictionary`.
|
|
147
|
+
*/
|
|
148
|
+
segments?: TSegments;
|
|
149
|
+
/**
|
|
150
|
+
* Callback triggered when dynamic segments validations returned issues.
|
|
151
|
+
* By default it returns a simple `400` response and issues are logged into the console.
|
|
152
|
+
*/
|
|
153
|
+
onSegmentsValidationErrorResponse?: OnValidationErrorResponse;
|
|
154
|
+
/**
|
|
155
|
+
* Search params used in the route.
|
|
156
|
+
*
|
|
157
|
+
* Please note the expected input is a `StandardSchemaDictionary`.
|
|
158
|
+
*/
|
|
159
|
+
searchParams?: TSearchParams;
|
|
160
|
+
/**
|
|
161
|
+
* Callback triggered when search params validations returned issues.
|
|
162
|
+
* By default it returns a simple `400` response and issues are logged into the console.
|
|
163
|
+
*/
|
|
164
|
+
onSearchParamsValidationErrorResponse?: OnValidationErrorResponse;
|
|
165
|
+
/**
|
|
166
|
+
* Request body.
|
|
167
|
+
*
|
|
168
|
+
* Returns a `405` response if the request method is not `POST`, 'PUT' or 'PATCH'.
|
|
169
|
+
* Returns a `415`response if the request does not explicitly set the `Content-Type` to `application/json`.
|
|
170
|
+
*
|
|
171
|
+
* IMPORTANT: The body is parsed as JSON, so it must be a valid JSON object!
|
|
172
|
+
* IMPORTANT: Body shouldn't be used with `formData` at the same time. They are exclusive.
|
|
173
|
+
* Why making the distinction? `formData` is used as a `StandardSchemaDictionary` whereas `body` is used as a `StandardSchemaV1`.
|
|
174
|
+
*/
|
|
175
|
+
body?: TBody;
|
|
176
|
+
/**
|
|
177
|
+
* Callback triggered when body validation returned issues.
|
|
178
|
+
* By default it returns a simple `400` response and issues are logged into the console.
|
|
179
|
+
*/
|
|
180
|
+
onBodyValidationErrorResponse?: OnValidationErrorResponse;
|
|
181
|
+
/**
|
|
182
|
+
* Request form data.
|
|
183
|
+
*
|
|
184
|
+
* Returns a `405` response if the request method is not `POST`, 'PUT' or 'PATCH'.
|
|
185
|
+
* Returns a `415`response if the request does not explicitly set the `Content-Type` to `multipart/form-data`
|
|
186
|
+
* or to `application/x-www-form-urlencoded`.
|
|
187
|
+
*
|
|
188
|
+
* IMPORTANT: formData shouldn't be used with `body` at the same time. They are exclusive.
|
|
189
|
+
* Why making the distinction? `formData` is used as a `StandardSchemaDictionary` whereas `body` is used as a `StandardSchemaV1`.
|
|
190
|
+
*/
|
|
191
|
+
formData?: TFormData;
|
|
192
|
+
/**
|
|
193
|
+
* Callback triggered when form data validation returned issues.
|
|
194
|
+
* By default it returns a simple `400` response and issues are logged into the console.
|
|
195
|
+
*/
|
|
196
|
+
onFormDataValidationErrorResponse?: OnValidationErrorResponse;
|
|
197
|
+
/**
|
|
198
|
+
* Function to use to authorize the request.
|
|
199
|
+
* By default it always authorize the request.
|
|
200
|
+
*
|
|
201
|
+
* When returning a response, it will be used as the response for the request.
|
|
202
|
+
* Return a response when the request is not authorized.
|
|
203
|
+
*/
|
|
204
|
+
authorize?: AuthFunction<AC, TSegments, TSearchParams, TBody, TFormData>;
|
|
205
|
+
};
|
|
206
|
+
type ProvidedRouteContext = {
|
|
207
|
+
/**
|
|
208
|
+
* Route dynamic segments as params
|
|
209
|
+
*/
|
|
210
|
+
params: Awaitable<any> | undefined;
|
|
211
|
+
};
|
|
212
|
+
type CreateSafeRouteHandlerReturnType<TReq extends Request = Request> = (
|
|
213
|
+
/**
|
|
214
|
+
* Original request
|
|
215
|
+
*/
|
|
216
|
+
req: TReq,
|
|
217
|
+
/**
|
|
218
|
+
* Provided context added by Next.js itself
|
|
219
|
+
*/
|
|
220
|
+
providedContext: ProvidedRouteContext) => Promise<Response>;
|
|
221
|
+
type SafeRouteHandlerContext<AC extends AuthContext | undefined, TSegments extends TSegmentsDict | undefined, TSearchParams extends TSearchParamsDict | undefined, TBody extends TBodySchema | undefined, TFormData extends TFormDataDict | undefined> = {
|
|
222
|
+
/**
|
|
223
|
+
* Route handler ID
|
|
224
|
+
*/
|
|
225
|
+
readonly id: string;
|
|
226
|
+
/**
|
|
227
|
+
* Parsed request url
|
|
228
|
+
*/
|
|
229
|
+
readonly url: URL;
|
|
230
|
+
} & (AC extends AuthContext ? {
|
|
231
|
+
/**
|
|
232
|
+
* Auth context
|
|
233
|
+
*/
|
|
234
|
+
readonly auth: AC;
|
|
235
|
+
} : EmptyObjectType) & (TSegments extends TSegmentsDict ? {
|
|
236
|
+
/**
|
|
237
|
+
* Validated route dynamic segments
|
|
238
|
+
*/
|
|
239
|
+
readonly segments: UnwrapReadonlyObject<StandardSchemaDictionary.InferOutput<TSegments>>;
|
|
240
|
+
} : EmptyObjectType) & (TSearchParams extends TSearchParamsDict ? {
|
|
241
|
+
/**
|
|
242
|
+
* Validated search params
|
|
243
|
+
*/
|
|
244
|
+
readonly searchParams: UnwrapReadonlyObject<StandardSchemaDictionary.InferOutput<TSearchParams>>;
|
|
245
|
+
} : EmptyObjectType) & (TBody extends TBodySchema ? {
|
|
246
|
+
/**
|
|
247
|
+
* Validated request body
|
|
248
|
+
*/
|
|
249
|
+
readonly body: StandardSchemaV1.InferOutput<TBody>;
|
|
250
|
+
} : EmptyObjectType) & (TFormData extends TFormDataDict ? {
|
|
251
|
+
/**
|
|
252
|
+
* Validated form data
|
|
253
|
+
*/
|
|
254
|
+
readonly formData: UnwrapReadonlyObject<StandardSchemaDictionary.InferOutput<TFormData>>;
|
|
255
|
+
} : EmptyObjectType);
|
|
256
|
+
type SafeRouteHandler<AC extends AuthContext | undefined, TSegments extends TSegmentsDict | undefined, TSearchParams extends TSearchParamsDict | undefined, TBody extends TBodySchema | undefined, TFormData extends TFormDataDict | undefined, TReq extends Request = Request> = (
|
|
257
|
+
/**
|
|
258
|
+
* Safe route handler context
|
|
259
|
+
*/
|
|
260
|
+
ctx: SafeRouteHandlerContext<AC, TSegments, TSearchParams, TBody, TFormData>,
|
|
261
|
+
/**
|
|
262
|
+
* Original request
|
|
263
|
+
*/
|
|
264
|
+
req: TReq) => Promise<Response>;
|
|
265
|
+
|
|
266
|
+
export { type AuthContext as A, type BaseOptions as B, type CreateSafeRouteHandlerOptions as C, type EmptyObjectType as E, type OnValidationErrorResponse as O, type ProvidedRouteContext as P, type SafeRouteHandler as S, type TSegmentsDict as T, type UnwrapReadonlyObject as U, type TSearchParamsDict as a, type TBodySchema as b, type TFormDataDict as c, type CreateSafeRouteHandlerReturnType as d, type Awaitable as e, type AuthFunction as f, type AuthFunctionParams as g, type SafeRouteHandlerContext as h, StandardSchemaDictionary as i, StandardSchemaV1 as j };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sugardarius/anzen",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "A fast, framework validation agnostic, type-safe
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "A fast, framework validation agnostic, type-safe factories for creating route handlers, page and layout server components files in Next.js.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"packageManager": "npm@11.3.0",
|
|
7
7
|
"workspaces": [
|
|
@@ -23,6 +23,17 @@
|
|
|
23
23
|
"module": "./dist/index.js",
|
|
24
24
|
"default": "./dist/index.cjs"
|
|
25
25
|
}
|
|
26
|
+
},
|
|
27
|
+
"./server-components": {
|
|
28
|
+
"import": {
|
|
29
|
+
"types": "./dist/server-components/index.d.ts",
|
|
30
|
+
"default": "./dist/server-components/index.js"
|
|
31
|
+
},
|
|
32
|
+
"require": {
|
|
33
|
+
"types": "./dist/server-components/index.d.cts",
|
|
34
|
+
"module": "./dist/server-components/index.js",
|
|
35
|
+
"default": "./dist/server-components/index.cjs"
|
|
36
|
+
}
|
|
26
37
|
}
|
|
27
38
|
},
|
|
28
39
|
"files": [
|
|
@@ -58,28 +69,29 @@
|
|
|
58
69
|
"lint": "npm run lint:eslint && npm run lint:tsc && npm run lint:package",
|
|
59
70
|
"lint:eslint": "eslint src/",
|
|
60
71
|
"lint:tsc": "tsc --noEmit",
|
|
61
|
-
"lint:package": "publint --strict && attw --pack",
|
|
72
|
+
"lint:package": "publint --strict && attw --pack . --profile node16",
|
|
62
73
|
"format": "prettier --write src/",
|
|
63
74
|
"release": "release-it"
|
|
64
75
|
},
|
|
65
76
|
"peerDependencies": {
|
|
66
|
-
"next": "^
|
|
77
|
+
"next": "^16 || ^15 || ^14",
|
|
67
78
|
"typescript": "^5"
|
|
68
79
|
},
|
|
69
80
|
"devDependencies": {
|
|
70
81
|
"@arethetypeswrong/cli": "^0.18.2",
|
|
71
|
-
"@eslint/js": "^9.
|
|
82
|
+
"@eslint/js": "^9.39.1",
|
|
72
83
|
"@release-it/keep-a-changelog": "^7.0.0",
|
|
73
|
-
"@types/node": "^24.
|
|
84
|
+
"@types/node": "^24.10.1",
|
|
85
|
+
"@types/react": "^19.2.4",
|
|
74
86
|
"decoders": "^2.7.5",
|
|
75
|
-
"eslint": "^9.
|
|
87
|
+
"eslint": "^9.39.1",
|
|
76
88
|
"prettier": "^3.6.2",
|
|
77
|
-
"publint": "^0.3.
|
|
78
|
-
"release-it": "^19.0.
|
|
79
|
-
"tsup": "^8.5.
|
|
89
|
+
"publint": "^0.3.15",
|
|
90
|
+
"release-it": "^19.0.6",
|
|
91
|
+
"tsup": "^8.5.1",
|
|
80
92
|
"turbo": "^2.5.6",
|
|
81
93
|
"typescript": "^5.9.3",
|
|
82
|
-
"typescript-eslint": "^8.46.
|
|
94
|
+
"typescript-eslint": "^8.46.4",
|
|
83
95
|
"vitest": "^3.2.4",
|
|
84
96
|
"zod": "^4.1.12"
|
|
85
97
|
}
|