This commit is contained in:
parent
1a31c85e27
commit
96b3b1a798
|
|
@ -7,6 +7,15 @@ describe('unwrapApiEnvelope', () => {
|
||||||
expect(unwrapApiEnvelope({ code: 200, data: { id: 1 } })).toEqual({ id: 1 })
|
expect(unwrapApiEnvelope({ code: 200, data: { id: 1 } })).toEqual({ id: 1 })
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('treats backend code 0 as a successful response', () => {
|
||||||
|
const data = {
|
||||||
|
id: '1508503',
|
||||||
|
outTradeNo: '10050_20260714112755619_10001',
|
||||||
|
status: 1,
|
||||||
|
}
|
||||||
|
expect(unwrapApiEnvelope({ code: 0, message: 'OK', data })).toEqual(data)
|
||||||
|
})
|
||||||
|
|
||||||
it('supports a successful response without a code for backwards compatibility', () => {
|
it('supports a successful response without a code for backwards compatibility', () => {
|
||||||
expect(unwrapApiEnvelope({ data: 'ok' })).toBe('ok')
|
expect(unwrapApiEnvelope({ data: 'ok' })).toBe('ok')
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,8 @@ export function unwrapApiEnvelope<T>(value: unknown): T {
|
||||||
}
|
}
|
||||||
|
|
||||||
const envelope = value as ApiEnvelope<T>
|
const envelope = value as ApiEnvelope<T>
|
||||||
if (envelope.code !== undefined && envelope.code !== 200) {
|
const isSuccess = envelope.code === undefined || envelope.code === 0 || envelope.code === 200
|
||||||
|
if (!isSuccess) {
|
||||||
const message = envelope.message?.split(',')[0] || '请求失败'
|
const message = envelope.message?.split(',')[0] || '请求失败'
|
||||||
throw new ApiError(message, envelope.code)
|
throw new ApiError(message, envelope.code)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue