@alevnyacow/nzmt 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -0
- package/dist/errors.utils.d.ts +41 -0
- package/dist/index.cjs +7150 -0
- package/dist/index.cjs.LICENSE.txt +19 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +7031 -0
- package/dist/index.js.LICENSE.txt +19 -0
- package/dist/rslib-runtime.js +25 -0
- package/dist/store/index.d.ts +3 -0
- package/dist/store/store.pagination.entity.d.ts +18 -0
- package/dist/store/store.ram.spec.d.ts +1 -0
- package/dist/store/store.ram.utils.d.ts +23 -0
- package/dist/store/store.shared-models.utils.d.ts +86 -0
- package/dist/store/store.zod.utils.d.ts +449 -0
- package/dist/zod-controller.utils.d.ts +122 -0
- package/dist/zod-module.utils.d.ts +29 -0
- package/package.json +40 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { type NextRequest, NextResponse } from 'next/server';
|
|
2
|
+
import z, { type ZodObject } from 'zod';
|
|
3
|
+
import { type ControllerErrorModel, type ErrorBaseCreatingPayload } from './errors.utils';
|
|
4
|
+
export declare enum ZodControllerDefaultErrorCodes {
|
|
5
|
+
REQUEST_PARSING = "ZOD-CONTROLLER___REQUEST-PARSING",
|
|
6
|
+
RESPONSE_PARSING = "ZOD-CONTROLLER___RESPONSE-PARSING"
|
|
7
|
+
}
|
|
8
|
+
type SuccessResponse<ResponseZodSchema> = ResponseZodSchema extends undefined ? {} : z.infer<ResponseZodSchema>;
|
|
9
|
+
type ZodAPISchemas = {
|
|
10
|
+
body?: ZodObject;
|
|
11
|
+
query?: ZodObject;
|
|
12
|
+
response?: ZodObject;
|
|
13
|
+
};
|
|
14
|
+
type ErrorResponse = {
|
|
15
|
+
message: string;
|
|
16
|
+
code?: string;
|
|
17
|
+
details?: any;
|
|
18
|
+
};
|
|
19
|
+
type EndpointErrorGenerator = (payload: string | ErrorBaseCreatingPayload, errorStatus?: number, cause?: unknown) => ControllerErrorModel;
|
|
20
|
+
export type Guard = (payload: {
|
|
21
|
+
request: NextRequest;
|
|
22
|
+
endpointError: EndpointErrorGenerator;
|
|
23
|
+
}) => Promise<undefined | ControllerErrorModel>;
|
|
24
|
+
export type OnErrorHandler = (request: {
|
|
25
|
+
error: ControllerErrorModel;
|
|
26
|
+
req: NextRequest;
|
|
27
|
+
}) => Promise<void>;
|
|
28
|
+
type EndpointLogic<T extends ZodAPISchemas> = {
|
|
29
|
+
handler: (payload: (T['query'] extends ZodObject ? z.infer<T['query']> : {}) & (T['body'] extends ZodObject ? z.infer<T['body']> : {}), request: {
|
|
30
|
+
request: NextRequest;
|
|
31
|
+
flags: Record<string, boolean>;
|
|
32
|
+
endpointError: EndpointErrorGenerator;
|
|
33
|
+
}) => Promise<T['response'] extends ZodObject ? z.infer<T['response']> : void>;
|
|
34
|
+
guards?: Guard[];
|
|
35
|
+
eventHandlers?: {
|
|
36
|
+
onSuccess?: Array<(data: {
|
|
37
|
+
requestPayload: (T['query'] extends ZodObject ? z.infer<T['query']> : {}) & (T['body'] extends ZodObject ? z.infer<T['body']> : {});
|
|
38
|
+
request: NextRequest;
|
|
39
|
+
result: T['response'] extends ZodObject ? z.infer<T['response']> : undefined;
|
|
40
|
+
flags: Record<string, boolean>;
|
|
41
|
+
}) => Promise<void>>;
|
|
42
|
+
onError?: Array<OnErrorHandler>;
|
|
43
|
+
};
|
|
44
|
+
customResponseLogic?: {
|
|
45
|
+
onSuccess?: (payload: {
|
|
46
|
+
req: NextRequest;
|
|
47
|
+
response: T['response'] extends undefined ? undefined : z.infer<T['response']>;
|
|
48
|
+
}) => Promise<NextResponse>;
|
|
49
|
+
onError?: (payload: {
|
|
50
|
+
req: NextRequest;
|
|
51
|
+
error: ControllerErrorModel;
|
|
52
|
+
}) => Promise<NextResponse>;
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
export type SharedConfig = Partial<{
|
|
56
|
+
guards: Guard[];
|
|
57
|
+
onErrorHandlers?: Array<OnErrorHandler>;
|
|
58
|
+
}>;
|
|
59
|
+
export declare const zodAPIEndpointFactory: <T extends ZodControllerSchemas>(metadata: {
|
|
60
|
+
schemas: T;
|
|
61
|
+
name: string;
|
|
62
|
+
}, sharedConfig?: SharedConfig) => <Method extends keyof T>(method: Method, handler: EndpointLogic<T[Method]>["handler"], configuration?: Omit<EndpointLogic<T[Method]>, "handler">) => (request: NextRequest) => Promise<NextResponse<unknown>>;
|
|
63
|
+
type ZodControllerSchemas = Record<string, ZodAPISchemas>;
|
|
64
|
+
type Flatten<T extends Record<string, object>> = {
|
|
65
|
+
[K in keyof T]: T[K];
|
|
66
|
+
}[keyof T];
|
|
67
|
+
type OnlyObject<T> = T extends object ? T : never;
|
|
68
|
+
type SchemaShape = {
|
|
69
|
+
body?: unknown;
|
|
70
|
+
query?: unknown;
|
|
71
|
+
response?: unknown;
|
|
72
|
+
};
|
|
73
|
+
export type ZodControllerMetadata<T = ZodControllerSchemas> = {
|
|
74
|
+
name: string;
|
|
75
|
+
schemas: T;
|
|
76
|
+
};
|
|
77
|
+
type NormalizeSchema<T extends SchemaShape> = {
|
|
78
|
+
body: T extends {
|
|
79
|
+
body: infer B;
|
|
80
|
+
} ? B : undefined;
|
|
81
|
+
query: T extends {
|
|
82
|
+
query: infer Q;
|
|
83
|
+
} ? Q : undefined;
|
|
84
|
+
response: T extends {
|
|
85
|
+
response: infer R;
|
|
86
|
+
} ? R : undefined;
|
|
87
|
+
};
|
|
88
|
+
type RequestPayload<T extends SchemaShape> = Flatten<{
|
|
89
|
+
[K in Exclude<keyof NormalizeSchema<T>, 'response'> as OnlyObject<NormalizeSchema<T>[K]> extends never ? never : K]: z.infer<OnlyObject<NormalizeSchema<T>[K]>>;
|
|
90
|
+
}>;
|
|
91
|
+
type ResponsePayload<T extends SchemaShape> = T extends {
|
|
92
|
+
response: infer R;
|
|
93
|
+
} ? z.infer<R> : never;
|
|
94
|
+
type ZodAPIPayload<QueryParams, BodyParams> = (QueryParams extends undefined ? {} : {
|
|
95
|
+
query: z.infer<QueryParams>;
|
|
96
|
+
}) & (BodyParams extends undefined ? {} : {
|
|
97
|
+
body: z.infer<BodyParams>;
|
|
98
|
+
});
|
|
99
|
+
type ZodAPIMethod<Schemas extends {
|
|
100
|
+
body: unknown;
|
|
101
|
+
query: unknown;
|
|
102
|
+
response: unknown;
|
|
103
|
+
}> = {
|
|
104
|
+
payload: ZodAPIPayload<Schemas['query'], Schemas['body']>;
|
|
105
|
+
response: SuccessResponse<Schemas['response']>;
|
|
106
|
+
error: ErrorResponse;
|
|
107
|
+
};
|
|
108
|
+
export type ZodControllerAPI<Metadata extends {
|
|
109
|
+
schemas: Record<string, SchemaShape>;
|
|
110
|
+
}, CustomModels extends Record<string, unknown> | undefined = undefined> = {
|
|
111
|
+
endpoints: {
|
|
112
|
+
[K in keyof Metadata['schemas']]: ZodAPIMethod<NormalizeSchema<Metadata['schemas'][K]>>;
|
|
113
|
+
};
|
|
114
|
+
customModels: CustomModels extends undefined ? {} : CustomModels;
|
|
115
|
+
requestDTOs: {
|
|
116
|
+
[K in keyof Metadata['schemas']]: RequestPayload<Metadata['schemas'][K]>;
|
|
117
|
+
};
|
|
118
|
+
responseDTOs: {
|
|
119
|
+
[K in keyof Metadata['schemas']]: ResponsePayload<Metadata['schemas'][K]>;
|
|
120
|
+
};
|
|
121
|
+
};
|
|
122
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type z from 'zod';
|
|
2
|
+
import type { ZodType } from 'zod';
|
|
3
|
+
import { type ErrorBaseCreatingPayload, type ModuleErrorModel } from './errors.utils';
|
|
4
|
+
type ZodModuleSchemas = Record<string, {
|
|
5
|
+
payload: ZodType;
|
|
6
|
+
response: ZodType;
|
|
7
|
+
}>;
|
|
8
|
+
export type ZodModuleMetadata = {
|
|
9
|
+
name: string;
|
|
10
|
+
schemas: ZodModuleSchemas;
|
|
11
|
+
};
|
|
12
|
+
export type ZodModuleDTOs<T extends ZodModuleMetadata> = {
|
|
13
|
+
[K in keyof T['schemas'] as `${Extract<K, string>}Payload`]: z.infer<T['schemas'][K]['payload']>;
|
|
14
|
+
} & {
|
|
15
|
+
[K in keyof T['schemas'] as `${Extract<K, string>}Response`]: z.infer<T['schemas'][K]['response']>;
|
|
16
|
+
};
|
|
17
|
+
export type ZodModuleMethods<T extends ZodModuleMetadata> = {
|
|
18
|
+
[K in keyof T['schemas']]: (payload: z.infer<T['schemas'][K]['payload']>) => Promise<z.infer<T['schemas'][K]['response']>>;
|
|
19
|
+
};
|
|
20
|
+
export type ZodModuleConfig = {
|
|
21
|
+
onError?: (e: ModuleErrorModel) => Promise<void>;
|
|
22
|
+
};
|
|
23
|
+
export declare const zodModuleMethodFactory: <T extends ZodModuleSchemas>(metadata: {
|
|
24
|
+
schemas: T;
|
|
25
|
+
name: string;
|
|
26
|
+
}, sharedConfig?: ZodModuleConfig) => <Method extends keyof T>(methodName: Method, handler: (payload: z.infer<T[Method]["payload"]>, config: {
|
|
27
|
+
methodError: (payload: string | ErrorBaseCreatingPayload, cause?: unknown) => ModuleErrorModel;
|
|
28
|
+
}) => Promise<z.infer<T[Method]["response"]>>, config?: ZodModuleConfig) => (payload: z.infer<T[Method]["payload"]>) => Promise<z.infer<T[Method]["response"]>>;
|
|
29
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@alevnyacow/nzmt",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Next Zod Modules Toolkit",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"import": "./dist/index.js",
|
|
10
|
+
"require": "./dist/index.cjs"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"main": "./dist/index.cjs",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "rslib build",
|
|
20
|
+
"check": "biome check --write",
|
|
21
|
+
"dev": "rslib build --watch",
|
|
22
|
+
"format": "biome format --write",
|
|
23
|
+
"test": "rstest",
|
|
24
|
+
"test:watch": "rstest --watch"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@biomejs/biome": "2.4.6",
|
|
28
|
+
"@rslib/core": "^0.20.0",
|
|
29
|
+
"@rstest/adapter-rslib": "^0.2.1",
|
|
30
|
+
"@rstest/core": "^0.9.0",
|
|
31
|
+
"@types/node": "^24.12.0",
|
|
32
|
+
"next": "^16.1.6",
|
|
33
|
+
"typescript": "^5.9.3"
|
|
34
|
+
},
|
|
35
|
+
"private": false,
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"uuid": "^13.0.0",
|
|
38
|
+
"zod": "^4.3.6"
|
|
39
|
+
}
|
|
40
|
+
}
|