@lucaapp/service-utils 1.56.2 → 1.56.4
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,7 +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
|
|
11
|
+
const busboy_1 = __importDefault(require("busboy"));
|
|
12
12
|
const sendBadContextError = (response, error, debug) => {
|
|
13
13
|
response.err = error;
|
|
14
14
|
return response.status(500).send({
|
|
@@ -147,22 +147,46 @@ const validateAndSendResponse = async (expressResponse, response, validResponses
|
|
|
147
147
|
sendErrorResponse(expressResponse, error, undefined, debug);
|
|
148
148
|
}
|
|
149
149
|
};
|
|
150
|
-
|
|
151
|
-
storage: multer_1.default.memoryStorage(),
|
|
152
|
-
});
|
|
153
|
-
function handleFileUpload(request, response) {
|
|
150
|
+
async function stream2buffer(stream) {
|
|
154
151
|
return new Promise((resolve, reject) => {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
152
|
+
const buf = [];
|
|
153
|
+
stream.on('data', chunk => buf.push(chunk));
|
|
154
|
+
stream.on('end', () => resolve(Buffer.concat(buf)));
|
|
155
|
+
stream.on('error', error => reject(error));
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
function handleFileUpload(request) {
|
|
159
|
+
return new Promise((resolve, reject) => {
|
|
160
|
+
const bb = (0, busboy_1.default)({
|
|
161
|
+
headers: request.headers,
|
|
162
|
+
defCharset: 'utf8',
|
|
163
|
+
limits: {
|
|
164
|
+
fileSize: 20 * 1024 * 1024, // 20 mb
|
|
165
|
+
files: 1,
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
let file = undefined;
|
|
169
|
+
bb.once('error', error => {
|
|
170
|
+
reject(error);
|
|
171
|
+
});
|
|
172
|
+
bb.on('file', async (fieldname, fileStream, filename) => {
|
|
173
|
+
const { encoding, mimeType: mimetype, filename: name } = filename;
|
|
174
|
+
const originalName = Buffer.from(name, 'latin1').toString('utf8');
|
|
175
|
+
const fileBuffer = await stream2buffer(fileStream);
|
|
176
|
+
file = {
|
|
177
|
+
buffer: fileBuffer,
|
|
178
|
+
originalname: originalName,
|
|
179
|
+
encoding,
|
|
180
|
+
mimetype,
|
|
181
|
+
size: fileBuffer.length,
|
|
182
|
+
fieldname: fieldname,
|
|
183
|
+
filename: name,
|
|
184
|
+
};
|
|
185
|
+
});
|
|
186
|
+
bb.once('finish', () => {
|
|
187
|
+
resolve(file);
|
|
165
188
|
});
|
|
189
|
+
request.pipe(bb);
|
|
166
190
|
});
|
|
167
191
|
}
|
|
168
192
|
const handleMiddleware = async (middleware, context, request, response, debug) =>
|
|
@@ -194,10 +218,11 @@ new Promise(async (resolve) => {
|
|
|
194
218
|
}
|
|
195
219
|
const { ip, baseUrl, originalUrl, route, method } = request;
|
|
196
220
|
let file = undefined;
|
|
197
|
-
if (
|
|
198
|
-
|
|
221
|
+
if (middleware.options.config?.isFileUpload &&
|
|
222
|
+
request.headers['content-type']?.includes('multipart/form-data')) {
|
|
223
|
+
file = await handleFileUpload(request);
|
|
199
224
|
}
|
|
200
|
-
// if the header is multipart we should
|
|
225
|
+
// if the header is multipart we should return a buffer with the content
|
|
201
226
|
const handlerRequest = {
|
|
202
227
|
body: handlerRequestBody,
|
|
203
228
|
...(file && { file }),
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
1
2
|
import { z } from 'zod';
|
|
2
3
|
import { ArrayToUnion, ExtractZodOutput, ZodSchemaOuput } from './utils';
|
|
3
4
|
export type EndpointResponseSchema = {
|
|
@@ -6,6 +7,15 @@ export type EndpointResponseSchema = {
|
|
|
6
7
|
schema: z.ZodSchema<any> | z.ZodVoid;
|
|
7
8
|
headers?: Record<string, string>;
|
|
8
9
|
};
|
|
10
|
+
export type FileUpload = {
|
|
11
|
+
buffer: Buffer;
|
|
12
|
+
originalname: string;
|
|
13
|
+
encoding: string;
|
|
14
|
+
mimetype: string;
|
|
15
|
+
size: number;
|
|
16
|
+
fieldname: string;
|
|
17
|
+
filename: string;
|
|
18
|
+
};
|
|
9
19
|
export type MiddlewareHandler<TResponseSchema, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TContextSchema> = (request: {
|
|
10
20
|
body: ZodSchemaOuput<TRequestBodySchema>;
|
|
11
21
|
params: ZodSchemaOuput<TRequestParamsSchema>;
|
|
@@ -16,7 +26,7 @@ export type MiddlewareHandler<TResponseSchema, TRequestBodySchema, TRequestParam
|
|
|
16
26
|
originalUrl: string;
|
|
17
27
|
path: string;
|
|
18
28
|
method: string;
|
|
19
|
-
file?:
|
|
29
|
+
file?: FileUpload;
|
|
20
30
|
}, send: (response: ExtractZodOutput<ArrayToUnion<TResponseSchema>>) => void, next: (context?: ZodSchemaOuput<TContextSchema>) => void) => Promise<void>;
|
|
21
31
|
export type MiddlewareOptions<TResponseSchemas extends ReadonlyArray<EndpointResponseSchema>, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TContextSchema> = {
|
|
22
32
|
schemas?: {
|
|
@@ -27,6 +37,9 @@ export type MiddlewareOptions<TResponseSchemas extends ReadonlyArray<EndpointRes
|
|
|
27
37
|
context?: TContextSchema;
|
|
28
38
|
};
|
|
29
39
|
responses: TResponseSchemas;
|
|
40
|
+
config?: {
|
|
41
|
+
isFileUpload?: boolean;
|
|
42
|
+
};
|
|
30
43
|
errors?: Record<string, number>;
|
|
31
44
|
};
|
|
32
45
|
export type Middleware<TResponseSchemas extends ReadonlyArray<EndpointResponseSchema>, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TContextSchema> = {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lucaapp/service-utils",
|
|
3
|
-
"version": "1.56.
|
|
3
|
+
"version": "1.56.4",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -23,11 +23,11 @@
|
|
|
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",
|
|
27
26
|
"@types/node": "18.18.2",
|
|
28
27
|
"@types/response-time": "^2.3.5",
|
|
29
28
|
"@types/swagger-ui-express": "4.1.3",
|
|
30
29
|
"axios": "^1.6.0",
|
|
30
|
+
"busboy": "^1.6.0",
|
|
31
31
|
"cls-rtracer": "^2.6.2",
|
|
32
32
|
"express": "4.19.2",
|
|
33
33
|
"express-async-errors": "3.1.1",
|
|
@@ -36,7 +36,6 @@
|
|
|
36
36
|
"libphonenumber-js": "1.9.44",
|
|
37
37
|
"lodash": "^4.17.21",
|
|
38
38
|
"moment-timezone": "^0.5.44",
|
|
39
|
-
"multer": "^1.4.5-lts.1",
|
|
40
39
|
"negotiator": "^0.6.3",
|
|
41
40
|
"pino-http": "9.0.0",
|
|
42
41
|
"prom-client": "14.1.0",
|
|
@@ -49,6 +48,7 @@
|
|
|
49
48
|
"zod": "3.22.3"
|
|
50
49
|
},
|
|
51
50
|
"devDependencies": {
|
|
51
|
+
"@types/busboy": "^1.5.3",
|
|
52
52
|
"@types/express": "4.17.15",
|
|
53
53
|
"@types/lodash": "^4.14.187",
|
|
54
54
|
"@types/negotiator": "^0.6.1",
|