1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | return new (P || (P = Promise))(function (resolve, reject) {
|
4 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
7 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8 | });
|
9 | };
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | const request = require("request-promise");
|
12 | const BASE_JSON_OPTIONS = {
|
13 | json: true,
|
14 | };
|
15 | class RequestWrapper {
|
16 | constructor(baseUrl) {
|
17 | this.buildJsonRequest = (path, method = 'GET', options, context) => {
|
18 | return Object.assign({}, BASE_JSON_OPTIONS, {
|
19 | url: RequestWrapper.buildUrl(this.baseUrl, path),
|
20 | method,
|
21 | }, RequestWrapper.getMergedOptions(options, context));
|
22 | };
|
23 | this.baseUrl = baseUrl;
|
24 | }
|
25 | |
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | proxyJsonRequest(path, method = 'GET', options) {
|
33 | return __awaiter(this, void 0, void 0, function* () {
|
34 | return request(this.buildJsonRequest(path, method, options)).catch(RequestWrapper.interceptRequestError);
|
35 | });
|
36 | }
|
37 | |
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | proxyCorrelatedJsonRequest(path, method = 'GET', context, options) {
|
45 | return __awaiter(this, void 0, void 0, function* () {
|
46 | return request(this.buildJsonRequest(path, method, options, context)).catch(RequestWrapper.interceptRequestError);
|
47 | });
|
48 | }
|
49 | }
|
50 | RequestWrapper.buildUrl = (baseUrl, path) => {
|
51 | return `${baseUrl}${path}`;
|
52 | };
|
53 | RequestWrapper.interceptRequestError = error => {
|
54 |
|
55 | error.message = `${error.message} - ${error.options.url}`;
|
56 | throw error;
|
57 | };
|
58 | RequestWrapper.getMergedOptions = (options, context) => {
|
59 | if (!context || !options) {
|
60 | return options;
|
61 | }
|
62 |
|
63 | return Object.assign({}, options, {
|
64 | headers: Object.assign({}, options.headers ? options.headers : {}, {
|
65 | 'x-request-id': context.request.headers['x-request-id'],
|
66 | 'x-b3-traceid': context.request.headers['x-b3-traceid'],
|
67 | 'x-b3-spanid': context.request.headers['x-b3-spanid'],
|
68 | 'x-b3-parentspanid': context.request.headers['x-b3-parentspanid'],
|
69 | 'x-b3-sampled': context.request.headers['x-b3-sampled'],
|
70 | 'x-b3-flags': context.request.headers['x-b3-flags'],
|
71 | 'x-ot-span-context': context.request.headers['x-ot-span-context'],
|
72 | }),
|
73 | });
|
74 | };
|
75 | exports.RequestWrapper = RequestWrapper;
|
76 | function createWrapper(baseUrl) {
|
77 | return new RequestWrapper(baseUrl);
|
78 | }
|
79 | exports.createWrapper = createWrapper;
|