@clivly/sdk 0.3.0-next.5 → 0.3.0-next.7

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.
@@ -0,0 +1,84 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region src/chat-session.ts
3
+ const DEFAULT_API_URL = "https://api.clivly.com";
4
+ const JSON_HEADERS = { "content-type": "application/json" };
5
+ const TRAILING_SLASH = /\/$/;
6
+ function jsonResponse(body, status) {
7
+ return new Response(JSON.stringify(body), {
8
+ status,
9
+ headers: JSON_HEADERS
10
+ });
11
+ }
12
+ function normalizeApiUrl(apiUrl) {
13
+ return apiUrl.replace(TRAILING_SLASH, "");
14
+ }
15
+ function resolveOrigin(req) {
16
+ const headerOrigin = req.headers.get("origin");
17
+ if (headerOrigin) return headerOrigin;
18
+ try {
19
+ return new URL(req.url).origin;
20
+ } catch {
21
+ return null;
22
+ }
23
+ }
24
+ function isChatSessionRequestBody(value) {
25
+ if (typeof value !== "object" || value === null) return false;
26
+ const body = value;
27
+ if (typeof body.widgetId !== "string" || body.widgetId.trim().length === 0) return false;
28
+ if (typeof body.visitor !== "object" || body.visitor === null || Array.isArray(body.visitor)) return false;
29
+ const visitor = body.visitor;
30
+ if (typeof visitor.name !== "string" || visitor.name.trim().length === 0) return false;
31
+ if (typeof visitor.email !== "string" || visitor.email.trim().length === 0) return false;
32
+ return body.visitorToken === void 0 || body.visitorToken === null || typeof body.visitorToken === "string";
33
+ }
34
+ function originAllowed(origin, allowedOrigins) {
35
+ if (!allowedOrigins || allowedOrigins.length === 0) return true;
36
+ if (!origin) return false;
37
+ return allowedOrigins.includes(origin);
38
+ }
39
+ function createChatSessionHandler({ allowedOrigins, apiKey, apiUrl = DEFAULT_API_URL, fetchImpl = fetch }) {
40
+ const sessionUrl = `${normalizeApiUrl(apiUrl)}/clivly/widget/session`;
41
+ return async (request) => {
42
+ if (request.method !== "POST") return new Response(JSON.stringify({ error: "method_not_allowed" }), {
43
+ status: 405,
44
+ headers: {
45
+ ...JSON_HEADERS,
46
+ allow: "POST"
47
+ }
48
+ });
49
+ const origin = resolveOrigin(request);
50
+ if (!originAllowed(origin, allowedOrigins)) return jsonResponse({ error: "origin_not_allowed" }, 403);
51
+ let body;
52
+ try {
53
+ body = await request.json();
54
+ } catch {
55
+ return jsonResponse({ error: "invalid_json" }, 400);
56
+ }
57
+ if (!isChatSessionRequestBody(body)) return jsonResponse({ error: "invalid_request" }, 400);
58
+ try {
59
+ const upstream = await fetchImpl(sessionUrl, {
60
+ method: "POST",
61
+ headers: {
62
+ authorization: `Bearer ${apiKey}`,
63
+ "content-type": "application/json",
64
+ ...origin ? { origin } : {}
65
+ },
66
+ body: JSON.stringify({
67
+ widgetId: body.widgetId,
68
+ visitor: body.visitor,
69
+ visitorToken: body.visitorToken,
70
+ origin
71
+ })
72
+ });
73
+ const responseBody = await upstream.text();
74
+ return new Response(responseBody, {
75
+ status: upstream.status,
76
+ headers: { "content-type": upstream.headers.get("content-type") ?? "application/json" }
77
+ });
78
+ } catch {
79
+ return jsonResponse({ error: "upstream_unreachable" }, 502);
80
+ }
81
+ };
82
+ }
83
+ //#endregion
84
+ exports.createChatSessionHandler = createChatSessionHandler;
@@ -0,0 +1,11 @@
1
+ import { c as CreateChatSessionHandlerOptions } from "./types-jMafEyTv.cjs";
2
+
3
+ //#region src/chat-session.d.ts
4
+ declare function createChatSessionHandler({
5
+ allowedOrigins,
6
+ apiKey,
7
+ apiUrl,
8
+ fetchImpl
9
+ }: CreateChatSessionHandlerOptions): (request: Request) => Promise<Response>;
10
+ //#endregion
11
+ export { createChatSessionHandler };
@@ -0,0 +1,11 @@
1
+ import { c as CreateChatSessionHandlerOptions } from "./types-B7wBCHRT.mjs";
2
+
3
+ //#region src/chat-session.d.ts
4
+ declare function createChatSessionHandler({
5
+ allowedOrigins,
6
+ apiKey,
7
+ apiUrl,
8
+ fetchImpl
9
+ }: CreateChatSessionHandlerOptions): (request: Request) => Promise<Response>;
10
+ //#endregion
11
+ export { createChatSessionHandler };
@@ -0,0 +1,83 @@
1
+ //#region src/chat-session.ts
2
+ const DEFAULT_API_URL = "https://api.clivly.com";
3
+ const JSON_HEADERS = { "content-type": "application/json" };
4
+ const TRAILING_SLASH = /\/$/;
5
+ function jsonResponse(body, status) {
6
+ return new Response(JSON.stringify(body), {
7
+ status,
8
+ headers: JSON_HEADERS
9
+ });
10
+ }
11
+ function normalizeApiUrl(apiUrl) {
12
+ return apiUrl.replace(TRAILING_SLASH, "");
13
+ }
14
+ function resolveOrigin(req) {
15
+ const headerOrigin = req.headers.get("origin");
16
+ if (headerOrigin) return headerOrigin;
17
+ try {
18
+ return new URL(req.url).origin;
19
+ } catch {
20
+ return null;
21
+ }
22
+ }
23
+ function isChatSessionRequestBody(value) {
24
+ if (typeof value !== "object" || value === null) return false;
25
+ const body = value;
26
+ if (typeof body.widgetId !== "string" || body.widgetId.trim().length === 0) return false;
27
+ if (typeof body.visitor !== "object" || body.visitor === null || Array.isArray(body.visitor)) return false;
28
+ const visitor = body.visitor;
29
+ if (typeof visitor.name !== "string" || visitor.name.trim().length === 0) return false;
30
+ if (typeof visitor.email !== "string" || visitor.email.trim().length === 0) return false;
31
+ return body.visitorToken === void 0 || body.visitorToken === null || typeof body.visitorToken === "string";
32
+ }
33
+ function originAllowed(origin, allowedOrigins) {
34
+ if (!allowedOrigins || allowedOrigins.length === 0) return true;
35
+ if (!origin) return false;
36
+ return allowedOrigins.includes(origin);
37
+ }
38
+ function createChatSessionHandler({ allowedOrigins, apiKey, apiUrl = DEFAULT_API_URL, fetchImpl = fetch }) {
39
+ const sessionUrl = `${normalizeApiUrl(apiUrl)}/clivly/widget/session`;
40
+ return async (request) => {
41
+ if (request.method !== "POST") return new Response(JSON.stringify({ error: "method_not_allowed" }), {
42
+ status: 405,
43
+ headers: {
44
+ ...JSON_HEADERS,
45
+ allow: "POST"
46
+ }
47
+ });
48
+ const origin = resolveOrigin(request);
49
+ if (!originAllowed(origin, allowedOrigins)) return jsonResponse({ error: "origin_not_allowed" }, 403);
50
+ let body;
51
+ try {
52
+ body = await request.json();
53
+ } catch {
54
+ return jsonResponse({ error: "invalid_json" }, 400);
55
+ }
56
+ if (!isChatSessionRequestBody(body)) return jsonResponse({ error: "invalid_request" }, 400);
57
+ try {
58
+ const upstream = await fetchImpl(sessionUrl, {
59
+ method: "POST",
60
+ headers: {
61
+ authorization: `Bearer ${apiKey}`,
62
+ "content-type": "application/json",
63
+ ...origin ? { origin } : {}
64
+ },
65
+ body: JSON.stringify({
66
+ widgetId: body.widgetId,
67
+ visitor: body.visitor,
68
+ visitorToken: body.visitorToken,
69
+ origin
70
+ })
71
+ });
72
+ const responseBody = await upstream.text();
73
+ return new Response(responseBody, {
74
+ status: upstream.status,
75
+ headers: { "content-type": upstream.headers.get("content-type") ?? "application/json" }
76
+ });
77
+ } catch {
78
+ return jsonResponse({ error: "upstream_unreachable" }, 502);
79
+ }
80
+ };
81
+ }
82
+ //#endregion
83
+ export { createChatSessionHandler };
@@ -1,4 +1,4 @@
1
- import { d as SyncSource, t as NamespaceIntrospector } from "./namespace-probe-9wTakLJu.cjs";
1
+ import { m as NamespaceIntrospector, p as SyncSource } from "./types-jMafEyTv.cjs";
2
2
  import { Table } from "drizzle-orm";
3
3
 
4
4
  //#region src/drizzle.d.ts
@@ -1,4 +1,4 @@
1
- import { d as SyncSource, t as NamespaceIntrospector } from "./namespace-probe-BD1-lqlp.mjs";
1
+ import { m as NamespaceIntrospector, p as SyncSource } from "./types-B7wBCHRT.mjs";
2
2
  import { Table } from "drizzle-orm";
3
3
 
4
4
  //#region src/drizzle.d.ts
package/dist/index.cjs CHANGED
@@ -1,5 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_drizzle_meta = require("./drizzle-meta-iAu8w1hj.cjs");
3
+ const require_chat_session = require("./chat-session.cjs");
3
4
  //#region src/custom-slug.ts
4
5
  const NON_ALNUM = /[^a-z0-9]+/g;
5
6
  const EDGE_HYPHENS = /(^-|-$)/g;
@@ -936,4 +937,5 @@ exports.ClivlyAuthError = ClivlyAuthError;
936
937
  exports.ClivlyError = ClivlyError;
937
938
  exports.ClivlyNetworkError = ClivlyNetworkError;
938
939
  exports.ClivlyRateLimitError = ClivlyRateLimitError;
940
+ exports.createChatSessionHandler = require_chat_session.createChatSessionHandler;
939
941
  exports.createClivlySDK = createClivlySDK;
package/dist/index.d.cts CHANGED
@@ -1,4 +1,5 @@
1
- import { a as ClivlySDKConfig, c as DiscoveredTable, i as ClivlySDK, l as DoctorCheck, n as ClivlyConnectReason, o as ClivlyUser, r as ClivlyConnectResult, s as CursorStore, u as DoctorReport } from "./namespace-probe-9wTakLJu.cjs";
1
+ import { a as ClivlySDK, c as CreateChatSessionHandlerOptions, d as DoctorCheck, f as DoctorReport, i as ClivlyConnectResult, l as CursorStore, n as ChatWidgetVisitor, o as ClivlySDKConfig, r as ClivlyConnectReason, s as ClivlyUser, t as ChatSessionRequestBody, u as DiscoveredTable } from "./types-jMafEyTv.cjs";
2
+ import { createChatSessionHandler } from "./chat-session.cjs";
2
3
 
3
4
  //#region src/errors.d.ts
4
5
  declare class ClivlyError extends Error {
@@ -30,4 +31,4 @@ declare class ClivlyNetworkError extends ClivlyError {}
30
31
  */
31
32
  declare function createClivlySDK(config: ClivlySDKConfig): ClivlySDK;
32
33
  //#endregion
33
- export { ClivlyAuthError, type ClivlyConnectReason, type ClivlyConnectResult, ClivlyError, ClivlyNetworkError, ClivlyRateLimitError, type ClivlySDK, type ClivlySDKConfig, type ClivlyUser, type CursorStore, type DiscoveredTable, type DoctorCheck, type DoctorReport, createClivlySDK };
34
+ export { type ChatSessionRequestBody, type ChatWidgetVisitor, ClivlyAuthError, type ClivlyConnectReason, type ClivlyConnectResult, ClivlyError, ClivlyNetworkError, ClivlyRateLimitError, type ClivlySDK, type ClivlySDKConfig, type ClivlyUser, type CreateChatSessionHandlerOptions, type CursorStore, type DiscoveredTable, type DoctorCheck, type DoctorReport, createChatSessionHandler, createClivlySDK };
package/dist/index.d.mts CHANGED
@@ -1,4 +1,5 @@
1
- import { a as ClivlySDKConfig, c as DiscoveredTable, i as ClivlySDK, l as DoctorCheck, n as ClivlyConnectReason, o as ClivlyUser, r as ClivlyConnectResult, s as CursorStore, u as DoctorReport } from "./namespace-probe-BD1-lqlp.mjs";
1
+ import { a as ClivlySDK, c as CreateChatSessionHandlerOptions, d as DoctorCheck, f as DoctorReport, i as ClivlyConnectResult, l as CursorStore, n as ChatWidgetVisitor, o as ClivlySDKConfig, r as ClivlyConnectReason, s as ClivlyUser, t as ChatSessionRequestBody, u as DiscoveredTable } from "./types-B7wBCHRT.mjs";
2
+ import { createChatSessionHandler } from "./chat-session.mjs";
2
3
 
3
4
  //#region src/errors.d.ts
4
5
  declare class ClivlyError extends Error {
@@ -30,4 +31,4 @@ declare class ClivlyNetworkError extends ClivlyError {}
30
31
  */
31
32
  declare function createClivlySDK(config: ClivlySDKConfig): ClivlySDK;
32
33
  //#endregion
33
- export { ClivlyAuthError, type ClivlyConnectReason, type ClivlyConnectResult, ClivlyError, ClivlyNetworkError, ClivlyRateLimitError, type ClivlySDK, type ClivlySDKConfig, type ClivlyUser, type CursorStore, type DiscoveredTable, type DoctorCheck, type DoctorReport, createClivlySDK };
34
+ export { type ChatSessionRequestBody, type ChatWidgetVisitor, ClivlyAuthError, type ClivlyConnectReason, type ClivlyConnectResult, ClivlyError, ClivlyNetworkError, ClivlyRateLimitError, type ClivlySDK, type ClivlySDKConfig, type ClivlyUser, type CreateChatSessionHandlerOptions, type CursorStore, type DiscoveredTable, type DoctorCheck, type DoctorReport, createChatSessionHandler, createClivlySDK };
package/dist/index.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  import { t as DRIZZLE_META } from "./drizzle-meta-DMx_9nlj.mjs";
2
+ import { createChatSessionHandler } from "./chat-session.mjs";
2
3
  //#region src/custom-slug.ts
3
4
  const NON_ALNUM = /[^a-z0-9]+/g;
4
5
  const EDGE_HYPHENS = /(^-|-$)/g;
@@ -931,4 +932,4 @@ function createClivlySDK(config) {
931
932
  };
932
933
  }
933
934
  //#endregion
934
- export { ClivlyAuthError, ClivlyError, ClivlyNetworkError, ClivlyRateLimitError, createClivlySDK };
935
+ export { ClivlyAuthError, ClivlyError, ClivlyNetworkError, ClivlyRateLimitError, createChatSessionHandler, createClivlySDK };
@@ -13,6 +13,14 @@ interface DrizzleSourceMeta {
13
13
  tableName?: string;
14
14
  }
15
15
  //#endregion
16
+ //#region src/namespace-probe.d.ts
17
+ interface NamespaceIntrospector {
18
+ /** All base table names in the reachable schema. */
19
+ listTables(): Promise<string[]>;
20
+ /** Prove a table is genuinely selectable (SELECT ... LIMIT 1). Throws if unreachable/denied. */
21
+ sampleSelect(table: string): Promise<void>;
22
+ }
23
+ //#endregion
16
24
  //#region src/types.d.ts
17
25
  interface DiscoveredColumn {
18
26
  isForeignKey?: boolean;
@@ -179,6 +187,21 @@ interface ClivlyUser {
179
187
  id: string;
180
188
  name?: string;
181
189
  }
190
+ interface ChatWidgetVisitor {
191
+ email: string;
192
+ name: string;
193
+ }
194
+ interface ChatSessionRequestBody {
195
+ visitor: ChatWidgetVisitor;
196
+ visitorToken?: string | null;
197
+ widgetId: string;
198
+ }
199
+ interface CreateChatSessionHandlerOptions {
200
+ allowedOrigins?: string[];
201
+ apiKey: string;
202
+ apiUrl?: string;
203
+ fetchImpl?: typeof fetch;
204
+ }
182
205
  interface DoctorCheck {
183
206
  /** Human-readable outcome — what passed, or why it failed. */
184
207
  detail: string;
@@ -305,12 +328,4 @@ interface ClivlySDK {
305
328
  }): Promise<void>;
306
329
  }
307
330
  //#endregion
308
- //#region src/namespace-probe.d.ts
309
- interface NamespaceIntrospector {
310
- /** All base table names in the reachable schema. */
311
- listTables(): Promise<string[]>;
312
- /** Prove a table is genuinely selectable (SELECT ... LIMIT 1). Throws if unreachable/denied. */
313
- sampleSelect(table: string): Promise<void>;
314
- }
315
- //#endregion
316
- export { ClivlySDKConfig as a, DiscoveredTable as c, SyncSource as d, ClivlySDK as i, DoctorCheck as l, ClivlyConnectReason as n, ClivlyUser as o, ClivlyConnectResult as r, CursorStore as s, NamespaceIntrospector as t, DoctorReport as u };
331
+ export { ClivlySDK as a, CreateChatSessionHandlerOptions as c, DoctorCheck as d, DoctorReport as f, ClivlyConnectResult as i, CursorStore as l, NamespaceIntrospector as m, ChatWidgetVisitor as n, ClivlySDKConfig as o, SyncSource as p, ClivlyConnectReason as r, ClivlyUser as s, ChatSessionRequestBody as t, DiscoveredTable as u };
@@ -13,6 +13,14 @@ interface DrizzleSourceMeta {
13
13
  tableName?: string;
14
14
  }
15
15
  //#endregion
16
+ //#region src/namespace-probe.d.ts
17
+ interface NamespaceIntrospector {
18
+ /** All base table names in the reachable schema. */
19
+ listTables(): Promise<string[]>;
20
+ /** Prove a table is genuinely selectable (SELECT ... LIMIT 1). Throws if unreachable/denied. */
21
+ sampleSelect(table: string): Promise<void>;
22
+ }
23
+ //#endregion
16
24
  //#region src/types.d.ts
17
25
  interface DiscoveredColumn {
18
26
  isForeignKey?: boolean;
@@ -179,6 +187,21 @@ interface ClivlyUser {
179
187
  id: string;
180
188
  name?: string;
181
189
  }
190
+ interface ChatWidgetVisitor {
191
+ email: string;
192
+ name: string;
193
+ }
194
+ interface ChatSessionRequestBody {
195
+ visitor: ChatWidgetVisitor;
196
+ visitorToken?: string | null;
197
+ widgetId: string;
198
+ }
199
+ interface CreateChatSessionHandlerOptions {
200
+ allowedOrigins?: string[];
201
+ apiKey: string;
202
+ apiUrl?: string;
203
+ fetchImpl?: typeof fetch;
204
+ }
182
205
  interface DoctorCheck {
183
206
  /** Human-readable outcome — what passed, or why it failed. */
184
207
  detail: string;
@@ -305,12 +328,4 @@ interface ClivlySDK {
305
328
  }): Promise<void>;
306
329
  }
307
330
  //#endregion
308
- //#region src/namespace-probe.d.ts
309
- interface NamespaceIntrospector {
310
- /** All base table names in the reachable schema. */
311
- listTables(): Promise<string[]>;
312
- /** Prove a table is genuinely selectable (SELECT ... LIMIT 1). Throws if unreachable/denied. */
313
- sampleSelect(table: string): Promise<void>;
314
- }
315
- //#endregion
316
- export { ClivlySDKConfig as a, DiscoveredTable as c, SyncSource as d, ClivlySDK as i, DoctorCheck as l, ClivlyConnectReason as n, ClivlyUser as o, ClivlyConnectResult as r, CursorStore as s, NamespaceIntrospector as t, DoctorReport as u };
331
+ export { ClivlySDK as a, CreateChatSessionHandlerOptions as c, DoctorCheck as d, DoctorReport as f, ClivlyConnectResult as i, CursorStore as l, NamespaceIntrospector as m, ChatWidgetVisitor as n, ClivlySDKConfig as o, SyncSource as p, ClivlyConnectReason as r, ClivlyUser as s, ChatSessionRequestBody as t, DiscoveredTable as u };
package/dist/vite.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { i as ClivlySDK } from "./namespace-probe-9wTakLJu.cjs";
1
+ import { a as ClivlySDK } from "./types-jMafEyTv.cjs";
2
2
  import { Plugin } from "vite";
3
3
 
4
4
  //#region src/vite.d.ts
package/dist/vite.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { i as ClivlySDK } from "./namespace-probe-BD1-lqlp.mjs";
1
+ import { a as ClivlySDK } from "./types-B7wBCHRT.mjs";
2
2
  import { Plugin } from "vite";
3
3
 
4
4
  //#region src/vite.d.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clivly/sdk",
3
- "version": "0.3.0-next.5",
3
+ "version": "0.3.0-next.7",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Clivly SDK — connect your app to Clivly Cloud (heartbeat, discovery, auth bridge).",
@@ -17,6 +17,16 @@
17
17
  "default": "./dist/index.cjs"
18
18
  }
19
19
  },
20
+ "./chat-session": {
21
+ "import": {
22
+ "types": "./dist/chat-session.d.mts",
23
+ "default": "./dist/chat-session.mjs"
24
+ },
25
+ "require": {
26
+ "types": "./dist/chat-session.d.cts",
27
+ "default": "./dist/chat-session.cjs"
28
+ }
29
+ },
20
30
  "./drizzle": {
21
31
  "import": {
22
32
  "types": "./dist/drizzle.d.mts",
@@ -45,7 +55,7 @@
45
55
  "access": "public"
46
56
  },
47
57
  "dependencies": {
48
- "@clivly/core": "0.3.0-next.5"
58
+ "@clivly/core": "0.3.0-next.7"
49
59
  },
50
60
  "peerDependencies": {
51
61
  "drizzle-orm": ">=0.30",