one-shared 0.0.0-stage → 1.0.0
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 +70 -0
- package/db.ts +5 -0
- package/dist/index.d.mts +5538 -0
- package/dist/index.d.ts +5538 -0
- package/dist/index.js +288 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +250 -0
- package/dist/index.mjs.map +1 -0
- package/index.ts +5 -0
- package/package.json +33 -4
- package/schema.ts +211 -0
- package/tsconfig.json +12 -0
- package/tsdown.config.mts +17 -0
- package/types.ts +229 -0
- package/README.md +0 -3
package/client.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import z from "zod";
|
|
2
|
+
import {
|
|
3
|
+
apiSchemas,
|
|
4
|
+
ApiSchemas,
|
|
5
|
+
ProcedureName,
|
|
6
|
+
OptionalParams,
|
|
7
|
+
} from "./types";
|
|
8
|
+
|
|
9
|
+
/*
|
|
10
|
+
export const emptyParamsSchema = z.tuple([]).readonly()
|
|
11
|
+
// [] GOOD
|
|
12
|
+
// [string] BAD
|
|
13
|
+
// [string, string] BAD
|
|
14
|
+
|
|
15
|
+
export const singleParamSchema = z.tuple([z.string()]).readonly()
|
|
16
|
+
// [string] GOOD
|
|
17
|
+
// [] BAD
|
|
18
|
+
// [string, string] BAD
|
|
19
|
+
|
|
20
|
+
export const apiSchemas = {
|
|
21
|
+
getHealth: {
|
|
22
|
+
path: 'health',
|
|
23
|
+
output: getHealthOutputSchema,
|
|
24
|
+
params: emptyParamsSchema
|
|
25
|
+
},
|
|
26
|
+
getIncidents: {
|
|
27
|
+
path: 'incidents',
|
|
28
|
+
output: getIncidentsOutputSchema,
|
|
29
|
+
params: emptyParamsSchema
|
|
30
|
+
},
|
|
31
|
+
getIncident: {
|
|
32
|
+
path: 'incidents',
|
|
33
|
+
output: getIncidentOutputSchema,
|
|
34
|
+
params: singleParamSchema
|
|
35
|
+
},
|
|
36
|
+
}
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
export default async function client<P extends ProcedureName>(
|
|
40
|
+
props: { procedure: P } & OptionalParams<ApiSchemas[P], "params"> &
|
|
41
|
+
OptionalParams<ApiSchemas[P], "input">,
|
|
42
|
+
) {
|
|
43
|
+
const base = "http://localhost:3000";
|
|
44
|
+
const procedureSchemas = apiSchemas[props.procedure];
|
|
45
|
+
|
|
46
|
+
// Body
|
|
47
|
+
const init: RequestInit = {
|
|
48
|
+
method: procedureSchemas.method,
|
|
49
|
+
};
|
|
50
|
+
if ("input" in procedureSchemas) {
|
|
51
|
+
const parsed = procedureSchemas.input.parse(props.input);
|
|
52
|
+
const json = JSON.stringify(parsed);
|
|
53
|
+
init.body = json;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// URL
|
|
57
|
+
const parts = [base, procedureSchemas.path];
|
|
58
|
+
if ("params" in procedureSchemas) {
|
|
59
|
+
const parsed = procedureSchemas.params.parse(props.params);
|
|
60
|
+
const encoded = parsed.map((param) => encodeURIComponent(param));
|
|
61
|
+
parts.push(...encoded);
|
|
62
|
+
}
|
|
63
|
+
const url = parts.join("/");
|
|
64
|
+
|
|
65
|
+
const response = await fetch(url, init);
|
|
66
|
+
const data: unknown = await response.json();
|
|
67
|
+
const output = procedureSchemas.output.parse(data);
|
|
68
|
+
|
|
69
|
+
return output;
|
|
70
|
+
}
|