one-shared 1.1.0 → 1.1.2
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/client.ts +11 -4
- package/package.json +1 -1
- package/schema.ts +7 -0
- package/types.ts +14 -1
package/client.ts
CHANGED
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
ProcedureName,
|
|
6
6
|
OptionalParams,
|
|
7
7
|
} from "./types";
|
|
8
|
+
import { parseOutput } from "./schema";
|
|
8
9
|
|
|
9
10
|
/*
|
|
10
11
|
export const emptyParamsSchema = z.tuple([]).readonly()
|
|
@@ -39,7 +40,7 @@ export const apiSchemas = {
|
|
|
39
40
|
export default async function client<P extends ProcedureName>(
|
|
40
41
|
props: { procedure: P } & OptionalParams<ApiSchemas[P], "params"> &
|
|
41
42
|
OptionalParams<ApiSchemas[P], "input">,
|
|
42
|
-
): Promise<z.infer<ApiSchemas[P][
|
|
43
|
+
): Promise<z.infer<ApiSchemas[P]["output"]>> {
|
|
43
44
|
const base = "http://localhost:3000";
|
|
44
45
|
const procedureSchemas = apiSchemas[props.procedure];
|
|
45
46
|
|
|
@@ -52,7 +53,7 @@ export default async function client<P extends ProcedureName>(
|
|
|
52
53
|
const json = JSON.stringify(parsed);
|
|
53
54
|
init.body = json;
|
|
54
55
|
}
|
|
55
|
-
|
|
56
|
+
|
|
56
57
|
// URL
|
|
57
58
|
const parts = [base, procedureSchemas.path];
|
|
58
59
|
if ("params" in procedureSchemas) {
|
|
@@ -64,7 +65,13 @@ export default async function client<P extends ProcedureName>(
|
|
|
64
65
|
|
|
65
66
|
const response = await fetch(url, init);
|
|
66
67
|
const data: unknown = await response.json();
|
|
67
|
-
|
|
68
|
+
|
|
69
|
+
// const parser: ParsersByProcedure[P] = apiSchemas[props.procedure];
|
|
70
|
+
// const output = parser.output.parse(data);
|
|
71
|
+
|
|
72
|
+
const output = parseOutput(apiSchemas[props.procedure], data)
|
|
68
73
|
|
|
69
74
|
return output;
|
|
70
|
-
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
|
package/package.json
CHANGED
package/schema.ts
CHANGED
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
boolean,
|
|
12
12
|
varchar,
|
|
13
13
|
} from "drizzle-orm/pg-core";
|
|
14
|
+
import z from "zod";
|
|
14
15
|
|
|
15
16
|
export const statusSignalEnum = pgEnum("status_signal", [
|
|
16
17
|
"recovery",
|
|
@@ -209,3 +210,9 @@ export const relations = defineRelations(tables, (props) => {
|
|
|
209
210
|
};
|
|
210
211
|
});
|
|
211
212
|
|
|
213
|
+
export function parseOutput<Parser extends { output: z.ZodType; }>(
|
|
214
|
+
parser: Parser,
|
|
215
|
+
data: unknown): z.infer<Parser["output"]> {
|
|
216
|
+
return z.parse<Parser["output"]>(parser.output, data);
|
|
217
|
+
}
|
|
218
|
+
|
package/types.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import db from "./db";
|
|
2
|
-
import { z } from "zod";
|
|
2
|
+
import { output, z } from "zod";
|
|
3
3
|
import {
|
|
4
4
|
channelsTable,
|
|
5
5
|
eventsTable,
|
|
@@ -170,6 +170,18 @@ export type OptionalParams<S, K extends PropertyKey> = S extends {
|
|
|
170
170
|
? { [P in K]: z.infer<T> }
|
|
171
171
|
: { [P in K]?: never };
|
|
172
172
|
|
|
173
|
+
// export type OutputByProcedure = {
|
|
174
|
+
// [Name in ProcedureName]: z.infer<ApiSchemas[Name]['output']>
|
|
175
|
+
// }
|
|
176
|
+
|
|
177
|
+
export type ParsersByProcedure = {
|
|
178
|
+
[Name in ProcedureName]: {
|
|
179
|
+
output: {
|
|
180
|
+
parse(data: unknown): z.infer<ApiSchemas[Name]['output']>
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
173
185
|
export const incidentWithChannelSchema = incidentSchema.extend({
|
|
174
186
|
channel: channelSchema,
|
|
175
187
|
});
|
|
@@ -227,3 +239,4 @@ export type ApiSchemas = typeof apiSchemas;
|
|
|
227
239
|
|
|
228
240
|
export type ProcedureName = keyof ApiSchemas;
|
|
229
241
|
// 'getHealth' | 'getIncidents' | 'getIncident' ...
|
|
242
|
+
|