Skip to content

Instantly share code, notes, and snippets.

@zerkalica
Created September 30, 2023 07:36
Show Gist options
  • Save zerkalica/d920021960d9d89b076044e01c00b208 to your computer and use it in GitHub Desktop.
Save zerkalica/d920021960d9d89b076044e01c00b208 to your computer and use it in GitHub Desktop.
transport.ts
namespace $ {
export type $gd_rad_transport_req = Omit<RequestInit, 'headers'> & {
path: string;
headers?: Record<string, string>;
noAuth?: boolean;
body_object?: object
}
export class $gd_kit_transport extends $mol_object {
@ $mol_mem
static url_base() {
return ''
}
static origin_default() {
const loc = this.$.$mol_dom_context.location
return loc.hostname === 'localhost' ? 'https://some-host' : loc.origin
}
static ws_origin() {
return this.origin().replace(/^http/, 'ws')
}
@ $mol_mem
static origin( next?: string ) {
return this.$.$mol_state_local.value( '$gd_kit_transport:origin', next ) ?? this.origin_default()
}
static url_prefix() {
return this.origin() + this.url_base()
}
static token_key() {
return 'kc_mol_at'
}
@ $mol_mem
static token( next? : string | null ) {
return this.$.$mol_state_local.value(this.token_key(), next) ?? undefined
}
@ $mol_mem
static headers_default(): Record<string, string> {
return {
'Content-Type': 'application/json',
}
}
@ $mol_mem
static headers() {
const headers = this.headers_default()
const token = this.token()
if( !token ) return headers
return {
... headers,
'Authorization': `Bearer ${token}`,
}
}
static get(path: string, params?: Omit<$gd_rad_transport_req, 'path'>) {
return this.response({ ...params, path, method: 'GET' })
}
static head(path: string, params?: Omit<$gd_rad_transport_req, 'path'>) {
return this.response({ ...params, path, method: 'HEAD' })
}
static post(path: string, params?: Omit<$gd_rad_transport_req, 'path'>) {
return this.response({ ...params, path, method: 'POST' })
}
static delete(path: string, params?: Omit<$gd_rad_transport_req, 'path'>) {
return this.response({ ...params, path, method: 'DELETE' })
}
protected static response(params: $gd_rad_transport_req) {
return this.response_real(params, $mol_wire_auto()?.toString() ?? '')
}
@ $mol_action
protected static response_real(params: $gd_rad_transport_req, debug_name: string) {
const url = this.url_prefix() + params.path
const headers: $gd_rad_transport_req['headers'] = {
... this.headers(),
'X-Requested-From': debug_name,
... params.headers,
}
if (params.noAuth && headers['Authorization']) delete headers['Authorization']
const body = params.body ?? (params.body_object ? JSON.stringify(params.body_object) : undefined)
const init: RequestInit = { ...params, body, headers }
// const [, res_run ] = $mol_wire_race(
// () => {
// this.$.$mol_wait_timeout(5000)
// throw new Error('timeout')
// },
// () => this.$.$mol_fetch.response( url , init)
// )
// const res = res_run()
const res = this.$.$mol_fetch.response( url , init)
if( res.status() === 'success' ) return res
if (res.code() === 403) this.token(null)
throw new Error( res.message(), { cause: res } )
}
static error_res(e: unknown) {
return e instanceof Error && e.cause instanceof $mol_fetch_response ? e.cause : undefined
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment