talizen 0.2.7 → 0.2.8
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 -9
- package/auth.d.ts +16 -0
- package/auth.js +20 -0
- package/func-runtime.d.ts +37 -3
- package/func-runtime.js +1 -21
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -112,7 +112,14 @@ When a `File` object appears in the payload, `submitForm()` will:
|
|
|
112
112
|
### Login users
|
|
113
113
|
|
|
114
114
|
```ts
|
|
115
|
-
import {
|
|
115
|
+
import {
|
|
116
|
+
currentUser,
|
|
117
|
+
listAuthProviders,
|
|
118
|
+
login,
|
|
119
|
+
loginWithOAuth,
|
|
120
|
+
logout,
|
|
121
|
+
register,
|
|
122
|
+
} from "talizen/auth";
|
|
116
123
|
|
|
117
124
|
await register({
|
|
118
125
|
account: "alice",
|
|
@@ -124,6 +131,11 @@ await login({ account: "alice", password: "secret" });
|
|
|
124
131
|
|
|
125
132
|
const user = await currentUser();
|
|
126
133
|
await logout();
|
|
134
|
+
|
|
135
|
+
const providers = await listAuthProviders();
|
|
136
|
+
console.log(providers.map((provider) => provider.key));
|
|
137
|
+
|
|
138
|
+
await loginWithOAuth("github", { redirectUrl: "/account" });
|
|
127
139
|
```
|
|
128
140
|
|
|
129
141
|
### Invoke a custom function
|
|
@@ -146,24 +158,25 @@ await invoke("booking", { email: "hi@talizen.com" });
|
|
|
146
158
|
|
|
147
159
|
### Write function runtime code
|
|
148
160
|
|
|
149
|
-
Func code can use TypeScript and import
|
|
161
|
+
Func code can use TypeScript and import Func authoring types from `talizen/func-runtime`.
|
|
162
|
+
Runtime capabilities are passed through `ctx`:
|
|
150
163
|
|
|
151
164
|
```ts
|
|
152
|
-
import {
|
|
165
|
+
import type { TalizenFuncContext } from "talizen/func-runtime";
|
|
153
166
|
|
|
154
|
-
export function create(input: { title: string }) {
|
|
155
|
-
const user = auth.requireUser();
|
|
156
|
-
const row = db.insert("book", {
|
|
167
|
+
export function create(input: { title: string }, ctx: TalizenFuncContext) {
|
|
168
|
+
const user = ctx.auth.requireUser();
|
|
169
|
+
const row = ctx.db.insert("book", {
|
|
157
170
|
title: input.title,
|
|
158
171
|
user_id: user.id,
|
|
159
172
|
status: "draft",
|
|
160
173
|
});
|
|
161
|
-
cache.set(`book:${row.id}`, row, 60);
|
|
174
|
+
ctx.cache.set(`book:${row.id}`, row, 60);
|
|
162
175
|
return { ok: true, id: row.id };
|
|
163
176
|
}
|
|
164
177
|
```
|
|
165
178
|
|
|
166
|
-
`db`, `cache`, and
|
|
179
|
+
`ctx.db`, `ctx.cache`, `ctx.auth`, `ctx.request`, and `ctx.cookies` are injected by the Talizen Func runtime. `talizen/func-runtime` is a type-only authoring module; do not import runtime values from it.
|
|
167
180
|
|
|
168
181
|
## Package Layout
|
|
169
182
|
|
|
@@ -172,7 +185,7 @@ export function create(input: { title: string }) {
|
|
|
172
185
|
- `talizen/cms`: CMS content types and content query APIs.
|
|
173
186
|
- `talizen/form`: form submission helpers and related types.
|
|
174
187
|
- `talizen/func`: custom function invocation helpers such as `invoke`.
|
|
175
|
-
- `talizen/func-runtime`: Func-runtime-only `
|
|
188
|
+
- `talizen/func-runtime`: Func-runtime-only `ctx` capability types.
|
|
176
189
|
|
|
177
190
|
## Publish
|
|
178
191
|
|
package/auth.d.ts
CHANGED
|
@@ -35,6 +35,19 @@ export interface AuthRegisterInput extends AuthPasswordInput {
|
|
|
35
35
|
avatar?: string;
|
|
36
36
|
profile?: unknown;
|
|
37
37
|
}
|
|
38
|
+
export interface AuthProvider {
|
|
39
|
+
key: string;
|
|
40
|
+
name: string;
|
|
41
|
+
scopes?: string;
|
|
42
|
+
status?: string;
|
|
43
|
+
}
|
|
44
|
+
export interface AuthProviderListResponse {
|
|
45
|
+
total?: number;
|
|
46
|
+
list?: AuthProvider[];
|
|
47
|
+
}
|
|
48
|
+
export interface AuthOAuthLoginURLOptions extends TalizenRequestOptions {
|
|
49
|
+
redirectUrl?: string;
|
|
50
|
+
}
|
|
38
51
|
export declare class TalizenAuthError extends Error {
|
|
39
52
|
constructor(message: string);
|
|
40
53
|
}
|
|
@@ -43,3 +56,6 @@ export declare function login(input: AuthPasswordInput, options?: TalizenRequest
|
|
|
43
56
|
export declare function logout(options?: TalizenRequestOptions): Promise<void>;
|
|
44
57
|
export declare function currentUser(options?: TalizenRequestOptions): Promise<AuthUser | null>;
|
|
45
58
|
export declare function requireUser(options?: TalizenRequestOptions): Promise<AuthUser>;
|
|
59
|
+
export declare function listAuthProviders(options?: TalizenRequestOptions): Promise<AuthProvider[]>;
|
|
60
|
+
export declare function getOAuthLoginUrl(provider: string, options?: AuthOAuthLoginURLOptions): Promise<string>;
|
|
61
|
+
export declare function loginWithOAuth(provider: string, options?: AuthOAuthLoginURLOptions): Promise<void>;
|
package/auth.js
CHANGED
|
@@ -33,3 +33,23 @@ export async function requireUser(options) {
|
|
|
33
33
|
}
|
|
34
34
|
return user;
|
|
35
35
|
}
|
|
36
|
+
export async function listAuthProviders(options) {
|
|
37
|
+
const response = await requestJson("/auth/provider_list", { method: "GET" }, options);
|
|
38
|
+
return response.list ?? [];
|
|
39
|
+
}
|
|
40
|
+
export async function getOAuthLoginUrl(provider, options) {
|
|
41
|
+
const url = new URL("/auth/oauth/login_url", "https://talizen.local");
|
|
42
|
+
url.searchParams.set("provider", provider);
|
|
43
|
+
if (options?.redirectUrl) {
|
|
44
|
+
url.searchParams.set("redirect_url", options.redirectUrl);
|
|
45
|
+
}
|
|
46
|
+
const response = await requestJson(url.pathname + url.search, { method: "GET" }, options);
|
|
47
|
+
return response.url;
|
|
48
|
+
}
|
|
49
|
+
export async function loginWithOAuth(provider, options) {
|
|
50
|
+
const target = await getOAuthLoginUrl(provider, options);
|
|
51
|
+
if (typeof window === "undefined" || !window.location) {
|
|
52
|
+
throw new TalizenAuthError("OAuth login requires a browser window.");
|
|
53
|
+
}
|
|
54
|
+
window.location.href = target;
|
|
55
|
+
}
|
package/func-runtime.d.ts
CHANGED
|
@@ -60,6 +60,40 @@ export interface FuncCacheRuntime {
|
|
|
60
60
|
ok?: boolean;
|
|
61
61
|
};
|
|
62
62
|
}
|
|
63
|
-
export
|
|
64
|
-
|
|
65
|
-
|
|
63
|
+
export interface FuncReadonlyStringMap {
|
|
64
|
+
get(name: string): string | null;
|
|
65
|
+
}
|
|
66
|
+
export interface FuncRequestRuntime {
|
|
67
|
+
host: string;
|
|
68
|
+
ip: string;
|
|
69
|
+
method: string;
|
|
70
|
+
path: string;
|
|
71
|
+
headers: FuncReadonlyStringMap;
|
|
72
|
+
cookies: FuncReadonlyStringMap;
|
|
73
|
+
}
|
|
74
|
+
export interface FuncCookieSetOptions {
|
|
75
|
+
path?: string;
|
|
76
|
+
domain?: string;
|
|
77
|
+
maxAge?: number;
|
|
78
|
+
secure?: boolean;
|
|
79
|
+
httpOnly?: boolean;
|
|
80
|
+
sameSite?: "lax" | "strict" | "none";
|
|
81
|
+
}
|
|
82
|
+
export interface FuncCookieRuntime {
|
|
83
|
+
get(name: string): string | null;
|
|
84
|
+
set(name: string, value: string, options?: FuncCookieSetOptions): {
|
|
85
|
+
ok?: boolean;
|
|
86
|
+
};
|
|
87
|
+
delete(name: string, options?: Pick<FuncCookieSetOptions, "path" | "domain">): {
|
|
88
|
+
ok?: boolean;
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
export interface TalizenFuncContext {
|
|
92
|
+
trace_id: string;
|
|
93
|
+
extra?: Record<string, unknown>;
|
|
94
|
+
request: FuncRequestRuntime;
|
|
95
|
+
db: FuncDbRuntime;
|
|
96
|
+
auth: FuncAuthRuntime;
|
|
97
|
+
cache: FuncCacheRuntime;
|
|
98
|
+
cookies: FuncCookieRuntime;
|
|
99
|
+
}
|
package/func-runtime.js
CHANGED
|
@@ -1,21 +1 @@
|
|
|
1
|
-
|
|
2
|
-
throw new Error("talizen/func-runtime is only available inside Talizen Func runtime.");
|
|
3
|
-
}
|
|
4
|
-
export const db = {
|
|
5
|
-
get: unavailable,
|
|
6
|
-
query: unavailable,
|
|
7
|
-
insert: unavailable,
|
|
8
|
-
update: unavailable,
|
|
9
|
-
delete: unavailable,
|
|
10
|
-
};
|
|
11
|
-
export const auth = {
|
|
12
|
-
currentUser: unavailable,
|
|
13
|
-
requireUser: unavailable,
|
|
14
|
-
};
|
|
15
|
-
export const cache = {
|
|
16
|
-
get: unavailable,
|
|
17
|
-
set: unavailable,
|
|
18
|
-
del: unavailable,
|
|
19
|
-
incr: unavailable,
|
|
20
|
-
expire: unavailable,
|
|
21
|
-
};
|
|
1
|
+
export {};
|