Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Commit

Permalink
refactor: don't use default backend, to avoid unnecessary deps (#54)
Browse files Browse the repository at this point in the history
BREAKING CHANGE to avoid pulling in `request`.
  • Loading branch information
mvayngrib authored Apr 3, 2020
1 parent 33373af commit c4e7cf4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
5 changes: 3 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const Component = require('./loader')
const Request = require('./backends/request')

class Client {
constructor (options) {
const backend = options.backend || new Request(options.config)
const backend = options.backend
if (!backend) throw new Error('expected "backend"')

const root = new Component({ splits: [], backend, getNames: options.getNames })
if (options.spec) root._addSpec(options.spec)
return root
Expand Down
38 changes: 32 additions & 6 deletions lib/client.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-env mocha */
const { expect } = require('chai')
const nock = require('nock')
const RequestBackend = require('./backends/request')

const Client = require('./client')

Expand All @@ -16,8 +17,9 @@ describe('lib.client', () => {
message: 'ta dah'
})

const config = { url }
const options = {
config: { url },
config,
spec: {
paths: {
'/magic': {
Expand All @@ -26,7 +28,8 @@ describe('lib.client', () => {
}
}
}
}
},
backend: new RequestBackend(config)
}
const client = new Client(options)
client.magic.get()
Expand All @@ -45,8 +48,9 @@ describe('lib.client', () => {
message: 'fail!'
})

const config = { url }
const options = {
config: { url },
config,
spec: {
paths: {
'/magic': {
Expand All @@ -55,7 +59,8 @@ describe('lib.client', () => {
}
}
}
}
},
backend: new RequestBackend(config)
}
const client = new Client(options)
client.magic.get()
Expand All @@ -71,8 +76,9 @@ describe('lib.client', () => {
.get('/magic')
.replyWithError({ message: 'fail!' })

const config = { url }
const options = {
config: { url },
config,
spec: {
paths: {
'/magic': {
Expand All @@ -81,7 +87,8 @@ describe('lib.client', () => {
}
}
}
}
},
backend: new RequestBackend(config)
}
const client = new Client(options)
client.magic.get()
Expand All @@ -90,6 +97,25 @@ describe('lib.client', () => {
done()
})
})

it('throws on missing "backend"', done => {
const config = { url }
const options = {
config,
spec: {
paths: {
'/magic': {
get: {
operationId: 'getMagic'
}
}
}
}
}

expect(() => new Client(options)).to.throw('backend')
done()
})
})
})
})

0 comments on commit c4e7cf4

Please sign in to comment.