import 'whatwg-fetch' declare function fetch(path: string, opts?: any): any; export interface HasToken { token: string } /*const defaultConfig = { headers: { 'Content-Type': 'application/json;charset=utf-8' } } export function handler(res): T { let raw: string = res['request']['responseText'], first = raw.charAt(0), text = raw.substring(1, raw.length - 2), data if (first === '+') { data = JSON.parse(text) if (data[0]) throw data return data[1] } if (first !== '-') throw new Error('Malformed response.') if (raw.charAt(1) !== '[') throw new Error(text) throw JSON.parse(text) }*/ export type AuthOk = (token: string) => void export type AuthHandler = (cb: AuthOk) => void; export type GetHandler = (location: string, opts?: any) => PromiseLike export type PostHandler = (location: string, data: string) => PromiseLike export function checkStatus(res: any): T { let status = res.status if (status < 200 || status > 299) throw status return res.text() } export function handler(raw: string): T { let first = raw.charAt(0), len = raw.length, end = raw.charAt(len - 1) === '\n' ? len - 2 : len, data if (first === '+') { data = JSON.parse(raw.substring(1, end)) if (data[0]) throw data return data.length === 2 ? data[1] : data } if (first !== '-') throw new Error('Malformed response.') if (raw.charAt(1) !== '[') throw new Error(raw.substring(1, end)) throw JSON.parse(raw.substring(1, end)) } export interface Config { auth$$?: AuthHandler, get$$: GetHandler, post$$: PostHandler } export class P { handlers: any[] = [] cbFail: any = null authOk: AuthOk|null = null constructor(private url: string, private opts: any, private ih: any, private ah?: AuthHandler) { } then(resolve, reject): P { if (this.cbFail) { console.warn('Cannot add a resolve/reject handler once a reject handler is provided.') } else if (resolve) { this.handlers.push(resolve) } else { this.handlers.push(reject) this.run() } return this } catch(reject): P { if (this.cbFail) { console.warn('Only one reject handler is allowed.') } else { this.handlers.push(reject) this.run() } return this } run(token?: string): void { let url = this.url if (token) url = url.substring(0, url.lastIndexOf('=') + 1) + token let f = fetch(url, this.opts).then(this.ih).then(handler), array = this.handlers, last = array.length - 1, i = 0 while (i < last) f.then(array[i++]) f.then(undefined, this.cbFail || (this.cbFail = this.fail.bind(this))) } fail(err) { // check unauthorized if (!this.ah || (err !== 401 && (!Array.isArray(err) || err[0] !== 3))) this.handlers[this.handlers.length - 1](err) else this.ah(this.authOk || (this.authOk = this.run.bind(this))) } } const config_default = { auth$$: undefined, get$$, post$$ } as Config window['rpc_config_d'] = config_default const config: Config = window['rpc_config'] || config_default const prefix: string = window['rpc_host'] || '' export function setAuthHandler(handler: AuthHandler) { config.auth$$ = handler } function post$$(location: string, data: string, delegate?: boolean, initialHandler?: any): PromiseLike { let authHandler = config.auth$$, opts = { method: 'POST', body: data } if (prefix) location = prefix + location return delegate || authHandler ? new P(location, opts, initialHandler || checkStatus, authHandler) : fetch(location, opts).then(initialHandler || checkStatus).then(handler) } export function post(location: string, data: string): PromiseLike { return config.post$$(location, data) } function get$$(location: string, opts?: any, delegate?: boolean, initialHandler?: any): PromiseLike { let authHandler = config.auth$$ if (prefix) location = prefix + location return delegate || authHandler ? new P(location, opts, initialHandler || checkStatus, authHandler) : fetch(location, opts).then(initialHandler || checkStatus).then(handler) } export function get(location: string, opts?: any): PromiseLike { return config.get$$(location, opts) }