Skip to content

Commit 4946569

Browse files
committed
fix: add check for existing clients
1 parent f022d05 commit 4946569

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ module.exports.addTargetClient = function (target, client) {
242242
throw new Error('The supplied custom target client must contain an `info` object.')
243243
} else if (!('key' in client.info) || !('title' in client.info)) {
244244
throw new Error('The supplied custom target client must have an `info` object with a `key` and `title` property.')
245+
} else if (targets[target].hasOwnProperty(client.info.key)) {
246+
throw new Error('The supplied custom target client already exists, please use a different key')
245247
}
246248

247249
targets[target][client.info.key] = client

test/targets.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,20 @@ describe('Custom targets', function () {
143143
customClient = {
144144
...targets.node.request,
145145
info: {
146-
key: 'axios',
146+
key: 'axios-test',
147147
title: 'Axios',
148148
link: 'https://www.npmjs.com/package/axios',
149149
description: 'Promise based HTTP client for the browser and node.js'
150150
}
151151
}
152152
})
153153

154+
it('should throw if client already exists', function () {
155+
(function () {
156+
HTTPSnippet.addTargetClient('node', {...customClient, info: {...customClient.info, key: 'axios'}})
157+
}).should.throw(Error)
158+
})
159+
154160
it("should throw if the client's target does not exist", function () {
155161
(function () {
156162
HTTPSnippet.addTargetClient('node.js', customClient)

0 commit comments

Comments
 (0)