Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.

Commit 1c556c4

Browse files
feat: add support for header parameters (#724)
1 parent 0703bd9 commit 1c556c4

File tree

4 files changed

+62
-23
lines changed

4 files changed

+62
-23
lines changed

src/index.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,31 @@ test('Can use global parameters in request body', async (t) => {
253253
t.true(scope.isDone())
254254
})
255255

256+
test('Can set header parameters', async (t) => {
257+
const deployId = uuidv4()
258+
const functionName = 'testFunction'
259+
const body = 'test'
260+
const expectedResponse = { test: 'test' }
261+
const retryCount = 3
262+
const scope = nock(origin)
263+
.put(`${pathPrefix}/deploys/${deployId}/functions/${functionName}`, body, {
264+
'Content-Type': 'application/octet-stream',
265+
})
266+
.matchHeader('X-Nf-Retry-Count', retryCount.toString())
267+
.reply(200, expectedResponse)
268+
269+
const client = getClient()
270+
const response = await client.uploadDeployFunction({
271+
deploy_id: deployId,
272+
name: functionName,
273+
body: fromString(body),
274+
xNfRetryCount: retryCount,
275+
})
276+
277+
t.deepEqual(response, expectedResponse)
278+
t.true(scope.isDone())
279+
})
280+
256281
test('Validates required path parameters', async (t) => {
257282
const accountId = uuidv4()
258283
const scope = nock(origin).put(`${pathPrefix}/accounts/${accountId}`).reply(200)

src/methods/index.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fetch from 'node-fetch'
33
import { getOperations } from '../operations.js'
44

55
import { addBody } from './body.js'
6+
import { getRequestParams } from './params.js'
67
import { parseResponse, getFetchError } from './response.js'
78
import { shouldRetry, waitForRetry, MAX_RETRY } from './retry.js'
89
import { getUrl } from './url.js'
@@ -32,12 +33,23 @@ const callMethod = async function ({ method, basePath, defaultHeaders, agent, gl
3233
return parsedResponse
3334
}
3435

35-
const getOpts = function ({ method: { verb, parameters }, defaultHeaders, agent, requestParams: { body }, opts }) {
36+
const getOpts = function ({ method: { verb, parameters }, defaultHeaders, agent, requestParams, opts }) {
37+
const { body } = requestParams
3638
const optsA = addHttpMethod(verb, opts)
37-
const optsB = addDefaultHeaders(defaultHeaders, optsA)
38-
const optsC = addBody(body, parameters, optsB)
39-
const optsD = addAgent(agent, optsC)
40-
return optsD
39+
const optsB = addHeaderParams(parameters, requestParams, optsA)
40+
const optsC = addDefaultHeaders(defaultHeaders, optsB)
41+
const optsD = addBody(body, parameters, optsC)
42+
const optsE = addAgent(agent, optsD)
43+
return optsE
44+
}
45+
46+
// Add header parameters
47+
const addHeaderParams = function (parameters, requestParams, opts) {
48+
if (parameters.header === undefined) {
49+
return opts
50+
}
51+
52+
return { ...opts, headers: getRequestParams(parameters.header, requestParams, 'header parameter') }
4153
}
4254

4355
// Add the HTTP method based on the OpenAPI definition

src/methods/params.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import camelCase from 'lodash.camelcase'
2+
3+
export const getRequestParams = function (params, requestParams, name) {
4+
const entries = Object.values(params).map((param) => getRequestParam(param, requestParams, name))
5+
return Object.assign({}, ...entries)
6+
}
7+
8+
const getRequestParam = function (param, requestParams, name) {
9+
const value = requestParams[param.name] || requestParams[camelCase(param.name)]
10+
11+
if (value !== undefined) {
12+
return { [param.name]: value }
13+
}
14+
15+
if (param.required) {
16+
throw new Error(`Missing required ${name} '${param.name}'`)
17+
}
18+
}

src/methods/url.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import camelCase from 'lodash.camelcase'
21
import queryString from 'qs'
32

3+
import { getRequestParams } from './params.js'
4+
45
// Replace path parameters and query parameters in the URI, using the OpenAPI
56
// definition
67
export const getUrl = function ({ path, parameters }, basePath, requestParams) {
@@ -28,20 +29,3 @@ const addQueryParams = function (url, parameters, requestParams) {
2829

2930
return `${url}?${queryString.stringify(queryParams, { arrayFormat: 'brackets' })}`
3031
}
31-
32-
const getRequestParams = function (params, requestParams, name) {
33-
const entries = Object.values(params).map((param) => getRequestParam(param, requestParams, name))
34-
return Object.assign({}, ...entries)
35-
}
36-
37-
const getRequestParam = function (param, requestParams, name) {
38-
const value = requestParams[param.name] || requestParams[camelCase(param.name)]
39-
40-
if (value !== undefined) {
41-
return { [param.name]: value }
42-
}
43-
44-
if (param.required) {
45-
throw new Error(`Missing required ${name} '${param.name}'`)
46-
}
47-
}

0 commit comments

Comments
 (0)