@slotform/api 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +50 -2
- package/dist/client.d.ts +16 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +98 -0
- package/dist/client.js.map +1 -0
- package/dist/error.d.ts +2 -0
- package/dist/error.d.ts.map +1 -0
- package/dist/error.js +2 -0
- package/dist/error.js.map +1 -0
- package/dist/index.d.ts +153 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +640 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/chosen.d.ts +12 -0
- package/dist/lib/chosen.d.ts.map +1 -0
- package/dist/lib/chosen.js +18 -0
- package/dist/lib/chosen.js.map +1 -0
- package/dist/lib/cursor.d.ts +68 -0
- package/dist/lib/cursor.d.ts.map +1 -0
- package/dist/lib/cursor.js +220 -0
- package/dist/lib/cursor.js.map +1 -0
- package/dist/lib/limits.d.ts +4 -0
- package/dist/lib/limits.d.ts.map +1 -0
- package/dist/lib/limits.js +10 -0
- package/dist/lib/limits.js.map +1 -0
- package/dist/lib/local-origin.d.ts +2 -0
- package/dist/lib/local-origin.d.ts.map +1 -0
- package/dist/lib/local-origin.js +17 -0
- package/dist/lib/local-origin.js.map +1 -0
- package/dist/lib/slugify.d.ts +4 -0
- package/dist/lib/slugify.d.ts.map +1 -0
- package/dist/lib/slugify.js +26 -0
- package/dist/lib/slugify.js.map +1 -0
- package/dist/lib/window.d.ts +52 -0
- package/dist/lib/window.d.ts.map +1 -0
- package/dist/lib/window.js +70 -0
- package/dist/lib/window.js.map +1 -0
- package/dist/mcp-path.d.ts +2 -0
- package/dist/mcp-path.d.ts.map +1 -0
- package/dist/mcp-path.js +3 -0
- package/dist/mcp-path.js.map +1 -0
- package/dist/mcp.d.ts +15 -0
- package/dist/mcp.d.ts.map +1 -0
- package/dist/mcp.js +5 -0
- package/dist/mcp.js.map +1 -0
- package/dist/node/barrel.d.ts +3 -0
- package/dist/node/barrel.d.ts.map +1 -0
- package/dist/node/barrel.js +70 -0
- package/dist/node/barrel.js.map +1 -0
- package/dist/node/codegen.d.ts +10 -0
- package/dist/node/codegen.d.ts.map +1 -0
- package/dist/node/codegen.js +132 -0
- package/dist/node/codegen.js.map +1 -0
- package/dist/node/procedure-codegen.d.ts +14 -0
- package/dist/node/procedure-codegen.d.ts.map +1 -0
- package/dist/node/procedure-codegen.js +95 -0
- package/dist/node/procedure-codegen.js.map +1 -0
- package/dist/node/schema-child.d.ts +2 -0
- package/dist/node/schema-child.d.ts.map +1 -0
- package/dist/node/schema-child.js +31 -0
- package/dist/node/schema-child.js.map +1 -0
- package/dist/node/schema-source.d.ts +3 -0
- package/dist/node/schema-source.d.ts.map +1 -0
- package/dist/node/schema-source.js +33 -0
- package/dist/node/schema-source.js.map +1 -0
- package/dist/node/server-client.d.ts +19 -0
- package/dist/node/server-client.d.ts.map +1 -0
- package/dist/node/server-client.js +45 -0
- package/dist/node/server-client.js.map +1 -0
- package/dist/node/types.d.ts +42 -0
- package/dist/node/types.d.ts.map +1 -0
- package/dist/node/types.js +13 -0
- package/dist/node/types.js.map +1 -0
- package/dist/not-found.d.ts +2 -0
- package/dist/not-found.d.ts.map +1 -0
- package/dist/not-found.js +6 -0
- package/dist/not-found.js.map +1 -0
- package/dist/procedure.d.ts +188 -0
- package/dist/procedure.d.ts.map +1 -0
- package/dist/procedure.js +392 -0
- package/dist/procedure.js.map +1 -0
- package/dist/query-invalidation.d.ts +20 -0
- package/dist/query-invalidation.d.ts.map +1 -0
- package/dist/query-invalidation.js +66 -0
- package/dist/query-invalidation.js.map +1 -0
- package/dist/schema.d.ts +32 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +103 -0
- package/dist/schema.js.map +1 -0
- package/dist/types.d.ts +30 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/wire.d.ts +11 -0
- package/dist/wire.d.ts.map +1 -0
- package/dist/wire.js +26 -0
- package/dist/wire.js.map +1 -0
- package/dist/worker/guards.d.ts +6 -0
- package/dist/worker/guards.d.ts.map +1 -0
- package/dist/worker/guards.js +29 -0
- package/dist/worker/guards.js.map +1 -0
- package/dist/worker/index.d.ts +57 -0
- package/dist/worker/index.d.ts.map +1 -0
- package/dist/worker/index.js +418 -0
- package/dist/worker/index.js.map +1 -0
- package/dist/worker/mcp.d.ts +28 -0
- package/dist/worker/mcp.d.ts.map +1 -0
- package/dist/worker/mcp.js +361 -0
- package/dist/worker/mcp.js.map +1 -0
- package/dist/worker/request-log.d.ts +10 -0
- package/dist/worker/request-log.d.ts.map +1 -0
- package/dist/worker/request-log.js +37 -0
- package/dist/worker/request-log.js.map +1 -0
- package/guide/add-a-procedure.md +42 -0
- package/guide/background.md +86 -0
- package/guide/client.md +93 -0
- package/guide/config.md +85 -0
- package/guide/mcp.md +68 -0
- package/guide/middleware.md +79 -0
- package/guide/procedures.md +95 -0
- package/guide/utilities.md +135 -0
- package/package.json +120 -3
- package/src/client.ts +129 -0
- package/src/error.ts +1 -0
- package/src/index.ts +787 -0
- package/src/lib/chosen.ts +31 -0
- package/src/lib/cursor.ts +354 -0
- package/src/lib/limits.ts +10 -0
- package/src/lib/local-origin.ts +18 -0
- package/src/lib/slugify.ts +29 -0
- package/src/lib/window.ts +155 -0
- package/src/mcp-path.ts +2 -0
- package/src/mcp.ts +30 -0
- package/src/node/barrel.ts +77 -0
- package/src/node/codegen.ts +173 -0
- package/src/node/procedure-codegen.ts +131 -0
- package/src/node/schema-child.ts +31 -0
- package/src/node/schema-source.ts +45 -0
- package/src/node/server-client.ts +67 -0
- package/src/node/types.ts +64 -0
- package/src/not-found.ts +6 -0
- package/src/procedure.ts +935 -0
- package/src/query-invalidation.ts +106 -0
- package/src/schema.ts +133 -0
- package/src/types.ts +72 -0
- package/src/wire.ts +34 -0
- package/src/worker/guards.ts +37 -0
- package/src/worker/index.ts +681 -0
- package/src/worker/mcp.ts +470 -0
- package/src/worker/request-log.ts +50 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,787 @@
|
|
|
1
|
+
import { join, posix } from "node:path";
|
|
2
|
+
import type { ContributionCtx } from "@slotform/cli";
|
|
3
|
+
import { plugin, slot } from "@slotform/cli";
|
|
4
|
+
import type {
|
|
5
|
+
MiddlewareSpec,
|
|
6
|
+
TsExpression,
|
|
7
|
+
TsImportSpec,
|
|
8
|
+
TsTypeRef,
|
|
9
|
+
} from "@slotform/cli/ast";
|
|
10
|
+
import { cliSlots, emitArtifact } from "@slotform/cli/cli-slots";
|
|
11
|
+
import { writeArtifact } from "@slotform/cli/write-artifact";
|
|
12
|
+
import { z } from "zod";
|
|
13
|
+
import { isLocalOrigin } from "./lib/local-origin.ts";
|
|
14
|
+
import { MCP_PATH } from "./mcp-path.ts";
|
|
15
|
+
import { generateRouteBarrel, hasRoutableFiles } from "./node/barrel.ts";
|
|
16
|
+
import {
|
|
17
|
+
aggregateWorker,
|
|
18
|
+
middlewareCallsOf,
|
|
19
|
+
middlewareImportsOf,
|
|
20
|
+
} from "./node/codegen.ts";
|
|
21
|
+
import { aggregateProcedure } from "./node/procedure-codegen.ts";
|
|
22
|
+
import { schemaSource } from "./node/schema-source.ts";
|
|
23
|
+
import {
|
|
24
|
+
checkServer,
|
|
25
|
+
readServerFile,
|
|
26
|
+
readServerManifest,
|
|
27
|
+
serverTypesSource as renderServerTypes,
|
|
28
|
+
SERVER_MANIFEST,
|
|
29
|
+
SERVER_SCHEMA,
|
|
30
|
+
SERVER_TYPES,
|
|
31
|
+
} from "./node/server-client.ts";
|
|
32
|
+
import {
|
|
33
|
+
type CallbackSpec,
|
|
34
|
+
importSource,
|
|
35
|
+
type MiddlewareCall,
|
|
36
|
+
type PluginRuntimeEntry,
|
|
37
|
+
PROCEDURE_FILE,
|
|
38
|
+
ROUTES_BARREL,
|
|
39
|
+
ROUTES_BARREL_IMPORT_SOURCE,
|
|
40
|
+
ROUTES_DIR,
|
|
41
|
+
WORKER_FILE,
|
|
42
|
+
type WorkerPayload,
|
|
43
|
+
} from "./node/types.ts";
|
|
44
|
+
import type { EnvSpec } from "./types.ts";
|
|
45
|
+
import { ENTITY_NAME_RE } from "./wire.ts";
|
|
46
|
+
|
|
47
|
+
const envSpecSchema = z.object({
|
|
48
|
+
name: z.string().min(1),
|
|
49
|
+
devDefault: z.string(),
|
|
50
|
+
validate: z
|
|
51
|
+
.object({
|
|
52
|
+
minLength: z.number().int().positive().optional(),
|
|
53
|
+
url: z.boolean().optional(),
|
|
54
|
+
devLocalhost: z.boolean().optional(),
|
|
55
|
+
})
|
|
56
|
+
.optional(),
|
|
57
|
+
}) satisfies z.ZodType<EnvSpec>;
|
|
58
|
+
|
|
59
|
+
const apiOptionsSchema = z.object({
|
|
60
|
+
prefix: z
|
|
61
|
+
.string()
|
|
62
|
+
.refine((p) => p.startsWith("/"), {
|
|
63
|
+
error: "api: prefix must start with /",
|
|
64
|
+
})
|
|
65
|
+
.default("/rpc"),
|
|
66
|
+
// Consumer env vars: docs/architecture/plugins/api.md, "Env vars".
|
|
67
|
+
env: z.array(envSpecSchema).default([]),
|
|
68
|
+
// Entity names outside any plugin's tables: docs/architecture/plugins/api.md, "Entities".
|
|
69
|
+
entities: z
|
|
70
|
+
.array(
|
|
71
|
+
z.string().regex(ENTITY_NAME_RE, {
|
|
72
|
+
error:
|
|
73
|
+
'api: an entity name may only contain letters, digits, "_", ".", and "-"',
|
|
74
|
+
}),
|
|
75
|
+
)
|
|
76
|
+
.default([]),
|
|
77
|
+
// The server this app is a client of: docs/architecture/plugins/api.md, "Client-only apps".
|
|
78
|
+
server: z.string().min(1).optional(),
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
export type ApiOptions = z.input<typeof apiOptionsSchema>;
|
|
82
|
+
|
|
83
|
+
// Every worker fragment is a slot: docs/architecture/plugins/api.md, "Overview".
|
|
84
|
+
|
|
85
|
+
const SOURCE = "api";
|
|
86
|
+
|
|
87
|
+
type ParsedOptions = z.output<typeof apiOptionsSchema>;
|
|
88
|
+
|
|
89
|
+
// The consumer's MCP definition.
|
|
90
|
+
const MCP_FILE = "src/worker/mcp.ts";
|
|
91
|
+
|
|
92
|
+
// The background handlers' convention files: a queue consumer and a cron
|
|
93
|
+
// handler, each answering with the plugin context a procedure gets.
|
|
94
|
+
const QUEUE_FILE = "src/worker/queue.ts";
|
|
95
|
+
const SCHEDULED_FILE = "src/worker/scheduled.ts";
|
|
96
|
+
|
|
97
|
+
// The consumer middleware convention files: docs/architecture/runtime.md, "Middleware".
|
|
98
|
+
const MIDDLEWARE_FILE = "src/worker/middleware.ts";
|
|
99
|
+
const CONTEXT_MIDDLEWARE_FILE = "src/worker/middleware.context.ts";
|
|
100
|
+
|
|
101
|
+
// The consumer files `.slotform/procedure.ts` leaves out: each imports it back,
|
|
102
|
+
// the MCP definition to type itself against the routes and a job handler for
|
|
103
|
+
// `defineQueue`, as the route barrel does.
|
|
104
|
+
const WORKER_ONLY_IMPORTS = new Set([
|
|
105
|
+
ROUTES_BARREL_IMPORT_SOURCE,
|
|
106
|
+
...[MCP_FILE, QUEUE_FILE, SCHEDULED_FILE].map(importSource),
|
|
107
|
+
]);
|
|
108
|
+
|
|
109
|
+
// Sort imports by source so the emitted worker file is independent of
|
|
110
|
+
// `config.plugins` array order. dedupeImports preserves insertion order
|
|
111
|
+
// within a group, so we sort the raw list first.
|
|
112
|
+
const workerImports = slot.list<TsImportSpec>({
|
|
113
|
+
source: SOURCE,
|
|
114
|
+
name: "workerImports",
|
|
115
|
+
sortBy: (a, b) => a.source.localeCompare(b.source),
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
// Runtimes sort by plugin name: docs/architecture/runtime.md, "Chain order".
|
|
119
|
+
const pluginRuntimes = slot.list<PluginRuntimeEntry>({
|
|
120
|
+
source: SOURCE,
|
|
121
|
+
name: "pluginRuntimes",
|
|
122
|
+
sortBy: (a, b) => a.plugin.localeCompare(b.plugin),
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
const middlewareEntries = slot.list<MiddlewareSpec>({
|
|
126
|
+
source: SOURCE,
|
|
127
|
+
name: "middlewareEntries",
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// Derived view of middleware: sorted calls, each carrying the builder method
|
|
131
|
+
// that mounts it (`use` / `useAfterContext`).
|
|
132
|
+
const middlewareCalls = slot.derived({
|
|
133
|
+
source: SOURCE,
|
|
134
|
+
name: "middlewareCalls",
|
|
135
|
+
inputs: { entries: middlewareEntries },
|
|
136
|
+
compute: (inp): MiddlewareCall[] => middlewareCallsOf(inp.entries),
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
const middlewareImports = slot.derived({
|
|
140
|
+
source: SOURCE,
|
|
141
|
+
name: "middlewareImports",
|
|
142
|
+
inputs: { entries: middlewareEntries },
|
|
143
|
+
compute: (inp): TsImportSpec[] => middlewareImportsOf(inp.entries),
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
// The routes handler is gated on routable files, as the barrel is: docs/architecture/runtime.md, "Chain order".
|
|
147
|
+
const routesHandler = slot.value<{ identifier: string } | null>({
|
|
148
|
+
source: SOURCE,
|
|
149
|
+
name: "routesHandler",
|
|
150
|
+
seed: (ctx) => (hasRoutableFiles(ctx.cwd) ? { identifier: "routes" } : null),
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
// MCP auth flag: docs/architecture/slot-catalog.md, "`api.slots.*` (the `api` plugin)".
|
|
154
|
+
const mcpAuth = slot.value<boolean>({
|
|
155
|
+
source: SOURCE,
|
|
156
|
+
name: "mcpAuth",
|
|
157
|
+
override: true,
|
|
158
|
+
seed: () => false,
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
// Whether the server has `auth()`: docs/architecture/plugins/api.md, "Client-only apps".
|
|
162
|
+
const serverAuth = slot.value<boolean, ParsedOptions>({
|
|
163
|
+
source: SOURCE,
|
|
164
|
+
name: "serverAuth",
|
|
165
|
+
seed: async (ctx) =>
|
|
166
|
+
ctx.options.server === undefined
|
|
167
|
+
? false
|
|
168
|
+
: (await readServerManifest(ctx, ctx.options.server)).auth,
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
// A client app's `.slotform/schema.ts`: docs/architecture/plugins/api.md, "Client-only apps".
|
|
172
|
+
const serverSchemaSource = slot.derived({
|
|
173
|
+
source: SOURCE,
|
|
174
|
+
name: "serverSchemaSource",
|
|
175
|
+
compute: async (
|
|
176
|
+
_inp,
|
|
177
|
+
ctx: ContributionCtx<ParsedOptions>,
|
|
178
|
+
): Promise<string | null> => {
|
|
179
|
+
const { server } = ctx.options;
|
|
180
|
+
if (server === undefined) return null;
|
|
181
|
+
if (hasRoutableFiles(ctx.cwd)) {
|
|
182
|
+
throw new Error(
|
|
183
|
+
`api: \`server: "${server}"\` makes this app a client, which serves no procedures: remove ${ROUTES_DIR} or the option.`,
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
await checkServer(ctx, server);
|
|
187
|
+
return readServerFile(ctx, server, SERVER_SCHEMA);
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
// `.slotform/server.ts` of a client app: the server's `AppRouter` type.
|
|
192
|
+
const serverTypesSource = slot.derived({
|
|
193
|
+
source: SOURCE,
|
|
194
|
+
name: "serverTypesSource",
|
|
195
|
+
compute: (_inp, ctx: ContributionCtx<ParsedOptions>): string | null =>
|
|
196
|
+
ctx.options.server === undefined
|
|
197
|
+
? null
|
|
198
|
+
: renderServerTypes(ctx.options.server),
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
// The MCP endpoint's mount: set when `src/worker/mcp.ts` exists, and then
|
|
202
|
+
// refused unless every part it stands on is there. null without the file.
|
|
203
|
+
const mcpMount = slot.derived({
|
|
204
|
+
source: SOURCE,
|
|
205
|
+
name: "mcpMount",
|
|
206
|
+
inputs: { auth: mcpAuth, handler: routesHandler },
|
|
207
|
+
compute: async (
|
|
208
|
+
inp,
|
|
209
|
+
ctx: ContributionCtx<ParsedOptions>,
|
|
210
|
+
): Promise<{ identifier: string; name: string } | null> => {
|
|
211
|
+
if (!(await ctx.fileExists(MCP_FILE))) return null;
|
|
212
|
+
if (!inp.auth) {
|
|
213
|
+
throw new Error(
|
|
214
|
+
`api: ${MCP_FILE} serves MCP, which authenticates through the OAuth provider: set auth({ mcp: true }) in slotform.config.ts.`,
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
if (inp.handler === null) {
|
|
218
|
+
throw new Error(
|
|
219
|
+
`api: ${MCP_FILE} lists procedures of ${ROUTES_DIR}, which has no routable file.`,
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
if (ctx.options.prefix === MCP_PATH) {
|
|
223
|
+
throw new Error(
|
|
224
|
+
`api: the prefix ${MCP_PATH} is where the MCP endpoint answers; pick another api({ prefix }).`,
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
return { identifier: "mcp", name: ctx.app.name };
|
|
228
|
+
},
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
// A background handler's mount: docs/architecture/runtime.md, "Background handlers".
|
|
232
|
+
function handlerMount<TIdentifier extends string>(
|
|
233
|
+
name: string,
|
|
234
|
+
file: string,
|
|
235
|
+
identifier: TIdentifier,
|
|
236
|
+
) {
|
|
237
|
+
return slot.derived({
|
|
238
|
+
source: SOURCE,
|
|
239
|
+
name,
|
|
240
|
+
inputs: { handler: routesHandler, runtimes: pluginRuntimes },
|
|
241
|
+
compute: async (
|
|
242
|
+
inp,
|
|
243
|
+
ctx: ContributionCtx<ParsedOptions>,
|
|
244
|
+
): Promise<{ identifier: TIdentifier } | null> => {
|
|
245
|
+
if (!(await ctx.fileExists(file))) return null;
|
|
246
|
+
if (inp.handler === null && inp.runtimes.length === 0) {
|
|
247
|
+
throw new Error(
|
|
248
|
+
`api: ${file} runs in the worker, which has no routable file in ${ROUTES_DIR} and no plugin runtime.`,
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
return { identifier };
|
|
252
|
+
},
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const queueHandler = handlerMount("queueHandler", QUEUE_FILE, "queue");
|
|
257
|
+
|
|
258
|
+
// The cron handler's mount (`src/worker/scheduled.ts`).
|
|
259
|
+
const scheduledHandler = handlerMount(
|
|
260
|
+
"scheduledHandler",
|
|
261
|
+
SCHEDULED_FILE,
|
|
262
|
+
"scheduled",
|
|
263
|
+
);
|
|
264
|
+
|
|
265
|
+
// Frontend-contributed CORS origins: docs/architecture/plugins/api.md, "The production list".
|
|
266
|
+
const corsOrigins = slot.list<string>({
|
|
267
|
+
source: SOURCE,
|
|
268
|
+
name: "corsOrigins",
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
// Dev-only origins: docs/architecture/plugins/api.md, "Local and dev origins".
|
|
272
|
+
const devCorsOrigins = slot.list<string>({
|
|
273
|
+
source: SOURCE,
|
|
274
|
+
name: "devCorsOrigins",
|
|
275
|
+
sortBy: (a, b) => a.localeCompare(b),
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
// Dev origins of the deploy targets: docs/architecture/plugins/api.md, "Local and dev origins".
|
|
279
|
+
const devTargetOrigins = slot.list<string>({
|
|
280
|
+
source: SOURCE,
|
|
281
|
+
name: "devTargetOrigins",
|
|
282
|
+
sortBy: (a, b) => a.localeCompare(b),
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
// The native client's deep-link scheme: docs/architecture/plugins/api.md, "Local and dev origins".
|
|
286
|
+
const nativeScheme = slot.value<string | null>({
|
|
287
|
+
source: SOURCE,
|
|
288
|
+
name: "nativeScheme",
|
|
289
|
+
seed: () => null,
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
// Where local origins belong: docs/architecture/plugins/api.md, "Local and dev origins".
|
|
293
|
+
const localOrigins = slot.value<"dev" | "deployed">({
|
|
294
|
+
source: SOURCE,
|
|
295
|
+
name: "localOrigins",
|
|
296
|
+
seed: () => "dev",
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
// Worker-owned URL prefixes: docs/architecture/plugins/api.md, "Worker-owned URL prefixes".
|
|
300
|
+
const routePrefixes = slot.list<string>({
|
|
301
|
+
source: SOURCE,
|
|
302
|
+
name: "routePrefixes",
|
|
303
|
+
sortBy: (a, b) => a.localeCompare(b),
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
// The oRPC endpoint's prefix (`api({ prefix })`, `/rpc` by default): the one
|
|
307
|
+
// of `routePrefixes` a client's procedure calls go to.
|
|
308
|
+
const rpcPrefix = slot.value<string, ParsedOptions>({
|
|
309
|
+
source: SOURCE,
|
|
310
|
+
name: "rpcPrefix",
|
|
311
|
+
seed: (ctx) => ctx.options.prefix,
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
// The final CORS list: docs/architecture/plugins/api.md, "The production list".
|
|
315
|
+
const cors = slot.derived({
|
|
316
|
+
source: SOURCE,
|
|
317
|
+
name: "cors",
|
|
318
|
+
inputs: { extras: corsOrigins, local: localOrigins },
|
|
319
|
+
compute: (inp, ctx): string[] => {
|
|
320
|
+
// Local origins in an explicit list move to `devCorsOrigins`: docs/architecture/plugins/api.md, "Local and dev origins".
|
|
321
|
+
const result =
|
|
322
|
+
ctx.app.origins !== undefined
|
|
323
|
+
? ctx.app.origins.filter(
|
|
324
|
+
(origin) => inp.local === "deployed" || !isLocalOrigin(origin),
|
|
325
|
+
)
|
|
326
|
+
: [
|
|
327
|
+
`https://${ctx.app.domain}`,
|
|
328
|
+
`https://app.${ctx.app.domain}`,
|
|
329
|
+
...inp.extras,
|
|
330
|
+
];
|
|
331
|
+
const hasWildcard = result.includes("*");
|
|
332
|
+
const hasSpecific = result.some((o) => o !== "*");
|
|
333
|
+
if (hasWildcard && hasSpecific) {
|
|
334
|
+
throw new Error(
|
|
335
|
+
`api.cors: "*" mixed with specific origins (${result
|
|
336
|
+
.filter((o) => o !== "*")
|
|
337
|
+
.join(
|
|
338
|
+
", ",
|
|
339
|
+
)}); wildcard semantics are undefined when combined with explicit origins.`,
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
return result;
|
|
343
|
+
},
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
// Callback files for a peer plugin's runtime: docs/architecture/plugins/api.md, "Callbacks".
|
|
347
|
+
const callbacks = slot.map<CallbackSpec>({
|
|
348
|
+
source: SOURCE,
|
|
349
|
+
name: "callbacks",
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
// Env vars the worker reads: docs/architecture/plugins/api.md, "Env vars".
|
|
353
|
+
const env = slot.list<EnvSpec>({
|
|
354
|
+
source: SOURCE,
|
|
355
|
+
name: "env",
|
|
356
|
+
uniqueBy: (e) => e.name,
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
// The worker's `env` type: docs/architecture/plugins/api.md, "Runtime".
|
|
360
|
+
const envType = slot.value<TsTypeRef | null>({
|
|
361
|
+
source: SOURCE,
|
|
362
|
+
name: "envType",
|
|
363
|
+
override: true,
|
|
364
|
+
seed: () => null,
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
// The root builder call. Derived from cors + options so worker options
|
|
368
|
+
// (prefix / cors) are baked in purely from dataflow.
|
|
369
|
+
const workerBase = slot.derived({
|
|
370
|
+
source: SOURCE,
|
|
371
|
+
name: "workerBase",
|
|
372
|
+
inputs: {
|
|
373
|
+
cors,
|
|
374
|
+
devCors: devCorsOrigins,
|
|
375
|
+
devTargets: devTargetOrigins,
|
|
376
|
+
env,
|
|
377
|
+
envType,
|
|
378
|
+
},
|
|
379
|
+
compute: (inp, ctx: ContributionCtx<ParsedOptions>): TsExpression => {
|
|
380
|
+
const properties: Array<{ key: string; value: TsExpression }> = [
|
|
381
|
+
{
|
|
382
|
+
key: "prefix",
|
|
383
|
+
value: { kind: "string", value: ctx.options.prefix },
|
|
384
|
+
},
|
|
385
|
+
];
|
|
386
|
+
// `cors` is always emitted, an empty list included: docs/architecture/plugins/api.md, "The production list".
|
|
387
|
+
properties.push({
|
|
388
|
+
key: "cors",
|
|
389
|
+
value: {
|
|
390
|
+
kind: "array",
|
|
391
|
+
items: inp.cors.map((o) => ({ kind: "string", value: o })),
|
|
392
|
+
},
|
|
393
|
+
});
|
|
394
|
+
// `devCors` is omitted when empty: docs/architecture/plugins/api.md, "Local and dev origins".
|
|
395
|
+
const devCors = [...inp.devCors, ...inp.devTargets];
|
|
396
|
+
if (devCors.length > 0) {
|
|
397
|
+
properties.push({
|
|
398
|
+
key: "devCors",
|
|
399
|
+
value: {
|
|
400
|
+
kind: "array",
|
|
401
|
+
items: devCors.map((o) => ({ kind: "string", value: o })),
|
|
402
|
+
},
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
// Env value assertions: docs/architecture/plugins/api.md, "Env vars".
|
|
406
|
+
if (inp.env.length > 0) {
|
|
407
|
+
properties.push({
|
|
408
|
+
key: "envChecks",
|
|
409
|
+
value: {
|
|
410
|
+
kind: "array",
|
|
411
|
+
items: inp.env.map((s) => {
|
|
412
|
+
const props: Array<{ key: string; value: TsExpression }> = [
|
|
413
|
+
{ key: "name", value: { kind: "string", value: s.name } },
|
|
414
|
+
];
|
|
415
|
+
if (s.validate?.minLength !== undefined) {
|
|
416
|
+
props.push({
|
|
417
|
+
key: "minLength",
|
|
418
|
+
value: { kind: "number", value: s.validate.minLength },
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
if (s.validate?.url) {
|
|
422
|
+
props.push({
|
|
423
|
+
key: "url",
|
|
424
|
+
value: { kind: "boolean", value: true },
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
if (s.validate?.devLocalhost) {
|
|
428
|
+
props.push({
|
|
429
|
+
key: "devLocalhost",
|
|
430
|
+
value: { kind: "boolean", value: true },
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
return { kind: "object" as const, properties: props };
|
|
434
|
+
}),
|
|
435
|
+
},
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
return {
|
|
439
|
+
kind: "call",
|
|
440
|
+
callee: { kind: "identifier", name: "createWorker" },
|
|
441
|
+
args: [{ kind: "object", properties }],
|
|
442
|
+
...(inp.envType ? { typeArgs: [inp.envType] } : {}),
|
|
443
|
+
};
|
|
444
|
+
},
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
// RBAC statements for `rbac` and `can`: docs/architecture/slot-catalog.md, "`api.slots.*` (the `api` plugin)".
|
|
448
|
+
const rbacStatements = slot.value<Record<string, readonly string[]> | null>({
|
|
449
|
+
source: SOURCE,
|
|
450
|
+
name: "rbacStatements",
|
|
451
|
+
override: true,
|
|
452
|
+
seed: () => null,
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
// The entity vocabulary: docs/architecture/plugins/api.md, "Entities".
|
|
456
|
+
const entities = slot.list<string>({
|
|
457
|
+
source: SOURCE,
|
|
458
|
+
name: "entities",
|
|
459
|
+
sortBy: (a, b) => a.localeCompare(b),
|
|
460
|
+
uniqueBy: (e) => e,
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
// The routes barrel is null with nothing to barrel: docs/architecture/plugins/api.md, "Routes barrel".
|
|
464
|
+
const routeBarrelSource = slot.derived({
|
|
465
|
+
source: SOURCE,
|
|
466
|
+
name: "routeBarrelSource",
|
|
467
|
+
compute: (_inp, ctx): string | null => {
|
|
468
|
+
if (!hasRoutableFiles(ctx.cwd)) return null;
|
|
469
|
+
return generateRouteBarrel(ctx.cwd);
|
|
470
|
+
},
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
// The `.slotform/worker.ts` source: docs/architecture/plugins/api.md, "Generated files".
|
|
474
|
+
const workerSource = slot.derived({
|
|
475
|
+
source: SOURCE,
|
|
476
|
+
name: "workerSource",
|
|
477
|
+
inputs: {
|
|
478
|
+
imports: workerImports,
|
|
479
|
+
base: workerBase,
|
|
480
|
+
runtimes: pluginRuntimes,
|
|
481
|
+
middlewareCalls,
|
|
482
|
+
middlewareImports,
|
|
483
|
+
handler: routesHandler,
|
|
484
|
+
mcp: mcpMount,
|
|
485
|
+
queue: queueHandler,
|
|
486
|
+
scheduled: scheduledHandler,
|
|
487
|
+
callbacks,
|
|
488
|
+
},
|
|
489
|
+
compute: (inp): string | null => {
|
|
490
|
+
// No runtimes AND no routes — nothing would actually run. Return null
|
|
491
|
+
// so the file-emission contribution skips writing a hollow worker.
|
|
492
|
+
// Routes alone are a real worker (a routes-only consumer with no
|
|
493
|
+
// db/auth still serves its procedures).
|
|
494
|
+
if (inp.runtimes.length === 0 && inp.handler === null) return null;
|
|
495
|
+
|
|
496
|
+
const payload: WorkerPayload = {
|
|
497
|
+
imports: [...inp.imports, ...inp.middlewareImports],
|
|
498
|
+
base: inp.base,
|
|
499
|
+
pluginRuntimes: inp.runtimes,
|
|
500
|
+
middlewareChain: inp.middlewareCalls,
|
|
501
|
+
handler: inp.handler,
|
|
502
|
+
mcp: inp.mcp,
|
|
503
|
+
queue: inp.queue,
|
|
504
|
+
scheduled: inp.scheduled,
|
|
505
|
+
callbacks: inp.callbacks,
|
|
506
|
+
};
|
|
507
|
+
return aggregateWorker(payload);
|
|
508
|
+
},
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
// The `.slotform/procedure.ts` source and its gate: docs/architecture/runtime.md, "`virtual:slotform-procedure`".
|
|
512
|
+
// The chain it rebuilds: docs/architecture/runtime.md, "The rebuilt chain".
|
|
513
|
+
const procedureSource = slot.derived({
|
|
514
|
+
source: SOURCE,
|
|
515
|
+
name: "procedureSource",
|
|
516
|
+
inputs: {
|
|
517
|
+
base: workerBase,
|
|
518
|
+
runtimes: pluginRuntimes,
|
|
519
|
+
imports: workerImports,
|
|
520
|
+
middlewareCalls,
|
|
521
|
+
middlewareImports,
|
|
522
|
+
statements: rbacStatements,
|
|
523
|
+
entities,
|
|
524
|
+
handler: routesHandler,
|
|
525
|
+
},
|
|
526
|
+
compute: (inp): string | null => {
|
|
527
|
+
if (inp.runtimes.length === 0 && inp.handler === null) return null;
|
|
528
|
+
const procedureImports = (imports: TsImportSpec[]) =>
|
|
529
|
+
imports.filter((imp) => !WORKER_ONLY_IMPORTS.has(imp.source));
|
|
530
|
+
return aggregateProcedure({
|
|
531
|
+
base: inp.base,
|
|
532
|
+
runtimes: inp.runtimes,
|
|
533
|
+
imports: procedureImports(inp.imports),
|
|
534
|
+
middlewareChain: inp.middlewareCalls,
|
|
535
|
+
middlewareImports: procedureImports(inp.middlewareImports),
|
|
536
|
+
statements: inp.statements,
|
|
537
|
+
entities: inp.entities,
|
|
538
|
+
});
|
|
539
|
+
},
|
|
540
|
+
});
|
|
541
|
+
|
|
542
|
+
export const api = plugin("api", {
|
|
543
|
+
label: "API",
|
|
544
|
+
|
|
545
|
+
schema: apiOptionsSchema,
|
|
546
|
+
// A worker runs on exactly one server target, cloudflare by default; a client of another app's server (`server`) runs none.
|
|
547
|
+
requires: (options) =>
|
|
548
|
+
options.server === undefined ? [{ oneOf: ["cloudflare", "node"] }] : [],
|
|
549
|
+
|
|
550
|
+
guide: [
|
|
551
|
+
{
|
|
552
|
+
page: "add-a-procedure",
|
|
553
|
+
trigger: "Adding an endpoint the app calls",
|
|
554
|
+
},
|
|
555
|
+
{
|
|
556
|
+
page: "procedures",
|
|
557
|
+
trigger:
|
|
558
|
+
"Writing or changing a procedure: its `procedure()` options, input, output, middleware or errors",
|
|
559
|
+
},
|
|
560
|
+
{
|
|
561
|
+
page: "middleware",
|
|
562
|
+
trigger:
|
|
563
|
+
"Guarding every request, or adding a raw route outside the RPC tree (an upload, a webhook)",
|
|
564
|
+
},
|
|
565
|
+
{
|
|
566
|
+
page: "mcp",
|
|
567
|
+
trigger:
|
|
568
|
+
"Letting an MCP client call procedures: `src/worker/mcp.ts`, or `defineMcp`",
|
|
569
|
+
},
|
|
570
|
+
{
|
|
571
|
+
page: "background",
|
|
572
|
+
trigger: "Running work outside a request: a queue message or a cron",
|
|
573
|
+
},
|
|
574
|
+
{
|
|
575
|
+
page: "client",
|
|
576
|
+
trigger:
|
|
577
|
+
"Calling the API from the app: the typed client, queries, drawing a not-found state or cache invalidation",
|
|
578
|
+
},
|
|
579
|
+
{
|
|
580
|
+
page: "config",
|
|
581
|
+
trigger:
|
|
582
|
+
"Changing `api()`'s options, or adding an env var the worker code reads",
|
|
583
|
+
},
|
|
584
|
+
{
|
|
585
|
+
page: "utilities",
|
|
586
|
+
trigger:
|
|
587
|
+
"Paginating a list procedure (cursor or window), sorting it, acting on a selection of its query, or turning a name into a slug",
|
|
588
|
+
},
|
|
589
|
+
],
|
|
590
|
+
|
|
591
|
+
slots: {
|
|
592
|
+
workerImports,
|
|
593
|
+
pluginRuntimes,
|
|
594
|
+
middlewareEntries,
|
|
595
|
+
middlewareCalls,
|
|
596
|
+
middlewareImports,
|
|
597
|
+
routesHandler,
|
|
598
|
+
mcpAuth,
|
|
599
|
+
serverAuth,
|
|
600
|
+
serverSchemaSource,
|
|
601
|
+
serverTypesSource,
|
|
602
|
+
mcpMount,
|
|
603
|
+
queueHandler,
|
|
604
|
+
scheduledHandler,
|
|
605
|
+
corsOrigins,
|
|
606
|
+
devCorsOrigins,
|
|
607
|
+
devTargetOrigins,
|
|
608
|
+
nativeScheme,
|
|
609
|
+
routePrefixes,
|
|
610
|
+
rpcPrefix,
|
|
611
|
+
cors,
|
|
612
|
+
callbacks,
|
|
613
|
+
env,
|
|
614
|
+
envType,
|
|
615
|
+
workerBase,
|
|
616
|
+
workerSource,
|
|
617
|
+
routeBarrelSource,
|
|
618
|
+
rbacStatements,
|
|
619
|
+
localOrigins,
|
|
620
|
+
entities,
|
|
621
|
+
procedureSource,
|
|
622
|
+
},
|
|
623
|
+
|
|
624
|
+
contributes: (self) => [
|
|
625
|
+
// The oRPC prefix is a worker-owned URL space; deploy targets read
|
|
626
|
+
// routePrefixes to mount or forward it.
|
|
627
|
+
self.slots.routePrefixes.contribute(() => self.options.prefix),
|
|
628
|
+
// `/mcp` is the endpoint's own, when it is served.
|
|
629
|
+
self.slots.routePrefixes.contribute(async (ctx) =>
|
|
630
|
+
(await ctx.resolve(self.slots.mcpMount)) ? MCP_PATH : undefined,
|
|
631
|
+
),
|
|
632
|
+
self.slots.env.contribute(() => self.options.env),
|
|
633
|
+
self.slots.entities.contribute(() => self.options.entities),
|
|
634
|
+
// Local origins the consumer listed in `app.origins`: docs/architecture/plugins/api.md, "Local and dev origins".
|
|
635
|
+
self.slots.devCorsOrigins.contribute(async (ctx) =>
|
|
636
|
+
(await ctx.resolve(self.slots.localOrigins)) === "dev"
|
|
637
|
+
? ctx.app.origins?.filter(isLocalOrigin)
|
|
638
|
+
: undefined,
|
|
639
|
+
),
|
|
640
|
+
// Always import `createWorker` — the base call uses it verbatim.
|
|
641
|
+
self.slots.workerImports.contribute(
|
|
642
|
+
(): TsImportSpec => ({
|
|
643
|
+
source: "@slotform/api/runtime",
|
|
644
|
+
default: "createWorker",
|
|
645
|
+
}),
|
|
646
|
+
),
|
|
647
|
+
|
|
648
|
+
// The routes import follows `routesHandler`, so the import and the
|
|
649
|
+
// `.handler(routes)` call never disagree.
|
|
650
|
+
self.slots.workerImports.contribute(async (ctx) => {
|
|
651
|
+
const handler = await ctx.resolve(self.slots.routesHandler);
|
|
652
|
+
if (!handler) return undefined;
|
|
653
|
+
return {
|
|
654
|
+
source: ROUTES_BARREL_IMPORT_SOURCE,
|
|
655
|
+
namespace: handler.identifier,
|
|
656
|
+
};
|
|
657
|
+
}),
|
|
658
|
+
|
|
659
|
+
// The consumer's MCP definition: its default export, passed to `.handler`.
|
|
660
|
+
self.slots.workerImports.contribute(
|
|
661
|
+
async (ctx): Promise<TsImportSpec | undefined> => {
|
|
662
|
+
const mount = await ctx.resolve(self.slots.mcpMount);
|
|
663
|
+
if (!mount) return undefined;
|
|
664
|
+
return { source: importSource(MCP_FILE), default: mount.identifier };
|
|
665
|
+
},
|
|
666
|
+
),
|
|
667
|
+
|
|
668
|
+
// The consumer's background handlers: each default export, passed to
|
|
669
|
+
// `.handler`.
|
|
670
|
+
self.slots.workerImports.contribute(
|
|
671
|
+
async (ctx): Promise<TsImportSpec | undefined> => {
|
|
672
|
+
const mount = await ctx.resolve(self.slots.queueHandler);
|
|
673
|
+
if (!mount) return undefined;
|
|
674
|
+
return { source: importSource(QUEUE_FILE), default: mount.identifier };
|
|
675
|
+
},
|
|
676
|
+
),
|
|
677
|
+
self.slots.workerImports.contribute(
|
|
678
|
+
async (ctx): Promise<TsImportSpec | undefined> => {
|
|
679
|
+
const mount = await ctx.resolve(self.slots.scheduledHandler);
|
|
680
|
+
if (!mount) return undefined;
|
|
681
|
+
return {
|
|
682
|
+
source: importSource(SCHEDULED_FILE),
|
|
683
|
+
default: mount.identifier,
|
|
684
|
+
};
|
|
685
|
+
},
|
|
686
|
+
),
|
|
687
|
+
|
|
688
|
+
// Consumer middleware convention files: docs/architecture/runtime.md, "Middleware".
|
|
689
|
+
self.slots.middlewareEntries.contribute(async (ctx) => {
|
|
690
|
+
if (!(await ctx.fileExists(MIDDLEWARE_FILE))) return undefined;
|
|
691
|
+
return {
|
|
692
|
+
imports: [
|
|
693
|
+
{ source: importSource(MIDDLEWARE_FILE), default: "middleware" },
|
|
694
|
+
],
|
|
695
|
+
call: { kind: "identifier", name: "middleware" },
|
|
696
|
+
phase: "before-routes",
|
|
697
|
+
order: 100,
|
|
698
|
+
} as MiddlewareSpec;
|
|
699
|
+
}),
|
|
700
|
+
|
|
701
|
+
// `middleware.context.ts` runs after context injection: docs/architecture/runtime.md, "Middleware".
|
|
702
|
+
self.slots.middlewareEntries.contribute(async (ctx) => {
|
|
703
|
+
if (!(await ctx.fileExists(CONTEXT_MIDDLEWARE_FILE))) return undefined;
|
|
704
|
+
return {
|
|
705
|
+
imports: [
|
|
706
|
+
{
|
|
707
|
+
source: importSource(CONTEXT_MIDDLEWARE_FILE),
|
|
708
|
+
default: "contextMiddleware",
|
|
709
|
+
},
|
|
710
|
+
],
|
|
711
|
+
call: { kind: "identifier", name: "contextMiddleware" },
|
|
712
|
+
phase: "after-context",
|
|
713
|
+
order: 100,
|
|
714
|
+
} as MiddlewareSpec;
|
|
715
|
+
}),
|
|
716
|
+
|
|
717
|
+
// The `virtual:slotform-procedure` alias: docs/architecture/runtime.md, "`virtual:slotform-procedure`".
|
|
718
|
+
cliSlots.tsconfigPaths.contribute(() => ({
|
|
719
|
+
"virtual:slotform-procedure": [`./${PROCEDURE_FILE}`],
|
|
720
|
+
})),
|
|
721
|
+
|
|
722
|
+
// Both are null, and so not written, with no runtime and no route.
|
|
723
|
+
emitArtifact(WORKER_FILE, self.slots.workerSource),
|
|
724
|
+
emitArtifact(PROCEDURE_FILE, self.slots.procedureSource),
|
|
725
|
+
|
|
726
|
+
// `.slotform/schema.ts` and `.slotform/server.json`: docs/architecture/plugins/api.md, "Schema".
|
|
727
|
+
cliSlots.postWrite.contribute(async (ctx) => {
|
|
728
|
+
const auth = await ctx.resolve(self.slots.serverAuth);
|
|
729
|
+
return async () => {
|
|
730
|
+
const source = await schemaSource(ctx.cwd, ctx.env);
|
|
731
|
+
if (source === null) return;
|
|
732
|
+
await writeArtifact(join(ctx.cwd, SERVER_SCHEMA), source);
|
|
733
|
+
await writeArtifact(
|
|
734
|
+
join(ctx.cwd, SERVER_MANIFEST),
|
|
735
|
+
`${JSON.stringify({ auth })}\n`,
|
|
736
|
+
);
|
|
737
|
+
};
|
|
738
|
+
}),
|
|
739
|
+
|
|
740
|
+
// A client app's copies of the server's schema and router type.
|
|
741
|
+
emitArtifact(SERVER_SCHEMA, self.slots.serverSchemaSource),
|
|
742
|
+
emitArtifact(SERVER_TYPES, self.slots.serverTypesSource),
|
|
743
|
+
cliSlots.devWatchers.contribute((ctx) => {
|
|
744
|
+
const { server } = self.options;
|
|
745
|
+
if (server === undefined) return undefined;
|
|
746
|
+
return {
|
|
747
|
+
name: "server-schema",
|
|
748
|
+
paths: posix.join(posix.normalize(server), SERVER_SCHEMA),
|
|
749
|
+
debounce: 300,
|
|
750
|
+
async handler() {
|
|
751
|
+
await writeArtifact(
|
|
752
|
+
join(ctx.cwd, SERVER_SCHEMA),
|
|
753
|
+
await readServerFile(ctx, server, SERVER_SCHEMA),
|
|
754
|
+
);
|
|
755
|
+
ctx.log.info("Server schema copied");
|
|
756
|
+
},
|
|
757
|
+
};
|
|
758
|
+
}),
|
|
759
|
+
|
|
760
|
+
// The route barrel: docs/architecture/plugins/api.md, "Routes barrel".
|
|
761
|
+
emitArtifact(ROUTES_BARREL, self.slots.routeBarrelSource),
|
|
762
|
+
|
|
763
|
+
// In dev the barrel follows the directory, an emptied one included: the
|
|
764
|
+
// running worker imports it until the next generate.
|
|
765
|
+
cliSlots.devWatchers.contribute((ctx) => ({
|
|
766
|
+
name: "routes",
|
|
767
|
+
paths: `${ROUTES_DIR}/**`,
|
|
768
|
+
ignore: ["**/index.ts"],
|
|
769
|
+
debounce: 300,
|
|
770
|
+
async handler(_path, type) {
|
|
771
|
+
if (type !== "add" && type !== "unlink") return;
|
|
772
|
+
await writeArtifact(
|
|
773
|
+
join(ctx.cwd, ROUTES_BARREL),
|
|
774
|
+
generateRouteBarrel(ctx.cwd),
|
|
775
|
+
);
|
|
776
|
+
ctx.log.info("Route barrel regenerated");
|
|
777
|
+
},
|
|
778
|
+
})),
|
|
779
|
+
|
|
780
|
+
// Remove: clean the routes directory on `slotform remove api`.
|
|
781
|
+
cliSlots.removeFiles.contribute(() => `${ROUTES_DIR}/`),
|
|
782
|
+
],
|
|
783
|
+
});
|
|
784
|
+
|
|
785
|
+
export { ApiError } from "./error.ts";
|
|
786
|
+
export type { CallbackSpec, PluginRuntimeEntry } from "./node/types.ts";
|
|
787
|
+
export type { Middleware } from "./procedure.ts";
|