Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const request = require('./lib/request');
const WSO2Auth = require('./lib/WSO2Auth');
const randomPhrase = require('./lib/randomphrase');

const { MojaloopRequests, ThirdpartyRequests } = requests;
const { MojaloopRequests, ThirdpartyRequests, common } = requests;

module.exports = {
Errors,
Expand All @@ -29,6 +29,7 @@ module.exports = {
Logger,
MojaloopRequests,
ThirdpartyRequests,
common,
request,
requests,
WSO2Auth,
Expand Down
20 changes: 19 additions & 1 deletion src/lib/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
const ApiType = Object.freeze({
FSPIOP: 'fspiop',
ISO20022: 'iso20022',
});

const RESOURCES = Object.freeze({
fxQuotes: 'fxQuotes',
fxTransfers: 'fxTransfers'
Expand All @@ -19,10 +24,23 @@ const ILP_ADDRESS = 'g.mojaloop';

const ILP_AMOUNT_FOR_FX = '0';

const ISO_20022_HEADER_PART = 'iso20022';

const ONLY_FSPIOP_RESOURCES = [
'authorizations',
// 'bulkQuotes',
// 'bulkTransfers',
'transactionRequests',
];
// todo: think about bulkQuotes/bulkTransfers

module.exports = {
ApiType,
RESOURCES,
ERROR_MESSAGES,
ILP_VERSIONS,
ILP_ADDRESS,
ILP_AMOUNT_FOR_FX
ILP_AMOUNT_FOR_FX,
ISO_20022_HEADER_PART,
ONLY_FSPIOP_RESOURCES
};
8 changes: 2 additions & 6 deletions src/lib/requests/apiTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@
*************************************************************************/

const { TransformFacades } = require('@mojaloop/ml-schema-transformer-lib');

const ApiType = {
FSPIOP: 'fspiop',
ISO20022: 'iso20022',
};
const { ApiType } = require('../constants');

/*
Performs translation between message body formats on request bodies IF NEEDED.
Expand All @@ -29,7 +25,7 @@ class ApiTransformer {
this._logger = conf.logger;
this._apiType = conf.apiType;

if(!['fspiop', 'iso20022'].includes(this._apiType)) {
if(!Object.values(ApiType).includes(this._apiType)) {
throw new Error(`Unsupported apiType: ${this._apiType}`);
}
}
Expand Down
26 changes: 15 additions & 11 deletions src/lib/requests/baseRequests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ const safeStringify = require('fast-safe-stringify');
const http = require('http');
const https = require('https');

const { RESOURCES } = require('../constants');
const { RESOURCES, ISO_20022_HEADER_PART } = require('../constants');
const {
bodyStringifier,
buildUrl,
defineApiType,
formatEndpointOrDefault,
ResponseType,
throwOrJson,
Expand Down Expand Up @@ -270,10 +271,13 @@ class BaseRequests {
};

// transform the request. This will only change the request if translation is required i.e. if this.apiType is not 'fspiop'
const transformed = await this._apiTransformer.transformOutboundRequest(resourceType, reqOpts.method,
{ body: reqOpts.body, headers: reqOpts.headers, params: transformParams, isError: transformParams.isError,
$context: transformParams.$context
});
const transformed = await this._apiTransformer.transformOutboundRequest(resourceType, reqOpts.method, {
body: reqOpts.body,
headers: reqOpts.headers,
params: transformParams,
isError: transformParams.isError,
$context: transformParams.$context
});
reqOpts.body = transformed.body;
reqOpts.headers = { ...reqOpts.headers, ...transformed.headers };

Expand Down Expand Up @@ -394,13 +398,13 @@ class BaseRequests {
* @returns {*} headers object for use in requests to mojaloop api endpoints
*/
_buildHeaders(method, resourceType, dest) {
let isoInsert = '';
if(this.apiType === ApiType.ISO20022) {
isoInsert = '.iso20022';
}
const apiType = defineApiType(resourceType, this.apiType);
const isoPart = apiType === ApiType.ISO20022
? `.${ISO_20022_HEADER_PART}`
: '';

let headers = {
'content-type': `application/vnd.interoperability${isoInsert}.${resourceType}+json;version=${this.resourceVersions[resourceType].contentVersion}`,
'content-type': `application/vnd.interoperability${isoPart}.${resourceType}+json;version=${this.resourceVersions[resourceType].contentVersion}`,
'date': new Date().toUTCString(),
};

Expand All @@ -423,7 +427,7 @@ class BaseRequests {
// dont add accept header to PUT requests
if(method.toUpperCase() !== 'PUT') {
// if we are sending ISO we should "accept" it also
headers['accept'] = `application/vnd.interoperability${isoInsert}.${resourceType}+json;version=${this.resourceVersions[resourceType].acceptVersion}`;
headers['accept'] = `application/vnd.interoperability${isoPart}.${resourceType}+json;version=${this.resourceVersions[resourceType].acceptVersion}`;
}

return headers;
Expand Down
10 changes: 9 additions & 1 deletion src/lib/requests/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

'use strict';

const respErrSym = Symbol('ResponseErrorDataSym');
const safeStringify = require('fast-safe-stringify');
const { ApiType, ONLY_FSPIOP_RESOURCES} = require('../constants');

const respErrSym = Symbol('ResponseErrorDataSym');

/**
* An HTTPResponseError class
Expand Down Expand Up @@ -119,10 +120,17 @@ const formatEndpointOrDefault = (endpoint, transportScheme, defaultEndpoint) =>
return `${transportScheme}://${endpoint}`;
};

const defineApiType = (resource, configApiType) => {
if (ONLY_FSPIOP_RESOURCES.includes(resource)) {
return ApiType.FSPIOP;
}
return configApiType;
};

module.exports = {
bodyStringifier,
buildUrl,
defineApiType,
formatEndpointOrDefault,
HTTPResponseError,
ResponseType,
Expand Down
20 changes: 17 additions & 3 deletions test/unit/lib/requests/mojaloopRequests.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
* James Bush - james.bush@modusbox.com *
**************************************************************************/

const fs = require('fs');
jest.mock('http');
const http = require('http');
const fs = require('fs');

const { TransformFacades } = require('@mojaloop/ml-schema-transformer-lib');
const mr = require('../../../../src/lib/requests/mojaloopRequests.js');
const WSO2Auth = require('../../../../src/lib/WSO2Auth');
const { ApiType, ISO_20022_HEADER_PART} = require('../../../../src/lib/constants');
const mockLogger = require('../../../__mocks__/mockLogger');
const { ApiType} = require('../../../../src/lib/requests/apiTransformer');

// dummy request bodies
const putPartiesBody = require('../../data/putPartiesBody.json');
Expand All @@ -31,7 +32,6 @@ const postFxQuotesBody = require('../../data/postFxQuotesBody.json');
const putFxQuotesBody = require('../../data/putFxQuotesBody.json');
const postFxTransfersBody = require('../../data/postFxTransfersBody.json');
const putFxTransfersBody = require('../../data/putFxTransfersBody.json');
const { TransformFacades } = require('@mojaloop/ml-schema-transformer-lib');

const jwsSigningKey = fs.readFileSync(__dirname + '/../../data/jwsSigningKey.pem');

Expand Down Expand Up @@ -1393,4 +1393,18 @@ describe('MojaloopRequests', () => {
// check the body was NOT converted to ISO20022
expect(reqBody).toEqual(putErrorBody);
});

it('should set FSPIOP headers for putTransactionRequests', async () => {
const conf = {
...defaultConf,
apiType: ApiType.ISO20022,
};
http.request = jest.fn((options, callback) => {
return getReqMock(options, callback);
});
const testMr = new mr(conf);
const res = await testMr.putTransactionRequests('txnReqId', {}, 'fspId');

expect(res.originalRequest.headers['content-type']).not.toContain(ISO_20022_HEADER_PART);
});
});