mirror of
https://github.com/HeyPuter/puter.git
synced 2026-05-04 00:20:45 +00:00
dev: add threads module for puter.js
This commit is contained in:
@@ -25,6 +25,7 @@ import { PWispHandler } from './modules/networking/PWispHandler.js';
|
||||
import { make_http_api } from './lib/http.js';
|
||||
import Exec from './modules/Exec.js';
|
||||
import Convert from './modules/Convert.js';
|
||||
import Threads from './modules/Threads.js';
|
||||
|
||||
// TODO: This is for a safe-guard below; we should check if we can
|
||||
// generalize this behavior rather than hard-coding it.
|
||||
@@ -98,6 +99,7 @@ window.puter = (function() {
|
||||
this.registerModule('apps', Apps);
|
||||
this.registerModule('ai', AI);
|
||||
this.registerModule('kv', KV);
|
||||
this.registerModule('threads', Threads);
|
||||
this.registerModule('drivers', Drivers);
|
||||
this.registerModule('debug', Debug);
|
||||
this.registerModule('exec', Exec);
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
export default class Threads {
|
||||
setAuthToken (authToken) {
|
||||
this.authToken = authToken;
|
||||
}
|
||||
setAPIOrigin (APIOrigin) {
|
||||
this.APIOrigin = APIOrigin;
|
||||
}
|
||||
async req_ (method, route, body) {
|
||||
const resp = await fetch(
|
||||
this.APIOrigin + route, {
|
||||
method,
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.authToken}`,
|
||||
...(body ? { 'Content-Type': 'application/json' } : {}),
|
||||
},
|
||||
...(body ? { body: JSON.stringify(body) } : {}),
|
||||
}
|
||||
);
|
||||
return await resp.json();
|
||||
}
|
||||
|
||||
async create (spec, parent) {
|
||||
if ( typeof spec === 'string' ) spec = { text: spec };
|
||||
await this.req_('POST', '/threads/create', {
|
||||
...spec,
|
||||
...(parent ? { parent } : {}),
|
||||
});
|
||||
}
|
||||
|
||||
async edit (uid, spec = {}) {
|
||||
if ( typeof spec === 'string' ) spec = { text: spec };
|
||||
await this.req_('PUT', '/threads/edit/' + encodeURIComponent(uid), {
|
||||
...spec,
|
||||
});
|
||||
}
|
||||
|
||||
async delete (uid) {
|
||||
await this.req_('DELETE', '/threads/' + encodeURIComponent(uid));
|
||||
}
|
||||
|
||||
async list (uid, page, options) {
|
||||
await this.req_('POST',
|
||||
'/threads/list/' + encodeURIComponent(uid) + '/' + page,
|
||||
options ?? {},
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user