@lucaapp/service-utils 1.56.8 → 1.56.10
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/endpoint.d.ts +1 -1
- package/dist/lib/api/endpoint.js +18 -8
- package/dist/lib/api/middleware.d.ts +1 -1
- package/dist/lib/api/response.d.ts +10 -4
- package/dist/lib/api/response.js +8 -1
- package/dist/lib/api/send.d.ts +12 -4
- package/dist/lib/api/send.js +10 -1
- package/dist/lib/api/types/endpoint.d.ts +5 -1
- package/dist/lib/api/types/middleware.d.ts +1 -1
- package/dist/lib/email/emailDataType.js +1 -2
- package/dist/lib/kafka/events/location.d.ts +2 -0
- package/dist/lib/logger/index.d.ts +0 -3
- package/dist/lib/requestTracer/requestTracer.d.ts +1 -4
- package/package.json +1 -1
|
@@ -3,5 +3,5 @@ import { Middleware, EndpointResponseSchema } from './types/middleware';
|
|
|
3
3
|
import { EndpointOptions, EndpointHandler } from './types/endpoint';
|
|
4
4
|
import { ZodObjectSchemaOrUndefined } from './types/utils';
|
|
5
5
|
type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch';
|
|
6
|
-
export declare const mountEndpoint: <TResponseSchemas extends
|
|
6
|
+
export declare const mountEndpoint: <TResponseSchemas extends ReadonlyArray<EndpointResponseSchema>, TRequestBodySchema extends ZodObjectSchemaOrUndefined, TRequestParamsSchema extends ZodObjectSchemaOrUndefined, TRequestQuerySchema extends ZodObjectSchemaOrUndefined, TRequestHeadersSchema extends ZodObjectSchemaOrUndefined, TMiddlewares extends ReadonlyArray<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;
|
|
7
7
|
export {};
|
package/dist/lib/api/endpoint.js
CHANGED
|
@@ -9,6 +9,9 @@ const assert_1 = __importDefault(require("assert"));
|
|
|
9
9
|
const http_1 = require("./types/http");
|
|
10
10
|
const libphonenumber_js_1 = require("libphonenumber-js");
|
|
11
11
|
const busboy_1 = __importDefault(require("busboy"));
|
|
12
|
+
const isRawResponse = (response) => {
|
|
13
|
+
return response.isRawResponseEnd === true;
|
|
14
|
+
};
|
|
12
15
|
const sendBadContextError = (response, error, debug) => {
|
|
13
16
|
response.err = error;
|
|
14
17
|
return response.status(500).send({
|
|
@@ -120,9 +123,12 @@ const isTypedError = (error) => {
|
|
|
120
123
|
};
|
|
121
124
|
const validateAndSendResponse = async (expressResponse, response, validResponses, debug) => {
|
|
122
125
|
try {
|
|
126
|
+
if (isRawResponse(response)) {
|
|
127
|
+
expressResponse.end();
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
123
130
|
(0, assert_1.default)(typeof response.status === 'number');
|
|
124
|
-
|
|
125
|
-
const responseSchema = validResponses.find(responseSchema => responseSchema.status === response.status);
|
|
131
|
+
const responseSchema = validResponses.find(r => r.status === response.status);
|
|
126
132
|
const schema = responseSchema?.schema;
|
|
127
133
|
(0, assert_1.default)(schema);
|
|
128
134
|
if (response.headers) {
|
|
@@ -130,10 +136,16 @@ const validateAndSendResponse = async (expressResponse, response, validResponses
|
|
|
130
136
|
expressResponse.set(header, value);
|
|
131
137
|
});
|
|
132
138
|
}
|
|
133
|
-
|
|
134
|
-
|
|
139
|
+
const validatedResponseBody = await schema
|
|
140
|
+
.parseAsync(response.body)
|
|
141
|
+
.catch(error => {
|
|
142
|
+
if (debug || process.env.NODE_ENV !== 'production') {
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
console.error('Response schema validation failed:', error);
|
|
146
|
+
return response.body; // Return original body in production
|
|
147
|
+
});
|
|
135
148
|
if (validatedResponseBody === undefined) {
|
|
136
|
-
// No Content Response
|
|
137
149
|
expressResponse.status(response.status).end();
|
|
138
150
|
return;
|
|
139
151
|
}
|
|
@@ -157,7 +169,6 @@ async function stream2buffer(stream) {
|
|
|
157
169
|
}
|
|
158
170
|
function handleFileUpload(request) {
|
|
159
171
|
return new Promise((resolve, reject) => {
|
|
160
|
-
console.log('Handling file upload');
|
|
161
172
|
const bb = (0, busboy_1.default)({
|
|
162
173
|
headers: request.headers,
|
|
163
174
|
defCharset: 'utf8',
|
|
@@ -168,7 +179,6 @@ function handleFileUpload(request) {
|
|
|
168
179
|
});
|
|
169
180
|
let file = undefined;
|
|
170
181
|
bb.once('error', error => {
|
|
171
|
-
console.log('err');
|
|
172
182
|
reject(error);
|
|
173
183
|
});
|
|
174
184
|
bb.on('file', async (fieldname, fileStream, filename) => {
|
|
@@ -302,7 +312,7 @@ const mountEndpoint = (router, method, path, options, handler, debug) => {
|
|
|
302
312
|
}
|
|
303
313
|
await handler(handlerRequest, ctx, async (controllerResponse) => {
|
|
304
314
|
await validateAndSendResponse(response, controllerResponse, options.responses, debug);
|
|
305
|
-
});
|
|
315
|
+
}, response);
|
|
306
316
|
}
|
|
307
317
|
catch (error) {
|
|
308
318
|
sendErrorResponse(response, error, options.errors, debug);
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { Middleware, MiddlewareOptions, MiddlewareHandler, EndpointResponseSchema } from './types/middleware';
|
|
2
|
-
export declare const createMiddleware: <TResponseSchemas extends
|
|
2
|
+
export declare const createMiddleware: <TResponseSchemas extends ReadonlyArray<EndpointResponseSchema>, TRequestBody = undefined, TRequestParams = undefined, TRequestQuery = undefined, TRequestHeaders = undefined, TContext = undefined>(options: MiddlewareOptions<TResponseSchemas, TRequestBody, TRequestParams, TRequestQuery, TRequestHeaders, TContext>, handler: MiddlewareHandler<TResponseSchemas, TRequestBody, TRequestParams, TRequestQuery, TRequestHeaders, TContext>) => Middleware<TResponseSchemas, TRequestBody, TRequestParams, TRequestQuery, TRequestHeaders, TContext>;
|
|
@@ -1,18 +1,24 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
declare const noContentSchema: z.ZodVoid;
|
|
3
|
-
export declare const okResponse: <T>(schema?: z.
|
|
3
|
+
export declare const okResponse: <T>(schema?: z.ZodSchema<T>) => {
|
|
4
4
|
status: 200;
|
|
5
5
|
description: string;
|
|
6
|
-
schema: z.
|
|
6
|
+
schema: z.ZodSchema<T> | typeof noContentSchema;
|
|
7
7
|
};
|
|
8
|
-
export declare const createdResponse: <T>(schema?: z.
|
|
8
|
+
export declare const createdResponse: <T>(schema?: z.ZodSchema<T>) => {
|
|
9
9
|
status: 201;
|
|
10
10
|
description: string;
|
|
11
|
-
schema: z.
|
|
11
|
+
schema: z.ZodSchema<T> | typeof noContentSchema;
|
|
12
12
|
};
|
|
13
13
|
export declare const noContentResponse: () => {
|
|
14
14
|
status: 204;
|
|
15
15
|
description: string;
|
|
16
16
|
schema: typeof noContentSchema;
|
|
17
17
|
};
|
|
18
|
+
export declare const rawResponseEnd: () => {
|
|
19
|
+
status: number;
|
|
20
|
+
isRawResponseEnd: true;
|
|
21
|
+
description: string;
|
|
22
|
+
schema: typeof noContentSchema;
|
|
23
|
+
};
|
|
18
24
|
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.noContentResponse = exports.createdResponse = exports.okResponse = void 0;
|
|
3
|
+
exports.rawResponseEnd = exports.noContentResponse = exports.createdResponse = 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
|
(0, zod_to_openapi_1.extendZodWithOpenApi)(zod_1.z);
|
|
@@ -25,3 +25,10 @@ const noContentResponse = () => ({
|
|
|
25
25
|
schema: noContentSchema,
|
|
26
26
|
});
|
|
27
27
|
exports.noContentResponse = noContentResponse;
|
|
28
|
+
const rawResponseEnd = () => ({
|
|
29
|
+
status: 0,
|
|
30
|
+
isRawResponseEnd: true,
|
|
31
|
+
description: 'rawEnd',
|
|
32
|
+
schema: noContentSchema,
|
|
33
|
+
});
|
|
34
|
+
exports.rawResponseEnd = rawResponseEnd;
|
package/dist/lib/api/send.d.ts
CHANGED
|
@@ -1,15 +1,23 @@
|
|
|
1
|
-
export declare const ok: <T, U>(body?: T
|
|
1
|
+
export declare const ok: <T, U>(body?: T, headers?: U) => {
|
|
2
2
|
status: 200;
|
|
3
3
|
body: T;
|
|
4
4
|
headers: U;
|
|
5
5
|
};
|
|
6
|
-
export declare const created: <T, U>(body?: T
|
|
6
|
+
export declare const created: <T, U>(body?: T, headers?: U) => {
|
|
7
7
|
status: 201;
|
|
8
8
|
body: T;
|
|
9
9
|
headers: U;
|
|
10
10
|
};
|
|
11
|
-
export declare const noContent: <T>(headers?: T
|
|
11
|
+
export declare const noContent: <T>(headers?: T) => {
|
|
12
12
|
status: 204;
|
|
13
13
|
body: void;
|
|
14
|
-
headers?: T
|
|
14
|
+
headers?: T;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Specifies that the response is a raw response.
|
|
18
|
+
*
|
|
19
|
+
* `response.end()` is called in the handler function.
|
|
20
|
+
*/
|
|
21
|
+
export declare const rawEnd: () => {
|
|
22
|
+
isRawResponseEnd: true;
|
|
15
23
|
};
|
package/dist/lib/api/send.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.noContent = exports.created = exports.ok = void 0;
|
|
3
|
+
exports.rawEnd = exports.noContent = exports.created = exports.ok = void 0;
|
|
4
4
|
const ok = (body, headers) => ({
|
|
5
5
|
status: 200,
|
|
6
6
|
body: body,
|
|
@@ -19,3 +19,12 @@ const noContent = (headers) => ({
|
|
|
19
19
|
headers: headers,
|
|
20
20
|
});
|
|
21
21
|
exports.noContent = noContent;
|
|
22
|
+
/**
|
|
23
|
+
* Specifies that the response is a raw response.
|
|
24
|
+
*
|
|
25
|
+
* `response.end()` is called in the handler function.
|
|
26
|
+
*/
|
|
27
|
+
const rawEnd = () => ({
|
|
28
|
+
isRawResponseEnd: true,
|
|
29
|
+
});
|
|
30
|
+
exports.rawEnd = rawEnd;
|
|
@@ -1,11 +1,15 @@
|
|
|
1
|
+
import { Response } from 'express';
|
|
1
2
|
import { Middleware, EndpointResponseSchema } from './middleware';
|
|
2
3
|
import { ArrayToUnion, ExtractZodOutput, ExtractZodOutputFromMiddleware, Always, Merge, ZodSchemaOuput } from './utils';
|
|
4
|
+
export type RawResponseEnd = {
|
|
5
|
+
isRawResponseEnd: boolean;
|
|
6
|
+
};
|
|
3
7
|
export type EndpointHandler<TResponse, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TMiddlewares> = (request: {
|
|
4
8
|
body: ZodSchemaOuput<TRequestBodySchema>;
|
|
5
9
|
params: ZodSchemaOuput<TRequestParamsSchema>;
|
|
6
10
|
query: ZodSchemaOuput<TRequestQuerySchema>;
|
|
7
11
|
headers: ZodSchemaOuput<TRequestHeadersSchema>;
|
|
8
|
-
}, context: Merge<Always<ExtractZodOutputFromMiddleware<ArrayToUnion<TMiddlewares>>>>, send: (response: ExtractZodOutput<ArrayToUnion<TResponse>>) => void) => Promise<void>;
|
|
12
|
+
}, context: Merge<Always<ExtractZodOutputFromMiddleware<ArrayToUnion<TMiddlewares>>>>, send: (response: ExtractZodOutput<ArrayToUnion<TResponse>> | RawResponseEnd) => void, rawResponse: Response) => Promise<void>;
|
|
9
13
|
export type EndpointOptions<TResponseSchemas extends ReadonlyArray<EndpointResponseSchema>, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TMiddlewares extends ReadonlyArray<Middleware<any, any, any, any, any, any>> | undefined> = {
|
|
10
14
|
schemas?: {
|
|
11
15
|
body?: TRequestBodySchema;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
import { z } from 'zod';
|
|
3
2
|
import { ArrayToUnion, ExtractZodOutput, ZodSchemaOuput } from './utils';
|
|
4
3
|
export type EndpointResponseSchema = {
|
|
@@ -6,6 +5,7 @@ export type EndpointResponseSchema = {
|
|
|
6
5
|
description: string;
|
|
7
6
|
schema: z.ZodSchema<any> | z.ZodVoid;
|
|
8
7
|
headers?: Record<string, string>;
|
|
8
|
+
isRawResponseEnd?: boolean;
|
|
9
9
|
};
|
|
10
10
|
export type FileUpload = {
|
|
11
11
|
buffer: Buffer;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createEmailDataType =
|
|
3
|
+
exports.createEmailDataType = createEmailDataType;
|
|
4
4
|
const sequelize_1 = require("sequelize");
|
|
5
5
|
function createEmailDataType() {
|
|
6
6
|
class Email extends sequelize_1.DataTypes.CITEXT {
|
|
@@ -14,4 +14,3 @@ function createEmailDataType() {
|
|
|
14
14
|
Email.prototype.key = Email.key = 'Email';
|
|
15
15
|
sequelize_1.DataTypes.Email = sequelize_1.Utils.classToInvokable(Email);
|
|
16
16
|
}
|
|
17
|
-
exports.createEmailDataType = createEmailDataType;
|
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
/// <reference types="node/http" />
|
|
2
|
-
/// <reference types="lib/logger/types" />
|
|
3
|
-
/// <reference types="pino-http" />
|
|
4
1
|
declare const getRequestTracerId: () => string;
|
|
5
2
|
declare const getRequestTracerMixin: () => {
|
|
6
3
|
reqId: {};
|
|
@@ -8,5 +5,5 @@ declare const getRequestTracerMixin: () => {
|
|
|
8
5
|
reqId?: undefined;
|
|
9
6
|
};
|
|
10
7
|
declare const getRequestIdHeader: () => object;
|
|
11
|
-
declare const requestTracerMiddleware: () => (req: import("http").IncomingMessage, res: import("http").ServerResponse
|
|
8
|
+
declare const requestTracerMiddleware: () => (req: import("http").IncomingMessage, res: import("http").ServerResponse, next: (err?: any) => void) => void;
|
|
12
9
|
export { getRequestTracerId, getRequestTracerMixin, getRequestIdHeader, requestTracerMiddleware, };
|