Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function parseSortExpression(source: JSONAPISource, sortExpression) {
if (sortExpression.field.op === 'attribute') {
const [attribute] = sortExpression.field.args;
// Note: We don't know the `type` of the attribute here, so passing `null`
const resourceAttribute = source.serializer.resourceAttribute(null, attribute);
return (sortExpression.order === 'descending' ? '-' : '') + resourceAttribute;
}
throw new QueryExpressionParseError('Query expression could not be parsed.', sortExpression.field);
}
return sortSpecifiers.map(sortSpecifier => {
if (sortSpecifier.kind === 'attribute') {
const attributeSort = sortSpecifier as AttributeSortSpecifier;
// Note: We don't know the `type` of the attribute here, so passing `null`
const resourceAttribute = source.serializer.resourceAttribute(null, attributeSort.attribute);
return (sortSpecifier.order === 'descending' ? '-' : '') + resourceAttribute;
}
throw new QueryExpressionParseError(`Sort specifier ${sortSpecifier.kind} not recognized for JSONAPISource.`, sortSpecifier);
}).join(',');
}
.map(sortSpecifier => {
if (sortSpecifier.kind === 'attribute') {
const attributeSort = sortSpecifier as AttributeSortSpecifier;
// Note: We don't know the `type` of the attribute here, so passing `null`
const resourceAttribute = this.serializer.resourceAttribute(
null,
attributeSort.attribute
);
return (
(sortSpecifier.order === 'descending' ? '-' : '') +
resourceAttribute
);
}
throw new QueryExpressionParseError(
`Sort specifier ${sortSpecifier.kind} not recognized for JSONAPISource.`,
sortSpecifier
);
})
.join(',');
)
);
case 'all':
return expected.every(e =>
actual.some(a => a.id === e.id && a.type === e.type)
);
case 'some':
return expected.some(e =>
actual.some(a => a.id === e.id && a.type === e.type)
);
case 'none':
return !expected.some(e =>
actual.some(a => a.id === e.id && a.type === e.type)
);
default:
throw new QueryExpressionParseError(
'Filter operation ${filter.op} not recognized for Store.',
filter
);
}
} else if (filter.kind === 'relatedRecord') {
let relation = deepGet(record, ['relationships', filter.relation]);
let actual = relation === undefined ? undefined : relation.data;
let expected = filter.record;
switch (filter.op) {
case 'equal':
if (Array.isArray(expected)) {
return (
actual !== undefined &&
expected.some(e => actual.type === e.type && actual.id === e.id)
);
} else {
switch (filter.op) {
case 'equal':
if (Array.isArray(expected)) {
return (
actual !== undefined &&
expected.some(e => actual.type === e.type && actual.id === e.id)
);
} else {
return (
actual !== undefined &&
actual.type === expected.type &&
actual.id === expected.id
);
}
default:
throw new QueryExpressionParseError(
'Filter operation ${filter.op} not recognized for Store.',
filter
);
}
}
return false;
}
function paginateRecords(records: Record[], paginationOptions: any) {
if (paginationOptions.limit !== undefined) {
let offset =
paginationOptions.offset === undefined ? 0 : paginationOptions.offset;
let limit = paginationOptions.limit;
return records.slice(offset, offset + limit);
} else {
throw new QueryExpressionParseError(
'Pagination options not recognized for Store. Please specify `offset` and `limit`.',
paginationOptions
);
}
}
function paginateRecords(records: Record[], paginationOptions: any) {
if (paginationOptions.limit !== undefined) {
let offset =
paginationOptions.offset === undefined ? 0 : paginationOptions.offset;
let limit = paginationOptions.limit;
return records.slice(offset, offset + limit);
} else {
throw new QueryExpressionParseError(
'Pagination options not recognized for Store. Please specify `offset` and `limit`.',
paginationOptions
);
}
}
function buildRequestFromExpression(source: JSONAPISource, expression, request = {}): any {
if (ExpressionToRequestMap[expression.op]) {
ExpressionToRequestMap[expression.op](source, expression, request);
} else {
throw new QueryExpressionParseError('Query expression could not be parsed.', expression);
}
return request;
}