From d18aac8efa8e9b9ae43435d3c898f77a807890fb Mon Sep 17 00:00:00 2001 From: Stefan Zerkalica Date: Sun, 15 May 2022 12:56:04 +0300 Subject: [PATCH] $mol_model: headers, base_api --- model/model.ts | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/model/model.ts b/model/model.ts index deb53a1a2c0..90966ec7fc9 100644 --- a/model/model.ts +++ b/model/model.ts @@ -15,12 +15,38 @@ namespace $ { return instance } + @ $mol_mem + token( next? : string | null ) { + return this.$.$mol_state_local.value( 'token' , next ) + } + + @ $mol_mem + headers() { + + const headers = { + } as Record + + const token = this.token() + if( !token ) return headers + + return { + ... headers, + 'Authorization': `Bearer ${token}`, + } + + } + uri() { return '' } + @ $mol_mem + api_base( next?: string ) { + return this.$.$mol_state_local.value( '$api_base', next ) ?? $mol_dom_context.location.origin + '/' + } + resource_url() { - return this.uri() + return this.api_base() + this.uri() } method_put() { @@ -28,17 +54,18 @@ namespace $ { } @ $mol_mem - json( next? : null | Partial< Raw > ): Raw { + json( next? : Raw ) { 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