@springworks/api-client
Advanced tools
Comparing version 3005.287.0 to 3005.293.0
import { HTTPStatus } from '@springworks/http-status'; | ||
import { type Logger } from '@springworks/logger-interfaces'; | ||
import { type ApiClientRequestOptions, type CircuitBreakerConfig, ApiClient as IApiClient, BodyWithFullResponse } from './main'; | ||
import { type Mtls } from './mtls-helpers'; | ||
import RequestMaker from './request-maker'; | ||
@@ -9,4 +10,5 @@ export declare class ApiClient implements IApiClient { | ||
private readonly circuit_breaker?; | ||
private readonly source_identifier; | ||
constructor(request_maker: RequestMaker, logger: Logger, source_identifier: string, circuit_breaker_config?: CircuitBreakerConfig); | ||
private readonly source_identifier?; | ||
private readonly agent?; | ||
constructor(request_maker: RequestMaker, logger: Logger, source_identifier?: string, circuit_breaker_config?: CircuitBreakerConfig, mtls_config?: Mtls); | ||
sendRequest<T = any>(req_opts: ApiClientRequestOptions, expected_status_codes: HTTPStatus[]): Promise<T>; | ||
@@ -13,0 +15,0 @@ sendRequestWithFullResponse<Body = any, Code extends HTTPStatus = number>(req_opts: ApiClientRequestOptions, expected_status_codes: HTTPStatus[]): Promise<BodyWithFullResponse<Body, Code>>; |
@@ -8,17 +8,12 @@ "use strict"; | ||
const input_validator_1 = require("@springworks/input-validator"); | ||
const mtls_helpers_1 = require("./mtls-helpers"); | ||
const UNEXPECTED_RESPONSE_STATUS_CODE = 417; | ||
function addSourceHeader(req_opts, source_identifier) { | ||
return { | ||
...req_opts, | ||
headers: { | ||
...req_opts.headers, | ||
'm2h-source-identifier': source_identifier, | ||
}, | ||
}; | ||
} | ||
class ApiClient { | ||
constructor(request_maker, logger, source_identifier, circuit_breaker_config) { | ||
constructor(request_maker, logger, source_identifier, circuit_breaker_config, mtls_config) { | ||
this.request_maker = request_maker; | ||
this.logger = logger.child({ context: 'api-client', target_base_url: request_maker.base_url }); | ||
this.source_identifier = source_identifier; | ||
if (mtls_config) { | ||
this.agent = (0, mtls_helpers_1.getAgent)(mtls_config); | ||
} | ||
if (circuit_breaker_config) { | ||
@@ -33,5 +28,11 @@ const validated_circuit_breaker_config = validateCircuitBreakerConfig(circuit_breaker_config); | ||
const circuitBreakerCommand = async (success, failure) => { | ||
var _a; | ||
try { | ||
const req_opts_with_source_header = addSourceHeader(req_opts, this.source_identifier); | ||
const { res, body } = await this.request_maker.makeRequest(req_opts_with_source_header); | ||
if (this.source_identifier) { | ||
((_a = req_opts.headers) !== null && _a !== void 0 ? _a : (req_opts.headers = {}))['m2h-source-identifier'] = this.source_identifier; | ||
} | ||
if (this.agent && !req_opts.agent) { | ||
req_opts.agent = this.agent; | ||
} | ||
const { res, body } = await this.request_maker.makeRequest(req_opts); | ||
if (wasExpectedStatusCode(validated_expected_status_codes, res)) { | ||
@@ -66,4 +67,4 @@ success(); | ||
} | ||
catch (err) { | ||
const error = maybeScrubError(err); | ||
catch (cause) { | ||
const error = maybeScrubError(cause); | ||
const is_timeout = (error === null || error === void 0 ? void 0 : error.type) === 'request-timeout'; | ||
@@ -76,3 +77,3 @@ const request_error = (0, error_factory_1.createError)({ | ||
this.logger.error({ | ||
err: request_error, | ||
error: request_error, | ||
original_error: error, | ||
@@ -87,3 +88,3 @@ req_opts: loggableRequestOptions(req_opts), | ||
const circuitBreakerFallback = () => { | ||
const err = (0, error_factory_1.createError)({ | ||
const error = (0, error_factory_1.createError)({ | ||
code: http_status_1.HTTP5XX.SERVICE_UNAVAILABLE, | ||
@@ -93,3 +94,3 @@ message: 'Circuit breaker open', | ||
this.logger.error({ | ||
err, | ||
error, | ||
payload: { | ||
@@ -99,3 +100,3 @@ req_opts: loggableRequestOptions(req_opts), | ||
}, 'Circuit breaker open'); | ||
reject(err); | ||
reject(error); | ||
}; | ||
@@ -152,2 +153,3 @@ if (this.circuit_breaker) { | ||
function loggableRequestOptions(req_opts) { | ||
// eslint-disable-next-line deprecation/deprecation | ||
const { auth: _auth, json: _json, body: _body, form: _form, headers: _headers, agent: _agent, ...rest } = req_opts; | ||
@@ -154,0 +156,0 @@ if (rest.qs === undefined) { |
@@ -8,2 +8,4 @@ /// <reference types="node" /> | ||
import { Logger } from '@springworks/logger-interfaces'; | ||
import { type Mtls, parseMtlsProps } from './mtls-helpers'; | ||
export { type Mtls, parseMtlsProps }; | ||
export interface CircuitBreakerConfig { | ||
@@ -50,2 +52,3 @@ source_name: string; | ||
opt_options?: ApiClientCreateOptionalsConfig; | ||
mtls?: Mtls; | ||
} | ||
@@ -52,0 +55,0 @@ export type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createClient = void 0; | ||
exports.createClient = exports.parseMtlsProps = void 0; | ||
const input_validator_1 = require("@springworks/input-validator"); | ||
const api_client_1 = require("./api-client"); | ||
const mtls_helpers_1 = require("./mtls-helpers"); | ||
Object.defineProperty(exports, "parseMtlsProps", { enumerable: true, get: function () { return mtls_helpers_1.parseMtlsProps; } }); | ||
const request_maker_1 = require("./request-maker"); | ||
@@ -25,2 +27,3 @@ const circuit_breaker_config_schema = input_validator_1.joi.object().keys({ | ||
source_identifier: input_validator_1.joi.string().optional(), | ||
mtls: input_validator_1.joi.object().optional(), | ||
opt_options: input_validator_1.joi | ||
@@ -35,9 +38,11 @@ .object() | ||
function createClient(options) { | ||
var _a, _b, _c; | ||
var _a, _b; | ||
const validated_params = validateCreateParams(options); | ||
const logger = validated_params.logger.child({ context: 'api-client', target_base_url: validated_params.base_url }); | ||
const source_identifier = (_a = validated_params.source_identifier) !== null && _a !== void 0 ? _a : 'unknown'; | ||
const req_options = (_b = validated_params.opt_options) !== null && _b !== void 0 ? _b : {}; | ||
const request_maker = new request_maker_1.default(validated_params.base_url, req_options.opt_timeout, (_c = validated_params.auth) !== null && _c !== void 0 ? _c : req_options.opt_auth, logger); | ||
return new api_client_1.ApiClient(request_maker, logger, source_identifier, validated_params.circuit_breaker_config); | ||
const source_identifier = validated_params.source_identifier; | ||
const req_options = (_a = validated_params.opt_options) !== null && _a !== void 0 ? _a : {}; | ||
const request_maker = new request_maker_1.default(validated_params.base_url, req_options.opt_timeout, | ||
// eslint-disable-next-line deprecation/deprecation | ||
(_b = validated_params.auth) !== null && _b !== void 0 ? _b : req_options.opt_auth, logger); | ||
return new api_client_1.ApiClient(request_maker, logger, source_identifier, validated_params.circuit_breaker_config, validated_params.mtls); | ||
} | ||
@@ -44,0 +49,0 @@ exports.createClient = createClient; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getRequestBody = exports.objectFromHeaders = exports.default_user_agent_string = void 0; | ||
/* eslint-disable deprecation/deprecation */ | ||
const url_1 = require("url"); | ||
const node_fetch_1 = require("node-fetch"); | ||
const default_http_request_timeout = 3000; | ||
let version; | ||
void (async () => { | ||
// @ts-expect-error the import will still work even without adding option `--resolveJsonModule` | ||
const package_json = (await Promise.resolve().then(() => require('../package.json'))); | ||
version = package_json.version; | ||
})(); | ||
exports.default_user_agent_string = (() => { | ||
var _a; | ||
// TODO: maybe remove require or figure out how to do this line in ESM | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const file = require('../package.json'); | ||
const client_version = `SpringworksApiClient/${(_a = file.version) !== null && _a !== void 0 ? _a : ''}`; | ||
const client_version = `SpringworksApiClient/${version !== null && version !== void 0 ? version : ''}`; | ||
const node_version = `Node/${process.version.replace('v', '')}`; | ||
@@ -55,4 +58,3 @@ return `${client_version} ${node_version}`; | ||
} | ||
catch (err) { | ||
const error = err; | ||
catch (error) { | ||
const response_body_string = toString(response_body); | ||
@@ -59,0 +61,0 @@ if ((_a = options.json) !== null && _a !== void 0 ? _a : !response_body_string.trim()) { |
{ | ||
"name": "@springworks/api-client", | ||
"version": "3005.287.0", | ||
"version": "3005.293.0", | ||
"description": "Module for sending requests to other APIs through a circuit breaker", | ||
@@ -32,3 +32,3 @@ "main": "build/main.js", | ||
}, | ||
"gitHead": "cc7f45e776b516074c01e6a939a3fd621d04db8d" | ||
"gitHead": "ed2a49d08d965c1b0fa724278467b3c58fd942c3" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
78644
19
638
1