-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathactionResponse.js
More file actions
128 lines (107 loc) · 3.29 KB
/
Copy pathactionResponse.js
File metadata and controls
128 lines (107 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { ActionErrorType, createActionError } from '../types.js'
import { normalizeActionConfig } from './actionConfig.js'
const parseMaybeJson = (value) => {
if (typeof value !== 'string') {
return value
}
try {
return JSON.parse(value)
} catch (error) {
return value
}
}
export const resolveActionResult = (result) => {
let resolved = parseMaybeJson(result)
if (resolved && typeof resolved === 'object' && typeof resolved.action === 'string') {
resolved = {
...resolved,
action: parseMaybeJson(resolved.action)
}
}
return resolved
}
export const handleActionResponse = async (result, callbacks = {}) => {
const {
onToast,
onNextAction,
onSpecialAction,
onKeep,
onClose,
onSuccess,
onError
} = callbacks
if (result == null) {
await onSuccess?.(result)
await onClose?.()
return { handled: true, type: 'empty' }
}
const resolved = resolveActionResult(result)
if (typeof resolved === 'string') {
onToast?.(resolved, 'success')
await onSuccess?.(resolved)
await onClose?.()
return { handled: true, type: 'string' }
}
if (!resolved || typeof resolved !== 'object') {
await onSuccess?.(resolved)
await onClose?.()
return { handled: true, type: 'primitive' }
}
if (resolved.error) {
const error = createActionError(ActionErrorType.NETWORK_ERROR, resolved.error)
onError?.(error)
throw error
}
if (resolved.toast) {
onToast?.(resolved.toast, 'success')
}
if (resolved.message || resolved.msg) {
onToast?.(resolved.message || resolved.msg, 'success')
}
if (resolved.action) {
const nextAction = normalizeActionConfig(resolved.action, { ensureActionId: true, actionIdPrefix: 'next' })
await onNextAction?.(nextAction)
return { handled: true, type: 'next-action', action: nextAction }
}
if (resolved.actionId) {
if (resolved.actionId === '__keep__') {
await onKeep?.(resolved)
} else {
await onSpecialAction?.(resolved)
}
return { handled: true, type: 'special-action', action: resolved }
}
if (resolved.code !== undefined) {
if (resolved.code === 0 || resolved.code === 200) {
const data = resolveActionResult(resolved.data)
if (data && typeof data === 'object') {
if (data.action) {
const nextAction = normalizeActionConfig(data.action, { ensureActionId: true, actionIdPrefix: 'data' })
await onNextAction?.(nextAction)
return { handled: true, type: 'data-action', action: nextAction }
}
if (data.actionId) {
if (data.actionId === '__keep__') {
await onKeep?.(data)
} else {
await onSpecialAction?.(data)
}
return { handled: true, type: 'data-special-action', action: data }
}
}
onToast?.(resolved.message || resolved.msg || '操作成功', 'success')
await onSuccess?.(resolved)
await onClose?.()
return { handled: true, type: 'code-success' }
}
const error = createActionError(
ActionErrorType.NETWORK_ERROR,
resolved.message || resolved.msg || `操作失败,错误码: ${resolved.code}`
)
onError?.(error)
throw error
}
await onSuccess?.(resolved)
await onClose?.()
return { handled: true, type: 'object-success' }
}