Skip to content

Commit f18b3b3

Browse files
committed
add api token
1 parent d4f1b83 commit f18b3b3

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-update-cli",
3-
"version": "2.6.0",
3+
"version": "2.7.0",
44
"description": "command line tool for react-native-update (remote updates for react native)",
55
"main": "index.js",
66
"bin": {

src/api.ts

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,41 @@ import tcpp from 'tcp-ping';
99
import { getBaseUrl } from 'utils/http-helper';
1010
import packageJson from '../package.json';
1111
import type { Package, Session } from './types';
12-
import { credentialFile, pricingPageUrl } from './utils/constants';
12+
import { credentialFile, pricingPageUrl, IS_CRESC } from './utils/constants';
1313
import { t } from './utils/i18n';
1414

1515
const tcpPing = util.promisify(tcpp.ping);
1616

1717
let session: Session | undefined;
1818
let savedSession: Session | undefined;
19+
let apiToken: string | undefined;
1920

2021
const userAgent = `react-native-update-cli/${packageJson.version}`;
2122

2223
export const getSession = () => session;
2324

25+
export const getApiToken = () => apiToken;
26+
27+
export const setApiToken = (token: string) => {
28+
apiToken = token;
29+
};
30+
31+
const loadApiTokenFromEnv = () => {
32+
// Use CRESC_API_TOKEN for cresc, PUSHY_API_TOKEN for pushy
33+
const envToken = IS_CRESC
34+
? process.env.CRESC_API_TOKEN
35+
: process.env.PUSHY_API_TOKEN;
36+
if (envToken) {
37+
apiToken = envToken;
38+
}
39+
};
40+
2441
export const replaceSession = (newSession: { token: string }) => {
2542
session = newSession;
2643
};
2744

2845
export const loadSession = async () => {
46+
loadApiTokenFromEnv();
2947
if (fs.existsSync(credentialFile)) {
3048
try {
3149
replaceSession(JSON.parse(fs.readFileSync(credentialFile, 'utf8')));
@@ -78,27 +96,39 @@ async function query(url: string, options: fetch.RequestInit) {
7896
}
7997

8098
function queryWithoutBody(method: string) {
81-
return (api: string) =>
82-
query(api, {
99+
return (api: string) => {
100+
const headers: Record<string, string> = {
101+
'User-Agent': userAgent,
102+
};
103+
if (apiToken) {
104+
headers['x-api-token'] = apiToken;
105+
} else if (session?.token) {
106+
headers['X-AccessToken'] = session.token;
107+
}
108+
return query(api, {
83109
method,
84-
headers: {
85-
'User-Agent': userAgent,
86-
'X-AccessToken': session ? session.token : '',
87-
},
110+
headers,
88111
});
112+
};
89113
}
90114

91115
function queryWithBody(method: string) {
92-
return (api: string, body?: Record<string, any>) =>
93-
query(api, {
116+
return (api: string, body?: Record<string, any>) => {
117+
const headers: Record<string, string> = {
118+
'User-Agent': userAgent,
119+
'Content-Type': 'application/json',
120+
};
121+
if (apiToken) {
122+
headers['x-api-token'] = apiToken;
123+
} else if (session?.token) {
124+
headers['X-AccessToken'] = session.token;
125+
}
126+
return query(api, {
94127
method,
95-
headers: {
96-
'User-Agent': userAgent,
97-
'Content-Type': 'application/json',
98-
'X-AccessToken': session ? session.token : '',
99-
},
128+
headers,
100129
body: JSON.stringify(body),
101130
});
131+
};
102132
}
103133

104134
export const get = queryWithoutBody('GET');

0 commit comments

Comments
 (0)