From 1661269f1473641ce405680974e34c3812a83bab Mon Sep 17 00:00:00 2001 From: Gabor Soos Date: Fri, 26 Aug 2022 16:14:29 +0200 Subject: [PATCH] feat(package): remove default exports, rename package EME-5329 BREAKING CHANGE: rename package and remove default exports Co-authored-by: Gabor Nemeth --- package.json | 4 +-- src/request.spec.js | 62 +++++++++++++++++++-------------------- src/request.ts | 54 ++++++++++++++++------------------ src/requestError.spec.js | 12 ++++---- src/requestError.ts | 4 +-- src/requestOption.spec.js | 50 ++++++++++++++++++++----------- src/requestOption.ts | 22 +++++++------- src/wrapper.spec.js | 18 ++++++------ src/wrapper.ts | 16 +++++----- 9 files changed, 128 insertions(+), 114 deletions(-) diff --git a/package.json b/package.json index fc72816..26fd08c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "escher-suiteapi-js", - "description": "Escher Request", + "name": "@emartech/escher-request", + "description": "Requests with Escher authentication", "scripts": { "test": "mocha --require ts-node/register ./src/ --recursive", "lint": "eslint $(find ./src -name \"*.js\" -not -path \"./node_modules/*\")", diff --git a/src/request.spec.js b/src/request.spec.js index 791c078..8d70d68 100644 --- a/src/request.spec.js +++ b/src/request.spec.js @@ -1,10 +1,10 @@ -const SuiteRequest = require('./request'); +const { EscherRequest, EscherRequestOption } = require('./request'); const axios = require('axios'); const Escher = require('escher-auth'); const http = require('http'); const https = require('https'); -describe('SuiteRequest', function() { +describe('EscherRequest', function() { const serviceConfig = { host: 'localhost', port: 1234, @@ -23,37 +23,37 @@ describe('SuiteRequest', function() { let requestOptions; let requestStub; - let suiteRequest; + let escherRequest; beforeEach(function() { - requestOptions = new SuiteRequest.Options(serviceConfig.host, serviceConfig); + requestOptions = new EscherRequestOption(serviceConfig.host, serviceConfig); requestStub = this.sandbox.stub(axios, 'request').resolves(createDummyResponse()); - suiteRequest = SuiteRequest.create('key-id', 'secret', requestOptions); + escherRequest = EscherRequest.create('key-id', 'secret', requestOptions); }); it('should sign headers of GET request', async () => { - await suiteRequest.get('/path'); + await escherRequest.get('/path'); const requestArgument = requestStub.args[0][0]; expect(requestArgument.headers['x-ems-auth']).to.have.string('SignedHeaders=content-type;host;x-ems-date,'); }); it('should sign headers of PATCH request', async () => { - await suiteRequest.patch('/path', { name: 'Almanach' }); + await escherRequest.patch('/path', { name: 'Almanach' }); const requestArgument = requestStub.args[0][0]; expect(requestArgument.headers['x-ems-auth']).to.have.string('SignedHeaders=content-type;host;x-ems-date,'); }); it('should sign headers of POST request', async () => { - await suiteRequest.post('/path', { name: 'Almanach' }); + await escherRequest.post('/path', { name: 'Almanach' }); const requestArgument = requestStub.args[0][0]; expect(requestArgument.headers['x-ems-auth']).to.have.string('SignedHeaders=content-type;host;x-ems-date,'); }); it('should sign headers of DELETE request', async () => { - await suiteRequest.delete('/path'); + await escherRequest.delete('/path'); const requestArgument = requestStub.args[0][0]; expect(requestArgument.headers['x-ems-auth']).to.have.string('SignedHeaders=content-type;host;x-ems-date,'); @@ -62,21 +62,21 @@ describe('SuiteRequest', function() { it('should sign headers with non string values', async () => { requestOptions.setHeader(['x-customer-id', 15]); - await suiteRequest.post('/path', { name: 'Almanach' }); + await escherRequest.post('/path', { name: 'Almanach' }); const requestArgument = requestStub.args[0][0]; expect(requestArgument.headers['x-ems-auth']).to.have.string('content-type;host;x-customer-id;x-ems-date,'); }); it('should encode payload when content type is json', async () => { - await suiteRequest.post('/path', { name: 'Almanach' }); + await escherRequest.post('/path', { name: 'Almanach' }); const requestArgument = requestStub.args[0][0]; expect(requestArgument.data).to.eql('{"name":"Almanach"}'); }); it('should encode payload when content type is json and method is GET', async () => { - await suiteRequest.get('/path', { name: 'Almanach' }); + await escherRequest.get('/path', { name: 'Almanach' }); const requestArgument = requestStub.args[0][0]; expect(requestArgument.data).to.eql('{"name":"Almanach"}'); @@ -85,7 +85,7 @@ describe('SuiteRequest', function() { it('should encode payload when content type is utf8 json', async () => { requestOptions.setHeader(['content-type', 'application/json;charset=utf-8']); - await suiteRequest.post('/path', { name: 'Almanach' }); + await escherRequest.post('/path', { name: 'Almanach' }); const requestArgument = requestStub.args[0][0]; expect(requestArgument.data).to.eql('{"name":"Almanach"}'); @@ -94,7 +94,7 @@ describe('SuiteRequest', function() { it('should skip encoding of payload when content type is not json', async () => { requestOptions.setHeader(['content-type', 'text/csv']); - await suiteRequest.post('/path', 'header1;header2'); + await escherRequest.post('/path', 'header1;header2'); const requestArgument = requestStub.args[0][0]; expect(requestArgument.data).to.eql('header1;header2'); @@ -103,7 +103,7 @@ describe('SuiteRequest', function() { it('signs extra headers too', async () => { requestOptions.setHeader(['extra-header', 'header-value']); - await suiteRequest.get('/path'); + await escherRequest.get('/path'); const requestArgument = requestStub.args[0][0]; expect(requestArgument.headers['x-ems-auth']) @@ -111,7 +111,7 @@ describe('SuiteRequest', function() { }); it('should pass down parameters to request call from request options', async () => { - await suiteRequest.post('/path', { name: 'Almanach' }); + await escherRequest.post('/path', { name: 'Almanach' }); const requestArgument = requestStub.args[0][0]; @@ -128,7 +128,7 @@ describe('SuiteRequest', function() { const payload = { name: 'Test' }; this.sandbox.spy(Escher.prototype, 'signRequest'); - await suiteRequest.patch('/path', payload); + await escherRequest.patch('/path', payload); expect(Escher.prototype.signRequest.callCount).to.eql(1); const firstCall = Escher.prototype.signRequest.getCall(0); @@ -139,7 +139,7 @@ describe('SuiteRequest', function() { const payload = { name: 'Test' }; this.sandbox.spy(Escher.prototype, 'signRequest'); - await suiteRequest.post('/path', payload); + await escherRequest.post('/path', payload); expect(Escher.prototype.signRequest.callCount).to.eql(1); const firstCall = Escher.prototype.signRequest.getCall(0); @@ -150,7 +150,7 @@ describe('SuiteRequest', function() { const payload = { name: 'Test' }; this.sandbox.spy(Escher.prototype, 'signRequest'); - await suiteRequest.get('/path', payload); + await escherRequest.get('/path', payload); expect(Escher.prototype.signRequest.callCount).to.eql(1); const firstCall = Escher.prototype.signRequest.getCall(0); @@ -158,29 +158,29 @@ describe('SuiteRequest', function() { }); it('should not create http agents by default', function() { - suiteRequest = SuiteRequest.create('key-id', 'secret', requestOptions); + escherRequest = EscherRequest.create('key-id', 'secret', requestOptions); - expect(suiteRequest.httpAgent).to.be.undefined; - expect(suiteRequest.httpsAgent).to.be.undefined; + expect(escherRequest.httpAgent).to.be.undefined; + expect(escherRequest.httpsAgent).to.be.undefined; }); it('should create http agents when connection is keep alive', function() { - requestOptions = new SuiteRequest.Options(serviceConfig.host, Object.assign({ keepAlive: true }, serviceConfig)); + requestOptions = new EscherRequestOption(serviceConfig.host, Object.assign({ keepAlive: true }, serviceConfig)); - suiteRequest = SuiteRequest.create('key-id', 'secret', requestOptions); + escherRequest = EscherRequest.create('key-id', 'secret', requestOptions); - expect(suiteRequest.httpAgent).to.be.an.instanceOf(http.Agent); - expect(suiteRequest.httpsAgent).to.be.an.instanceOf(https.Agent); + expect(escherRequest.httpAgent).to.be.an.instanceOf(http.Agent); + expect(escherRequest.httpsAgent).to.be.an.instanceOf(https.Agent); }); it('should pass http agents to wrapper', async () => { - requestOptions = new SuiteRequest.Options(serviceConfig.host, Object.assign({ keepAlive: true }, serviceConfig)); - suiteRequest = SuiteRequest.create('key-id', 'secret', requestOptions); + requestOptions = new EscherRequestOption(serviceConfig.host, Object.assign({ keepAlive: true }, serviceConfig)); + escherRequest = EscherRequest.create('key-id', 'secret', requestOptions); - await suiteRequest.post('/path', { name: 'Almanach' }); + await escherRequest.post('/path', { name: 'Almanach' }); const requestArgument = requestStub.args[0][0]; - expect(requestArgument.httpAgent).to.eql(suiteRequest.httpAgent); - expect(requestArgument.httpsAgent).to.eql(suiteRequest.httpsAgent); + expect(requestArgument.httpAgent).to.eql(escherRequest.httpAgent); + expect(requestArgument.httpsAgent).to.eql(escherRequest.httpsAgent); }); }); diff --git a/src/request.ts b/src/request.ts index a561488..4b32ce4 100644 --- a/src/request.ts +++ b/src/request.ts @@ -1,15 +1,16 @@ import Escher from 'escher-auth'; import { Agent as HttpAgent } from 'http'; import { Agent as HttpsAgent } from 'https'; -import { SuiteRequestOption } from './requestOption'; -import { RequestWrapper, ExtendedRequestOption } from './wrapper'; -import { SuiteRequestError } from './requestError'; +import { EscherRequestOption } from './requestOption'; +export { EscherRequestOption } from './requestOption'; +import { RequestWrapper, ExtendedRequestOption, TransformedResponse } from './wrapper'; +export { TransformedResponse } from './wrapper'; +import { EscherRequestError } from './requestError'; +export { EscherRequestError } from './requestError'; import createLogger from '@emartech/json-logger'; const logger = createLogger('suiterequest'); -export class SuiteRequest { - static Options = SuiteRequestOption; - static Error = SuiteRequestError; +export class EscherRequest { static EscherConstants = { algoPrefix: 'EMS', vendorKey: 'EMS', @@ -18,19 +19,19 @@ export class SuiteRequest { dateHeaderName: 'X-Ems-Date' }; _escher: Escher; - _options: SuiteRequestOption; + _options: EscherRequestOption; httpAgent?: HttpAgent; httpsAgent?: HttpsAgent; - static create(accessKeyId: string, apiSecret: string, requestOptions: SuiteRequestOption) { - return new SuiteRequest(accessKeyId, apiSecret, requestOptions); + static create(accessKeyId: string, apiSecret: string, requestOptions: EscherRequestOption) { + return new EscherRequest(accessKeyId, apiSecret, requestOptions); } - constructor(accessKeyId: string, apiSecret: string, requestOptions: SuiteRequestOption) { - const escherConfig = Object.assign({}, SuiteRequest.EscherConstants, { + constructor(accessKeyId: string, apiSecret: string, requestOptions: EscherRequestOption) { + const escherConfig = Object.assign({}, EscherRequest.EscherConstants, { accessKeyId: accessKeyId, apiSecret: apiSecret, - credentialScope: requestOptions.credentialScope || SuiteRequest.EscherConstants.credentialScope + credentialScope: requestOptions.credentialScope || EscherRequest.EscherConstants.credentialScope }); this._escher = new Escher(escherConfig); @@ -42,26 +43,34 @@ export class SuiteRequest { } } - get(path: string, data: any) { + get(path: string, data: any): Promise> { return this._request('GET', path, data); } - patch(path: string, data: any) { + patch(path: string, data: any): Promise> { return this._request('PATCH', path, data); } - post(path: string, data: any) { + post(path: string, data: any): Promise> { return this._request('POST', path, data); } - put(path: string, data: any) { + put(path: string, data: any): Promise> { return this._request('PUT', path, data); } - delete(path: string) { + delete(path: string): Promise> { return this._request('DELETE', path); } + setOptions(requestOptions: EscherRequestOption): void { + this._options = requestOptions; + } + + getOptions(): EscherRequestOption { + return this._options; + } + _request(method: string, path: string, data?: any) { const options = this._getOptionsFor(method, path); const payload = data ? this._getPayload(data) : ''; @@ -71,14 +80,6 @@ export class SuiteRequest { return this._getRequestFor(signedOptions, payload).send(); } - setOptions(requestOptions: SuiteRequestOption) { - this._options = requestOptions; - } - - getOptions() { - return this._options; - } - _getRequestFor(requestOptions: ExtendedRequestOption, payload: any) { const protocol = (this._options.secure) ? 'https:' : 'http:'; return new RequestWrapper(requestOptions, protocol, payload); @@ -118,6 +119,3 @@ export class SuiteRequest { return JSON.stringify(data); } } - -module.exports = SuiteRequest; -export default SuiteRequest; diff --git a/src/requestError.spec.js b/src/requestError.spec.js index 571540d..7dc539e 100644 --- a/src/requestError.spec.js +++ b/src/requestError.spec.js @@ -1,14 +1,14 @@ -const { SuiteRequestError } = require('./requestError'); +const { EscherRequestError } = require('./requestError'); -describe('SuiteRequestError', function() { +describe('EscherRequestError', function() { it('should extend base Error class', function() { - const error = new SuiteRequestError(); + const error = new EscherRequestError('Unauthorized', 401); expect(error).to.be.an.instanceOf(Error); }); it('should store constructor parameters', function() { - const error = new SuiteRequestError('Invalid request', 400, { + const error = new EscherRequestError('Invalid request', 400, { data: { replyText: 'Too long', detailedMessage: 'Line too long' @@ -25,7 +25,7 @@ describe('SuiteRequestError', function() { }); it('should store response as is when no data attribute present', function() { - const error = new SuiteRequestError('Invalid request', 400, { + const error = new EscherRequestError('Invalid request', 400, { replyText: 'Too long', detailedMessage: 'Line too long' }); @@ -39,7 +39,7 @@ describe('SuiteRequestError', function() { }); it('should always contain data on error', function() { - const error = new SuiteRequestError('Unauthorized'); + const error = new EscherRequestError('Unauthorized', 401); expect(error.data).to.eql({ replyText: 'Unauthorized' }); }); diff --git a/src/requestError.ts b/src/requestError.ts index c5dfbc4..e5fc4dc 100644 --- a/src/requestError.ts +++ b/src/requestError.ts @@ -1,6 +1,6 @@ import { AxiosResponse } from 'axios'; -export class SuiteRequestError extends Error { +export class EscherRequestError extends Error { code: number; originalCode: string | undefined; data: any; @@ -10,7 +10,7 @@ export class SuiteRequestError extends Error { this.code = code; this.originalCode = originalCode; - this.name = 'SuiteRequestError'; + this.name = 'EscherRequestError'; if (response) { this.data = (response as AxiosResponse).data || response; diff --git a/src/requestOption.spec.js b/src/requestOption.spec.js index 87d0cfa..bf04148 100644 --- a/src/requestOption.spec.js +++ b/src/requestOption.spec.js @@ -1,7 +1,6 @@ -const { SuiteRequestOption } = require('./requestOption'); - -describe('SuiteRequestOption', function() { +const { EscherRequestOption } = require('./requestOption'); +describe('EscherRequestOption', function() { let dummyServiceConfig; beforeEach(function() { @@ -17,15 +16,23 @@ describe('SuiteRequestOption', function() { describe('create', function() { it('should populate options with parameters', function() { - const options = SuiteRequestOption.create('example-host', '/api-test', true); + const options = EscherRequestOption.create('example-host', '/api-test', true); expect(options.rejectUnauthorized).to.eql(true); expect(options.host).to.eql('example-host'); expect(options.prefix).to.eql('/api-test'); }); + it('should populate options with default parameters', function() { + const options = EscherRequestOption.create('example-host'); + + expect(options.rejectUnauthorized).to.eql(true); + expect(options.host).to.eql('example-host'); + expect(options.prefix).to.eql(''); + }); + it('should populate options with for internal api', function() { - const options = SuiteRequestOption.createForInternalApi('example-host', true); + const options = EscherRequestOption.createForInternalApi('example-host', true); expect(options.rejectUnauthorized).to.eql(true); expect(options.host).to.eql('example-host'); @@ -33,7 +40,7 @@ describe('SuiteRequestOption', function() { }); it('should populate options with for services api', function() { - const options = SuiteRequestOption.createForServiceApi('example-host', true); + const options = EscherRequestOption.createForServiceApi('example-host', true); expect(options.rejectUnauthorized).to.eql(true); expect(options.host).to.eql('example-host'); @@ -45,7 +52,7 @@ describe('SuiteRequestOption', function() { it('can accept additional headers', function() { const dummyHeader = ['header-name', 'header-value']; - const requestOptions = new SuiteRequestOption(dummyServiceConfig.host, dummyServiceConfig); + const requestOptions = new EscherRequestOption(dummyServiceConfig.host, dummyServiceConfig); requestOptions.setHeader(dummyHeader); @@ -53,14 +60,14 @@ describe('SuiteRequestOption', function() { }); it('should add default content type', function() { - const requestOptions = new SuiteRequestOption(dummyServiceConfig.host, dummyServiceConfig); + const requestOptions = new EscherRequestOption(dummyServiceConfig.host, dummyServiceConfig); expect(requestOptions.getHeader('content-type')).to.eql('application/json'); }); it('should not duplicate headers with same name', function() { const expectedContentTypeHeader = ['content-type', 'text/csv']; - const requestOptions = new SuiteRequestOption(dummyServiceConfig.host, dummyServiceConfig); + const requestOptions = new EscherRequestOption(dummyServiceConfig.host, dummyServiceConfig); requestOptions.setHeader(expectedContentTypeHeader); @@ -71,14 +78,14 @@ describe('SuiteRequestOption', function() { describe('allowEmptyResponse', function() { it('should be set to false by default', function() { - const requestOptions = new SuiteRequestOption(dummyServiceConfig.host, dummyServiceConfig); + const requestOptions = new EscherRequestOption(dummyServiceConfig.host, dummyServiceConfig); expect(requestOptions.allowEmptyResponse).to.eql(false); }); it('should be set to the value provided in config', function() { dummyServiceConfig.allowEmptyResponse = true; - const requestOptions = new SuiteRequestOption(dummyServiceConfig.host, dummyServiceConfig); + const requestOptions = new EscherRequestOption(dummyServiceConfig.host, dummyServiceConfig); expect(requestOptions.allowEmptyResponse).to.eql(true); }); @@ -86,7 +93,7 @@ describe('SuiteRequestOption', function() { describe('timeout', function() { it('should return a default value', function() { - const requestOptions = new SuiteRequestOption(dummyServiceConfig.host, dummyServiceConfig); + const requestOptions = new EscherRequestOption(dummyServiceConfig.host, dummyServiceConfig); expect(requestOptions.getTimeout()).to.be.eql(15000); }); @@ -94,13 +101,13 @@ describe('SuiteRequestOption', function() { it('should return the timeout passed in the constructor', function() { const options = Object.assign({}, dummyServiceConfig); options.timeout = 0; - const requestOptions = new SuiteRequestOption(dummyServiceConfig.host, options); + const requestOptions = new EscherRequestOption(dummyServiceConfig.host, options); expect(requestOptions.getTimeout()).to.be.eql(0); }); it('should return the timeout set by setTimeout', function() { - const requestOptions = new SuiteRequestOption(dummyServiceConfig.host, dummyServiceConfig); + const requestOptions = new EscherRequestOption(dummyServiceConfig.host, dummyServiceConfig); requestOptions.setTimeout(60000); @@ -112,7 +119,7 @@ describe('SuiteRequestOption', function() { describe('toHash', function() { it('should return the proper object', function() { - const requestOptions = new SuiteRequestOption(dummyServiceConfig.host, dummyServiceConfig); + const requestOptions = new EscherRequestOption(dummyServiceConfig.host, dummyServiceConfig); expect(requestOptions.toHash()).to.be.eql({ headers: [ @@ -131,16 +138,25 @@ describe('SuiteRequestOption', function() { it('should add allowEmptyResponse to hash if set to TRUE', function() { dummyServiceConfig.allowEmptyResponse = true; - const requestOptions = new SuiteRequestOption(dummyServiceConfig.host, dummyServiceConfig); + const requestOptions = new EscherRequestOption(dummyServiceConfig.host, dummyServiceConfig); expect(requestOptions.toHash()).to.have.property('allowEmptyResponse', true); }); it('should not cache headers', function() { - const requestOptions = new SuiteRequestOption(dummyServiceConfig.host, dummyServiceConfig); + const requestOptions = new EscherRequestOption(dummyServiceConfig.host, dummyServiceConfig); requestOptions.toHash().headers.push('from_test'); expect(requestOptions.toHash().headers).not.to.include('from_test'); }); }); + describe('setHost', () => { + it('should set host', () => { + const requestOptions = new EscherRequestOption(dummyServiceConfig.host, dummyServiceConfig); + + requestOptions.setHost('suitedocker.ett.local'); + + expect(requestOptions.host).to.eql('suitedocker.ett.local'); + }) + }); }); diff --git a/src/requestOption.ts b/src/requestOption.ts index 86b9443..7b856bc 100644 --- a/src/requestOption.ts +++ b/src/requestOption.ts @@ -11,10 +11,10 @@ export interface RequestOptions { allowEmptyResponse?: boolean; maxContentLength?: number; keepAlive?: boolean; - environment?: string; + credentialScope?: string; } -export class SuiteRequestOption { +export class EscherRequestOption implements RequestOptions { secure = true; port = 443; host = ''; @@ -27,26 +27,26 @@ export class SuiteRequestOption { keepAlive = false; credentialScope = ''; - static createForInternalApi(environment: string, rejectUnauthorized: boolean) { - return this.create(environment, '/api/v2/internal', rejectUnauthorized); + static createForInternalApi(host: string | RequestOptions, rejectUnauthorized: boolean) { + return this.create(host, '/api/v2/internal', rejectUnauthorized); } - static createForServiceApi(environment: string, rejectUnauthorized: boolean) { - return this.create(environment, '/api/services', rejectUnauthorized); + static createForServiceApi(host: string | RequestOptions, rejectUnauthorized: boolean) { + return this.create(host, '/api/services', rejectUnauthorized); } - static create(host: string, prefix: string, rejectUnauthorized: boolean) { + static create(host: string | RequestOptions, prefix = '', rejectUnauthorized = true) { let options: RequestOptions = {}; if (typeof host === 'object') { options = host; - host = options.environment || ''; + host = options.host || ''; } else { options.rejectUnauthorized = rejectUnauthorized; } options.prefix = prefix; - return new SuiteRequestOption(host, options); + return new EscherRequestOption(host, options); } constructor(host: string, options: RequestOptions) { @@ -79,8 +79,8 @@ export class SuiteRequestOption { this.secure = false; } - setEnvironment(environment: string) { - this.host = environment; + setHost(host: string) { + this.host = host; } setPort(port: number) { diff --git a/src/wrapper.spec.js b/src/wrapper.spec.js index f3bd5d8..b164e0c 100644 --- a/src/wrapper.spec.js +++ b/src/wrapper.spec.js @@ -1,11 +1,11 @@ const axios = require('axios'); const { RequestWrapper } = require('./wrapper'); -const { SuiteRequestError } = require('./requestError'); -const { SuiteRequestOption } = require('./requestOption'); +const { EscherRequestError } = require('./requestError'); +const { EscherRequestOption } = require('./requestOption'); const http = require('http'); const https = require('https'); -describe('Wrapper', function() { +describe('RequestWrapper', function() { let apiResponse; let expectedApiResponse; let escherRequestOptions; @@ -26,7 +26,7 @@ describe('Wrapper', function() { statusMessage: 'OK' }; - escherRequestOptions = new SuiteRequestOption('very.host.io', { + escherRequestOptions = new EscherRequestOption('very.host.io', { port: 443, headers: [ ['content-type', 'very-format'], @@ -102,7 +102,7 @@ describe('Wrapper', function() { await wrapper.send(); throw new Error('Error should have been thrown'); } catch (err) { - expect(err).to.be.an.instanceof(SuiteRequestError); + expect(err).to.be.an.instanceof(EscherRequestError); expect(err.message).to.eql('Error in http response (status: 400)'); expect(err.code).to.eql(400); expect(err.data).to.eql({ replyText: 'Unknown route' }); @@ -133,7 +133,7 @@ describe('Wrapper', function() { try { await wrapper.send(); } catch (err) { - expect(err).to.be.an.instanceof(SuiteRequestError); + expect(err).to.be.an.instanceof(EscherRequestError); expect(err.message).to.match(/Unexpected end/); expect(err.code).to.eql(500); return; @@ -149,7 +149,7 @@ describe('Wrapper', function() { try { await wrapper.send(); } catch (err) { - expect(err).to.be.an.instanceof(SuiteRequestError); + expect(err).to.be.an.instanceof(EscherRequestError); expect(err.message).to.eql('Empty http response'); expect(err.code).to.eql(500); expect(err.data).to.eql(expectedApiResponse.statusMessage); @@ -180,7 +180,7 @@ describe('Wrapper', function() { await wrapper.send(); throw new Error('should throw'); } catch (err) { - expect(err).to.be.an.instanceOf(SuiteRequestError); + expect(err).to.be.an.instanceOf(EscherRequestError); expect(err.code).to.eql(apiResponse.status); expect(err.message).to.eql('Error in http response (status: 404)'); expect(err.data).to.eql(apiResponse.data); @@ -252,7 +252,7 @@ describe('Wrapper', function() { try { await wrapper.send(); } catch (err) { - expect(err).to.be.an.instanceof(SuiteRequestError); + expect(err).to.be.an.instanceof(EscherRequestError); expect(err.message).to.match(/Unexpected token/); expect(err.code).to.eql(500); return; diff --git a/src/wrapper.ts b/src/wrapper.ts index 7a30b36..d6a726d 100644 --- a/src/wrapper.ts +++ b/src/wrapper.ts @@ -1,4 +1,4 @@ -import { SuiteRequestError } from './requestError'; +import { EscherRequestError } from './requestError'; import { RequestOptions } from './requestOption'; import { AxiosError, AxiosRequestConfig, AxiosResponse, AxiosResponseHeaders, CancelTokenSource } from 'axios'; import { Agent as HttpAgent } from 'http'; @@ -16,7 +16,7 @@ export interface ExtendedRequestOption extends RequestOptions { httpsAgent?: HttpsAgent; } -interface TransformedResponse { +export interface TransformedResponse { body: T, statusCode: number; statusMessage: string; @@ -39,7 +39,7 @@ export class RequestWrapper { }); } - send() { + send(): Promise> { const timer = logger.timer(); const method = this.requestOptions.method.toLowerCase(); @@ -95,16 +95,16 @@ export class RequestWrapper { const recoverableErrorCodes = ['ECONNRESET', 'ETIMEDOUT', 'ECONNREFUSED', 'ECONNABORTED']; const code = recoverableErrorCodes.includes(error.code || '') ? 503 : 500; - throw new SuiteRequestError(error.message, code, undefined, error.code); + throw new EscherRequestError(error.message, code, undefined, error.code); } - _handleResponse(response: TransformedResponse) { + _handleResponse(response: TransformedResponse): TransformedResponse { if (response.statusCode >= 400) { logger.error('server_error', this._getLogParameters({ code: response.statusCode, reply_text: response.body.replyText })); - throw new SuiteRequestError( + throw new EscherRequestError( 'Error in http response (status: ' + response.statusCode + ')', response.statusCode, this._parseBody(response) @@ -113,7 +113,7 @@ export class RequestWrapper { if (!this.requestOptions.allowEmptyResponse && !response.body) { logger.error('server_error empty response data', this._getLogParameters()); - throw new SuiteRequestError('Empty http response', 500, response.statusMessage); + throw new EscherRequestError('Empty http response', 500, response.statusMessage); } return { @@ -167,7 +167,7 @@ export class RequestWrapper { return JSON.parse(response.body); } catch (ex) { logger.fromError('fatal_error', ex, this._getLogParameters()); - throw new SuiteRequestError((ex as Error).message, 500); + throw new EscherRequestError((ex as Error).message, 500); } } }