@apimatic/core
Advanced tools
Comparing version 0.10.11 to 0.10.12
@@ -6,3 +6,3 @@ import { __spreadArray, __read, __assign, __awaiter, __generator, __values } from 'tslib'; | ||
import { ResponseValidationError } from '../errors/responseValidationError.js'; | ||
import { validateAndUnmapXml, validateAndMap, validateAndMapXml } from '@apimatic/schema'; | ||
import { validateAndUnmapXml, validateAndMapXml, validateAndMap } from '@apimatic/schema'; | ||
import { JSON_CONTENT_TYPE, setHeader, mergeHeaders, TEXT_CONTENT_TYPE, XML_CONTENT_TYPE, ACCEPT_HEADER, CONTENT_TYPE_HEADER, CONTENT_LENGTH_HEADER, setHeaderIfNotSet } from '@apimatic/http-headers'; | ||
@@ -105,3 +105,4 @@ import { callHttpInterceptors } from './httpInterceptor.js'; | ||
DefaultRequestBuilder.prototype.text = function (body) { | ||
this._body = body; | ||
var _a; | ||
this._body = (_a = body === null || body === void 0 ? void 0 : body.toString()) !== null && _a !== void 0 ? _a : undefined; | ||
this._setContentTypeIfNotSet(TEXT_CONTENT_TYPE); | ||
@@ -321,3 +322,3 @@ }; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result, parsed, mappingResult; | ||
var result; | ||
return __generator(this, function (_a) { | ||
@@ -336,20 +337,4 @@ switch (_a.label) { | ||
result = _a.sent(); | ||
if (typeof result.body !== 'string') { | ||
throw new Error('Could not parse body as JSON. The response body is not a string.'); | ||
} | ||
if (result.body.trim() === '') { | ||
// Try mapping the missing body as null | ||
return [2 /*return*/, this.tryMappingAsNull(schema, result)]; | ||
} | ||
try { | ||
parsed = JSON.parse(result.body); | ||
} catch (error) { | ||
throw new Error("Could not parse body as JSON.\n\n" + error.message); | ||
} | ||
mappingResult = validateAndMap(parsed, schema); | ||
if (mappingResult.errors) { | ||
throw new ResponseValidationError(result, mappingResult.errors); | ||
} | ||
return [2 /*return*/, __assign(__assign({}, result), { | ||
result: mappingResult.result | ||
result: parseJsonResult(schema, result) | ||
})]; | ||
@@ -360,11 +345,2 @@ } | ||
}; | ||
DefaultRequestBuilder.prototype.tryMappingAsNull = function (schema, result) { | ||
var nullMappingResult = validateAndMap(null, schema); | ||
if (nullMappingResult.errors) { | ||
throw new Error('Could not parse body as JSON. The response body is empty.'); | ||
} | ||
return __assign(__assign({}, result), { | ||
result: nullMappingResult.result | ||
}); | ||
}; | ||
DefaultRequestBuilder.prototype.callAsXml = function (rootName, schema, requestOptions) { | ||
@@ -572,10 +548,41 @@ return __awaiter(this, void 0, void 0, function () { | ||
} | ||
if (left[left.length - 1] === '/' && right[0] === '/') { | ||
return left + right.substr(1); | ||
} else if (left[left.length - 1] === '/' || right[0] === '/') { | ||
return left + right; | ||
} else { | ||
return left + "/" + right; | ||
// remove all occurances of `/` (if any) from the end of left path | ||
left = left.replace('/', ' ').trimEnd().replace(' ', '/'); | ||
// remove all occurances of `/` (if any) from the start of right sub-path | ||
right = right.replace('/', ' ').trimStart().replace(' ', '/'); | ||
return left + "/" + right; | ||
} | ||
function parseJsonResult(schema, res) { | ||
if (typeof res.body !== 'string') { | ||
throw new Error('Could not parse body as JSON. The response body is not a string.'); | ||
} | ||
if (res.body.trim() === '') { | ||
var resEmptyErr_1 = new Error('Could not parse body as JSON. The response body is empty.'); | ||
return validateJson(schema, null, function (_) { | ||
return resEmptyErr_1; | ||
}); | ||
} | ||
var parsed; | ||
try { | ||
parsed = JSON.parse(res.body); | ||
} catch (error) { | ||
var resUnParseErr_1 = new Error("Could not parse body as JSON.\n\n" + error.message); | ||
return validateJson(schema, res.body, function (_) { | ||
return resUnParseErr_1; | ||
}); | ||
} | ||
var resInvalidErr = function (errors) { | ||
return new ResponseValidationError(res, errors); | ||
}; | ||
return validateJson(schema, parsed, function (errors) { | ||
return resInvalidErr(errors); | ||
}); | ||
} | ||
function validateJson(schema, value, errorCreater) { | ||
var mappingResult = validateAndMap(value, schema); | ||
if (mappingResult.errors) { | ||
throw errorCreater(mappingResult.errors); | ||
} | ||
return mappingResult.result; | ||
} | ||
export { DefaultRequestBuilder, createRequestBuilderFactory, skipEncode }; |
/// <reference types="node" /> | ||
import { FileWrapper } from '@apimatic/file-wrapper'; | ||
import { ApiResponse, AuthenticatorInterface, HttpContext, HttpMethod, HttpRequest, HttpRequestMultipartFormBody, HttpRequestUrlEncodedFormBody, HttpResponse, HttpInterceptorInterface, RequestOptions, RetryConfiguration, ApiLoggerInterface } from '../coreInterfaces'; | ||
import { ApiResponse, AuthenticatorInterface, HttpContext, HttpMethod, HttpRequest, HttpRequestMultipartFormBody, HttpRequestUrlEncodedFormBody, HttpInterceptorInterface, RequestOptions, RetryConfiguration, ApiLoggerInterface, HttpClientInterface } from '../coreInterfaces'; | ||
import { Schema } from '../schema'; | ||
@@ -13,3 +13,2 @@ import { PathTemplatePrimitiveTypes, PathTemplateTypes, SkipEncode } from './pathTemplate'; | ||
export declare function skipEncode<T extends PathTemplatePrimitiveTypes>(value: T): SkipEncode<T>; | ||
export declare type HttpClientInterface = (request: HttpRequest, requestOptions?: RequestOptions) => Promise<HttpResponse>; | ||
export declare type ApiErrorConstructor = new (response: HttpContext, message: string) => any; | ||
@@ -39,7 +38,7 @@ export interface ErrorType<ErrorCtorArgs extends any[]> { | ||
headers(headersToMerge: Record<string, string>): void; | ||
query(name: string, value: QueryValue, prefixFormat?: ArrayPrefixFunction): void; | ||
query(name: string, value: QueryValue | Record<string, QueryValue>, prefixFormat?: ArrayPrefixFunction): void; | ||
query(parameters?: Record<string, QueryValue> | null, prefixFormat?: ArrayPrefixFunction): void; | ||
form(parameters: Record<string, unknown>, prefixFormat?: ArrayPrefixFunction): void; | ||
formData(parameters: Record<string, unknown>, prefixFormat?: ArrayPrefixFunction): void; | ||
text(body: string): void; | ||
text(body: string | number | bigint | boolean | null | undefined): void; | ||
json(data: unknown): void; | ||
@@ -104,5 +103,5 @@ requestRetryOption(option: RequestRetryOption): void; | ||
headers(headersToMerge: Record<string, string>): void; | ||
query(name: string, value: QueryValue, prefixFormat?: ArrayPrefixFunction): void; | ||
query(name: string, value: QueryValue | Record<string, QueryValue>, prefixFormat?: ArrayPrefixFunction): void; | ||
query(parameters?: Record<string, QueryValue> | null, prefixFormat?: ArrayPrefixFunction): void; | ||
text(body: string): void; | ||
text(body: string | number | bigint | boolean | null | undefined): void; | ||
json(data: unknown): void; | ||
@@ -125,3 +124,2 @@ xml<T>(argName: string, data: T, rootName: string, schema: Schema<T, any>): void; | ||
callAsJson<T>(schema: Schema<T>, requestOptions?: RequestOptions): Promise<ApiResponse<T>>; | ||
private tryMappingAsNull; | ||
callAsXml<T>(rootName: string, schema: Schema<T, any>, requestOptions?: RequestOptions): Promise<ApiResponse<T>>; | ||
@@ -128,0 +126,0 @@ private _setContentTypeIfNotSet; |
@@ -107,3 +107,4 @@ "use strict"; | ||
DefaultRequestBuilder.prototype.text = function (body) { | ||
this._body = body; | ||
var _a; | ||
this._body = (_a = body === null || body === void 0 ? void 0 : body.toString()) !== null && _a !== void 0 ? _a : undefined; | ||
this._setContentTypeIfNotSet(httpHeaders_1.TEXT_CONTENT_TYPE); | ||
@@ -275,3 +276,3 @@ }; | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var result, parsed, mappingResult; | ||
var result; | ||
return tslib_1.__generator(this, function (_a) { | ||
@@ -288,20 +289,3 @@ switch (_a.label) { | ||
result = _a.sent(); | ||
if (typeof result.body !== 'string') { | ||
throw new Error('Could not parse body as JSON. The response body is not a string.'); | ||
} | ||
if (result.body.trim() === '') { | ||
// Try mapping the missing body as null | ||
return [2 /*return*/, this.tryMappingAsNull(schema, result)]; | ||
} | ||
try { | ||
parsed = JSON.parse(result.body); | ||
} | ||
catch (error) { | ||
throw new Error("Could not parse body as JSON.\n\n" + error.message); | ||
} | ||
mappingResult = schema_1.validateAndMap(parsed, schema); | ||
if (mappingResult.errors) { | ||
throw new responseValidationError_1.ResponseValidationError(result, mappingResult.errors); | ||
} | ||
return [2 /*return*/, tslib_1.__assign(tslib_1.__assign({}, result), { result: mappingResult.result })]; | ||
return [2 /*return*/, tslib_1.__assign(tslib_1.__assign({}, result), { result: parseJsonResult(schema, result) })]; | ||
} | ||
@@ -311,9 +295,2 @@ }); | ||
}; | ||
DefaultRequestBuilder.prototype.tryMappingAsNull = function (schema, result) { | ||
var nullMappingResult = schema_1.validateAndMap(null, schema); | ||
if (nullMappingResult.errors) { | ||
throw new Error('Could not parse body as JSON. The response body is empty.'); | ||
} | ||
return tslib_1.__assign(tslib_1.__assign({}, result), { result: nullMappingResult.result }); | ||
}; | ||
DefaultRequestBuilder.prototype.callAsXml = function (rootName, schema, requestOptions) { | ||
@@ -507,11 +484,35 @@ return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
} | ||
if (left[left.length - 1] === '/' && right[0] === '/') { | ||
return left + right.substr(1); | ||
// remove all occurances of `/` (if any) from the end of left path | ||
left = left.replace('/', ' ').trimEnd().replace(' ', '/'); | ||
// remove all occurances of `/` (if any) from the start of right sub-path | ||
right = right.replace('/', ' ').trimStart().replace(' ', '/'); | ||
return left + "/" + right; | ||
} | ||
function parseJsonResult(schema, res) { | ||
if (typeof res.body !== 'string') { | ||
throw new Error('Could not parse body as JSON. The response body is not a string.'); | ||
} | ||
else if (left[left.length - 1] === '/' || right[0] === '/') { | ||
return left + right; | ||
if (res.body.trim() === '') { | ||
var resEmptyErr_1 = new Error('Could not parse body as JSON. The response body is empty.'); | ||
return validateJson(schema, null, function (_) { return resEmptyErr_1; }); | ||
} | ||
else { | ||
return left + "/" + right; | ||
var parsed; | ||
try { | ||
parsed = JSON.parse(res.body); | ||
} | ||
catch (error) { | ||
var resUnParseErr_1 = new Error("Could not parse body as JSON.\n\n" + error.message); | ||
return validateJson(schema, res.body, function (_) { return resUnParseErr_1; }); | ||
} | ||
var resInvalidErr = function (errors) { | ||
return new responseValidationError_1.ResponseValidationError(res, errors); | ||
}; | ||
return validateJson(schema, parsed, function (errors) { return resInvalidErr(errors); }); | ||
} | ||
function validateJson(schema, value, errorCreater) { | ||
var mappingResult = schema_1.validateAndMap(value, schema); | ||
if (mappingResult.errors) { | ||
throw errorCreater(mappingResult.errors); | ||
} | ||
return mappingResult.result; | ||
} |
{ | ||
"name": "@apimatic/core", | ||
"author": "APIMatic Ltd.", | ||
"version": "0.10.11", | ||
"version": "0.10.12", | ||
"license": "MIT", | ||
@@ -63,3 +63,3 @@ "sideEffects": false, | ||
"@apimatic/convert-to-stream": "^0.0.2", | ||
"@apimatic/core-interfaces": "^0.2.5", | ||
"@apimatic/core-interfaces": "^0.2.6", | ||
"@apimatic/file-wrapper": "^0.3.2", | ||
@@ -69,3 +69,3 @@ "@apimatic/http-headers": "^0.3.2", | ||
"@apimatic/json-bigint": "^1.2.0", | ||
"@apimatic/schema": "^0.7.11", | ||
"@apimatic/schema": "^0.7.12", | ||
"detect-browser": "^5.3.0", | ||
@@ -88,3 +88,3 @@ "detect-node": "^2.0.4", | ||
}, | ||
"gitHead": "42e868dae6a8254c16463c5f0f40de55cba4466b" | ||
"gitHead": "96d47bc2390aefc3119b26012053cb630de7b796" | ||
} |
@@ -12,3 +12,2 @@ import JSONBig from '@apimatic/json-bigint'; | ||
HttpRequestUrlEncodedFormBody, | ||
HttpResponse, | ||
HttpInterceptorInterface, | ||
@@ -18,2 +17,3 @@ RequestOptions, | ||
ApiLoggerInterface, | ||
HttpClientInterface, | ||
} from '../coreInterfaces'; | ||
@@ -24,2 +24,3 @@ import { ArgumentsValidationError } from '../errors/argumentsValidationError'; | ||
Schema, | ||
SchemaValidationError, | ||
validateAndMap, | ||
@@ -87,7 +88,2 @@ validateAndMapXml, | ||
export type HttpClientInterface = ( | ||
request: HttpRequest, | ||
requestOptions?: RequestOptions | ||
) => Promise<HttpResponse>; | ||
export type ApiErrorConstructor = new ( | ||
@@ -127,3 +123,3 @@ response: HttpContext, | ||
name: string, | ||
value: QueryValue, | ||
value: QueryValue | Record<string, QueryValue>, | ||
prefixFormat?: ArrayPrefixFunction | ||
@@ -143,3 +139,3 @@ ): void; | ||
): void; | ||
text(body: string): void; | ||
text(body: string | number | bigint | boolean | null | undefined): void; | ||
json(data: unknown): void; | ||
@@ -298,3 +294,3 @@ requestRetryOption(option: RequestRetryOption): void; | ||
name: string, | ||
value: QueryValue, | ||
value: QueryValue | Record<string, QueryValue>, | ||
prefixFormat?: ArrayPrefixFunction | ||
@@ -327,4 +323,6 @@ ): void; | ||
} | ||
public text(body: string): void { | ||
this._body = body; | ||
public text( | ||
body: string | number | bigint | boolean | null | undefined | ||
): void { | ||
this._body = body?.toString() ?? undefined; | ||
this._setContentTypeIfNotSet(TEXT_CONTENT_TYPE); | ||
@@ -510,37 +508,5 @@ } | ||
const result = await this.call(requestOptions); | ||
if (typeof result.body !== 'string') { | ||
throw new Error( | ||
'Could not parse body as JSON. The response body is not a string.' | ||
); | ||
} | ||
if (result.body.trim() === '') { | ||
// Try mapping the missing body as null | ||
return this.tryMappingAsNull<T>(schema, result); | ||
} | ||
let parsed: unknown; | ||
try { | ||
parsed = JSON.parse(result.body); | ||
} catch (error) { | ||
throw new Error(`Could not parse body as JSON.\n\n${error.message}`); | ||
} | ||
const mappingResult = validateAndMap(parsed, schema); | ||
if (mappingResult.errors) { | ||
throw new ResponseValidationError(result, mappingResult.errors); | ||
} | ||
return { ...result, result: mappingResult.result }; | ||
} | ||
private tryMappingAsNull<T>( | ||
schema: Schema<T, any>, | ||
result: ApiResponse<void> | ||
) { | ||
const nullMappingResult = validateAndMap(null, schema); | ||
if (nullMappingResult.errors) { | ||
throw new Error( | ||
'Could not parse body as JSON. The response body is empty.' | ||
); | ||
} | ||
return { ...result, result: nullMappingResult.result }; | ||
return { ...result, result: parseJsonResult(schema, result) }; | ||
} | ||
public async callAsXml<T>( | ||
@@ -605,3 +571,2 @@ rootName: string, | ||
} | ||
private _addApiLoggerInterceptors(): void { | ||
@@ -619,3 +584,2 @@ if (this._apiLogger) { | ||
} | ||
private _addAuthentication() { | ||
@@ -627,3 +591,2 @@ this.intercept((...args) => { | ||
} | ||
private _addRetryInterceptor() { | ||
@@ -674,3 +637,2 @@ this.intercept(async (request, options, next) => { | ||
} | ||
private _addErrorHandlingInterceptor() { | ||
@@ -727,10 +689,46 @@ this.interceptResponse((context) => { | ||
} | ||
// remove all occurances of `/` (if any) from the end of left path | ||
left = left.replace('/', ' ').trimEnd().replace(' ', '/'); | ||
// remove all occurances of `/` (if any) from the start of right sub-path | ||
right = right.replace('/', ' ').trimStart().replace(' ', '/'); | ||
if (left[left.length - 1] === '/' && right[0] === '/') { | ||
return left + right.substr(1); | ||
} else if (left[left.length - 1] === '/' || right[0] === '/') { | ||
return left + right; | ||
} else { | ||
return `${left}/${right}`; | ||
return `${left}/${right}`; | ||
} | ||
function parseJsonResult<T>(schema: Schema<T, any>, res: ApiResponse<void>): T { | ||
if (typeof res.body !== 'string') { | ||
throw new Error( | ||
'Could not parse body as JSON. The response body is not a string.' | ||
); | ||
} | ||
if (res.body.trim() === '') { | ||
const resEmptyErr = new Error( | ||
'Could not parse body as JSON. The response body is empty.' | ||
); | ||
return validateJson(schema, null, (_) => resEmptyErr); | ||
} | ||
let parsed: unknown; | ||
try { | ||
parsed = JSON.parse(res.body); | ||
} catch (error) { | ||
const resUnParseErr = new Error( | ||
`Could not parse body as JSON.\n\n${error.message}` | ||
); | ||
return validateJson(schema, res.body, (_) => resUnParseErr); | ||
} | ||
const resInvalidErr = (errors: SchemaValidationError[]) => | ||
new ResponseValidationError(res, errors); | ||
return validateJson(schema, parsed, (errors) => resInvalidErr(errors)); | ||
} | ||
function validateJson<T>( | ||
schema: Schema<T, any>, | ||
value: any, | ||
errorCreater: (errors: SchemaValidationError[]) => Error | ||
): T { | ||
const mappingResult = validateAndMap(value, schema); | ||
if (mappingResult.errors) { | ||
throw errorCreater(mappingResult.errors); | ||
} | ||
return mappingResult.result; | ||
} |
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
194486
4948
Updated@apimatic/schema@^0.7.12