@farcaster/snap-turso 1.0.0 → 1.0.2

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/dist/index.d.ts CHANGED
@@ -1,8 +1,13 @@
1
- import { type SnapFunction } from "@farcaster/snap";
2
- /** Reserved for future options (e.g. custom table name). */
3
- export type WithTursoServerlessOptions = Record<string, never>;
4
- /**
5
- * Wraps a SnapFunction and injects a Turso serverless-backed data store into the
6
- * context.
7
- */
8
- export declare function withTursoServerless(snapFn: SnapFunction, _options?: WithTursoServerlessOptions): SnapFunction;
1
+ import { type SnapHandlerResult, type SnapContext } from "@farcaster/snap";
2
+ export type DataStoreValue = string | number | boolean | null | DataStoreValue[] | {
3
+ [key: string]: DataStoreValue;
4
+ };
5
+ export type DataStore = {
6
+ get(key: string): Promise<DataStoreValue | null>;
7
+ set(key: string, value: DataStoreValue): Promise<void>;
8
+ };
9
+ export type SnapFunctionWithDataStore = (ctx: SnapContext & {
10
+ data: DataStore;
11
+ }) => Promise<SnapHandlerResult>;
12
+ export declare function createInMemoryDataStore(): DataStore;
13
+ export declare function createTursoDataStore(): DataStore;
package/dist/index.js CHANGED
@@ -1,15 +1,22 @@
1
1
  import { connect } from "@tursodatabase/serverless";
2
2
  const TABLE_SQL = `CREATE TABLE IF NOT EXISTS snap_kv (key TEXT PRIMARY KEY, value TEXT NOT NULL)`;
3
- /**
4
- * Wraps a SnapFunction and injects a Turso serverless-backed data store into the
5
- * context.
6
- */
7
- export function withTursoServerless(snapFn, _options) {
3
+ export function createInMemoryDataStore() {
4
+ const data = new Map();
5
+ return {
6
+ async get(key) {
7
+ return data.get(key) ?? null;
8
+ },
9
+ async set(key, value) {
10
+ data.set(key, value);
11
+ },
12
+ };
13
+ }
14
+ export function createTursoDataStore() {
8
15
  const url = process.env.TURSO_DATABASE_URL;
9
16
  const authToken = process.env.TURSO_AUTH_TOKEN;
10
17
  if (!url || !authToken) {
11
- console.warn("missing env vars -- skipping Turso data store setup");
12
- return snapFn;
18
+ console.warn("missing Turso env vars -- using in-memory data store");
19
+ return createInMemoryDataStore();
13
20
  }
14
21
  const conn = connect({ url, authToken });
15
22
  let ensureTablePromise;
@@ -19,7 +26,7 @@ export function withTursoServerless(snapFn, _options) {
19
26
  }
20
27
  await ensureTablePromise;
21
28
  };
22
- const store = {
29
+ return {
23
30
  async get(key) {
24
31
  await ensureTable();
25
32
  const stmt = await conn.prepare("SELECT value FROM snap_kv WHERE key = ?");
@@ -41,5 +48,4 @@ export function withTursoServerless(snapFn, _options) {
41
48
  await stmt.run([key, JSON.stringify(value)]);
42
49
  },
43
50
  };
44
- return (ctx) => snapFn({ ...ctx, data: store });
45
51
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@farcaster/snap-turso",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Turso data store adapter for Farcaster Snaps",
5
5
  "repository": {
6
6
  "type": "git",
@@ -27,7 +27,7 @@
27
27
  "license": "MIT",
28
28
  "dependencies": {
29
29
  "@tursodatabase/serverless": "^0.2.0",
30
- "@farcaster/snap": "1.8.0"
30
+ "@farcaster/snap": "1.13.0"
31
31
  },
32
32
  "devDependencies": {
33
33
  "@types/node": "^25.5.0",
package/src/index.ts CHANGED
@@ -1,29 +1,48 @@
1
1
  import { connect } from "@tursodatabase/serverless";
2
2
  import {
3
- type DataStoreValue,
4
- type SnapDataStore,
5
3
  type SnapFunction,
4
+ type SnapHandlerResult,
5
+ type SnapContext,
6
6
  } from "@farcaster/snap";
7
7
 
8
- /** Reserved for future options (e.g. custom table name). */
9
- export type WithTursoServerlessOptions = Record<string, never>;
10
-
11
8
  const TABLE_SQL = `CREATE TABLE IF NOT EXISTS snap_kv (key TEXT PRIMARY KEY, value TEXT NOT NULL)`;
12
9
 
13
- /**
14
- * Wraps a SnapFunction and injects a Turso serverless-backed data store into the
15
- * context.
16
- */
17
- export function withTursoServerless(
18
- snapFn: SnapFunction,
19
- _options?: WithTursoServerlessOptions,
20
- ): SnapFunction {
10
+ export type DataStoreValue =
11
+ | string
12
+ | number
13
+ | boolean
14
+ | null
15
+ | DataStoreValue[]
16
+ | { [key: string]: DataStoreValue };
17
+
18
+ export type DataStore = {
19
+ get(key: string): Promise<DataStoreValue | null>;
20
+ set(key: string, value: DataStoreValue): Promise<void>;
21
+ };
22
+
23
+ export type SnapFunctionWithDataStore = (
24
+ ctx: SnapContext & { data: DataStore },
25
+ ) => Promise<SnapHandlerResult>;
26
+
27
+ export function createInMemoryDataStore(): DataStore {
28
+ const data = new Map<string, DataStoreValue>();
29
+ return {
30
+ async get(key: string): Promise<DataStoreValue | null> {
31
+ return data.get(key) ?? null;
32
+ },
33
+ async set(key: string, value: DataStoreValue): Promise<void> {
34
+ data.set(key, value);
35
+ },
36
+ };
37
+ }
38
+
39
+ export function createTursoDataStore(): DataStore {
21
40
  const url = process.env.TURSO_DATABASE_URL;
22
41
  const authToken = process.env.TURSO_AUTH_TOKEN;
23
42
 
24
43
  if (!url || !authToken) {
25
- console.warn("missing env vars -- skipping Turso data store setup");
26
- return snapFn;
44
+ console.warn("missing Turso env vars -- using in-memory data store");
45
+ return createInMemoryDataStore();
27
46
  }
28
47
 
29
48
  const conn = connect({ url, authToken });
@@ -37,7 +56,7 @@ export function withTursoServerless(
37
56
  await ensureTablePromise;
38
57
  };
39
58
 
40
- const store: SnapDataStore = {
59
+ return {
41
60
  async get(key: string): Promise<DataStoreValue | null> {
42
61
  await ensureTable();
43
62
  const stmt = await conn.prepare(
@@ -64,6 +83,4 @@ export function withTursoServerless(
64
83
  await stmt.run([key, JSON.stringify(value)]);
65
84
  },
66
85
  };
67
-
68
- return (ctx) => snapFn({ ...ctx, data: store });
69
86
  }