@artisan-cloud/plugin-framework-admin 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -12,6 +12,8 @@ export interface PowerXCapabilityRequest {
|
|
|
12
12
|
signal?: AbortSignal
|
|
13
13
|
apiBase?: string
|
|
14
14
|
endpoint?: string
|
|
15
|
+
preferredProtocol?: string
|
|
16
|
+
metadata?: Record<string, any>
|
|
15
17
|
}
|
|
16
18
|
|
|
17
19
|
export interface PowerXCapabilityResponse {
|
|
@@ -19,19 +21,26 @@ export interface PowerXCapabilityResponse {
|
|
|
19
21
|
status?: string
|
|
20
22
|
data?: Record<string, any> | null
|
|
21
23
|
errors?: Record<string, any> | Record<string, any>[] | null
|
|
24
|
+
warnings?: string[] | null
|
|
25
|
+
raw?: Record<string, any> | Record<string, any>[] | string | null
|
|
22
26
|
}
|
|
23
27
|
|
|
24
28
|
export class PowerXCapabilityBridgeError extends Error {
|
|
25
29
|
status?: number
|
|
26
30
|
traceId?: string
|
|
27
31
|
details?: any
|
|
32
|
+
warnings?: string[]
|
|
28
33
|
|
|
29
|
-
constructor(
|
|
34
|
+
constructor(
|
|
35
|
+
message: string,
|
|
36
|
+
opts: { status?: number; traceId?: string; details?: any; warnings?: string[] } = {}
|
|
37
|
+
) {
|
|
30
38
|
super(message)
|
|
31
39
|
this.name = 'PowerXCapabilityBridgeError'
|
|
32
40
|
this.status = opts.status
|
|
33
41
|
this.traceId = opts.traceId
|
|
34
42
|
this.details = opts.details
|
|
43
|
+
this.warnings = opts.warnings
|
|
35
44
|
}
|
|
36
45
|
}
|
|
37
46
|
|
|
@@ -54,6 +63,50 @@ const combineURL = (base?: string, endpoint?: string) => {
|
|
|
54
63
|
return `${normalizedBase}${normalizedEndpoint}`
|
|
55
64
|
}
|
|
56
65
|
|
|
66
|
+
const pickStatus = (payload: Record<string, any>) => {
|
|
67
|
+
const topLevel = typeof payload.status === 'string' ? payload.status.trim() : ''
|
|
68
|
+
if (topLevel) {
|
|
69
|
+
return topLevel
|
|
70
|
+
}
|
|
71
|
+
const dataStatus = typeof payload?.data?.status === 'string' ? payload.data.status.trim() : ''
|
|
72
|
+
if (dataStatus) {
|
|
73
|
+
return dataStatus
|
|
74
|
+
}
|
|
75
|
+
const rawStatus = typeof payload?.raw?.status === 'string' ? payload.raw.status.trim() : ''
|
|
76
|
+
if (rawStatus) {
|
|
77
|
+
return rawStatus
|
|
78
|
+
}
|
|
79
|
+
return ''
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const pickRaw = (payload: Record<string, any>) => {
|
|
83
|
+
if (payload.raw !== undefined && payload.raw !== null) {
|
|
84
|
+
return payload.raw
|
|
85
|
+
}
|
|
86
|
+
if (payload.data !== undefined && payload.data !== null) {
|
|
87
|
+
return payload.data
|
|
88
|
+
}
|
|
89
|
+
return null
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const pickData = (payload: Record<string, any>) => {
|
|
93
|
+
const data = payload?.data
|
|
94
|
+
if (data && typeof data === 'object') {
|
|
95
|
+
if (data.payload !== undefined && data.payload !== null) {
|
|
96
|
+
return data.payload
|
|
97
|
+
}
|
|
98
|
+
return data
|
|
99
|
+
}
|
|
100
|
+
const raw = payload?.raw
|
|
101
|
+
if (raw && typeof raw === 'object') {
|
|
102
|
+
if (raw.payload !== undefined && raw.payload !== null) {
|
|
103
|
+
return raw.payload
|
|
104
|
+
}
|
|
105
|
+
return raw
|
|
106
|
+
}
|
|
107
|
+
return null
|
|
108
|
+
}
|
|
109
|
+
|
|
57
110
|
const createBridge = (options: BridgeOptions): PowerXCapabilityBridge => {
|
|
58
111
|
const fetcher = options.fetcher ?? ofetch
|
|
59
112
|
const endpoint = options.endpoint || DEFAULT_ENDPOINT
|
|
@@ -77,35 +130,48 @@ const createBridge = (options: BridgeOptions): PowerXCapabilityBridge => {
|
|
|
77
130
|
}
|
|
78
131
|
let response: FetchResponse<Record<string, any>>
|
|
79
132
|
try {
|
|
133
|
+
const body: Record<string, any> = {
|
|
134
|
+
capabilityId,
|
|
135
|
+
action,
|
|
136
|
+
payload: request.payload ?? {}
|
|
137
|
+
}
|
|
138
|
+
if (request.preferredProtocol) {
|
|
139
|
+
body.preferredProtocol = request.preferredProtocol
|
|
140
|
+
}
|
|
141
|
+
if (request.metadata) {
|
|
142
|
+
body.metadata = request.metadata
|
|
143
|
+
}
|
|
80
144
|
response = await fetcher.raw(url, {
|
|
81
145
|
method: 'POST',
|
|
82
146
|
headers,
|
|
83
|
-
body
|
|
84
|
-
capabilityId,
|
|
85
|
-
action,
|
|
86
|
-
payload: request.payload ?? {}
|
|
87
|
-
},
|
|
147
|
+
body,
|
|
88
148
|
signal: request.signal
|
|
89
149
|
})
|
|
90
150
|
} catch (error) {
|
|
91
|
-
throw new PowerXCapabilityBridgeError(
|
|
151
|
+
throw new PowerXCapabilityBridgeError(
|
|
152
|
+
error instanceof Error ? error.message : 'capability invoke failed'
|
|
153
|
+
)
|
|
92
154
|
}
|
|
93
155
|
|
|
94
156
|
const payload = response._data ?? {}
|
|
95
157
|
const traceId = payload.traceId || response.headers.get('x-trace-id') || undefined
|
|
158
|
+
const warnings = Array.isArray(payload.warnings) ? payload.warnings : undefined
|
|
96
159
|
if (response.status >= 400 || payload.error) {
|
|
97
160
|
throw new PowerXCapabilityBridgeError(payload.error?.message || 'capability invoke failed', {
|
|
98
161
|
status: response.status,
|
|
99
162
|
traceId,
|
|
100
|
-
details: payload.error || payload.errors
|
|
163
|
+
details: payload.error || payload.errors,
|
|
164
|
+
warnings
|
|
101
165
|
})
|
|
102
166
|
}
|
|
103
167
|
|
|
104
168
|
return {
|
|
105
169
|
traceId,
|
|
106
|
-
status: payload
|
|
107
|
-
data: payload
|
|
108
|
-
errors: payload.errors ?? null
|
|
170
|
+
status: pickStatus(payload),
|
|
171
|
+
data: pickData(payload),
|
|
172
|
+
errors: payload.errors ?? null,
|
|
173
|
+
warnings,
|
|
174
|
+
raw: pickRaw(payload)
|
|
109
175
|
}
|
|
110
176
|
}
|
|
111
177
|
}
|