@derivesome/server 1.0.16 → 1.0.18
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/cjs/app.d.ts.map +1 -1
- package/dist/cjs/app.js +25 -10
- package/dist/cjs/app.js.map +1 -1
- package/dist/esm/app.d.ts.map +1 -1
- package/dist/esm/app.js +25 -10
- package/dist/esm/app.js.map +1 -1
- package/package.json +2 -2
- package/src/.app.ts.~undo-tree~ +170 -2
- package/src/app.ts +30 -11
- package/src/app.ts~ +29 -11
package/src/app.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ShapeMapping, validate } from "shapedef";
|
|
1
|
+
import { parse, ShapeMapping, validate } from "shapedef";
|
|
2
2
|
import { isPlainObject, LooseRecord, UnknownRecord } from "./common";
|
|
3
3
|
import { MetaProvider } from "./meta-provider";
|
|
4
4
|
import { RequestMethod } from "./request-method";
|
|
@@ -208,17 +208,36 @@ export class App<Meta extends UnknownRecord = UnknownRecord> {
|
|
|
208
208
|
ctx: RouteHandlerContext<Request, Response, any, any, any>,
|
|
209
209
|
route: Route,
|
|
210
210
|
) {
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
211
|
+
ctx.args = isPlainObject(ctx.req.body) ? ctx.req.body : {};
|
|
212
|
+
|
|
213
|
+
if (ctx.req.raw.url && route.mergeParams !== false) {
|
|
214
|
+
const url = new URL(ctx.req.raw.url!, "http://does-not-matter");
|
|
215
|
+
const obj = Object.fromEntries(Array.from(url.searchParams.entries()));
|
|
216
|
+
|
|
217
|
+
const validated = Object.entries(obj).map(([k, v]) => {
|
|
218
|
+
const args = route.args;
|
|
219
|
+
if (!args) return { [k]: { ok: true as const, value: v } };
|
|
220
|
+
if (!(k in args.rec)) return { [k]: { ok: true as const, value: v } };
|
|
221
|
+
return { [k]: parse(v, args.rec[k]) };
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
const parsedQ: Record<any, any> = {};
|
|
225
|
+
|
|
226
|
+
for (const it of validated) {
|
|
227
|
+
for (const [k, v] of Object.entries(it)) {
|
|
228
|
+
if (!v.ok)
|
|
229
|
+
return await this.sendReturn(ctx, ctx.R.json(v).status(400));
|
|
230
|
+
parsedQ[k] = v.value;
|
|
220
231
|
}
|
|
221
|
-
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
ctx.args = { ...ctx.args, ...parsedQ };
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (route.args) {
|
|
238
|
+
const validation = validate(ctx.args, route.args);
|
|
239
|
+
if (!validation.ok) {
|
|
240
|
+
return await this.sendReturn(ctx, ctx.R.json(validation).status(400));
|
|
222
241
|
}
|
|
223
242
|
}
|
|
224
243
|
|
package/src/app.ts~
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { parse, ShapeMapping, validate } from "shapedef";
|
|
2
2
|
import { isPlainObject, LooseRecord, UnknownRecord } from "./common";
|
|
3
3
|
import { MetaProvider } from "./meta-provider";
|
|
4
4
|
import { RequestMethod } from "./request-method";
|
|
@@ -208,17 +208,35 @@ export class App<Meta extends UnknownRecord = UnknownRecord> {
|
|
|
208
208
|
ctx: RouteHandlerContext<Request, Response, any, any, any>,
|
|
209
209
|
route: Route,
|
|
210
210
|
) {
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
211
|
+
ctx.args = (isPlainObject(ctx.req.body) ? ctx.req.body : {});
|
|
212
|
+
|
|
213
|
+
if (ctx.req.raw.url && route.mergeParams !== false) {
|
|
214
|
+
const url = new URL(ctx.req.raw.url!, 'http://does-not-matter');
|
|
215
|
+
const obj = Object.fromEntries(Array.from(url.searchParams.entries()));
|
|
216
|
+
|
|
217
|
+
const validated = Object.entries(obj).map(([k, v]) => {
|
|
218
|
+
const args = route.args;
|
|
219
|
+
if (!args) return {[k]: { ok: true as const, value: v }};
|
|
220
|
+
if (!(k in args.rec)) return {[k]: { ok: true as const, value: v }};
|
|
221
|
+
return {[k]: parse(v, args.rec[k])};
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
const parsedQ: Record<any, any> = {};
|
|
225
|
+
|
|
226
|
+
for (const it of validated) {
|
|
227
|
+
for (const [k, v] of Object.entries(it)) {
|
|
228
|
+
if (!v.ok) return await this.sendReturn(ctx, ctx.R.json(v).status(400));
|
|
229
|
+
parsedQ[k] = v.value;
|
|
220
230
|
}
|
|
221
|
-
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
ctx.args = {...ctx.args, ...(parsedQ)};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (route.args) {
|
|
237
|
+
const validation = validate(ctx.args, route.args);
|
|
238
|
+
if (!validation.ok) {
|
|
239
|
+
return await this.sendReturn(ctx, ctx.R.json(validation).status(400));
|
|
222
240
|
}
|
|
223
241
|
}
|
|
224
242
|
|