@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
@@ -0,0 +1,138 @@
1
+ import { panic } from "../assertions.js";
2
+ import { sessionConfig } from "./config.js";
3
+ import { permissionsSchema } from "./permissions.js";
4
+ import { z } from "zod";
5
+
6
+ //#region src/auth/platform.ts
7
+ function makePlatform(applicationId) {
8
+ return {
9
+ session: async (token) => {
10
+ const result = await request(`/v1/vibe/applications/${applicationId}/users/session`, {
11
+ method: "GET",
12
+ token
13
+ }, z.object({
14
+ user: z.object({
15
+ id: z.string(),
16
+ email: z.string().nullish(),
17
+ fullName: z.string().nullish(),
18
+ userGroupIds: z.array(z.string()).nullish()
19
+ }).nullish(),
20
+ permissions: permissionsSchema
21
+ }).transform(({ user, permissions }) => ({
22
+ user: user ? {
23
+ id: user.id,
24
+ email: user.email ?? "",
25
+ name: user.fullName ?? "",
26
+ userGroupIds: user.userGroupIds ?? []
27
+ } : null,
28
+ permissions
29
+ })));
30
+ if (result.status === 404) panic(`Application ${applicationId} not found. Check SOFTR_APPLICATION_ID.`);
31
+ return result;
32
+ },
33
+ tokens: (credentials) => request(`/v1/vibe/applications/${applicationId}/users/tokens`, {
34
+ method: "POST",
35
+ json: credentials
36
+ }, z.object({
37
+ jwt: z.string(),
38
+ user: z.object({
39
+ id: z.string(),
40
+ email: z.string().nullish(),
41
+ fullName: z.string().nullish(),
42
+ userGroupIds: z.array(z.string()).nullish()
43
+ }).nullish(),
44
+ permissions: permissionsSchema
45
+ }).transform(({ jwt, user, permissions }) => ({
46
+ token: jwt,
47
+ session: {
48
+ user: user ? {
49
+ id: user.id,
50
+ email: user.email ?? "",
51
+ name: user.fullName ?? "",
52
+ userGroupIds: user.userGroupIds ?? []
53
+ } : null,
54
+ permissions
55
+ }
56
+ }))),
57
+ signIn: (credentials) => request(`/v1/applications/${applicationId}/users/sign-in`, {
58
+ method: "POST",
59
+ json: credentials
60
+ }, z.object({
61
+ jwt: z.string().nullish(),
62
+ authenticatorAppAction: z.string().nullish()
63
+ }).nullable()),
64
+ signUp: (body) => request(`/v1/applications/${applicationId}/users/sign-up`, {
65
+ method: "POST",
66
+ json: body
67
+ }, z.object({ jwt: z.string().nullish() }).nullable()),
68
+ new: (body) => request(`/v1/applications/${applicationId}/users/new`, {
69
+ method: "POST",
70
+ json: body
71
+ }, z.object({ jwt: z.string().nullish() }).nullable()),
72
+ signOut: (token) => request(`/v1/applications/${applicationId}/users/sign-out`, {
73
+ method: "POST",
74
+ token
75
+ }, z.null())
76
+ };
77
+ }
78
+ const errorSchema = z.object({ message: z.string() });
79
+ async function request(path, init, schema, parse = (response$1) => response$1.json()) {
80
+ const response = await fetch(`${await platformApiUrl()}${path}`, {
81
+ method: init.method,
82
+ headers: {
83
+ accept: "application/json",
84
+ ...init.json === void 0 ? {} : { "content-type": "application/json" },
85
+ ...init.token ? { authorization: `Bearer ${init.token}` } : {}
86
+ },
87
+ ...init.json === void 0 ? {} : { body: JSON.stringify(init.json) }
88
+ });
89
+ if (!response.ok) {
90
+ const error = errorSchema.safeParse(await response.json().catch(() => null));
91
+ return {
92
+ ok: false,
93
+ status: response.status,
94
+ message: error.success && error.data.message ? error.data.message : response.statusText
95
+ };
96
+ }
97
+ const parsed = schema.safeParse(await parse(response).catch(() => null));
98
+ if (!parsed.success) throw new Error(`Unexpected ${path.split("/").pop()} response: ` + z.prettifyError(parsed.error));
99
+ return {
100
+ ok: true,
101
+ status: response.status,
102
+ data: parsed.data
103
+ };
104
+ }
105
+ let cached;
106
+ async function platformApiUrl() {
107
+ cached ??= resolvePlatformApiUrl().catch((error) => {
108
+ cached = void 0;
109
+ throw error;
110
+ });
111
+ return cached;
112
+ }
113
+ const contextSchema = z.object({ platformApiUrl: z.string() });
114
+ async function resolvePlatformApiUrl() {
115
+ const pinned = process.env.SOFTR_PLATFORM_API_URL;
116
+ if (pinned) return withoutTrailingSlash(pinned);
117
+ const studioApiUrl = process.env.SOFTR_STUDIO_API_URL;
118
+ if (!studioApiUrl || process.env.NODE_ENV === "production") panic("SOFTR_PLATFORM_API_URL is not set.");
119
+ const { applicationId } = sessionConfig();
120
+ const url = `${withoutTrailingSlash(studioApiUrl)}/v1/api/applications/${applicationId}/context/public`;
121
+ let response;
122
+ try {
123
+ response = await fetch(url, { headers: { accept: "application/json" } });
124
+ } catch {
125
+ panic(`Could not reach ${url}`);
126
+ }
127
+ if (response.status === 404) panic(`Unknown application ${applicationId}`);
128
+ if (!response.ok) panic(`${url} answered ${response.status}`);
129
+ const body = contextSchema.safeParse(await response.json().catch(() => null));
130
+ if (!body.success) panic(`No platformApiUrl at ${url}`);
131
+ return withoutTrailingSlash(body.data.platformApiUrl);
132
+ }
133
+ function withoutTrailingSlash(url) {
134
+ return url.replace(/\/+$/, "");
135
+ }
136
+
137
+ //#endregion
138
+ export { makePlatform };
@@ -0,0 +1,17 @@
1
+ import { signIn, signOut } from "./methods/password.js";
2
+ import { confirmSignUp, signUp } from "./methods/signup.js";
3
+ import "@tanstack/react-start/server-only";
4
+
5
+ //#region src/auth/server.d.ts
6
+
7
+ /**
8
+ * Server-only auth methods, should be called from the app's own `createServerFn` handlers.
9
+ */
10
+ declare const auth: {
11
+ signIn: typeof signIn;
12
+ signOut: typeof signOut;
13
+ signUp: typeof signUp;
14
+ confirmSignUp: typeof confirmSignUp;
15
+ };
16
+ //#endregion
17
+ export { auth };
@@ -0,0 +1,17 @@
1
+ import { signIn, signOut } from "./methods/password.js";
2
+ import { confirmSignUp, signUp } from "./methods/signup.js";
3
+ import "@tanstack/react-start/server-only";
4
+
5
+ //#region src/auth/server.ts
6
+ /**
7
+ * Server-only auth methods, should be called from the app's own `createServerFn` handlers.
8
+ */
9
+ const auth = {
10
+ signIn,
11
+ signOut,
12
+ signUp,
13
+ confirmSignUp
14
+ };
15
+
16
+ //#endregion
17
+ export { auth };
@@ -0,0 +1,167 @@
1
+ import { readRequestCookies, serializeCookie, writeCookies } from "../cookies.js";
2
+ import { sessionConfig } from "./config.js";
3
+ import { permissionsSchema } from "./permissions.js";
4
+ import { getRequestProtocol } from "@tanstack/react-start/server";
5
+ import { z } from "zod";
6
+ import { createCipheriv, createDecipheriv, createHash, randomBytes } from "node:crypto";
7
+
8
+ //#region src/auth/session.ts
9
+ const SESSION_TOKEN = "session_token";
10
+ const SESSION_DATA = "session_data";
11
+ const SESSION_DATA_TTL_SECONDS = 300;
12
+ /**
13
+ * A browser refuses a `__Host-` cookie unless it is `Secure`, `Path=/` and free
14
+ * of `Domain` — that last clause is the one worth having, since it stops a
15
+ * sibling host planting a cookie of this name for us to honour.
16
+ */
17
+ const HOST_PREFIX = "__Host-";
18
+ function requestIsSecure() {
19
+ return getRequestProtocol() === "https";
20
+ }
21
+ function sessionCookie(base) {
22
+ const secure = requestIsSecure();
23
+ return {
24
+ name: secure ? HOST_PREFIX + base : base,
25
+ secure
26
+ };
27
+ }
28
+ /** The platform token this request carries, or `null`. */
29
+ function readSessionToken() {
30
+ const { name } = sessionCookie(SESSION_TOKEN);
31
+ return openEnvelope(readRequestCookies()[name], SESSION_TOKEN);
32
+ }
33
+ /** The cached session for `sessionToken`, or `null` on any kind of miss. */
34
+ function readSessionData(sessionToken) {
35
+ const { name } = sessionCookie(SESSION_DATA);
36
+ const payload = parseSessionData(openEnvelope(readRequestCookies()[name], SESSION_DATA));
37
+ if (payload === null) return null;
38
+ if (payload.exp <= Math.floor(Date.now() / 1e3)) return null;
39
+ if (payload.tb !== tokenBinding(sessionToken)) return null;
40
+ return {
41
+ user: payload.user,
42
+ permissions: payload.permissions
43
+ };
44
+ }
45
+ function makeSessionTokenCookie(token) {
46
+ const { name, secure } = sessionCookie(SESSION_TOKEN);
47
+ return serializeCookie(name, sealEnvelope(token, SESSION_TOKEN), {
48
+ maxAge: sessionConfig().sessionMaxAge,
49
+ secure
50
+ });
51
+ }
52
+ function makeSessionDataCookie(session, sessionToken) {
53
+ const { name, secure } = sessionCookie(SESSION_DATA);
54
+ const payload = {
55
+ user: session.user,
56
+ permissions: session.permissions,
57
+ exp: Math.floor(Date.now() / 1e3) + SESSION_DATA_TTL_SECONDS,
58
+ tb: tokenBinding(sessionToken)
59
+ };
60
+ return serializeCookie(name, sealEnvelope(JSON.stringify(payload), SESSION_DATA), {
61
+ maxAge: SESSION_DATA_TTL_SECONDS,
62
+ secure
63
+ });
64
+ }
65
+ /**
66
+ * Writes rather than returns: a forgotten list would leave the session live.
67
+ */
68
+ function clearSession() {
69
+ clearCookies([SESSION_TOKEN, SESSION_DATA]);
70
+ }
71
+ const sessionDataSchema = z.object({
72
+ user: z.object({
73
+ id: z.string(),
74
+ email: z.string(),
75
+ name: z.string(),
76
+ userGroupIds: z.array(z.string())
77
+ }).nullable(),
78
+ permissions: permissionsSchema,
79
+ exp: z.number(),
80
+ tb: z.string()
81
+ });
82
+ /**
83
+ * Both spellings on https: a jar can hold both pairs across a move between
84
+ * schemes, and the one left behind would revive if the scheme changed back.
85
+ */
86
+ function clearCookies(bases) {
87
+ const secure = requestIsSecure();
88
+ writeCookies(bases.flatMap((base) => secure ? [HOST_PREFIX + base, base] : [base]).map((name) => serializeCookie(name, "", {
89
+ maxAge: 0,
90
+ secure
91
+ })));
92
+ }
93
+ /**
94
+ * Over the plaintext token, never the cookie value: that is resealed with a
95
+ * fresh iv on every write, so a binding over its bytes would be a permanent
96
+ * cache miss.
97
+ */
98
+ function tokenBinding(sessionToken) {
99
+ if (sessionToken === null) return "anonymous";
100
+ return createHash("sha256").update(sessionToken).digest("base64url");
101
+ }
102
+ function parseSessionData(json) {
103
+ if (json === null) return null;
104
+ let payload;
105
+ try {
106
+ payload = JSON.parse(json);
107
+ } catch {
108
+ return null;
109
+ }
110
+ const data = sessionDataSchema.safeParse(payload);
111
+ return data.success ? data.data : null;
112
+ }
113
+ const IV_BYTES = 12;
114
+ const TAG_BYTES = 16;
115
+ /** Iv + one byte of ciphertext + tag. Anything shorter is not ours. */
116
+ const MIN_ENVELOPE_BYTES = IV_BYTES + TAG_BYTES + 1;
117
+ /**
118
+ * `Buffer.from(…, "base64url")` skips stray characters rather than throwing.
119
+ */
120
+ const BASE64URL = /^[A-Za-z0-9_-]+$/;
121
+ /**
122
+ * One key for both cookies. The application id goes in with the secret so a
123
+ * cookie from staging is inert in production even when the two share a secret.
124
+ */
125
+ function sessionKey() {
126
+ const { applicationId, sessionSecret } = sessionConfig();
127
+ return createHash("sha256").update(Buffer.from(sessionSecret, "hex")).update(applicationId).digest();
128
+ }
129
+ /**
130
+ * `base64url(iv ‖ ct ‖ tag)`, sealed with AES-256-GCM — an AEAD, so opening
131
+ * throws on a single flipped bit and needs no separate signature. The iv must
132
+ * be fresh per seal: nonce reuse under one key leaks the payloads and the
133
+ * authentication subkey.
134
+ *
135
+ * The purpose is bound in as additional authenticated data rather than by
136
+ * giving each cookie a key of its own: one key, and a `session_data` value
137
+ * moved into the token slot then fails to open at all instead of opening into
138
+ * a later field check.
139
+ */
140
+ function sealEnvelope(payload, purpose) {
141
+ const iv = randomBytes(IV_BYTES);
142
+ const cipher = createCipheriv("aes-256-gcm", sessionKey(), iv);
143
+ cipher.setAAD(Buffer.from(purpose));
144
+ const sealed = Buffer.concat([cipher.update(payload, "utf8"), cipher.final()]);
145
+ return Buffer.concat([
146
+ iv,
147
+ sealed,
148
+ cipher.getAuthTag()
149
+ ]).toString("base64url");
150
+ }
151
+ /** `null` for anything that is not one of ours, with no reason attached. */
152
+ function openEnvelope(raw, purpose) {
153
+ if (!raw || !BASE64URL.test(raw)) return null;
154
+ try {
155
+ const bytes = Buffer.from(raw, "base64url");
156
+ if (bytes.length < MIN_ENVELOPE_BYTES) return null;
157
+ const decipher = createDecipheriv("aes-256-gcm", sessionKey(), bytes.subarray(0, IV_BYTES));
158
+ decipher.setAAD(Buffer.from(purpose));
159
+ decipher.setAuthTag(bytes.subarray(bytes.length - TAG_BYTES));
160
+ return Buffer.concat([decipher.update(bytes.subarray(IV_BYTES, bytes.length - TAG_BYTES)), decipher.final()]).toString("utf8");
161
+ } catch {
162
+ return null;
163
+ }
164
+ }
165
+
166
+ //#endregion
167
+ export { clearSession, makeSessionDataCookie, makeSessionTokenCookie, readSessionData, readSessionToken };
@@ -0,0 +1,28 @@
1
+ //#region src/auth/user.d.ts
2
+ /**
3
+ * Declare this interface in your app to narrow every group id the SDK takes and
4
+ * returns. Without a declaration, a group id is a plain `string`.
5
+ *
6
+ * @example
7
+ * declare module "@softr/sdk" {
8
+ * interface SoftrUserGroups {
9
+ * ids:
10
+ * | "b1f2c3d4-5e6f-4a7b-8c9d-0e1f2a3b4c5d"
11
+ * | "f9e8d7c6-5b4a-4938-8271-605f4e3d2c1b"
12
+ * }
13
+ * }
14
+ */
15
+ interface SoftrUserGroups {}
16
+ /** A Softr user group id, narrowed to your own ids if you declare {@link SoftrUserGroups}. */
17
+ type SoftrUserGroupId = SoftrUserGroups extends {
18
+ ids: infer Id extends string;
19
+ } ? Id : string;
20
+ /** The signed-in visitor. */
21
+ type User = {
22
+ id: string;
23
+ email: string;
24
+ name: string;
25
+ userGroupIds: Array<SoftrUserGroupId>;
26
+ };
27
+ //#endregion
28
+ export { SoftrUserGroupId, SoftrUserGroups, User };
@@ -0,0 +1,4 @@
1
+ //#region src/client/timezone.d.ts
2
+ declare function useUserTimezone(): void;
3
+ //#endregion
4
+ export { useUserTimezone };
@@ -0,0 +1,11 @@
1
+ import { useEffect } from "react";
2
+
3
+ //#region src/client/timezone.ts
4
+ function useUserTimezone() {
5
+ useEffect(() => {
6
+ document.cookie = `user-timezone=${Intl.DateTimeFormat().resolvedOptions().timeZone}; path=/`;
7
+ }, []);
8
+ }
9
+
10
+ //#endregion
11
+ export { useUserTimezone };
package/dist/client.d.ts CHANGED
@@ -1,6 +1,3 @@
1
+ import { useUserTimezone } from "./client/timezone.js";
1
2
  import "@tanstack/react-start/client-only";
2
-
3
- //#region src/client/timezone.d.ts
4
- declare function useUserTimezone(): void;
5
- //#endregion
6
3
  export { useUserTimezone };
package/dist/client.js CHANGED
@@ -1,12 +1,4 @@
1
+ import { useUserTimezone } from "./client/timezone.js";
1
2
  import "@tanstack/react-start/client-only";
2
- import { useEffect } from "react";
3
3
 
4
- //#region src/client/timezone.ts
5
- function useUserTimezone() {
6
- useEffect(() => {
7
- document.cookie = `user-timezone=${Intl.DateTimeFormat().resolvedOptions().timeZone}; path=/`;
8
- }, []);
9
- }
10
-
11
- //#endregion
12
4
  export { useUserTimezone };
@@ -0,0 +1,47 @@
1
+ import { getRequestHeader, getResponseHeaders, setResponseHeader } from "@tanstack/react-start/server";
2
+
3
+ //#region src/cookies.ts
4
+ function parseCookieHeader(header) {
5
+ const cookies = {};
6
+ if (!header) return cookies;
7
+ for (const pair of header.split(";")) {
8
+ const separator = pair.indexOf("=");
9
+ if (separator === -1) continue;
10
+ const name = pair.slice(0, separator).trim();
11
+ if (name.length === 0) continue;
12
+ cookies[name] = decode(pair.slice(separator + 1).trim());
13
+ }
14
+ return cookies;
15
+ }
16
+ function decode(value) {
17
+ try {
18
+ return decodeURIComponent(value);
19
+ } catch {
20
+ return value;
21
+ }
22
+ }
23
+ function readRequestCookies() {
24
+ return parseCookieHeader(getRequestHeader("cookie"));
25
+ }
26
+ function serializeCookie(name, value, { maxAge, secure }) {
27
+ if (name.startsWith("__Host-") && !secure) throw new Error(`Refusing to write ${name} without Secure: a __Host- cookie is only valid when Secure, Path=/ and free of Domain, and a browser silently discards one that is not.`);
28
+ const parts = [
29
+ `${name}=${encodeURIComponent(value)}`,
30
+ "Path=/",
31
+ "HttpOnly"
32
+ ];
33
+ if (secure) parts.push("Secure");
34
+ parts.push("SameSite=Lax", `Max-Age=${maxAge}`);
35
+ return parts.join("; ");
36
+ }
37
+ const USER_TIMEZONE_COOKIE = "user-timezone";
38
+ function readUserTimezone() {
39
+ return readRequestCookies()[USER_TIMEZONE_COOKIE] ?? null;
40
+ }
41
+ function writeCookies(cookies) {
42
+ if (cookies.length === 0) return;
43
+ setResponseHeader("Set-Cookie", [...getResponseHeaders().getSetCookie(), ...cookies]);
44
+ }
45
+
46
+ //#endregion
47
+ export { readRequestCookies, readUserTimezone, serializeCookie, writeCookies };
@@ -0,0 +1,21 @@
1
+ import { ChartDataOptions, ChartDataResult, CreateOptions, GetOptions, ListOptions, ListResult, MetricOptions, MetricResult, SoftrRecord, UpdateOptions } from "../types.js";
2
+
3
+ //#region src/core/datasource.d.ts
4
+ declare function listRecords(datasourceId: string, options?: ListOptions): Promise<ListResult>;
5
+ declare function getRecord(datasourceId: string, options: GetOptions): Promise<SoftrRecord | null>;
6
+ declare function createRecord(datasourceId: string, options: CreateOptions): Promise<SoftrRecord>;
7
+ declare function updateRecord(datasourceId: string, options: UpdateOptions): Promise<SoftrRecord>;
8
+ declare function deleteRecord(datasourceId: string, id: string): Promise<void>;
9
+ declare function getMetric(datasourceId: string, options: MetricOptions): Promise<MetricResult>;
10
+ declare function getChartData(datasourceId: string, options: ChartDataOptions): Promise<ChartDataResult>;
11
+ declare const ds: {
12
+ list: typeof listRecords;
13
+ get: typeof getRecord;
14
+ create: typeof createRecord;
15
+ update: typeof updateRecord;
16
+ delete: typeof deleteRecord;
17
+ metric: typeof getMetric;
18
+ chartData: typeof getChartData;
19
+ };
20
+ //#endregion
21
+ export { ds };