@synnaxlabs/freighter 0.8.1 → 0.9.1
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.
- package/dist/freighter.cjs +1 -1
- package/dist/freighter.cjs.map +1 -1
- package/dist/freighter.js +2730 -2712
- package/dist/freighter.js.map +1 -1
- package/dist/http.d.ts +1 -1
- package/dist/unary.d.ts +2 -2
- package/package.json +3 -4
- package/src/http.spec.ts +3 -0
- package/src/http.ts +7 -5
- package/src/unary.ts +6 -4
package/dist/http.d.ts
CHANGED
|
@@ -16,5 +16,5 @@ export declare class HTTPClient extends MiddlewareCollector implements UnaryClie
|
|
|
16
16
|
fetch: typeof fetch;
|
|
17
17
|
constructor(endpoint: URL, encoder: binary.EncoderDecoder, secure?: boolean);
|
|
18
18
|
get headers(): Record<string, string>;
|
|
19
|
-
send<RQ extends z.ZodTypeAny, RS extends z.ZodTypeAny = RQ>(target: string, req: z.input<RQ> |
|
|
19
|
+
send<RQ extends z.ZodTypeAny, RS extends z.ZodTypeAny = RQ>(target: string, req: z.input<RQ> | z.output<RQ>, reqSchema: RQ, resSchema: RS): Promise<[z.output<RS> | null, Error | null]>;
|
|
20
20
|
}
|
package/dist/unary.d.ts
CHANGED
|
@@ -11,6 +11,6 @@ export interface UnaryClient extends Transport {
|
|
|
11
11
|
* @param req - The request to send.
|
|
12
12
|
* @param resSchema - The schema to validate the response against.
|
|
13
13
|
*/
|
|
14
|
-
send: <RQ extends z.ZodTypeAny, RS extends z.ZodTypeAny = RQ>(target: string, req: z.input<RQ> |
|
|
14
|
+
send: <RQ extends z.ZodTypeAny, RS extends z.ZodTypeAny = RQ>(target: string, req: z.input<RQ> | z.output<RQ>, reqSchema: RQ, resSchema: RS) => Promise<[z.output<RS>, null] | [null, Error]>;
|
|
15
15
|
}
|
|
16
|
-
export declare const sendRequired: <RQ extends z.ZodTypeAny, RS extends z.ZodTypeAny = RQ>(client: UnaryClient, target: string, req: z.input<RQ> | z.output<RQ>, resSchema: RS
|
|
16
|
+
export declare const sendRequired: <RQ extends z.ZodTypeAny, RS extends z.ZodTypeAny = RQ>(client: UnaryClient, target: string, req: z.input<RQ> | z.output<RQ>, reqSchema: RQ, resSchema: RS) => Promise<z.output<RS>>;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@synnaxlabs/freighter",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.9.1",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "a modular transport abstraction",
|
|
7
7
|
"repository": "https://github.com/synnaxlabs/synnax/tree/main/freighter/ts",
|
|
@@ -20,12 +20,11 @@
|
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"js-convert-case": "^4.2.0",
|
|
23
|
-
"msgpackr": "^1.10.0",
|
|
24
23
|
"node-fetch": "2.6.11",
|
|
25
24
|
"ws": "^8.15.1",
|
|
26
25
|
"zod": "3.22.4",
|
|
27
|
-
"@synnaxlabs/
|
|
28
|
-
"@synnaxlabs/
|
|
26
|
+
"@synnaxlabs/alamos": "0.3.0",
|
|
27
|
+
"@synnaxlabs/x": "0.14.1"
|
|
29
28
|
},
|
|
30
29
|
"devDependencies": {
|
|
31
30
|
"@types/node": "^20.10.5",
|
package/src/http.spec.ts
CHANGED
|
@@ -36,6 +36,7 @@ describe("http", () => {
|
|
|
36
36
|
message: "hello",
|
|
37
37
|
},
|
|
38
38
|
messageZ,
|
|
39
|
+
messageZ,
|
|
39
40
|
);
|
|
40
41
|
expect(error).toBeNull();
|
|
41
42
|
expect(response).toEqual({ id: 2, message: "hello" });
|
|
@@ -46,6 +47,7 @@ describe("http", () => {
|
|
|
46
47
|
"/not-found",
|
|
47
48
|
{},
|
|
48
49
|
messageZ,
|
|
50
|
+
messageZ,
|
|
49
51
|
);
|
|
50
52
|
expect(error?.message).toEqual("Not Found");
|
|
51
53
|
expect(response).toBeNull();
|
|
@@ -60,6 +62,7 @@ describe("http", () => {
|
|
|
60
62
|
"/middlewareCheck",
|
|
61
63
|
{},
|
|
62
64
|
messageZ,
|
|
65
|
+
messageZ,
|
|
63
66
|
);
|
|
64
67
|
expect(error).toBeNull();
|
|
65
68
|
expect(response?.message).toEqual("");
|
package/src/http.ts
CHANGED
|
@@ -70,10 +70,12 @@ export class HTTPClient extends MiddlewareCollector implements UnaryClient {
|
|
|
70
70
|
|
|
71
71
|
async send<RQ extends z.ZodTypeAny, RS extends z.ZodTypeAny = RQ>(
|
|
72
72
|
target: string,
|
|
73
|
-
req: z.input<RQ> |
|
|
74
|
-
|
|
73
|
+
req: z.input<RQ> | z.output<RQ>,
|
|
74
|
+
reqSchema: RQ,
|
|
75
|
+
resSchema: RS,
|
|
75
76
|
): Promise<[z.output<RS> | null, Error | null]> {
|
|
76
|
-
|
|
77
|
+
req = reqSchema?.parse(req)
|
|
78
|
+
let res: RS | null = null;
|
|
77
79
|
const url = this.endpoint.child(target);
|
|
78
80
|
const request: RequestInit = {};
|
|
79
81
|
request.method = "POST";
|
|
@@ -103,7 +105,7 @@ export class HTTPClient extends MiddlewareCollector implements UnaryClient {
|
|
|
103
105
|
}
|
|
104
106
|
const data = await httpRes.arrayBuffer();
|
|
105
107
|
if (httpRes?.ok) {
|
|
106
|
-
if (resSchema != null)
|
|
108
|
+
if (resSchema != null) res = this.encoder.decode(data, resSchema);
|
|
107
109
|
return [outCtx, null];
|
|
108
110
|
}
|
|
109
111
|
try {
|
|
@@ -124,6 +126,6 @@ export class HTTPClient extends MiddlewareCollector implements UnaryClient {
|
|
|
124
126
|
},
|
|
125
127
|
);
|
|
126
128
|
|
|
127
|
-
return [
|
|
129
|
+
return [res, err];
|
|
128
130
|
}
|
|
129
131
|
}
|
package/src/unary.ts
CHANGED
|
@@ -24,8 +24,9 @@ export interface UnaryClient extends Transport {
|
|
|
24
24
|
*/
|
|
25
25
|
send: <RQ extends z.ZodTypeAny, RS extends z.ZodTypeAny = RQ>(
|
|
26
26
|
target: string,
|
|
27
|
-
req: z.input<RQ> |
|
|
28
|
-
|
|
27
|
+
req: z.input<RQ> | z.output<RQ>,
|
|
28
|
+
reqSchema: RQ ,
|
|
29
|
+
resSchema: RS,
|
|
29
30
|
) => Promise<[z.output<RS>, null] | [null, Error]>;
|
|
30
31
|
}
|
|
31
32
|
|
|
@@ -36,9 +37,10 @@ export const sendRequired = async <
|
|
|
36
37
|
client: UnaryClient,
|
|
37
38
|
target: string,
|
|
38
39
|
req: z.input<RQ> | z.output<RQ>,
|
|
39
|
-
|
|
40
|
+
reqSchema: RQ,
|
|
41
|
+
resSchema: RS,
|
|
40
42
|
): Promise<z.output<RS>> => {
|
|
41
|
-
const [res, err] = await client.send(target, req, resSchema);
|
|
43
|
+
const [res, err] = await client.send(target, req, reqSchema, resSchema);
|
|
42
44
|
if (err != null) throw err;
|
|
43
45
|
return res;
|
|
44
46
|
};
|