@@ -7,7 +7,7 @@ import packageJson from '../package.json';
7
7
import tcpp from 'tcp-ping' ;
8
8
import filesizeParser from 'filesize-parser' ;
9
9
import { pricingPageUrl } from './utils' ;
10
- import { Session } from 'types' ;
10
+ import type { Session } from 'types' ;
11
11
import FormData from 'form-data' ;
12
12
13
13
const tcpPing = util . promisify ( tcpp . ping ) ;
@@ -20,15 +20,13 @@ let host = process.env.PUSHY_REGISTRY || defaultEndpoint;
20
20
21
21
const userAgent = `react-native-update-cli/${ packageJson . version } ` ;
22
22
23
- export const getSession = function ( ) {
24
- return session ;
25
- } ;
23
+ export const getSession = ( ) => session ;
26
24
27
- export const replaceSession = function ( newSession : { token : string } ) {
25
+ export const replaceSession = ( newSession : { token : string } ) => {
28
26
session = newSession ;
29
27
} ;
30
28
31
- export const loadSession = async function ( ) {
29
+ export const loadSession = async ( ) => {
32
30
if ( fs . existsSync ( '.update' ) ) {
33
31
try {
34
32
replaceSession ( JSON . parse ( fs . readFileSync ( '.update' , 'utf8' ) ) ) ;
@@ -42,7 +40,7 @@ export const loadSession = async function () {
42
40
}
43
41
} ;
44
42
45
- export const saveSession = function ( ) {
43
+ export const saveSession = ( ) => {
46
44
// Only save on change.
47
45
if ( session !== savedSession ) {
48
46
const current = session ;
@@ -52,7 +50,7 @@ export const saveSession = function () {
52
50
}
53
51
} ;
54
52
55
- export const closeSession = function ( ) {
53
+ export const closeSession = ( ) => {
56
54
if ( fs . existsSync ( '.update' ) ) {
57
55
fs . unlinkSync ( '.update' ) ;
58
56
savedSession = undefined ;
@@ -64,38 +62,35 @@ export const closeSession = function () {
64
62
async function query ( url : string , options : fetch . RequestInit ) {
65
63
const resp = await fetch ( url , options ) ;
66
64
const text = await resp . text ( ) ;
67
- let json ;
65
+ let json : any ;
68
66
try {
69
67
json = JSON . parse ( text ) ;
70
- } catch ( e ) {
71
- if ( resp . statusText . includes ( 'Unauthorized' ) ) {
72
- throw new Error ( '登录信息已过期,请使用 pushy login 命令重新登录' ) ;
73
- } else {
74
- throw new Error ( `Server error: ${ resp . statusText } ` ) ;
75
- }
76
- }
68
+ } catch ( e ) { }
77
69
78
70
if ( resp . status !== 200 ) {
79
- throw new Error ( `${ resp . status } : ${ resp . statusText } ` ) ;
71
+ const message = json ?. message || resp . statusText ;
72
+ if ( resp . status === 401 ) {
73
+ throw new Error ( '登录信息已过期,请使用 pushy login 命令重新登录' ) ;
74
+ }
75
+ throw new Error ( message ) ;
80
76
}
81
77
return json ;
82
78
}
83
79
84
80
function queryWithoutBody ( method : string ) {
85
- return function ( api : string ) {
86
- return query ( host + api , {
81
+ return ( api : string ) =>
82
+ query ( host + api , {
87
83
method,
88
84
headers : {
89
85
'User-Agent' : userAgent ,
90
86
'X-AccessToken' : session ? session . token : '' ,
91
87
} ,
92
88
} ) ;
93
- } ;
94
89
}
95
90
96
91
function queryWithBody ( method : string ) {
97
- return function ( api : string , body : Record < string , any > ) {
98
- return query ( host + api , {
92
+ return ( api : string , body : Record < string , any > ) =>
93
+ query ( host + api , {
99
94
method,
100
95
headers : {
101
96
'User-Agent' : userAgent ,
@@ -104,10 +99,8 @@ function queryWithBody(method: string) {
104
99
} ,
105
100
body : JSON . stringify ( body ) ,
106
101
} ) ;
107
- } ;
108
102
}
109
103
110
- export const get = queryWithoutBody ( 'GET' ) ;
111
104
export const post = queryWithBody ( 'POST' ) ;
112
105
export const put = queryWithBody ( 'PUT' ) ;
113
106
export const doDelete = queryWithBody ( 'DELETE' ) ;
@@ -155,7 +148,7 @@ export async function uploadFile(fn: string, key?: string) {
155
148
form . append ( k , v ) ;
156
149
} ) ;
157
150
const fileStream = fs . createReadStream ( fn ) ;
158
- fileStream . on ( 'data' , function ( data ) {
151
+ fileStream . on ( 'data' , ( data ) => {
159
152
bar . tick ( data . length ) ;
160
153
} ) ;
161
154
0 commit comments