@lucaapp/service-utils 1.56.0 → 1.56.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/lib/api/endpoint.js
CHANGED
|
@@ -8,6 +8,7 @@ const zod_1 = require("zod");
|
|
|
8
8
|
const assert_1 = __importDefault(require("assert"));
|
|
9
9
|
const http_1 = require("./types/http");
|
|
10
10
|
const libphonenumber_js_1 = require("libphonenumber-js");
|
|
11
|
+
const multer_1 = __importDefault(require("multer"));
|
|
11
12
|
const sendBadContextError = (response, error, debug) => {
|
|
12
13
|
response.err = error;
|
|
13
14
|
return response.status(500).send({
|
|
@@ -117,15 +118,15 @@ const isTypedError = (error) => {
|
|
|
117
118
|
return false;
|
|
118
119
|
return true;
|
|
119
120
|
};
|
|
120
|
-
const validateAndSendResponse = async (expressResponse, response, validResponses, debug
|
|
121
|
+
const validateAndSendResponse = async (expressResponse, response, validResponses, debug) => {
|
|
121
122
|
try {
|
|
122
123
|
(0, assert_1.default)(typeof response.status === 'number');
|
|
123
124
|
// get schema respective to status code
|
|
124
125
|
const responseSchema = validResponses.find(responseSchema => responseSchema.status === response.status);
|
|
125
126
|
const schema = responseSchema?.schema;
|
|
126
127
|
(0, assert_1.default)(schema);
|
|
127
|
-
if (
|
|
128
|
-
Object.entries(
|
|
128
|
+
if (response.headers) {
|
|
129
|
+
Object.entries(response.headers).forEach(([header, value]) => {
|
|
129
130
|
expressResponse.set(header, value);
|
|
130
131
|
});
|
|
131
132
|
}
|
|
@@ -146,6 +147,24 @@ const validateAndSendResponse = async (expressResponse, response, validResponses
|
|
|
146
147
|
sendErrorResponse(expressResponse, error, undefined, debug);
|
|
147
148
|
}
|
|
148
149
|
};
|
|
150
|
+
const upload = (0, multer_1.default)({
|
|
151
|
+
storage: multer_1.default.memoryStorage(),
|
|
152
|
+
});
|
|
153
|
+
function handleFileUpload(request, response) {
|
|
154
|
+
return new Promise((resolve, reject) => {
|
|
155
|
+
upload.single('file')(request, response, err => {
|
|
156
|
+
if (err) {
|
|
157
|
+
reject(err);
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
if (!request.file) {
|
|
161
|
+
return resolve();
|
|
162
|
+
}
|
|
163
|
+
resolve(request.file);
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
}
|
|
149
168
|
const handleMiddleware = async (middleware, context, request, response, debug) =>
|
|
150
169
|
// resolves true when next middleware should continue
|
|
151
170
|
// resolves false when a response was sent and next middleware should not be called
|
|
@@ -174,8 +193,14 @@ new Promise(async (resolve) => {
|
|
|
174
193
|
throw error;
|
|
175
194
|
}
|
|
176
195
|
const { ip, baseUrl, originalUrl, route, method } = request;
|
|
196
|
+
let file = undefined;
|
|
197
|
+
if (request.headers['content-type']?.includes('multipart/form-data')) {
|
|
198
|
+
file = await handleFileUpload(request, response);
|
|
199
|
+
}
|
|
200
|
+
// if the header is multipart we should use multer and return a buffer with the content
|
|
177
201
|
const handlerRequest = {
|
|
178
202
|
body: handlerRequestBody,
|
|
203
|
+
...(file && { file }),
|
|
179
204
|
params: handlerRequestParams,
|
|
180
205
|
query: handlerRequestQuery,
|
|
181
206
|
headers: handlerRequestHeaders,
|
|
@@ -249,7 +274,7 @@ const mountEndpoint = (router, method, path, options, handler, debug) => {
|
|
|
249
274
|
}
|
|
250
275
|
}
|
|
251
276
|
await handler(handlerRequest, ctx, async (controllerResponse) => {
|
|
252
|
-
await validateAndSendResponse(response, controllerResponse, options.responses, debug
|
|
277
|
+
await validateAndSendResponse(response, controllerResponse, options.responses, debug);
|
|
253
278
|
});
|
|
254
279
|
}
|
|
255
280
|
catch (error) {
|
package/dist/lib/api/send.d.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
export declare const ok: <T>(body?: T | undefined) => {
|
|
1
|
+
export declare const ok: <T, U>(body?: T | undefined, headers?: U | undefined) => {
|
|
2
2
|
status: 200;
|
|
3
3
|
body: T;
|
|
4
|
+
headers: U;
|
|
4
5
|
};
|
|
5
|
-
export declare const created: <T>(body?: T | undefined) => {
|
|
6
|
+
export declare const created: <T, U>(body?: T | undefined, headers?: U | undefined) => {
|
|
6
7
|
status: 201;
|
|
7
8
|
body: T;
|
|
9
|
+
headers: U;
|
|
8
10
|
};
|
|
9
|
-
export declare const noContent: () => {
|
|
11
|
+
export declare const noContent: <T>(headers?: T | undefined) => {
|
|
10
12
|
status: 204;
|
|
11
13
|
body: void;
|
|
14
|
+
headers?: T | undefined;
|
|
12
15
|
};
|
package/dist/lib/api/send.js
CHANGED
|
@@ -1,18 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.noContent = exports.created = exports.ok = void 0;
|
|
4
|
-
const ok = (body) => ({
|
|
4
|
+
const ok = (body, headers) => ({
|
|
5
5
|
status: 200,
|
|
6
6
|
body: body,
|
|
7
|
+
headers: headers,
|
|
7
8
|
});
|
|
8
9
|
exports.ok = ok;
|
|
9
|
-
const created = (body) => ({
|
|
10
|
+
const created = (body, headers) => ({
|
|
10
11
|
status: 201,
|
|
11
12
|
body: body,
|
|
13
|
+
headers: headers,
|
|
12
14
|
});
|
|
13
15
|
exports.created = created;
|
|
14
|
-
const noContent = () => ({
|
|
16
|
+
const noContent = (headers) => ({
|
|
15
17
|
status: 204,
|
|
16
18
|
body: undefined,
|
|
19
|
+
headers: headers,
|
|
17
20
|
});
|
|
18
21
|
exports.noContent = noContent;
|
|
@@ -4,6 +4,7 @@ export type EndpointResponseSchema = {
|
|
|
4
4
|
status: number;
|
|
5
5
|
description: string;
|
|
6
6
|
schema: z.ZodSchema<any> | z.ZodVoid;
|
|
7
|
+
headers?: Record<string, string>;
|
|
7
8
|
};
|
|
8
9
|
export type MiddlewareHandler<TResponseSchema, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TContextSchema> = (request: {
|
|
9
10
|
body: ZodSchemaOuput<TRequestBodySchema>;
|
|
@@ -15,6 +16,7 @@ export type MiddlewareHandler<TResponseSchema, TRequestBodySchema, TRequestParam
|
|
|
15
16
|
originalUrl: string;
|
|
16
17
|
path: string;
|
|
17
18
|
method: string;
|
|
19
|
+
file?: Express.Multer.File;
|
|
18
20
|
}, send: (response: ExtractZodOutput<ArrayToUnion<TResponseSchema>>) => void, next: (context?: ZodSchemaOuput<TContextSchema>) => void) => Promise<void>;
|
|
19
21
|
export type MiddlewareOptions<TResponseSchemas extends ReadonlyArray<EndpointResponseSchema>, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TContextSchema> = {
|
|
20
22
|
schemas?: {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lucaapp/service-utils",
|
|
3
|
-
"version": "1.56.
|
|
3
|
+
"version": "1.56.2",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"@tsconfig/node18": "1.0.1",
|
|
24
24
|
"@types/express": "4.17.15",
|
|
25
25
|
"@types/express-serve-static-core": "^4.17.43",
|
|
26
|
+
"@types/multer": "^1.4.11",
|
|
26
27
|
"@types/node": "18.18.2",
|
|
27
28
|
"@types/response-time": "^2.3.5",
|
|
28
29
|
"@types/swagger-ui-express": "4.1.3",
|
|
@@ -35,6 +36,7 @@
|
|
|
35
36
|
"libphonenumber-js": "1.9.44",
|
|
36
37
|
"lodash": "^4.17.21",
|
|
37
38
|
"moment-timezone": "^0.5.44",
|
|
39
|
+
"multer": "^1.4.5-lts.1",
|
|
38
40
|
"negotiator": "^0.6.3",
|
|
39
41
|
"pino-http": "9.0.0",
|
|
40
42
|
"prom-client": "14.1.0",
|