Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[add] Strapi 4 wrapper #5

Merged
merged 3 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
wrapper/
test/
.env
.parcel-cache/
Expand Down
115 changes: 45 additions & 70 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,20 @@ export const client = new HTTPClient({

```typescript
import { buildURLData } from 'web-utility';
import { ListModel } from 'mobx-restful';
import { NewData, ListModel } from 'mobx-restful';

import { client } from './client';

export type Repository = Record<'full_name' | 'html_url', string>;

export class RepositoryModel extends ListModel<Repository> {
export class RepositoryModel<
D extends Repository = Repository,
F extends NewData<D> = NewData<D>
> extends ListModel<D, F> {
client = client;
baseURI = 'orgs/idea2app/repos';

protected async loadPage(page: number, per_page: number) {
async loadPage(page: number, per_page: number) {
const { body } = await this.client.get<Repository[]>(
`${this.baseURI}?${buildURLData({ page, per_page })}`
);
Expand Down Expand Up @@ -92,85 +95,57 @@ export class RepositoryPage extends WebCell() {

### Preload List

#### `model/Repository.ts`
#### `model/PreloadRepository.ts`

```diff
```typescript
import { buildURLData } from 'web-utility';
-import { ListModel } from 'mobx-restful';
+import { BufferListModel } from 'mobx-restful';
import { Buffer } from 'mobx-restful';

import { client } from './client';
import { RepositoryModel } from './Repository';

export type Repository = Record<'full_name' | 'html_url', string>;

-export class RepositoryModel extends ListModel<Repository> {
+export class RepositoryModel extends BufferListModel<Repository>() {
client = client;
baseURI = 'orgs/idea2app/repos';

protected async loadPage(page: number, per_page: number) {
const { body } = await this.client.get<Repository[]>(
`${this.baseURI}?${buildURLData({ page, per_page })}`
);
return { pageData: body };
}
}
export class PreloadRepositoryModel extends Buffer(RepositoryModel) {}

export default new RepositoryModel();
export default new PreloadRepositoryModel();
```

### Multiple Source List

#### `model/Repository.ts`

```diff
-import { buildURLData } from 'web-utility';
+import { buildURLData, mergeStream } from 'web-utility';
-import { ListModel } from 'mobx-restful';
+import { StreamListModel } from 'mobx-restful';
#### `model/MultipleRepository.ts`

import { client } from './client';

export type Repository = Record<'full_name' | 'html_url', string>;

-export class RepositoryModel extends ListModel<Repository> {
+export class RepositoryModel extends StreamListModel<Repository>() {
client = client;
- baseURI = 'orgs/idea2app/repos';

- protected async loadPage(page: number, per_page: number) {
- const { body } = await this.client.get<Repository[]>(
- `${this.baseURI}?${buildURLData({ page, per_page })}`
- );
- return { pageData: body };
- }
+ protected openStream() {
+ return mergeStream(
+ async function* () {
+ for (let i = 1; ; i++) {
+ const { body } = await client.get<Repository[]>(
+ 'orgs/idea2app/repos?page=' + i
+ );
+ if (!body[0]) break;
+
+ yield* body;
+ }
+ },
+ async function* () {
+ for (let i = 1; ; i++) {
+ const { body } = await client.get<Repository[]>(
+ 'users/TechQuery/repos?page=' + i
+ );
+ if (!body[0]) break;
+
+ yield* body;
+ }
+ }
+ );
+ }
```typescript
import { buildURLData, mergeStream } from 'web-utility';
import { Stream } from 'mobx-restful';

import { Repository, RepositoryModel } from './Repository';

export class MultipleRepository extends Stream(RepositoryModel) {
openStream() {
return mergeStream(
async function* () {
for (let i = 1; ; i++) {
const { body } = await client.get<Repository[]>(
'orgs/idea2app/repos?page=' + i
);
if (!body[0]) break;

yield* body;
}
},
async function* () {
for (let i = 1; ; i++) {
const { body } = await client.get<Repository[]>(
'users/TechQuery/repos?page=' + i
);
if (!body[0]) break;

yield* body;
}
}
);
}
}

export default new RepositoryModel();
export default new MultipleRepository();
```

## Scaffold
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mobx-restful",
"version": "0.6.0-rc.0",
"version": "0.6.0-rc.3",
"license": "LGPL-3.0",
"author": "shiy2008@gmail.com",
"description": "MobX SDK for RESTful API",
Expand All @@ -27,7 +27,7 @@
"module": "dist/index.esm.js",
"main": "dist/index.js",
"dependencies": {
"@swc/helpers": "^0.4.6",
"@swc/helpers": "^0.4.7",
"class-validator": "^0.13.2",
"koajax": "^0.8.1",
"reflect-metadata": "^0.1.13",
Expand Down Expand Up @@ -79,7 +79,7 @@
"scripts": {
"prepare": "husky install",
"test": "lint-staged && jest",
"build": "rm -rf dist/ docs/ && typedoc source/ && parcel build",
"build": "rm -rf dist/ docs/ && parcel build",
"start": "typedoc source/ && open-cli docs/index.html",
"prepublishOnly": "npm test && npm run build"
}
Expand Down
14 changes: 7 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions source/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export abstract class BaseListModel<D extends DataObject> extends BaseModel {
return store;
}

@action
clear() {
this.currentOne = {} as D;
this.validity = {};
Expand Down
72 changes: 21 additions & 51 deletions source/List.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Constructor, splitArray } from 'web-utility';
import { splitArray } from 'web-utility';
import { observable, computed, action, reaction } from 'mobx';

import { DataObject, NewData, toggle } from './utility';
import { AbstractClass, DataObject, NewData, toggle } from './utility';
import { BaseListModel } from './Base';

export interface PageData<D extends DataObject> {
Expand Down Expand Up @@ -73,17 +73,13 @@ export abstract class ListModel<
return this;
}

protected abstract loadPage(
abstract loadPage(
pageIndex: number,
pageSize: number,
filter: F
): Promise<PageData<D>>;

protected async loadNewPage(
pageIndex: number,
pageSize: number,
filter: F
) {
async loadNewPage(pageIndex: number, pageSize: number, filter: F) {
const { pageData, totalCount } = await this.loadPage(
pageIndex,
pageSize,
Expand All @@ -101,7 +97,7 @@ export abstract class ListModel<
return { pageData, totalCount };
}

protected flushPage(pageIndex: number, pageSize: number, pageData: D[]) {
flushPage(pageIndex: number, pageSize: number, pageData: D[]) {
this.turnTo(pageIndex, pageSize);

this.noMore = pageData.length < pageSize;
Expand All @@ -127,27 +123,18 @@ export abstract class ListModel<
}
}

/**
* Only for Type Hint of {@link BufferListModel} mixin
*
* @deprecated
*/
export abstract class BufferList<
export function Buffer<
D extends DataObject,
F extends NewData<D> = NewData<D>
> extends ListModel<D, F> {
protected pendingList: ReturnType<ListModel<D, F>['loadPage']>[] = [];
}
F extends NewData<D> = NewData<D>,
M extends AbstractClass<ListModel<D, F>> = AbstractClass<ListModel<D, F>>
>(Super: M) {
abstract class BufferListMixin extends Super {
pendingList: ReturnType<ListModel<D, F>['loadPage']>[] = [];

export function BufferListModel<
D extends DataObject,
F extends NewData<D> = NewData<D>
>(): abstract new () => BufferList<D, F> {
abstract class BufferListMixin extends BufferList<D, F> {
clear() {
this.pendingList = [];

return this;
return super.clear();
}

@toggle('downloading')
Expand Down Expand Up @@ -187,41 +174,24 @@ export function BufferListModel<
return BufferListMixin;
}

/**
* Only for Type Hint of {@link StreamListModel} mixin
*
* @deprecated
*/
export abstract class StreamList<
export function Stream<
D extends DataObject,
F extends NewData<D> = NewData<D>
> extends ListModel<D, F> {
baseURI = '';
F extends NewData<D> = NewData<D>,
M extends AbstractClass<ListModel<D, F>> = AbstractClass<ListModel<D, F>>
>(Super: M) {
abstract class StreamListMixin extends Super {
baseURI = '';

protected stream?: AsyncGenerator<D, void, D>;
protected abstract openStream(filter: F): AsyncGenerator<D, void, D>;
stream?: AsyncGenerator<D, void, D>;
abstract openStream(filter: F): AsyncGenerator<D, void, D>;

protected async loadPage(pageIndex: number, pageSize: number, filter: F) {
return { pageData: [] };
}
}

export function StreamListModel<
D extends DataObject,
F extends NewData<D> = NewData<D>
>(): abstract new () => StreamList<D, F> {
abstract class StreamListMixin extends StreamList<D, F> {
clear() {
this.stream = undefined;

return super.clear();
}

protected async loadPage(
pageIndex: number,
pageSize: number,
filter: F
) {
async loadPage(pageIndex: number, pageSize: number, filter: F) {
const { allItems } = this,
newList: D[] = [];
const newCount = pageIndex * pageSize - allItems.length;
Expand Down
2 changes: 2 additions & 0 deletions source/utility/type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { TypeKeys } from 'web-utility';
import { HTTPClient, Context } from 'koajax';

export type AbstractClass<I> = abstract new (...data: any[]) => I;

export type IDType = number | string;

export type DataObject = Record<string, any>;
Expand Down
Loading