Skip to content

Commit

Permalink
feat: support passing additional HTTP headers (#868)
Browse files Browse the repository at this point in the history
  • Loading branch information
z0al committed Apr 13, 2021
1 parent 76df976 commit 0c00bd6
Show file tree
Hide file tree
Showing 76 changed files with 415 additions and 172 deletions.
4 changes: 3 additions & 1 deletion .prettierrc
@@ -1,4 +1,6 @@
{
"singleQuote": true,
"semi": false
"semi": false,
"trailingComma": "none",
"arrowParens": "avoid"
}
2 changes: 1 addition & 1 deletion lib/cli.js
Expand Up @@ -26,7 +26,7 @@ yargs
})
.version(false)
.epilog('Copyright 2019 Contentful')
.fail(function(msg, err, yargs) {
.fail(function (msg, err, yargs) {
if (err) throw err
console.error(yargs.help())
console.error(msg)
Expand Down
2 changes: 1 addition & 1 deletion lib/cmds/config.js
Expand Up @@ -2,7 +2,7 @@ module.exports.command = 'config'

module.exports.desc = 'Manage and list your configs'

module.exports.builder = function(yargs) {
module.exports.builder = function (yargs) {
return yargs
.commandDir('config_cmds')
.demandCommand(4, 'Please specify a sub command.')
Expand Down
2 changes: 1 addition & 1 deletion lib/cmds/config_cmds/remove.js
Expand Up @@ -70,7 +70,7 @@ const removeHandler = async argv => {
'insecure',
'host',
'proxy',
'rawProxy',
'rawProxy'
]
contextKeys.forEach(key => argv[key] && delete options[key])
}
Expand Down
2 changes: 1 addition & 1 deletion lib/cmds/content-type.js
Expand Up @@ -3,7 +3,7 @@ module.exports.aliases = ['ct']

module.exports.desc = 'Manage and list your space content types'

module.exports.builder = function(yargs) {
module.exports.builder = function (yargs) {
return yargs
.commandDir('content-type_cmds')
.demandCommand(4, 'Please specify a sub command.')
Expand Down
9 changes: 8 additions & 1 deletion lib/cmds/content-type_cmds/get.js
Expand Up @@ -4,6 +4,7 @@ const { createManagementClient } = require('../../utils/contentful-clients')
const { log } = require('../../utils/log')
const { handleAsyncError: handle } = require('../../utils/async')
const { getId } = require('../../utils/helpers')
const { getHeadersFromOption } = require('../../utils/headers')

module.exports.command = 'get'

Expand All @@ -19,6 +20,11 @@ module.exports.builder = yargs => {
describe: 'Contentful management API token'
})
.option('environment-id', { type: 'string', describe: 'Environment id' })
.option('header', {
alias: 'H',
type: 'string',
describe: 'Pass an additional HTTP Header'
})
.epilog('Copyright 2019 Contentful')
}

Expand All @@ -28,7 +34,8 @@ async function ctShow(argv) {

const client = await createManagementClient({
accessToken: managementToken,
feature: 'content_type-get'
feature: 'content_type-get',
headers: getHeadersFromOption(argv.header)
})

const space = await client.getSpace(activeSpaceId)
Expand Down
11 changes: 9 additions & 2 deletions lib/cmds/content-type_cmds/list.js
Expand Up @@ -5,6 +5,7 @@ const { handleAsyncError: handle } = require('../../utils/async')
const { log } = require('../../utils/log')
const paginate = require('../../utils/pagination')
const { highlightStyle } = require('../../utils/styles')
const { getHeadersFromOption } = require('../../utils/headers')

module.exports.command = 'list'

Expand All @@ -26,15 +27,21 @@ module.exports.builder = yargs => {
describe:
'Environment ID you want to interact with. Defaults to the current active environment.'
})
.option('header', {
alias: 'H',
type: 'string',
describe: 'Pass an additional HTTP Header'
})
.epilog('Copyright 2019 Contentful')
}

async function ctList({ context }) {
async function ctList({ header, context }) {
const { managementToken, activeSpaceId, activeEnvironmentId } = context

const client = await createManagementClient({
accessToken: managementToken,
feature: 'content_type-list'
feature: 'content_type-list',
headers: getHeadersFromOption(header)
})

const space = await client.getSpace(activeSpaceId)
Expand Down
2 changes: 1 addition & 1 deletion lib/cmds/extension.js
Expand Up @@ -2,7 +2,7 @@ module.exports.command = 'extension'

module.exports.desc = 'Manage and list your extensions'

module.exports.builder = function(yargs) {
module.exports.builder = function (yargs) {
return yargs
.commandDir('extension_cmds')
.demandCommand(4, 'Please specify a sub command.')
Expand Down
9 changes: 8 additions & 1 deletion lib/cmds/extension_cmds/create.js
@@ -1,6 +1,7 @@
const { handleAsyncError: handle } = require('../../utils/async')
const { createManagementClient } = require('../../utils/contentful-clients')
const { successEmoji } = require('../../utils/emojis')
const { getHeadersFromOption } = require('../../utils/headers')
const { success } = require('../../utils/log')

const { assertExtensionValuesProvided } = require('./utils/assertions')
Expand Down Expand Up @@ -41,6 +42,11 @@ module.exports.builder = yargs => {
type: 'string',
describe: 'JSON string of installation parameter key-value pairs'
})
.option('header', {
alias: 'H',
type: 'string',
describe: 'Pass an additional HTTP Header'
})
.epilog('Copyright 2019 Contentful')
}

Expand Down Expand Up @@ -73,7 +79,8 @@ async function createExtensionHandler(argv) {

const client = await createManagementClient({
accessToken: managementToken,
feature: 'extension-create'
feature: 'extension-create',
headers: getHeadersFromOption(argv.header)
})

const space = await client.getSpace(activeSpaceId)
Expand Down
9 changes: 8 additions & 1 deletion lib/cmds/extension_cmds/delete.js
@@ -1,6 +1,7 @@
const { handleAsyncError: handle } = require('../../utils/async')
const { createManagementClient } = require('../../utils/contentful-clients')
const { successEmoji } = require('../../utils/emojis')
const { getHeadersFromOption } = require('../../utils/headers')
const { success } = require('../../utils/log')

const { assertForceOrCorrectVersionProvided } = require('./utils/assertions')
Expand All @@ -27,6 +28,11 @@ module.exports.builder = yargs => {
type: 'boolean',
describe: 'Force operation without explicit version'
})
.option('header', {
alias: 'H',
type: 'string',
describe: 'Pass an additional HTTP Header'
})
.epilog('Copyright 2019 Contentful')
}

Expand All @@ -36,7 +42,8 @@ async function deleteExtension(argv) {

const client = await createManagementClient({
accessToken: managementToken,
feature: 'extension-delete'
feature: 'extension-delete',
headers: getHeadersFromOption(argv.header)
})

const space = await client.getSpace(activeSpaceId)
Expand Down
11 changes: 9 additions & 2 deletions lib/cmds/extension_cmds/get.js
@@ -1,6 +1,7 @@
const { createManagementClient } = require('../../utils/contentful-clients')
const { handleAsyncError: handle } = require('../../utils/async')
const { logExtension } = require('./utils/log-as-table')
const { getHeadersFromOption } = require('../../utils/headers')

module.exports.command = 'get'

Expand All @@ -16,15 +17,21 @@ module.exports.builder = yargs => {
})
.option('space-id', { type: 'string', describe: 'Space id' })
.option('environment-id', { type: 'string', describe: 'Environment id' })
.option('header', {
alias: 'H',
type: 'string',
describe: 'Pass an additional HTTP Header'
})
.epilog('Copyright 2019 Contentful')
}

async function getExtension({ context, id }) {
async function getExtension({ context, id, header }) {
const { managementToken, activeSpaceId, activeEnvironmentId } = context

const client = await createManagementClient({
accessToken: managementToken,
feature: 'extension-get'
feature: 'extension-get',
headers: getHeadersFromOption(header)
})

const space = await client.getSpace(activeSpaceId)
Expand Down
11 changes: 9 additions & 2 deletions lib/cmds/extension_cmds/list.js
Expand Up @@ -4,6 +4,7 @@ const { createManagementClient } = require('../../utils/contentful-clients')
const { handleAsyncError: handle } = require('../../utils/async')
const { log } = require('../../utils/log')
const paginate = require('../../utils/pagination')
const { getHeadersFromOption } = require('../../utils/headers')

module.exports.command = 'list'

Expand All @@ -18,15 +19,21 @@ module.exports.builder = yargs => {
})
.option('space-id', { type: 'string', describe: 'Space id' })
.option('environment-id', { type: 'string', describe: 'Environment id' })
.option('header', {
alias: 'H',
type: 'string',
describe: 'Pass an additional HTTP Header'
})
.epilog('Copyright 2019 Contentful')
}

async function listExtensions({ context }) {
async function listExtensions({ context, header }) {
const { managementToken, activeSpaceId, activeEnvironmentId } = context

const client = await createManagementClient({
accessToken: managementToken,
feature: 'extension-list'
feature: 'extension-list',
headers: getHeadersFromOption(header)
})

const space = await client.getSpace(activeSpaceId)
Expand Down
9 changes: 8 additions & 1 deletion lib/cmds/extension_cmds/update.js
@@ -1,6 +1,7 @@
const { handleAsyncError: handle } = require('../../utils/async')
const { createManagementClient } = require('../../utils/contentful-clients')
const { successEmoji } = require('../../utils/emojis')
const { getHeadersFromOption } = require('../../utils/headers')
const { success } = require('../../utils/log')
const { createExtension } = require('./create')

Expand Down Expand Up @@ -53,6 +54,11 @@ module.exports.builder = yargs => {
type: 'string',
describe: 'JSON string of installation parameter key-value pairs'
})
.option('header', {
alias: 'H',
type: 'string',
describe: 'Pass an additional HTTP Header'
})
.epilog('Copyright 2019 Contentful')
}

Expand Down Expand Up @@ -80,7 +86,8 @@ async function updateExtensionHandler(argv) {

const client = await createManagementClient({
accessToken: managementToken,
feature: 'extension-update'
feature: 'extension-update',
headers: getHeadersFromOption(argv.header)
})

const space = await client.getSpace(activeSpaceId)
Expand Down
2 changes: 1 addition & 1 deletion lib/cmds/organization.js
Expand Up @@ -2,7 +2,7 @@ module.exports.command = 'organization'

module.exports.desc = 'Manage and list your organizations'

module.exports.builder = function(yargs) {
module.exports.builder = function (yargs) {
return yargs
.usage('')
.commandDir('organization_cmds')
Expand Down
11 changes: 9 additions & 2 deletions lib/cmds/organization_cmds/list.js
Expand Up @@ -2,6 +2,7 @@ const Table = require('cli-table3')

const { handleAsyncError: handle } = require('../../utils/async')
const { createManagementClient } = require('../../utils/contentful-clients')
const { getHeadersFromOption } = require('../../utils/headers')
const { log } = require('../../utils/log')
const paginate = require('../../utils/pagination')

Expand All @@ -17,6 +18,11 @@ module.exports.builder = yargs => {
describe: 'Contentful management API token',
type: 'string'
})
.option('header', {
alias: 'H',
type: 'string',
describe: 'Pass an additional HTTP Header'
})
.epilog(
[
'See more at:',
Expand All @@ -28,12 +34,13 @@ module.exports.builder = yargs => {

module.exports.aliases = ['ls']

async function organizationList({ context }) {
async function organizationList({ context, header }) {
const { managementToken } = context

const client = await createManagementClient({
accessToken: managementToken,
feature: 'organization-list'
feature: 'organization-list',
headers: getHeadersFromOption(header)
})

const result = await paginate({ client, method: 'getOrganizations' })
Expand Down
8 changes: 4 additions & 4 deletions lib/cmds/snake.js
Expand Up @@ -11,7 +11,7 @@ const FOOD = '@'
let interval
let score = 0

module.exports.handler = function() {
module.exports.handler = function () {
const screen = blessed.screen({
smartCSR: true
})
Expand All @@ -35,17 +35,17 @@ module.exports.handler = function() {

screen.append(game)

interval = setInterval(function() {
interval = setInterval(function () {
game.setContent(board.update())
screen.render()
}, INTERVAL)

screen.key(['left', 'right', 'up', 'down'], function(ch, key) {
screen.key(['left', 'right', 'up', 'down'], function (ch, key) {
DIRECTION = key.full
})

// Quit on Escape, q, or Control-C.
screen.key(['escape', 'q', 'C-c'], function() {
screen.key(['escape', 'q', 'C-c'], function () {
return process.exit(0)
})

Expand Down
2 changes: 1 addition & 1 deletion lib/cmds/space.js
Expand Up @@ -2,7 +2,7 @@ module.exports.command = 'space'

module.exports.desc = 'Manage and list your spaces'

module.exports.builder = function(yargs) {
module.exports.builder = function (yargs) {
return yargs
.usage('')
.commandDir('space_cmds')
Expand Down
2 changes: 1 addition & 1 deletion lib/cmds/space_cmds/accesstoken.js
Expand Up @@ -4,7 +4,7 @@ module.exports.desc = 'Manage and list your delivery access tokens'

module.exports.aliases = ['at']

module.exports.builder = function(yargs) {
module.exports.builder = function (yargs) {
return yargs
.commandDir('accesstoken_cmds')
.demandCommand(5, 'Please specify a sub command.')
Expand Down
9 changes: 8 additions & 1 deletion lib/cmds/space_cmds/accesstoken_cmds/create.js
@@ -1,6 +1,7 @@
const { handleAsyncError: handle } = require('../../../utils/async')
const { createManagementClient } = require('../../../utils/contentful-clients')
const { successEmoji } = require('../../../utils/emojis')
const { getHeadersFromOption } = require('../../../utils/headers')
const { success } = require('../../../utils/log')
const paginate = require('../../../utils/pagination')
const { highlightStyle } = require('../../../utils/styles')
Expand Down Expand Up @@ -34,6 +35,11 @@ module.exports.builder = yargs => {
describe: 'Suppress command output',
default: false
})
.option('header', {
alias: 'H',
type: 'string',
describe: 'Pass an additional HTTP Header'
})
.epilog('Copyright 2019 Contentful')
}

Expand All @@ -49,7 +55,8 @@ async function accessTokenCreate(argv) {

const client = await createManagementClient({
accessToken: managementToken,
feature
feature,
headers: getHeadersFromOption(argv.header)
})

const space = await client.getSpace(activeSpaceId)
Expand Down

0 comments on commit 0c00bd6

Please sign in to comment.