@teko-builder/utils
Advanced tools
Comparing version 0.2.4 to 0.2.5
@@ -17,3 +17,3 @@ import { ApiError } from './error'; | ||
appendParamsToURL(url: string, params?: any): string; | ||
request(url: string, body: FormData | any, method: string, config?: RequestConfig): Promise<{ | ||
request(path: string, body: FormData | any, method: string, config?: RequestConfig): Promise<{ | ||
code: number; | ||
@@ -20,0 +20,0 @@ data: any; |
@@ -0,1 +1,2 @@ | ||
import { globalTracer, FORMAT_HTTP_HEADERS } from 'opentracing'; | ||
import pino from 'pino'; | ||
@@ -23,3 +24,3 @@ import pinoHttp from 'pino-http'; | ||
vietnameseName: /^[a-zA-ZÀÁÂÃÈÉÊÌÍÒÓÔÕÙÚĂĐĨŨƠàáâãèéêìíòóôõùúăđĩũơƯĂẠẢẤẦẨẪẬẮẰẲẴẶẸẺẼỀỀỂẾưăạảấầẩẫậắằẳẵặẹẻẽềềểếỄỆỈỊỌỎỐỒỔỖỘỚỜỞỠỢỤỦỨỪễệỉịọỏốồổỗộớờởỡợụủứừỬỮỰỲỴÝỶỸửữựỳỵýỷỹ\s\w|_]+$/, | ||
domain: new RegExp(`^(${domainNameRegex.source}|((?!www)${domainNameRegex.source}{2}))[a-z]{2,63}$`, 'i'), | ||
domain: new RegExp(`^(${domainNameRegex.source}|((?!www\.)${domainNameRegex.source}{2,}))[a-z]{2,63}$`, 'i'), | ||
}; | ||
@@ -72,6 +73,8 @@ | ||
} | ||
async request(url, body, method, config = {}) { | ||
async request(path, body, method, config = {}) { | ||
var _a; | ||
const { params: configParams } = config; | ||
let headers = config.headers || {}; | ||
url = this.appendParamsToURL(url, config.params); | ||
const url = this.appendParamsToURL(path, configParams || {}); | ||
let serializedBody = null; | ||
if (body) { | ||
@@ -81,3 +84,3 @@ if (typeof body !== 'string') { | ||
else { | ||
body = JSON.stringify(body); | ||
serializedBody = JSON.stringify(body); | ||
headers = Object.assign({ 'Content-Type': 'application/json' }, headers); | ||
@@ -87,3 +90,17 @@ } | ||
} | ||
const requestObj = Object.assign(Object.assign({}, this.defaultConfig), { method: method, headers: new Headers(headers), body }); | ||
let serializedParams = null; | ||
if (configParams) { | ||
serializedParams = JSON.stringify(configParams); | ||
} | ||
const tracer = globalTracer(); | ||
const spanName = `[${method}] ${normalizeSlashUrl(this.baseURL, path)}`; | ||
const span = tracer.startSpan(spanName); | ||
tracer.inject(span, FORMAT_HTTP_HEADERS, headers); | ||
span.setTag('http.method', method); | ||
span.setTag('http.baseurl', this.baseURL); | ||
span.log({ | ||
body: serializedBody, | ||
params: serializedParams, | ||
}); | ||
const requestObj = Object.assign(Object.assign({}, this.defaultConfig), { method: method, headers: new Headers(headers), body: serializedBody }); | ||
this.interceptors.forEach((fn) => fn(requestObj)); | ||
@@ -94,2 +111,7 @@ const res = await fetch(url.startsWith('http') ? url : normalizeSlashUrl(this.baseURL, url), requestObj); | ||
const error = ((_a = this.getError) === null || _a === void 0 ? void 0 : _a.call(this, resp)) || new ApiError(DEFAULT_ERROR_MESSAGE, -1, resp); | ||
span.setTag('status', 'error'); | ||
span.log({ | ||
error: JSON.stringify(error), | ||
}); | ||
span.finish(); | ||
return Promise.reject(error); | ||
@@ -100,2 +122,4 @@ } | ||
const data = await this._parseJson(res); | ||
span.setTag('status', 'success'); | ||
span.finish(); | ||
return { code: res.status, data, headers: respHeaders }; | ||
@@ -102,0 +126,0 @@ } |
@@ -5,2 +5,3 @@ 'use strict'; | ||
var opentracing = require('opentracing'); | ||
var pino = require('pino'); | ||
@@ -33,3 +34,3 @@ var pinoHttp = require('pino-http'); | ||
vietnameseName: /^[a-zA-ZÀÁÂÃÈÉÊÌÍÒÓÔÕÙÚĂĐĨŨƠàáâãèéêìíòóôõùúăđĩũơƯĂẠẢẤẦẨẪẬẮẰẲẴẶẸẺẼỀỀỂẾưăạảấầẩẫậắằẳẵặẹẻẽềềểếỄỆỈỊỌỎỐỒỔỖỘỚỜỞỠỢỤỦỨỪễệỉịọỏốồổỗộớờởỡợụủứừỬỮỰỲỴÝỶỸửữựỳỵýỷỹ\s\w|_]+$/, | ||
domain: new RegExp(`^(${domainNameRegex.source}|((?!www)${domainNameRegex.source}{2}))[a-z]{2,63}$`, 'i'), | ||
domain: new RegExp(`^(${domainNameRegex.source}|((?!www\.)${domainNameRegex.source}{2,}))[a-z]{2,63}$`, 'i'), | ||
}; | ||
@@ -82,6 +83,8 @@ | ||
} | ||
async request(url, body, method, config = {}) { | ||
async request(path, body, method, config = {}) { | ||
var _a; | ||
const { params: configParams } = config; | ||
let headers = config.headers || {}; | ||
url = this.appendParamsToURL(url, config.params); | ||
const url = this.appendParamsToURL(path, configParams || {}); | ||
let serializedBody = null; | ||
if (body) { | ||
@@ -91,3 +94,3 @@ if (typeof body !== 'string') { | ||
else { | ||
body = JSON.stringify(body); | ||
serializedBody = JSON.stringify(body); | ||
headers = Object.assign({ 'Content-Type': 'application/json' }, headers); | ||
@@ -97,3 +100,17 @@ } | ||
} | ||
const requestObj = Object.assign(Object.assign({}, this.defaultConfig), { method: method, headers: new Headers(headers), body }); | ||
let serializedParams = null; | ||
if (configParams) { | ||
serializedParams = JSON.stringify(configParams); | ||
} | ||
const tracer = opentracing.globalTracer(); | ||
const spanName = `[${method}] ${normalizeSlashUrl(this.baseURL, path)}`; | ||
const span = tracer.startSpan(spanName); | ||
tracer.inject(span, opentracing.FORMAT_HTTP_HEADERS, headers); | ||
span.setTag('http.method', method); | ||
span.setTag('http.baseurl', this.baseURL); | ||
span.log({ | ||
body: serializedBody, | ||
params: serializedParams, | ||
}); | ||
const requestObj = Object.assign(Object.assign({}, this.defaultConfig), { method: method, headers: new Headers(headers), body: serializedBody }); | ||
this.interceptors.forEach((fn) => fn(requestObj)); | ||
@@ -104,2 +121,7 @@ const res = await fetch(url.startsWith('http') ? url : normalizeSlashUrl(this.baseURL, url), requestObj); | ||
const error = ((_a = this.getError) === null || _a === void 0 ? void 0 : _a.call(this, resp)) || new ApiError(DEFAULT_ERROR_MESSAGE, -1, resp); | ||
span.setTag('status', 'error'); | ||
span.log({ | ||
error: JSON.stringify(error), | ||
}); | ||
span.finish(); | ||
return Promise.reject(error); | ||
@@ -110,2 +132,4 @@ } | ||
const data = await this._parseJson(res); | ||
span.setTag('status', 'success'); | ||
span.finish(); | ||
return { code: res.status, data, headers: respHeaders }; | ||
@@ -112,0 +136,0 @@ } |
{ | ||
"name": "@teko-builder/utils", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"description": "Utility functions", | ||
@@ -14,2 +14,3 @@ "license": "MIT", | ||
"dependencies": { | ||
"opentracing": "^0.14.7", | ||
"pino": "^6.11.3", | ||
@@ -16,0 +17,0 @@ "pino-http": "^5.5.0", |
@@ -0,0 +0,0 @@ { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22978
528
6
+ Addedopentracing@^0.14.7
+ Addedopentracing@0.14.7(transitive)