talizen 0.2.4 → 0.2.5
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 +22 -1
- package/func.d.ts +65 -0
- package/func.js +21 -0
- package/index.d.ts +0 -1
- package/index.js +0 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -137,13 +137,34 @@ const result = await invoke<{ success: boolean }>("user/auth.login", {
|
|
|
137
137
|
await invoke("user/auth", { email: "hi@talizen.com" });
|
|
138
138
|
```
|
|
139
139
|
|
|
140
|
+
### Write function runtime code
|
|
141
|
+
|
|
142
|
+
Func code can use TypeScript and import runtime-only helpers from `talizen/func`:
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
import { auth, db, cache } from "talizen/func";
|
|
146
|
+
|
|
147
|
+
export function create(input: { title: string }) {
|
|
148
|
+
const user = auth.requireUser();
|
|
149
|
+
const row = db.insert("book", {
|
|
150
|
+
title: input.title,
|
|
151
|
+
user_id: user.id,
|
|
152
|
+
status: "draft",
|
|
153
|
+
});
|
|
154
|
+
cache.set(`book:${row.id}`, row, 60);
|
|
155
|
+
return { ok: true, id: row.id };
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
`db`, `cache`, and Func `auth` are injected by the Talizen Func runtime. They are typed in this package for authoring, but they are not browser/Node general-purpose APIs.
|
|
160
|
+
|
|
140
161
|
## Package Layout
|
|
141
162
|
|
|
142
163
|
- `talizen/core`: shared runtime config, request helpers, and base data types.
|
|
143
164
|
- `talizen/auth`: project user register, login, logout, and current user helpers.
|
|
144
165
|
- `talizen/cms`: CMS content types and content query APIs.
|
|
145
166
|
- `talizen/form`: form submission helpers and related types.
|
|
146
|
-
- `talizen/func`: custom function invocation helpers.
|
|
167
|
+
- `talizen/func`: custom function invocation helpers and Func-runtime-only `db`, `cache`, `auth` types.
|
|
147
168
|
|
|
148
169
|
## Publish
|
|
149
170
|
|
package/func.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type TalizenRequestOptions } from "./core.js";
|
|
2
|
+
import type { AuthUser } from "./auth.js";
|
|
2
3
|
export interface FuncLogEntry {
|
|
3
4
|
level: string;
|
|
4
5
|
text: string;
|
|
@@ -9,6 +10,70 @@ export interface FuncRunResponse<T = unknown> {
|
|
|
9
10
|
logs?: FuncLogEntry[];
|
|
10
11
|
error?: string;
|
|
11
12
|
}
|
|
13
|
+
export type DbOrderBy = string;
|
|
14
|
+
export interface DbFilterCondition {
|
|
15
|
+
field_id?: string;
|
|
16
|
+
fieldId?: string;
|
|
17
|
+
operator: "equal" | "not_equal" | "in";
|
|
18
|
+
value?: unknown;
|
|
19
|
+
values?: unknown[];
|
|
20
|
+
}
|
|
21
|
+
export interface DbFilter {
|
|
22
|
+
match?: "and" | "or";
|
|
23
|
+
conditions?: DbFilterCondition[];
|
|
24
|
+
}
|
|
25
|
+
export interface DbQuery {
|
|
26
|
+
where?: Record<string, unknown>;
|
|
27
|
+
filter?: DbFilter;
|
|
28
|
+
limit?: number;
|
|
29
|
+
offset?: number;
|
|
30
|
+
order_by?: DbOrderBy;
|
|
31
|
+
orderBy?: DbOrderBy;
|
|
32
|
+
}
|
|
33
|
+
export type DbRecord<T extends Record<string, unknown> = Record<string, unknown>> = T & {
|
|
34
|
+
id: string;
|
|
35
|
+
};
|
|
36
|
+
export interface FuncDbRuntime {
|
|
37
|
+
get<T extends Record<string, unknown> = Record<string, unknown>>(table: string, id: string): DbRecord<T> | null;
|
|
38
|
+
query<T extends Record<string, unknown> = Record<string, unknown>>(table: string, query?: DbQuery): Array<DbRecord<T>>;
|
|
39
|
+
insert<T extends Record<string, unknown> = Record<string, unknown>>(table: string, data: T): DbRecord<T>;
|
|
40
|
+
update<T extends Record<string, unknown> = Record<string, unknown>>(table: string, id: string, data: Partial<T>): {
|
|
41
|
+
ok?: boolean;
|
|
42
|
+
updated?: boolean;
|
|
43
|
+
} | DbRecord<T>;
|
|
44
|
+
delete(table: string, id: string): {
|
|
45
|
+
ok?: boolean;
|
|
46
|
+
deleted?: boolean;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
export interface FuncAuthRuntime {
|
|
50
|
+
currentUser(): AuthUser | null;
|
|
51
|
+
requireUser(): AuthUser;
|
|
52
|
+
}
|
|
53
|
+
export interface CacheSetOptions {
|
|
54
|
+
ttl?: number;
|
|
55
|
+
ttlSeconds?: number;
|
|
56
|
+
}
|
|
57
|
+
export interface FuncCacheRuntime {
|
|
58
|
+
get<T = unknown>(key: string): T | null;
|
|
59
|
+
set(key: string, value: unknown, ttlSeconds?: number): {
|
|
60
|
+
ok?: boolean;
|
|
61
|
+
};
|
|
62
|
+
set(key: string, value: unknown, options?: CacheSetOptions): {
|
|
63
|
+
ok?: boolean;
|
|
64
|
+
};
|
|
65
|
+
del(key: string): {
|
|
66
|
+
ok?: boolean;
|
|
67
|
+
deleted?: boolean;
|
|
68
|
+
};
|
|
69
|
+
incr(key: string, delta?: number): number;
|
|
70
|
+
expire(key: string, ttlSeconds: number): {
|
|
71
|
+
ok?: boolean;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
export declare const db: FuncDbRuntime;
|
|
75
|
+
export declare const auth: FuncAuthRuntime;
|
|
76
|
+
export declare const cache: FuncCacheRuntime;
|
|
12
77
|
export declare class TalizenFuncError extends Error {
|
|
13
78
|
readonly key: string;
|
|
14
79
|
readonly method: string;
|
package/func.js
CHANGED
|
@@ -1,4 +1,25 @@
|
|
|
1
1
|
import { requestJson, } from "./core.js";
|
|
2
|
+
function funcRuntimeUnavailable() {
|
|
3
|
+
throw new Error("talizen/func db/cache/auth are only available inside Talizen Func runtime.");
|
|
4
|
+
}
|
|
5
|
+
export const db = {
|
|
6
|
+
get: funcRuntimeUnavailable,
|
|
7
|
+
query: funcRuntimeUnavailable,
|
|
8
|
+
insert: funcRuntimeUnavailable,
|
|
9
|
+
update: funcRuntimeUnavailable,
|
|
10
|
+
delete: funcRuntimeUnavailable,
|
|
11
|
+
};
|
|
12
|
+
export const auth = {
|
|
13
|
+
currentUser: funcRuntimeUnavailable,
|
|
14
|
+
requireUser: funcRuntimeUnavailable,
|
|
15
|
+
};
|
|
16
|
+
export const cache = {
|
|
17
|
+
get: funcRuntimeUnavailable,
|
|
18
|
+
set: funcRuntimeUnavailable,
|
|
19
|
+
del: funcRuntimeUnavailable,
|
|
20
|
+
incr: funcRuntimeUnavailable,
|
|
21
|
+
expire: funcRuntimeUnavailable,
|
|
22
|
+
};
|
|
2
23
|
export class TalizenFuncError extends Error {
|
|
3
24
|
key;
|
|
4
25
|
method;
|
package/index.d.ts
CHANGED
package/index.js
CHANGED