@@ -7,7 +7,7 @@ import packageJson from '../package.json';
77import tcpp from 'tcp-ping' ;
88import filesizeParser from 'filesize-parser' ;
99import { pricingPageUrl } from './utils' ;
10- import { Session } from 'types' ;
10+ import type { Session } from 'types' ;
1111import FormData from 'form-data' ;
1212
1313const tcpPing = util . promisify ( tcpp . ping ) ;
@@ -20,15 +20,13 @@ let host = process.env.PUSHY_REGISTRY || defaultEndpoint;
2020
2121const userAgent = `react-native-update-cli/${ packageJson . version } ` ;
2222
23- export const getSession = function ( ) {
24- return session ;
25- } ;
23+ export const getSession = ( ) => session ;
2624
27- export const replaceSession = function ( newSession : { token : string } ) {
25+ export const replaceSession = ( newSession : { token : string } ) => {
2826 session = newSession ;
2927} ;
3028
31- export const loadSession = async function ( ) {
29+ export const loadSession = async ( ) => {
3230 if ( fs . existsSync ( '.update' ) ) {
3331 try {
3432 replaceSession ( JSON . parse ( fs . readFileSync ( '.update' , 'utf8' ) ) ) ;
@@ -42,7 +40,7 @@ export const loadSession = async function () {
4240 }
4341} ;
4442
45- export const saveSession = function ( ) {
43+ export const saveSession = ( ) => {
4644 // Only save on change.
4745 if ( session !== savedSession ) {
4846 const current = session ;
@@ -52,7 +50,7 @@ export const saveSession = function () {
5250 }
5351} ;
5452
55- export const closeSession = function ( ) {
53+ export const closeSession = ( ) => {
5654 if ( fs . existsSync ( '.update' ) ) {
5755 fs . unlinkSync ( '.update' ) ;
5856 savedSession = undefined ;
@@ -64,38 +62,35 @@ export const closeSession = function () {
6462async function query ( url : string , options : fetch . RequestInit ) {
6563 const resp = await fetch ( url , options ) ;
6664 const text = await resp . text ( ) ;
67- let json ;
65+ let json : any ;
6866 try {
6967 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 ) { }
7769
7870 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 ) ;
8076 }
8177 return json ;
8278}
8379
8480function queryWithoutBody ( method : string ) {
85- return function ( api : string ) {
86- return query ( host + api , {
81+ return ( api : string ) =>
82+ query ( host + api , {
8783 method,
8884 headers : {
8985 'User-Agent' : userAgent ,
9086 'X-AccessToken' : session ? session . token : '' ,
9187 } ,
9288 } ) ;
93- } ;
9489}
9590
9691function 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 , {
9994 method,
10095 headers : {
10196 'User-Agent' : userAgent ,
@@ -104,10 +99,8 @@ function queryWithBody(method: string) {
10499 } ,
105100 body : JSON . stringify ( body ) ,
106101 } ) ;
107- } ;
108102}
109103
110- export const get = queryWithoutBody ( 'GET' ) ;
111104export const post = queryWithBody ( 'POST' ) ;
112105export const put = queryWithBody ( 'PUT' ) ;
113106export const doDelete = queryWithBody ( 'DELETE' ) ;
@@ -155,7 +148,7 @@ export async function uploadFile(fn: string, key?: string) {
155148 form . append ( k , v ) ;
156149 } ) ;
157150 const fileStream = fs . createReadStream ( fn ) ;
158- fileStream . on ( 'data' , function ( data ) {
151+ fileStream . on ( 'data' , ( data ) => {
159152 bar . tick ( data . length ) ;
160153 } ) ;
161154
0 commit comments