@superblocksteam/sdk 2.0.130-next.1 → 2.0.130-next.3

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.
@@ -2,6 +2,8 @@ import path from "node:path";
2
2
 
3
3
  import type { Plugin } from "vite";
4
4
 
5
+ const SERVER_APIS_DIR = "server/apis";
6
+
5
7
  /**
6
8
  * Build a sourcemap `mappings` string for a simple prepend operation.
7
9
  *
@@ -24,27 +26,95 @@ function buildPrependMappings(
24
26
  return empty + original;
25
27
  }
26
28
 
29
+ function qualifyAnchoredServerApisRelative(relative: string): string | null {
30
+ if (relative === `${SERVER_APIS_DIR}/index.ts`) {
31
+ return null;
32
+ }
33
+ const baseName = path.posix.basename(relative);
34
+ if (baseName === "index.ts") {
35
+ return null;
36
+ }
37
+ if (!relative.endsWith(".ts") && !relative.endsWith(".tsx")) {
38
+ return null;
39
+ }
40
+ return relative;
41
+ }
42
+
43
+ function qualifySegmentFallbackRelative(relative: string): string | null {
44
+ if (!relative.startsWith(`${SERVER_APIS_DIR}/`)) {
45
+ return null;
46
+ }
47
+ // Reject false positives when appRootDir sits under a server/apis/ path.
48
+ if (relative.includes("/client/")) {
49
+ return null;
50
+ }
51
+ const baseName = path.posix.basename(relative);
52
+ if (baseName !== "api.ts" && baseName !== "api.tsx") {
53
+ return null;
54
+ }
55
+ return relative;
56
+ }
57
+
58
+ /**
59
+ * Resolve the app-root-relative entry point path for a Vite module id.
60
+ *
61
+ * Fullstack apps keep `server/apis/` at the app root while `vite.config.ts`
62
+ * often sets `root: "./client"`. Dev server and build already pass the true
63
+ * app root into the plugin; failures are intermittent and depend on how Vite
64
+ * resolves module ids for files outside the client subtree (for example
65
+ * `../server/apis/...` relatives or `/@fs/...` absolute ids). Plain absolute
66
+ * paths under `<appRoot>/server/apis/` already matched before this helper.
67
+ */
68
+ export function resolveSdkApiEntryPointRelativePath(
69
+ appRootDir: string,
70
+ moduleId: string,
71
+ ): string | null {
72
+ const normalizedId = path.normalize(moduleId);
73
+ const normalizedRoot = path.normalize(appRootDir);
74
+
75
+ const relative = path
76
+ .relative(normalizedRoot, normalizedId)
77
+ .replace(/\\/g, "/");
78
+ if (relative.startsWith(`${SERVER_APIS_DIR}/`)) {
79
+ return qualifyAnchoredServerApisRelative(relative);
80
+ }
81
+
82
+ // Plugin base is client/ or Vite passes a ../server/apis/... module id.
83
+ if (relative.startsWith(`../${SERVER_APIS_DIR}/`)) {
84
+ return qualifyAnchoredServerApisRelative(relative.slice("../".length));
85
+ }
86
+
87
+ // Fallback for module ids not matched above: /@fs/ prefixed ids, standard
88
+ // absolute paths when appRootDir is 2+ levels above server/apis/, etc.
89
+ // NOTE: unanchored to appRootDir — matches any id containing /server/apis/
90
+ // with an api.ts/api.tsx basename. lastIndexOf picks the deepest segment.
91
+ const posixId = normalizedId.replace(/\\/g, "/");
92
+ const segment = `/${SERVER_APIS_DIR}/`;
93
+ const segmentIndex = posixId.lastIndexOf(segment);
94
+ if (segmentIndex !== -1) {
95
+ return qualifySegmentFallbackRelative(posixId.slice(segmentIndex + 1));
96
+ }
97
+
98
+ return null;
99
+ }
100
+
27
101
  /**
28
102
  * Vite plugin that prepends `__setEntryPoint(relativePath)` to SDK API files
29
103
  * so that `api()` can stamp the authoritative source file
30
104
  * path onto the compiled API object.
31
105
  *
106
+ * @param appRootDir - App root (parent of `client/` and `server/`).
107
+ *
32
108
  * Runs in both dev and build. The user's source code is never modified on disk.
33
109
  */
34
- export function sdkApiEntryPointPlugin(root: string): Plugin {
110
+ export function sdkApiEntryPointPlugin(appRootDir: string): Plugin {
35
111
  return {
36
112
  name: "sb-sdk-api-entry-point",
37
113
  enforce: "pre",
38
114
 
39
115
  transform(code, id) {
40
- const relative = path.relative(root, id).replace(/\\/g, "/");
41
- if (
42
- !relative.startsWith("server/apis/") ||
43
- relative === "server/apis/index.ts"
44
- ) {
45
- return null;
46
- }
47
- if (!relative.endsWith(".ts") && !relative.endsWith(".tsx")) {
116
+ const relative = resolveSdkApiEntryPointRelativePath(appRootDir, id);
117
+ if (!relative) {
48
118
  return null;
49
119
  }
50
120
 
@@ -2,7 +2,10 @@ import path from "node:path";
2
2
 
3
3
  import { describe, it, expect } from "vitest";
4
4
 
5
- import { sdkApiEntryPointPlugin } from "./vite-plugin-sdk-api-entry-point.mjs";
5
+ import {
6
+ resolveSdkApiEntryPointRelativePath,
7
+ sdkApiEntryPointPlugin,
8
+ } from "./vite-plugin-sdk-api-entry-point.mjs";
6
9
 
7
10
  type TransformResult = {
8
11
  code: string;
@@ -88,6 +91,95 @@ function expectValidSourcemap(
88
91
  });
89
92
  }
90
93
 
94
+ describe("resolveSdkApiEntryPointRelativePath", () => {
95
+ it("resolves server API paths from the true app root", () => {
96
+ expect(
97
+ resolveSdkApiEntryPointRelativePath(
98
+ "/app",
99
+ "/app/server/apis/GetUsers/api.ts",
100
+ ),
101
+ ).toBe("server/apis/GetUsers/api.ts");
102
+ });
103
+
104
+ it("resolves server API paths when the base dir is vite client/", () => {
105
+ expect(
106
+ resolveSdkApiEntryPointRelativePath(
107
+ "/app/client",
108
+ "/app/server/apis/GetPastAnalyses/api.ts",
109
+ ),
110
+ ).toBe("server/apis/GetPastAnalyses/api.ts");
111
+ });
112
+
113
+ it("resolves server API paths from absolute /@fs module ids", () => {
114
+ expect(
115
+ resolveSdkApiEntryPointRelativePath(
116
+ "/app",
117
+ "/@fs/Users/me/app/server/apis/GetFeedback/api.ts",
118
+ ),
119
+ ).toBe("server/apis/GetFeedback/api.ts");
120
+ });
121
+
122
+ it("returns null for the registry barrel file", () => {
123
+ expect(
124
+ resolveSdkApiEntryPointRelativePath("/app", "/app/server/apis/index.ts"),
125
+ ).toBeNull();
126
+ });
127
+
128
+ it("returns null when app root contains server/apis and module is a client file", () => {
129
+ expect(
130
+ resolveSdkApiEntryPointRelativePath(
131
+ "/home/server/apis/myapp",
132
+ "/home/server/apis/myapp/client/components/Button.tsx",
133
+ ),
134
+ ).toBeNull();
135
+ });
136
+
137
+ it("uses the deepest server/apis segment in /@fs fallback", () => {
138
+ expect(
139
+ resolveSdkApiEntryPointRelativePath(
140
+ "/app",
141
+ "/@fs/home/server/apis/decoy/server/apis/GetReal/api.ts",
142
+ ),
143
+ ).toBe("server/apis/GetReal/api.ts");
144
+ });
145
+
146
+ it("resolves server API paths for api.tsx entry points", () => {
147
+ expect(
148
+ resolveSdkApiEntryPointRelativePath(
149
+ "/app",
150
+ "/app/server/apis/Render/api.tsx",
151
+ ),
152
+ ).toBe("server/apis/Render/api.tsx");
153
+ });
154
+
155
+ it("resolves Clark-style API entry points that are not named api.ts", () => {
156
+ expect(
157
+ resolveSdkApiEntryPointRelativePath(
158
+ "/app",
159
+ "/app/server/apis/issues/list-issues.ts",
160
+ ),
161
+ ).toBe("server/apis/issues/list-issues.ts");
162
+ });
163
+
164
+ it("resolves helper modules under server/apis/ on anchored paths", () => {
165
+ expect(
166
+ resolveSdkApiEntryPointRelativePath(
167
+ "/app",
168
+ "/app/server/apis/GetFoo/helper.ts",
169
+ ),
170
+ ).toBe("server/apis/GetFoo/helper.ts");
171
+ });
172
+
173
+ it("returns null for index.ts under an API directory", () => {
174
+ expect(
175
+ resolveSdkApiEntryPointRelativePath(
176
+ "/app",
177
+ "/app/server/apis/GetFoo/index.ts",
178
+ ),
179
+ ).toBeNull();
180
+ });
181
+ });
182
+
91
183
  describe("sdkApiEntryPointPlugin", () => {
92
184
  const root = "/app";
93
185
 
@@ -98,6 +190,19 @@ describe("sdkApiEntryPointPlugin", () => {
98
190
  });
99
191
 
100
192
  describe("transform", () => {
193
+ it("prepends __setEntryPoint for Clark-style list-issues.ts entry points", () => {
194
+ const { transform } = createPlugin(root);
195
+ const result = transform(
196
+ 'export default api({ name: "ListIssues" });',
197
+ path.join(root, "server/apis/issues/list-issues.ts"),
198
+ );
199
+
200
+ expect(result).not.toBeNull();
201
+ expect(result!.code).toContain(
202
+ '__sb_set_ep("server/apis/issues/list-issues.ts");',
203
+ );
204
+ });
205
+
101
206
  it("prepends __setEntryPoint for a standard API file", () => {
102
207
  const { transform } = createPlugin(root);
103
208
  const result = transform(
@@ -171,6 +276,34 @@ describe("sdkApiEntryPointPlugin", () => {
171
276
  );
172
277
  });
173
278
 
279
+ it("prepends __setEntryPoint when plugin root is vite client/ subdir (fullstack)", () => {
280
+ const appRoot = "/app";
281
+ const viteRoot = path.join(appRoot, "client");
282
+ const { transform } = createPlugin(viteRoot);
283
+ const result = transform(
284
+ 'export default api({ name: "GetPastAnalyses" });',
285
+ path.join(appRoot, "server/apis/GetPastAnalyses/api.ts"),
286
+ );
287
+
288
+ expect(result).not.toBeNull();
289
+ expect(result!.code).toContain(
290
+ '__sb_set_ep("server/apis/GetPastAnalyses/api.ts");',
291
+ );
292
+ });
293
+
294
+ it("prepends __setEntryPoint for /@fs/ module ids", () => {
295
+ const { transform } = createPlugin(root);
296
+ const result = transform(
297
+ 'export default api({ name: "GetFeedback" });',
298
+ "/@fs/Users/me/app/server/apis/GetFeedback/api.ts",
299
+ );
300
+
301
+ expect(result).not.toBeNull();
302
+ expect(result!.code).toContain(
303
+ '__sb_set_ep("server/apis/GetFeedback/api.ts");',
304
+ );
305
+ });
306
+
174
307
  it("preserves original code after the prepend", () => {
175
308
  const { transform } = createPlugin(root);
176
309
  const originalCode = [