@vybe-adk/public-app-lib 0.0.0-stage → 0.0.1

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/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # @vybe-adk/public-app-lib
2
+
3
+ ## 0.0.1
4
+
5
+ ### Changes
6
+
7
+ - 3161f54 - feat: add @vybe-adk/public-app-lib with submit helpers for public apps
package/README.md CHANGED
@@ -1,3 +1,35 @@
1
- # Temporary Holding Version
1
+ # @vybe-adk/public-app-lib
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The library layer of a Vybe public app: an app anyone can open without a Vybe account. A public app has no access to
4
+ organization data. Its only way into Vybe is saving visitor submissions in its own database, which accepts inserts only.
5
+
6
+ ## Submissions
7
+
8
+ Anyone can post to the submit endpoint directly, so checks in the app's code, in the browser or on its server, are for
9
+ the visitor's convenience only. Put every rule a row must satisfy in a `NOT NULL` or `CHECK` constraint on the table.
10
+
11
+ Never pass `id`, `created_at` or `injection_flagged`: Vybe sets them.
12
+
13
+ ### `submitToTable` — client-side
14
+
15
+ `client-lib/submit`. Posts the values to `/__vybe/submit/<table>` on the app's own origin.
16
+
17
+ ```typescript
18
+ import { submitToTable } from "@vybe-adk/public-app-lib/client-lib/submit";
19
+
20
+ const { id } = await submitToTable("signups", { email: "visitor@example.com" });
21
+ ```
22
+
23
+ ### `submitToTableFromServer` — server-side
24
+
25
+ `server-lib/submit`. Same operation from a route handler. Needs `VYBE_APP_ID` and `NEXT_PUBLIC_VYBE_BASE_URL`, which
26
+ Vybe sets in the deployment and the preview.
27
+
28
+ ```typescript
29
+ import { submitToTableFromServer } from "@vybe-adk/public-app-lib/server-lib/submit";
30
+
31
+ const { id } = await submitToTableFromServer("signups", { email: "visitor@example.com" });
32
+ ```
33
+
34
+ Both throw with the server's message on a 400 (unknown column, constraint failed), 404 (unknown table, or app not
35
+ published) or 413 (submission too large).
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/client-lib/submit.ts
21
+ var submit_exports = {};
22
+ __export(submit_exports, {
23
+ submitToTable: () => submitToTable
24
+ });
25
+ module.exports = __toCommonJS(submit_exports);
26
+ async function submitToTable(table, values) {
27
+ let response;
28
+ try {
29
+ response = await fetch(`/__vybe/submit/${encodeURIComponent(table)}`, {
30
+ method: "POST",
31
+ headers: { "Content-Type": "application/json" },
32
+ body: JSON.stringify(values)
33
+ });
34
+ } catch (error) {
35
+ throw new Error("Could not send your submission. Check your connection and try again.", { cause: error });
36
+ }
37
+ const body = await response.json().catch(() => null);
38
+ if (!response.ok) {
39
+ throw new Error(
40
+ typeof body?.error === "string" ? body.error : `Your submission failed (status ${response.status}). Try again.`
41
+ );
42
+ }
43
+ if (typeof body?.id !== "string") {
44
+ throw new Error("Your submission was sent, but the response was unexpected.");
45
+ }
46
+ return { id: body.id };
47
+ }
48
+ // Annotate the CommonJS export names for ESM import in node:
49
+ 0 && (module.exports = {
50
+ submitToTable
51
+ });
52
+ //# sourceMappingURL=submit.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/client-lib/submit.ts"],"sourcesContent":["/**\n * Saves one row in a table of this app's database and returns the new row's id.\n * Column names are the keys of `values`. Never pass `id`, `created_at` or `injection_flagged`: Vybe sets them.\n */\nexport async function submitToTable(table: string, values: Record<string, unknown>): Promise<{ id: string }> {\n let response: Response;\n try {\n response = await fetch(`/__vybe/submit/${encodeURIComponent(table)}`, {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify(values),\n });\n } catch (error) {\n throw new Error(\"Could not send your submission. Check your connection and try again.\", { cause: error });\n }\n\n const body = (await response.json().catch(() => null)) as { id?: string; error?: string } | null;\n if (!response.ok) {\n throw new Error(\n typeof body?.error === \"string\" ? body.error : `Your submission failed (status ${response.status}). Try again.`,\n );\n }\n if (typeof body?.id !== \"string\") {\n throw new Error(\"Your submission was sent, but the response was unexpected.\");\n }\n return { id: body.id };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,eAAsB,cAAc,OAAe,QAA0D;AAC3G,MAAI;AACJ,MAAI;AACF,eAAW,MAAM,MAAM,kBAAkB,mBAAmB,KAAK,CAAC,IAAI;AAAA,MACpE,QAAQ;AAAA,MACR,SAAS,EAAE,gBAAgB,mBAAmB;AAAA,MAC9C,MAAM,KAAK,UAAU,MAAM;AAAA,IAC7B,CAAC;AAAA,EACH,SAAS,OAAO;AACd,UAAM,IAAI,MAAM,wEAAwE,EAAE,OAAO,MAAM,CAAC;AAAA,EAC1G;AAEA,QAAM,OAAQ,MAAM,SAAS,KAAK,EAAE,MAAM,MAAM,IAAI;AACpD,MAAI,CAAC,SAAS,IAAI;AAChB,UAAM,IAAI;AAAA,MACR,OAAO,MAAM,UAAU,WAAW,KAAK,QAAQ,kCAAkC,SAAS,MAAM;AAAA,IAClG;AAAA,EACF;AACA,MAAI,OAAO,MAAM,OAAO,UAAU;AAChC,UAAM,IAAI,MAAM,4DAA4D;AAAA,EAC9E;AACA,SAAO,EAAE,IAAI,KAAK,GAAG;AACvB;","names":[]}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Saves one row in a table of this app's database and returns the new row's id.
3
+ * Column names are the keys of `values`. Never pass `id`, `created_at` or `injection_flagged`: Vybe sets them.
4
+ */
5
+ declare function submitToTable(table: string, values: Record<string, unknown>): Promise<{
6
+ id: string;
7
+ }>;
8
+
9
+ export { submitToTable };
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Saves one row in a table of this app's database and returns the new row's id.
3
+ * Column names are the keys of `values`. Never pass `id`, `created_at` or `injection_flagged`: Vybe sets them.
4
+ */
5
+ declare function submitToTable(table: string, values: Record<string, unknown>): Promise<{
6
+ id: string;
7
+ }>;
8
+
9
+ export { submitToTable };
@@ -0,0 +1,27 @@
1
+ // src/client-lib/submit.ts
2
+ async function submitToTable(table, values) {
3
+ let response;
4
+ try {
5
+ response = await fetch(`/__vybe/submit/${encodeURIComponent(table)}`, {
6
+ method: "POST",
7
+ headers: { "Content-Type": "application/json" },
8
+ body: JSON.stringify(values)
9
+ });
10
+ } catch (error) {
11
+ throw new Error("Could not send your submission. Check your connection and try again.", { cause: error });
12
+ }
13
+ const body = await response.json().catch(() => null);
14
+ if (!response.ok) {
15
+ throw new Error(
16
+ typeof body?.error === "string" ? body.error : `Your submission failed (status ${response.status}). Try again.`
17
+ );
18
+ }
19
+ if (typeof body?.id !== "string") {
20
+ throw new Error("Your submission was sent, but the response was unexpected.");
21
+ }
22
+ return { id: body.id };
23
+ }
24
+ export {
25
+ submitToTable
26
+ };
27
+ //# sourceMappingURL=submit.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/client-lib/submit.ts"],"sourcesContent":["/**\n * Saves one row in a table of this app's database and returns the new row's id.\n * Column names are the keys of `values`. Never pass `id`, `created_at` or `injection_flagged`: Vybe sets them.\n */\nexport async function submitToTable(table: string, values: Record<string, unknown>): Promise<{ id: string }> {\n let response: Response;\n try {\n response = await fetch(`/__vybe/submit/${encodeURIComponent(table)}`, {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify(values),\n });\n } catch (error) {\n throw new Error(\"Could not send your submission. Check your connection and try again.\", { cause: error });\n }\n\n const body = (await response.json().catch(() => null)) as { id?: string; error?: string } | null;\n if (!response.ok) {\n throw new Error(\n typeof body?.error === \"string\" ? body.error : `Your submission failed (status ${response.status}). Try again.`,\n );\n }\n if (typeof body?.id !== \"string\") {\n throw new Error(\"Your submission was sent, but the response was unexpected.\");\n }\n return { id: body.id };\n}\n"],"mappings":";AAIA,eAAsB,cAAc,OAAe,QAA0D;AAC3G,MAAI;AACJ,MAAI;AACF,eAAW,MAAM,MAAM,kBAAkB,mBAAmB,KAAK,CAAC,IAAI;AAAA,MACpE,QAAQ;AAAA,MACR,SAAS,EAAE,gBAAgB,mBAAmB;AAAA,MAC9C,MAAM,KAAK,UAAU,MAAM;AAAA,IAC7B,CAAC;AAAA,EACH,SAAS,OAAO;AACd,UAAM,IAAI,MAAM,wEAAwE,EAAE,OAAO,MAAM,CAAC;AAAA,EAC1G;AAEA,QAAM,OAAQ,MAAM,SAAS,KAAK,EAAE,MAAM,MAAM,IAAI;AACpD,MAAI,CAAC,SAAS,IAAI;AAChB,UAAM,IAAI;AAAA,MACR,OAAO,MAAM,UAAU,WAAW,KAAK,QAAQ,kCAAkC,SAAS,MAAM;AAAA,IAClG;AAAA,EACF;AACA,MAAI,OAAO,MAAM,OAAO,UAAU;AAChC,UAAM,IAAI,MAAM,4DAA4D;AAAA,EAC9E;AACA,SAAO,EAAE,IAAI,KAAK,GAAG;AACvB;","names":[]}
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/server-lib/submit.ts
21
+ var submit_exports = {};
22
+ __export(submit_exports, {
23
+ submitToTableFromServer: () => submitToTableFromServer
24
+ });
25
+ module.exports = __toCommonJS(submit_exports);
26
+ var PREVIEW_SUBMIT_TOKEN_HEADER = "x-vybe-preview-submit-token";
27
+ async function submitToTableFromServer(table, values) {
28
+ const appId = process.env.VYBE_APP_ID;
29
+ const vybeDomain = process.env.VYBE_SERVER_CALLBACK_URL ?? process.env.NEXT_PUBLIC_VYBE_BASE_URL;
30
+ if (!appId || !vybeDomain) {
31
+ throw new Error("VYBE_APP_ID and NEXT_PUBLIC_VYBE_BASE_URL must be set to submit from the server");
32
+ }
33
+ const headers = { "Content-Type": "application/json" };
34
+ const previewSubmitToken = process.env.VYBE_PREVIEW_SUBMIT_TOKEN;
35
+ if (previewSubmitToken) {
36
+ headers[PREVIEW_SUBMIT_TOKEN_HEADER] = previewSubmitToken;
37
+ }
38
+ const response = await fetch(
39
+ `${vybeDomain}/api/public-apps/${encodeURIComponent(appId)}/submit/${encodeURIComponent(table)}`,
40
+ { method: "POST", headers, body: JSON.stringify(values), cache: "no-store" }
41
+ );
42
+ const body = await response.json().catch(() => null);
43
+ if (!response.ok) {
44
+ throw new Error(typeof body?.error === "string" ? body.error : `Submission failed (status ${response.status})`);
45
+ }
46
+ if (typeof body?.id !== "string") {
47
+ throw new Error("Submission was sent, but the response had no id");
48
+ }
49
+ return { id: body.id };
50
+ }
51
+ // Annotate the CommonJS export names for ESM import in node:
52
+ 0 && (module.exports = {
53
+ submitToTableFromServer
54
+ });
55
+ //# sourceMappingURL=submit.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/server-lib/submit.ts"],"sourcesContent":["const PREVIEW_SUBMIT_TOKEN_HEADER = \"x-vybe-preview-submit-token\";\n\n/**\n * Saves one row in a table of this app's database and returns the new row's id.\n * This function can only be used server side and should NEVER be used client side.\n * Column names are the keys of `values`. Never pass `id`, `created_at` or `injection_flagged`: Vybe sets them.\n */\nexport async function submitToTableFromServer(table: string, values: Record<string, unknown>): Promise<{ id: string }> {\n const appId = process.env.VYBE_APP_ID;\n const vybeDomain = process.env.VYBE_SERVER_CALLBACK_URL ?? process.env.NEXT_PUBLIC_VYBE_BASE_URL;\n if (!appId || !vybeDomain) {\n throw new Error(\"VYBE_APP_ID and NEXT_PUBLIC_VYBE_BASE_URL must be set to submit from the server\");\n }\n\n const headers: Record<string, string> = { \"Content-Type\": \"application/json\" };\n const previewSubmitToken = process.env.VYBE_PREVIEW_SUBMIT_TOKEN;\n if (previewSubmitToken) {\n headers[PREVIEW_SUBMIT_TOKEN_HEADER] = previewSubmitToken;\n }\n\n const response = await fetch(\n `${vybeDomain}/api/public-apps/${encodeURIComponent(appId)}/submit/${encodeURIComponent(table)}`,\n { method: \"POST\", headers, body: JSON.stringify(values), cache: \"no-store\" },\n );\n\n const body = (await response.json().catch(() => null)) as { id?: string; error?: string } | null;\n if (!response.ok) {\n throw new Error(typeof body?.error === \"string\" ? body.error : `Submission failed (status ${response.status})`);\n }\n if (typeof body?.id !== \"string\") {\n throw new Error(\"Submission was sent, but the response had no id\");\n }\n return { id: body.id };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAM,8BAA8B;AAOpC,eAAsB,wBAAwB,OAAe,QAA0D;AACrH,QAAM,QAAQ,QAAQ,IAAI;AAC1B,QAAM,aAAa,QAAQ,IAAI,4BAA4B,QAAQ,IAAI;AACvE,MAAI,CAAC,SAAS,CAAC,YAAY;AACzB,UAAM,IAAI,MAAM,iFAAiF;AAAA,EACnG;AAEA,QAAM,UAAkC,EAAE,gBAAgB,mBAAmB;AAC7E,QAAM,qBAAqB,QAAQ,IAAI;AACvC,MAAI,oBAAoB;AACtB,YAAQ,2BAA2B,IAAI;AAAA,EACzC;AAEA,QAAM,WAAW,MAAM;AAAA,IACrB,GAAG,UAAU,oBAAoB,mBAAmB,KAAK,CAAC,WAAW,mBAAmB,KAAK,CAAC;AAAA,IAC9F,EAAE,QAAQ,QAAQ,SAAS,MAAM,KAAK,UAAU,MAAM,GAAG,OAAO,WAAW;AAAA,EAC7E;AAEA,QAAM,OAAQ,MAAM,SAAS,KAAK,EAAE,MAAM,MAAM,IAAI;AACpD,MAAI,CAAC,SAAS,IAAI;AAChB,UAAM,IAAI,MAAM,OAAO,MAAM,UAAU,WAAW,KAAK,QAAQ,6BAA6B,SAAS,MAAM,GAAG;AAAA,EAChH;AACA,MAAI,OAAO,MAAM,OAAO,UAAU;AAChC,UAAM,IAAI,MAAM,iDAAiD;AAAA,EACnE;AACA,SAAO,EAAE,IAAI,KAAK,GAAG;AACvB;","names":[]}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Saves one row in a table of this app's database and returns the new row's id.
3
+ * This function can only be used server side and should NEVER be used client side.
4
+ * Column names are the keys of `values`. Never pass `id`, `created_at` or `injection_flagged`: Vybe sets them.
5
+ */
6
+ declare function submitToTableFromServer(table: string, values: Record<string, unknown>): Promise<{
7
+ id: string;
8
+ }>;
9
+
10
+ export { submitToTableFromServer };
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Saves one row in a table of this app's database and returns the new row's id.
3
+ * This function can only be used server side and should NEVER be used client side.
4
+ * Column names are the keys of `values`. Never pass `id`, `created_at` or `injection_flagged`: Vybe sets them.
5
+ */
6
+ declare function submitToTableFromServer(table: string, values: Record<string, unknown>): Promise<{
7
+ id: string;
8
+ }>;
9
+
10
+ export { submitToTableFromServer };
@@ -0,0 +1,30 @@
1
+ // src/server-lib/submit.ts
2
+ var PREVIEW_SUBMIT_TOKEN_HEADER = "x-vybe-preview-submit-token";
3
+ async function submitToTableFromServer(table, values) {
4
+ const appId = process.env.VYBE_APP_ID;
5
+ const vybeDomain = process.env.VYBE_SERVER_CALLBACK_URL ?? process.env.NEXT_PUBLIC_VYBE_BASE_URL;
6
+ if (!appId || !vybeDomain) {
7
+ throw new Error("VYBE_APP_ID and NEXT_PUBLIC_VYBE_BASE_URL must be set to submit from the server");
8
+ }
9
+ const headers = { "Content-Type": "application/json" };
10
+ const previewSubmitToken = process.env.VYBE_PREVIEW_SUBMIT_TOKEN;
11
+ if (previewSubmitToken) {
12
+ headers[PREVIEW_SUBMIT_TOKEN_HEADER] = previewSubmitToken;
13
+ }
14
+ const response = await fetch(
15
+ `${vybeDomain}/api/public-apps/${encodeURIComponent(appId)}/submit/${encodeURIComponent(table)}`,
16
+ { method: "POST", headers, body: JSON.stringify(values), cache: "no-store" }
17
+ );
18
+ const body = await response.json().catch(() => null);
19
+ if (!response.ok) {
20
+ throw new Error(typeof body?.error === "string" ? body.error : `Submission failed (status ${response.status})`);
21
+ }
22
+ if (typeof body?.id !== "string") {
23
+ throw new Error("Submission was sent, but the response had no id");
24
+ }
25
+ return { id: body.id };
26
+ }
27
+ export {
28
+ submitToTableFromServer
29
+ };
30
+ //# sourceMappingURL=submit.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/server-lib/submit.ts"],"sourcesContent":["const PREVIEW_SUBMIT_TOKEN_HEADER = \"x-vybe-preview-submit-token\";\n\n/**\n * Saves one row in a table of this app's database and returns the new row's id.\n * This function can only be used server side and should NEVER be used client side.\n * Column names are the keys of `values`. Never pass `id`, `created_at` or `injection_flagged`: Vybe sets them.\n */\nexport async function submitToTableFromServer(table: string, values: Record<string, unknown>): Promise<{ id: string }> {\n const appId = process.env.VYBE_APP_ID;\n const vybeDomain = process.env.VYBE_SERVER_CALLBACK_URL ?? process.env.NEXT_PUBLIC_VYBE_BASE_URL;\n if (!appId || !vybeDomain) {\n throw new Error(\"VYBE_APP_ID and NEXT_PUBLIC_VYBE_BASE_URL must be set to submit from the server\");\n }\n\n const headers: Record<string, string> = { \"Content-Type\": \"application/json\" };\n const previewSubmitToken = process.env.VYBE_PREVIEW_SUBMIT_TOKEN;\n if (previewSubmitToken) {\n headers[PREVIEW_SUBMIT_TOKEN_HEADER] = previewSubmitToken;\n }\n\n const response = await fetch(\n `${vybeDomain}/api/public-apps/${encodeURIComponent(appId)}/submit/${encodeURIComponent(table)}`,\n { method: \"POST\", headers, body: JSON.stringify(values), cache: \"no-store\" },\n );\n\n const body = (await response.json().catch(() => null)) as { id?: string; error?: string } | null;\n if (!response.ok) {\n throw new Error(typeof body?.error === \"string\" ? body.error : `Submission failed (status ${response.status})`);\n }\n if (typeof body?.id !== \"string\") {\n throw new Error(\"Submission was sent, but the response had no id\");\n }\n return { id: body.id };\n}\n"],"mappings":";AAAA,IAAM,8BAA8B;AAOpC,eAAsB,wBAAwB,OAAe,QAA0D;AACrH,QAAM,QAAQ,QAAQ,IAAI;AAC1B,QAAM,aAAa,QAAQ,IAAI,4BAA4B,QAAQ,IAAI;AACvE,MAAI,CAAC,SAAS,CAAC,YAAY;AACzB,UAAM,IAAI,MAAM,iFAAiF;AAAA,EACnG;AAEA,QAAM,UAAkC,EAAE,gBAAgB,mBAAmB;AAC7E,QAAM,qBAAqB,QAAQ,IAAI;AACvC,MAAI,oBAAoB;AACtB,YAAQ,2BAA2B,IAAI;AAAA,EACzC;AAEA,QAAM,WAAW,MAAM;AAAA,IACrB,GAAG,UAAU,oBAAoB,mBAAmB,KAAK,CAAC,WAAW,mBAAmB,KAAK,CAAC;AAAA,IAC9F,EAAE,QAAQ,QAAQ,SAAS,MAAM,KAAK,UAAU,MAAM,GAAG,OAAO,WAAW;AAAA,EAC7E;AAEA,QAAM,OAAQ,MAAM,SAAS,KAAK,EAAE,MAAM,MAAM,IAAI;AACpD,MAAI,CAAC,SAAS,IAAI;AAChB,UAAM,IAAI,MAAM,OAAO,MAAM,UAAU,WAAW,KAAK,QAAQ,6BAA6B,SAAS,MAAM,GAAG;AAAA,EAChH;AACA,MAAI,OAAO,MAAM,OAAO,UAAU;AAChC,UAAM,IAAI,MAAM,iDAAiD;AAAA,EACnE;AACA,SAAO,EAAE,IAAI,KAAK,GAAG;AACvB;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,52 @@
1
1
  {
2
2
  "name": "@vybe-adk/public-app-lib",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.1",
4
+ "license": "Apache-2.0",
5
+ "files": [
6
+ "dist",
7
+ "src",
8
+ "CHANGELOG.md",
9
+ "README.md"
10
+ ],
11
+ "scripts": {
12
+ "build": "npm run clean && tsup --tsconfig tsconfig.build.json",
13
+ "build:watch": "npm run clean && tsup --tsconfig tsconfig.build.json --watch",
14
+ "clean": "rm -rf dist *.tsbuildinfo",
15
+ "lint": "eslint \"./**/*.ts*\"",
16
+ "type-check": "tsc --build",
17
+ "prettier-check": "prettier --check \"./**/*.ts*\"",
18
+ "release": "npm publish --access public",
19
+ "local:push": "npm run build ; yalc push --scripts --update --replace"
20
+ },
21
+ "exports": {
22
+ "./package.json": "./package.json",
23
+ "./client-lib/*": {
24
+ "types": "./dist/client-lib/*.d.ts",
25
+ "import": "./dist/client-lib/*.mjs",
26
+ "require": "./dist/client-lib/*.cjs"
27
+ },
28
+ "./server-lib/*": {
29
+ "types": "./dist/server-lib/*.d.ts",
30
+ "import": "./dist/server-lib/*.mjs",
31
+ "require": "./dist/server-lib/*.cjs"
32
+ }
33
+ },
34
+ "devDependencies": {
35
+ "@types/node": "20.17.24",
36
+ "tsup": "^8",
37
+ "typescript": "5.8.3"
38
+ },
39
+ "publishConfig": {
40
+ "access": "public"
41
+ },
42
+ "homepage": "https://www.vybe.build",
43
+ "repository": {
44
+ "type": "git",
45
+ "url": "git+https://github.com/vybe-build/vybe-adk.git"
46
+ },
47
+ "keywords": [
48
+ "vybe",
49
+ "adk",
50
+ "public-app-lib"
51
+ ]
52
+ }
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Saves one row in a table of this app's database and returns the new row's id.
3
+ * Column names are the keys of `values`. Never pass `id`, `created_at` or `injection_flagged`: Vybe sets them.
4
+ */
5
+ export async function submitToTable(table: string, values: Record<string, unknown>): Promise<{ id: string }> {
6
+ let response: Response;
7
+ try {
8
+ response = await fetch(`/__vybe/submit/${encodeURIComponent(table)}`, {
9
+ method: "POST",
10
+ headers: { "Content-Type": "application/json" },
11
+ body: JSON.stringify(values),
12
+ });
13
+ } catch (error) {
14
+ throw new Error("Could not send your submission. Check your connection and try again.", { cause: error });
15
+ }
16
+
17
+ const body = (await response.json().catch(() => null)) as { id?: string; error?: string } | null;
18
+ if (!response.ok) {
19
+ throw new Error(
20
+ typeof body?.error === "string" ? body.error : `Your submission failed (status ${response.status}). Try again.`,
21
+ );
22
+ }
23
+ if (typeof body?.id !== "string") {
24
+ throw new Error("Your submission was sent, but the response was unexpected.");
25
+ }
26
+ return { id: body.id };
27
+ }
@@ -0,0 +1,34 @@
1
+ const PREVIEW_SUBMIT_TOKEN_HEADER = "x-vybe-preview-submit-token";
2
+
3
+ /**
4
+ * Saves one row in a table of this app's database and returns the new row's id.
5
+ * This function can only be used server side and should NEVER be used client side.
6
+ * Column names are the keys of `values`. Never pass `id`, `created_at` or `injection_flagged`: Vybe sets them.
7
+ */
8
+ export async function submitToTableFromServer(table: string, values: Record<string, unknown>): Promise<{ id: string }> {
9
+ const appId = process.env.VYBE_APP_ID;
10
+ const vybeDomain = process.env.VYBE_SERVER_CALLBACK_URL ?? process.env.NEXT_PUBLIC_VYBE_BASE_URL;
11
+ if (!appId || !vybeDomain) {
12
+ throw new Error("VYBE_APP_ID and NEXT_PUBLIC_VYBE_BASE_URL must be set to submit from the server");
13
+ }
14
+
15
+ const headers: Record<string, string> = { "Content-Type": "application/json" };
16
+ const previewSubmitToken = process.env.VYBE_PREVIEW_SUBMIT_TOKEN;
17
+ if (previewSubmitToken) {
18
+ headers[PREVIEW_SUBMIT_TOKEN_HEADER] = previewSubmitToken;
19
+ }
20
+
21
+ const response = await fetch(
22
+ `${vybeDomain}/api/public-apps/${encodeURIComponent(appId)}/submit/${encodeURIComponent(table)}`,
23
+ { method: "POST", headers, body: JSON.stringify(values), cache: "no-store" },
24
+ );
25
+
26
+ const body = (await response.json().catch(() => null)) as { id?: string; error?: string } | null;
27
+ if (!response.ok) {
28
+ throw new Error(typeof body?.error === "string" ? body.error : `Submission failed (status ${response.status})`);
29
+ }
30
+ if (typeof body?.id !== "string") {
31
+ throw new Error("Submission was sent, but the response had no id");
32
+ }
33
+ return { id: body.id };
34
+ }