Skip to content

Commit

Permalink
chore: remove type level complexities on api keys (#1521)
Browse files Browse the repository at this point in the history
  • Loading branch information
shineli1984 authored Feb 22, 2024
1 parent ed9a745 commit b14b299
Showing 1 changed file with 2 additions and 39 deletions.
41 changes: 2 additions & 39 deletions packages/config/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import axios from 'axios';
import { track, setEnvironment, setPublishableApiKey } from '@imtbl/metrics';

export enum Environment {
Expand Down Expand Up @@ -55,6 +54,7 @@ export const addKeysToHeadersOverride = <T extends { headers?: Record<string, st

if (baseConfig.publishableKey) {
newHeaders[KeyHeaders.PUBLISHABLE_KEY] = baseConfig.publishableKey;
setPublishableApiKey(baseConfig.publishableKey);
}

if (baseConfig.rateLimitingKey) {
Expand All @@ -78,44 +78,7 @@ export const addKeysToHeadersOverride = <T extends { headers?: Record<string, st
} as T;
};

const API_KEY_PREFIX = 'sk_imapik-';
const PUBLISHABLE_KEY_PREFIX = 'pk_imapik-';

export const addApiKeyToAxiosHeader = (apiKey: string) => {
if (!apiKey.startsWith(API_KEY_PREFIX)) {
throw new Error(
'Invalid API key. Create your api key in Immutable developer hub. https://hub.immutable.com',
);
}
axios.defaults.headers.common[KeyHeaders.API_KEY] = apiKey;
};

export const addPublishableKeyToAxiosHeader = (publishableKey: string) => {
if (!publishableKey.startsWith(PUBLISHABLE_KEY_PREFIX)) {
throw new Error(
'Invalid Publishable key. Create your Publishable key in Immutable developer hub.'
+ ' https://hub.immutable.com',
);
}
setPublishableApiKey(publishableKey);
axios.defaults.headers.common[KeyHeaders.PUBLISHABLE_KEY] = publishableKey;
};

export const addRateLimitingKeyToAxiosHeader = (rateLimitingKey: string) => {
axios.defaults.headers.common[KeyHeaders.RATE_LIMITING_KEY] = rateLimitingKey;
};

type ImmutableConfigurationWithRequireableFields<T> = ImmutableConfiguration &
(T extends { apiKey: 'required' } ? Required<{ apiKey: string }> : {}) &
(T extends { publishableKey: 'required' }
? Required<{ publishableKey: string }>
: {});

type ImmutableConfigurationWithOmitableFields<T> = T extends { apiKey: 'omit' }
? Omit<ImmutableConfigurationWithRequireableFields<T>, 'apiKey'>
: ImmutableConfigurationWithRequireableFields<T>;

export interface ModuleConfiguration<T> {
baseConfig: ImmutableConfigurationWithOmitableFields<T>;
baseConfig: ImmutableConfiguration;
overrides?: T;
}

0 comments on commit b14b299

Please sign in to comment.