@@ -2,16 +2,18 @@ import { components } from '@octokit/openapi-types';
22import { clear } from 'idb-keyval' ;
33import { HTTPClient , HTTPError } from 'koajax' ;
44import { observable } from 'mobx' ;
5- import { BaseModel , NewData , persist , restore , toggle } from 'mobx-restful' ;
5+ import { Filter , IDType , NewData , persist , restore , toggle } from 'mobx-restful' ;
6+ import { stringify } from 'qs' ;
7+
8+ import { StrapiListModel } from './index' ;
69
710export type Base = Record < 'documentId' | 'createdAt' | 'updatedAt' , string > & {
811 id : number ;
912} ;
10- export type BaseUser = Partial <
11- Base &
12- Record < 'username' | 'email' | 'provider' , string > &
13- Record < 'confirmed' | 'blocked' , boolean >
14- > ;
13+ export type BaseUser = Base &
14+ Record < 'username' | 'email' | 'provider' , string > &
15+ Record < 'confirmed' | 'blocked' , boolean > ;
16+
1517export type GithubUser = components [ 'schemas' ] [ 'public-user' ] ;
1618
1719export type OAuthProvider =
@@ -47,31 +49,31 @@ export type Media = Base &
4749 provider_metadata : { } ;
4850 } ;
4951
50- export class SessionModel < T extends BaseUser = BaseUser > extends BaseModel {
52+ export class UserModel <
53+ D extends BaseUser = BaseUser ,
54+ F extends Filter < D > = Filter < D >
55+ > extends StrapiListModel < D , F > {
56+ baseURI = 'users' ;
57+
5158 @persist ( )
5259 @observable
53- accessor accessToken = new URLSearchParams ( globalThis . location ?. search ) . get (
54- 'access_token'
55- ) ;
60+ accessor accessToken = new URLSearchParams ( globalThis . location ?. search ) . get ( 'access_token' ) ;
5661
5762 @persist ( )
5863 @observable
5964 accessor jwt = '' ;
6065
6166 @persist ( )
6267 @observable
63- accessor user : T | undefined ;
68+ accessor session : D | undefined ;
6469
6570 @persist ( )
6671 @observable
6772 accessor userGithub : GithubUser | undefined ;
6873
6974 restored = restore ( this , 'Session' ) ;
7075
71- client = new HTTPClient ( {
72- baseURI : 'http://localhost:1337/api/' ,
73- responseType : 'json'
74- } ) . use ( async ( { request } , next ) => {
76+ client = new HTTPClient ( { responseType : 'json' } ) . use ( async ( { request } , next ) => {
7577 await this . restored ;
7678
7779 request . headers = {
@@ -83,17 +85,22 @@ export class SessionModel<T extends BaseUser = BaseUser> extends BaseModel {
8385 return next ( ) ;
8486 } ) ;
8587
88+ constructor ( baseURL = 'http://localhost:1337/api/' ) {
89+ super ( ) ;
90+ this . client . baseURI = baseURL ;
91+ }
92+
8693 oAuthLinkOf = ( provider : OAuthProvider ) =>
8794 new URL ( `connect/${ provider } ` , this . client . baseURI ) + '' ;
8895
8996 @toggle ( 'uploading' )
9097 async signInOauth ( token = this . accessToken , provider = 'github' ) {
91- const { body } = await this . client . get < { jwt : string ; user : T } > (
98+ const { body } = await this . client . get < { jwt : string ; user : D } > (
9299 `auth/${ provider } /callback?access_token=${ token } `
93100 ) ;
94101 Object . assign ( this , body ) ;
95102
96- return this . getProfile ( body . user . id ) ;
103+ return this . getSession ( ) ;
97104 }
98105
99106 async signOut ( ) {
@@ -103,41 +110,53 @@ export class SessionModel<T extends BaseUser = BaseUser> extends BaseModel {
103110 }
104111
105112 @toggle ( 'downloading' )
106- async getProfile ( id = this . user ?. id ) {
113+ async getSession ( ) {
107114 try {
108- const { body } = await this . client . get < T > ( `users/${ id || 'me' } ` ) ;
115+ const { body } = await this . client . get < D > ( `users/me ` ) ;
109116
110- return ( this . user = body ) ;
117+ return ( this . session = body ) ;
111118 } catch ( error ) {
112119 if ( ( error as HTTPError ) . response . status !== 400 ) throw error ;
113120 }
114121 }
115122
116123 @toggle ( 'downloading' )
117124 async getGithubProfile ( name : string ) {
118- const { body } = await this . client . get < GithubUser > (
119- 'https://api.github.com/users/' + name ,
120- { Authorization : `Bearer ${ this . accessToken } ` }
121- ) ;
122-
125+ const { body } = await this . client . get < GithubUser > ( 'https://api.github.com/users/' + name , {
126+ Authorization : `Bearer ${ this . accessToken } `
127+ } ) ;
123128 return ( this . userGithub = body ) ;
124129 }
125130
131+ @toggle ( 'downloading' )
132+ async getOne ( id : IDType ) {
133+ const { body } = await this . client . get < D > ( `users/${ id } ` ) ;
134+
135+ return ( this . currentOne = body ) ;
136+ }
137+
126138 @toggle ( 'uploading' )
127- async updateProfile ( { id = this . user ?. id , ...data } : NewData < BaseUser > ) {
128- const { body } = await this . client . put < T > ( 'users/' + id , data ) ;
139+ async updateOne ( data : Partial < NewData < D > > , id ?: IDType ) {
140+ const { body } = await ( id
141+ ? this . client . put < D > ( `${ this . baseURI } /${ id } ` , data )
142+ : this . client . post < D > ( this . baseURI , data ) ) ;
143+
144+ return ( this . currentOne = body ! ) ;
145+ }
129146
130- return ( this . user = body ) ;
147+ async loadPage ( pageIndex : number , pageSize : number , filter : F ) {
148+ const query = stringify ( this . makeFilter ( pageIndex , pageSize , filter ) , {
149+ encodeValuesOnly : true
150+ } ) ;
151+ const [ { body : totalCount } , { body : pageData } ] = await Promise . all ( [
152+ this . client . get < number > ( `${ this . baseURI } /count?${ query } ` ) ,
153+ this . client . get < D [ ] > ( `${ this . baseURI } ?${ query } ` )
154+ ] ) ;
155+ return { pageData, totalCount } ;
131156 }
132157
133158 @toggle ( 'uploading' )
134- async upload (
135- model : string ,
136- id : string ,
137- key : string ,
138- files : Blob [ ] ,
139- module ?: string
140- ) {
159+ async upload ( model : string , id : string , key : string , files : Blob [ ] , module ?: string ) {
141160 const data = new FormData ( ) ;
142161
143162 data . append ( 'ref' , model ) ;
0 commit comments