|
| 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