@@ -9,23 +9,41 @@ import tcpp from 'tcp-ping';
99import { getBaseUrl } from 'utils/http-helper' ;
1010import packageJson from '../package.json' ;
1111import type { Package , Session } from './types' ;
12- import { credentialFile , pricingPageUrl } from './utils/constants' ;
12+ import { credentialFile , pricingPageUrl , IS_CRESC } from './utils/constants' ;
1313import { t } from './utils/i18n' ;
1414
1515const tcpPing = util . promisify ( tcpp . ping ) ;
1616
1717let session : Session | undefined ;
1818let savedSession : Session | undefined ;
19+ let apiToken : string | undefined ;
1920
2021const userAgent = `react-native-update-cli/${ packageJson . version } ` ;
2122
2223export 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+
2441export const replaceSession = ( newSession : { token : string } ) => {
2542 session = newSession ;
2643} ;
2744
2845export 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
8098function 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
91115function 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
104134export const get = queryWithoutBody ( 'GET' ) ;
0 commit comments