Ts conversion of utils

This commit is contained in:
Alexandre Bodin 2023-06-07 11:45:47 +02:00
parent 877ccaa485
commit 42e189cc3f
6 changed files with 53 additions and 42 deletions

View File

@ -1,4 +1,4 @@
node_modules/ node_modules/
.eslintrc.js .eslintrc.js
index.d.ts
jest.config.js jest.config.js
dist/

View File

@ -93,4 +93,3 @@ build
node_modules node_modules
.node_history .node_history
package-lock.json package-lock.json

View File

@ -1,5 +1,3 @@
'use strict';
module.exports = { module.exports = {
foo: 'bar', foo: 'bar',
cb() { cb() {

View File

@ -1,5 +1,3 @@
'use strict';
module.exports = { module.exports = {
__esModule: true, __esModule: true,
default: { default: {

View File

@ -38,28 +38,30 @@ export interface SortMap {
[key: string]: SortOrder | SortMap; [key: string]: SortOrder | SortMap;
} }
type SortQuery = string | string[] | object; type SortParams = string | string[] | object;
type FieldsQuery = string | string[]; type FieldsParams = string | string[];
export interface FiltersQuery {} type FiltersParams = unknown;
export interface PopulateParams { export interface PopulateAttributesParams {
sort?: SortQuery; [key: string]: PopulateObjectParams;
fields?: FieldsQuery; }
filters?: FiltersQuery; export interface PopulateObjectParams {
populate?: PopulateQuery; sort?: SortParams;
on: { fields?: FieldsParams;
[key: string]: PopulateParams; filters?: FiltersParams;
}; populate?: PopulateParams;
publicationState?: 'live' | 'preview';
on: PopulateAttributesParams;
} }
type PopulateQuery = boolean | string | string[] | PopulateParams; type PopulateParams = string | string[] | PopulateAttributesParams;
export interface Query { export interface Params {
sort?: SortQuery; sort?: SortParams;
fields?: FieldsQuery; fields?: FieldsParams;
filters?: FiltersQuery; filters?: FiltersParams;
populate?: PopulateQuery; populate?: PopulateParams;
count: boolean; count: boolean;
ordering: unknown; ordering: unknown;
_q?: string; _q?: string;
@ -70,12 +72,26 @@ export interface Query {
publicationState?: 'live' | 'preview'; publicationState?: 'live' | 'preview';
} }
export interface ConvertedQuery { type FiltersQuery = (options: { meta: Model }) => WhereQuery | undefined;
orderBy?: SortQuery; type OrderByQuery = SortMap | SortMap[];
select?: FieldsQuery; type SelectQuery = string | string[];
where?: FiltersQuery; export interface WhereQuery {
[key: string]: any;
}
type PopulateQuery =
| boolean
| string[]
| {
[key: string]: PopulateQuery;
};
export interface Query {
orderBy?: OrderByQuery;
select?: SelectQuery;
where?: WhereQuery;
// NOTE: those are internal DB filters do not modify // NOTE: those are internal DB filters do not modify
filters?: any; filters?: FiltersQuery;
populate?: PopulateQuery; populate?: PopulateQuery;
count?: boolean; count?: boolean;
ordering?: unknown; ordering?: unknown;
@ -121,7 +137,7 @@ const isStringArray = (value: unknown): value is string[] =>
/** /**
* Sort query parser * Sort query parser
*/ */
const convertSortQueryParams = (sortQuery: SortQuery): SortMap | SortMap[] => { const convertSortQueryParams = (sortQuery: SortParams): OrderByQuery => {
if (typeof sortQuery === 'string') { if (typeof sortQuery === 'string') {
return convertStringSortQueryParam(sortQuery); return convertStringSortQueryParam(sortQuery);
} }
@ -252,7 +268,7 @@ class InvalidPopulateError extends Error {
// NOTE: we could support foo.* or foo.bar.* etc later on // NOTE: we could support foo.* or foo.bar.* etc later on
const convertPopulateQueryParams = ( const convertPopulateQueryParams = (
populate: PopulateQuery, populate: PopulateParams,
schema: Model, schema: Model,
depth = 0 depth = 0
): PopulateQuery => { ): PopulateQuery => {
@ -284,7 +300,7 @@ const convertPopulateQueryParams = (
throw new InvalidPopulateError(); throw new InvalidPopulateError();
}; };
const convertPopulateObject = (populate: PopulateParams, schema: Model) => { const convertPopulateObject = (populate: PopulateAttributesParams, schema: Model) => {
if (!schema) { if (!schema) {
return {}; return {};
} }
@ -375,7 +391,7 @@ const convertPopulateObject = (populate: PopulateParams, schema: Model) => {
}, {}); }, {});
}; };
const convertNestedPopulate = (subPopulate: PopulateQuery, schema: Model) => { const convertNestedPopulate = (subPopulate: PopulateObjectParams, schema: Model) => {
if (_.isString(subPopulate)) { if (_.isString(subPopulate)) {
return parseType({ type: 'boolean', value: subPopulate, forceCast: true }); return parseType({ type: 'boolean', value: subPopulate, forceCast: true });
} }
@ -384,14 +400,14 @@ const convertNestedPopulate = (subPopulate: PopulateQuery, schema: Model) => {
return subPopulate; return subPopulate;
} }
if (!_.isPlainObject(subPopulate)) { if (!isPlainObject(subPopulate)) {
throw new Error(`Invalid nested populate. Expected '*' or an object`); throw new Error(`Invalid nested populate. Expected '*' or an object`);
} }
const { sort, filters, fields, populate, count, ordering, page, pageSize, start, limit } = const { sort, filters, fields, populate, count, ordering, page, pageSize, start, limit } =
subPopulate; subPopulate;
const query: ConvertedQuery = {}; const query: Query = {};
if (sort) { if (sort) {
query.orderBy = convertSortQueryParams(sort); query.orderBy = convertSortQueryParams(sort);
@ -440,7 +456,7 @@ const convertNestedPopulate = (subPopulate: PopulateQuery, schema: Model) => {
return query; return query;
}; };
const convertFieldsQueryParams = (fields: FieldsQuery, depth = 0): string[] | undefined => { const convertFieldsQueryParams = (fields: FieldsParams, depth = 0): SelectQuery | undefined => {
if (depth === 0 && fields === '*') { if (depth === 0 && fields === '*') {
return undefined; return undefined;
} }
@ -462,7 +478,7 @@ const convertFieldsQueryParams = (fields: FieldsQuery, depth = 0): string[] | un
throw new Error('Invalid fields parameter. Expected a string or an array of strings'); throw new Error('Invalid fields parameter. Expected a string or an array of strings');
}; };
const convertFiltersQueryParams = (filters: FiltersQuery, schema: Model) => { const convertFiltersQueryParams = (filters: FiltersParams, schema: Model): WhereQuery => {
// Filters need to be either an array or an object // Filters need to be either an array or an object
// Here we're only checking for 'object' type since typeof [] => object and typeof {} => object // Here we're only checking for 'object' type since typeof [] => object and typeof {} => object
if (!isObject(filters)) { if (!isObject(filters)) {
@ -475,9 +491,9 @@ const convertFiltersQueryParams = (filters: FiltersQuery, schema: Model) => {
return convertAndSanitizeFilters(filtersCopy, schema); return convertAndSanitizeFilters(filtersCopy, schema);
}; };
const convertAndSanitizeFilters = (filters: FiltersQuery, schema: Model) => { const convertAndSanitizeFilters = (filters: FiltersParams, schema: Model): WhereQuery => {
if (!isPlainObject(filters)) { if (!isPlainObject(filters)) {
return filters; return filters as WhereQuery;
} }
if (Array.isArray(filters)) { if (Array.isArray(filters)) {
@ -549,7 +565,7 @@ const convertAndSanitizeFilters = (filters: FiltersQuery, schema: Model) => {
const convertPublicationStateParams = ( const convertPublicationStateParams = (
schema: Model, schema: Model,
params: { publicationState?: 'live' | 'preview' } = {}, params: { publicationState?: 'live' | 'preview' } = {},
query: ConvertedQuery = {} query: Query = {}
) => { ) => {
if (!schema) { if (!schema) {
return; return;
@ -573,11 +589,11 @@ const convertPublicationStateParams = (
} }
}; };
const transformParamsToQuery = (uid: string, params: Query) => { const transformParamsToQuery = (uid: string, params: Params): Query => {
// NOTE: can be a CT, a Compo or nothing in the case of polymorphism (DZ & morph relations) // NOTE: can be a CT, a Compo or nothing in the case of polymorphism (DZ & morph relations)
const schema = strapi.getModel(uid); const schema = strapi.getModel(uid);
const query: ConvertedQuery = {}; const query: Query = {};
const { _q, sort, filters, fields, populate, page, pageSize, start, limit } = params; const { _q, sort, filters, fields, populate, page, pageSize, start, limit } = params;

View File

@ -3,4 +3,4 @@
export default function importDefault(modName: string) { export default function importDefault(modName: string) {
const mod = require(modName); const mod = require(modName);
return mod && mod.__esModule ? mod.default : mod; return mod && mod.__esModule ? mod.default : mod;
}; }