@lucaapp/service-utils 1.56.4 → 1.56.6
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.js +18 -6
- package/dist/lib/api/response.d.ts +6 -0
- package/dist/lib/api/response.js +8 -1
- package/dist/lib/api/send.d.ts +8 -0
- 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 -0
- package/package.json +1 -1
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
|
}
|
|
@@ -300,7 +312,7 @@ const mountEndpoint = (router, method, path, options, handler, debug) => {
|
|
|
300
312
|
}
|
|
301
313
|
await handler(handlerRequest, ctx, async (controllerResponse) => {
|
|
302
314
|
await validateAndSendResponse(response, controllerResponse, options.responses, debug);
|
|
303
|
-
});
|
|
315
|
+
}, response);
|
|
304
316
|
}
|
|
305
317
|
catch (error) {
|
|
306
318
|
sendErrorResponse(response, error, options.errors, debug);
|
|
@@ -15,4 +15,10 @@ export declare const noContentResponse: () => {
|
|
|
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
|
@@ -13,3 +13,11 @@ export declare const noContent: <T>(headers?: T | undefined) => {
|
|
|
13
13
|
body: void;
|
|
14
14
|
headers?: T | undefined;
|
|
15
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;
|
|
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;
|