Skip to content

Commit

Permalink
feat: add custom headers support (#698)
Browse files Browse the repository at this point in the history
  • Loading branch information
z0al authored Apr 14, 2021
1 parent b89df03 commit 168c7f2
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ module.exports = function (migration, context) {
| accessToken | | string | The access token to use | true |
| yes | false | boolean | Skips any confirmation before applying the migration,script | false |
| requestBatchSize | 100 | number | Limit for every single request | false |
| headers | | object | Additional headers to attach to the requests | false |

### Chaining vs Object notation

Expand Down
50 changes: 47 additions & 3 deletions src/bin/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface ClientConfig {
proxy?: string,
rawProxy?: boolean
requestBatchSize?: number
headers?: Record<string, unknown>
}

function getFileConfig (): ClientConfig {
Expand All @@ -32,14 +33,15 @@ function getEnvConfig (): ClientConfig {
{}
}

function getArgvConfig ({ spaceId, environmentId = 'master', accessToken, proxy, rawProxy, requestBatchSize }): ClientConfig {
function getArgvConfig ({ spaceId, environmentId = 'master', accessToken, proxy, rawProxy, requestBatchSize, headers }): ClientConfig {
const config = {
spaceId,
environmentId,
accessToken,
proxy,
rawProxy,
requestBatchSize
requestBatchSize,
headers
}

if (!config.accessToken) {
Expand All @@ -49,10 +51,52 @@ function getArgvConfig ({ spaceId, environmentId = 'master', accessToken, proxy,
return config
}

/**
* Turn header option into an object. Invalid header values
* are ignored.
*
* @example
* getHeadersConfig('Accept: Any')
* // -> {Accept: 'Any'}
*
* @example
* getHeadersConfig(['Accept: Any', 'X-Version: 1'])
* // -> {Accept: 'Any', 'X-Version': '1'}
*/
function getHeadersConfig (value?: string | string[]) {
if (!value) {
return {}
}

const values = Array.isArray(value) ? value : [value]

return values.reduce((headers, value) => {
value = value.trim()

const separatorIndex = value.indexOf(':')

// Invalid header format
if (separatorIndex === -1) {
return headers
}

const headerKey = value.slice(0, separatorIndex).trim()
const headerValue = value.slice(separatorIndex + 1).trim()

return {
...headers,
[headerKey]: headerValue
}
}, {})
}

function getConfig (argv) {
const fileConfig = getFileConfig()
const envConfig = getEnvConfig()
const argvConfig = getArgvConfig(argv || {})
const argvConfig = getArgvConfig({
...argv,
headers: argv?.headers || getHeadersConfig(argv?.header)
})

return Object.assign(fileConfig, envConfig, argvConfig)
}
Expand Down
5 changes: 5 additions & 0 deletions src/bin/usage-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ export default yargs
type: 'number',
default: 100
})
.option('header', {
alias: 'H',
type: 'string',
describe: 'Pass an additional HTTP Header'
})
.demandOption(['space-id'], 'Please provide a space ID')
.help('h')
.alias('h', 'help')
Expand Down
10 changes: 10 additions & 0 deletions test/unit/bin/lib/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,14 @@ describe('Config', function () {
const config = getConfig({ requestBatchSize: 99 })
expect(config.requestBatchSize).to.eql(99)
})

it('exposes headers from argv', function () {
const config = getConfig({ headers: { test: true } })
expect(config.headers).to.eql({ test: true })
})

it('parses argv.header if provided', function () {
const config = getConfig({ header: ['Accept : application/json ', ' X-Header: 1'] })
expect(config.headers).to.eql({ Accept: 'application/json', 'X-Header': '1' })
})
})

0 comments on commit 168c7f2

Please sign in to comment.