@n-dream/builders-sdk 1.0.0-next.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +104 -7
- package/dist/auth.d.ts +25 -0
- package/dist/auth.js +159 -0
- package/dist/data.d.ts +3 -0
- package/dist/data.js +4 -0
- package/dist/db.d.ts +125 -0
- package/dist/db.js +581 -0
- package/dist/declared.d.ts +1 -0
- package/dist/declared.js +5 -0
- package/dist/engine.d.ts +236 -0
- package/dist/engine.js +981 -0
- package/dist/errors.d.ts +4 -1
- package/dist/errors.js +22 -2
- package/dist/index.d.ts +3 -1
- package/dist/index.js +5 -2
- package/dist/local-db.d.ts +15 -0
- package/dist/local-db.js +396 -0
- package/dist/local.d.ts +13 -0
- package/dist/local.js +70 -0
- package/dist/memory-storage.d.ts +55 -0
- package/dist/memory-storage.js +276 -0
- package/dist/rules.d.ts +219 -0
- package/dist/rules.js +768 -0
- package/dist/server.d.ts +3 -0
- package/dist/server.js +6 -0
- package/dist/session.d.ts +5 -0
- package/dist/session.js +18 -0
- package/dist/testing.d.ts +14 -0
- package/dist/testing.js +50 -0
- package/dist/vite.d.ts +21 -0
- package/dist/vite.js +46 -0
- package/package.json +20 -2
package/dist/server.d.ts
ADDED
package/dist/server.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// @n-dream/builders-sdk/engine: the data engine and its memory storage for
|
|
2
|
+
// the Builders services (apps/data, apps/api, packages/store). Apps do not
|
|
3
|
+
// use it: they use `db` (docs/login-and-data.md 8.1).
|
|
4
|
+
export * from "./engine.js";
|
|
5
|
+
export { MemoryStorage } from "./memory-storage.js";
|
|
6
|
+
export * from "./rules.js";
|
package/dist/session.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// The user that this page believes is signed in (the user guard, 8.3).
|
|
2
|
+
// auth keeps it; every data and file request names it, and the data
|
|
3
|
+
// service (or the local mode) refuses a request whose user is not the
|
|
4
|
+
// session's user with user-changed.
|
|
5
|
+
export const believed = {
|
|
6
|
+
// undefined until auth knows; null: nobody.
|
|
7
|
+
uid: undefined,
|
|
8
|
+
// Reads the signed-in user again (auth sets it). Its listeners hear a
|
|
9
|
+
// change.
|
|
10
|
+
refresh: null,
|
|
11
|
+
};
|
|
12
|
+
// The user to name in a request: auth reads it first when it does not know
|
|
13
|
+
// it yet. undefined: still not known (offline), then the request names none.
|
|
14
|
+
export async function claimedUser() {
|
|
15
|
+
if (believed.uid === undefined)
|
|
16
|
+
await believed.refresh?.().catch(() => { });
|
|
17
|
+
return believed.uid;
|
|
18
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { LoginMode } from "./rules.js";
|
|
2
|
+
export declare function signInAs(name: string, opts?: {
|
|
3
|
+
developer?: boolean;
|
|
4
|
+
}): void;
|
|
5
|
+
export declare function signOut(): void;
|
|
6
|
+
export declare function reset(): void;
|
|
7
|
+
export declare function setLoginMode(mode: LoginMode, list?: string[]): void;
|
|
8
|
+
export declare function seed(collection: string, items: Array<{
|
|
9
|
+
id?: string;
|
|
10
|
+
data: Record<string, unknown>;
|
|
11
|
+
}>, opts?: {
|
|
12
|
+
as?: string;
|
|
13
|
+
developer?: boolean;
|
|
14
|
+
}): Promise<void>;
|
package/dist/testing.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// @n-dream/builders-sdk/testing: helpers for headless tests in the local
|
|
2
|
+
// mode (docs/login-and-data.md 8.4). They work only on localhost.
|
|
3
|
+
import { isLocalHost, local, localUserId, setLocalUser } from "./local.js";
|
|
4
|
+
import { localLoginRefusal, resetLocal, seedLocal, setLocalLoginMode } from "./local-db.js";
|
|
5
|
+
function check() {
|
|
6
|
+
if (!isLocalHost(globalThis.location?.hostname))
|
|
7
|
+
throw new Error("The test helpers of @n-dream/builders-sdk work only on localhost.");
|
|
8
|
+
}
|
|
9
|
+
// Signs in a test user. The same name gives the same user id. In the login
|
|
10
|
+
// modes "domains" and "emails" the name is the email, and it must be in the
|
|
11
|
+
// list (setLoginMode); a developer always signs in.
|
|
12
|
+
export function signInAs(name, opts = {}) {
|
|
13
|
+
check();
|
|
14
|
+
const refused = localLoginRefusal(name, !!opts.developer);
|
|
15
|
+
if (refused)
|
|
16
|
+
throw new Error(refused);
|
|
17
|
+
setLocalUser({ id: localUserId(name), name, developer: !!opts.developer });
|
|
18
|
+
}
|
|
19
|
+
export function signOut() {
|
|
20
|
+
check();
|
|
21
|
+
setLocalUser(null);
|
|
22
|
+
}
|
|
23
|
+
// Back to the start: nobody signed in, no test users, no data, login mode
|
|
24
|
+
// "anyone".
|
|
25
|
+
export function reset() {
|
|
26
|
+
check();
|
|
27
|
+
resetLocal();
|
|
28
|
+
setLocalUser(null);
|
|
29
|
+
}
|
|
30
|
+
// Who can sign in (4.4). With "domains" or "emails", public lists need
|
|
31
|
+
// sign-in to read (LD9).
|
|
32
|
+
export function setLoginMode(mode, list = []) {
|
|
33
|
+
check();
|
|
34
|
+
setLocalLoginMode(mode, list);
|
|
35
|
+
}
|
|
36
|
+
// Writes items as a test user (default: the signed-in user, else "Seed
|
|
37
|
+
// user"), with the same rules as the app's own writes. An item with an id
|
|
38
|
+
// is set(id, data), else add(data).
|
|
39
|
+
export async function seed(collection, items, opts = {}) {
|
|
40
|
+
check();
|
|
41
|
+
const before = local.user;
|
|
42
|
+
const name = opts.as ?? before?.name ?? "Seed user";
|
|
43
|
+
setLocalUser({ id: localUserId(name), name, developer: opts.as ? !!opts.developer : (before?.developer ?? false) });
|
|
44
|
+
try {
|
|
45
|
+
await seedLocal(collection, items);
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
setLocalUser(before);
|
|
49
|
+
}
|
|
50
|
+
}
|
package/dist/vite.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
interface PluginContext {
|
|
2
|
+
error(message: string): never;
|
|
3
|
+
addWatchFile(id: string): void;
|
|
4
|
+
}
|
|
5
|
+
export interface BuildersVitePlugin {
|
|
6
|
+
name: string;
|
|
7
|
+
enforce: "pre";
|
|
8
|
+
config(): {
|
|
9
|
+
optimizeDeps: {
|
|
10
|
+
exclude: string[];
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
configResolved(c: {
|
|
14
|
+
root: string;
|
|
15
|
+
}): void;
|
|
16
|
+
load(this: PluginContext, id: string): Promise<string | null>;
|
|
17
|
+
}
|
|
18
|
+
export declare function builders(opts?: {
|
|
19
|
+
file?: string;
|
|
20
|
+
}): BuildersVitePlugin;
|
|
21
|
+
export {};
|
package/dist/vite.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// @n-dream/builders-sdk/vite: the Vite plugin of the local mode
|
|
2
|
+
// (docs/login-and-data.md 8.4). It puts builders.data.json of the app into
|
|
3
|
+
// the build, so that the local mode on localhost (vite dev and
|
|
4
|
+
// test:headless) knows the collections. It checks the file with the same
|
|
5
|
+
// rules as the CLI and the platform, and stops the build with the problems.
|
|
6
|
+
//
|
|
7
|
+
// import { builders } from "@n-dream/builders-sdk/vite";
|
|
8
|
+
// export default defineConfig({ plugins: [builders(), ...] });
|
|
9
|
+
//
|
|
10
|
+
// The file is not secret: it has only the names and presets.
|
|
11
|
+
import { DECLARATION_FILE, parseDeclaration } from "./rules.js";
|
|
12
|
+
// The module of the SDK that the plugin fills (src/declared.ts).
|
|
13
|
+
const DECLARED = /[\\/]builders-sdk[\\/](?:dist[\\/]declared\.js|src[\\/]declared\.ts)$/;
|
|
14
|
+
export function builders(opts = {}) {
|
|
15
|
+
let root = ".";
|
|
16
|
+
return {
|
|
17
|
+
name: "builders-sdk",
|
|
18
|
+
enforce: "pre",
|
|
19
|
+
// Pre-bundled dependencies skip load(): the SDK stays a normal module.
|
|
20
|
+
config: () => ({ optimizeDeps: { exclude: ["@n-dream/builders-sdk"] } }),
|
|
21
|
+
configResolved(c) {
|
|
22
|
+
root = c.root;
|
|
23
|
+
},
|
|
24
|
+
async load(id) {
|
|
25
|
+
if (!DECLARED.test(id.split("?")[0]))
|
|
26
|
+
return null;
|
|
27
|
+
const fsName = "node:fs";
|
|
28
|
+
const fs = (await import(/* @vite-ignore */ fsName));
|
|
29
|
+
const file = `${root.replace(/[\\/]$/, "")}/${opts.file ?? DECLARATION_FILE}`;
|
|
30
|
+
this.addWatchFile(file);
|
|
31
|
+
if (!fs.existsSync(file))
|
|
32
|
+
return "export const declared = null;\n";
|
|
33
|
+
let raw;
|
|
34
|
+
try {
|
|
35
|
+
raw = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
36
|
+
}
|
|
37
|
+
catch (e) {
|
|
38
|
+
this.error(`${DECLARATION_FILE} is not valid JSON: ${e.message}`);
|
|
39
|
+
}
|
|
40
|
+
const r = parseDeclaration(raw);
|
|
41
|
+
if (!r.ok)
|
|
42
|
+
this.error(`${DECLARATION_FILE} is not valid:\n${r.problems.map((p) => `- ${p}`).join("\n")}`);
|
|
43
|
+
return `export const declared = ${JSON.stringify(r.value)};\n`;
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@n-dream/builders-sdk",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Builders SDK for app code: app information
|
|
3
|
+
"version": "1.4.0",
|
|
4
|
+
"description": "Builders SDK for app code: app information, sign-in of app users and app data.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/index.js",
|
|
@@ -10,6 +10,24 @@
|
|
|
10
10
|
".": {
|
|
11
11
|
"types": "./dist/index.d.ts",
|
|
12
12
|
"import": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./testing": {
|
|
15
|
+
"types": "./dist/testing.d.ts",
|
|
16
|
+
"import": "./dist/testing.js"
|
|
17
|
+
},
|
|
18
|
+
"./vite": {
|
|
19
|
+
"types": "./dist/vite.d.ts",
|
|
20
|
+
"import": "./dist/vite.js"
|
|
21
|
+
},
|
|
22
|
+
"./rules": {
|
|
23
|
+
"builders-source": "./src/rules.ts",
|
|
24
|
+
"types": "./dist/rules.d.ts",
|
|
25
|
+
"import": "./dist/rules.js"
|
|
26
|
+
},
|
|
27
|
+
"./engine": {
|
|
28
|
+
"builders-source": "./src/server.ts",
|
|
29
|
+
"types": "./dist/server.d.ts",
|
|
30
|
+
"import": "./dist/server.js"
|
|
13
31
|
}
|
|
14
32
|
},
|
|
15
33
|
"files": [
|