Skip to content

Commit

Permalink
chore: add user-agent field (#782)
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy authored Aug 21, 2022
1 parent 9b1239c commit 96c33bf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
29 changes: 24 additions & 5 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ yargs
default: 'stylish' as OutputFormat,
},
}),
handleStats
(argv) => {
process.env.REDOCLY_CLI_COMMAND = 'stats';
handleStats(argv);
}
)
.command(
'split [api]',
Expand All @@ -54,7 +57,10 @@ yargs
},
})
.demandOption('api'),
handleSplit
(argv) => {
process.env.REDOCLY_CLI_COMMAND = 'split';
handleSplit(argv);
}
)
.command(
'join [apis...]',
Expand Down Expand Up @@ -89,6 +95,7 @@ yargs
},
}),
(argv) => {
process.env.REDOCLY_CLI_COMMAND = 'join';
handleJoin(argv, version);
}
)
Expand Down Expand Up @@ -127,7 +134,10 @@ yargs
})
.implies('batch-id', 'batch-size')
.implies('batch-size', 'batch-id'),
transformPush(handlePush)
(argv) => {
process.env.REDOCLY_CLI_COMMAND = 'push';
transformPush(handlePush)(argv);
}
)
.command(
'lint [apis...]',
Expand Down Expand Up @@ -183,6 +193,7 @@ yargs
},
}),
(argv) => {
process.env.REDOCLY_CLI_COMMAND = 'lint';
handleLint(argv, version);
}
)
Expand Down Expand Up @@ -259,6 +270,7 @@ yargs
},
}),
(argv) => {
process.env.REDOCLY_CLI_COMMAND = 'bundle';
handleBundle(argv, version);
}
)
Expand All @@ -277,13 +289,17 @@ yargs
choices: regionChoices,
},
}),
handleLogin
(argv) => {
process.env.REDOCLY_CLI_COMMAND = 'login';
handleLogin(argv);
}
)
.command(
'logout',
'Clear your stored credentials for the Redocly API registry.',
(yargs) => yargs,
async () => {
process.env.REDOCLY_CLI_COMMAND = 'logout';
const client = new RedoclyClient();
client.logout();
process.stdout.write('Logged out from the Redocly account. ✋\n');
Expand Down Expand Up @@ -330,7 +346,10 @@ yargs
type: 'string',
},
}),
previewDocs
(argv) => {
process.env.REDOCLY_CLI_COMMAND = 'preview-docs';
previewDocs(argv);
}
)
.completion('completion', 'Generate completion script.')
.demandCommand(1)
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/redocly/registry-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import { DEFAULT_REGION, DOMAINS } from '../config/config';
import { isNotEmptyObject } from '../utils';
const version = require('../../package.json').version;

export const currentCommand =
typeof process !== 'undefined' ? process.env?.REDOCLY_CLI_COMMAND || '' : '';

export class RegistryApi {
constructor(private accessTokens: AccessTokens, private region: Region) {}

Expand All @@ -27,7 +30,10 @@ export class RegistryApi {
}

private async request(path = '', options: RequestInit = {}, region?: Region) {
const headers = Object.assign({}, options.headers || {}, { 'x-redocly-cli-version': version });
const headers = Object.assign({}, options.headers || {}, {
'x-redocly-cli-version': version,
'user-agent': `redocly-cli / ${version} ${currentCommand}`,
});

if (!headers.hasOwnProperty('authorization')) {
throw new Error('Unauthorized');
Expand Down

1 comment on commit 96c33bf

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.
Category Percentage Covered / Total
🟡 Statements 68.71% 2890/4206
🟡 Branches 61.92% 1595/2576
🟡 Functions 60.13% 463/770
🟡 Lines 68.54% 2678/3907

Test suite run success

459 tests passing in 76 suites.

Report generated by 🧪jest coverage report action from 96c33bf

Please sign in to comment.