Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,14 @@ await request.showPetById()

#### `APIOptions`

| property | type | required | default | description |
|---------------|------------------|----------|-------------------|-------------------------------------------------------------------------|
| **`client`** | `Function` | ✗ | [`unfetch`][] | a Function that executes the HTTP request. *(see [`clientFunction`][])* |
| **`server`** | `String|Object` | ✗ | `spec.servers[0]` | Root server url String, or [`Server Object`][] |
| **`headers`** | `Object` | ✗ | `{}` | Global HTTP request headers *(used with every request)* |
| **`query`** | `Object` | ✗ | `{}` | Global Query String *(used with every request)* |
| **`params`** | `Object` | ✗ | `{}` | Global [Path Templating][] parameters *(used with every request)* |
| property | type | required | default | description |
|---------------------|-----------------|----------|----------------------------------|--------------------------------------------------------------------------------|
| **`client`** | `Function` | ✗ | [`unfetch`][] | a Function that executes the HTTP request. *(see [`clientFunction`][])* |
| **`clientFactory`** | `Function` | x | `function () { return unfetch }` | a Function that returns a client Function. *(see [`clientFactoryFunction`][])* |
| **`server`** | `String|Object` | ✗ | `spec.servers[0]` | Root server url String, or [`Server Object`][] |
| **`headers`** | `Object` | ✗ | `{}` | Global HTTP request headers *(used with every request)* |
| **`query`** | `Object` | ✗ | `{}` | Global Query String *(used with every request)* |
| **`params`** | `Object` | ✗ | `{}` | Global [Path Templating][] parameters *(used with every request)* |

##### `clientFunction`

Expand Down Expand Up @@ -349,7 +350,7 @@ console.log(response.data)
</details>

<details>
<summary><em>Example: using <code>axios</code></summary>
<summary><em>Example: using <code>axios</code></em></summary>

``` js
const spec = require('./petstore.json')
Expand Down Expand Up @@ -378,6 +379,10 @@ const response = await request.createPet({

</details>

##### `clientFactoryFunction`

a `Function` with the signature: `Function(url, requestOptions, spec)` to return a client function to execute the HTTP request, by default this will return the passed in `client` option or uses the default [`isomorphic-unfetch`][], you can customize the client to use whatever HTTP library you prefer.

##### `ServerObject`

> ***⚠️ Note**: This is not the same as OpenAPI Specification's [Server Object][], though it's similarly structured*
Expand Down
12 changes: 8 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ module.exports = function (spec) {
constructor (options = {}) {
// process spec.servers & options.server
this.options = {
client: options.client || fetch,
clientFactory: options.clientFactory || function () {
return options.client || fetch
},
server: parseServer(options.server, spec),
// global properties
headers: options.headers || {},
Expand All @@ -21,7 +23,9 @@ module.exports = function (spec) {
}
}

__request (url, options) {
__request (url, options, spec) {
const client = this.options.clientFactory(url, options, spec)

// merge params with global defaults
const params = { ...this.options.params, ...options.params }

Expand All @@ -47,7 +51,7 @@ module.exports = function (spec) {
// construct combined headers object
const headers = { ...this.options.headers, ...options.headers }

return this.options.client(WHATWGURL, {
return client(WHATWGURL, {
...options,
headers // use the already combined headers
})
Expand All @@ -65,7 +69,7 @@ module.exports = function (spec) {
enumerable: true,
writable: false,
value: function (options) {
return this.__request(url, { ...options, method })
return this.__request(url, { ...options, method }, { method, operation })
}
})
}
Expand Down
51 changes: 51 additions & 0 deletions test/custom-client-factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const { test } = require('tap')

const spec = require('./fixtures/httpbin.json')
const OASRequest = require('../lib')(spec)

const fetch = require('isomorphic-unfetch')

const { Response } = require('node-fetch')

const api = new OASRequest({
server: 'https://httpbin.org:443',

clientFactory: function (url, options, spec) {
const { operation: { operationId } } = spec

if (operationId === 'httpGet') {
return () => new Response(JSON.stringify({ mock: 'httpGet' }), { status: 599 })
}

return fetch
}
})

test('GET /', async assert => {
assert.plan(2)

const response = await api.httpGet()

const body = await response.json()

assert.equal(response.status, 599)

assert.match(body, {
mock: 'httpGet'
})
})

test('POST json', async assert => {
assert.plan(1)

const response = await api.httpPost({
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({ foo: 'bar' })
})

const body = await response.json()

assert.match(body, { data: '{"foo":"bar"}', json: { foo: 'bar' } })
})