@softr/sdk 0.0.12 → 0.0.14

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 (47) hide show
  1. package/dist/assertions.js +68 -0
  2. package/dist/auth/config.js +27 -0
  3. package/dist/auth/context.d.ts +18 -0
  4. package/dist/auth/isomorphic.d.ts +73 -0
  5. package/dist/auth/isomorphic.js +113 -0
  6. package/dist/auth/methods/password.d.ts +22 -0
  7. package/dist/auth/methods/password.js +51 -0
  8. package/dist/auth/methods/signup.d.ts +37 -0
  9. package/dist/auth/methods/signup.js +89 -0
  10. package/dist/auth/methods/utils.d.ts +15 -0
  11. package/dist/auth/methods/utils.js +13 -0
  12. package/dist/auth/permissions.d.ts +17 -0
  13. package/dist/auth/permissions.js +13 -0
  14. package/dist/auth/platform.js +138 -0
  15. package/dist/auth/server.d.ts +17 -0
  16. package/dist/auth/server.js +17 -0
  17. package/dist/auth/session.js +167 -0
  18. package/dist/auth/user.d.ts +28 -0
  19. package/dist/client/timezone.d.ts +4 -0
  20. package/dist/client/timezone.js +11 -0
  21. package/dist/client.d.ts +1 -4
  22. package/dist/client.js +1 -9
  23. package/dist/cookies.js +47 -0
  24. package/dist/core/datasource.d.ts +21 -0
  25. package/dist/core/datasource.js +221 -0
  26. package/dist/core/integration.d.ts +7 -0
  27. package/dist/core/integration.js +32 -0
  28. package/dist/{types-n-Sir_Y2.d.ts → core/query.d.ts} +1 -129
  29. package/dist/core/query.js +528 -0
  30. package/dist/core/workflow.d.ts +7 -0
  31. package/dist/core/workflow.js +19 -0
  32. package/dist/index.d.ts +8 -172
  33. package/dist/index.js +4 -176
  34. package/dist/routes/discover.d.ts +9 -0
  35. package/dist/routes/discover.js +198 -0
  36. package/dist/routes/manifest.d.ts +17 -0
  37. package/dist/routes/manifest.js +148 -0
  38. package/dist/routes/mem-fs.js +71 -0
  39. package/dist/routes/visibility.d.ts +142 -0
  40. package/dist/routes/visibility.js +125 -0
  41. package/dist/server.d.ts +4 -113
  42. package/dist/server.js +4 -429
  43. package/dist/types.d.ts +115 -0
  44. package/dist/vite.d.ts +1 -21
  45. package/dist/vite.js +2 -405
  46. package/package.json +4 -4
  47. package/dist/session-kTqxT5_K.js +0 -891
package/dist/vite.js CHANGED
@@ -1,411 +1,8 @@
1
- import { randomUUID } from "node:crypto";
2
- import * as nodeFs from "node:fs/promises";
3
- import { mkdir, readFile, writeFile } from "node:fs/promises";
1
+ import { ManifestError, generateRoutesManifest } from "./routes/manifest.js";
2
+ import { mkdir, writeFile } from "node:fs/promises";
4
3
  import path from "node:path";
5
4
  import { pathToFileURL } from "node:url";
6
- import { Generator, getConfig, inferFullPath } from "@tanstack/router-generator";
7
- import os from "node:os";
8
- import { parse } from "@babel/parser";
9
- import babelTraverse from "@babel/traverse";
10
5
 
11
- //#region src/routes/mem-fs.ts
12
- /**
13
- * Reads through to disk and swallows every write. Driving the generator without this rewrites the
14
- * app's route sources and strips Start's `Register` block from its routeTree.gen.ts.
15
- */
16
- function createReadOnlyFs() {
17
- const writes = /* @__PURE__ */ new Map();
18
- return {
19
- async stat(filePath) {
20
- const stat = await statOrNull(path.resolve(filePath));
21
- if (stat === null) return {
22
- mtimeMs: SYNTHETIC_MTIME,
23
- mode: 420,
24
- uid: 0,
25
- gid: 0
26
- };
27
- return {
28
- mtimeMs: stat.mtimeMs,
29
- mode: Number(stat.mode),
30
- uid: Number(stat.uid),
31
- gid: Number(stat.gid)
32
- };
33
- },
34
- async rename(oldPath, newPath) {
35
- const from = path.resolve(oldPath);
36
- const content = writes.get(from);
37
- if (content !== void 0) {
38
- writes.delete(from);
39
- writes.set(path.resolve(newPath), content);
40
- }
41
- },
42
- async writeFile(filePath, content) {
43
- writes.set(path.resolve(filePath), content);
44
- },
45
- async readFile(filePath) {
46
- const resolved = path.resolve(filePath);
47
- const pending = writes.get(resolved);
48
- const stat = await statOrNull(resolved);
49
- if (pending !== void 0) return {
50
- stat: { mtimeMs: stat?.mtimeMs ?? SYNTHETIC_MTIME },
51
- fileContent: pending
52
- };
53
- if (stat === null) return "file-not-existing";
54
- try {
55
- const fileContent = await nodeFs.readFile(resolved, "utf8");
56
- return {
57
- stat: { mtimeMs: stat.mtimeMs },
58
- fileContent
59
- };
60
- } catch {
61
- return "file-not-existing";
62
- }
63
- },
64
- async chmod() {},
65
- async chown() {}
66
- };
67
- }
68
- const SYNTHETIC_MTIME = 1n;
69
- async function statOrNull(filePath) {
70
- try {
71
- return await nodeFs.stat(filePath, { bigint: true });
72
- } catch {
73
- return null;
74
- }
75
- }
76
-
77
- //#endregion
78
- //#region src/routes/discover.ts
79
- async function crawlRoutes(opts) {
80
- const config = getConfig({
81
- disableLogging: true,
82
- tmpDir: TMP_DIR,
83
- generatedRouteTree: generatedRouteTreePath()
84
- }, opts.root);
85
- const generator = new Generator({
86
- config,
87
- root: opts.root,
88
- fs: createReadOnlyFs()
89
- });
90
- await generator.run();
91
- const crawlingResult = await generator.getCrawlingResult();
92
- if (!crawlingResult) throw new Error(`the route crawl produced no result for ${opts.root}`);
93
- return {
94
- rootRouteNode: crawlingResult.rootRouteNode,
95
- routeNodes: crawlingResult.acc.routeNodes,
96
- routePiecesByPath: crawlingResult.acc.routePiecesByPath,
97
- routeTree: crawlingResult.acc.routeTree,
98
- routesDirectory: path.relative(opts.root, path.resolve(opts.root, config.routesDirectory)).split(path.sep).join("/")
99
- };
100
- }
101
- /** Reads the rule out of the source text. The route file is never executed. */
102
- function extractVisibility(source, filePath) {
103
- let program;
104
- try {
105
- program = parse(source, {
106
- sourceType: "module",
107
- plugins: ["typescript", "jsx"]
108
- });
109
- } catch (error) {
110
- const position = error.loc;
111
- return fail("unparsable", `could not parse ${filePath}: ${error.message}`, filePath, position?.line ?? 1, position?.column ?? 0);
112
- }
113
- const calls = findVisibilityCalls(program);
114
- if (calls.length > 1) {
115
- const second = calls[1].call;
116
- return fail("duplicate-declaration", `${filePath} declares visibility more than once; a route has exactly one rule`, filePath, second.loc?.start.line ?? 1, second.loc?.start.column ?? 0);
117
- }
118
- const declaration = calls[0];
119
- if (declaration === void 0) return {
120
- ok: true,
121
- visibility: null
122
- };
123
- const { call, statement } = declaration;
124
- const argument = call.arguments[0];
125
- if (argument === void 0) return fail("not-literal", `${filePath} calls ${VISIBILITY_FUNCTION} with no argument`, filePath, call.loc?.start.line ?? 1, call.loc?.start.column ?? 0);
126
- const read = readLiteral(argument);
127
- if (!read.ok) return fail("not-literal", `${filePath}: the visibility rule must be a literal — ${read.reason}`, filePath, read.line, read.column);
128
- return {
129
- ok: true,
130
- visibility: {
131
- value: read.value,
132
- line: statement.loc?.start.line ?? 1,
133
- column: statement.loc?.start.column ?? 0
134
- }
135
- };
136
- }
137
- /**
138
- * The generator `mkdirSync`s `tmpDir` and the directory of `generatedRouteTree` outside the `fs`
139
- * shim, so defaulted they create directories in the crawled project. Aimed at `os.tmpdir()` both
140
- * calls are no-ops, and nothing is ever written through either path. The uuid keeps the generated
141
- * tree file a miss rather than whatever a shared temp directory happens to hold.
142
- */
143
- const TMP_DIR = os.tmpdir();
144
- const generatedRouteTreePath = () => path.join(TMP_DIR, `softr-sdk-route-tree-${randomUUID()}.gen.ts`);
145
- const SDK_PACKAGE_NAME = "@softr/sdk";
146
- const VISIBILITY_FUNCTION = "defineVisibility";
147
- function fail(code, message, filePath, line, column) {
148
- return {
149
- ok: false,
150
- error: {
151
- code,
152
- message,
153
- filePath,
154
- line,
155
- column
156
- }
157
- };
158
- }
159
- const traverse = babelTraverse.default ?? babelTraverse;
160
- /** Only a call initialising a top-level variable counts; one inside a function or block does not. */
161
- function findVisibilityCalls(ast) {
162
- const found = [];
163
- traverse(ast, { VariableDeclarator(path$1) {
164
- const list = path$1.parentPath;
165
- if (!list.isVariableDeclaration()) return;
166
- const statement = list.parentPath.isExportNamedDeclaration() ? list.parentPath : list;
167
- if (!statement.parentPath?.isProgram()) return;
168
- if (path$1.node.init === null || path$1.node.init === void 0) return;
169
- const call = unwrap(path$1.node.init);
170
- if (call.type !== "CallExpression" && call.type !== "OptionalCallExpression") return;
171
- if (call.callee.type !== "Identifier") return;
172
- if (!isVisibilityImport(path$1, call.callee.name)) return;
173
- found.push({
174
- call,
175
- statement: statement.node
176
- });
177
- } });
178
- return found;
179
- }
180
- function isVisibilityImport(path$1, name) {
181
- const binding = path$1.scope.getBinding(name);
182
- if (binding === void 0 || binding.kind !== "module") return false;
183
- const specifier = binding.path.node;
184
- const declaration = binding.path.parent;
185
- if (specifier.type !== "ImportSpecifier" || declaration.type !== "ImportDeclaration") return false;
186
- if (specifier.importKind === "type" || declaration.importKind === "type") return false;
187
- if (declaration.source.value !== SDK_PACKAGE_NAME) return false;
188
- return (specifier.imported.type === "Identifier" ? specifier.imported.name : specifier.imported.value) === VISIBILITY_FUNCTION;
189
- }
190
- /** Strips the wrappers that leave the runtime value unchanged: `as const`, `satisfies`, `!`, `()`. */
191
- function unwrap(node) {
192
- switch (node.type) {
193
- case "TSAsExpression":
194
- case "TSSatisfiesExpression":
195
- case "TSNonNullExpression":
196
- case "TSTypeAssertion":
197
- case "TSInstantiationExpression":
198
- case "ParenthesizedExpression": return unwrap(node.expression);
199
- default: return node;
200
- }
201
- }
202
- function reject(node, reason) {
203
- return {
204
- ok: false,
205
- reason,
206
- line: node.loc?.start.line ?? 1,
207
- column: node.loc?.start.column ?? 0
208
- };
209
- }
210
- function readLiteral(input) {
211
- const node = unwrap(input);
212
- switch (node.type) {
213
- case "StringLiteral": return {
214
- ok: true,
215
- value: node.value
216
- };
217
- case "TemplateLiteral":
218
- if (node.expressions.length > 0) return reject(node, "a template literal with an expression is not a static value");
219
- return {
220
- ok: true,
221
- value: node.quasis[0]?.value.cooked ?? ""
222
- };
223
- case "ArrayExpression": {
224
- const values = [];
225
- for (const element of node.elements) {
226
- if (element === null) return reject(node, "an array hole is not a static value");
227
- if (element.type === "SpreadElement") return reject(element, "a spread is not a static value");
228
- const read = readLiteral(element);
229
- if (!read.ok) return read;
230
- values.push(read.value);
231
- }
232
- return {
233
- ok: true,
234
- value: values
235
- };
236
- }
237
- case "ObjectExpression": {
238
- const value = {};
239
- for (const property of node.properties) {
240
- if (property.type === "SpreadElement") return reject(property, "a spread is not a static value");
241
- if (property.type === "ObjectMethod") return reject(property, "a method is not a static value");
242
- if (property.computed) return reject(property, "a computed key is not a static value");
243
- if (property.shorthand) return reject(property, "a shorthand property is not a static value");
244
- const key = property.key.type === "Identifier" ? property.key.name : property.key.type === "StringLiteral" ? property.key.value : null;
245
- if (key === null) return reject(property.key, "this key is not a static value");
246
- if (key === "__proto__") return reject(property.key, "a __proto__ key is not a static value");
247
- const read = readLiteral(property.value);
248
- if (!read.ok) return read;
249
- value[key] = read.value;
250
- }
251
- return {
252
- ok: true,
253
- value
254
- };
255
- }
256
- case "NumericLiteral":
257
- case "BooleanLiteral":
258
- case "NullLiteral": return reject(node, "a visibility rule is built from strings, arrays and objects");
259
- case "Identifier": return reject(node, `an identifier (${node.name}) is not a static value`);
260
- case "CallExpression":
261
- case "NewExpression": return reject(node, "a call is not a static value");
262
- default: return reject(node, `${node.type} is not a static value`);
263
- }
264
- }
265
-
266
- //#endregion
267
- //#region src/routes/manifest.ts
268
- var ManifestError = class extends Error {
269
- detail;
270
- constructor(detail) {
271
- super(detail.message);
272
- this.name = "ManifestError";
273
- this.detail = detail;
274
- }
275
- };
276
- /** Returns the manifest as the exact text to write. Throws on a declaration it cannot read. */
277
- async function generateRoutesManifest(root) {
278
- const crawl = await crawlRoutes({ root });
279
- const routeFiles = new Set([crawl.rootRouteNode.filePath]);
280
- for (const node of crawl.routeNodes) routeFiles.add(node.filePath);
281
- for (const pieces of Object.values(crawl.routePiecesByPath)) if (pieces.lazy) routeFiles.add(pieces.lazy.filePath);
282
- const sources = /* @__PURE__ */ new Map();
283
- for (const relative of routeFiles) {
284
- const absolute = path.resolve(root, crawl.routesDirectory, relative);
285
- const source = await readIfPresent(absolute);
286
- if (source !== null) sources.set(absolute, source);
287
- }
288
- const manifest = buildManifest({
289
- crawl,
290
- root,
291
- sources
292
- });
293
- return `${JSON.stringify(manifest, null, 2)}\n`;
294
- }
295
- function buildManifest(opts) {
296
- const routesDirectory = opts.crawl.routesDirectory;
297
- const toNode = (node) => {
298
- const projectRelative = toProjectRelative(opts.root, routesDirectory, node.filePath);
299
- const routePath = node.routePath;
300
- if (routePath === void 0) throw new Error(`the route crawl produced a node with no route path: ${projectRelative}`);
301
- let declared = visibilityOf(opts, node.filePath);
302
- const lazyPiece = opts.crawl.routePiecesByPath[routePath]?.lazy;
303
- if (lazyPiece && lazyPiece.filePath !== node.filePath) {
304
- const lazyDeclared = visibilityOf(opts, lazyPiece.filePath);
305
- if (lazyDeclared !== null && declared !== null) throw new ManifestError({
306
- code: "duplicate-declaration",
307
- message: `${routePath} declares visibility in both ${projectRelative} and ${lazyDeclared.filePath}; a route has exactly one rule`,
308
- filePath: lazyDeclared.filePath,
309
- line: lazyDeclared.line,
310
- column: lazyDeclared.column
311
- });
312
- if (lazyDeclared !== null) declared = lazyDeclared;
313
- }
314
- return {
315
- routePath,
316
- filePath: projectRelative,
317
- ...node._fsRouteType ? { _fsRouteType: node._fsRouteType } : {},
318
- inferFullPath: inferFullPath(node) ?? null,
319
- createFileRouteProps: [...node.createFileRouteProps ?? []].sort(),
320
- visibility: declared?.value ?? null,
321
- children: (node.children ?? []).map(toNode)
322
- };
323
- };
324
- const rootNode = opts.crawl.rootRouteNode;
325
- const rootRelative = toProjectRelative(opts.root, routesDirectory, rootNode.filePath);
326
- const rootDeclared = visibilityOf(opts, rootNode.filePath);
327
- if (rootDeclared !== null) throw new ManifestError({
328
- code: "root-declaration",
329
- message: "the root route cannot carry a visibility rule: it would gate sign-in with no way back in. Move this declaration to the routes you want gated.",
330
- filePath: rootRelative,
331
- line: rootDeclared.line,
332
- column: rootDeclared.column
333
- });
334
- return { root: {
335
- routePath: ROOT_ROUTE_ID,
336
- filePath: rootRelative,
337
- inferFullPath: inferFullPath(rootNode) ?? null,
338
- createFileRouteProps: [...rootNode.createFileRouteProps ?? []].sort(),
339
- visibility: null,
340
- children: opts.crawl.routeTree.map(toNode)
341
- } };
342
- }
343
- /**
344
- * `rootRouteId` from `@tanstack/router-core`, which is what a consumer matches against — not the
345
- * generator's `rootPathId`, which is the base name of `__root.tsx` and names a file, not a route.
346
- */
347
- const ROOT_ROUTE_ID = "__root__";
348
- /** The crawl's paths are relative to the routes directory; the manifest's are project-relative. */
349
- function toProjectRelative(root, routesDirectory, filePath) {
350
- const absolute = path.resolve(root, routesDirectory, filePath);
351
- return path.relative(root, absolute).split(path.sep).join("/");
352
- }
353
- const USER_GROUPS = [
354
- "ALL_USERS",
355
- "NON_LOGGED_IN_USERS",
356
- "LOGGED_IN_USERS"
357
- ];
358
- function checkVisibilityShape(declared) {
359
- const reject$1 = (message) => {
360
- throw new ManifestError({
361
- code: "invalid-visibility",
362
- message: `${declared.filePath}: ${message}`,
363
- filePath: declared.filePath,
364
- line: declared.line,
365
- column: declared.column
366
- });
367
- };
368
- const visibility = declared.value;
369
- if (typeof visibility !== "object" || Array.isArray(visibility)) reject$1(`the visibility rule must be an object literal like { userGroup: 'ALL_USERS' }, not ${JSON.stringify(visibility)}`);
370
- for (const key of Object.keys(visibility)) {
371
- if (key === "userGroup" || key === "userGroupIds") continue;
372
- reject$1(`"${key}" is not part of a visibility rule; a rule has userGroup and, under LOGGED_IN_USERS, userGroupIds. Check the spelling.`);
373
- }
374
- const userGroup = visibility["userGroup"];
375
- if (typeof userGroup !== "string" || !USER_GROUPS.includes(userGroup)) reject$1(`userGroup must be one of ${USER_GROUPS.join(", ")}, not ${JSON.stringify(userGroup)}. A rule the build cannot read is a route with no rule at all.`);
376
- if (!("userGroupIds" in visibility)) return;
377
- if (userGroup !== "LOGGED_IN_USERS") reject$1(`userGroupIds only means anything under LOGGED_IN_USERS, not under ${userGroup}`);
378
- const ids = visibility["userGroupIds"];
379
- if (!Array.isArray(ids) || ids.some((id) => typeof id !== "string")) reject$1(`userGroupIds must be an array of group id strings, not ${JSON.stringify(ids)}. Drop the key entirely to mean "any logged-in user".`);
380
- }
381
- function visibilityOf(opts, filePath) {
382
- const projectRelative = toProjectRelative(opts.root, opts.crawl.routesDirectory, filePath);
383
- const source = opts.sources.get(path.resolve(opts.root, opts.crawl.routesDirectory, filePath));
384
- if (source === void 0) return null;
385
- const result = extractVisibility(source, projectRelative);
386
- if (!result.ok) throw new ManifestError(result.error);
387
- if (result.visibility === null) return null;
388
- const declared = {
389
- ...result.visibility,
390
- filePath: projectRelative
391
- };
392
- checkVisibilityShape(declared);
393
- return declared;
394
- }
395
- /**
396
- * Only ENOENT becomes `null`. The caller reads `null` as "nothing declared here", so swallowing any
397
- * other read error would turn an unreadable declaration into an ungated route.
398
- */
399
- async function readIfPresent(filePath) {
400
- try {
401
- return await readFile(filePath, "utf8");
402
- } catch (error) {
403
- if (error.code === "ENOENT") return null;
404
- throw error;
405
- }
406
- }
407
-
408
- //#endregion
409
6
  //#region src/vite.ts
410
7
  const MANIFEST_RELATIVE_PATH = path.join("dist", "routes.json");
411
8
  async function renderManifest(root) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@softr/sdk",
3
- "version": "0.0.12",
3
+ "version": "0.0.14",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" },
@@ -12,14 +12,14 @@
12
12
  "publishConfig": { "access": "public" },
13
13
  "scripts": {
14
14
  "build": "tsdown",
15
- "test": "node --test",
15
+ "test": "node --import ./src/test-utils/setup.ts --test",
16
16
  "typecheck": "tsc --noEmit"
17
17
  },
18
18
  "dependencies": {
19
19
  "@babel/parser": "^7.28.0",
20
20
  "@babel/traverse": "^7.28.0",
21
21
  "@babel/types": "^7.28.0",
22
- "@tanstack/router-generator": "1.167.23",
22
+ "@tanstack/router-generator": "^1.167.0",
23
23
  "zod": "^4.4.3"
24
24
  },
25
25
  "devDependencies": {
@@ -32,7 +32,7 @@
32
32
  "vite": "^8.0.0"
33
33
  },
34
34
  "peerDependencies": {
35
- "react": ">=18",
35
+ "react": "^18.0.0 || ^19.0.0",
36
36
  "vite": "^8.0.0",
37
37
  "@tanstack/react-router": "^1.168.0",
38
38
  "@tanstack/react-start": "^1.168.0"