@lucaapp/service-utils 1.56.3 → 1.56.5
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
|
@@ -121,8 +121,7 @@ const isTypedError = (error) => {
|
|
|
121
121
|
const validateAndSendResponse = async (expressResponse, response, validResponses, debug) => {
|
|
122
122
|
try {
|
|
123
123
|
(0, assert_1.default)(typeof response.status === 'number');
|
|
124
|
-
|
|
125
|
-
const responseSchema = validResponses.find(responseSchema => responseSchema.status === response.status);
|
|
124
|
+
const responseSchema = validResponses.find(r => r.status === response.status);
|
|
126
125
|
const schema = responseSchema?.schema;
|
|
127
126
|
(0, assert_1.default)(schema);
|
|
128
127
|
if (response.headers) {
|
|
@@ -130,10 +129,16 @@ const validateAndSendResponse = async (expressResponse, response, validResponses
|
|
|
130
129
|
expressResponse.set(header, value);
|
|
131
130
|
});
|
|
132
131
|
}
|
|
133
|
-
|
|
134
|
-
|
|
132
|
+
const validatedResponseBody = await schema
|
|
133
|
+
.parseAsync(response.body)
|
|
134
|
+
.catch(error => {
|
|
135
|
+
if (debug || process.env.NODE_ENV !== 'production') {
|
|
136
|
+
throw error;
|
|
137
|
+
}
|
|
138
|
+
console.error('Response schema validation failed:', error);
|
|
139
|
+
return response.body; // Return original body in production
|
|
140
|
+
});
|
|
135
141
|
if (validatedResponseBody === undefined) {
|
|
136
|
-
// No Content Response
|
|
137
142
|
expressResponse.status(response.status).end();
|
|
138
143
|
return;
|
|
139
144
|
}
|
|
@@ -156,7 +161,7 @@ async function stream2buffer(stream) {
|
|
|
156
161
|
});
|
|
157
162
|
}
|
|
158
163
|
function handleFileUpload(request) {
|
|
159
|
-
return new Promise(resolve => {
|
|
164
|
+
return new Promise((resolve, reject) => {
|
|
160
165
|
const bb = (0, busboy_1.default)({
|
|
161
166
|
headers: request.headers,
|
|
162
167
|
defCharset: 'utf8',
|
|
@@ -166,8 +171,11 @@ function handleFileUpload(request) {
|
|
|
166
171
|
},
|
|
167
172
|
});
|
|
168
173
|
let file = undefined;
|
|
174
|
+
bb.once('error', error => {
|
|
175
|
+
reject(error);
|
|
176
|
+
});
|
|
169
177
|
bb.on('file', async (fieldname, fileStream, filename) => {
|
|
170
|
-
const { encoding, mimetype, filename: name } = filename;
|
|
178
|
+
const { encoding, mimeType: mimetype, filename: name } = filename;
|
|
171
179
|
const originalName = Buffer.from(name, 'latin1').toString('utf8');
|
|
172
180
|
const fileBuffer = await stream2buffer(fileStream);
|
|
173
181
|
file = {
|
|
@@ -180,7 +188,7 @@ function handleFileUpload(request) {
|
|
|
180
188
|
filename: name,
|
|
181
189
|
};
|
|
182
190
|
});
|
|
183
|
-
bb.
|
|
191
|
+
bb.once('finish', () => {
|
|
184
192
|
resolve(file);
|
|
185
193
|
});
|
|
186
194
|
request.pipe(bb);
|
|
@@ -215,7 +223,8 @@ new Promise(async (resolve) => {
|
|
|
215
223
|
}
|
|
216
224
|
const { ip, baseUrl, originalUrl, route, method } = request;
|
|
217
225
|
let file = undefined;
|
|
218
|
-
if (
|
|
226
|
+
if (middleware.options.config?.isFileUpload &&
|
|
227
|
+
request.headers['content-type']?.includes('multipart/form-data')) {
|
|
219
228
|
file = await handleFileUpload(request);
|
|
220
229
|
}
|
|
221
230
|
// if the header is multipart we should return a buffer with the content
|
|
@@ -37,6 +37,9 @@ export type MiddlewareOptions<TResponseSchemas extends ReadonlyArray<EndpointRes
|
|
|
37
37
|
context?: TContextSchema;
|
|
38
38
|
};
|
|
39
39
|
responses: TResponseSchemas;
|
|
40
|
+
config?: {
|
|
41
|
+
isFileUpload?: boolean;
|
|
42
|
+
};
|
|
40
43
|
errors?: Record<string, number>;
|
|
41
44
|
};
|
|
42
45
|
export type Middleware<TResponseSchemas extends ReadonlyArray<EndpointResponseSchema>, TRequestBodySchema, TRequestParamsSchema, TRequestQuerySchema, TRequestHeadersSchema, TContextSchema> = {
|