Skip to content

Commit 1f05df6

Browse files
committed
dev: add threads module for puter.js
1 parent 01ba616 commit 1f05df6

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/puter-js/src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { PWispHandler } from './modules/networking/PWispHandler.js';
2525
import { make_http_api } from './lib/http.js';
2626
import Exec from './modules/Exec.js';
2727
import Convert from './modules/Convert.js';
28+
import Threads from './modules/Threads.js';
2829

2930
// TODO: This is for a safe-guard below; we should check if we can
3031
// generalize this behavior rather than hard-coding it.
@@ -98,6 +99,7 @@ window.puter = (function() {
9899
this.registerModule('apps', Apps);
99100
this.registerModule('ai', AI);
100101
this.registerModule('kv', KV);
102+
this.registerModule('threads', Threads);
101103
this.registerModule('drivers', Drivers);
102104
this.registerModule('debug', Debug);
103105
this.registerModule('exec', Exec);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
export default class Threads {
2+
setAuthToken (authToken) {
3+
this.authToken = authToken;
4+
}
5+
setAPIOrigin (APIOrigin) {
6+
this.APIOrigin = APIOrigin;
7+
}
8+
async req_ (method, route, body) {
9+
const resp = await fetch(
10+
this.APIOrigin + route, {
11+
method,
12+
headers: {
13+
Authorization: `Bearer ${this.authToken}`,
14+
...(body ? { 'Content-Type': 'application/json' } : {}),
15+
},
16+
...(body ? { body: JSON.stringify(body) } : {}),
17+
}
18+
);
19+
return await resp.json();
20+
}
21+
22+
async create (spec, parent) {
23+
if ( typeof spec === 'string' ) spec = { text: spec };
24+
await this.req_('POST', '/threads/create', {
25+
...spec,
26+
...(parent ? { parent } : {}),
27+
});
28+
}
29+
30+
async edit (uid, spec = {}) {
31+
if ( typeof spec === 'string' ) spec = { text: spec };
32+
await this.req_('PUT', '/threads/edit/' + encodeURIComponent(uid), {
33+
...spec,
34+
});
35+
}
36+
37+
async delete (uid) {
38+
await this.req_('DELETE', '/threads/' + encodeURIComponent(uid));
39+
}
40+
41+
async list (uid, page, options) {
42+
await this.req_('POST',
43+
'/threads/list/' + encodeURIComponent(uid) + '/' + page,
44+
options ?? {},
45+
);
46+
}
47+
}

0 commit comments

Comments
 (0)