Skip to content

Commit eac79f1

Browse files
committed
feat(js-api-client): add staticAuthToken compliance
1 parent f6fd053 commit eac79f1

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@crystallize/js-api-client",
33
"license": "MIT",
4-
"version": "1.4.0",
4+
"version": "1.5.0",
55
"author": "Crystallize <[email protected]> (https://crystallize.com)",
66
"contributors": [
77
"Sébastien Morel <[email protected]>"

src/core/client.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export type ClientConfiguration = {
55
tenantId?: string;
66
accessTokenId?: string;
77
accessTokenSecret?: string;
8+
staticAuthToken?: string;
89
sessionId?: string;
910
origin?: string;
1011
};
@@ -21,6 +22,23 @@ export type ClientInterface = {
2122
config: Pick<ClientConfiguration, 'tenantIdentifier' | 'tenantId' | 'origin'>;
2223
};
2324

25+
function authenticationHeaders(config: ClientConfiguration) {
26+
if (config.sessionId) {
27+
return {
28+
Cookie: 'connect.sid=' + config.sessionId,
29+
};
30+
}
31+
if (config.staticAuthToken) {
32+
return {
33+
'X-Crystallize-Static-Auth-Token': config.staticAuthToken,
34+
};
35+
}
36+
return {
37+
'X-Crystallize-Access-Token-Id': config.accessTokenId || '',
38+
'X-Crystallize-Access-Token-Secret': config.accessTokenSecret || '',
39+
};
40+
}
41+
2442
async function post<T>(
2543
path: string,
2644
config: ClientConfiguration,
@@ -35,12 +53,7 @@ async function post<T>(
3553
};
3654
const headers = {
3755
...commonHeaders,
38-
...(config.sessionId
39-
? { Cookie: 'connect.sid=' + config.sessionId }
40-
: {
41-
'X-Crystallize-Access-Token-Id': config.accessTokenId || '',
42-
'X-Crystallize-Access-Token-Secret': config.accessTokenSecret || '',
43-
}),
56+
...authenticationHeaders(config),
4457
};
4558
const body = JSON.stringify({ query, variables });
4659

0 commit comments

Comments
 (0)