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

$mol_model: headers, base_api #492

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
33 changes: 30 additions & 3 deletions model/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,57 @@ namespace $ {
return instance
}

@ $mol_mem
token( next? : string | null ) {
return this.$.$mol_state_local.value( 'token' , next )
}

@ $mol_mem
headers() {

const headers = {
} as Record<string, string | string[]>

const token = this.token()
if( !token ) return headers

return {
... headers,
'Authorization': `Bearer ${token}`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не думаю, что хорошая идея хардкодить конкретный тип авторизации в общем модуле.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Да, хотелось бы иметь возможность задать стратегию авторизации такую какую мне надо,

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Проще всего это сделать через наследование с переопределением метода, возвращающего заголовки.

Copy link
Contributor

@osv osv May 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Может оставить заглушкой для переопределения метода headers? В противном случае если авторизация по JWT надо переопределять полностю json() метод, а это много копипасты

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Агась, логично.

}

}

uri() {
return ''
}

@ $mol_mem
api_base( next?: string ) {
return this.$.$mol_state_local.value( '$api_base', next ) ?? $mol_dom_context.location.origin + '/'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Разные модели могут работать с разными api.

}

resource_url() {
return this.uri()
return this.api_base() + this.uri()
}

method_put() {
return 'PUT'
}

@ $mol_mem
json( next? : null | Partial< Raw > ): Raw {
json( next? : Raw ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null используется, чтобы энфорсить повторную загрузку данных.


const prev = this.json_update()
if( next ) return this.json_update({ ... prev, ... next })
if( next === undefined && prev !== undefined ) return prev

let json = $mol_fetch.json( this.resource_url() , {
const json = this.$.$mol_fetch.json( this.resource_url() , {
method : next ? this.method_put() : 'GET' ,
body : next && JSON.stringify( next ) ,
headers : {
'content-type' : 'application/json',
... this.headers()
},
} ) as Raw

Expand Down