网络层
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
export const JsonCodec = {
|
||||
/** POST/PUT 的请求体直接用对象,让传输层序列化为 JSON */
|
||||
encode<T = any>(obj: T): any {
|
||||
return obj ?? {};
|
||||
},
|
||||
|
||||
/** 根据传输层返回的类型做解码:string → JSON.parse,其他直接返回 */
|
||||
decode<T = any>(resp: any): T {
|
||||
if (typeof resp === "string") {
|
||||
try { return JSON.parse(resp) as T; } catch { /* 可能是纯文本 */ }
|
||||
}
|
||||
return resp as T;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "4d188874-4871-46e7-bc80-a179ae1ed67a",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import type { Type } from "protobufjs";
|
||||
|
||||
export const ProtoCodec = {
|
||||
encode(reqType: Type, obj: any): Uint8Array {
|
||||
const err = reqType.verify(obj);
|
||||
if (err) throw Error(err);
|
||||
return reqType.encode(reqType.create(obj)).finish();
|
||||
},
|
||||
|
||||
decode<T = any>(resType: Type, buf: ArrayBuffer | Uint8Array): T {
|
||||
const u8 = buf instanceof Uint8Array ? buf : new Uint8Array(buf);
|
||||
return resType.decode(u8) as unknown as T;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "7e271138-d20e-4236-8355-ca4bcec0a23e",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
Reference in New Issue
Block a user