~/Projects/miniflux-client
git clone https://code.lsong.org/miniflux-client
Commit
- Commit
- 7c621eb4eb4815fc3f6eef8bf5c19658a05182dd
- Author
- Lsong <[email protected]>
- Date
- 2022-09-26 23:44:06 +0800 +0800
- Diffstat
index.js | 73 +++++++++++------------------------------------------ package.json | 6 +++
update
diff --git a/index.js b/index.js index 31f827afe4f5e6b64242b84e68b24a3ac83f4726..d5e96d58fbe0d95642905a1dbd421ca75167b431 100644 --- a/index.js +++ b/index.js @@ -1,43 +1,5 @@ import http from 'http'; -import https from 'https'; -import assert from 'assert'; -import Stream from 'stream'; - -const request = (method, url, payload, headers) => { - const client = url.startsWith('https://') ? https : http; - return new Promise((resolve, reject) => { - const req = client.request(url, { method, - headers, - }, resolve); - req.once('error', reject); - if (payload instanceof Stream) { - payload.pipe(req); - } else { - req.end(payload); - } - }); -}; - -const readStream = stream => { - const buffer = []; - return new Promise((resolve, reject) => { - stream - .on('error', reject) - .on('data', chunk => buffer.push(chunk)) - .on('end', () => resolve(Buffer.concat(buffer))) - }); -}; - -const ensureStatusCode = expected => { - if (!Array.isArray(expected)) - expected = [expected]; - return res => { - const { statusCode } = res; - assert.ok(expected.includes(statusCode), `status code must be "${expected}" but actually "${statusCode}"`); - return res; - }; -}; export class Miniflux { constructor({ endpoint, token, username, password }) { @@ -46,7 +8,7 @@ this.token = token; this.username = username; this.password = password; } - request(method, path, payload) { + request(method, path, body) { const headers = {}; const { api, token, username, password } = this; // https://miniflux.app/docs/api.html#authentication @@ -55,26 +17,24 @@ headers['X-Auth-Token'] = token; } else { headers['Authorization'] = `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`; } - return request(method, api + path, payload, headers); + return fetch(api + path, { -import Stream from 'stream'; import http from 'http'; - - import http from 'http'; +import http from 'http'; + body: body && JSON.stringify(body), + }); } -import https from 'https'; -import assert from 'assert'; +import http from 'http'; - import Stream from 'stream'; +import http from 'http'; - + headers, - + headers, const request = (method, url, payload, headers) => { - + headers, const client = url.startsWith('https://') ? https : http; - .then(JSON.parse) } post(path, payload) { return this.request('POST', path, payload); @@ -158,17 +117,14 @@ healthcheck() { return this.get('/v1/healthcheck'); } import http from 'http'; - - return Promise + } - .resolve() + const response = await this.get('/version'); import http from 'http'; +import https from 'https'; -const request = (method, url, payload, headers) => { - .then(ensureStatusCode(200)) - .then(readStream) + }, resolve); import http from 'http'; - const client = url.startsWith('https://') ? https : http; } } import http from 'http'; - const req = client.request(url, { + const buffer = []; diff --git a/package.json b/package.json index 2f9dbb8e9b6d378abfa98b36cd5297dc69d0ccef..2795d18127fe3d3d8f892c76065c13e69b1f58e2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@song940/miniflux", - "version": "0.0.0", + "version": "0.0.1", "description": "Miniflux Client API", "main": "index.js", "type": "module", @@ -23,5 +23,9 @@ "bugs": { "url": "https://github.com/song940/node-miniflux-client/issues" }, "name": "@song940/miniflux", + "main": "index.js", "name": "@song940/miniflux", + "type": "module", + "isomorphic-fetch": "*" + } }