springnext 0.0.1 → 0.0.2
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/dist/errors.utils.d.ts +37 -0
- package/dist/index.cjs +510 -4
- package/dist/index.d.ts +4 -1
- package/dist/index.js +511 -2
- package/dist/store/index.d.ts +3 -0
- package/dist/store/store.ram.spec.d.ts +1 -0
- package/dist/store/store.ram.utils.d.ts +969 -0
- package/dist/store/store.shared-models.utils.d.ts +74 -0
- package/dist/store/store.zod.utils.d.ts +581 -0
- package/dist/value-objects/index.d.ts +1 -0
- package/dist/value-objects/pagination.value-object.d.ts +19 -0
- package/dist/zod-controller.spec.d.ts +1 -0
- package/dist/zod-controller.utils.d.ts +126 -0
- package/dist/zod-module.spec.d.ts +1 -0
- package/dist/zod-module.utils.d.ts +29 -0
- package/package.json +14 -3
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
declare function jsonResponse<T>(data: T, init?: ResponseInit): import("undici-types").Response;
|
|
2
|
+
import { type NextRequest, type NextResponse } from 'next/server';
|
|
3
|
+
import z, { type ZodType, type ZodObject, type ZodUnion } from 'zod';
|
|
4
|
+
import { type ControllerErrorModel, type ErrorBaseCreatingPayload } from './errors.utils';
|
|
5
|
+
export declare enum DefaultErrorCodes {
|
|
6
|
+
REQUEST_PARSING = "NZMT-CONTROLLER___REQUEST-PARSING",
|
|
7
|
+
RESPONSE_PARSING = "NZMT-CONTROLLER___RESPONSE-PARSING"
|
|
8
|
+
}
|
|
9
|
+
type SuccessResponse<ResponseZodSchema> = ResponseZodSchema extends undefined ? {} : z.infer<ResponseZodSchema>;
|
|
10
|
+
type ZodAPISchemas = {
|
|
11
|
+
body?: ZodObject | ZodUnion<[ZodObject<any>, ...ZodObject<any>[]]>;
|
|
12
|
+
query?: ZodObject | ZodUnion<[ZodObject<any>, ...ZodObject<any>[]]>;
|
|
13
|
+
response?: ZodType;
|
|
14
|
+
};
|
|
15
|
+
type ErrorResponse = {
|
|
16
|
+
code: string;
|
|
17
|
+
message?: string;
|
|
18
|
+
details?: any;
|
|
19
|
+
};
|
|
20
|
+
type EndpointErrorGenerator = (payload: string | ErrorBaseCreatingPayload, errorStatus?: number, cause?: unknown) => ControllerErrorModel;
|
|
21
|
+
export type Guard = (payload: {
|
|
22
|
+
request: NextRequest;
|
|
23
|
+
endpointError: EndpointErrorGenerator;
|
|
24
|
+
}) => Promise<undefined | ControllerErrorModel>;
|
|
25
|
+
export type OnErrorHandler = (request: {
|
|
26
|
+
error: ControllerErrorModel;
|
|
27
|
+
req: NextRequest;
|
|
28
|
+
}) => Promise<void>;
|
|
29
|
+
export type EndpointList<T extends Metadata> = {
|
|
30
|
+
[K in keyof T['schemas']]: (request: NextRequest) => Promise<ReturnType<typeof jsonResponse>>;
|
|
31
|
+
};
|
|
32
|
+
type EndpointLogic<T extends ZodAPISchemas> = {
|
|
33
|
+
handler: (payload: (T['query'] extends ZodType ? z.infer<T['query']> : {}) & (T['body'] extends ZodType ? z.infer<T['body']> : {}), request: {
|
|
34
|
+
request: NextRequest;
|
|
35
|
+
flags: Record<string, boolean>;
|
|
36
|
+
endpointError: EndpointErrorGenerator;
|
|
37
|
+
}) => Promise<T['response'] extends ZodType ? z.infer<T['response']> : void>;
|
|
38
|
+
guards?: Guard[];
|
|
39
|
+
eventHandlers?: {
|
|
40
|
+
onSuccess?: Array<(data: {
|
|
41
|
+
requestPayload: (T['query'] extends ZodType ? z.infer<T['query']> : {}) & (T['body'] extends ZodType ? z.infer<T['body']> : {});
|
|
42
|
+
request: NextRequest;
|
|
43
|
+
result: T['response'] extends ZodType ? z.infer<T['response']> : undefined;
|
|
44
|
+
flags: Record<string, boolean>;
|
|
45
|
+
}) => Promise<void>>;
|
|
46
|
+
onError?: Array<OnErrorHandler>;
|
|
47
|
+
};
|
|
48
|
+
customResponseLogic?: {
|
|
49
|
+
onSuccess?: (payload: {
|
|
50
|
+
req: NextRequest;
|
|
51
|
+
response: T['response'] extends undefined ? undefined : z.infer<T['response']>;
|
|
52
|
+
}) => Promise<NextResponse>;
|
|
53
|
+
onError?: (payload: {
|
|
54
|
+
req: NextRequest;
|
|
55
|
+
error: ControllerErrorModel;
|
|
56
|
+
}) => Promise<NextResponse>;
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
export type SharedConfig = Partial<{
|
|
60
|
+
guards: Guard[];
|
|
61
|
+
onErrorHandlers?: Array<OnErrorHandler>;
|
|
62
|
+
}>;
|
|
63
|
+
export declare const endpoints: <T extends Schemas>(metadata: {
|
|
64
|
+
schemas: T;
|
|
65
|
+
name: string;
|
|
66
|
+
}, sharedConfig?: SharedConfig) => <Method extends keyof T>(method: Method, handler: EndpointLogic<T[Method]>["handler"], configuration?: Omit<EndpointLogic<T[Method]>, "handler">) => (request: NextRequest) => Promise<import("undici-types").Response>;
|
|
67
|
+
type Schemas = Record<string, ZodAPISchemas>;
|
|
68
|
+
type Flatten<T extends Record<string, object>> = {
|
|
69
|
+
[K in keyof T]: T[K];
|
|
70
|
+
}[keyof T];
|
|
71
|
+
type OnlyObject<T> = T extends object ? T : never;
|
|
72
|
+
type SchemaShape = {
|
|
73
|
+
body?: unknown;
|
|
74
|
+
query?: unknown;
|
|
75
|
+
response?: unknown;
|
|
76
|
+
};
|
|
77
|
+
export type Metadata<T = Schemas> = {
|
|
78
|
+
name: string;
|
|
79
|
+
schemas: T;
|
|
80
|
+
};
|
|
81
|
+
type NormalizeSchema<T extends SchemaShape> = {
|
|
82
|
+
body: T extends {
|
|
83
|
+
body: infer B;
|
|
84
|
+
} ? B : undefined;
|
|
85
|
+
query: T extends {
|
|
86
|
+
query: infer Q;
|
|
87
|
+
} ? Q : undefined;
|
|
88
|
+
response: T extends {
|
|
89
|
+
response: infer R;
|
|
90
|
+
} ? R : undefined;
|
|
91
|
+
};
|
|
92
|
+
type RequestPayload<T extends SchemaShape> = Flatten<{
|
|
93
|
+
[K in Exclude<keyof NormalizeSchema<T>, 'response'> as OnlyObject<NormalizeSchema<T>[K]> extends never ? never : K]: z.infer<OnlyObject<NormalizeSchema<T>[K]>>;
|
|
94
|
+
}>;
|
|
95
|
+
type ResponsePayload<T extends SchemaShape> = T extends {
|
|
96
|
+
response: infer R;
|
|
97
|
+
} ? z.infer<R> : never;
|
|
98
|
+
type ZodAPIPayload<QueryParams, BodyParams> = (QueryParams extends undefined ? {} : {
|
|
99
|
+
query: z.infer<QueryParams>;
|
|
100
|
+
}) & (BodyParams extends undefined ? {} : {
|
|
101
|
+
body: z.infer<BodyParams>;
|
|
102
|
+
});
|
|
103
|
+
type ZodAPIMethod<Schemas extends {
|
|
104
|
+
body: unknown;
|
|
105
|
+
query: unknown;
|
|
106
|
+
response: unknown;
|
|
107
|
+
}> = {
|
|
108
|
+
payload: ZodAPIPayload<Schemas['query'], Schemas['body']>;
|
|
109
|
+
response: SuccessResponse<Schemas['response']>;
|
|
110
|
+
error: ErrorResponse;
|
|
111
|
+
};
|
|
112
|
+
export type Contract<Metadata extends {
|
|
113
|
+
schemas: Record<string, SchemaShape>;
|
|
114
|
+
}, CustomModels extends Record<string, unknown> | undefined = undefined> = {
|
|
115
|
+
endpoints: {
|
|
116
|
+
[K in keyof Metadata['schemas']]: ZodAPIMethod<NormalizeSchema<Metadata['schemas'][K]>>;
|
|
117
|
+
};
|
|
118
|
+
customModels: CustomModels extends undefined ? {} : CustomModels;
|
|
119
|
+
requestDTOs: {
|
|
120
|
+
[K in keyof Metadata['schemas']]: RequestPayload<Metadata['schemas'][K]>;
|
|
121
|
+
};
|
|
122
|
+
responseDTOs: {
|
|
123
|
+
[K in keyof Metadata['schemas']]: ResponsePayload<Metadata['schemas'][K]>;
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
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 Schemas = Record<string, {
|
|
5
|
+
payload: ZodType;
|
|
6
|
+
response: ZodType;
|
|
7
|
+
}>;
|
|
8
|
+
export type Metadata = {
|
|
9
|
+
name: string;
|
|
10
|
+
schemas: Schemas;
|
|
11
|
+
};
|
|
12
|
+
export type DTOs<T extends Metadata> = {
|
|
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 Methods<T extends Metadata> = {
|
|
18
|
+
[K in keyof T['schemas']]: (payload: z.infer<T['schemas'][K]['payload']>) => Promise<z.infer<T['schemas'][K]['response']>>;
|
|
19
|
+
};
|
|
20
|
+
export type Config = {
|
|
21
|
+
onError?: (e: ModuleErrorModel) => Promise<void>;
|
|
22
|
+
};
|
|
23
|
+
export declare const methods: <T extends Schemas>(metadata: {
|
|
24
|
+
schemas: T;
|
|
25
|
+
name: string;
|
|
26
|
+
}, sharedConfig?: Config) => <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?: Config) => (payload: z.infer<T[Method]["payload"]>) => Promise<z.infer<T[Method]["response"]>>;
|
|
29
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "springnext",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"exports": {
|
|
@@ -17,11 +17,22 @@
|
|
|
17
17
|
],
|
|
18
18
|
"scripts": {
|
|
19
19
|
"build": "rslib",
|
|
20
|
-
"dev": "rslib --watch"
|
|
20
|
+
"dev": "rslib --watch",
|
|
21
|
+
"test": "rstest"
|
|
21
22
|
},
|
|
22
23
|
"devDependencies": {
|
|
23
|
-
"@rslib/core": "^0.20.
|
|
24
|
+
"@rslib/core": "^0.20.0",
|
|
25
|
+
"@rstest/adapter-rslib": "^0.2.1",
|
|
26
|
+
"@rstest/core": "^0.9.0",
|
|
24
27
|
"@types/node": "^24.12.0",
|
|
28
|
+
"next": "^16.1.6",
|
|
29
|
+
"zod": "^4.3.6",
|
|
25
30
|
"typescript": "^5.9.3"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"next": ">=13 <17",
|
|
34
|
+
"zod": ">=3 <5",
|
|
35
|
+
"inversify": ">=7 <9",
|
|
36
|
+
"reflect-metadata": "^0.2.2"
|
|
26
37
|
}
|
|
27
38
|
}
|