one-shared 0.0.0-stage → 1.1.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 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
+ ): Promise<z.infer<ApiSchemas[P]['output']>> {
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) as unknown as z.infer<ApiSchemas[P]['output']>
68
+
69
+ return output;
70
+ }
package/db.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { drizzle } from "drizzle-orm/neon-http";
2
+ import { relations } from "./schema";
3
+
4
+ const db = drizzle({ connection: 'postgresql://fake:fake@fake.com', relations });
5
+ export default db