@bifot/json-rpc
Advanced tools
Comparing version 1.2.0 to 1.3.0
{ | ||
"name": "@bifot/json-rpc", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Solution for communication between services using HTTP protocol via JSON RPC. 🔬", | ||
@@ -20,3 +20,4 @@ "main": "src/index.js", | ||
"dependencies": { | ||
"axios": "^0.19.0", | ||
"axios": "^0.18.0", | ||
"axios-retry": "^3.1.2", | ||
"co-body": "^6.0.0", | ||
@@ -23,0 +24,0 @@ "http-assert": "^1.4.1" |
@@ -54,2 +54,5 @@ # @bifot/json-rpc | ||
id: 10, | ||
}, { | ||
retries: 5, | ||
retryDelay: 500, | ||
}); | ||
@@ -56,0 +59,0 @@ |
const axios = require('axios'); | ||
const enableRetries = require('axios-retry'); | ||
const { buildOptions } = require('./helpers'); | ||
enableRetries(axios, { | ||
retries: 0, | ||
}); | ||
class Client { | ||
@@ -17,6 +23,17 @@ constructor(options) { | ||
async ask(name, params, options) { | ||
mock(responses) { | ||
this.responses = responses; | ||
return this; | ||
} | ||
async ask(name, params = {}, options = {}) { | ||
const [service, method] = name.split('.'); | ||
const url = this.services.get(service); | ||
const mock = this.responses && this.responses[service] && this.responses[service][method]; | ||
if (mock) { | ||
return typeof mock === 'function' ? mock(params) : mock; | ||
} | ||
if (!url) { | ||
@@ -32,3 +49,3 @@ throw new Error('Service is not found'); | ||
id: params.params && params.id, | ||
}, options); | ||
}, buildOptions(options)); | ||
@@ -35,0 +52,0 @@ return { |
const formatResponse = require('./formatResponse'); | ||
const buildOptions = require('./buildOptions'); | ||
module.exports = { | ||
formatResponse, | ||
buildOptions, | ||
}; |
const { expect } = require('chai'); | ||
const { Client } = require('../src'); | ||
const server = require('../examples/server'); | ||
require('../examples/server'); | ||
describe('api', () => { | ||
@@ -45,4 +44,49 @@ let client; | ||
result: undefined, | ||
}) | ||
}); | ||
}); | ||
it('should get response after retries', async () => { | ||
server.close(); | ||
setTimeout(() => { | ||
server.listen(process.env.APP_PORT); | ||
}, 1000); | ||
const user = await client.ask('users.get', { | ||
age: 10, | ||
}, { | ||
retries: 5, | ||
retryDelay: 500, | ||
}); | ||
expect(user).to.be.deep.equal({ | ||
error: { | ||
code: 400, | ||
message: 'Invalid age', | ||
}, | ||
result: undefined, | ||
}); | ||
}); | ||
it('should get mock response', async () => { | ||
const response = { | ||
error: { | ||
code: 500, | ||
message: 'Server error', | ||
}, | ||
result: undefined, | ||
}; | ||
server.close(); | ||
client.mock({ | ||
users: { | ||
get: response, | ||
}, | ||
}); | ||
const user = await client.ask('users.get'); | ||
expect(user).to.be.deep.equal(response); | ||
}); | ||
}); |
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
8034
12
251
68
4
5
+ Addedaxios-retry@^3.1.2
+ Added@babel/runtime@7.26.0(transitive)
+ Addedaxios@0.18.1(transitive)
+ Addedaxios-retry@3.9.1(transitive)
+ Addedis-buffer@2.0.5(transitive)
+ Addedis-retry-allowed@2.2.0(transitive)
+ Addedregenerator-runtime@0.14.1(transitive)
- Removedaxios@0.19.2(transitive)
Updatedaxios@^0.18.0