UNPKG

@akala/core

Version:
163 lines 8 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const each_1 = require("../each"); const global_injector_1 = require("../global-injector"); const helpers_1 = require("../helpers"); const pathRegexp = __importStar(require("path-to-regexp")); class Rest { constructor(api) { this.api = api; } static buildCall(config, baseURL, param) { var url = config.url; if (url[0] == '/') url = url.substr(1); switch (config.param) { case 'body': return { method: config.method, url: new URL(url, baseURL).toString(), body: param, type: config.type || 'json' }; case 'query': return { method: config.method, url: new URL(url, baseURL).toString(), queryString: param, type: config.type || 'json' }; case 'route': return { method: config.method, url: new URL(pathRegexp.compile(url)(param), baseURL).toString(), type: config.type || 'json' }; default: var route = null; var options = { method: config.method, url: baseURL.toString(), type: config.type || 'json' }; if (typeof (config.param) == 'string') throw new Error(`${config.param} is not a valid value`); if (config.param !== null && typeof (config.param) !== 'undefined') { each_1.each(config.param, function (value, key) { switch (value) { case 'body': if (typeof (param[key]) == 'object') options.body = Object.assign(options.body || {}, param[key]); else { if (!options.body) options.body = {}; options.body[key] = param[key]; } break; case 'header': if (typeof (param[key]) == 'object') options.headers = Object.assign(options.headers || {}, param[key]); else { if (!options.headers) options.headers = {}; options.headers[key] = param[key]; } break; case 'query': if (typeof (param[key]) == 'object') options.queryString = Object.assign(options.queryString || {}, param[key]); else { if (!options.queryString) options.queryString = {}; options.queryString[key] = param[key]; } break; case 'route': if (typeof (param[key]) == 'object') route = Object.assign(route || {}, param[key]); else { if (!route) route = {}; route[key] = param[key]; } break; default: var indexOfDot = value.indexOf('.'); if (~indexOfDot) { var subKey = value.substr(indexOfDot + 1); switch (value.substr(0, indexOfDot)) { case 'body': options.body = options.body || {}; options.body[subKey] = param[key]; break; case 'header': if (!options.headers) options.headers = {}; options.headers[subKey] = param[key]; break; case 'query': if (!options.queryString) options.queryString = {}; options.queryString[subKey] = param[key]; break; case 'route': if (!route) route = {}; route[subKey] = param[key]; break; default: console.log('ignoring ' + value); } break; } else console.log('ignoring ' + value); } }); if (route) options.url = new URL(pathRegexp.compile(url)(route), baseURL).toString(); else options.url = new URL(url, baseURL).toString(); } return options; } } createServerProxy(baseUrl) { var client = global_injector_1.resolve('$http'); var resolveUrl = global_injector_1.resolve('$resolveUrl'); baseUrl = resolveUrl(baseUrl); if (!baseUrl.endsWith('/')) baseUrl += '/'; var proxy = {}; each_1.each(this.api.serverTwoWayConfig, function (config, key) { if (config['rest']) { proxy[key] = function (param) { return client.call(Rest.buildCall(config['rest'], baseUrl, param)).then((res) => { switch (config['rest'].type) { case 'raw': return res; case 'text': return res.text(); case 'json': default: return res.json(); } }); }; } }); each_1.each(this.api.serverOneWayConfig, function (config, key) { if (config['rest']) { proxy[key] = function (param) { return client.call(Rest.buildCall(config['rest'], baseUrl, param)).then((res) => { switch (config['rest'].type) { case 'raw': return res; case 'text': return res.text(); case 'json': default: return res.json(); } }); }; } }); return proxy; } createClient(baseUrl) { return (impl) => Object.assign(impl, { $proxy: () => { return this.createServerProxy(baseUrl); } }); } } exports.Rest = Rest; helpers_1.module('$api').register('rest', Rest); //# sourceMappingURL=rest.js.map