Skip to content

Commit

Permalink
Added QS options
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalanamini committed Jan 7, 2019
1 parent f8013d5 commit 341141c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -58,6 +58,7 @@ namespace restify {
export interface Options<P extends boolean = false> {
name: string;
prefix: string;
qs: IParseOptions;
defaults: Query;
routes: P extends true ? Partial<RoutesOptions> : RoutesOptions;
}
Expand All @@ -72,6 +73,10 @@ const restify = (model: typeof Odin, options: Partial<restify.Options<true>> = {
name: model.toString(),
prefix: "",
...options,
qs: {
depth: 100,
...(options.qs || {}),
},
defaults: {
limit: 10,
skip: 0,
Expand All @@ -92,7 +97,7 @@ const restify = (model: typeof Odin, options: Partial<restify.Options<true>> = {

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));

Expand Down
40 changes: 40 additions & 0 deletions test/index/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
});
});

0 comments on commit 341141c

Please sign in to comment.