talizen 0.2.7 → 0.2.9
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 +26 -9
- package/auth.d.ts +20 -2
- package/auth.js +26 -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,15 @@ 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
|
+
updateProfile,
|
|
123
|
+
} from "talizen/auth";
|
|
116
124
|
|
|
117
125
|
await register({
|
|
118
126
|
account: "alice",
|
|
@@ -123,7 +131,15 @@ await register({
|
|
|
123
131
|
await login({ account: "alice", password: "secret" });
|
|
124
132
|
|
|
125
133
|
const user = await currentUser();
|
|
134
|
+
await updateProfile({
|
|
135
|
+
address: "No. 1 Example Road",
|
|
136
|
+
});
|
|
126
137
|
await logout();
|
|
138
|
+
|
|
139
|
+
const providers = await listAuthProviders();
|
|
140
|
+
console.log(providers.map((provider) => provider.key));
|
|
141
|
+
|
|
142
|
+
await loginWithOAuth("github", { redirectUrl: "/account" });
|
|
127
143
|
```
|
|
128
144
|
|
|
129
145
|
### Invoke a custom function
|
|
@@ -146,24 +162,25 @@ await invoke("booking", { email: "hi@talizen.com" });
|
|
|
146
162
|
|
|
147
163
|
### Write function runtime code
|
|
148
164
|
|
|
149
|
-
Func code can use TypeScript and import
|
|
165
|
+
Func code can use TypeScript and import Func authoring types from `talizen/func-runtime`.
|
|
166
|
+
Runtime capabilities are passed through `ctx`:
|
|
150
167
|
|
|
151
168
|
```ts
|
|
152
|
-
import {
|
|
169
|
+
import type { TalizenFuncContext } from "talizen/func-runtime";
|
|
153
170
|
|
|
154
|
-
export function create(input: { title: string }) {
|
|
155
|
-
const user = auth.requireUser();
|
|
156
|
-
const row = db.insert("book", {
|
|
171
|
+
export function create(input: { title: string }, ctx: TalizenFuncContext) {
|
|
172
|
+
const user = ctx.auth.requireUser();
|
|
173
|
+
const row = ctx.db.insert("book", {
|
|
157
174
|
title: input.title,
|
|
158
175
|
user_id: user.id,
|
|
159
176
|
status: "draft",
|
|
160
177
|
});
|
|
161
|
-
cache.set(`book:${row.id}`, row, 60);
|
|
178
|
+
ctx.cache.set(`book:${row.id}`, row, 60);
|
|
162
179
|
return { ok: true, id: row.id };
|
|
163
180
|
}
|
|
164
181
|
```
|
|
165
182
|
|
|
166
|
-
`db`, `cache`, and
|
|
183
|
+
`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
184
|
|
|
168
185
|
## Package Layout
|
|
169
186
|
|
|
@@ -172,7 +189,7 @@ export function create(input: { title: string }) {
|
|
|
172
189
|
- `talizen/cms`: CMS content types and content query APIs.
|
|
173
190
|
- `talizen/form`: form submission helpers and related types.
|
|
174
191
|
- `talizen/func`: custom function invocation helpers such as `invoke`.
|
|
175
|
-
- `talizen/func-runtime`: Func-runtime-only `
|
|
192
|
+
- `talizen/func-runtime`: Func-runtime-only `ctx` capability types.
|
|
176
193
|
|
|
177
194
|
## Publish
|
|
178
195
|
|
package/auth.d.ts
CHANGED
|
@@ -13,12 +13,13 @@ export interface AuthUser {
|
|
|
13
13
|
name?: string;
|
|
14
14
|
avatar?: string;
|
|
15
15
|
status?: string;
|
|
16
|
-
profile?:
|
|
16
|
+
profile?: AuthProfile;
|
|
17
17
|
email_verified_at?: string | null;
|
|
18
18
|
last_login_at?: string | null;
|
|
19
19
|
created_at?: string;
|
|
20
20
|
updated_at?: string;
|
|
21
21
|
}
|
|
22
|
+
export type AuthProfile = Record<string, unknown>;
|
|
22
23
|
export interface AuthPasswordInput {
|
|
23
24
|
/**
|
|
24
25
|
* Login identifier for register/login.
|
|
@@ -33,7 +34,20 @@ export interface AuthRegisterInput extends AuthPasswordInput {
|
|
|
33
34
|
phone?: string;
|
|
34
35
|
name?: string;
|
|
35
36
|
avatar?: string;
|
|
36
|
-
profile?:
|
|
37
|
+
profile?: AuthProfile;
|
|
38
|
+
}
|
|
39
|
+
export interface AuthProvider {
|
|
40
|
+
key: string;
|
|
41
|
+
name: string;
|
|
42
|
+
scopes?: string;
|
|
43
|
+
status?: string;
|
|
44
|
+
}
|
|
45
|
+
export interface AuthProviderListResponse {
|
|
46
|
+
total?: number;
|
|
47
|
+
list?: AuthProvider[];
|
|
48
|
+
}
|
|
49
|
+
export interface AuthOAuthLoginURLOptions extends TalizenRequestOptions {
|
|
50
|
+
redirectUrl?: string;
|
|
37
51
|
}
|
|
38
52
|
export declare class TalizenAuthError extends Error {
|
|
39
53
|
constructor(message: string);
|
|
@@ -42,4 +56,8 @@ export declare function register(input: AuthRegisterInput, options?: TalizenRequ
|
|
|
42
56
|
export declare function login(input: AuthPasswordInput, options?: TalizenRequestOptions): Promise<AuthUser>;
|
|
43
57
|
export declare function logout(options?: TalizenRequestOptions): Promise<void>;
|
|
44
58
|
export declare function currentUser(options?: TalizenRequestOptions): Promise<AuthUser | null>;
|
|
59
|
+
export declare function updateProfile(profile: AuthProfile, options?: TalizenRequestOptions): Promise<AuthUser>;
|
|
45
60
|
export declare function requireUser(options?: TalizenRequestOptions): Promise<AuthUser>;
|
|
61
|
+
export declare function listAuthProviders(options?: TalizenRequestOptions): Promise<AuthProvider[]>;
|
|
62
|
+
export declare function getOAuthLoginUrl(provider: string, options?: AuthOAuthLoginURLOptions): Promise<string>;
|
|
63
|
+
export declare function loginWithOAuth(provider: string, options?: AuthOAuthLoginURLOptions): Promise<void>;
|
package/auth.js
CHANGED
|
@@ -26,6 +26,12 @@ export async function logout(options) {
|
|
|
26
26
|
export async function currentUser(options) {
|
|
27
27
|
return requestJson("/auth/me", { method: "GET" }, options);
|
|
28
28
|
}
|
|
29
|
+
export async function updateProfile(profile, options) {
|
|
30
|
+
return requestJson("/auth/me/profile", {
|
|
31
|
+
method: "PUT",
|
|
32
|
+
body: JSON.stringify({ profile }),
|
|
33
|
+
}, options);
|
|
34
|
+
}
|
|
29
35
|
export async function requireUser(options) {
|
|
30
36
|
const user = await currentUser(options);
|
|
31
37
|
if (!user) {
|
|
@@ -33,3 +39,23 @@ export async function requireUser(options) {
|
|
|
33
39
|
}
|
|
34
40
|
return user;
|
|
35
41
|
}
|
|
42
|
+
export async function listAuthProviders(options) {
|
|
43
|
+
const response = await requestJson("/auth/provider_list", { method: "GET" }, options);
|
|
44
|
+
return response.list ?? [];
|
|
45
|
+
}
|
|
46
|
+
export async function getOAuthLoginUrl(provider, options) {
|
|
47
|
+
const url = new URL("/auth/oauth/login_url", "https://talizen.local");
|
|
48
|
+
url.searchParams.set("provider", provider);
|
|
49
|
+
if (options?.redirectUrl) {
|
|
50
|
+
url.searchParams.set("redirect_url", options.redirectUrl);
|
|
51
|
+
}
|
|
52
|
+
const response = await requestJson(url.pathname + url.search, { method: "GET" }, options);
|
|
53
|
+
return response.url;
|
|
54
|
+
}
|
|
55
|
+
export async function loginWithOAuth(provider, options) {
|
|
56
|
+
const target = await getOAuthLoginUrl(provider, options);
|
|
57
|
+
if (typeof window === "undefined" || !window.location) {
|
|
58
|
+
throw new TalizenAuthError("OAuth login requires a browser window.");
|
|
59
|
+
}
|
|
60
|
+
window.location.href = target;
|
|
61
|
+
}
|
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 {};
|