@apimatic/core
Advanced tools
Comparing version 0.9.2 to 0.9.3
@@ -0,3 +1,6 @@ | ||
import { __read } from 'tslib'; | ||
import { getHeader } from '@apimatic/http-headers'; | ||
import { detect } from 'detect-browser'; | ||
import warning from 'tiny-warning'; | ||
import { JsonPointer } from 'json-ptr'; | ||
/** | ||
@@ -87,3 +90,77 @@ * Validates the protocol and removes duplicate forward slashes | ||
} | ||
/** | ||
* Replace the templated placeholders in error with the platform | ||
* related information. | ||
* @param message message value to be updated | ||
* @returns Updated message value | ||
*/ | ||
export { deprecated, sanitizeUrl, updateUserAgent }; | ||
function updateErrorMessage(message, response) { | ||
var placeholders = message.match(/\{\$.*?\}/g); | ||
var statusCodePlaceholder = placeholders === null || placeholders === void 0 ? void 0 : placeholders.includes('{$statusCode}'); | ||
var headerPlaceholders = placeholders === null || placeholders === void 0 ? void 0 : placeholders.filter(function (value) { | ||
return value.startsWith('{$response.header'); | ||
}); | ||
var bodyPlaceholders = placeholders === null || placeholders === void 0 ? void 0 : placeholders.filter(function (value) { | ||
return value.startsWith('{$response.body'); | ||
}); | ||
message = replaceStatusCodePlaceholder(message, response.statusCode, statusCodePlaceholder); | ||
message = replaceHeaderPlaceholders(message, response.headers, headerPlaceholders); | ||
if (typeof response.body === 'string') { | ||
message = replaceBodyPlaceholders(message, response.body, bodyPlaceholders); | ||
} | ||
return message; | ||
} | ||
function replaceStatusCodePlaceholder(message, statusCode, statusCodePlaceholder) { | ||
if (statusCodePlaceholder) { | ||
return message.replace('{$statusCode}', statusCode.toString()); | ||
} | ||
return message; | ||
} | ||
function replaceHeaderPlaceholders(message, headers, headerPlaceholders) { | ||
if (headerPlaceholders) { | ||
headerPlaceholders.forEach(function (element) { | ||
var _a, _b; | ||
var headerName = (_a = element.split('.').pop()) === null || _a === void 0 ? void 0 : _a.slice(0, -1); | ||
if (typeof headerName !== 'undefined') { | ||
var value = (_b = getHeader(headers, headerName)) !== null && _b !== void 0 ? _b : ''; | ||
message = message.replace(element, value); | ||
} | ||
}); | ||
} | ||
return message; | ||
} | ||
function replaceBodyPlaceholders(message, body, bodyPlaceholders) { | ||
var parsed = JSON.parse(body); | ||
bodyPlaceholders === null || bodyPlaceholders === void 0 ? void 0 : bodyPlaceholders.forEach(function (element) { | ||
var _a; | ||
if (element.includes('#')) { | ||
var _b = __read(element === null || element === void 0 ? void 0 : element.split('#')), | ||
rest = _b.slice(1); | ||
var nodePointer = (_a = rest.join('#')) === null || _a === void 0 ? void 0 : _a.slice(0, -1); | ||
if (nodePointer) { | ||
var value = JsonPointer.create(nodePointer).get(parsed); | ||
var replaced_value = typeof value !== 'undefined' ? JSON.stringify(value) : ''; | ||
message = message.replace(element, replaced_value); | ||
} | ||
} else { | ||
message = message.replace(element, JSON.stringify(parsed)); | ||
} | ||
}); | ||
return message; | ||
} | ||
export { deprecated, sanitizeUrl, updateErrorMessage, updateUserAgent }; |
import { __spreadArray, __read, __assign, __awaiter, __generator } from 'tslib'; | ||
import JSONBig from '@apimatic/json-bigint'; | ||
import { deprecated, sanitizeUrl } from '../apiHelper.js'; | ||
import { deprecated, sanitizeUrl, updateErrorMessage } from '../apiHelper.js'; | ||
import { ArgumentsValidationError } from '../errors/argumentsValidationError.js'; | ||
@@ -26,6 +26,6 @@ import { ResponseValidationError } from '../errors/responseValidationError.js'; | ||
function () { | ||
function DefaultRequestBuilder(_httpClient, _baseUrlProvider, _apiErrorFactory, _authenticationProvider, _httpMethod, _xmlSerializer, _retryConfig, _path) { | ||
function DefaultRequestBuilder(_httpClient, _baseUrlProvider, _apiErrorCtr, _authenticationProvider, _httpMethod, _xmlSerializer, _retryConfig, _path) { | ||
this._httpClient = _httpClient; | ||
this._baseUrlProvider = _baseUrlProvider; | ||
this._apiErrorFactory = _apiErrorFactory; | ||
this._apiErrorCtr = _apiErrorCtr; | ||
this._authenticationProvider = _authenticationProvider; | ||
@@ -40,2 +40,5 @@ this._httpMethod = _httpMethod; | ||
this._validateResponse = true; | ||
this._apiErrorFactory = { | ||
apiErrorCtor: _apiErrorCtr | ||
}; | ||
@@ -250,4 +253,7 @@ this._addResponseValidator(); | ||
DefaultRequestBuilder.prototype.defaultToError = function (apiErrorCtor) { | ||
this._apiErrorFactory = apiErrorCtor; | ||
DefaultRequestBuilder.prototype.defaultToError = function (apiErrorCtor, message) { | ||
this._apiErrorFactory = { | ||
apiErrorCtor: apiErrorCtor, | ||
message: message | ||
}; | ||
}; | ||
@@ -259,7 +265,7 @@ | ||
DefaultRequestBuilder.prototype.throwOn = function (statusCode, errorConstructor) { | ||
DefaultRequestBuilder.prototype.throwOn = function (statusCode, errorConstructor, isTemplate) { | ||
var args = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
args[_i - 2] = arguments[_i]; | ||
for (var _i = 3; _i < arguments.length; _i++) { | ||
args[_i - 3] = arguments[_i]; | ||
} | ||
@@ -270,2 +276,6 @@ | ||
if (isTemplate && args.length > 0) { | ||
args[0] = updateErrorMessage(args[0], response); | ||
} | ||
if (typeof statusCode === 'number' && response.statusCode === statusCode || typeof statusCode !== 'number' && response.statusCode >= statusCode[0] && response.statusCode <= statusCode[1]) { | ||
@@ -539,6 +549,12 @@ throw new (errorConstructor.bind.apply(errorConstructor, __spreadArray([void 0, context], __read(args))))(); | ||
this.interceptResponse(function (context) { | ||
var _a; | ||
var response = context.response; | ||
if (_this._validateResponse && (response.statusCode < 200 || response.statusCode >= 300)) { | ||
throw new _this._apiErrorFactory(context, "Response status code was not ok: " + response.statusCode + "."); | ||
if (typeof ((_a = _this._apiErrorFactory) === null || _a === void 0 ? void 0 : _a.message) === 'undefined') { | ||
_this._apiErrorFactory.message = "Response status code was not ok: " + response.statusCode + "."; | ||
} | ||
throw new _this._apiErrorFactory.apiErrorCtor(context, _this._apiErrorFactory.message); | ||
} | ||
@@ -659,3 +675,3 @@ | ||
function createRequestBuilderFactory(httpClient, baseUrlProvider, apiErrorFactory, authenticationProvider, retryConfig, xmlSerializer) { | ||
function createRequestBuilderFactory(httpClient, baseUrlProvider, apiErrorConstructor, authenticationProvider, retryConfig, xmlSerializer) { | ||
if (xmlSerializer === void 0) { | ||
@@ -666,3 +682,3 @@ xmlSerializer = new XmlSerialization(); | ||
return function (httpMethod, path) { | ||
return new DefaultRequestBuilder(httpClient, baseUrlProvider, apiErrorFactory, authenticationProvider, httpMethod, xmlSerializer, retryConfig, path); | ||
return new DefaultRequestBuilder(httpClient, baseUrlProvider, apiErrorConstructor, authenticationProvider, httpMethod, xmlSerializer, retryConfig, path); | ||
}; | ||
@@ -669,0 +685,0 @@ } |
@@ -1,2 +0,2 @@ | ||
export { deprecated, sanitizeUrl, updateUserAgent } from './apiHelper.js'; | ||
export { deprecated, sanitizeUrl, updateErrorMessage, updateUserAgent } from './apiHelper.js'; | ||
export * from '@apimatic/core-interfaces'; | ||
@@ -3,0 +3,0 @@ export * from '@apimatic/file-wrapper'; |
@@ -0,1 +1,2 @@ | ||
import { HttpResponse } from '@apimatic/core-interfaces'; | ||
/** | ||
@@ -25,2 +26,9 @@ * Validates the protocol and removes duplicate forward slashes | ||
export declare function updateUserAgent(userAgent: string, apiVersion?: string, detail?: string): string; | ||
/** | ||
* Replace the templated placeholders in error with the platform | ||
* related information. | ||
* @param message message value to be updated | ||
* @returns Updated message value | ||
*/ | ||
export declare function updateErrorMessage(message: string, response: HttpResponse): string; | ||
//# sourceMappingURL=apiHelper.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.updateUserAgent = exports.deprecated = exports.sanitizeUrl = void 0; | ||
exports.updateErrorMessage = exports.updateUserAgent = exports.deprecated = exports.sanitizeUrl = void 0; | ||
var tslib_1 = require("tslib"); | ||
var http_headers_1 = require("@apimatic/http-headers"); | ||
var detect_browser_1 = require("detect-browser"); | ||
var tiny_warning_1 = tslib_1.__importDefault(require("tiny-warning")); | ||
var json_ptr_1 = require("json-ptr"); | ||
/** | ||
@@ -78,1 +80,62 @@ * Validates the protocol and removes duplicate forward slashes | ||
} | ||
/** | ||
* Replace the templated placeholders in error with the platform | ||
* related information. | ||
* @param message message value to be updated | ||
* @returns Updated message value | ||
*/ | ||
function updateErrorMessage(message, response) { | ||
var placeholders = message.match(/\{\$.*?\}/g); | ||
var statusCodePlaceholder = placeholders === null || placeholders === void 0 ? void 0 : placeholders.includes('{$statusCode}'); | ||
var headerPlaceholders = placeholders === null || placeholders === void 0 ? void 0 : placeholders.filter(function (value) { | ||
return value.startsWith('{$response.header'); | ||
}); | ||
var bodyPlaceholders = placeholders === null || placeholders === void 0 ? void 0 : placeholders.filter(function (value) { | ||
return value.startsWith('{$response.body'); | ||
}); | ||
message = replaceStatusCodePlaceholder(message, response.statusCode, statusCodePlaceholder); | ||
message = replaceHeaderPlaceholders(message, response.headers, headerPlaceholders); | ||
if (typeof response.body === 'string') { | ||
message = replaceBodyPlaceholders(message, response.body, bodyPlaceholders); | ||
} | ||
return message; | ||
} | ||
exports.updateErrorMessage = updateErrorMessage; | ||
function replaceStatusCodePlaceholder(message, statusCode, statusCodePlaceholder) { | ||
if (statusCodePlaceholder) { | ||
return message.replace('{$statusCode}', statusCode.toString()); | ||
} | ||
return message; | ||
} | ||
function replaceHeaderPlaceholders(message, headers, headerPlaceholders) { | ||
if (headerPlaceholders) { | ||
headerPlaceholders.forEach(function (element) { | ||
var _a, _b; | ||
var headerName = (_a = element.split('.').pop()) === null || _a === void 0 ? void 0 : _a.slice(0, -1); | ||
if (typeof headerName !== 'undefined') { | ||
var value = (_b = http_headers_1.getHeader(headers, headerName)) !== null && _b !== void 0 ? _b : ''; | ||
message = message.replace(element, value); | ||
} | ||
}); | ||
} | ||
return message; | ||
} | ||
function replaceBodyPlaceholders(message, body, bodyPlaceholders) { | ||
var parsed = JSON.parse(body); | ||
bodyPlaceholders === null || bodyPlaceholders === void 0 ? void 0 : bodyPlaceholders.forEach(function (element) { | ||
var _a; | ||
if (element.includes('#')) { | ||
var _b = tslib_1.__read(element === null || element === void 0 ? void 0 : element.split('#')), rest = _b.slice(1); | ||
var nodePointer = (_a = rest.join('#')) === null || _a === void 0 ? void 0 : _a.slice(0, -1); | ||
if (nodePointer) { | ||
var value = json_ptr_1.JsonPointer.create(nodePointer).get(parsed); | ||
var replaced_value = typeof value !== 'undefined' ? JSON.stringify(value) : ''; | ||
message = message.replace(element, replaced_value); | ||
} | ||
} | ||
else { | ||
message = message.replace(element, JSON.stringify(parsed)); | ||
} | ||
}); | ||
return message; | ||
} |
@@ -15,2 +15,6 @@ /// <reference types="node" /> | ||
export declare type ApiErrorConstructor = new (response: HttpContext, message: string) => any; | ||
export interface ApiErrorFactory { | ||
apiErrorCtor: ApiErrorConstructor; | ||
message?: string | undefined; | ||
} | ||
export interface RequestBuilder<BaseUrlParamType, AuthParams> { | ||
@@ -42,5 +46,6 @@ deprecated(methodName: string, message?: string): void; | ||
interceptResponse(interceptor: (response: HttpContext) => HttpContext): void; | ||
defaultToError(apiErrorCtor: ApiErrorConstructor): void; | ||
defaultToError(apiErrorCtor: ApiErrorConstructor, message?: string): void; | ||
validateResponse(validate: boolean): void; | ||
throwOn<ErrorCtorArgs extends any[]>(statusCode: number | [number, number], errorConstructor: new (response: HttpContext, ...args: ErrorCtorArgs) => any, ...args: ErrorCtorArgs): void; | ||
throwOn<ErrorCtorArgs extends any[]>(statusCode: number | [number, number], errorConstructor: new (response: HttpContext, ...args: ErrorCtorArgs) => any, isTemplate: boolean, ...args: ErrorCtorArgs): void; | ||
call(requestOptions?: RequestOptions): Promise<ApiResponse<void>>; | ||
@@ -57,3 +62,3 @@ callAsJson<T>(schema: Schema<T, any>, requestOptions?: RequestOptions): Promise<ApiResponse<T>>; | ||
protected _baseUrlProvider: (arg?: BaseUrlParamType) => string; | ||
protected _apiErrorFactory: ApiErrorConstructor; | ||
protected _apiErrorCtr: ApiErrorConstructor; | ||
protected _authenticationProvider: AuthenticatorInterface<AuthParams>; | ||
@@ -77,4 +82,5 @@ protected _httpMethod: HttpMethod; | ||
protected _retryOption: RequestRetryOption; | ||
protected _apiErrorFactory: ApiErrorFactory; | ||
prepareArgs: typeof prepareArgs; | ||
constructor(_httpClient: HttpClientInterface, _baseUrlProvider: (arg?: BaseUrlParamType) => string, _apiErrorFactory: ApiErrorConstructor, _authenticationProvider: AuthenticatorInterface<AuthParams>, _httpMethod: HttpMethod, _xmlSerializer: XmlSerializerInterface, _retryConfig: RetryConfiguration, _path?: string | undefined); | ||
constructor(_httpClient: HttpClientInterface, _baseUrlProvider: (arg?: BaseUrlParamType) => string, _apiErrorCtr: ApiErrorConstructor, _authenticationProvider: AuthenticatorInterface<AuthParams>, _httpMethod: HttpMethod, _xmlSerializer: XmlSerializerInterface, _retryConfig: RetryConfiguration, _path?: string | undefined); | ||
authenticate(params: AuthParams): void; | ||
@@ -104,3 +110,3 @@ requestRetryOption(option: RequestRetryOption): void; | ||
interceptResponse(interceptor: (response: HttpContext) => HttpContext): void; | ||
defaultToError(apiErrorCtor: ApiErrorConstructor): void; | ||
defaultToError(apiErrorCtor: ApiErrorConstructor, message?: string): void; | ||
validateResponse(validate: boolean): void; | ||
@@ -119,4 +125,4 @@ throwOn<ErrorCtorArgs extends any[]>(statusCode: number | [number, number], errorConstructor: new (response: HttpContext, ...args: ErrorCtorArgs) => any, ...args: ErrorCtorArgs): void; | ||
} | ||
export declare function createRequestBuilderFactory<BaseUrlParamType, AuthParams>(httpClient: HttpClientInterface, baseUrlProvider: (arg?: BaseUrlParamType) => string, apiErrorFactory: ApiErrorConstructor, authenticationProvider: AuthenticatorInterface<AuthParams>, retryConfig: RetryConfiguration, xmlSerializer?: XmlSerializerInterface): RequestBuilderFactory<BaseUrlParamType, AuthParams>; | ||
export declare function createRequestBuilderFactory<BaseUrlParamType, AuthParams>(httpClient: HttpClientInterface, baseUrlProvider: (arg?: BaseUrlParamType) => string, apiErrorConstructor: ApiErrorConstructor, authenticationProvider: AuthenticatorInterface<AuthParams>, retryConfig: RetryConfiguration, xmlSerializer?: XmlSerializerInterface): RequestBuilderFactory<BaseUrlParamType, AuthParams>; | ||
export {}; | ||
//# sourceMappingURL=requestBuilder.d.ts.map |
@@ -24,6 +24,6 @@ "use strict"; | ||
var DefaultRequestBuilder = /** @class */ (function () { | ||
function DefaultRequestBuilder(_httpClient, _baseUrlProvider, _apiErrorFactory, _authenticationProvider, _httpMethod, _xmlSerializer, _retryConfig, _path) { | ||
function DefaultRequestBuilder(_httpClient, _baseUrlProvider, _apiErrorCtr, _authenticationProvider, _httpMethod, _xmlSerializer, _retryConfig, _path) { | ||
this._httpClient = _httpClient; | ||
this._baseUrlProvider = _baseUrlProvider; | ||
this._apiErrorFactory = _apiErrorFactory; | ||
this._apiErrorCtr = _apiErrorCtr; | ||
this._authenticationProvider = _authenticationProvider; | ||
@@ -38,2 +38,3 @@ this._httpMethod = _httpMethod; | ||
this._validateResponse = true; | ||
this._apiErrorFactory = { apiErrorCtor: _apiErrorCtr }; | ||
this._addResponseValidator(); | ||
@@ -181,4 +182,4 @@ this._addAuthentication(); | ||
}; | ||
DefaultRequestBuilder.prototype.defaultToError = function (apiErrorCtor) { | ||
this._apiErrorFactory = apiErrorCtor; | ||
DefaultRequestBuilder.prototype.defaultToError = function (apiErrorCtor, message) { | ||
this._apiErrorFactory = { apiErrorCtor: apiErrorCtor, message: message }; | ||
}; | ||
@@ -188,9 +189,12 @@ DefaultRequestBuilder.prototype.validateResponse = function (validate) { | ||
}; | ||
DefaultRequestBuilder.prototype.throwOn = function (statusCode, errorConstructor) { | ||
DefaultRequestBuilder.prototype.throwOn = function (statusCode, errorConstructor, isTemplate) { | ||
var args = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
args[_i - 2] = arguments[_i]; | ||
for (var _i = 3; _i < arguments.length; _i++) { | ||
args[_i - 3] = arguments[_i]; | ||
} | ||
this.interceptResponse(function (context) { | ||
var response = context.response; | ||
if (isTemplate && args.length > 0) { | ||
args[0] = apiHelper_1.updateErrorMessage(args[0], response); | ||
} | ||
if ((typeof statusCode === 'number' && | ||
@@ -364,6 +368,10 @@ response.statusCode === statusCode) || | ||
this.interceptResponse(function (context) { | ||
var _a; | ||
var response = context.response; | ||
if (_this._validateResponse && | ||
(response.statusCode < 200 || response.statusCode >= 300)) { | ||
throw new _this._apiErrorFactory(context, "Response status code was not ok: " + response.statusCode + "."); | ||
if (typeof ((_a = _this._apiErrorFactory) === null || _a === void 0 ? void 0 : _a.message) === 'undefined') { | ||
_this._apiErrorFactory.message = "Response status code was not ok: " + response.statusCode + "."; | ||
} | ||
throw new _this._apiErrorFactory.apiErrorCtor(context, _this._apiErrorFactory.message); | ||
} | ||
@@ -439,6 +447,6 @@ return context; | ||
exports.DefaultRequestBuilder = DefaultRequestBuilder; | ||
function createRequestBuilderFactory(httpClient, baseUrlProvider, apiErrorFactory, authenticationProvider, retryConfig, xmlSerializer) { | ||
function createRequestBuilderFactory(httpClient, baseUrlProvider, apiErrorConstructor, authenticationProvider, retryConfig, xmlSerializer) { | ||
if (xmlSerializer === void 0) { xmlSerializer = new xmlSerializer_1.XmlSerialization(); } | ||
return function (httpMethod, path) { | ||
return new DefaultRequestBuilder(httpClient, baseUrlProvider, apiErrorFactory, authenticationProvider, httpMethod, xmlSerializer, retryConfig, path); | ||
return new DefaultRequestBuilder(httpClient, baseUrlProvider, apiErrorConstructor, authenticationProvider, httpMethod, xmlSerializer, retryConfig, path); | ||
}; | ||
@@ -445,0 +453,0 @@ } |
{ | ||
"name": "@apimatic/core", | ||
"author": "APIMatic Ltd.", | ||
"version": "0.9.2", | ||
"version": "0.9.3", | ||
"license": "MIT", | ||
@@ -72,2 +72,3 @@ "sideEffects": false, | ||
"form-data": "^3.0.0", | ||
"json-ptr": "^3.1.0", | ||
"lodash.flatmap": "^4.5.0", | ||
@@ -74,0 +75,0 @@ "tiny-warning": "^1.0.3", |
@@ -0,3 +1,6 @@ | ||
import { HttpResponse } from '@apimatic/core-interfaces'; | ||
import { getHeader } from '@apimatic/http-headers'; | ||
import { detect } from 'detect-browser'; | ||
import warning from 'tiny-warning'; | ||
import { JsonPointer } from 'json-ptr'; | ||
@@ -81,1 +84,87 @@ /** | ||
} | ||
/** | ||
* Replace the templated placeholders in error with the platform | ||
* related information. | ||
* @param message message value to be updated | ||
* @returns Updated message value | ||
*/ | ||
export function updateErrorMessage( | ||
message: string, | ||
response: HttpResponse | ||
): string { | ||
const placeholders = message.match(/\{\$.*?\}/g); | ||
const statusCodePlaceholder = placeholders?.includes('{$statusCode}'); | ||
const headerPlaceholders = placeholders?.filter((value) => | ||
value.startsWith('{$response.header') | ||
); | ||
const bodyPlaceholders = placeholders?.filter((value) => | ||
value.startsWith('{$response.body') | ||
); | ||
message = replaceStatusCodePlaceholder( | ||
message, | ||
response.statusCode, | ||
statusCodePlaceholder | ||
); | ||
message = replaceHeaderPlaceholders( | ||
message, | ||
response.headers, | ||
headerPlaceholders | ||
); | ||
if (typeof response.body === 'string') { | ||
message = replaceBodyPlaceholders(message, response.body, bodyPlaceholders); | ||
} | ||
return message; | ||
} | ||
function replaceStatusCodePlaceholder( | ||
message: string, | ||
statusCode: number, | ||
statusCodePlaceholder?: boolean | ||
): string { | ||
if (statusCodePlaceholder) { | ||
return message.replace('{$statusCode}', statusCode.toString()); | ||
} | ||
return message; | ||
} | ||
function replaceHeaderPlaceholders( | ||
message: string, | ||
headers: Record<string, string>, | ||
headerPlaceholders?: string[] | ||
): string { | ||
if (headerPlaceholders) { | ||
headerPlaceholders.forEach((element) => { | ||
const headerName = element.split('.').pop()?.slice(0, -1); | ||
if (typeof headerName !== 'undefined') { | ||
const value = getHeader(headers, headerName) ?? ''; | ||
message = message.replace(element, value); | ||
} | ||
}); | ||
} | ||
return message; | ||
} | ||
function replaceBodyPlaceholders( | ||
message: string, | ||
body: string, | ||
bodyPlaceholders?: string[] | ||
): string { | ||
const parsed = JSON.parse(body); | ||
bodyPlaceholders?.forEach((element) => { | ||
if (element.includes('#')) { | ||
const [, ...rest] = element?.split('#'); | ||
const nodePointer = rest.join('#')?.slice(0, -1); | ||
if (nodePointer) { | ||
const value = JsonPointer.create(nodePointer).get(parsed); | ||
const replaced_value = | ||
typeof value !== 'undefined' ? JSON.stringify(value) : ''; | ||
message = message.replace(element, replaced_value); | ||
} | ||
} else { | ||
message = message.replace(element, JSON.stringify(parsed)); | ||
} | ||
}); | ||
return message; | ||
} |
import JSONBig from '@apimatic/json-bigint'; | ||
import { FileWrapper } from '@apimatic/file-wrapper'; | ||
import { deprecated, sanitizeUrl } from '../apiHelper'; | ||
import { deprecated, sanitizeUrl, updateErrorMessage } from '../apiHelper'; | ||
import { | ||
@@ -93,2 +93,7 @@ ApiResponse, | ||
export interface ApiErrorFactory { | ||
apiErrorCtor: ApiErrorConstructor; | ||
message?: string | undefined; | ||
} | ||
export interface RequestBuilder<BaseUrlParamType, AuthParams> { | ||
@@ -143,3 +148,3 @@ deprecated(methodName: string, message?: string): void; | ||
interceptResponse(interceptor: (response: HttpContext) => HttpContext): void; | ||
defaultToError(apiErrorCtor: ApiErrorConstructor): void; | ||
defaultToError(apiErrorCtor: ApiErrorConstructor, message?: string): void; | ||
validateResponse(validate: boolean): void; | ||
@@ -154,2 +159,11 @@ throwOn<ErrorCtorArgs extends any[]>( | ||
): void; | ||
throwOn<ErrorCtorArgs extends any[]>( | ||
statusCode: number | [number, number], | ||
errorConstructor: new ( | ||
response: HttpContext, | ||
...args: ErrorCtorArgs | ||
) => any, | ||
isTemplate: boolean, | ||
...args: ErrorCtorArgs | ||
): void; | ||
call(requestOptions?: RequestOptions): Promise<ApiResponse<void>>; | ||
@@ -196,2 +210,3 @@ callAsJson<T>( | ||
protected _retryOption: RequestRetryOption; | ||
protected _apiErrorFactory: ApiErrorFactory; | ||
public prepareArgs: typeof prepareArgs; | ||
@@ -202,3 +217,3 @@ | ||
protected _baseUrlProvider: (arg?: BaseUrlParamType) => string, | ||
protected _apiErrorFactory: ApiErrorConstructor, | ||
protected _apiErrorCtr: ApiErrorConstructor, | ||
protected _authenticationProvider: AuthenticatorInterface<AuthParams>, | ||
@@ -214,2 +229,3 @@ protected _httpMethod: HttpMethod, | ||
this._validateResponse = true; | ||
this._apiErrorFactory = { apiErrorCtor: _apiErrorCtr }; | ||
this._addResponseValidator(); | ||
@@ -394,4 +410,7 @@ this._addAuthentication(); | ||
} | ||
public defaultToError(apiErrorCtor: ApiErrorConstructor): void { | ||
this._apiErrorFactory = apiErrorCtor; | ||
public defaultToError( | ||
apiErrorCtor: ApiErrorConstructor, | ||
message?: string | ||
): void { | ||
this._apiErrorFactory = { apiErrorCtor, message }; | ||
} | ||
@@ -408,5 +427,17 @@ public validateResponse(validate: boolean): void { | ||
...args: ErrorCtorArgs | ||
): void; | ||
public throwOn<ErrorCtorArgs extends any[]>( | ||
statusCode: number | [number, number], | ||
errorConstructor: new ( | ||
response: HttpContext, | ||
...args: ErrorCtorArgs | ||
) => any, | ||
isTemplate?: boolean, | ||
...args: ErrorCtorArgs | ||
): void { | ||
this.interceptResponse((context) => { | ||
const { response } = context; | ||
if (isTemplate && args.length > 0) { | ||
args[0] = updateErrorMessage(args[0], response); | ||
} | ||
if ( | ||
@@ -551,5 +582,8 @@ (typeof statusCode === 'number' && | ||
) { | ||
throw new this._apiErrorFactory( | ||
if (typeof this._apiErrorFactory?.message === 'undefined') { | ||
this._apiErrorFactory.message = `Response status code was not ok: ${response.statusCode}.`; | ||
} | ||
throw new this._apiErrorFactory.apiErrorCtor( | ||
context, | ||
`Response status code was not ok: ${response.statusCode}.` | ||
this._apiErrorFactory.message | ||
); | ||
@@ -616,3 +650,3 @@ } | ||
baseUrlProvider: (arg?: BaseUrlParamType) => string, | ||
apiErrorFactory: ApiErrorConstructor, | ||
apiErrorConstructor: ApiErrorConstructor, | ||
authenticationProvider: AuthenticatorInterface<AuthParams>, | ||
@@ -626,3 +660,3 @@ retryConfig: RetryConfiguration, | ||
baseUrlProvider, | ||
apiErrorFactory, | ||
apiErrorConstructor, | ||
authenticationProvider, | ||
@@ -629,0 +663,0 @@ httpMethod, |
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
143873
3547
15
+ Addedjson-ptr@^3.1.0
+ Addedjson-ptr@3.1.1(transitive)