From 8f0ca60d24f798aedf503cb907b3406932b8327a Mon Sep 17 00:00:00 2001 From: sunnylqm Date: Thu, 9 Jan 2025 20:58:06 +0800 Subject: [PATCH] fix error message --- src/api.ts | 43 ++++++++++++++++++------------------------- src/app.js | 12 +++++++----- src/user.js | 8 ++++---- 3 files changed, 29 insertions(+), 34 deletions(-) diff --git a/src/api.ts b/src/api.ts index 21fbc69..5bea072 100644 --- a/src/api.ts +++ b/src/api.ts @@ -7,7 +7,7 @@ import packageJson from '../package.json'; import tcpp from 'tcp-ping'; import filesizeParser from 'filesize-parser'; import { pricingPageUrl } from './utils'; -import { Session } from 'types'; +import type { Session } from 'types'; import FormData from 'form-data'; const tcpPing = util.promisify(tcpp.ping); @@ -20,15 +20,13 @@ let host = process.env.PUSHY_REGISTRY || defaultEndpoint; const userAgent = `react-native-update-cli/${packageJson.version}`; -export const getSession = function () { - return session; -}; +export const getSession = () => session; -export const replaceSession = function (newSession: { token: string }) { +export const replaceSession = (newSession: { token: string }) => { session = newSession; }; -export const loadSession = async function () { +export const loadSession = async () => { if (fs.existsSync('.update')) { try { replaceSession(JSON.parse(fs.readFileSync('.update', 'utf8'))); @@ -42,7 +40,7 @@ export const loadSession = async function () { } }; -export const saveSession = function () { +export const saveSession = () => { // Only save on change. if (session !== savedSession) { const current = session; @@ -52,7 +50,7 @@ export const saveSession = function () { } }; -export const closeSession = function () { +export const closeSession = () => { if (fs.existsSync('.update')) { fs.unlinkSync('.update'); savedSession = undefined; @@ -64,38 +62,35 @@ export const closeSession = function () { async function query(url: string, options: fetch.RequestInit) { const resp = await fetch(url, options); const text = await resp.text(); - let json; + let json: any; try { json = JSON.parse(text); - } catch (e) { - if (resp.statusText.includes('Unauthorized')) { - throw new Error('登录信息已过期,请使用 pushy login 命令重新登录'); - } else { - throw new Error(`Server error: ${resp.statusText}`); - } - } + } catch (e) {} if (resp.status !== 200) { - throw new Error(`${resp.status}: ${resp.statusText}`); + const message = json?.message || resp.statusText; + if (resp.status === 401) { + throw new Error('登录信息已过期,请使用 pushy login 命令重新登录'); + } + throw new Error(message); } return json; } function queryWithoutBody(method: string) { - return function (api: string) { - return query(host + api, { + return (api: string) => + query(host + api, { method, headers: { 'User-Agent': userAgent, 'X-AccessToken': session ? session.token : '', }, }); - }; } function queryWithBody(method: string) { - return function (api: string, body: Record) { - return query(host + api, { + return (api: string, body: Record) => + query(host + api, { method, headers: { 'User-Agent': userAgent, @@ -104,10 +99,8 @@ function queryWithBody(method: string) { }, body: JSON.stringify(body), }); - }; } -export const get = queryWithoutBody('GET'); export const post = queryWithBody('POST'); export const put = queryWithBody('PUT'); export const doDelete = queryWithBody('DELETE'); @@ -155,7 +148,7 @@ export async function uploadFile(fn: string, key?: string) { form.append(k, v); }); const fileStream = fs.createReadStream(fn); - fileStream.on('data', function (data) { + fileStream.on('data', (data) => { bar.tick(data.length); }); diff --git a/src/app.js b/src/app.js index eddfae0..df8521e 100644 --- a/src/app.js +++ b/src/app.js @@ -1,5 +1,5 @@ import { question } from './utils'; -import fs from 'fs'; +import fs from 'node:fs'; import Table from 'tty-table'; import { post, get, doDelete } from './api'; @@ -84,7 +84,7 @@ export const commands = { options: { platform, downloadUrl }, }); }, - deleteApp: async function ({ args, options }) { + deleteApp: async ({ args, options }) => { const { platform } = options; const id = args[0] || chooseApp(platform); if (!id) { @@ -93,15 +93,17 @@ export const commands = { await doDelete(`/app/${id}`); console.log('操作成功'); }, - apps: async function ({ options }) { + apps: async ({ options }) => { const { platform } = options; listApp(platform); }, - selectApp: async function ({ args, options }) { + selectApp: async ({ args, options }) => { const platform = checkPlatform( options.platform || (await question('平台(ios/android/harmony):')), ); - const id = args[0] ? parseInt(args[0]) : (await chooseApp(platform)).id; + const id = args[0] + ? Number.parseInt(args[0]) + : (await chooseApp(platform)).id; let updateInfo = {}; if (fs.existsSync('update.json')) { diff --git a/src/user.js b/src/user.js index 39cf651..563d318 100644 --- a/src/user.js +++ b/src/user.js @@ -1,13 +1,13 @@ import { question } from './utils'; import { post, get, replaceSession, saveSession, closeSession } from './api'; -import crypto from 'crypto'; +import crypto from 'node:crypto'; function md5(str) { return crypto.createHash('md5').update(str).digest('hex'); } export const commands = { - login: async function ({ args }) { + login: async ({ args }) => { const email = args[0] || (await question('email:')); const pwd = args[1] || (await question('password:', true)); const { token, info } = await post('/user/login', { @@ -18,11 +18,11 @@ export const commands = { await saveSession(); console.log(`欢迎使用 pushy 热更新服务, ${info.name}.`); }, - logout: async function () { + logout: async () => { await closeSession(); console.log('已退出登录'); }, - me: async function () { + me: async () => { const me = await get('/user/me'); for (const k in me) { if (k !== 'ok') {