@dontcode2/backend 0.2.1 → 0.2.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/README.md +58 -2
- package/dist/cache.d.ts +36 -0
- package/dist/{chunk-HSPHQ6OU.js → chunk-6DFTM26Y.js} +2 -2
- package/dist/{chunk-CAYYXFFZ.js → chunk-LBN4R3GH.js} +150 -2
- package/dist/chunk-LBN4R3GH.js.map +1 -0
- package/dist/cli.cjs +145 -1
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +2 -2
- package/dist/client.d.ts +6 -2
- package/dist/http.d.ts +4 -0
- package/dist/index.cjs +153 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +9 -1
- package/dist/mcp/index.cjs +145 -1
- package/dist/mcp/index.cjs.map +1 -1
- package/dist/mcp/index.js +2 -2
- package/dist/node.cjs +145 -1
- package/dist/node.cjs.map +1 -1
- package/dist/node.js +2 -2
- package/dist/realtime.d.ts +29 -0
- package/dist/types.d.ts +24 -0
- package/package.json +1 -1
- package/dist/chunk-CAYYXFFZ.js.map +0 -1
- /package/dist/{chunk-HSPHQ6OU.js.map → chunk-6DFTM26Y.js.map} +0 -0
package/dist/mcp/index.js
CHANGED
package/dist/node.cjs
CHANGED
|
@@ -182,6 +182,24 @@ var Transport = class {
|
|
|
182
182
|
);
|
|
183
183
|
return this.parse(res);
|
|
184
184
|
}
|
|
185
|
+
/** PUT a JSON body and parse the JSON response. */
|
|
186
|
+
async put(path, body, opts) {
|
|
187
|
+
const res = await this.send(
|
|
188
|
+
path,
|
|
189
|
+
{
|
|
190
|
+
method: "PUT",
|
|
191
|
+
headers: { ...this.headers(opts), "Content-Type": "application/json" },
|
|
192
|
+
body: JSON.stringify(body ?? null)
|
|
193
|
+
},
|
|
194
|
+
opts
|
|
195
|
+
);
|
|
196
|
+
return this.parse(res);
|
|
197
|
+
}
|
|
198
|
+
/** DELETE and parse the JSON response. */
|
|
199
|
+
async del(path, opts) {
|
|
200
|
+
const res = await this.send(path, { method: "DELETE", headers: this.headers(opts) }, opts);
|
|
201
|
+
return this.parse(res);
|
|
202
|
+
}
|
|
185
203
|
/** PUT a multipart form (file uploads). The runtime sets the boundary. */
|
|
186
204
|
async multipart(path, form, opts) {
|
|
187
205
|
const res = await this.send(path, { method: "PUT", headers: this.headers(opts), body: form }, opts);
|
|
@@ -542,6 +560,96 @@ var AuthApi = class {
|
|
|
542
560
|
}
|
|
543
561
|
};
|
|
544
562
|
|
|
563
|
+
// src/cache.ts
|
|
564
|
+
var CACHE_PATH = "/api/v1/cache";
|
|
565
|
+
var enc = (key) => encodeURIComponent(key);
|
|
566
|
+
function asMiss(err, fallback) {
|
|
567
|
+
if (isDontCodeError(err) && err.status === 404) return fallback;
|
|
568
|
+
throw err;
|
|
569
|
+
}
|
|
570
|
+
var CacheClient = class {
|
|
571
|
+
constructor(transport) {
|
|
572
|
+
this.transport = transport;
|
|
573
|
+
}
|
|
574
|
+
/** Read a value. Returns `null` on a miss or expiry. */
|
|
575
|
+
async get(key) {
|
|
576
|
+
try {
|
|
577
|
+
const r = await this.transport.get(`${CACHE_PATH}/kv/${enc(key)}`);
|
|
578
|
+
return r.value;
|
|
579
|
+
} catch (err) {
|
|
580
|
+
return asMiss(err, null);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
/** Set a value, optionally with a TTL (seconds) or set-if-absent (`nx`).
|
|
584
|
+
* Returns `false` when `nx` is set and the key already existed. */
|
|
585
|
+
async set(key, value, options = {}) {
|
|
586
|
+
const params = new URLSearchParams();
|
|
587
|
+
if (options.ttl != null) params.set("ttl", String(options.ttl));
|
|
588
|
+
if (options.nx) params.set("nx", "true");
|
|
589
|
+
const qs = params.toString() ? `?${params}` : "";
|
|
590
|
+
const r = await this.transport.put(`${CACHE_PATH}/kv/${enc(key)}${qs}`, value);
|
|
591
|
+
return r.set;
|
|
592
|
+
}
|
|
593
|
+
/** Delete a key. Returns whether it existed. */
|
|
594
|
+
async del(key) {
|
|
595
|
+
const r = await this.transport.del(`${CACHE_PATH}/kv/${enc(key)}`);
|
|
596
|
+
return r.deleted;
|
|
597
|
+
}
|
|
598
|
+
/** Set or clear (`null`) the TTL on an existing key. Returns `false` if absent. */
|
|
599
|
+
async expire(key, ttl) {
|
|
600
|
+
const r = await this.transport.json(`${CACHE_PATH}/expire/${enc(key)}`, {
|
|
601
|
+
ttl
|
|
602
|
+
});
|
|
603
|
+
return r.applied;
|
|
604
|
+
}
|
|
605
|
+
// --- hashes -------------------------------------------------------------
|
|
606
|
+
/** Set fields on a hash. Returns the number of fields written. */
|
|
607
|
+
async hset(key, fields) {
|
|
608
|
+
const r = await this.transport.json(`${CACHE_PATH}/hset/${enc(key)}`, {
|
|
609
|
+
fields
|
|
610
|
+
});
|
|
611
|
+
return r.written;
|
|
612
|
+
}
|
|
613
|
+
/** Read a whole hash. Returns `null` on a miss. */
|
|
614
|
+
async hgetAll(key) {
|
|
615
|
+
try {
|
|
616
|
+
const r = await this.transport.get(`${CACHE_PATH}/hgetall/${enc(key)}`);
|
|
617
|
+
return r.value;
|
|
618
|
+
} catch (err) {
|
|
619
|
+
return asMiss(err, null);
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
// --- sets ---------------------------------------------------------------
|
|
623
|
+
/** Add members to a set. Returns the number newly added. */
|
|
624
|
+
async sAdd(key, ...members) {
|
|
625
|
+
const r = await this.transport.json(`${CACHE_PATH}/sadd/${enc(key)}`, {
|
|
626
|
+
members
|
|
627
|
+
});
|
|
628
|
+
return r.added;
|
|
629
|
+
}
|
|
630
|
+
/** List set members. Returns `[]` on a miss. */
|
|
631
|
+
async sMembers(key) {
|
|
632
|
+
try {
|
|
633
|
+
const r = await this.transport.get(
|
|
634
|
+
`${CACHE_PATH}/smembers/${enc(key)}`
|
|
635
|
+
);
|
|
636
|
+
return r.value ?? [];
|
|
637
|
+
} catch (err) {
|
|
638
|
+
return asMiss(err, []);
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
/** Remove members from a set. Returns the number removed. */
|
|
642
|
+
async sRem(key, ...members) {
|
|
643
|
+
const r = await this.transport.json(`${CACHE_PATH}/srem/${enc(key)}`, {
|
|
644
|
+
members
|
|
645
|
+
});
|
|
646
|
+
return r.removed;
|
|
647
|
+
}
|
|
648
|
+
};
|
|
649
|
+
function createCache(transport) {
|
|
650
|
+
return new CacheClient(transport);
|
|
651
|
+
}
|
|
652
|
+
|
|
545
653
|
// src/db.ts
|
|
546
654
|
var DB_PATH = "/api/v1/db";
|
|
547
655
|
var MIGRATE_PATH = "/api/v1/db/migrate";
|
|
@@ -609,6 +717,40 @@ function createDb(transport) {
|
|
|
609
717
|
});
|
|
610
718
|
}
|
|
611
719
|
|
|
720
|
+
// src/realtime.ts
|
|
721
|
+
var REALTIME_PATH = "/api/v1/realtime";
|
|
722
|
+
var RealtimeApi = class {
|
|
723
|
+
constructor(transport) {
|
|
724
|
+
this.transport = transport;
|
|
725
|
+
}
|
|
726
|
+
/** Mint a short-lived, channel-scoped connection token for a browser. */
|
|
727
|
+
mintToken(input) {
|
|
728
|
+
return this.transport.json(`${REALTIME_PATH}/token`, {
|
|
729
|
+
channels: input.channels,
|
|
730
|
+
identity: input.identity,
|
|
731
|
+
ttl: input.ttl
|
|
732
|
+
});
|
|
733
|
+
}
|
|
734
|
+
/** Publish a message to a channel. Returns how many subscribers it reached. */
|
|
735
|
+
async publish(channel, payload) {
|
|
736
|
+
const r = await this.transport.json(`${REALTIME_PATH}/publish`, {
|
|
737
|
+
channel,
|
|
738
|
+
payload
|
|
739
|
+
});
|
|
740
|
+
return r.delivered;
|
|
741
|
+
}
|
|
742
|
+
/** Who is currently connected to a channel. */
|
|
743
|
+
async presence(channel) {
|
|
744
|
+
const r = await this.transport.get(
|
|
745
|
+
`${REALTIME_PATH}/channels/${encodeURIComponent(channel)}/presence`
|
|
746
|
+
);
|
|
747
|
+
return r.presence ?? [];
|
|
748
|
+
}
|
|
749
|
+
};
|
|
750
|
+
function createRealtime(transport) {
|
|
751
|
+
return new RealtimeApi(transport);
|
|
752
|
+
}
|
|
753
|
+
|
|
612
754
|
// src/storage.ts
|
|
613
755
|
var STORAGE_PATH = "/api/v1/storage";
|
|
614
756
|
var DEFAULT_CONTENT_TYPE = "application/octet-stream";
|
|
@@ -702,7 +844,9 @@ function dontcode(options = {}) {
|
|
|
702
844
|
return {
|
|
703
845
|
auth: new AuthApi(transport, options.session),
|
|
704
846
|
db: createDb(transport),
|
|
705
|
-
storage: createStorage(transport)
|
|
847
|
+
storage: createStorage(transport),
|
|
848
|
+
cache: createCache(transport),
|
|
849
|
+
realtime: createRealtime(transport)
|
|
706
850
|
};
|
|
707
851
|
}
|
|
708
852
|
|