diff --git a/index.d.ts b/index.d.ts index 57adc0f..8e0241e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -65,6 +65,7 @@ declare namespace customPlugin { port?: number, protocol?: 'http' | 'https', headers?: object, + prefix?: string, } interface ServiceOptions { returnAs: 'JSON' | 'BUFFER' | 'STREAM' diff --git a/lib/serviceBuilder.js b/lib/serviceBuilder.js index 926f27d..e7f95dc 100644 --- a/lib/serviceBuilder.js +++ b/lib/serviceBuilder.js @@ -100,11 +100,12 @@ function makeCallAsJSON(endUrl, method, body, options) { function makeCall(hostname, path, method, querystring, body, options = {}) { const protocol = (options && options.protocol) ? options.protocol : 'http' const port = (options && options.port) ? options.port : undefined + const pathWithPrefix = ((options && options.prefix) ? options.prefix : '') + path const endUrl = url.format({ protocol, hostname, port, - pathname: path, + pathname: pathWithPrefix, query: querystring, }) const asJSON = !options || !options.returnAs || options.returnAs === 'JSON' diff --git a/tests/serviceBuilder.test.js b/tests/serviceBuilder.test.js index f75b69e..e083b68 100644 --- a/tests/serviceBuilder.test.js +++ b/tests/serviceBuilder.test.js @@ -781,6 +781,28 @@ t.test('serviceBuilder', t => { myServiceNameScope.done() }) + t.test('with prefix', async t => { + t.plan(4) + + const myServiceNameScope = nock('http://my-service-name') + .replyContentLength() + .get('/my-prefix/foo?aa=bar') + .reply(200, { the: 'response' }, { + some: 'response-header', + }) + + const service = serviceBuilder('my-service-name', { }, { prefix: '/my-prefix' }) + + const response = await service.get('/foo', { aa: 'bar' }, { returnAs: 'JSON' }) + + t.equal(response.statusCode, 200) + t.strictSame(response.payload, { the: 'response' }) + t.strictSame(response.headers.some, 'response-header') + t.ok(response.headers['content-length']) + + myServiceNameScope.done() + }) + t.end() }) diff --git a/tests/types/index.ts b/tests/types/index.ts index e7a05b0..1bfea8b 100644 --- a/tests/types/index.ts +++ b/tests/types/index.ts @@ -121,6 +121,9 @@ a(async function (service) { const proxiedServiceWithOptionsAndHeaders: cpl.Service = request.getServiceProxy({port: 3000, protocol: 'http', headers: { key: 'value1' }}) await invokeSomeApis(proxiedServiceWithOptionsAndHeaders) + const proxiedServiceWithPrefix: cpl.Service = request.getServiceProxy({port: 3000, protocol: 'http', prefix: '/my-prefix'}) + await invokeSomeApis(proxiedServiceWithPrefix) + return { 'aa': 'boo' } }, { headers: {