Skip to content

Commit 0cecdd3

Browse files
committed
Adding the ability to import custom targets and target clients.
1 parent f090b1c commit 0cecdd3

File tree

3 files changed

+116
-4
lines changed

3 files changed

+116
-4
lines changed

src/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,28 @@ HTTPSnippet.prototype._matchTarget = function (target, client) {
221221
// exports
222222
module.exports = HTTPSnippet
223223

224+
module.exports.addTarget = function (target) {
225+
if (!('info' in target)) {
226+
throw new Error('The supplied custom target must contain an `info` object.')
227+
} else if (!('key' in target.info) || !('title' in target.info) || !('extname' in target.info) || !('default' in target.info)) {
228+
throw new Error('The supplied custom target must have an `info` object with a `key`, `title`, `extname`, and `default` property.')
229+
} else if (targets.hasOwnProperty(target.info.key)) {
230+
throw new Error('The supplied custom target already exists.')
231+
} else if (Object.keys(target).length === 1) {
232+
throw new Error('A custom target must have a client defined on it.')
233+
}
234+
235+
targets[target.info.key] = target
236+
}
237+
238+
module.exports.addTargetClient = function (target, client, plugin) {
239+
if (!targets.hasOwnProperty(target)) {
240+
throw new Error(`Sorry, but no ${target} target exists to add clients to.`)
241+
}
242+
243+
targets[target][client] = plugin
244+
}
245+
224246
module.exports.availableTargets = function () {
225247
return Object.keys(targets).map(function (key) {
226248
var target = Object.assign({}, targets[key].info)

test/fixtures/customTarget.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict'
2+
3+
module.exports = {
4+
info: {
5+
key: 'js-variant',
6+
title: 'JavaScript Variant',
7+
extname: '.js',
8+
default: 'request'
9+
},
10+
11+
request: require('../../src/targets/node/request')
12+
}

test/targets.js

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global describe, it */
1+
/* global describe, it, beforeEach */
22

33
'use strict'
44

@@ -85,15 +85,93 @@ var itShouldGenerateOutput = function (request, path, target, client) {
8585
}
8686

8787
describe('Available Targets', function () {
88-
var targets = HTTPSnippet.availableTargets()
89-
90-
targets.forEach(function (target) {
88+
HTTPSnippet.availableTargets().forEach(function (target) {
9189
it('available-targets.json should include ' + target.title, function () {
9290
fixtures['available-targets'].should.containEql(target)
9391
})
9492
})
9593
})
9694

95+
describe('Custom targets', function () {
96+
describe('Adding a custom target', function () {
97+
it('should throw if the target does has no info object', function () {
98+
(function () {
99+
HTTPSnippet.addTarget({})
100+
}).should.throw(Error)
101+
})
102+
103+
it('should throw if the target does not have a properly constructed info object', function () {
104+
(function () {
105+
HTTPSnippet.addTarget({info: {key: ''}})
106+
}).should.throw(Error)
107+
})
108+
109+
it('should throw if the target already exists', function () {
110+
(function () {
111+
HTTPSnippet.addTarget(targets.node)
112+
}).should.throw(Error)
113+
})
114+
115+
it('should throw if the target has no client', function () {
116+
(function () {
117+
HTTPSnippet.addTarget({
118+
info: targets.node.info
119+
})
120+
}).should.throw(Error)
121+
})
122+
123+
it('should add and convert for a new custom target', function () {
124+
const customTarget = require('./fixtures/customTarget')
125+
126+
HTTPSnippet.addTarget(customTarget)
127+
const target = HTTPSnippet.availableTargets().find(function (target) { return target.key === customTarget.info.key })
128+
const client = target.clients.find(function (client) { return client.key === customTarget.info.default })
129+
client.should.be.an.Object()
130+
131+
Object.keys(fixtures.requests).filter(clearInfo).forEach(function (request) {
132+
// Re-using the `request` module fixtures and framework since we copied it to create a custom client.
133+
itShouldGenerateOutput(request, 'node/request/', customTarget.info.key, customTarget.info.default)
134+
})
135+
})
136+
})
137+
138+
describe('Adding a custom client target', function () {
139+
let customClient
140+
141+
beforeEach(function () {
142+
// Re-using the existing request client instead of mocking out something completely new.
143+
customClient = {
144+
...targets.node.request,
145+
info: {
146+
key: 'axios',
147+
title: 'Axios',
148+
link: 'https://www.npmjs.com/package/axios',
149+
description: 'Promise based HTTP client for the browser and node.js'
150+
}
151+
}
152+
})
153+
154+
it("should throw if the client's target does not exist", function () {
155+
(function () {
156+
HTTPSnippet.addTargetClient('node.js', 'axios', customClient)
157+
}).should.throw(Error)
158+
})
159+
160+
it('should add and convert for a new custom client target', function () {
161+
HTTPSnippet.addTargetClient('node', 'axios', customClient)
162+
163+
const target = HTTPSnippet.availableTargets().find(function (target) { return target.key === 'node' })
164+
const client = target.clients.find(function (client) { return client.key === 'axios' })
165+
client.should.be.an.Object()
166+
167+
Object.keys(fixtures.requests).filter(clearInfo).forEach(function (request) {
168+
// Re-using the `request` module fixtures and framework since we copied it to create a custom client target.
169+
itShouldGenerateOutput(request, 'node/request/', 'node', 'axios')
170+
})
171+
})
172+
})
173+
})
174+
97175
// test all the things!
98176
describe('Targets', function () {
99177
Object.keys(targets).forEach(function (target) {

0 commit comments

Comments
 (0)