UNPKG

@clipboard-health/json-api-nestjs

Version:

TypeScript-friendly utilities for adhering to the JSON:API specification with NestJS.

43 lines 1.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.queryFilterPreprocessor = queryFilterPreprocessor; /** * NestJS-specific query processing based on the output of `ctx.switchToHttp().getRequest().query`. * * We handle the following cases: * 1. For `?filter[age]=10,20`, `value` is `10,20`, which we transform to `{eq: "10,20"}`. * 2. For `?filter[age]=20`, `value` is `20`, which we transform to `{eq: "20"}`. * 3. For `?filter[age][gt]=10&filter[age]=20`, `value` is `{'20': true, gt: '10'}`, which we * transform to `{eq: "20", gt: "10"}`. * 4. For `?filter[age]=10&filter[age]=20`, `value` is `['10', '20']`, which we transform to `{eq: * "10,20"}`. * 5. For `?filter[age][gt]=5&filter[age]=10&filter[age]=20`, `value` is `{'0': '10', '1': '20', gt: * '5'}`, which we transform to `{eq: "10,20", gt: "5"}`. */ function queryFilterPreprocessor(value) { if (!isObject(value)) { // Cases 1 and 2. return { eq: String(value) }; } return Object.entries(value).reduce((filter, [key, value]) => { const boolValue = typeof value === "boolean"; if (/^\d+$/.test(key) || boolValue) { // Cases 3 and 5. const eq = boolValue ? key : String(value); return { ...filter, eq: filter["eq"] ? `${filter["eq"]},${eq}` : eq, }; } // Case 4 and other cases like `gt`, `lt`, etc. const stringValue = Array.isArray(value) ? value.join(",") : String(value); return { ...filter, [key]: filter[key] ? `${filter[key]},${stringValue}` : stringValue, }; }, {}); } function isObject(value) { return value !== null && value !== undefined && typeof value === "object"; } //# sourceMappingURL=queryFilterPreprocessor.js.map