Skip to content

Commit

Permalink
feat: add headers
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanVor committed Dec 30, 2024
1 parent 364e0c7 commit a5a246f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@
"generate_services": "ts-node scripts/generate_services",
"clear_generated_services": "ts-node scripts/clear_generated_services",
"build:services": "ts-node scripts/build_services",
"build:mono-service": "cross-env NODE_OPTIONS=\"--max-old-space-size=4096\" tsc -p .",
"generate_default_headers": "ts-node scripts/generate_default_headers",
"build:mono-service": "npm run generate_default_headers && cross-env NODE_OPTIONS=\"--max-old-space-size=4096\" tsc -p .",
"build": "npm run build:mono-service && npm run build:services",
"typecheck:clients": "tsc -p ./clients --noEmit"
},
Expand Down
16 changes: 16 additions & 0 deletions scripts/generate_default_headers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { readFileSync, writeFileSync } from 'fs';
import { resolve } from 'path';

const packageJSONPath = resolve('package.json');
const data = readFileSync(packageJSONPath, 'utf8');
const jsonData = JSON.parse(data);

const defaultHeadersPath = resolve('./src/default-headers.ts');
writeFileSync(
defaultHeadersPath,
`export const DEFAULT_HEADERS = {
['x-proxy-agent']: '${jsonData.name}',
['x-proxy-agent-version']: '${jsonData.version}',
};\n`,
'utf8',
);
4 changes: 4 additions & 0 deletions src/default-headers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const DEFAULT_HEADERS = {
['x-proxy-agent']: '@yandex-cloud/nodejs-sdk',
['x-proxy-agent-version']: 'undefined',
};
10 changes: 6 additions & 4 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import {
IamTokenServiceClient,
} from './generated/yandex/cloud/iam/v1/iam_token_service';

import { DEFAULT_HEADERS } from './default-headers';
import { merge } from 'lodash';

const isOAuth = (config: SessionConfig): config is OAuthCredentialsConfig => 'oauthToken' in config;

const isIamToken = (config: SessionConfig): config is IamTokenCredentialsConfig =>
Expand Down Expand Up @@ -100,13 +103,12 @@ export class Session {

private static readonly DEFAULT_CONFIG = {
pollInterval: 1000,
headers: DEFAULT_HEADERS,
};

constructor(config?: SessionConfig) {
this.config = {
...Session.DEFAULT_CONFIG,
...config,
};
this.config = merge(Session.DEFAULT_CONFIG, config);

this.tokenCreator = newTokenCreator(this.config);
this.channelCredentials = newChannelCredentials(
this.tokenCreator,
Expand Down

0 comments on commit a5a246f

Please sign in to comment.