@nkzw/fate 0.1.2 → 1.0.0-rc.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/README.md +810 -129
- package/docs/api/classes/FateClient.md +832 -0
- package/docs/api/functions/clientRoot.md +27 -0
- package/docs/api/functions/createClient.md +21 -0
- package/docs/api/functions/createHTTPTransport.md +51 -0
- package/docs/api/functions/createTRPCTransport.md +46 -0
- package/docs/api/functions/getListEntries.md +15 -0
- package/docs/api/functions/getSelectionPlan.md +36 -0
- package/docs/api/functions/isRecord.md +15 -0
- package/docs/api/functions/isViewTag.md +17 -0
- package/docs/api/functions/liveConnectionTopic.md +19 -0
- package/docs/api/functions/liveEntityTopic.md +19 -0
- package/docs/api/functions/liveGlobalConnectionTopic.md +15 -0
- package/docs/api/functions/mutation.md +32 -0
- package/docs/api/functions/toEntityId.md +21 -0
- package/docs/api/functions/view.md +26 -0
- package/docs/api/index.md +81 -0
- package/docs/api/interfaces/FateThenable.md +31 -0
- package/docs/api/interfaces/Transport.md +199 -0
- package/docs/api/server/classes/FateRequestError.md +67 -0
- package/docs/api/server/drizzle/functions/createDrizzleFate.md +431 -0
- package/docs/api/server/drizzle/functions/createDrizzleSourceAdapter.md +21 -0
- package/docs/api/server/drizzle/functions/createDrizzleSourceRegistry.md +21 -0
- package/docs/api/server/drizzle/index.md +23 -0
- package/docs/api/server/drizzle/type-aliases/DrizzleManyToManyConfig.md +15 -0
- package/docs/api/server/drizzle/type-aliases/DrizzleManyToManyInput.md +5 -0
- package/docs/api/server/drizzle/type-aliases/DrizzleQueryExtra.md +21 -0
- package/docs/api/server/drizzle/type-aliases/DrizzleSourceAdapter.md +337 -0
- package/docs/api/server/drizzle/type-aliases/DrizzleViewConfig.md +29 -0
- package/docs/api/server/functions/bindSourceProcedures.md +32 -0
- package/docs/api/server/functions/computed.md +43 -0
- package/docs/api/server/functions/count.md +21 -0
- package/docs/api/server/functions/createFateFetchHandler.md +21 -0
- package/docs/api/server/functions/createFateServer.md +41 -0
- package/docs/api/server/functions/createHonoFateHandler.md +21 -0
- package/docs/api/server/functions/createLiveEventBus.md +17 -0
- package/docs/api/server/functions/createNestedSourcePlan.md +103 -0
- package/docs/api/server/functions/createResolver.md +74 -0
- package/docs/api/server/functions/createSourcePlan.md +95 -0
- package/docs/api/server/functions/createSourceProcedures.md +46 -0
- package/docs/api/server/functions/dataView.md +32 -0
- package/docs/api/server/functions/field.md +15 -0
- package/docs/api/server/functions/getNestedSelection.md +19 -0
- package/docs/api/server/functions/getScopedArgs.md +21 -0
- package/docs/api/server/functions/hasNestedSelection.md +19 -0
- package/docs/api/server/functions/list.md +28 -0
- package/docs/api/server/functions/refetchSourceById.md +55 -0
- package/docs/api/server/functions/resolveSourceById.md +55 -0
- package/docs/api/server/functions/resolveSourceByIds.md +55 -0
- package/docs/api/server/functions/resolveSourceConnection.md +67 -0
- package/docs/api/server/functions/resolver.md +42 -0
- package/docs/api/server/functions/toPrismaSelect.md +25 -0
- package/docs/api/server/functions/withConnection.md +24 -0
- package/docs/api/server/index.md +109 -0
- package/docs/api/server/prisma/functions/createPrismaFate.md +435 -0
- package/docs/api/server/prisma/functions/createPrismaSourceAdapter.md +21 -0
- package/docs/api/server/prisma/functions/createPrismaSourceRegistry.md +21 -0
- package/docs/api/server/prisma/functions/prismaConnectionArgs.md +57 -0
- package/docs/api/server/prisma/index.md +23 -0
- package/docs/api/server/prisma/type-aliases/PrismaDelegate.md +59 -0
- package/docs/api/server/prisma/type-aliases/PrismaQueryExtra.md +5 -0
- package/docs/api/server/prisma/type-aliases/PrismaSourceAdapter.md +343 -0
- package/docs/api/server/prisma/type-aliases/PrismaViewConfig.md +31 -0
- package/docs/api/server/type-aliases/ComputedField.md +55 -0
- package/docs/api/server/type-aliases/ComputedSelection.md +5 -0
- package/docs/api/server/type-aliases/ConnectionItem.md +29 -0
- package/docs/api/server/type-aliases/ConnectionPagination.md +39 -0
- package/docs/api/server/type-aliases/ConnectionResult.md +29 -0
- package/docs/api/server/type-aliases/CountSelection.md +29 -0
- package/docs/api/server/type-aliases/CountWhere.md +5 -0
- package/docs/api/server/type-aliases/DataViewListOptions.md +13 -0
- package/docs/api/server/type-aliases/DataViewOrderBy.md +5 -0
- package/docs/api/server/type-aliases/DataViewOrderDirection.md +5 -0
- package/docs/api/server/type-aliases/DataViewResult.md +11 -0
- package/docs/api/server/type-aliases/Entity.md +21 -0
- package/docs/api/server/type-aliases/FateServer.md +75 -0
- package/docs/api/server/type-aliases/FateServerManifest.md +5 -0
- package/docs/api/server/type-aliases/FieldSelection.md +21 -0
- package/docs/api/server/type-aliases/LiveConnectionEventType.md +5 -0
- package/docs/api/server/type-aliases/LiveConnectionSourceEvent.md +5 -0
- package/docs/api/server/type-aliases/LiveEventBus.md +5 -0
- package/docs/api/server/type-aliases/LiveEventType.md +5 -0
- package/docs/api/server/type-aliases/LiveSourceEvent.md +5 -0
- package/docs/api/server/type-aliases/NativeFateAPI.md +47 -0
- package/docs/api/server/type-aliases/OrderDirection.md +5 -0
- package/docs/api/server/type-aliases/SourceConfig.md +55 -0
- package/docs/api/server/type-aliases/SourceDefinition.md +55 -0
- package/docs/api/server/type-aliases/SourceOrder.md +5 -0
- package/docs/api/server/type-aliases/SourceOrderField.md +21 -0
- package/docs/api/server/type-aliases/SourcePlan.md +29 -0
- package/docs/api/server/type-aliases/SourcePlanNode.md +29 -0
- package/docs/api/server/type-aliases/SourceRegistry.md +11 -0
- package/docs/api/server/type-aliases/SourceRelation.md +15 -0
- package/docs/api/server/type-aliases/SourceRelationConfig.md +23 -0
- package/docs/api/server/variables/byIdInput.md +5 -0
- package/docs/api/server/variables/connectionArgs.md +7 -0
- package/docs/api/type-aliases/ConnectionMetadata.md +7 -0
- package/docs/api/type-aliases/ConnectionRef.md +13 -0
- package/docs/api/type-aliases/Entity.md +15 -0
- package/docs/api/type-aliases/EntityId.md +7 -0
- package/docs/api/type-aliases/FateLiveConnectionEvent.md +5 -0
- package/docs/api/type-aliases/FateLiveEvent.md +5 -0
- package/docs/api/type-aliases/FateMutations.md +7 -0
- package/docs/api/type-aliases/FateOperation.md +5 -0
- package/docs/api/type-aliases/FateProtocolRequest.md +5 -0
- package/docs/api/type-aliases/FateProtocolResponse.md +5 -0
- package/docs/api/type-aliases/FateRecord.md +7 -0
- package/docs/api/type-aliases/FateRoots.md +7 -0
- package/docs/api/type-aliases/InferFateAPI.md +11 -0
- package/docs/api/type-aliases/List.md +5 -0
- package/docs/api/type-aliases/ListEntry.md +5 -0
- package/docs/api/type-aliases/ListItem.md +13 -0
- package/docs/api/type-aliases/Mask.md +15 -0
- package/docs/api/type-aliases/MutationDefinition.md +21 -0
- package/docs/api/type-aliases/MutationEntity.md +13 -0
- package/docs/api/type-aliases/MutationIdentifier.md +19 -0
- package/docs/api/type-aliases/MutationInput.md +13 -0
- package/docs/api/type-aliases/MutationResult.md +13 -0
- package/docs/api/type-aliases/NodesItem.md +13 -0
- package/docs/api/type-aliases/Pagination.md +39 -0
- package/docs/api/type-aliases/Request.md +7 -0
- package/docs/api/type-aliases/RequestMode.md +7 -0
- package/docs/api/type-aliases/RequestOptions.md +7 -0
- package/docs/api/type-aliases/RequestResult.md +18 -0
- package/docs/api/type-aliases/Selection.md +13 -0
- package/docs/api/type-aliases/Snapshot.md +7 -0
- package/docs/api/type-aliases/TypeConfig.md +41 -0
- package/docs/api/type-aliases/View.md +17 -0
- package/docs/api/type-aliases/ViewData.md +18 -0
- package/docs/api/type-aliases/ViewEntity.md +13 -0
- package/docs/api/type-aliases/ViewEntityName.md +13 -0
- package/docs/api/type-aliases/ViewRef.md +13 -0
- package/docs/api/type-aliases/ViewSelection.md +13 -0
- package/docs/api/type-aliases/ViewSnapshot.md +18 -0
- package/docs/api/type-aliases/ViewTag.md +7 -0
- package/docs/api/variables/ConnectionTag.md +7 -0
- package/docs/guide/actions.md +263 -0
- package/docs/guide/core-concepts.md +22 -0
- package/docs/guide/getting-started.md +57 -0
- package/docs/guide/list-views.md +86 -0
- package/docs/guide/live-views.md +245 -0
- package/docs/guide/requests.md +130 -0
- package/docs/guide/server-integration.md +630 -0
- package/docs/guide/views.md +278 -0
- package/docs/guide/void-integration.md +167 -0
- package/docs/index.md +4 -0
- package/lib/cli.mjs +17 -250
- package/lib/clientStub.d.mts +6 -0
- package/lib/clientStub.mjs +6 -0
- package/lib/executor-BqwHdN3n.d.mts +468 -0
- package/lib/index.d.mts +64 -3
- package/lib/index.mjs +1965 -559
- package/lib/list-4wRNSVgI.d.mts +10 -0
- package/lib/list-C_mi6GnE.mjs +16 -0
- package/lib/list.d.mts +2 -0
- package/lib/list.mjs +2 -0
- package/lib/liveTopics-DzNtJaBD.mjs +197 -0
- package/lib/{record-DnhZuvUe.mjs → record-AeZJC9fd.mjs} +1 -2
- package/lib/record-ENh92gyb.d.mts +315 -0
- package/lib/server/drizzle.d.mts +304 -0
- package/lib/server/drizzle.mjs +688 -0
- package/lib/server/prisma.d.mts +309 -0
- package/lib/server/prisma.mjs +345 -0
- package/lib/server.d.mts +82 -184
- package/lib/server.mjs +870 -416
- package/lib/sourceRouter-Bwpkr062.mjs +972 -0
- package/lib/{types-Djlh7mMH.d.mts → types-Dz46PXr3.d.mts} +327 -197
- package/lib/vite.d.mts +17 -0
- package/lib/vite.mjs +592 -0
- package/package.json +44 -6
package/lib/cli.mjs
CHANGED
|
@@ -1,259 +1,26 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { writeFileSync } from "node:fs";
|
|
3
2
|
import path from "node:path";
|
|
3
|
+
import process from "node:process";
|
|
4
4
|
import { styleText } from "node:util";
|
|
5
|
-
|
|
6
|
-
//#region src/codegen/schema.ts
|
|
7
|
-
const isDataViewField = (field) => Boolean(field) && typeof field === "object" && "fields" in field;
|
|
8
|
-
/**
|
|
9
|
-
* Builds the schema object used by the CLI generator from your data views and
|
|
10
|
-
* root resolver configs.
|
|
11
|
-
*/
|
|
12
|
-
const createSchema = (dataViews, roots) => {
|
|
13
|
-
const canonicalViews = /* @__PURE__ */ new Map();
|
|
14
|
-
const rootSchema = {};
|
|
15
|
-
const fateTypes = /* @__PURE__ */ new Map();
|
|
16
|
-
const processing = /* @__PURE__ */ new Set();
|
|
17
|
-
const ensureType = (view) => {
|
|
18
|
-
const typeName = view.typeName;
|
|
19
|
-
const canonicalView = canonicalViews.get(typeName) ?? view;
|
|
20
|
-
const existing = fateTypes.get(typeName);
|
|
21
|
-
if (existing && !processing.has(typeName)) return typeName;
|
|
22
|
-
if (processing.has(typeName)) return typeName;
|
|
23
|
-
processing.add(typeName);
|
|
24
|
-
const fields = existing?.fields ?? {};
|
|
25
|
-
for (const [field, child] of Object.entries(canonicalView.fields)) if (isDataViewField(child)) {
|
|
26
|
-
const relationType = ensureType(child);
|
|
27
|
-
fields[field] = child.kind === "list" ? { listOf: relationType } : { type: relationType };
|
|
28
|
-
}
|
|
29
|
-
const descriptor = { type: typeName };
|
|
30
|
-
if (Object.keys(fields).length) descriptor.fields = fields;
|
|
31
|
-
fateTypes.set(typeName, descriptor);
|
|
32
|
-
processing.delete(typeName);
|
|
33
|
-
return typeName;
|
|
34
|
-
};
|
|
35
|
-
for (const view of dataViews) {
|
|
36
|
-
const typeName = view.typeName;
|
|
37
|
-
if (!typeName) throw new Error("Data view is missing a type name.");
|
|
38
|
-
if (!canonicalViews.has(typeName)) canonicalViews.set(typeName, view);
|
|
39
|
-
}
|
|
40
|
-
for (const view of dataViews) ensureType(view);
|
|
41
|
-
for (const [name, root$1] of Object.entries(roots)) {
|
|
42
|
-
const config = "fields" in root$1 ? { view: root$1 } : root$1;
|
|
43
|
-
const view = config.view;
|
|
44
|
-
const type = ensureType(view);
|
|
45
|
-
if (!view.typeName) throw new Error(`Root "${name}" is missing a data view.`);
|
|
46
|
-
const router = config.router ?? view.typeName[0]?.toLowerCase() + view.typeName.slice(1);
|
|
47
|
-
if (!router) throw new Error(`Root "${name}" is missing a router name.`);
|
|
48
|
-
rootSchema[name] = {
|
|
49
|
-
kind: view.kind === "list" ? "list" : "query",
|
|
50
|
-
procedure: config.procedure ?? (view.kind === "list" ? "list" : name),
|
|
51
|
-
router,
|
|
52
|
-
type
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
for (const view of dataViews) ensureType(view);
|
|
56
|
-
return {
|
|
57
|
-
roots: rootSchema,
|
|
58
|
-
types: Array.from(fateTypes.values())
|
|
59
|
-
};
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
//#endregion
|
|
5
|
+
import { resolveConfig } from "vite";
|
|
63
6
|
//#region src/cli.ts
|
|
64
|
-
const
|
|
65
|
-
const
|
|
66
|
-
if (
|
|
67
|
-
|
|
7
|
+
const clientModuleId = "\0@nkzw/fate/client.ts";
|
|
8
|
+
const usage = (message) => {
|
|
9
|
+
if (message) process.stderr.write(`${styleText("red", message)}\n\n`);
|
|
10
|
+
process.stderr.write(`${styleText("bold", "Usage:")} ${styleText("blue", "fate generate")}
|
|
68
11
|
|
|
69
|
-
Generates
|
|
70
|
-
|
|
71
|
-
${styleText("dim", "<moduleName>")} The module name to import the tRPC router from.
|
|
72
|
-
${styleText("dim", "<targetFile>")} The file path to write the generated client to.
|
|
73
|
-
|
|
74
|
-
${styleText("bold", "Example:")} ${styleText("blue", `pnpm fate generate @org/server/trpc/router.ts client/lib/fate.ts`)}
|
|
12
|
+
Generates fate client files from the fate plugin in ./vite.config.ts.
|
|
75
13
|
`);
|
|
76
14
|
process.exit(1);
|
|
77
|
-
}
|
|
78
|
-
const formatRelation = (value) => "listOf" in value ? `{ listOf: '${value.listOf}' }` : `{ type: '${value.type}' }`;
|
|
79
|
-
const formatTypes = (types) => {
|
|
80
|
-
if (!types.length) return "[]";
|
|
81
|
-
const lines = ["["];
|
|
82
|
-
for (const typeConfig of types) {
|
|
83
|
-
lines.push(" {");
|
|
84
|
-
if (typeConfig.fields) {
|
|
85
|
-
lines.push(" fields: {");
|
|
86
|
-
for (const [field, relation] of Object.entries(typeConfig.fields)) lines.push(` ${field}: ${formatRelation(relation)},`);
|
|
87
|
-
lines.push(" },");
|
|
88
|
-
}
|
|
89
|
-
lines.push(` type: '${typeConfig.type}',`, " },");
|
|
90
|
-
}
|
|
91
|
-
lines.push("]");
|
|
92
|
-
return lines.join("\n");
|
|
93
|
-
};
|
|
94
|
-
const indentBlock = (value, spaces) => value.split("\n").map((line) => line.length ? `${" ".repeat(spaces)}${line}` : line).join("\n");
|
|
95
|
-
const generate = async () => {
|
|
96
|
-
console.log(styleText("bold", `Generating fate client…\n`));
|
|
97
|
-
const { appRouter, Root, ...dataViews } = await import(moduleName);
|
|
98
|
-
const { roots, types } = createSchema(Object.values(dataViews), Root ?? {});
|
|
99
|
-
const routerRecord = appRouter._def?.record ?? {};
|
|
100
|
-
const mutationEntries = [];
|
|
101
|
-
const byIdEntries = [];
|
|
102
|
-
const listEntries = [];
|
|
103
|
-
const queryEntries = [];
|
|
104
|
-
const rootsByRouter = /* @__PURE__ */ new Map();
|
|
105
|
-
for (const entry of Object.entries(roots)) {
|
|
106
|
-
const list = rootsByRouter.get(entry[1].router) ?? [];
|
|
107
|
-
list.push(entry);
|
|
108
|
-
rootsByRouter.set(entry[1].router, list);
|
|
109
|
-
}
|
|
110
|
-
for (const [router, procedures] of Object.entries(routerRecord)) {
|
|
111
|
-
const routerRoots = rootsByRouter.get(router);
|
|
112
|
-
if (!routerRoots?.length) continue;
|
|
113
|
-
const entityType = routerRoots[0][1].type;
|
|
114
|
-
for (const [procedureName, procedure] of Object.entries(procedures)) {
|
|
115
|
-
const type = procedure?._def?.type;
|
|
116
|
-
if (!type) continue;
|
|
117
|
-
if (type === "mutation") {
|
|
118
|
-
mutationEntries.push({
|
|
119
|
-
entityType,
|
|
120
|
-
name: `${router}.${procedureName}`,
|
|
121
|
-
procedure: procedureName,
|
|
122
|
-
router
|
|
123
|
-
});
|
|
124
|
-
continue;
|
|
125
|
-
}
|
|
126
|
-
if (procedureName === "byId" && type === "query") {
|
|
127
|
-
byIdEntries.push({
|
|
128
|
-
entityType,
|
|
129
|
-
router
|
|
130
|
-
});
|
|
131
|
-
continue;
|
|
132
|
-
}
|
|
133
|
-
for (const [queryName, root$1] of routerRoots) {
|
|
134
|
-
if (root$1.kind !== "query") continue;
|
|
135
|
-
if (procedureName === root$1.procedure && type === "query") queryEntries.push({
|
|
136
|
-
name: queryName,
|
|
137
|
-
procedure: root$1.procedure,
|
|
138
|
-
router,
|
|
139
|
-
type: root$1.type
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
for (const [listName, root$1] of routerRoots) {
|
|
143
|
-
if (root$1.kind !== "list") continue;
|
|
144
|
-
if (procedureName === root$1.procedure && type === "query") listEntries.push({
|
|
145
|
-
list: listName,
|
|
146
|
-
procedure: root$1.procedure,
|
|
147
|
-
router,
|
|
148
|
-
type: root$1.type
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
mutationEntries.sort((a, b) => a.name.localeCompare(b.name));
|
|
154
|
-
byIdEntries.sort((a, b) => a.entityType.localeCompare(b.entityType));
|
|
155
|
-
queryEntries.sort((a, b) => a.name.localeCompare(b.name));
|
|
156
|
-
listEntries.sort((a, b) => a.list.localeCompare(b.list));
|
|
157
|
-
const rootEntries = [
|
|
158
|
-
...byIdEntries.map(({ entityType, router }) => ({
|
|
159
|
-
name: router,
|
|
160
|
-
type: entityType,
|
|
161
|
-
value: `'${router}': clientRoot<RouterOutputs['${router}']['byId'], '${entityType}'>('${entityType}'),`
|
|
162
|
-
})),
|
|
163
|
-
...queryEntries.map(({ name, procedure, router, type }) => ({
|
|
164
|
-
name,
|
|
165
|
-
type,
|
|
166
|
-
value: `'${name}': clientRoot<RouterOutputs['${router}']['${procedure}'], '${type}'>('${type}'),`
|
|
167
|
-
})),
|
|
168
|
-
...listEntries.map(({ list, procedure, router, type }) => ({
|
|
169
|
-
name: list,
|
|
170
|
-
type,
|
|
171
|
-
value: `'${list}': clientRoot<RouterOutputs['${router}']['${procedure}'], '${type}'>('${type}'),`
|
|
172
|
-
}))
|
|
173
|
-
];
|
|
174
|
-
rootEntries.sort((a, b) => a.name.localeCompare(b.name));
|
|
175
|
-
const viewTypes = Array.from(["AppRouter", ...new Set(mutationEntries.map((entry) => entry.entityType))]).sort();
|
|
176
|
-
const mutationResolverLines = mutationEntries.map(({ name, procedure, router }) => `'${name}': (client: TRPCClientType) => client.${router}.${procedure}.mutate,`);
|
|
177
|
-
const mutationConfigLines = mutationEntries.map(({ entityType, name, procedure, router }) => `'${name}': mutation<
|
|
178
|
-
${entityType},
|
|
179
|
-
RouterInputs['${router}']['${procedure}'],
|
|
180
|
-
RouterOutputs['${router}']['${procedure}']
|
|
181
|
-
>('${entityType}'),`);
|
|
182
|
-
const byIdLines = byIdEntries.map(({ entityType, router }) => `${entityType}: (client: TRPCClientType) => ({
|
|
183
|
-
args,
|
|
184
|
-
ids,
|
|
185
|
-
select,
|
|
186
|
-
}: { args?: Record<string, unknown>; ids: Array<string | number>; select: Array<string> }) =>
|
|
187
|
-
client.${router}.byId.query({
|
|
188
|
-
args,
|
|
189
|
-
ids: ids.map(String),
|
|
190
|
-
select,
|
|
191
|
-
}),`);
|
|
192
|
-
const listLines = listEntries.map(({ list, procedure, router }) => `${list}: (client: TRPCClientType) => client.${router}.${procedure}.query,`);
|
|
193
|
-
const queryLines = queryEntries.map(({ name, procedure, router }) => `${name}: (client: TRPCClientType) => client.${router}.${procedure}.query,`);
|
|
194
|
-
const typeImports = `import type { ${viewTypes.join(", ")} } from '${moduleName}';`;
|
|
195
|
-
const typesBlock = indentBlock(formatTypes(types), 6);
|
|
196
|
-
const mutationResolverBlock = indentBlock(mutationResolverLines.join("\n"), 4);
|
|
197
|
-
const mutationConfigBlock = indentBlock(mutationConfigLines.join("\n"), 2);
|
|
198
|
-
const byIdBlock = indentBlock(byIdLines.join("\n"), 8);
|
|
199
|
-
const rootsBlock = indentBlock(rootEntries.map((entry) => entry.value).join("\n"), 2);
|
|
200
|
-
const listsBlockContent = listLines.join("\n");
|
|
201
|
-
const listsBlock = listLines.length ? ` lists: {\n${indentBlock(listsBlockContent, 8)}\n },\n` : "";
|
|
202
|
-
const queriesBlockContent = queryLines.join("\n");
|
|
203
|
-
const source = `// @generated by \`pnpm fate generate\`
|
|
204
|
-
${typeImports}
|
|
205
|
-
import { createTRPCProxyClient } from '@trpc/client';
|
|
206
|
-
import { inferRouterInputs, inferRouterOutputs } from '@trpc/server';
|
|
207
|
-
import { clientRoot, createClient, createTRPCTransport, mutation } from 'react-fate';
|
|
208
|
-
|
|
209
|
-
type TRPCClientType = ReturnType<typeof createTRPCProxyClient<AppRouter>>;
|
|
210
|
-
type RouterInputs = inferRouterInputs<AppRouter>;
|
|
211
|
-
type RouterOutputs = inferRouterOutputs<AppRouter>;
|
|
212
|
-
|
|
213
|
-
const mutations = {
|
|
214
|
-
${mutationConfigBlock}
|
|
215
|
-
} as const;
|
|
216
|
-
|
|
217
|
-
const roots = {
|
|
218
|
-
${rootsBlock}
|
|
219
|
-
} as const;
|
|
220
|
-
|
|
221
|
-
type GeneratedClientMutations = typeof mutations;
|
|
222
|
-
type GeneratedClientRoots = typeof roots;
|
|
223
|
-
|
|
224
|
-
declare module 'react-fate' {
|
|
225
|
-
interface ClientMutations extends GeneratedClientMutations {}
|
|
226
|
-
interface ClientRoots extends GeneratedClientRoots {}
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
export const createFateClient = (options: {
|
|
230
|
-
links: Parameters<typeof createTRPCProxyClient>[0]['links'];
|
|
231
|
-
}) => {
|
|
232
|
-
const trpcClient = createTRPCProxyClient<AppRouter>(options);
|
|
233
|
-
|
|
234
|
-
const trpcMutations = {
|
|
235
|
-
${mutationResolverBlock}
|
|
236
|
-
} as const;
|
|
237
|
-
|
|
238
|
-
return createClient<[GeneratedClientRoots, GeneratedClientMutations]>({
|
|
239
|
-
mutations,
|
|
240
|
-
roots,
|
|
241
|
-
transport: createTRPCTransport<AppRouter, typeof trpcMutations>({
|
|
242
|
-
byId: {
|
|
243
|
-
${byIdBlock}
|
|
244
|
-
},
|
|
245
|
-
client: trpcClient,
|
|
246
|
-
${queryLines.length ? ` queries: {\n${indentBlock(queriesBlockContent, 8)}\n },\n` : ""}${listsBlock} mutations: trpcMutations,
|
|
247
|
-
}),
|
|
248
|
-
types: ${typesBlock.trimStart()},
|
|
249
|
-
});
|
|
250
15
|
};
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
16
|
+
const [command, ...args] = process.argv.slice(2);
|
|
17
|
+
if (command !== "generate" || args.length > 0) usage();
|
|
18
|
+
const configFile = path.join(process.cwd(), "vite.config.ts");
|
|
19
|
+
const plugin = (await resolveConfig({ configFile }, "serve")).plugins.find((candidate) => candidate.name === "@nkzw/fate");
|
|
20
|
+
const load = plugin?.load;
|
|
21
|
+
const loadContext = {};
|
|
22
|
+
const result = typeof load === "function" ? await load.call(loadContext, clientModuleId) : typeof load?.handler === "function" ? await load.handler.call(loadContext, clientModuleId) : null;
|
|
23
|
+
if (!plugin || result == null) usage(`No fate plugin found in '${path.relative(process.cwd(), configFile)}'.`);
|
|
24
|
+
process.stdout.write(styleText("green", ` fate client generated.\n`));
|
|
258
25
|
//#endregion
|
|
259
|
-
export {
|
|
26
|
+
export {};
|