@apimatic/core
Advanced tools
Comparing version 0.10.5 to 0.10.6
@@ -1,2 +0,2 @@ | ||
import { __spreadArray, __read, __assign, __awaiter, __generator } from 'tslib'; | ||
import { __spreadArray, __read, __assign, __awaiter, __generator, __values } from 'tslib'; | ||
import JSONBig from '@apimatic/json-bigint'; | ||
@@ -23,3 +23,3 @@ import { deprecated, sanitizeUrl, updateErrorMessage } from '../apiHelper.js'; | ||
function () { | ||
function DefaultRequestBuilder(_httpClient, _baseUrlProvider, _apiErrorCtr, _authenticationProvider, _httpMethod, _xmlSerializer, _retryConfig, _path) { | ||
function DefaultRequestBuilder(_httpClient, _baseUrlProvider, _apiErrorCtr, _authenticationProvider, _httpMethod, _xmlSerializer, _retryConfig, _path, _apiLogger) { | ||
this._httpClient = _httpClient; | ||
@@ -33,5 +33,7 @@ this._baseUrlProvider = _baseUrlProvider; | ||
this._path = _path; | ||
this._apiLogger = _apiLogger; | ||
this._headers = {}; | ||
this._query = []; | ||
this._interceptors = []; | ||
this._errorTypes = []; | ||
this._validateResponse = true; | ||
@@ -44,2 +46,4 @@ this._apiErrorFactory = { | ||
this._addRetryInterceptor(); | ||
this._addErrorHandlingInterceptor(); | ||
this._addApiLoggerInterceptors(); | ||
this._retryOption = RequestRetryOption.Default; | ||
@@ -210,11 +214,7 @@ this.prepareArgs = prepareArgs.bind(this); | ||
} | ||
this.interceptResponse(function (context) { | ||
var response = context.response; | ||
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]) { | ||
throw new (errorConstructor.bind.apply(errorConstructor, __spreadArray([void 0, context], __read(args))))(); | ||
} | ||
return context; | ||
this._errorTypes.push({ | ||
statusCode: statusCode, | ||
errorConstructor: errorConstructor, | ||
isTemplate: isTemplate, | ||
args: args | ||
}); | ||
@@ -422,2 +422,24 @@ }; | ||
}; | ||
DefaultRequestBuilder.prototype._addApiLoggerInterceptors = function () { | ||
var _this = this; | ||
if (this._apiLogger) { | ||
var apiLogger_1 = this._apiLogger; | ||
this.intercept(function (request, options, next) { | ||
return __awaiter(_this, void 0, void 0, function () { | ||
var context; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
apiLogger_1.logRequest(request); | ||
return [4 /*yield*/, next(request, options)]; | ||
case 1: | ||
context = _a.sent(); | ||
apiLogger_1.logResponse(context.response); | ||
return [2 /*return*/, context]; | ||
} | ||
}); | ||
}); | ||
}); | ||
} | ||
}; | ||
DefaultRequestBuilder.prototype._addAuthentication = function () { | ||
@@ -493,5 +515,38 @@ var _this = this; | ||
}; | ||
DefaultRequestBuilder.prototype._addErrorHandlingInterceptor = function () { | ||
var _this = this; | ||
this.interceptResponse(function (context) { | ||
var e_1, _a; | ||
var response = context.response; | ||
try { | ||
for (var _b = __values(_this._errorTypes), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var _d = _c.value, | ||
statusCode = _d.statusCode, | ||
errorConstructor = _d.errorConstructor, | ||
isTemplate = _d.isTemplate, | ||
args = _d.args; | ||
if (typeof statusCode === 'number' && response.statusCode === statusCode || typeof statusCode !== 'number' && response.statusCode >= statusCode[0] && response.statusCode <= statusCode[1]) { | ||
if (isTemplate && args.length > 0) { | ||
args[0] = updateErrorMessage(args[0], response); | ||
} | ||
throw new (errorConstructor.bind.apply(errorConstructor, __spreadArray([void 0, context], __read(args))))(); | ||
} | ||
} | ||
} catch (e_1_1) { | ||
e_1 = { | ||
error: e_1_1 | ||
}; | ||
} finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
} finally { | ||
if (e_1) throw e_1.error; | ||
} | ||
} | ||
return context; | ||
}); | ||
}; | ||
return DefaultRequestBuilder; | ||
}(); | ||
function createRequestBuilderFactory(httpClient, baseUrlProvider, apiErrorConstructor, authenticationProvider, retryConfig, xmlSerializer) { | ||
function createRequestBuilderFactory(httpClient, baseUrlProvider, apiErrorConstructor, authenticationProvider, retryConfig, xmlSerializer, apiLogger) { | ||
if (xmlSerializer === void 0) { | ||
@@ -501,3 +556,3 @@ xmlSerializer = new XmlSerialization(); | ||
return function (httpMethod, path) { | ||
return new DefaultRequestBuilder(httpClient, baseUrlProvider, apiErrorConstructor, authenticationProvider, httpMethod, xmlSerializer, retryConfig, path); | ||
return new DefaultRequestBuilder(httpClient, baseUrlProvider, apiErrorConstructor, authenticationProvider, httpMethod, xmlSerializer, retryConfig, path, apiLogger); | ||
}; | ||
@@ -504,0 +559,0 @@ } |
@@ -13,2 +13,6 @@ export { deprecated, sanitizeUrl, updateErrorMessage, updateUserAgent } from './apiHelper.js'; | ||
export { RequestRetryOption } from './http/retryConfiguration.js'; | ||
export { ApiLogger } from './logger/apiLogger.js'; | ||
export { ConsoleLogger } from './logger/defaultLogger.js'; | ||
export { NullLogger } from './logger/nullLogger.js'; | ||
export { DEFAULT_LOGGING_OPTIONS, mergeLoggingOptions } from './logger/defaultLoggingConfiguration.js'; | ||
export { commaPrefix, indexedPrefix, pipePrefix, plainPrefix, tabPrefix, unindexedPrefix } from '@apimatic/http-query'; |
/// <reference types="node" /> | ||
import { FileWrapper } from '@apimatic/file-wrapper'; | ||
import { ApiResponse, AuthenticatorInterface, HttpContext, HttpMethod, HttpRequest, HttpRequestMultipartFormBody, HttpRequestUrlEncodedFormBody, HttpResponse, HttpInterceptorInterface, RequestOptions, RetryConfiguration } from '../coreInterfaces'; | ||
import { ApiResponse, AuthenticatorInterface, HttpContext, HttpMethod, HttpRequest, HttpRequestMultipartFormBody, HttpRequestUrlEncodedFormBody, HttpResponse, HttpInterceptorInterface, RequestOptions, RetryConfiguration, ApiLoggerInterface } from '../coreInterfaces'; | ||
import { Schema } from '../schema'; | ||
@@ -15,2 +15,8 @@ import { PathTemplatePrimitiveTypes, PathTemplateTypes, SkipEncode } from './pathTemplate'; | ||
export declare type ApiErrorConstructor = new (response: HttpContext, message: string) => any; | ||
export interface ErrorType<ErrorCtorArgs extends any[]> { | ||
statusCode: number | [number, number]; | ||
errorConstructor: new (response: HttpContext, ...args: ErrorCtorArgs) => any; | ||
isTemplate?: boolean; | ||
args: ErrorCtorArgs; | ||
} | ||
export interface ApiErrorFactory { | ||
@@ -67,2 +73,3 @@ apiErrorCtor: ApiErrorConstructor; | ||
protected _path?: string | undefined; | ||
protected _apiLogger?: ApiLoggerInterface | undefined; | ||
protected _accept?: string; | ||
@@ -82,4 +89,5 @@ protected _contentType?: string; | ||
protected _apiErrorFactory: ApiErrorFactory; | ||
protected _errorTypes: Array<ErrorType<any>>; | ||
prepareArgs: typeof prepareArgs; | ||
constructor(_httpClient: HttpClientInterface, _baseUrlProvider: (arg?: BaseUrlParamType) => string, _apiErrorCtr: 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, _apiLogger?: ApiLoggerInterface | undefined); | ||
authenticate(params: AuthParams): void; | ||
@@ -120,7 +128,9 @@ requestRetryOption(option: RequestRetryOption): void; | ||
private _addResponseValidator; | ||
private _addApiLoggerInterceptors; | ||
private _addAuthentication; | ||
private _addRetryInterceptor; | ||
private _addErrorHandlingInterceptor; | ||
} | ||
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 declare function createRequestBuilderFactory<BaseUrlParamType, AuthParams>(httpClient: HttpClientInterface, baseUrlProvider: (arg?: BaseUrlParamType) => string, apiErrorConstructor: ApiErrorConstructor, authenticationProvider: AuthenticatorInterface<AuthParams>, retryConfig: RetryConfiguration, xmlSerializer?: XmlSerializerInterface, apiLogger?: ApiLoggerInterface): RequestBuilderFactory<BaseUrlParamType, AuthParams>; | ||
export {}; | ||
//# sourceMappingURL=requestBuilder.d.ts.map |
@@ -24,3 +24,3 @@ "use strict"; | ||
var DefaultRequestBuilder = /** @class */ (function () { | ||
function DefaultRequestBuilder(_httpClient, _baseUrlProvider, _apiErrorCtr, _authenticationProvider, _httpMethod, _xmlSerializer, _retryConfig, _path) { | ||
function DefaultRequestBuilder(_httpClient, _baseUrlProvider, _apiErrorCtr, _authenticationProvider, _httpMethod, _xmlSerializer, _retryConfig, _path, _apiLogger) { | ||
this._httpClient = _httpClient; | ||
@@ -34,5 +34,7 @@ this._baseUrlProvider = _baseUrlProvider; | ||
this._path = _path; | ||
this._apiLogger = _apiLogger; | ||
this._headers = {}; | ||
this._query = []; | ||
this._interceptors = []; | ||
this._errorTypes = []; | ||
this._validateResponse = true; | ||
@@ -43,2 +45,4 @@ this._apiErrorFactory = { apiErrorCtor: _apiErrorCtr }; | ||
this._addRetryInterceptor(); | ||
this._addErrorHandlingInterceptor(); | ||
this._addApiLoggerInterceptors(); | ||
this._retryOption = retryConfiguration_1.RequestRetryOption.Default; | ||
@@ -194,16 +198,3 @@ this.prepareArgs = validate_1.prepareArgs.bind(this); | ||
} | ||
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' && | ||
response.statusCode === statusCode) || | ||
(typeof statusCode !== 'number' && | ||
response.statusCode >= statusCode[0] && | ||
response.statusCode <= statusCode[1])) { | ||
throw new (errorConstructor.bind.apply(errorConstructor, tslib_1.__spreadArray([void 0, context], tslib_1.__read(args))))(); | ||
} | ||
return context; | ||
}); | ||
this._errorTypes.push({ statusCode: statusCode, errorConstructor: errorConstructor, isTemplate: isTemplate, args: args }); | ||
}; | ||
@@ -380,2 +371,22 @@ DefaultRequestBuilder.prototype.call = function (requestOptions) { | ||
}; | ||
DefaultRequestBuilder.prototype._addApiLoggerInterceptors = function () { | ||
var _this = this; | ||
if (this._apiLogger) { | ||
var apiLogger_1 = this._apiLogger; | ||
this.intercept(function (request, options, next) { return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var context; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
apiLogger_1.logRequest(request); | ||
return [4 /*yield*/, next(request, options)]; | ||
case 1: | ||
context = _a.sent(); | ||
apiLogger_1.logResponse(context.response); | ||
return [2 /*return*/, context]; | ||
} | ||
}); | ||
}); }); | ||
} | ||
}; | ||
DefaultRequestBuilder.prototype._addAuthentication = function () { | ||
@@ -444,9 +455,40 @@ var _this = this; | ||
}; | ||
DefaultRequestBuilder.prototype._addErrorHandlingInterceptor = function () { | ||
var _this = this; | ||
this.interceptResponse(function (context) { | ||
var e_1, _a; | ||
var response = context.response; | ||
try { | ||
for (var _b = tslib_1.__values(_this | ||
._errorTypes), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var _d = _c.value, statusCode = _d.statusCode, errorConstructor = _d.errorConstructor, isTemplate = _d.isTemplate, args = _d.args; | ||
if ((typeof statusCode === 'number' && | ||
response.statusCode === statusCode) || | ||
(typeof statusCode !== 'number' && | ||
response.statusCode >= statusCode[0] && | ||
response.statusCode <= statusCode[1])) { | ||
if (isTemplate && args.length > 0) { | ||
args[0] = apiHelper_1.updateErrorMessage(args[0], response); | ||
} | ||
throw new (errorConstructor.bind.apply(errorConstructor, tslib_1.__spreadArray([void 0, context], tslib_1.__read(args))))(); | ||
} | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
return context; | ||
}); | ||
}; | ||
return DefaultRequestBuilder; | ||
}()); | ||
exports.DefaultRequestBuilder = DefaultRequestBuilder; | ||
function createRequestBuilderFactory(httpClient, baseUrlProvider, apiErrorConstructor, authenticationProvider, retryConfig, xmlSerializer) { | ||
function createRequestBuilderFactory(httpClient, baseUrlProvider, apiErrorConstructor, authenticationProvider, retryConfig, xmlSerializer, apiLogger) { | ||
if (xmlSerializer === void 0) { xmlSerializer = new xmlSerializer_1.XmlSerialization(); } | ||
return function (httpMethod, path) { | ||
return new DefaultRequestBuilder(httpClient, baseUrlProvider, apiErrorConstructor, authenticationProvider, httpMethod, xmlSerializer, retryConfig, path); | ||
return new DefaultRequestBuilder(httpClient, baseUrlProvider, apiErrorConstructor, authenticationProvider, httpMethod, xmlSerializer, retryConfig, path, apiLogger); | ||
}; | ||
@@ -453,0 +495,0 @@ } |
@@ -14,4 +14,9 @@ /// <reference path="shim/index.d.ts" /> | ||
export { RequestRetryOption } from './http/retryConfiguration'; | ||
export * from './logger/apiLogger'; | ||
export * from './logger/defaultLogger'; | ||
export * from './logger/nullLogger'; | ||
export * from './logger/defaultLoggingConfiguration'; | ||
export * from './logger/loggingOptions'; | ||
export { indexedPrefix, unindexedPrefix, plainPrefix, commaPrefix, tabPrefix, pipePrefix, } from './http/queryString'; | ||
export { XmlSerializerInterface } from './xml/xmlSerializer'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -20,2 +20,7 @@ "use strict"; | ||
Object.defineProperty(exports, "RequestRetryOption", { enumerable: true, get: function () { return retryConfiguration_1.RequestRetryOption; } }); | ||
tslib_1.__exportStar(require("./logger/apiLogger"), exports); | ||
tslib_1.__exportStar(require("./logger/defaultLogger"), exports); | ||
tslib_1.__exportStar(require("./logger/nullLogger"), exports); | ||
tslib_1.__exportStar(require("./logger/defaultLoggingConfiguration"), exports); | ||
tslib_1.__exportStar(require("./logger/loggingOptions"), exports); | ||
var queryString_1 = require("./http/queryString"); | ||
@@ -22,0 +27,0 @@ Object.defineProperty(exports, "indexedPrefix", { enumerable: true, get: function () { return queryString_1.indexedPrefix; } }); |
{ | ||
"name": "@apimatic/core", | ||
"author": "APIMatic Ltd.", | ||
"version": "0.10.5", | ||
"version": "0.10.6", | ||
"license": "MIT", | ||
@@ -48,2 +48,3 @@ "sideEffects": false, | ||
"@types/lodash.flatmap": "^4.5.6", | ||
"@types/lodash.defaultsdeep": "^4.6.9", | ||
"abort-controller": "^3.0.0", | ||
@@ -74,2 +75,3 @@ "babel-plugin-annotate-pure-calls": "^0.4.0", | ||
"lodash.flatmap": "^4.5.0", | ||
"lodash.defaultsdeep": "^4.6.1", | ||
"tiny-warning": "^1.0.3", | ||
@@ -76,0 +78,0 @@ "tslib": "^2.1.0" |
@@ -16,2 +16,3 @@ import JSONBig from '@apimatic/json-bigint'; | ||
RetryConfiguration, | ||
ApiLoggerInterface, | ||
} from '../coreInterfaces'; | ||
@@ -94,2 +95,9 @@ import { ArgumentsValidationError } from '../errors/argumentsValidationError'; | ||
export interface ErrorType<ErrorCtorArgs extends any[]> { | ||
statusCode: number | [number, number]; | ||
errorConstructor: new (response: HttpContext, ...args: ErrorCtorArgs) => any; | ||
isTemplate?: boolean; | ||
args: ErrorCtorArgs; | ||
} | ||
export interface ApiErrorFactory { | ||
@@ -99,3 +107,2 @@ apiErrorCtor: ApiErrorConstructor; | ||
} | ||
export interface RequestBuilder<BaseUrlParamType, AuthParams> { | ||
@@ -211,2 +218,3 @@ deprecated(methodName: string, message?: string): void; | ||
protected _apiErrorFactory: ApiErrorFactory; | ||
protected _errorTypes: Array<ErrorType<any>>; | ||
public prepareArgs: typeof prepareArgs; | ||
@@ -222,3 +230,4 @@ | ||
protected _retryConfig: RetryConfiguration, | ||
protected _path?: string | ||
protected _path?: string, | ||
protected _apiLogger?: ApiLoggerInterface | ||
) { | ||
@@ -228,2 +237,3 @@ this._headers = {}; | ||
this._interceptors = []; | ||
this._errorTypes = []; | ||
this._validateResponse = true; | ||
@@ -234,2 +244,5 @@ this._apiErrorFactory = { apiErrorCtor: _apiErrorCtr }; | ||
this._addRetryInterceptor(); | ||
this._addErrorHandlingInterceptor(); | ||
this._addApiLoggerInterceptors(); | ||
this._retryOption = RequestRetryOption.Default; | ||
@@ -437,18 +450,3 @@ this.prepareArgs = prepareArgs.bind(this); | ||
): void { | ||
this.interceptResponse((context) => { | ||
const { response } = context; | ||
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]) | ||
) { | ||
throw new errorConstructor(context, ...args); | ||
} | ||
return context; | ||
}); | ||
this._errorTypes.push({ statusCode, errorConstructor, isTemplate, args }); | ||
} | ||
@@ -593,2 +591,16 @@ public async call( | ||
} | ||
private _addApiLoggerInterceptors(): void { | ||
if (this._apiLogger) { | ||
const apiLogger = this._apiLogger; | ||
this.intercept(async (request, options, next) => { | ||
apiLogger.logRequest(request); | ||
const context = await next(request, options); | ||
apiLogger.logResponse(context.response); | ||
return context; | ||
}); | ||
} | ||
} | ||
private _addAuthentication() { | ||
@@ -600,2 +612,3 @@ this.intercept((...args) => { | ||
} | ||
private _addRetryInterceptor() { | ||
@@ -646,2 +659,24 @@ this.intercept(async (request, options, next) => { | ||
} | ||
private _addErrorHandlingInterceptor() { | ||
this.interceptResponse((context) => { | ||
const { response } = context; | ||
for (const { statusCode, errorConstructor, isTemplate, args } of this | ||
._errorTypes) { | ||
if ( | ||
(typeof statusCode === 'number' && | ||
response.statusCode === statusCode) || | ||
(typeof statusCode !== 'number' && | ||
response.statusCode >= statusCode[0] && | ||
response.statusCode <= statusCode[1]) | ||
) { | ||
if (isTemplate && args.length > 0) { | ||
args[0] = updateErrorMessage(args[0], response); | ||
} | ||
throw new errorConstructor(context, ...args); | ||
} | ||
} | ||
return context; | ||
}); | ||
} | ||
} | ||
@@ -655,3 +690,4 @@ | ||
retryConfig: RetryConfiguration, | ||
xmlSerializer: XmlSerializerInterface = new XmlSerialization() | ||
xmlSerializer: XmlSerializerInterface = new XmlSerialization(), | ||
apiLogger?: ApiLoggerInterface | ||
): RequestBuilderFactory<BaseUrlParamType, AuthParams> { | ||
@@ -667,3 +703,4 @@ return (httpMethod, path?) => { | ||
retryConfig, | ||
path | ||
path, | ||
apiLogger | ||
); | ||
@@ -670,0 +707,0 @@ }; |
@@ -16,2 +16,7 @@ // tslint:disable-next-line:no-reference | ||
export { RequestRetryOption } from './http/retryConfiguration'; | ||
export * from './logger/apiLogger'; | ||
export * from './logger/defaultLogger'; | ||
export * from './logger/nullLogger'; | ||
export * from './logger/defaultLoggingConfiguration'; | ||
export * from './logger/loggingOptions'; | ||
export { | ||
@@ -18,0 +23,0 @@ indexedPrefix, |
193560
88
4909
15
17
+ Addedlodash.defaultsdeep@^4.6.1
+ Addedlodash.defaultsdeep@4.6.1(transitive)