Skip to content

Commit c39baf9

Browse files
committed
[add] Filter Operator types & option of Strapi REST API
1 parent 0606636 commit c39baf9

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

wrapper/Strapi/ReadMe.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,23 @@ if (!['localhost', '127.0.0.1', '0.0.0.0'].includes(location.hostname))
5353
### `model/Article.ts`
5454

5555
```typescript
56-
import { StrapiListModel } from 'mobx-strapi';
56+
import { Base, BaseUser, StrapiListModel } from 'mobx-strapi';
5757

5858
import { session } from './Session';
5959

60-
export type Article = Record<'id' | 'title' | 'summary', string>;
60+
export interface Article extends Base, Record<'title' | 'summary', string> {
61+
author: BaseUser;
62+
}
6163

6264
export class ArticleModel extends StrapiListModel<Article> {
6365
client = session.client;
6466
baseURI = 'articles';
67+
// for Strapi 5
68+
indexKey = 'documentId';
69+
// optional
70+
operator = { title: '$containsi' } as const;
71+
// optional
72+
searchKeys = ['title', 'summary'] as const;
6573
}
6674

6775
export default new ArticleModel();
@@ -75,12 +83,13 @@ Use [WebCell][7] as an Example
7583
import { component, observer } from 'web-cell';
7684

7785
import articleStore from '../../model/Article';
86+
import { sessionStore } from '../../model/Session';
7887

7988
@component({ tagName: 'article-page' })
8089
@observer
8190
export class ArticlePage extends HTMLElement {
8291
connectedCallback() {
83-
articleStore.getList();
92+
articleStore.getList({ author: sessionStore.user?.documentId });
8493
}
8594

8695
disconnectedCallback() {

wrapper/Strapi/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mobx-strapi",
3-
"version": "0.6.6",
3+
"version": "0.6.7",
44
"license": "LGPL-3.0",
55
"author": "[email protected]",
66
"description": "MobX SDK for Strapi headless CMS",

wrapper/Strapi/source/index.ts

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TypeKeys } from 'web-utility';
1+
import { IndexKey, TypeKeys } from 'web-utility';
22
import { stringify } from 'qs';
33
import { computed, observable } from 'mobx';
44
import {
@@ -9,6 +9,7 @@ import {
99
ListModel,
1010
toggle
1111
} from 'mobx-restful';
12+
import { Base } from './Session';
1213

1314
export * 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+
4874
export 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

5884
export 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

Comments
 (0)