@cofondateurauchomage/libs 1.1.162 → 1.1.164
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/build/api.d.ts +1 -0
- package/build/api.validate.js +41 -10
- package/package.json +1 -1
package/build/api.d.ts
CHANGED
|
@@ -39,6 +39,7 @@ export type BodyForO<O extends CloudFunctionNames | RouteNames> = {
|
|
|
39
39
|
stripeRefundedCents?: number;
|
|
40
40
|
subscriptionCancellationFeedback?: string | null;
|
|
41
41
|
subscriptionCancellationComment?: string | null;
|
|
42
|
+
checkoutOrigin?: string | null;
|
|
42
43
|
};
|
|
43
44
|
addStatsAssociation: {};
|
|
44
45
|
updateNewsletter: Omit<INewsletter, "new_profil_last_sent_date">;
|
package/build/api.validate.js
CHANGED
|
@@ -176,6 +176,7 @@ const schemasForAllRoutes = {
|
|
|
176
176
|
stripeRefundedCents: zod_1.z.number().int().optional(),
|
|
177
177
|
subscriptionCancellationFeedback: zod_1.z.union([zod_1.z.string(), zod_1.z.null()]).optional(),
|
|
178
178
|
subscriptionCancellationComment: zod_1.z.union([zod_1.z.string(), zod_1.z.null()]).optional(),
|
|
179
|
+
checkoutOrigin: zod_1.z.string().optional(),
|
|
179
180
|
}),
|
|
180
181
|
addStatsAssociation: zod_1.z.object({}),
|
|
181
182
|
updateNewsletter: zod_1.z.object({
|
|
@@ -255,6 +256,24 @@ const schemasForAllRoutes = {
|
|
|
255
256
|
}),
|
|
256
257
|
webhook: zod_1.z.object({}),
|
|
257
258
|
};
|
|
259
|
+
/**
|
|
260
|
+
* Keys that accept `undefined` on the route schema (`.optional()` or `.partial()`).
|
|
261
|
+
* Invalid values for these keys are stripped instead of failing the whole request.
|
|
262
|
+
*/
|
|
263
|
+
function getLenientOptionalKeys(schema) {
|
|
264
|
+
const keys = new Set();
|
|
265
|
+
for (const [key, fieldSchema] of Object.entries(schema.shape)) {
|
|
266
|
+
const field = fieldSchema;
|
|
267
|
+
if (field.safeParse(undefined).success) {
|
|
268
|
+
keys.add(key);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return keys;
|
|
272
|
+
}
|
|
273
|
+
const lenientKeysByRoute = Object.fromEntries(Object.keys(schemasForAllRoutes).map((route) => [
|
|
274
|
+
route,
|
|
275
|
+
getLenientOptionalKeys(schemasForAllRoutes[route]),
|
|
276
|
+
]));
|
|
258
277
|
/**
|
|
259
278
|
* Validate the request body depending on the route.
|
|
260
279
|
* @param route - Route to know which validators to use.
|
|
@@ -266,19 +285,31 @@ function validateBodyForO(route, body) {
|
|
|
266
285
|
throw new ValidationError(400, "Le payload est requis");
|
|
267
286
|
}
|
|
268
287
|
const schema = schemasForAllRoutes[route];
|
|
269
|
-
const
|
|
270
|
-
if (!parsed.success) {
|
|
271
|
-
throwFromZodError(parsed.error);
|
|
272
|
-
}
|
|
288
|
+
const lenientKeys = lenientKeysByRoute[route];
|
|
273
289
|
const input = body;
|
|
274
|
-
const
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
if (
|
|
278
|
-
|
|
290
|
+
const candidate = { ...input };
|
|
291
|
+
while (true) {
|
|
292
|
+
const parsed = schema.safeParse(candidate);
|
|
293
|
+
if (parsed.success) {
|
|
294
|
+
const data = parsed.data;
|
|
295
|
+
const result = {};
|
|
296
|
+
for (const key of Object.keys(data)) {
|
|
297
|
+
if (input[key] !== undefined) {
|
|
298
|
+
result[key] = data[key];
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return result;
|
|
279
302
|
}
|
|
303
|
+
const issue = parsed.error.issues[0];
|
|
304
|
+
const key = issue.path.find((p) => typeof p === "string");
|
|
305
|
+
if (key !== undefined &&
|
|
306
|
+
lenientKeys.has(key) &&
|
|
307
|
+
Object.prototype.hasOwnProperty.call(candidate, key)) {
|
|
308
|
+
delete candidate[key];
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
throwFromZodError(parsed.error);
|
|
280
312
|
}
|
|
281
|
-
return result;
|
|
282
313
|
}
|
|
283
314
|
/**
|
|
284
315
|
* Custom error class to handle validation errors.
|