From e6c6f68a3a352e9634e33e0e3bb9ee8b8378cac4 Mon Sep 17 00:00:00 2001 From: chiyi Date: Thu, 20 Aug 2026 12:09:43 +0800 Subject: [PATCH] fun: upgrade TS client interceptors to fun-client parity (v1.2.0) - request interceptor now receives the mutable per-request state map (token injection) - response interceptor may return a replacement result (central token swap / error handling) - fetch failures normalized to result status=4 (network error) instead of throwing - stream requests also pass through request interceptors with state copy --- template_ts.go | 47 +++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/template_ts.go b/template_ts.go index 364a302..c930757 100644 --- a/template_ts.go +++ b/template_ts.go @@ -11,17 +11,22 @@ func (ctx templateTs) genClientTemplate() string { status: number; }; +export type resultStatus = 0 | 1 | 2 | 4 | 5; +// 0 成功;1 框架错误;2 业务错误;4 网络错误;5 超时 + export type RequestInterceptor = ( serviceName: string, methodName: string, - dto: any + state: Record, + dto?: any ) => Promise | void; +// 返回新 result 将替换原结果继续向下传递(可用于集中换 token / 错误处理) export type ResponseInterceptor = ( serviceName: string, methodName: string, result: result -) => Promise | void; +) => Promise | void> | result | void; export class Client { private url: string; @@ -46,26 +51,27 @@ export class Client { } async request(serviceName: string, methodName: string, dto?: any): Promise> { + const state: Record = { ...this.state }; for (const i of this.requestInterceptors) { - await i(serviceName, methodName, dto); + await i(serviceName, methodName, state, dto); } - const res = await fetch(this.url + "/cell", { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ serviceName, methodName, data: dto, ...(Object.keys(this.state).length ? { state: this.state } : {}) }), - }); - const out = (await res.json()) as result; - const anyResult: result = { - id: out.id, - code: out.code, - data: out.data, - msg: out.msg, - status: out.status, - }; + let out: result; + try { + const res = await fetch(this.url + "/cell", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ serviceName, methodName, data: dto, ...(Object.keys(state).length ? { state } : {}) }), + }); + out = (await res.json()) as result; + } catch (e: any) { + out = { status: 4, msg: (e && e.message) || "网络错误" } as result; + } + let cur: result = out as result; for (const i of this.responseInterceptors) { - await i(serviceName, methodName, anyResult); + const replaced = await i(serviceName, methodName, cur); + if (replaced) cur = replaced; } - return out; + return cur as result; } async stream( @@ -74,13 +80,14 @@ export class Client { dto: any | undefined, onMessage: (data: T) => void ): Promise { + const state: Record = { ...this.state }; for (const i of this.requestInterceptors) { - await i(serviceName, methodName, dto); + await i(serviceName, methodName, state, dto); } const res = await fetch(this.url + "/cell", { method: "POST", headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ serviceName, methodName, data: dto, ...(Object.keys(this.state).length ? { state: this.state } : {}) }), + body: JSON.stringify({ serviceName, methodName, data: dto, ...(Object.keys(state).length ? { state } : {}) }), }); if (!res.ok) return; const anyResult: result = { status: 0 };