Skip to content

Commit

Permalink
Merge pull request #1028 from contentful/feat/retryLimit-parameter
Browse files Browse the repository at this point in the history
feat(options): Expose retryLimit parameter [ZEND-1819]
  • Loading branch information
andreascful authored Feb 16, 2022
2 parents 7295218 + 88fc079 commit e255683
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ runMigration(options)
| environmentId | `'master'` | string | ID of the environment within the space to run the | false |
| accessToken | | string | The access token to use | true |
| yes | false | boolean | Skips any confirmation before applying the migration,script | false |
| retryLimit | 5 | number | Number of retries before failure (every subsequent retry will increase the timeout to the previous retry by about 1.5 seconds) | false |
| requestBatchSize | 100 | number | Limit for every single request | false |
| headers | | object | Additional headers to attach to the requests | false |

Expand Down
42 changes: 26 additions & 16 deletions src/bin/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,51 @@ const configPath = path.resolve(homedir, '.contentfulrc.json')
interface ClientConfig {
accessToken?: string
spaceId?: string
environmentId?: string,
proxy?: string,
environmentId?: string
proxy?: string
rawProxy?: boolean
requestBatchSize?: number
headers?: Record<string, unknown>
retryLimit?: number
}

function getFileConfig (): ClientConfig {
try {
const config = require(configPath)
return config.cmaToken ?
{ accessToken: config.cmaToken } :
{}
return config.cmaToken ? { accessToken: config.cmaToken } : {}
} catch (e) {
return {}
}
}

function getEnvConfig (): ClientConfig {
const envKey = 'CONTENTFUL_MANAGEMENT_ACCESS_TOKEN'
return process.env[envKey] ?
{ accessToken : process.env[envKey] } :
{}
return process.env[envKey] ? { accessToken: process.env[envKey] } : {}
}

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

if (config.retryLimit && (config.retryLimit < 0 || config.retryLimit > 60)) {
throw new Error('retryLimit must be between 0 and 60')
}

if (!config.accessToken) {
Expand Down Expand Up @@ -107,18 +118,17 @@ function getConfig (argv) {
* @param {object} headers
*/
function addSequenceHeader (headers) {
if (typeof headers !== 'object') throw new Error('addSequence function expects an object as input')
if (typeof headers !== 'object') {
throw new Error('addSequence function expects an object as input')
}

return {
...headers,
// Unique sequence header
// Unique sequence header
'CF-Sequence': uuidv4()
}
}

export default getConfig

export {
getConfig,
ClientConfig
}
export { getConfig, ClientConfig }
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 @@ -38,6 +38,16 @@ describe('Config', function () {
expect(config.rawProxy).to.eql(true)
})

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

it('validates retryLimit argument', function () {
expect(() => getConfig({ retryLimit: -1 })).to.throw(Error, 'retryLimit must be between 0 and 60')
expect(() => getConfig({ retryLimit: 61 })).to.throw(Error, 'retryLimit must be between 0 and 60')
})

it('exposes requestBatchSize from argv', function () {
const config = getConfig({ requestBatchSize: 99 })
expect(config.requestBatchSize).to.eql(99)
Expand Down

0 comments on commit e255683

Please sign in to comment.