Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rapiq

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rapiq - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

dist/parameter/fields/constants.d.ts

2

dist/parameter/fields/index.d.ts
export * from './build';
export * from './constants';
export * from './parse';
export * from './type';
export * from './utils';

@@ -24,4 +24,6 @@ "use strict";

__exportStar(require("./build"), exports);
__exportStar(require("./constants"), exports);
__exportStar(require("./parse"), exports);
__exportStar(require("./type"), exports);
__exportStar(require("./utils"), exports);
//# sourceMappingURL=index.js.map

1

dist/parameter/fields/parse.d.ts
import { FieldsParseOptions, FieldsParseOutput } from './type';
export declare function buildDomainFields(data: Record<string, string[]> | string[], options?: FieldsParseOptions): Record<string, string[]>;
export declare function parseQueryFields(data: unknown, options?: FieldsParseOptions): FieldsParseOutput;

@@ -9,34 +9,38 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.parseQueryFields = exports.buildDomainFields = void 0;
exports.parseQueryFields = void 0;
const utils_1 = require("../../utils");
const type_1 = require("./type");
const constants_1 = require("./constants");
const utils_2 = require("./utils");
// --------------------------------------------------
// --------------------------------------------------
function buildDomainFields(data, options) {
options = options !== null && options !== void 0 ? options : { defaultAlias: type_1.DEFAULT_ALIAS_ID };
let domainFields = {};
if (Array.isArray(data)) {
domainFields[options.defaultAlias] = data;
function isRelationIncluded(key, options) {
// is not default domain && includes are defined?
if (key !== constants_1.DEFAULT_ALIAS_ID &&
key !== options.defaultAlias &&
typeof options.relations !== 'undefined') {
let index = -1;
for (let j = 0; j < options.relations.length; j++) {
if (options.relations[j].value === key) {
index = j;
break;
}
}
if (index === -1) {
return false;
}
}
else {
domainFields = data;
}
return domainFields;
return true;
}
exports.buildDomainFields = buildDomainFields;
function parseQueryFields(data, options) {
var _a, _b, _c;
var _a, _b;
options !== null && options !== void 0 ? options : (options = {});
(_a = options.defaultAlias) !== null && _a !== void 0 ? _a : (options.defaultAlias = constants_1.DEFAULT_ALIAS_ID);
const defaultDomainFields = (0, utils_2.buildFieldDomainRecords)(options.default);
const defaultDomainKeys = Object.keys(defaultDomainFields);
const allowedDomainFields = (0, utils_2.mergeFieldsDomainRecords)((0, utils_2.buildFieldDomainRecords)(options.allowed), Object.assign({}, defaultDomainFields), options);
const allowedDomainKeys = Object.keys(allowedDomainFields);
// If it is an empty array nothing is allowed
if (typeof options.allowed !== 'undefined' &&
Object.keys(options.allowed).length === 0) {
if (allowedDomainKeys.length === 0) {
return [];
}
(_a = options.aliasMapping) !== null && _a !== void 0 ? _a : (options.aliasMapping = {});
(_b = options.relations) !== null && _b !== void 0 ? _b : (options.relations = []);
(_c = options.defaultAlias) !== null && _c !== void 0 ? _c : (options.defaultAlias = type_1.DEFAULT_ALIAS_ID);
let allowedDomainFields;
if (options.allowed) {
allowedDomainFields = buildDomainFields(options.allowed, options);
}
(_b = options.aliasMapping) !== null && _b !== void 0 ? _b : (options.aliasMapping = {});
const prototype = Object.prototype.toString.call(data);

@@ -55,87 +59,109 @@ if (prototype !== '[object Object]' &&

let transformed = [];
const keys = Object.keys(data);
for (let i = 0; i < keys.length; i++) {
if (!(0, utils_1.hasOwnProperty)(data, keys[i]) ||
typeof keys[i] !== 'string') {
const domainKeys = Object.keys(data);
const processedDomainKeys = [];
for (let i = 0; i < domainKeys.length; i++) {
if (!(0, utils_1.hasOwnProperty)(data, domainKeys[i]) ||
typeof domainKeys[i] !== 'string') {
continue;
}
const fieldsArr = buildArrayFieldsRepresentation(data[keys[i]]);
if (fieldsArr.length === 0) {
let domainKey = domainKeys[i];
let output = [];
const fields = (0, utils_2.parseFieldsInput)(data[domainKey]);
domainKey = (0, utils_1.hasOwnProperty)(options.aliasMapping, domainKeys[i]) ?
options.aliasMapping[domainKeys[i]] :
domainKey;
if (!isRelationIncluded(domainKey, options)) {
continue;
}
let fields = [];
for (let j = 0; j < fieldsArr.length; j++) {
let operator;
switch (true) {
case fieldsArr[j].substring(0, 1) === type_1.FieldOperator.INCLUDE:
operator = type_1.FieldOperator.INCLUDE;
break;
case fieldsArr[j].substring(0, 1) === type_1.FieldOperator.EXCLUDE:
operator = type_1.FieldOperator.EXCLUDE;
break;
domainKey = allowedDomainKeys.length === 1 ?
allowedDomainKeys[0] :
domainKey;
if (fields.length === 0 &&
!(0, utils_1.hasOwnProperty)(defaultDomainFields, domainKey)) {
continue;
}
if (fields.length > 0) {
const fieldsInputTransformed = (0, utils_2.transformFieldsInput)(fields);
if (fieldsInputTransformed.default.length === 0 &&
(0, utils_1.hasOwnProperty)(defaultDomainFields, domainKey)) {
fieldsInputTransformed.default = defaultDomainFields[domainKey];
}
if (operator)
fieldsArr[j] = fieldsArr[j].substring(1);
fields.push(Object.assign({ key: fieldsArr[j] }, (operator ? { value: operator } : {})));
}
const allowedDomains = typeof allowedDomainFields !== 'undefined' ?
Object.keys(allowedDomainFields) :
[];
const targetKey = allowedDomains.length === 1 ?
allowedDomains[0] :
keys[i];
// is not default domain && includes are defined?
if (keys[i] !== type_1.DEFAULT_ALIAS_ID &&
keys[i] !== options.defaultAlias &&
typeof options.relations !== 'undefined') {
let index = -1;
for (let j = 0; j < options.relations.length; j++) {
if (options.relations[j].value === keys[i]) {
index = j;
break;
if (fieldsInputTransformed.default.length > 0) {
fieldsInputTransformed.default = Array.from(new Set([...fieldsInputTransformed.default, ...fieldsInputTransformed.included]));
for (let j = 0; j < fieldsInputTransformed.excluded.length; j++) {
const index = fieldsInputTransformed.default.indexOf(fieldsInputTransformed.excluded[j]);
if (index !== -1) {
fieldsInputTransformed.default.splice(index, 1);
}
}
fieldsInputTransformed.included = [];
fieldsInputTransformed.excluded = [];
}
if (index === -1) {
continue;
if (fieldsInputTransformed.default.length > 0) {
for (let j = 0; j < fieldsInputTransformed.default.length; j++) {
output.push({
key: fieldsInputTransformed.default[j],
});
}
}
if (fieldsInputTransformed.included.length > 0) {
for (let j = 0; j < fieldsInputTransformed.included.length; j++) {
output.push({
key: fieldsInputTransformed.included[j],
value: constants_1.FieldOperator.INCLUDE,
});
}
}
if (fieldsInputTransformed.excluded.length > 0) {
for (let j = 0; j < fieldsInputTransformed.excluded.length; j++) {
output.push({
key: fieldsInputTransformed.excluded[j],
value: constants_1.FieldOperator.EXCLUDE,
});
}
}
}
for (let j = 0; j < fields.length; j++) {
const fullKey = `${keys[i]}.${fields[j].key}`;
fields[j] = Object.assign(Object.assign(Object.assign({}, (targetKey && targetKey !== type_1.DEFAULT_ALIAS_ID ? { alias: targetKey } : {})), fields[j]), { key: (0, utils_1.hasOwnProperty)(options.aliasMapping, fullKey) ?
options.aliasMapping[fullKey].split('.').pop() :
fields[j].key });
else if (defaultDomainFields[domainKey].length > 0) {
for (let j = 0; j < defaultDomainFields[domainKey].length; j++) {
output.push({
key: defaultDomainFields[domainKey][j],
});
}
}
fields = fields
.filter((field) => {
if (typeof allowedDomainFields === 'undefined') {
return true;
for (let j = 0; j < output.length; j++) {
output[j].key = (0, utils_2.getFieldNamedByAliasMapping)(domainKey, output[j].key, options.aliasMapping);
if (domainKey !== options.defaultAlias) {
output[j].alias = domainKey;
}
return (0, utils_1.hasOwnProperty)(allowedDomainFields, targetKey) &&
allowedDomainFields[targetKey].indexOf(field.key) !== -1;
});
if (fields.length > 0) {
transformed = [...transformed, ...fields];
}
if (typeof options.allowed !== 'undefined' ||
typeof options.default !== 'undefined') {
output = output
.filter((field) => (0, utils_1.hasOwnProperty)(allowedDomainFields, domainKey) &&
allowedDomainFields[domainKey].indexOf(field.key) !== -1);
}
if (output.length > 0) {
processedDomainKeys.push(domainKey);
transformed = [...transformed, ...output];
}
}
if (defaultDomainKeys.length > 0) {
const missingDomainKeys = [];
for (let i = 0; i < defaultDomainKeys.length; i++) {
if (processedDomainKeys.indexOf(defaultDomainKeys[i]) === -1 &&
isRelationIncluded(defaultDomainKeys[i], options)) {
missingDomainKeys.push(defaultDomainKeys[i]);
}
}
if (missingDomainKeys.length > 0) {
for (let i = 0; i < missingDomainKeys.length; i++) {
for (let j = 0; j < defaultDomainFields[missingDomainKeys[i]].length; j++) {
transformed.push(Object.assign(Object.assign({}, (missingDomainKeys[i] !== options.defaultAlias ? { alias: missingDomainKeys[i] } : {})), { key: (0, utils_2.getFieldNamedByAliasMapping)(missingDomainKeys[i], defaultDomainFields[missingDomainKeys[i]][j], options.aliasMapping) }));
}
}
}
}
return transformed;
}
exports.parseQueryFields = parseQueryFields;
function buildArrayFieldsRepresentation(data) {
const valuePrototype = Object.prototype.toString.call(data);
if (valuePrototype !== '[object Array]' &&
valuePrototype !== '[object String]') {
return [];
}
let fieldsArr = [];
/* istanbul ignore next */
if (valuePrototype === '[object String]') {
fieldsArr = data.split(',');
}
/* istanbul ignore next */
if (valuePrototype === '[object Array]') {
fieldsArr = data
.filter((val) => typeof val === 'string');
}
return fieldsArr;
}
//# sourceMappingURL=parse.js.map
import { Parameter } from '../../constants';
import { Flatten, KeyWithOptionalPrefix, OnlyObject, ParseOptionsBase, ParseOutputElementBase, ToOneAndMany } from '../type';
export declare const DEFAULT_ALIAS_ID = "__DEFAULT__";
export declare enum FieldOperator {
INCLUDE = "+",
EXCLUDE = "-"
}
import { FieldOperator } from './constants';
declare type FieldWithOperator<T extends Record<string, any>> = KeyWithOptionalPrefix<keyof T, FieldOperator> | KeyWithOptionalPrefix<keyof T, FieldOperator>[];

@@ -14,5 +10,12 @@ export declare type FieldsBuildInput<T extends Record<string, any>> = {

} | FieldWithOperator<T>;
export declare type FieldsParseOptions = ParseOptionsBase<Parameter.FIELDS, Record<string, string[]> | string[]>;
export declare type FieldsParseOptions = ParseOptionsBase<Parameter.FIELDS, Record<string, string[]> | string[]> & {
default?: Record<string, string[]> | string[];
};
export declare type FieldsParseOutputElement = ParseOutputElementBase<Parameter.FIELDS, FieldOperator>;
export declare type FieldsParseOutput = FieldsParseOutputElement[];
export declare type FieldsInputTransformed = {
default: string[];
included: string[];
excluded: string[];
};
export {};

@@ -9,12 +9,2 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.FieldOperator = exports.DEFAULT_ALIAS_ID = void 0;
exports.DEFAULT_ALIAS_ID = '__DEFAULT__';
// -----------------------------------------------------------
// Build
// -----------------------------------------------------------
var FieldOperator;
(function (FieldOperator) {
FieldOperator["INCLUDE"] = "+";
FieldOperator["EXCLUDE"] = "-";
})(FieldOperator = exports.FieldOperator || (exports.FieldOperator = {}));
//# sourceMappingURL=type.js.map

@@ -29,4 +29,4 @@ "use strict";

// If it is an empty array nothing is allowed
if (typeof options.allowed !== 'undefined' &&
Object.keys(options.allowed).length === 0) {
if (typeof options.allowed === 'undefined' ||
options.allowed.length === 0) {
return [];

@@ -33,0 +33,0 @@ }

@@ -8,3 +8,2 @@ import { Parameter } from '../../constants';

};
export declare type FilterOperatorLabelType = `${FilterOperatorLabel}`;
declare type FilterValue<V> = V extends string | number | boolean ? (V | V[]) : never;

@@ -11,0 +10,0 @@ declare type FilterValueWithOperator<V> = V extends string | number | boolean ? (FilterValue<V> | FilterValueOperator<V> | Array<FilterValueOperator<V>>) : never;

@@ -1,6 +0,7 @@

import { FilterOperatorConfig, FilterOperatorLabelType } from './type';
import { FilterOperatorConfig } from './type';
import { FilterOperatorLabel } from './constants';
export declare function determineFilterOperatorLabelsByValue(input: string): {
operators: FilterOperatorLabelType[];
operators: (`${FilterOperatorLabel}`)[];
value: string | string[];
};
export declare function isFilterOperatorConfig(data: unknown): data is FilterOperatorConfig<any>;

@@ -49,3 +49,3 @@ "use strict";

// If it is an empty array nothing is allowed
if (Array.isArray(options.allowed) &&
if (typeof options.allowed === 'undefined' ||
options.allowed.length === 0) {

@@ -76,9 +76,7 @@ return [];

}
items = items
.map((item) => {
if ((0, utils_1.hasOwnProperty)(options.aliasMapping, item)) {
item = options.aliasMapping[item];
for (let i = 0; i < items.length; i++) {
if ((0, utils_1.hasOwnProperty)(options.aliasMapping, items[i])) {
items[i] = options.aliasMapping[items[i]];
}
return item;
});
}
if (options.allowed) {

@@ -85,0 +83,0 @@ items = items

@@ -19,8 +19,8 @@ import { Parameter } from '../constants';

});
export declare type ParseOptionsBase<K extends `${Parameter}`, A = string[]> = (K extends `${Parameter.PAGINATION}` ? Record<string, any> : {
export declare type ParseOptionsBase<K extends `${Parameter}` | Parameter, A = string[]> = (K extends `${Parameter.PAGINATION}` | Parameter.PAGINATION ? Record<string, any> : {
aliasMapping?: Record<string, string>;
allowed?: A;
defaultAlias?: string;
}) & (K extends `${Parameter.PAGINATION}` | `${Parameter.RELATIONS}` ? Record<string, any> : {
}) & (K extends `${Parameter.PAGINATION}` | Parameter.PAGINATION | `${Parameter.RELATIONS}` | Parameter.RELATIONS ? Record<string, any> : {
relations?: ParseOutputElementBase<Parameter.RELATIONS, string>[];
});

@@ -16,9 +16,6 @@ "use strict";

const output = {};
const nonEnabled = Object.keys(options).length === 0;
let relations;
if (!!options[constants_1.Parameter.RELATIONS] || nonEnabled) {
relations = (0, parameter_1.parseQueryParameter)(constants_1.Parameter.RELATIONS, (_a = input[constants_1.Parameter.RELATIONS]) !== null && _a !== void 0 ? _a : input[constants_1.URLParameter.RELATIONS], options[constants_1.Parameter.RELATIONS]);
output[constants_1.Parameter.RELATIONS] = relations;
}
const keys = [
// relations must be first parameter
constants_1.Parameter.RELATIONS,
constants_1.Parameter.FIELDS,

@@ -30,20 +27,40 @@ constants_1.Parameter.FILTERS,

for (let i = 0; i < keys.length; i++) {
const enabled = !!options[keys[i]] ||
nonEnabled;
if (!enabled)
continue;
const key = keys[i];
switch (key) {
case constants_1.Parameter.FIELDS:
output[constants_1.Parameter.FIELDS] = (0, parameter_1.parseQueryParameter)(keys[i], (_b = input[constants_1.Parameter.FIELDS]) !== null && _b !== void 0 ? _b : input[constants_1.URLParameter.FIELDS], options[constants_1.Parameter.FIELDS], relations);
case constants_1.Parameter.RELATIONS: {
const value = (_a = input[constants_1.Parameter.RELATIONS]) !== null && _a !== void 0 ? _a : input[constants_1.URLParameter.RELATIONS];
if (value || options[constants_1.Parameter.RELATIONS]) {
relations = (0, parameter_1.parseQueryParameter)(constants_1.Parameter.RELATIONS, value, options[constants_1.Parameter.RELATIONS]);
output[constants_1.Parameter.RELATIONS] = relations;
}
break;
case constants_1.Parameter.FILTERS:
output[constants_1.Parameter.FILTERS] = (0, parameter_1.parseQueryParameter)(keys[i], (_c = input[constants_1.Parameter.FILTERS]) !== null && _c !== void 0 ? _c : input[constants_1.URLParameter.FILTERS], options[constants_1.Parameter.FILTERS], relations);
}
case constants_1.Parameter.FIELDS: {
const value = (_b = input[constants_1.Parameter.FIELDS]) !== null && _b !== void 0 ? _b : input[constants_1.URLParameter.FIELDS];
if (value || options[constants_1.Parameter.FIELDS]) {
output[constants_1.Parameter.FIELDS] = (0, parameter_1.parseQueryParameter)(keys[i], value, options[constants_1.Parameter.FIELDS], relations);
}
break;
case constants_1.Parameter.PAGINATION:
output[constants_1.Parameter.PAGINATION] = (0, parameter_1.parseQueryParameter)(keys[i], (_d = input[constants_1.Parameter.PAGINATION]) !== null && _d !== void 0 ? _d : input[constants_1.URLParameter.PAGINATION], options[constants_1.Parameter.PAGINATION], relations);
}
case constants_1.Parameter.FILTERS: {
const value = (_c = input[constants_1.Parameter.FILTERS]) !== null && _c !== void 0 ? _c : input[constants_1.URLParameter.FILTERS];
if (value || options[constants_1.Parameter.FILTERS]) {
output[constants_1.Parameter.FILTERS] = (0, parameter_1.parseQueryParameter)(keys[i], value, options[constants_1.Parameter.FILTERS], relations);
}
break;
case constants_1.Parameter.SORT:
output[constants_1.Parameter.SORT] = (0, parameter_1.parseQueryParameter)(keys[i], (_e = input[constants_1.Parameter.SORT]) !== null && _e !== void 0 ? _e : input[constants_1.URLParameter.SORT], options[constants_1.Parameter.SORT], relations);
}
case constants_1.Parameter.PAGINATION: {
const value = (_d = input[constants_1.Parameter.PAGINATION]) !== null && _d !== void 0 ? _d : input[constants_1.URLParameter.PAGINATION];
if (value || options[constants_1.Parameter.PAGINATION]) {
output[constants_1.Parameter.PAGINATION] = (0, parameter_1.parseQueryParameter)(keys[i], value, options[constants_1.Parameter.PAGINATION], relations);
}
break;
}
case constants_1.Parameter.SORT: {
const value = (_e = input[constants_1.Parameter.SORT]) !== null && _e !== void 0 ? _e : input[constants_1.URLParameter.SORT];
if (value || options[constants_1.Parameter.SORT]) {
output[constants_1.Parameter.SORT] = (0, parameter_1.parseQueryParameter)(keys[i], value, options[constants_1.Parameter.SORT], relations);
}
break;
}
}

@@ -50,0 +67,0 @@ }

import { RelationsParseOutput } from '../../parameter';
import { Parameter, URLParameter } from '../../constants';
import { ParseParameterOptions, ParseParameterOutput } from './type';
export declare function parseQueryParameter<K extends `${Parameter}` | `${URLParameter}`>(key: K, data: unknown, options?: ParseParameterOptions<K> | boolean, relations?: RelationsParseOutput): ParseParameterOutput<K>;
export declare function parseQueryParameter<K extends `${Parameter}` | `${URLParameter}`>(key: K, data: unknown, options?: ParseParameterOptions<K>, relations?: RelationsParseOutput): ParseParameterOutput<K>;

@@ -7,3 +7,3 @@ import { Parameter, URLParameter } from '../constants';

export declare type ParseOptions = {
[K in `${Parameter}`]?: ParseParameterOptions<K> | boolean;
[K in `${Parameter}`]?: ParseParameterOptions<K>;
};

@@ -10,0 +10,0 @@ export declare type ParseOutput = {

{
"name": "rapiq",
"version": "0.0.1",
"version": "0.0.2",
"description": "A tiny library which provides utility types/functions for request and response query handling.",

@@ -14,2 +14,4 @@ "main": "./dist/index.js",

"test:coverage": "cross-env NODE_ENV=test jest --config ./test/jest.config.js --coverage",
"lint": "eslint --ext .js,.ts ./src",
"lint:fix": "npm run lint -- --fix",
"prepublishOnly": "npm run build",

@@ -54,3 +56,3 @@ "docs:dev": "vitepress dev docs --temp .temp",

"@types/jest": "^27.5.0",
"@types/node": "^18.0.0",
"@types/node": "^18.6.4",
"codecov": "^3.8.3",

@@ -57,0 +59,0 @@ "cross-env": "^7.0.3",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc