1- import { TypeKeys } from 'web-utility' ;
1+ import { IndexKey , TypeKeys } from 'web-utility' ;
22import { stringify } from 'qs' ;
33import { computed , observable } from 'mobx' ;
44import {
99 ListModel ,
1010 toggle
1111} from 'mobx-restful' ;
12+ import { Base } from './Session' ;
1213
1314export * from './Session' ;
1415
@@ -45,6 +46,31 @@ export type StrapiNestedData<T extends DataObject> = {
4546 : T [ K ] ;
4647} ;
4748
49+ export type NotableOperator < T extends string > =
50+ | `$${T } `
51+ | `$not${Capitalize < T > } `;
52+
53+ export type CaseInsensitive < T extends `$${string } `> = T | `${T } i`;
54+
55+ export type StrapiFilterOperator =
56+ | CaseInsensitive < '$eq' >
57+ | CaseInsensitive < '$ne' >
58+ | `$${'l' | 'g' } t${'' | 'e' } `
59+ | NotableOperator < 'in' >
60+ | CaseInsensitive < NotableOperator < 'contains' > >
61+ | NotableOperator < 'null' >
62+ | '$between'
63+ | CaseInsensitive < `$${'start' | 'end' } sWith`>
64+ | '$or'
65+ | '$and'
66+ | '$not' ;
67+ export type StrapiFilterValue < T = any > = Record < StrapiFilterOperator , T > ;
68+
69+ export type StrapiFilter < Index extends IndexKey > = Record <
70+ string ,
71+ StrapiFilterValue | Record < Index , StrapiFilterValue >
72+ > ;
73+
4874export type StrapiPopulateQuery < D extends DataObject > = {
4975 [ K in TypeKeys < D , DataObject | DataObject [ ] > ] ?: {
5076 populate :
@@ -56,9 +82,14 @@ export type StrapiPopulateQuery<D extends DataObject> = {
5682} ;
5783
5884export abstract class StrapiListModel <
59- D extends DataObject ,
85+ D extends Base ,
6086 F extends Filter < D > = Filter < D >
6187> extends ListModel < D , F > {
88+ operator = {
89+ createdAt : '$startsWith' ,
90+ updatedAt : '$startsWith'
91+ } as Partial < Record < keyof D , StrapiFilterOperator > > ;
92+
6293 populate : StrapiPopulateQuery < D > = { } ;
6394
6495 searchKeys : readonly TypeKeys < D , string > [ ] = [ ] ;
@@ -118,19 +149,16 @@ export abstract class StrapiListModel<
118149 }
119150
120151 makeFilter ( pageIndex : number , pageSize : number , filter : F ) {
121- const { indexKey, populate, keywords } = this ;
152+ const { indexKey, operator , populate, keywords } = this ;
122153
123154 const filters = Object . fromEntries (
124155 Object . entries ( filter ) . map ( ( [ key , value ] ) => [
125156 key ,
126157 key in populate
127158 ? { [ indexKey ] : { $eq : value } }
128- : { $eq : value }
159+ : { [ key in operator ? operator [ key ] : ' $eq' ] : value }
129160 ] )
130- ) as Record <
131- string ,
132- { $eq : any } | { [ key in typeof indexKey ] : { $eq : any } }
133- > ;
161+ ) as StrapiFilter < typeof indexKey > ;
134162
135163 return {
136164 populate,
0 commit comments