everything-dev 1.26.1 → 1.28.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.
Files changed (166) hide show
  1. package/dist/api-contract.cjs.map +1 -1
  2. package/dist/api-contract.mjs.map +1 -1
  3. package/dist/cli/catalog.cjs.map +1 -1
  4. package/dist/cli/catalog.mjs.map +1 -1
  5. package/dist/cli/framework-version.cjs.map +1 -1
  6. package/dist/cli/framework-version.mjs.map +1 -1
  7. package/dist/cli/infra.cjs +1 -1
  8. package/dist/cli/infra.cjs.map +1 -1
  9. package/dist/cli/infra.mjs +1 -1
  10. package/dist/cli/infra.mjs.map +1 -1
  11. package/dist/cli/init.cjs +127 -121
  12. package/dist/cli/init.cjs.map +1 -1
  13. package/dist/cli/init.d.cts +6 -6
  14. package/dist/cli/init.d.cts.map +1 -1
  15. package/dist/cli/init.d.mts +6 -6
  16. package/dist/cli/init.d.mts.map +1 -1
  17. package/dist/cli/init.mjs +125 -121
  18. package/dist/cli/init.mjs.map +1 -1
  19. package/dist/cli/parse.cjs.map +1 -1
  20. package/dist/cli/parse.mjs.map +1 -1
  21. package/dist/cli/prompts.cjs +29 -25
  22. package/dist/cli/prompts.cjs.map +1 -1
  23. package/dist/cli/prompts.mjs +28 -25
  24. package/dist/cli/prompts.mjs.map +1 -1
  25. package/dist/cli/status.cjs.map +1 -1
  26. package/dist/cli/status.mjs.map +1 -1
  27. package/dist/cli/sync.cjs +46 -93
  28. package/dist/cli/sync.cjs.map +1 -1
  29. package/dist/cli/sync.mjs +49 -96
  30. package/dist/cli/sync.mjs.map +1 -1
  31. package/dist/cli/timing.cjs.map +1 -1
  32. package/dist/cli/timing.mjs.map +1 -1
  33. package/dist/cli/upgrade.cjs +41 -20
  34. package/dist/cli/upgrade.cjs.map +1 -1
  35. package/dist/cli/upgrade.mjs +43 -22
  36. package/dist/cli/upgrade.mjs.map +1 -1
  37. package/dist/cli.cjs +187 -12
  38. package/dist/cli.cjs.map +1 -1
  39. package/dist/cli.mjs +187 -12
  40. package/dist/cli.mjs.map +1 -1
  41. package/dist/components/dev-view.cjs.map +1 -1
  42. package/dist/components/dev-view.mjs.map +1 -1
  43. package/dist/components/streaming-view.cjs.map +1 -1
  44. package/dist/components/streaming-view.mjs.map +1 -1
  45. package/dist/config.cjs.map +1 -1
  46. package/dist/config.mjs.map +1 -1
  47. package/dist/contract.cjs +174 -173
  48. package/dist/contract.cjs.map +1 -1
  49. package/dist/contract.d.cts +48 -44
  50. package/dist/contract.d.cts.map +1 -1
  51. package/dist/contract.d.mts +48 -44
  52. package/dist/contract.d.mts.map +1 -1
  53. package/dist/contract.mjs +3 -1
  54. package/dist/contract.mjs.map +1 -1
  55. package/dist/dev-logs.cjs.map +1 -1
  56. package/dist/dev-logs.mjs.map +1 -1
  57. package/dist/dev-session.cjs +0 -1
  58. package/dist/dev-session.mjs +1 -1
  59. package/dist/fastkv.cjs.map +1 -1
  60. package/dist/fastkv.mjs.map +1 -1
  61. package/dist/index.cjs +2 -4
  62. package/dist/index.d.cts +3 -3
  63. package/dist/index.d.mts +3 -3
  64. package/dist/index.mjs +1 -2
  65. package/dist/integrity.cjs.map +1 -1
  66. package/dist/integrity.mjs.map +1 -1
  67. package/dist/internal/manifest-normalizer.cjs.map +1 -1
  68. package/dist/internal/manifest-normalizer.mjs.map +1 -1
  69. package/dist/merge.cjs.map +1 -1
  70. package/dist/merge.mjs.map +1 -1
  71. package/dist/near-cli.cjs +1 -1
  72. package/dist/near-cli.cjs.map +1 -1
  73. package/dist/near-cli.mjs +1 -1
  74. package/dist/near-cli.mjs.map +1 -1
  75. package/dist/orchestrator.cjs +1 -1
  76. package/dist/orchestrator.cjs.map +1 -1
  77. package/dist/orchestrator.mjs +1 -1
  78. package/dist/orchestrator.mjs.map +1 -1
  79. package/dist/plugin.cjs +182 -171
  80. package/dist/plugin.cjs.map +1 -1
  81. package/dist/plugin.d.cts +72 -39
  82. package/dist/plugin.d.cts.map +1 -1
  83. package/dist/plugin.d.mts +71 -39
  84. package/dist/plugin.d.mts.map +1 -1
  85. package/dist/plugin.mjs +170 -160
  86. package/dist/plugin.mjs.map +1 -1
  87. package/dist/sdk.cjs +2 -2
  88. package/dist/sdk.d.cts +1 -1
  89. package/dist/sdk.d.mts +1 -1
  90. package/dist/sdk.mjs +1 -1
  91. package/dist/service-descriptor.d.cts +34 -0
  92. package/dist/service-descriptor.d.cts.map +1 -0
  93. package/dist/service-descriptor.d.mts +36 -0
  94. package/dist/service-descriptor.d.mts.map +1 -0
  95. package/dist/shared.cjs.map +1 -1
  96. package/dist/shared.mjs.map +1 -1
  97. package/dist/types.cjs +184 -184
  98. package/dist/types.cjs.map +1 -1
  99. package/dist/types.d.cts +5 -5
  100. package/dist/types.d.mts +5 -5
  101. package/dist/types.mjs +1 -1
  102. package/dist/types.mjs.map +1 -1
  103. package/dist/ui/types.d.cts +1 -0
  104. package/dist/ui/types.d.cts.map +1 -1
  105. package/dist/ui/types.d.mts +1 -0
  106. package/dist/ui/types.d.mts.map +1 -1
  107. package/dist/utils/banner.cjs.map +1 -1
  108. package/dist/utils/banner.mjs.map +1 -1
  109. package/dist/utils/run.cjs.map +1 -1
  110. package/dist/utils/run.mjs.map +1 -1
  111. package/package.json +3 -3
  112. package/skills/init-upgrade/SKILL.md +22 -16
  113. package/skills/publish-sync/SKILL.md +7 -18
  114. package/dist/utils/path-match.cjs +0 -18
  115. package/dist/utils/path-match.cjs.map +0 -1
  116. package/dist/utils/path-match.mjs +0 -17
  117. package/dist/utils/path-match.mjs.map +0 -1
  118. package/src/api-contract.ts +0 -623
  119. package/src/app.ts +0 -193
  120. package/src/cli/catalog.ts +0 -49
  121. package/src/cli/framework-version.ts +0 -61
  122. package/src/cli/help.ts +0 -13
  123. package/src/cli/infra.ts +0 -190
  124. package/src/cli/init.ts +0 -1202
  125. package/src/cli/parse.ts +0 -147
  126. package/src/cli/prompts.ts +0 -135
  127. package/src/cli/snapshot.ts +0 -46
  128. package/src/cli/status.ts +0 -99
  129. package/src/cli/sync.ts +0 -518
  130. package/src/cli/timing.ts +0 -63
  131. package/src/cli/upgrade.ts +0 -828
  132. package/src/cli.ts +0 -516
  133. package/src/components/dev-view.tsx +0 -352
  134. package/src/components/streaming-view.ts +0 -177
  135. package/src/config.ts +0 -893
  136. package/src/contract.meta.ts +0 -140
  137. package/src/contract.ts +0 -325
  138. package/src/dev-logs.ts +0 -92
  139. package/src/dev-session.ts +0 -283
  140. package/src/fastkv.ts +0 -181
  141. package/src/index.ts +0 -8
  142. package/src/integrity.ts +0 -138
  143. package/src/internal/manifest-normalizer.ts +0 -290
  144. package/src/merge.ts +0 -187
  145. package/src/mf.ts +0 -147
  146. package/src/near-cli.ts +0 -259
  147. package/src/network.ts +0 -3
  148. package/src/orchestrator.ts +0 -493
  149. package/src/plugin.ts +0 -1821
  150. package/src/sdk.ts +0 -14
  151. package/src/service-descriptor.ts +0 -281
  152. package/src/shared.ts +0 -249
  153. package/src/sidebar.ts +0 -140
  154. package/src/types.ts +0 -330
  155. package/src/ui/head.ts +0 -83
  156. package/src/ui/index.ts +0 -5
  157. package/src/ui/metadata.ts +0 -95
  158. package/src/ui/router.ts +0 -88
  159. package/src/ui/runtime.ts +0 -42
  160. package/src/ui/types.ts +0 -64
  161. package/src/utils/banner.ts +0 -21
  162. package/src/utils/linkify.ts +0 -11
  163. package/src/utils/path-match.ts +0 -16
  164. package/src/utils/run.ts +0 -31
  165. package/src/utils/save-config.ts +0 -20
  166. package/src/utils/theme.ts +0 -39
@@ -1,623 +0,0 @@
1
- import { createHash } from "node:crypto";
2
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
3
- import { dirname, join, relative } from "node:path";
4
- import type { RuntimeConfig, RuntimePluginConfig } from "./types";
5
-
6
- const REMOTE_FETCH_TIMEOUT_MS = 10_000;
7
-
8
- export interface ApiPluginManifest {
9
- schemaVersion: 1;
10
- kind: "every-plugin/manifest";
11
- plugin: {
12
- name: string;
13
- version: string;
14
- };
15
- runtime: {
16
- remoteEntry: string;
17
- };
18
- contract?: {
19
- kind: "orpc";
20
- types: {
21
- path: string;
22
- exportName: string;
23
- typeName: string;
24
- sha256?: string;
25
- };
26
- };
27
- additionalExports?: Array<{
28
- path: string;
29
- exports: string[];
30
- sha256?: string;
31
- }>;
32
- }
33
-
34
- interface ContractSource {
35
- key: string;
36
- importName: string;
37
- sourceFilePath: string;
38
- generatedPath?: string;
39
- }
40
-
41
- function sha256(input: string): string {
42
- return createHash("sha256").update(input).digest("hex");
43
- }
44
-
45
- function trimTrailingSlash(input: string): string {
46
- return input.replace(/\/$/, "");
47
- }
48
-
49
- function sanitizeIdentifier(input: string): string {
50
- return input.replace(/[^A-Za-z0-9_]/g, "_").replace(/^[^A-Za-z_]+/, "_");
51
- }
52
-
53
- function toImportPath(fromFile: string, targetFile: string): string {
54
- const rel = relative(dirname(fromFile), targetFile).replace(/\\/g, "/");
55
- return rel.startsWith(".") ? rel : `./${rel}`;
56
- }
57
-
58
- function writeFileIfChanged(filePath: string, content: string) {
59
- try {
60
- if (readFileSync(filePath, "utf8") === content) return false;
61
- } catch {
62
- // file does not exist yet
63
- }
64
-
65
- writeFileSync(filePath, content);
66
- return true;
67
- }
68
-
69
- function getApiPluginManifestUrl(apiBaseUrl: string): string {
70
- return `${trimTrailingSlash(apiBaseUrl)}/plugin.manifest.json`;
71
- }
72
-
73
- async function fetchWithTimeout(url: string): Promise<Response> {
74
- const controller = new AbortController();
75
- const timeout = setTimeout(() => controller.abort(), REMOTE_FETCH_TIMEOUT_MS);
76
-
77
- try {
78
- return await fetch(url, { signal: controller.signal });
79
- } catch (error) {
80
- if (error instanceof Error && error.name === "AbortError") {
81
- throw new Error(`Timed out fetching ${url} after ${REMOTE_FETCH_TIMEOUT_MS}ms`);
82
- }
83
- throw error;
84
- } finally {
85
- clearTimeout(timeout);
86
- }
87
- }
88
-
89
- async function fetchApiPluginManifest(apiBaseUrl: string): Promise<ApiPluginManifest> {
90
- const response = await fetchWithTimeout(getApiPluginManifestUrl(apiBaseUrl));
91
- if (!response.ok) {
92
- throw new Error(
93
- `Failed to fetch API plugin manifest: ${response.status} ${response.statusText}`,
94
- );
95
- }
96
-
97
- const manifest = (await response.json()) as ApiPluginManifest;
98
- if (manifest.schemaVersion !== 1 || manifest.kind !== "every-plugin/manifest") {
99
- throw new Error("Unsupported API plugin manifest format");
100
- }
101
-
102
- return manifest;
103
- }
104
-
105
- function localApiContractSource(configDir: string): ContractSource {
106
- const sourcePath = join(configDir, "api", "src", "contract.ts");
107
- return {
108
- key: "api",
109
- importName: "BaseApiContract",
110
- sourceFilePath: sourcePath,
111
- };
112
- }
113
-
114
- function localAuthContractSource(configDir: string): ContractSource {
115
- const sourcePath = join(configDir, "plugins", "auth", "src", "contract.ts");
116
- return {
117
- key: "auth",
118
- importName: "authContract",
119
- sourceFilePath: sourcePath,
120
- };
121
- }
122
-
123
- async function remoteContractSource(opts: {
124
- configDir: string;
125
- runtimeDir: string;
126
- name: string;
127
- baseUrl: string;
128
- generatedSubdir: string;
129
- }): Promise<ContractSource> {
130
- const manifest = await fetchApiPluginManifest(opts.baseUrl);
131
- if (!manifest.contract) {
132
- throw new Error(
133
- `Plugin manifest for ${manifest.plugin.name} does not advertise contract types`,
134
- );
135
- }
136
-
137
- const contractUrl = `${trimTrailingSlash(opts.baseUrl)}/${manifest.contract.types.path.replace(/^\.\//, "")}`;
138
- const contractResponse = await fetchWithTimeout(contractUrl);
139
- if (!contractResponse.ok) {
140
- throw new Error(
141
- `Failed to fetch contract types: ${contractResponse.status} ${contractResponse.statusText}`,
142
- );
143
- }
144
-
145
- const contractTypes = await contractResponse.text();
146
- if (manifest.contract.types.sha256 && manifest.contract.types.sha256 !== sha256(contractTypes)) {
147
- throw new Error("Fetched contract types failed checksum verification");
148
- }
149
-
150
- const generatedPath = join(opts.runtimeDir, opts.generatedSubdir, "contract.d.ts");
151
- mkdirSync(dirname(generatedPath), { recursive: true });
152
- writeFileIfChanged(generatedPath, contractTypes);
153
-
154
- return {
155
- key: opts.name,
156
- importName: `${sanitizeIdentifier(opts.name)}Contract`,
157
- sourceFilePath: generatedPath,
158
- generatedPath,
159
- };
160
- }
161
-
162
- async function fetchAuthExportTypes(opts: {
163
- baseUrl: string;
164
- runtimeDir: string;
165
- manifest: ApiPluginManifest;
166
- }): Promise<string | null> {
167
- if (!opts.manifest.additionalExports || opts.manifest.additionalExports.length === 0) {
168
- return null;
169
- }
170
-
171
- const authExportEntry = opts.manifest.additionalExports.find(
172
- (entry) => entry.path.includes("auth-export") || entry.path.endsWith("auth-export.d.ts"),
173
- );
174
-
175
- if (!authExportEntry) {
176
- return null;
177
- }
178
-
179
- const exportUrl = `${trimTrailingSlash(opts.baseUrl)}/${authExportEntry.path.replace(/^\.\//, "")}`;
180
- const response = await fetchWithTimeout(exportUrl);
181
- if (!response.ok) {
182
- console.warn(`[API Contract] Failed to fetch auth export types: ${response.status}`);
183
- return null;
184
- }
185
-
186
- const content = await response.text();
187
- if (authExportEntry.sha256 && authExportEntry.sha256 !== sha256(content)) {
188
- console.warn("[API Contract] Auth export types checksum mismatch");
189
- return null;
190
- }
191
-
192
- const generatedPath = join(opts.runtimeDir, "auth", "auth-export.d.ts");
193
- mkdirSync(dirname(generatedPath), { recursive: true });
194
- writeFileIfChanged(generatedPath, content);
195
-
196
- return generatedPath;
197
- }
198
-
199
- function writeAuthTypesGen(targetPath: string, authExportPath: string) {
200
- const exportImportPath = toImportPath(targetPath, authExportPath);
201
- const content = [
202
- `export type {`,
203
- ` Auth,`,
204
- ` AuthOrganizationContext,`,
205
- ` AuthOrganization,`,
206
- ` AuthOrganizationSummary,`,
207
- ` AuthOrganizationMember,`,
208
- ` AuthApiKey,`,
209
- ` AuthInvitation,`,
210
- ` GetActiveMemberInput,`,
211
- ` GetOrganizationInput,`,
212
- ` ListMembersInput,`,
213
- ` ListInvitationsInput,`,
214
- ` ListApiKeysInput,`,
215
- ` AuthServices,`,
216
- ` createAuthInstance,`,
217
- `} from "${exportImportPath}";`,
218
- `import type { InferOutput, ContractType as AuthContract } from "${toImportPath(targetPath, join(dirname(authExportPath), "contract.d.ts"))}";`,
219
- `import type { Auth as BaseAuth } from "${exportImportPath}";`,
220
- "",
221
- 'type RawAuthSession = InferOutput<"getSession">;',
222
- 'type RawAuthRequestContext = InferOutput<"getContext">;',
223
- 'type RawAuthActiveMember = InferOutput<"getActiveMember">;',
224
- "",
225
- 'export type AuthSessionUser = NonNullable<RawAuthSession["user"]> & {',
226
- " role?: string | null;",
227
- " isAnonymous?: boolean | null;",
228
- " walletAddress?: string | null;",
229
- " banned?: boolean | null;",
230
- "};",
231
- 'export type AuthSessionData = NonNullable<RawAuthSession["session"]> & {',
232
- " activeOrganizationId?: string | null;",
233
- "};",
234
- "export type AuthSession = {",
235
- " user: AuthSessionUser | null;",
236
- " session: AuthSessionData | null;",
237
- "};",
238
- "export type AuthRequestContext = RawAuthRequestContext;",
239
- "export type AuthActiveMember = RawAuthActiveMember;",
240
- 'export type AuthBaseSession = BaseAuth["$Infer"]["Session"];',
241
- "export type AuthContractType = AuthContract;",
242
- "",
243
- ].join("\n");
244
- mkdirSync(dirname(targetPath), { recursive: true });
245
- writeFileIfChanged(targetPath, content);
246
- }
247
-
248
- function writeFallbackAuthTypesGen(targetPath: string) {
249
- const content = [
250
- 'import type { Auth } from "better-auth";',
251
- 'export type { Auth } from "better-auth";',
252
- 'export type AuthSession = Auth["$Infer"]["Session"];',
253
- "export type AuthSessionData = AuthSession;",
254
- 'export type AuthSessionUser = NonNullable<AuthSession["user"]>;',
255
- "export interface AuthOrganizationContext {",
256
- " activeOrganizationId: string | null;",
257
- " organization: { id: string; name: string; slug: string; logo?: string | null; metadata?: Record<string, unknown> } | null;",
258
- " member: { id: string; role: string } | null;",
259
- " isPersonal: boolean;",
260
- " hasOrganization: boolean;",
261
- "}",
262
- "export interface AuthRequestContext {",
263
- " user: AuthSessionUser | null;",
264
- " userId: string | null;",
265
- " isAuthenticated: boolean;",
266
- ' authMethod: "session" | "apiKey" | "anonymous" | "none";',
267
- " near: {",
268
- " primaryAccountId: string | null;",
269
- " linkedAccounts: Array<{ accountId: string; network: string; publicKey: string; isPrimary: boolean }>;",
270
- " hasNearAccount: boolean;",
271
- " };",
272
- " organization: AuthOrganizationContext;",
273
- " organizations?: Array<{ id: string; role: string; name?: string; slug?: string }>;",
274
- "}",
275
- "export type AuthActiveMember = { id: string | null; role: string | null; organizationId: string | null };",
276
- "export type AuthOrganization = {",
277
- " id: string;",
278
- " name: string;",
279
- " slug: string;",
280
- " logo?: string | null;",
281
- " metadata?: Record<string, unknown> | null;",
282
- " createdAt: Date;",
283
- "};",
284
- 'export type AuthOrganizationSummary = NonNullable<AuthOrganizationContext["organization"]>;',
285
- 'export type AuthOrganizationMember = NonNullable<AuthOrganizationContext["member"]>;',
286
- "export type AuthApiKey = {",
287
- " id: string;",
288
- " name: string | null;",
289
- " prefix: string | null;",
290
- " start: string | null;",
291
- " expiresAt: Date | null;",
292
- " createdAt: Date;",
293
- " updatedAt: Date;",
294
- " metadata: unknown | null;",
295
- " permissions: Record<string, string[]> | null;",
296
- "};",
297
- "export type AuthInvitation = {",
298
- " id: string;",
299
- " organizationId: string;",
300
- " email: string;",
301
- " role: string | null;",
302
- " status: string;",
303
- " expiresAt: Date;",
304
- " inviterId: string;",
305
- "};",
306
- "export type GetActiveMemberInput = { organizationId?: string };",
307
- "export type GetOrganizationInput = { id: string };",
308
- "export type ListMembersInput = { organizationId: string };",
309
- "export type ListInvitationsInput = { organizationId: string };",
310
- "export type ListApiKeysInput = { organizationId?: string };",
311
- "export type createAuthInstance = never;",
312
- "export interface AuthServices {",
313
- " auth: Auth;",
314
- " db: unknown;",
315
- " driver: { close(): Promise<void> };",
316
- " handler: (req: Request) => Promise<Response>;",
317
- "}",
318
- "",
319
- ].join("\n");
320
- mkdirSync(dirname(targetPath), { recursive: true });
321
- writeFileIfChanged(targetPath, content);
322
- }
323
-
324
- async function resolveContractSource(opts: {
325
- configDir: string;
326
- runtimeDir: string;
327
- key: string;
328
- source: RuntimePluginConfig | { url: string; localPath?: string; name: string } | null;
329
- baseUrl: string;
330
- generatedSubdir: string;
331
- localSourceFactory?: (configDir: string) => ContractSource;
332
- }): Promise<ContractSource> {
333
- if (opts.key === "api") {
334
- const localPath = opts.source && "localPath" in opts.source ? opts.source.localPath : undefined;
335
- if (localPath != null && localPath !== "") {
336
- return {
337
- key: opts.key,
338
- importName: "BaseApiContract",
339
- sourceFilePath: join(localPath, "src", "contract.ts"),
340
- };
341
- }
342
-
343
- if (!opts.baseUrl) {
344
- return localApiContractSource(opts.configDir);
345
- }
346
- }
347
-
348
- if (opts.key === "auth" && opts.localSourceFactory) {
349
- const localPath = opts.source && "localPath" in opts.source ? opts.source.localPath : undefined;
350
- if (localPath != null && localPath !== "") {
351
- return {
352
- key: opts.key,
353
- importName: "authContract",
354
- sourceFilePath: join(localPath, "src", "contract.ts"),
355
- };
356
- }
357
-
358
- if (!opts.baseUrl) {
359
- return opts.localSourceFactory(opts.configDir);
360
- }
361
- }
362
-
363
- if (
364
- opts.source &&
365
- "localPath" in opts.source &&
366
- opts.source.localPath != null &&
367
- opts.source.localPath !== ""
368
- ) {
369
- return {
370
- key: opts.key,
371
- importName: `${sanitizeIdentifier(opts.key)}Contract`,
372
- sourceFilePath: join(opts.source.localPath, "src", "contract.ts"),
373
- };
374
- }
375
-
376
- return remoteContractSource({
377
- configDir: opts.configDir,
378
- runtimeDir: opts.runtimeDir,
379
- name: opts.key,
380
- baseUrl: opts.baseUrl,
381
- generatedSubdir: opts.generatedSubdir,
382
- });
383
- }
384
-
385
- function writeGeneratedFiles(opts: {
386
- configDir: string;
387
- sources: ContractSource[];
388
- pluginKeys: string[];
389
- authSource: ContractSource | null;
390
- authExportPath?: string | null;
391
- }) {
392
- const baseSource = opts.sources.find((source) => source.key === "api");
393
- const pluginSources = opts.pluginKeys
394
- .map((key) => opts.sources.find((entry) => entry.key === key))
395
- .filter((source): source is ContractSource => Boolean(source));
396
-
397
- if (!baseSource) {
398
- throw new Error("API contract source is required to generate the aggregate contract");
399
- }
400
-
401
- // --- Generate ui/src/lib/api-types.gen.ts ---
402
- const uiContractPath = join(opts.configDir, "ui", "src", "lib", "api-types.gen.ts");
403
- const uiLines: string[] = [];
404
-
405
- for (const source of opts.sources) {
406
- const importPath = toImportPath(uiContractPath, source.sourceFilePath);
407
- uiLines.push(`import type { ContractType as ${source.importName} } from "${importPath}";`);
408
- }
409
-
410
- uiLines.push("");
411
-
412
- const compositeParts: string[] = [];
413
- if (opts.authSource) {
414
- compositeParts.push(`auth: ${opts.authSource.importName}`);
415
- }
416
- for (const source of pluginSources) {
417
- const key = /^[$A-Z_][0-9A-Z_$]*$/i.test(source.key) ? source.key : JSON.stringify(source.key);
418
- compositeParts.push(`${key}: ${source.importName}`);
419
- }
420
-
421
- if (compositeParts.length === 0) {
422
- uiLines.push(`export type ApiContract = ${baseSource.importName};`);
423
- } else {
424
- uiLines.push(`export type ApiContract = ${baseSource.importName} & {`);
425
- for (const part of compositeParts) {
426
- uiLines.push(` ${part};`);
427
- }
428
- uiLines.push("};");
429
- }
430
- mkdirSync(dirname(uiContractPath), { recursive: true });
431
- writeFileIfChanged(uiContractPath, `${uiLines.join("\n")}\n`);
432
-
433
- // --- Generate api/src/lib/plugins-types.gen.ts ---
434
- // Includes both plugin contracts AND auth as a unified PluginsClient type
435
- const pluginsClientPath = join(opts.configDir, "api", "src", "lib", "plugins-types.gen.ts");
436
- const pluginsClientLines: string[] = [];
437
-
438
- for (const source of pluginSources) {
439
- const importPath = toImportPath(pluginsClientPath, source.sourceFilePath);
440
- pluginsClientLines.push(
441
- `import type { ContractType as ${source.importName} } from "${importPath}";`,
442
- );
443
- }
444
-
445
- if (opts.authSource) {
446
- const authImportPath = toImportPath(pluginsClientPath, opts.authSource.sourceFilePath);
447
- pluginsClientLines.push(
448
- `import type { ContractType as ${opts.authSource.importName} } from "${authImportPath}";`,
449
- );
450
- }
451
-
452
- pluginsClientLines.push(
453
- 'import type { ContractRouterClient, AnyContractRouter } from "@orpc/contract";',
454
- );
455
- pluginsClientLines.push(
456
- "type ClientFactory<C extends AnyContractRouter> = (context?: Record<string, unknown>) => ContractRouterClient<C>;",
457
- );
458
- pluginsClientLines.push("");
459
-
460
- const allPluginSources = [...pluginSources];
461
- if (opts.authSource) {
462
- allPluginSources.push({ ...opts.authSource, key: "auth" });
463
- }
464
-
465
- if (allPluginSources.length === 0) {
466
- pluginsClientLines.push("export type PluginsClient = Record<string, never>;");
467
- } else {
468
- pluginsClientLines.push("export type PluginsClient = {");
469
- for (const source of allPluginSources) {
470
- const key = /^[$A-Z_][0-9A-Z_$]*$/i.test(source.key)
471
- ? source.key
472
- : JSON.stringify(source.key);
473
- pluginsClientLines.push(` ${key}: ClientFactory<${source.importName}>;`);
474
- }
475
- pluginsClientLines.push("};");
476
- }
477
-
478
- mkdirSync(dirname(pluginsClientPath), { recursive: true });
479
- writeFileIfChanged(pluginsClientPath, `${pluginsClientLines.join("\n")}\n`);
480
-
481
- // --- Generate */src/lib/auth-types.gen.ts ---
482
- const authTypeTargets = [join(opts.configDir, "ui", "src", "lib", "auth-types.gen.ts")];
483
- const apiLibDir = join(opts.configDir, "api", "src", "lib");
484
- if (existsSync(apiLibDir)) {
485
- authTypeTargets.push(join(apiLibDir, "auth-types.gen.ts"));
486
- }
487
- const hostLibDir = join(opts.configDir, "host", "src", "lib");
488
- if (existsSync(join(opts.configDir, "host", "src"))) {
489
- authTypeTargets.push(join(hostLibDir, "auth-types.gen.ts"));
490
- }
491
-
492
- if (opts.authExportPath) {
493
- for (const authTypesPath of authTypeTargets) {
494
- writeAuthTypesGen(authTypesPath, opts.authExportPath);
495
- }
496
- } else if (opts.authSource) {
497
- for (const authTypesPath of authTypeTargets) {
498
- writeFallbackAuthTypesGen(authTypesPath);
499
- }
500
- }
501
-
502
- return uiContractPath;
503
- }
504
-
505
- export async function syncApiContractBridge(opts: {
506
- configDir: string;
507
- runtimeConfig: RuntimeConfig;
508
- apiBaseUrl: string;
509
- }): Promise<{
510
- bridgePath: string;
511
- generatedPath: string | null;
512
- manifest: ApiPluginManifest | null;
513
- source: "local" | "remote";
514
- }> {
515
- const runtimeDir = join(opts.configDir, ".bos", "generated");
516
- const pluginEntries = Object.entries(opts.runtimeConfig.plugins ?? {}).sort(([a], [b]) =>
517
- a.localeCompare(b),
518
- );
519
- const sources: ContractSource[] = [];
520
- let manifest: ApiPluginManifest | null = null;
521
- let generatedPath: string | null = null;
522
- let authSource: ContractSource | null = null;
523
- let authExportPath: string | null = null;
524
-
525
- const baseSource = await resolveContractSource({
526
- configDir: opts.configDir,
527
- runtimeDir,
528
- key: "api",
529
- source: opts.runtimeConfig.api,
530
- baseUrl: opts.apiBaseUrl,
531
- generatedSubdir: "api",
532
- });
533
- sources.push(baseSource);
534
-
535
- if (opts.runtimeConfig.auth) {
536
- authSource = await resolveContractSource({
537
- configDir: opts.configDir,
538
- runtimeDir,
539
- key: "auth",
540
- source: opts.runtimeConfig.auth,
541
- baseUrl: opts.runtimeConfig.auth.url,
542
- generatedSubdir: "auth",
543
- localSourceFactory: localAuthContractSource,
544
- });
545
- sources.push(authSource);
546
- if (authSource.generatedPath) {
547
- generatedPath = authSource.generatedPath;
548
- }
549
-
550
- // Fetch auth additional exports (auth-export.d.ts) for remote auth
551
- if (opts.runtimeConfig.auth.url && opts.runtimeConfig.auth.source !== "local") {
552
- try {
553
- const authManifest = await fetchApiPluginManifest(opts.runtimeConfig.auth.url);
554
- const fetchedAuthExportPath = await fetchAuthExportTypes({
555
- baseUrl: opts.runtimeConfig.auth.url,
556
- runtimeDir,
557
- manifest: authManifest,
558
- });
559
- if (fetchedAuthExportPath) {
560
- authExportPath = fetchedAuthExportPath;
561
- }
562
- } catch (error) {
563
- console.warn(
564
- `[API Contract] Failed to fetch auth additional exports: ${error instanceof Error ? error.message : String(error)}`,
565
- );
566
- }
567
- }
568
-
569
- if (!authExportPath) {
570
- const localAuthExport = join(opts.configDir, "plugins", "auth", "src", "auth-export.ts");
571
- if (existsSync(localAuthExport)) {
572
- authExportPath = localAuthExport;
573
- } else {
574
- const generatedAuthExport = join(runtimeDir, "auth", "auth-export.d.ts");
575
- if (existsSync(generatedAuthExport)) {
576
- authExportPath = generatedAuthExport;
577
- }
578
- }
579
- }
580
- }
581
-
582
- for (const [key, plugin] of pluginEntries) {
583
- if (!plugin.url && !plugin.localPath) {
584
- console.warn(
585
- `[API Contract] Skipping plugin "${key}" — no URL resolved (local path missing and no production URL configured)`,
586
- );
587
- continue;
588
- }
589
- const source = await resolveContractSource({
590
- configDir: opts.configDir,
591
- runtimeDir,
592
- key,
593
- source: plugin,
594
- baseUrl: plugin.url,
595
- generatedSubdir: `plugins/${key}`,
596
- });
597
- sources.push(source);
598
- if (source.generatedPath) {
599
- generatedPath = source.generatedPath;
600
- }
601
- }
602
-
603
- const allPluginKeys = pluginEntries.map(([key]) => key);
604
-
605
- writeGeneratedFiles({
606
- configDir: opts.configDir,
607
- sources,
608
- pluginKeys: allPluginKeys,
609
- authSource,
610
- authExportPath,
611
- });
612
-
613
- if (opts.runtimeConfig.api.source !== "local") {
614
- manifest = await fetchApiPluginManifest(opts.apiBaseUrl);
615
- }
616
-
617
- return {
618
- bridgePath: join(opts.configDir, "ui", "src", "lib", "api-types.gen.ts"),
619
- generatedPath,
620
- manifest,
621
- source: opts.runtimeConfig.api.source,
622
- };
623
- }