@lucaapp/service-utils 1.31.0 → 1.32.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/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/response.d.ts +15 -0
- package/dist/lib/api/response.js +11 -1
- 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/dist/lib/negotiator/language.d.ts +3 -0
- package/dist/lib/negotiator/language.js +13 -1
- 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 && {
|
|
@@ -41,6 +41,16 @@ declare const notFoundErrorSchema: z.ZodObject<{
|
|
|
41
41
|
message: string;
|
|
42
42
|
error: HTTPStatus.NOT_FOUND;
|
|
43
43
|
}>;
|
|
44
|
+
declare const tooManyRequestsErrorSchema: z.ZodObject<{
|
|
45
|
+
error: z.ZodLiteral<HTTPStatus.TOO_MANY_REQUESTS>;
|
|
46
|
+
message: z.ZodString;
|
|
47
|
+
}, "strip", z.ZodTypeAny, {
|
|
48
|
+
message: string;
|
|
49
|
+
error: HTTPStatus.TOO_MANY_REQUESTS;
|
|
50
|
+
}, {
|
|
51
|
+
message: string;
|
|
52
|
+
error: HTTPStatus.TOO_MANY_REQUESTS;
|
|
53
|
+
}>;
|
|
44
54
|
export declare const okResponse: <T>(schema: z.ZodType<T, z.ZodTypeDef, T>) => {
|
|
45
55
|
status: 200;
|
|
46
56
|
description: string;
|
|
@@ -71,4 +81,9 @@ export declare const notFoundResponse: () => {
|
|
|
71
81
|
description: string;
|
|
72
82
|
schema: typeof notFoundErrorSchema;
|
|
73
83
|
};
|
|
84
|
+
export declare const tooManyRequestsResponse: () => {
|
|
85
|
+
status: 429;
|
|
86
|
+
description: string;
|
|
87
|
+
schema: typeof tooManyRequestsErrorSchema;
|
|
88
|
+
};
|
|
74
89
|
export {};
|
package/dist/lib/api/response.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.notFoundResponse = exports.forbiddenResponse = exports.unauthorizedResponse = exports.badRequestResponse = exports.noContentResponse = exports.okResponse = void 0;
|
|
3
|
+
exports.tooManyRequestsResponse = exports.notFoundResponse = exports.forbiddenResponse = exports.unauthorizedResponse = exports.badRequestResponse = exports.noContentResponse = exports.okResponse = void 0;
|
|
4
4
|
const zod_to_openapi_1 = require("@asteasolutions/zod-to-openapi");
|
|
5
5
|
const zod_1 = require("zod");
|
|
6
6
|
const http_1 = require("./types/http");
|
|
@@ -23,6 +23,10 @@ const notFoundErrorSchema = zod_1.z.object({
|
|
|
23
23
|
error: zod_1.z.literal(http_1.HTTPStatus.NOT_FOUND),
|
|
24
24
|
message: zod_1.z.string(),
|
|
25
25
|
});
|
|
26
|
+
const tooManyRequestsErrorSchema = zod_1.z.object({
|
|
27
|
+
error: zod_1.z.literal(http_1.HTTPStatus.TOO_MANY_REQUESTS),
|
|
28
|
+
message: zod_1.z.string(),
|
|
29
|
+
});
|
|
26
30
|
// response functions
|
|
27
31
|
const okResponse = (schema) => ({
|
|
28
32
|
status: 200,
|
|
@@ -60,3 +64,9 @@ const notFoundResponse = () => ({
|
|
|
60
64
|
schema: notFoundErrorSchema,
|
|
61
65
|
});
|
|
62
66
|
exports.notFoundResponse = notFoundResponse;
|
|
67
|
+
const tooManyRequestsResponse = () => ({
|
|
68
|
+
status: 429,
|
|
69
|
+
description: 'Too Many Requests',
|
|
70
|
+
schema: tooManyRequestsErrorSchema,
|
|
71
|
+
});
|
|
72
|
+
exports.tooManyRequestsResponse = tooManyRequestsResponse;
|
|
@@ -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?: {
|
|
@@ -2,4 +2,7 @@ import { Request } from 'express';
|
|
|
2
2
|
export type InternalLanguages = 'de' | 'en';
|
|
3
3
|
export declare const DEFAULT_LANGUAGE = "de";
|
|
4
4
|
export declare const AVAILABLE_LANGUAGES: string[];
|
|
5
|
+
type LanguageOptions = typeof AVAILABLE_LANGUAGES[number];
|
|
6
|
+
export declare const parseAcceptLanguageHeader: (header?: string) => LanguageOptions;
|
|
5
7
|
export declare const getPreferredLanguageFromRequest: (request: Request) => string;
|
|
8
|
+
export {};
|
|
@@ -3,10 +3,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.getPreferredLanguageFromRequest = exports.AVAILABLE_LANGUAGES = exports.DEFAULT_LANGUAGE = void 0;
|
|
6
|
+
exports.getPreferredLanguageFromRequest = exports.parseAcceptLanguageHeader = exports.AVAILABLE_LANGUAGES = exports.DEFAULT_LANGUAGE = void 0;
|
|
7
7
|
const negotiator_1 = __importDefault(require("negotiator"));
|
|
8
8
|
exports.DEFAULT_LANGUAGE = 'de';
|
|
9
9
|
exports.AVAILABLE_LANGUAGES = ['en', 'de'];
|
|
10
|
+
const parseAcceptLanguageHeader = (header) => {
|
|
11
|
+
if (!header) {
|
|
12
|
+
return exports.DEFAULT_LANGUAGE;
|
|
13
|
+
}
|
|
14
|
+
const negotiator = new negotiator_1.default({
|
|
15
|
+
headers: {
|
|
16
|
+
'accept-language': header,
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
return (negotiator.language(exports.AVAILABLE_LANGUAGES) ?? 'en');
|
|
20
|
+
};
|
|
21
|
+
exports.parseAcceptLanguageHeader = parseAcceptLanguageHeader;
|
|
10
22
|
const getPreferredLanguageFromRequest = (request) => {
|
|
11
23
|
const acceptLanguageHeader = request.headers['accept-language'];
|
|
12
24
|
if (!acceptLanguageHeader) {
|