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
153 changes: 127 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@
"genexec": "pkg -t node8-win ."
},
"dependencies": {
"@elastic/elasticsearch": "9.1.0",
"@elastic/elasticsearch": "9.1.1",
"@hapi/basic": "7.0.2",
"@hapi/boom": "10.0.1",
"@hapi/good": "9.0.1",
"@hapi/hapi": "21.4.2",
"@hapi/hapi": "21.4.3",
"@hapi/inert": "7.1.0",
"@hapi/vision": "7.0.3",
"@mojaloop/central-services-logger": "11.9.0",
Expand Down Expand Up @@ -108,13 +108,13 @@
"json-rules-engine": "7.3.1",
"jsonwebtoken": "9.0.2",
"lodash": "4.17.21",
"mongoose": "8.17.1",
"mongoose": "8.17.2",
"multer": "2.0.2",
"mustache": "4.2.0",
"mv": "2.1.1",
"node-dir": "0.1.17",
"node-strings": "1.0.2",
"openapi-backend": "5.13.0",
"openapi-backend": "5.14.0",
"parse-strings-in-object": "1.6.0",
"passport": "0.7.0",
"passport-jwt": "4.0.1",
Expand Down
18 changes: 17 additions & 1 deletion src/lib/api-management.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

const addFormats = require('ajv-formats')
const OpenApiBackend = require('openapi-backend').default
const fs = require('node:fs')
const Utils = require('./utils')
const path = require('path')
const Config = require('./config')
Expand All @@ -37,9 +38,24 @@ const OpenApiMockHandler = require('./mocking/openApiMockHandler')

const apiDefinitionsPath = 'spec_files/api_definitions/'

// check if the file contains URL and return it instead of the file name
const checkUrl = async fileName => {
const buffer = Buffer.alloc(10)
const fileHandle = await fs.promises.open(fileName, 'r')
try {
await fileHandle.read(buffer, 0, 10, 0)
} finally {
await fileHandle.close()
}
const prefix = buffer.toString('utf8').replace('\uFEFF', '') // Remove BOM
return (prefix.startsWith('"http'))
? JSON.parse(fs.readFileSync(fileName).toString('utf8'))
: fileName
}

const validateDefinition = async (apiFilePath) => {
const newApi = new OpenApiBackend({
definition: path.join(apiFilePath),
definition: await checkUrl(path.join(apiFilePath)),
customizeAjv: ajv => addFormats(ajv),
strict: true,
quick: true
Expand Down
1 change: 1 addition & 0 deletions test/api_spec_url.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"http://localhost/api_spec_sync_empty.yaml"
3 changes: 3 additions & 0 deletions test/unit/lib/api-management.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ describe('API Management', () => {
it('should not throw an error', async () => {
await expect(APIManagement.addDefinition(specFilePrefix + 'api_spec_sync.yaml', 'name', '1.0', 'false')).resolves.toBeUndefined()
})
it('should load definition from URL', async () => {
await expect(APIManagement.addDefinition(specFilePrefix + 'api_spec_url.json', 'name', '1.0', 'false')).resolves.toBeUndefined()
})
})
describe('deleteDefinition', () => {
it('Happy path', async () => {
Expand Down