From 341141c1654692940fd7d8d8d3e5ce2319ddbf92 Mon Sep 17 00:00:00 2001 From: Ardalan Amini Date: Mon, 7 Jan 2019 18:22:12 +0330 Subject: [PATCH] Added QS options --- src/index.ts | 9 +++++++-- test/index/filter.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index b065fbb..39b0bf1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,7 @@ import * as DB from "@foxify/odin/dist/DB"; import * as Foxify from "foxify"; import { Router } from "foxify/framework/routing"; import * as pluralize from "prototyped.js/es6/string/pluralize/method"; -import { parse } from "qs"; +import { IParseOptions,parse } from "qs"; import { count, delete as deleteController, index, responder, show, store, update, } from "./controllers"; @@ -58,6 +58,7 @@ namespace restify { export interface Options

{ name: string; prefix: string; + qs: IParseOptions; defaults: Query; routes: P extends true ? Partial : RoutesOptions; } @@ -72,6 +73,10 @@ const restify = (model: typeof Odin, options: Partial> = { name: model.toString(), prefix: "", ...options, + qs: { + depth: 100, + ...(options.qs || {}), + }, defaults: { limit: 10, skip: 0, @@ -92,7 +97,7 @@ const restify = (model: typeof Odin, options: Partial> = { const foxifyRestifyOdin: (single?: boolean) => Foxify.Handler = (single = false) => { return function foxify_restify_odin(req, res, next) { - const parsed = parse((req.url as string).replace(/^.*\?/, "")); + const parsed = parse((req.url as string).replace(/^.*\?/, ""), options.qs); const decoded = Object.assign({}, options.defaults, decoder(parsed)); diff --git a/test/index/filter.ts b/test/index/filter.ts index a67e3f7..200158f 100644 --- a/test/index/filter.ts +++ b/test/index/filter.ts @@ -462,3 +462,43 @@ it("Should filter [whereHas inside first level and]", async () => { meta: { limit: 10, page: 0, count: users.length, total_count: users.length }, }); }); + +it("Should filter [whereHas and]", async () => { + expect.assertions(1); + + const app = new Foxify(); + + app.use(restify(User)); + + const result = await app.inject(`/users?${stringify( + { + filter: { + and: [ + { + has: { + relation: "age", + filter: { + and: [ + { + field: "age", + operator: "gte", + value: 25, + }, + ], + }, + }, + }, + ], + }, + }, + )}`); + + const users = ITEMS + .filter(({ username }) => ITEMS2.any(item => item.username === username && item.age >= 25)); + + expect(JSON.parse(result.body)) + .toEqual({ + users, + meta: { limit: 10, page: 0, count: users.length, total_count: users.length }, + }); +});