@wolf-scope/gamifykit
Version:
UI library for web applications using Lit
74 lines (65 loc) • 1.71 kB
text/typescript
import { UIAddonType } from "@/models";
export type SDKConfig = {
appToken: string;
theme?: Record<string, string>;
authToken?: string;
referenceId?: string;
apiBaseURL?: string;
additionalHeaders?: Record<string, string>;
ui: {
tagPrefix: string;
addons: UIAddonType[];
}
events?: {
connectionType: 'websocket' | 'polling';
wsBaseURL?: string;
pollInterval?: number;
};
debug?: 'log' | false;
};
let config: Readonly<SDKConfig> = {
appToken: '',
theme: {},
authToken: '',
referenceId: '',
debug: false,
events: {
connectionType: 'websocket',
wsBaseURL: '',
pollInterval: 5000,
},
ui: {
tagPrefix: 'gamifykit-',
addons: [],
}
};
export const debugLog = (message: string, data: any = null) => {
if (config.debug === 'log') {
if(data === null) {
return console.log(`[GamifyKit] ${message}`);
} else {
return console.debug(`[GamifyKit] ${message}`, data);
}
}
}
export const getConfig = () => {
return config;
}
export const setConfig = (newConfig: Partial<SDKConfig>) => {
config = { ...config, ...newConfig };
}
export const getApiHeaders = () => {
if (!config.authToken) throw new Error('SDK not initialized');
return {
'Accept': 'application/json',
'Content-Type': 'application/json',
...(config.authToken ? { Authorization: `Bearer ${config.authToken}` } : {}),
'X-Entity-Ref-Id': config.referenceId ?? '',
};
}
export const customFetch = async (url: string, options?: RequestInit) => {
return fetch(`${config.apiBaseURL}${url}`, {
...options,
headers: getApiHeaders(),
});
}