Skip to content

Commit

Permalink
Merge pull request #96 from getamis/qubic-version
Browse files Browse the repository at this point in the history
Add customHeaders for x-qubic-client-version
  • Loading branch information
xxxooo authored May 2, 2024
2 parents cc34714 + 856dd9b commit 3d8888c
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 13 deletions.
3 changes: 3 additions & 0 deletions examples/example-pure-js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/QubicConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export class QubicConnect {
disableOpenExternalBrowserWhenLineIab = false,
trackGaSettings = [],
autoLoginInWalletIabType = 'enable',
customHeaders = {},
} = config;
if (!apiKey) {
throw Error('new QubicConnect should have key');
Expand All @@ -127,6 +128,7 @@ export class QubicConnect {
disableOpenExternalBrowserWhenLineIab,
trackGaSettings,
autoLoginInWalletIabType,
customHeaders,
};

QubicConnect.checkProviderOptions(config?.providerOptions);
Expand All @@ -135,6 +137,7 @@ export class QubicConnect {
apiKey,
apiSecret,
apiUrl,
customHeaders,
});

this.requestGraphql = createRequestGraphql({
Expand All @@ -148,6 +151,7 @@ export class QubicConnect {
apiKey,
apiSecret,
apiUrl: marketApiUrl,
customHeaders,
onUnauthenticated: () => this.handleLogout(null),
});

Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/types/QubicConnectConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export interface QubicConnectConfig {
// # fast login
// authRedirectUrl where have different type of wallet
authRedirectUrl?: string;

customHeaders?: Record<string, string>;
}

export interface InternalQubicConnectConfig extends Omit<Required<QubicConnectConfig>, 'providerOptions'> {
Expand Down
21 changes: 10 additions & 11 deletions packages/core/src/utils/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,16 @@ function parseGraphqlErrors(errorResponse: string): Array<{
}
}

type CreateRequestGraphqlProps = {
apiKey: string;
apiSecret: string;
apiUrl: string;
customHeaders?: Record<string, string>;
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
Expand Down Expand Up @@ -102,6 +100,7 @@ export const createRequestGraphql =
apiSecret,
body,
accessToken: getAccessToken(),
customHeaders,
});

return new Promise((resolve, reject) => {
Expand Down
12 changes: 10 additions & 2 deletions packages/core/src/utils/sdkFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,15 @@ export interface SdkFetch {
(path: string, init: RequestInit): Promise<Response>;
}

type CreateFetchProps = {
apiKey: string;
apiSecret: string;
apiUrl: string;
customHeaders?: Record<string, string>;
};

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}`;

Expand All @@ -44,15 +51,16 @@ export const createFetch =
apiKey,
apiSecret,
accessToken: getAccessToken(),
customHeaders,
});

const { headers: initHeaders, ...restInit } = init;

const response = await crossFetch(serviceUri, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
...serviceHeaders,
...initHeaders,
...serviceHeaders,
},
credentials: 'include',
...restInit,
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/utils/serviceHeaderBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type BuilderInput = {
apiSecret: string;
body?: BodyInit | null;
accessToken?: string | null;
customHeaders?: Record<string, string>;
};

const serviceHeaderBuilder = ({
Expand All @@ -17,6 +18,7 @@ const serviceHeaderBuilder = ({
apiKey,
body = '',
accessToken,
customHeaders = {},
}: BuilderInput): HeadersInit => {
if (!apiKey || !apiSecret) {
return {};
Expand All @@ -38,6 +40,7 @@ const serviceHeaderBuilder = ({
'Access-Control-Allow-Credentials': 'true',
Authorization: `Bearer ${accessToken}`,
}),
...customHeaders,
};
};

Expand Down

0 comments on commit 3d8888c

Please sign in to comment.