Skip to content

Commit 1f4d4ea

Browse files
committed
feat: stringify the result from api controller
1 parent e52ea69 commit 1f4d4ea

4 files changed

Lines changed: 59 additions & 54 deletions

File tree

packages/neuron-ui/src/services/remote/apiMethodWrapper.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// TODO: use error code
21
interface SuccessFromController {
32
status: 1
43
result: any
@@ -12,6 +11,7 @@ interface FailureFromController {
1211
meta?: { [key: string]: string }
1312
}
1413
}
14+
1515
export type ControllerResponse = SuccessFromController | FailureFromController
1616

1717
export const RemoteNotLoadError = {
@@ -22,15 +22,7 @@ export const RemoteNotLoadError = {
2222
}
2323

2424
export const apiMethodWrapper = <T = any>(
25-
callControllerMethod: (
26-
controller: any
27-
) => (
28-
params: T
29-
) => Promise<{
30-
status: any
31-
result: any
32-
message: { code?: number; content?: string; meta?: { [key: string]: string } }
33-
}>
25+
callControllerMethod: (controller: any) => (params: T) => Promise<string>
3426
) => async (realParams: T): Promise<ControllerResponse> => {
3527
if (!window.remote) {
3628
return RemoteNotLoadError
@@ -44,7 +36,16 @@ export const apiMethodWrapper = <T = any>(
4436
},
4537
}
4638
}
47-
const res = await callControllerMethod(controller)(realParams)
39+
40+
const res: SuccessFromController | FailureFromController = await callControllerMethod(controller)(realParams)
41+
.then(stringifiedRes => (stringifiedRes ? JSON.parse(stringifiedRes) : stringifiedRes))
42+
.catch(() => ({
43+
status: 0,
44+
message: {
45+
content: 'Invalid response format',
46+
},
47+
}))
48+
4849
if (process.env.NODE_ENV === 'development' && window.localStorage.getItem('log-response')) {
4950
console.group('api controller')
5051
console.info(JSON.stringify(res, null, 2))

packages/neuron-wallet/src/controllers/api.ts

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import WalletsService from 'services/wallets'
1717
import SkipDataAndType from 'services/settings/skip-data-and-type'
1818
import { ConnectionStatusSubject } from 'models/subjects/node'
1919
import { SystemScriptSubject } from 'models/subjects/system-script'
20-
import { CatchControllerError } from 'decorators/errors'
20+
import { MapApiResponse } from 'decorators'
2121
import { ResponseCode } from 'utils/const'
2222
import { TransactionWithoutHash } from 'types/cell-types'
2323

@@ -27,7 +27,9 @@ import { TransactionWithoutHash } from 'types/cell-types'
2727
*/
2828
export default class ApiController {
2929
// App
30-
public static loadInitData = async () => {
30+
31+
@MapApiResponse
32+
public static async loadInitData() {
3133
const walletsService = WalletsService.getInstance()
3234
const networksService = NetworksService.getInstance()
3335
const [
@@ -114,69 +116,71 @@ export default class ApiController {
114116
return { status: ResponseCode.Success, result: initState }
115117
}
116118

117-
public static handleViewError = (error: string) => {
119+
@MapApiResponse
120+
public static handleViewError(error: string) {
118121
if (env.isDevMode) {
119122
console.error(error)
120123
}
121124
}
122125

126+
@MapApiResponse
123127
public static async contextMenu(params: { type: string; id: string }) {
124128
return popContextMenu(params)
125129
}
126130

127131
// Wallets
128132

129-
@CatchControllerError
133+
@MapApiResponse
130134
public static async getAllWallets() {
131135
return WalletsController.getAll()
132136
}
133137

134-
@CatchControllerError
138+
@MapApiResponse
135139
public static async getCurrentWallet() {
136140
return WalletsController.getCurrent()
137141
}
138142

139-
@CatchControllerError
140-
public static async importMnemonic(params: { name: string, password: string, mnemonic: string }) {
143+
@MapApiResponse
144+
public static async importMnemonic(params: { name: string; password: string; mnemonic: string }) {
141145
return WalletsController.importMnemonic(params)
142146
}
143147

144-
@CatchControllerError
145-
public static async importKeystore(params: { name: string, password: string, keystorePath: string }) {
148+
@MapApiResponse
149+
public static async importKeystore(params: { name: string; password: string; keystorePath: string }) {
146150
return WalletsController.importKeystore(params)
147151
}
148152

149-
@CatchControllerError
150-
public static async createWallet(params: { name: string, password: string, mnemonic: string }) {
153+
@MapApiResponse
154+
public static async createWallet(params: { name: string; password: string; mnemonic: string }) {
151155
return WalletsController.create(params)
152156
}
153157

154-
@CatchControllerError
155-
public static async updateWallet(params: { id: string, password: string, name: string, newPassword?: string }) {
156-
WalletsController.update(params)
158+
@MapApiResponse
159+
public static async updateWallet(params: { id: string; password: string; name: string; newPassword?: string }) {
160+
return WalletsController.update(params)
157161
}
158162

159-
@CatchControllerError
163+
@MapApiResponse
160164
public static async deleteWallet({ id = '', password = '' }) {
161165
return WalletsController.delete({ id, password })
162166
}
163167

164-
@CatchControllerError
168+
@MapApiResponse
165169
public static async backupWallet({ id = '', password = '' }) {
166170
return WalletsController.backup({ id, password })
167171
}
168172

169-
@CatchControllerError
173+
@MapApiResponse
170174
public static async setCurrentWallet(id: string) {
171175
return WalletsController.activate(id)
172176
}
173177

174-
@CatchControllerError
178+
@MapApiResponse
175179
public static async getAddressesByWalletID(id: string) {
176180
return WalletsController.getAllAddresses(id)
177181
}
178182

179-
@CatchControllerError
183+
@MapApiResponse
180184
public static async sendCapacity(params: {
181185
id: string
182186
walletID: string
@@ -191,17 +195,17 @@ export default class ApiController {
191195
return WalletsController.sendCapacity(params)
192196
}
193197

194-
@CatchControllerError
198+
@MapApiResponse
195199
public static async sendTx(params: {
196200
walletID: string
197-
tx: TransactionWithoutHash,
201+
tx: TransactionWithoutHash
198202
password: string
199203
description?: string
200204
}) {
201205
return WalletsController.sendTx(params)
202206
}
203207

204-
@CatchControllerError
208+
@MapApiResponse
205209
public static async generateTx(params: {
206210
walletID: string
207211
items: {
@@ -214,12 +218,12 @@ export default class ApiController {
214218
return WalletsController.generateTx(params)
215219
}
216220

217-
@CatchControllerError
221+
@MapApiResponse
218222
public static async computeCycles(params: { walletID: string; capacities: string }) {
219223
return WalletsController.computeCycles(params)
220224
}
221225

222-
@CatchControllerError
226+
@MapApiResponse
223227
public static async updateAddressDescription(params: {
224228
walletID: string
225229
address: string
@@ -230,46 +234,46 @@ export default class ApiController {
230234

231235
// Networks
232236

233-
@CatchControllerError
237+
@MapApiResponse
234238
public static async getAllNetworks() {
235239
return NetworksController.getAll()
236240
}
237241

238-
@CatchControllerError
242+
@MapApiResponse
239243
public static async createNetwork({ name, remote, type = NetworkType.Normal, chain = 'ckb' }: Network) {
240244
return NetworksController.create({ name, remote, type, chain })
241245
}
242246

243-
@CatchControllerError
247+
@MapApiResponse
244248
public static async updateNetwork(id: NetworkID, options: Partial<Network>) {
245249
return NetworksController.update(id, options)
246250
}
247251

248-
@CatchControllerError
252+
@MapApiResponse
249253
public static async getCurrentNetworkID() {
250254
return NetworksController.currentID()
251255
}
252256

253-
@CatchControllerError
257+
@MapApiResponse
254258
public static async setCurrentNetowrk(id: NetworkID) {
255259
return NetworksController.activate(id)
256260
}
257261

258262
// Transactions
259263

260-
@CatchControllerError
264+
@MapApiResponse
261265
public static async getTransactionList(
262266
params: Controller.Params.TransactionsByKeywords,
263267
) {
264268
return TransactionsController.getAllByKeywords(params)
265269
}
266270

267-
@CatchControllerError
271+
@MapApiResponse
268272
public static async getTransaction(walletID: string, hash: string) {
269273
return TransactionsController.get(walletID, hash)
270274
}
271275

272-
@CatchControllerError
276+
@MapApiResponse
273277
public static async updateTransactionDescription(params: { hash: string; description: string }) {
274278
return TransactionsController.updateDescription(params)
275279
}
@@ -280,7 +284,7 @@ export default class ApiController {
280284

281285
// Misc
282286

283-
@CatchControllerError
287+
@MapApiResponse
284288
public static async updateSkipDataAndType(skip: boolean) {
285289
return SkipDataAndTypeController.update(skip)
286290
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import errorDecorators from './errors'
1+
import mappers from './mappers'
22
import validatorDecorators from './validators'
33

4-
export const { CatchControllerError } = errorDecorators
4+
export const { MapApiResponse } = mappers
55
export const { Validate, Password, Required } = validatorDecorators
66

77
export default {
8-
...errorDecorators,
8+
...mappers,
99
...validatorDecorators,
1010
}

packages/neuron-wallet/src/decorators/errors.ts renamed to packages/neuron-wallet/src/decorators/mappers.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@ import logger from 'utils/logger'
33

44
const NODE_DISCONNECTED_CODE = 104
55

6-
export const CatchControllerError = (target: any, name: string, descriptor: PropertyDescriptor) => {
6+
export const MapApiResponse = (target: any, name: string, descriptor: PropertyDescriptor) => {
77
const originalMethod = descriptor.value
88
return {
99
...descriptor,
10-
async value(...args: any[]) {
10+
async value(...args: any[]): Promise<any> {
1111
try {
12-
return await originalMethod(...args)
12+
const res = await originalMethod(...args)
13+
return JSON.stringify(res)
1314
} catch (err) {
1415
logger.error(`${target.name}.${name}:`, err)
1516
if (err.code === 'ECONNREFUSED') {
1617
err.code = NODE_DISCONNECTED_CODE
1718
}
18-
return {
19+
const res = {
1920
status: err.code || ResponseCode.Fail,
2021
message: typeof err.message === 'string' ? { content: err.message } : err.message,
2122
}
23+
return JSON.stringify(res)
2224
}
2325
},
2426
}
2527
}
2628

27-
export default {
28-
CatchControllerError,
29-
}
29+
export default { MapApiResponse }

0 commit comments

Comments
 (0)