UNPKG

@jorsek/ezd-client

Version:
101 lines 4.38 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const axios_1 = require("axios"); require("reflect-metadata"); const auth_1 = require("./browser_auth/auth"); const Config_1 = require("./Config"); const Content_1 = require("./resources/Content"); const Contribute_1 = require("./resources/Contribute"); const Search_1 = require("./resources/Search"); const nookies_1 = require("nookies"); const EZD_CLIENT_VERSION = "0.1.0"; const API_VERSION = "v1"; class Client { constructor(args) { const { org, token, rootMapId, timeout = 3000, scheme = "https://", hostname = "content.easydita.com", env = "latest", } = args; this.initial_args = args; if (!org) { throw new Error("easyDITA org not set"); } if (!token) { throw new Error("easyDITA token not set"); } if (!rootMapId) { throw new Error("easyDITA rootMapId not set"); } this.base_url = `${scheme}${hostname}/org/${org}/env/${env}/map/${rootMapId}/`; this.content_service_token = token; const config = { baseURL: this.base_url, timeout, // withCredentials: true, headers: { // "X-EZD-Client": "JS/" + EZD_CLIENT_VERSION, }, }; this.content_axios = axios_1.default.create(config); this.content_axios.interceptors.request.use(intercepted_config => { if (!intercepted_config.params) { intercepted_config.params = {}; } // intercepted_config.headers["Content-Type"] = "application/text"; intercepted_config.params.token = this.user_token() ? this.user_token() : this.content_service_token; return intercepted_config; }); this.contribute_axios = axios_1.default.create(config); this.contribute_axios.interceptors.request.use(intercepted_config => { if (!intercepted_config.params) { intercepted_config.params = {}; } intercepted_config.params.rootMapId = rootMapId; intercepted_config.params.token = this.user_token(); return intercepted_config; }); const portal_config = new Config_1.Config(this.content_axios); this.content = new Content_1.Content({ axios: this.content_axios, config: portal_config }); this.search = new Search_1.Search({ axios: this.content_axios, rootMapId, config: portal_config }); this.contribute = new Contribute_1.Contribute({ axios: this.contribute_axios, config: portal_config }); } iframe_url() { let s = this.base_url; s = s.replace("https://", ""); s = s.replace("http://", ""); return s; } user_token() { if (typeof window !== "undefined") { return auth_1.getUserTokens().idToken; } else { return null; } } login() { return __awaiter(this, void 0, void 0, function* () { // create an ezd session so the editor will load without asking user to login first const response = yield this.content_axios.post("/auth"); }); } prepare(args) { if (args.req) { const cookies = nookies_1.parseCookies(args); if (cookies.portal_auth_token) { return new Client(Object.assign({}, this.initial_args, { token: cookies.portal_auth_token })); } if (args.req.query.portal_auth_token) { return new Client(Object.assign({}, this.initial_args, { token: args.req.query.portal_auth_token })); } } return this; } } exports.default = Client; //# sourceMappingURL=Client.js.map