Skip to content

Commit abc4a2d

Browse files
add api key and client api id to axios header (#1087)
Co-authored-by: Nathan Ballantyne <[email protected]>
1 parent 422c386 commit abc4a2d

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,22 @@ If you run out of memory, set NODE_OPTIONS to limit Node's use of memory (this a
121121
export NODE_OPTIONS=--max-old-space-size=14366
122122
```
123123

124+
### API keys and Client App Id
125+
126+
API keys and Client App ID are used to authenticate and track usage respectively per partner/project/environment.
127+
128+
Once created from hub, they can be optionally passed into base config as followed:
129+
130+
```
131+
import { config } from '@imtbl/sdk';
132+
133+
const baseConfig = new config.ImmutableConfiguration({
134+
environment: config.Environment.PRODUCTION,
135+
clientAppId: '....',
136+
apiKey: '....',
137+
});
138+
```
139+
124140
### Linting
125141
126142
#### ESLint Tooling

packages/config/src/index.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,41 @@
1+
import axios from 'axios';
2+
13
export enum Environment {
24
PRODUCTION = 'production',
35
SANDBOX = 'sandbox',
46
}
57
export class ImmutableConfiguration {
68
readonly environment: Environment;
79

10+
readonly rateLimitingKey?: string;
11+
812
readonly apiKey?: string;
913

10-
constructor(options: { environment: Environment; apiKey?: string }) {
14+
readonly clientAppId?: string;
15+
16+
constructor(options: { environment: Environment; rateLimitingKey?: string; apiKey?: string; clientAppId?: string }) {
1117
this.environment = options.environment;
12-
this.apiKey = options.apiKey;
18+
19+
if (options.rateLimitingKey) {
20+
this.rateLimitingKey = options.rateLimitingKey;
21+
axios.defaults.headers.common['x-api-key'] = this.rateLimitingKey;
22+
}
23+
24+
if (options.apiKey) {
25+
if (!options.apiKey.startsWith('sk_imapik-')) {
26+
throw new Error('Invalid API key');
27+
}
28+
this.apiKey = options.apiKey;
29+
axios.defaults.headers.common['x-immutable-api-key'] = this.apiKey;
30+
}
31+
32+
if (options.clientAppId) {
33+
if (!options.clientAppId.startsWith('cai_imapik-')) {
34+
throw new Error('Invalid Client App Id');
35+
}
36+
this.clientAppId = options.clientAppId;
37+
axios.defaults.headers.common['x-immutable-client-app-id'] = this.clientAppId;
38+
}
1339
}
1440
}
1541

0 commit comments

Comments
 (0)