UNPKG

@chuxingpay/hc-faas-base

Version:

FuXun Hotel Connector Base Objects

45 lines (44 loc) 1.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const got_1 = require("got"); class Request { constructor(config, caller) { this.token = config.token; this.host = config.host; this.caller = caller.name; this.version = caller.version; this.request = got_1.default.extend({ prefixUrl: this.host }); } invokeGeneric(endpoint, parameter) { parameter.responseType = 'json'; parameter.retry = { limit: 3, }; const options = parameter; options.headers = {}; const ua = `${this.caller}/${this.version} Node.js/${process.version} pid/${process.pid}`; options.headers['User-Agent'] = ua; options.headers['user-agent'] = ua; options.headers['Authorization'] = `Key ${this.token}`; return this.request(endpoint, options); } get(endpoint, parameter) { return this.invokeGeneric(endpoint, Object.assign({ method: 'GET' }, parameter)); } post(endpoint, parameter) { return this.invokeGeneric(endpoint, Object.assign({ method: 'POST' }, parameter)); } put(endpoint, parameter) { return this.invokeGeneric(endpoint, Object.assign({ method: 'PUT' }, parameter)); } patch(endpoint, parameter) { return this.invokeGeneric(endpoint, Object.assign({ method: 'PATCH' }, parameter)); } delete(endpoint, parameter) { return this.invokeGeneric(endpoint, Object.assign({ method: 'DELETE' }, parameter)); } head(endpoint, parameter) { return this.invokeGeneric(endpoint, Object.assign({ method: 'HEAD' }, parameter)); } } exports.default = Request;