diff --git a/examples/example-pure-js/src/index.ts b/examples/example-pure-js/src/index.ts index d18389f..bc0513f 100644 --- a/examples/example-pure-js/src/index.ts +++ b/examples/example-pure-js/src/index.ts @@ -78,6 +78,9 @@ async function main() { provider: wcProvider as any, }, }, + customHeaders: { + 'x-qubic-client-version': '0.0.0-alpha.0', + }, }; const qubicConnect = new QubicConnect(SDK_CONFIG); diff --git a/packages/core/src/QubicConnect.ts b/packages/core/src/QubicConnect.ts index 4d63335..5cd9498 100644 --- a/packages/core/src/QubicConnect.ts +++ b/packages/core/src/QubicConnect.ts @@ -105,6 +105,7 @@ export class QubicConnect { disableOpenExternalBrowserWhenLineIab = false, trackGaSettings = [], autoLoginInWalletIabType = 'enable', + customHeaders = {}, } = config; if (!apiKey) { throw Error('new QubicConnect should have key'); @@ -127,6 +128,7 @@ export class QubicConnect { disableOpenExternalBrowserWhenLineIab, trackGaSettings, autoLoginInWalletIabType, + customHeaders, }; QubicConnect.checkProviderOptions(config?.providerOptions); @@ -135,6 +137,7 @@ export class QubicConnect { apiKey, apiSecret, apiUrl, + customHeaders, }); this.requestGraphql = createRequestGraphql({ @@ -148,6 +151,7 @@ export class QubicConnect { apiKey, apiSecret, apiUrl: marketApiUrl, + customHeaders, onUnauthenticated: () => this.handleLogout(null), }); diff --git a/packages/core/src/types/QubicConnectConfig.ts b/packages/core/src/types/QubicConnectConfig.ts index 9f8fb33..87c1d3a 100644 --- a/packages/core/src/types/QubicConnectConfig.ts +++ b/packages/core/src/types/QubicConnectConfig.ts @@ -20,6 +20,8 @@ export interface QubicConnectConfig { // # fast login // authRedirectUrl where have different type of wallet authRedirectUrl?: string; + + customHeaders?: Record; } export interface InternalQubicConnectConfig extends Omit, 'providerOptions'> { diff --git a/packages/core/src/utils/graphql.ts b/packages/core/src/utils/graphql.ts index 903cd30..5f1ff0e 100644 --- a/packages/core/src/utils/graphql.ts +++ b/packages/core/src/utils/graphql.ts @@ -63,18 +63,16 @@ function parseGraphqlErrors(errorResponse: string): Array<{ } } +type CreateRequestGraphqlProps = { + apiKey: string; + apiSecret: string; + apiUrl: string; + customHeaders?: Record; + onUnauthenticated: () => void; +}; + export const createRequestGraphql = - ({ - apiKey, - apiSecret, - apiUrl, - onUnauthenticated, - }: { - apiKey: string; - apiSecret: string; - apiUrl: string; - onUnauthenticated: () => void; - }): SdkRequestGraphql => + ({ apiKey, apiSecret, apiUrl, customHeaders, onUnauthenticated }: CreateRequestGraphqlProps): SdkRequestGraphql => ({ query, variables, path = '' }) => { // currently, we have 3 graphql endpoints: // origin api url has public and acc @@ -102,6 +100,7 @@ export const createRequestGraphql = apiSecret, body, accessToken: getAccessToken(), + customHeaders, }); return new Promise((resolve, reject) => { diff --git a/packages/core/src/utils/sdkFetch.ts b/packages/core/src/utils/sdkFetch.ts index 53a0402..b9d6be7 100644 --- a/packages/core/src/utils/sdkFetch.ts +++ b/packages/core/src/utils/sdkFetch.ts @@ -32,8 +32,15 @@ export interface SdkFetch { (path: string, init: RequestInit): Promise; } +type CreateFetchProps = { + apiKey: string; + apiSecret: string; + apiUrl: string; + customHeaders?: Record; +}; + export const createFetch = - ({ apiKey, apiSecret, apiUrl }: { apiKey: string; apiSecret: string; apiUrl: string }): SdkFetch => + ({ apiKey, apiSecret, apiUrl, customHeaders }: CreateFetchProps): SdkFetch => async (path, init) => { const serviceUri = `${apiUrl}/${path}`; @@ -44,6 +51,7 @@ export const createFetch = apiKey, apiSecret, accessToken: getAccessToken(), + customHeaders, }); const { headers: initHeaders, ...restInit } = init; @@ -51,8 +59,8 @@ export const createFetch = const response = await crossFetch(serviceUri, { headers: { 'Content-Type': 'application/x-www-form-urlencoded', - ...serviceHeaders, ...initHeaders, + ...serviceHeaders, }, credentials: 'include', ...restInit, diff --git a/packages/core/src/utils/serviceHeaderBuilder.ts b/packages/core/src/utils/serviceHeaderBuilder.ts index 193caa0..53b96ac 100644 --- a/packages/core/src/utils/serviceHeaderBuilder.ts +++ b/packages/core/src/utils/serviceHeaderBuilder.ts @@ -8,6 +8,7 @@ type BuilderInput = { apiSecret: string; body?: BodyInit | null; accessToken?: string | null; + customHeaders?: Record; }; const serviceHeaderBuilder = ({ @@ -17,6 +18,7 @@ const serviceHeaderBuilder = ({ apiKey, body = '', accessToken, + customHeaders = {}, }: BuilderInput): HeadersInit => { if (!apiKey || !apiSecret) { return {}; @@ -38,6 +40,7 @@ const serviceHeaderBuilder = ({ 'Access-Control-Allow-Credentials': 'true', Authorization: `Bearer ${accessToken}`, }), + ...customHeaders, }; };