import { env, Global, inject, Kernel } from "oly"; import { HttpClient, IHttpRequest, IHttpResponse } from "oly-http"; import { PixieSession } from "./PixieSession"; import { PixieStore } from "./PixieStore"; /** * HttpClient with PixieStore. * * All get|post|... requests are wrapped with PixieStore#fly(). */ export class PixieHttp extends HttpClient { /** * Force ApiRoot. */ @env("PIXIE_HTTP_ROOT") public apiRoot: string = ""; @inject protected kernel: Kernel; @inject protected store: PixieStore; @inject protected session: PixieSession; /** * API Prefix. Each request starting with / will be prefixed by root. See PIXIE_HTTP_ROOT. */ public get root(): string { if (Global.isBrowser()) { return this.apiRoot || this.store.get("API_PREFIX") || "/api"; } else { // try to create API_ROOT based on oly-http an oly-api if available const port = this.kernel.state("HTTP_SERVER_PORT") || 3000; const host = this.kernel.state("HTTP_SERVER_HOST") || "localhost"; const prefix = this.kernel.state("API_PREFIX") || ""; if (prefix) { this.store.set("API_PREFIX", prefix); } return `http://${host}:${port}${prefix}`; } } /** * Like HttpClient#request() with * * - an auto-prefix if possible (see PixieHttp#root) * - auto-authorization if possible (see PixieSession#getIdentity()) * *
* Response is not CACHED into PixieStore unlike PixieHttp#get() & cie. * * @param options HttpClient request options */ public request(options: IHttpRequest): Promise> { options.method = options.method || "GET"; options.url = options.url || "/"; options.headers = options.headers || {}; if (options.url.indexOf("http") !== 0) { options.url = this.root + options.url; } const identity = this.session.getIdentity(); if (!!identity) { options.headers["Authorization"] = `Bearer ${identity}`; } return super.request(options); } /** * Make a HTTP GET and cache response with PixieStore#fly().
* Based on HttpClient#get(). * * @param {string} url * @param {IHttpRequest} options * @returns {Promise} */ public get(url: string, options?: IHttpRequest): Promise { return this.store.fly(`GET_${url}`, () => super.get(url, options)); } /** * Make a HTTP POST and cache response with PixieStore#fly().
* Based on HttpClient#post(). * * @param {string} url * @param body * @param {IHttpRequest} options * @returns {Promise} */ public post(url: string, body?: any, options?: IHttpRequest): Promise { return this.store.fly(`POST_${url}`, () => super.post(url, body, options)); } /** * Make a HTTP PUT and cache response with PixieStore#fly().
* Based on HttpClient#put(). * * @param {string} url * @param body * @param {IHttpRequest} options * @returns {Promise} */ public put(url: string, body?: any, options?: IHttpRequest): Promise { return this.store.fly(`PUT_${url}`, () => super.put(url, body, options)); } /** * Make a HTTP DEL and cache response with PixieStore#fly().
* Based on HttpClient#del(). * * @param {string} url * @param {IHttpRequest} options * @returns {Promise} */ public del(url: string, options?: IHttpRequest): Promise { return this.store.fly(`DEL_${url}`, () => super.del(url, options)); } }