@lucaapp/service-utils 1.31.0 → 1.32.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/dist/lib/api/api.d.ts +15 -5
- package/dist/lib/api/api.js +17 -8
- package/dist/lib/api/endpoint.d.ts +3 -2
- package/dist/lib/api/endpoint.js +4 -2
- package/dist/lib/api/types/endpoint.d.ts +11 -12
- package/dist/lib/api/types/middleware.d.ts +16 -16
- package/dist/lib/api/types/utils.d.ts +2 -0
- package/package.json +1 -1
package/dist/lib/api/api.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import 'express-async-errors';
|
|
|
3
3
|
import { OpenAPIRegistry } from '@asteasolutions/zod-to-openapi';
|
|
4
4
|
import { EndpointHandler, EndpointOptions } from './types/endpoint';
|
|
5
5
|
import { Middleware, EndpointResponseSchema } from './types/middleware';
|
|
6
|
+
import { ZodObjectSchemaOrUndefined } from './types/utils';
|
|
6
7
|
type OpenApiVersion = '3.0.0' | '3.0.1' | '3.0.2' | '3.0.3' | '3.1.0';
|
|
7
8
|
type ApiConstructorOptions = {
|
|
8
9
|
prefixPath?: string;
|
|
@@ -11,6 +12,13 @@ type ApiConstructorOptions = {
|
|
|
11
12
|
apiTitle?: string;
|
|
12
13
|
apiDescription?: string;
|
|
13
14
|
debug?: boolean;
|
|
15
|
+
tags?: string[];
|
|
16
|
+
registry?: OpenAPIRegistry;
|
|
17
|
+
router?: Router;
|
|
18
|
+
};
|
|
19
|
+
type ApiChildOptions = {
|
|
20
|
+
prefixPath?: string;
|
|
21
|
+
tags?: string[];
|
|
14
22
|
};
|
|
15
23
|
export declare class Api {
|
|
16
24
|
router: Router;
|
|
@@ -21,12 +29,14 @@ export declare class Api {
|
|
|
21
29
|
apiDescription: string;
|
|
22
30
|
private prefixPath;
|
|
23
31
|
debug: boolean;
|
|
32
|
+
tags: string[];
|
|
24
33
|
constructor(options?: ApiConstructorOptions);
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
34
|
+
child(options: ApiChildOptions): Api;
|
|
35
|
+
get<TResponseSchemas extends ReadonlyArray<EndpointResponseSchema>, TRequestBodySchema extends ZodObjectSchemaOrUndefined = undefined, TRequestParamsSchema extends ZodObjectSchemaOrUndefined = undefined, TRequestQuerySchema extends ZodObjectSchemaOrUndefined = undefined, TRequestHeadersSchema extends ZodObjectSchemaOrUndefined = undefined, TMiddlewares extends ReadonlyArray<Middleware<any, any, any, any, any, any>> | undefined = undefined>(path: string, summary: string, options: EndpointOptions<TResponseSchemas, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TMiddlewares>, handler: EndpointHandler<TResponseSchemas, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TMiddlewares>): void;
|
|
36
|
+
post<TResponseSchemas extends ReadonlyArray<EndpointResponseSchema>, TRequestBodySchema extends ZodObjectSchemaOrUndefined = undefined, TRequestParamsSchema extends ZodObjectSchemaOrUndefined = undefined, TRequestQuerySchema extends ZodObjectSchemaOrUndefined = undefined, TRequestHeadersSchema extends ZodObjectSchemaOrUndefined = undefined, TMiddlewares extends ReadonlyArray<Middleware<any, any, any, any, any, any>> | undefined = undefined>(path: string, summary: string, options: EndpointOptions<TResponseSchemas, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TMiddlewares>, handler: EndpointHandler<TResponseSchemas, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TMiddlewares>): void;
|
|
37
|
+
patch<TResponseSchemas extends ReadonlyArray<EndpointResponseSchema>, TRequestBodySchema extends ZodObjectSchemaOrUndefined = undefined, TRequestParamsSchema extends ZodObjectSchemaOrUndefined = undefined, TRequestQuerySchema extends ZodObjectSchemaOrUndefined = undefined, TRequestHeadersSchema extends ZodObjectSchemaOrUndefined = undefined, TMiddlewares extends ReadonlyArray<Middleware<any, any, any, any, any, any>> | undefined = undefined>(path: string, summary: string, options: EndpointOptions<TResponseSchemas, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TMiddlewares>, handler: EndpointHandler<TResponseSchemas, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TMiddlewares>): void;
|
|
38
|
+
put<TResponseSchemas extends ReadonlyArray<EndpointResponseSchema>, TRequestBodySchema extends ZodObjectSchemaOrUndefined = undefined, TRequestParamsSchema extends ZodObjectSchemaOrUndefined = undefined, TRequestQuerySchema extends ZodObjectSchemaOrUndefined = undefined, TRequestHeadersSchema extends ZodObjectSchemaOrUndefined = undefined, TMiddlewares extends ReadonlyArray<Middleware<any, any, any, any, any, any>> | undefined = undefined>(path: string, summary: string, options: EndpointOptions<TResponseSchemas, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TMiddlewares>, handler: EndpointHandler<TResponseSchemas, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TMiddlewares>): void;
|
|
39
|
+
delete<TResponseSchemas extends ReadonlyArray<EndpointResponseSchema>, TRequestBodySchema extends ZodObjectSchemaOrUndefined = undefined, TRequestParamsSchema extends ZodObjectSchemaOrUndefined = undefined, TRequestQuerySchema extends ZodObjectSchemaOrUndefined = undefined, TRequestHeadersSchema extends ZodObjectSchemaOrUndefined = undefined, TMiddlewares extends ReadonlyArray<Middleware<any, any, any, any, any, any>> | undefined = undefined>(path: string, summary: string, options: EndpointOptions<TResponseSchemas, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TMiddlewares>, handler: EndpointHandler<TResponseSchemas, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TMiddlewares>): void;
|
|
30
40
|
generateOpenAPISpec(): import("openapi3-ts").OpenAPIObject;
|
|
31
41
|
mountSwaggerMiddlewares(): void;
|
|
32
42
|
}
|
package/dist/lib/api/api.js
CHANGED
|
@@ -11,35 +11,44 @@ const zod_to_openapi_1 = require("@asteasolutions/zod-to-openapi");
|
|
|
11
11
|
const endpoint_1 = require("./endpoint");
|
|
12
12
|
class Api {
|
|
13
13
|
constructor(options = {}) {
|
|
14
|
-
this.router = (0, express_1.Router)();
|
|
15
|
-
this.registry = new zod_to_openapi_1.OpenAPIRegistry();
|
|
14
|
+
this.router = options.router || (0, express_1.Router)();
|
|
15
|
+
this.registry = options.registry || new zod_to_openapi_1.OpenAPIRegistry();
|
|
16
16
|
this.prefixPath = options.prefixPath || '';
|
|
17
17
|
this.openApiVersion = options.openApiVersion || '3.0.0';
|
|
18
18
|
this.apiVersion = options.apiVersion || '1.0.0';
|
|
19
19
|
this.apiTitle = options.apiTitle || 'API';
|
|
20
20
|
this.apiDescription = options.apiDescription || '';
|
|
21
21
|
this.debug = options.debug || false;
|
|
22
|
-
this.
|
|
22
|
+
this.tags = options.tags || [];
|
|
23
|
+
}
|
|
24
|
+
child(options) {
|
|
25
|
+
return new Api({
|
|
26
|
+
prefixPath: this.prefixPath + (options.prefixPath || ''),
|
|
27
|
+
debug: this.debug,
|
|
28
|
+
tags: [...this.tags, ...(options.tags || [])],
|
|
29
|
+
registry: this.registry,
|
|
30
|
+
router: this.router,
|
|
31
|
+
});
|
|
23
32
|
}
|
|
24
33
|
get(path, summary, options, handler) {
|
|
25
34
|
(0, endpoint_1.mountEndpoint)(this.router, 'get', path, options, handler, this.debug);
|
|
26
|
-
(0, endpoint_1.registerEndpoint)(this.registry, 'get', this.prefixPath, path, summary, options, this.debug);
|
|
35
|
+
(0, endpoint_1.registerEndpoint)(this.registry, 'get', this.prefixPath, path, summary, options, this.tags, this.debug);
|
|
27
36
|
}
|
|
28
37
|
post(path, summary, options, handler) {
|
|
29
38
|
(0, endpoint_1.mountEndpoint)(this.router, 'post', path, options, handler, this.debug);
|
|
30
|
-
(0, endpoint_1.registerEndpoint)(this.registry, 'post', this.prefixPath, path, summary, options, this.debug);
|
|
39
|
+
(0, endpoint_1.registerEndpoint)(this.registry, 'post', this.prefixPath, path, summary, options, this.tags, this.debug);
|
|
31
40
|
}
|
|
32
41
|
patch(path, summary, options, handler) {
|
|
33
42
|
(0, endpoint_1.mountEndpoint)(this.router, 'patch', path, options, handler, this.debug);
|
|
34
|
-
(0, endpoint_1.registerEndpoint)(this.registry, 'patch', this.prefixPath, path, summary, options, this.debug);
|
|
43
|
+
(0, endpoint_1.registerEndpoint)(this.registry, 'patch', this.prefixPath, path, summary, options, this.tags, this.debug);
|
|
35
44
|
}
|
|
36
45
|
put(path, summary, options, handler) {
|
|
37
46
|
(0, endpoint_1.mountEndpoint)(this.router, 'put', path, options, handler, this.debug);
|
|
38
|
-
(0, endpoint_1.registerEndpoint)(this.registry, 'put', this.prefixPath, path, summary, options, this.debug);
|
|
47
|
+
(0, endpoint_1.registerEndpoint)(this.registry, 'put', this.prefixPath, path, summary, options, this.tags, this.debug);
|
|
39
48
|
}
|
|
40
49
|
delete(path, summary, options, handler) {
|
|
41
50
|
(0, endpoint_1.mountEndpoint)(this.router, 'delete', path, options, handler, this.debug);
|
|
42
|
-
(0, endpoint_1.registerEndpoint)(this.registry, 'delete', this.prefixPath, path, summary, options, this.debug);
|
|
51
|
+
(0, endpoint_1.registerEndpoint)(this.registry, 'delete', this.prefixPath, path, summary, options, this.tags, this.debug);
|
|
43
52
|
}
|
|
44
53
|
generateOpenAPISpec() {
|
|
45
54
|
const generator = new zod_to_openapi_1.OpenAPIGenerator(this.registry.definitions, this.openApiVersion);
|
|
@@ -2,7 +2,8 @@ import { Router } from 'express';
|
|
|
2
2
|
import { OpenAPIRegistry } from '@asteasolutions/zod-to-openapi';
|
|
3
3
|
import { Middleware, EndpointResponseSchema } from './types/middleware';
|
|
4
4
|
import { EndpointOptions, EndpointHandler } from './types/endpoint';
|
|
5
|
+
import { ZodObjectSchemaOrUndefined } from './types/utils';
|
|
5
6
|
type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch';
|
|
6
|
-
export declare const mountEndpoint: <TResponseSchemas extends readonly EndpointResponseSchema[],
|
|
7
|
-
export declare const registerEndpoint: (registry: OpenAPIRegistry, method: HttpMethod, prefixPath: string, path: string, summary: string, options: EndpointOptions<any, any, any, any, any, any>, debug: boolean) => void;
|
|
7
|
+
export declare const mountEndpoint: <TResponseSchemas extends readonly EndpointResponseSchema[], TRequestBodySchema extends ZodObjectSchemaOrUndefined, TRequestParamsSchema extends ZodObjectSchemaOrUndefined, TRequestQuerySchema extends ZodObjectSchemaOrUndefined, TRequestHeadersSchema extends ZodObjectSchemaOrUndefined, TMiddlewares extends readonly Middleware<any, any, any, any, any, any>[] | undefined = undefined>(router: Router, method: HttpMethod, path: string, options: EndpointOptions<TResponseSchemas, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TMiddlewares>, handler: EndpointHandler<TResponseSchemas, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TMiddlewares>, debug: boolean) => void;
|
|
8
|
+
export declare const registerEndpoint: (registry: OpenAPIRegistry, method: HttpMethod, prefixPath: string, path: string, summary: string, options: EndpointOptions<any, any, any, any, any, any>, tags: string[], debug: boolean) => void;
|
|
8
9
|
export {};
|
package/dist/lib/api/endpoint.js
CHANGED
|
@@ -7,6 +7,7 @@ exports.registerEndpoint = exports.mountEndpoint = void 0;
|
|
|
7
7
|
const zod_1 = require("zod");
|
|
8
8
|
const assert_1 = __importDefault(require("assert"));
|
|
9
9
|
const http_1 = require("./types/http");
|
|
10
|
+
const mapExpressParamsToOpenAPIParams = (path) => path.replaceAll(/\:([^\:\/]+)/g, '{$1}');
|
|
10
11
|
const buildResponseConfig = (responseOptions) => {
|
|
11
12
|
const responses = {};
|
|
12
13
|
responseOptions.forEach(response => {
|
|
@@ -213,7 +214,7 @@ const mountEndpoint = (router, method, path, options, handler, debug) => {
|
|
|
213
214
|
exports.mountEndpoint = mountEndpoint;
|
|
214
215
|
const registerEndpoint = (registry, method, prefixPath, path, summary,
|
|
215
216
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
216
|
-
options, debug) => {
|
|
217
|
+
options, tags, debug) => {
|
|
217
218
|
let bodySchema = options.schemas?.body || undefined;
|
|
218
219
|
let paramsSchema = options.schemas?.params || undefined;
|
|
219
220
|
let querySchema = options.schemas?.query || undefined;
|
|
@@ -297,8 +298,9 @@ options, debug) => {
|
|
|
297
298
|
const responseMap = buildResponseConfig(responseSchemas);
|
|
298
299
|
registry.registerPath({
|
|
299
300
|
method,
|
|
300
|
-
path: (prefixPath + path)
|
|
301
|
+
path: mapExpressParamsToOpenAPIParams(prefixPath + path),
|
|
301
302
|
summary,
|
|
303
|
+
tags,
|
|
302
304
|
// description: summary,
|
|
303
305
|
request: {
|
|
304
306
|
...(bodySchema && {
|
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
1
|
import { Middleware, EndpointResponseSchema } from './middleware';
|
|
3
|
-
import { ArrayToUnion, ExtractZodOutput, ExtractZodOutputFromMiddleware, Always, Merge } from './utils';
|
|
4
|
-
export type EndpointHandler<TResponse,
|
|
5
|
-
body:
|
|
6
|
-
params:
|
|
7
|
-
query:
|
|
8
|
-
headers:
|
|
2
|
+
import { ArrayToUnion, ExtractZodOutput, ExtractZodOutputFromMiddleware, Always, Merge, ZodSchemaOuput } from './utils';
|
|
3
|
+
export type EndpointHandler<TResponse, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TMiddlewares> = (request: {
|
|
4
|
+
body: ZodSchemaOuput<TRequestBodySchema>;
|
|
5
|
+
params: ZodSchemaOuput<TRequestParamsSchema>;
|
|
6
|
+
query: ZodSchemaOuput<TRequestQuerySchema>;
|
|
7
|
+
headers: ZodSchemaOuput<TRequestHeadersSchema>;
|
|
9
8
|
}, context: Merge<Always<ExtractZodOutputFromMiddleware<ArrayToUnion<TMiddlewares>>>>, send: (response: ExtractZodOutput<ArrayToUnion<TResponse>>) => void) => Promise<void>;
|
|
10
|
-
export type EndpointOptions<TResponseSchemas extends ReadonlyArray<EndpointResponseSchema>,
|
|
9
|
+
export type EndpointOptions<TResponseSchemas extends ReadonlyArray<EndpointResponseSchema>, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TMiddlewares extends ReadonlyArray<Middleware<any, any, any, any, any, any>> | undefined> = {
|
|
11
10
|
schemas?: {
|
|
12
|
-
body?:
|
|
13
|
-
params?:
|
|
14
|
-
query?:
|
|
15
|
-
headers?:
|
|
11
|
+
body?: TRequestBodySchema;
|
|
12
|
+
params?: TRequestParamsSchema;
|
|
13
|
+
query?: TRequestQuerySchema;
|
|
14
|
+
headers?: TRequestHeadersSchema;
|
|
16
15
|
};
|
|
17
16
|
middlewares?: TMiddlewares;
|
|
18
17
|
responses: TResponseSchemas;
|
|
@@ -1,32 +1,32 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
-
import { ArrayToUnion, ExtractZodOutput } from './utils';
|
|
2
|
+
import { ArrayToUnion, ExtractZodOutput, ZodSchemaOuput } from './utils';
|
|
3
3
|
export type EndpointResponseSchema = {
|
|
4
4
|
status: number;
|
|
5
5
|
description: string;
|
|
6
6
|
schema: z.ZodSchema<any> | z.ZodVoid;
|
|
7
7
|
};
|
|
8
|
-
export type MiddlewareHandler<TResponseSchema,
|
|
9
|
-
body:
|
|
10
|
-
params:
|
|
11
|
-
query:
|
|
12
|
-
headers:
|
|
8
|
+
export type MiddlewareHandler<TResponseSchema, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TContextSchema> = (request: {
|
|
9
|
+
body: ZodSchemaOuput<TRequestBodySchema>;
|
|
10
|
+
params: ZodSchemaOuput<TRequestParamsSchema>;
|
|
11
|
+
query: ZodSchemaOuput<TRequestQuerySchema>;
|
|
12
|
+
headers: ZodSchemaOuput<TRequestHeadersSchema>;
|
|
13
13
|
ip: string;
|
|
14
14
|
baseUrl: string;
|
|
15
15
|
path: string;
|
|
16
16
|
method: string;
|
|
17
|
-
}, send: (response: ExtractZodOutput<ArrayToUnion<TResponseSchema>>) => void, next: (context:
|
|
18
|
-
export type MiddlewareOptions<TResponseSchemas extends ReadonlyArray<EndpointResponseSchema>,
|
|
17
|
+
}, send: (response: ExtractZodOutput<ArrayToUnion<TResponseSchema>>) => void, next: (context: ZodSchemaOuput<TContextSchema>) => void) => Promise<void>;
|
|
18
|
+
export type MiddlewareOptions<TResponseSchemas extends ReadonlyArray<EndpointResponseSchema>, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TContextSchema> = {
|
|
19
19
|
schemas?: {
|
|
20
|
-
body?:
|
|
21
|
-
params?:
|
|
22
|
-
query?:
|
|
23
|
-
headers?:
|
|
24
|
-
context?:
|
|
20
|
+
body?: TRequestBodySchema;
|
|
21
|
+
params?: TRequestParamsSchema;
|
|
22
|
+
query?: TRequestQuerySchema;
|
|
23
|
+
headers?: TRequestHeadersSchema;
|
|
24
|
+
context?: TContextSchema;
|
|
25
25
|
};
|
|
26
26
|
responses: TResponseSchemas;
|
|
27
27
|
errors?: Record<string, number>;
|
|
28
28
|
};
|
|
29
|
-
export type Middleware<TResponseSchemas extends ReadonlyArray<EndpointResponseSchema>,
|
|
30
|
-
options: MiddlewareOptions<TResponseSchemas,
|
|
31
|
-
handler: MiddlewareHandler<TResponseSchemas,
|
|
29
|
+
export type Middleware<TResponseSchemas extends ReadonlyArray<EndpointResponseSchema>, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TContextSchema> = {
|
|
30
|
+
options: MiddlewareOptions<TResponseSchemas, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TContextSchema>;
|
|
31
|
+
handler: MiddlewareHandler<TResponseSchemas, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TContextSchema>;
|
|
32
32
|
};
|
|
@@ -7,6 +7,8 @@ export type ExtractZodOutput<T> = T extends {
|
|
|
7
7
|
status: Z;
|
|
8
8
|
body: V;
|
|
9
9
|
} : never;
|
|
10
|
+
export type ZodObjectSchemaOrUndefined = z.ZodObject<any> | undefined;
|
|
11
|
+
export type ZodSchemaOuput<T> = T extends z.ZodSchema<any> ? z.infer<T> : undefined;
|
|
10
12
|
export type ExtractZodOutputFromMiddleware<T> = T extends {
|
|
11
13
|
options: {
|
|
12
14
|
schemas?: {
|