mirror of
https://github.com/bcomnes/deploy-to-neocities.git
synced 2026-01-24 01:41:14 +00:00
Switch back over to CJS. sa la vie
This commit is contained in:
164
lib/client.js
164
lib/client.js
@@ -1,134 +1,124 @@
|
||||
import assert from 'nanoassert';
|
||||
import fetch from 'node-fetch';
|
||||
import { URL } from 'url';
|
||||
import qs from 'qs';
|
||||
import pkg from '../package.json';
|
||||
import os from 'os';
|
||||
const assert = require('nanoassert')
|
||||
const fetch = require('node-fetch')
|
||||
const { URL } = require('url')
|
||||
const qs = require('qs')
|
||||
const os = require('os')
|
||||
const { createReadStream } = require('fs')
|
||||
const FormData = require('form-data')
|
||||
const { handleResponse } = require('fetch-errors')
|
||||
const pkg = require('../package.json')
|
||||
|
||||
const defaultURL = 'https://neocities.org/api';
|
||||
const defaultURL = 'https://neocities.org'
|
||||
|
||||
export class NeocitiesAPIClient {
|
||||
class NeocitiesAPIClient {
|
||||
static getKey (sitename, password, opts) {
|
||||
assert(sitename, 'must pass sitename as first arg');
|
||||
assert(typeof sitename === 'string', 'user arg must be a string');
|
||||
assert(password, 'must pass a password as the second arg');
|
||||
assert(typeof password, 'password arg must be a string');
|
||||
assert(sitename, 'must pass sitename as first arg')
|
||||
assert(typeof sitename === 'string', 'user arg must be a string')
|
||||
assert(password, 'must pass a password as the second arg')
|
||||
assert(typeof password, 'password arg must be a string')
|
||||
|
||||
opts = Object.assign({
|
||||
url: defaultURL
|
||||
}, opts);
|
||||
}, opts)
|
||||
|
||||
return fetch();
|
||||
return fetch()
|
||||
}
|
||||
|
||||
constructor (apiKey, opts) {
|
||||
assert(apiKey, 'must pass apiKey as first argument');
|
||||
assert(typeof apiKey === 'string', 'apiKey must be a string');
|
||||
assert(apiKey, 'must pass apiKey as first argument')
|
||||
assert(typeof apiKey === 'string', 'apiKey must be a string')
|
||||
opts = Object.assign({
|
||||
url: defaultURL
|
||||
});
|
||||
})
|
||||
|
||||
this.opts = opts;
|
||||
this.url = opts.url;
|
||||
this.apiKey = apiKey;
|
||||
this.opts = opts
|
||||
this.url = opts.url
|
||||
this.apiKey = apiKey
|
||||
}
|
||||
|
||||
request (endpoint, args, opts) {
|
||||
assert(endpoint, 'must pass endpoint as first argument');
|
||||
opts = Object.assign({}, opts);
|
||||
opts.headers = Object.assign({
|
||||
get defaultHeaders () {
|
||||
return {
|
||||
Authorization: `Bearer ${this.apiKey}`,
|
||||
Accept: 'application/json',
|
||||
'User-Agent': `deploy-to-neocities/${pkg.version} (${os.type()})`
|
||||
}, opts.headers);
|
||||
|
||||
let path = `/api/${endpoint}`;
|
||||
if (args) path += `?${qs.stringify(args)}`;
|
||||
|
||||
const url = new URL(path, this.url);
|
||||
return fetch(url, opts);
|
||||
}
|
||||
}
|
||||
|
||||
get (endpoint, args, opts) {
|
||||
get (endpoint, quieries, opts) {
|
||||
assert(endpoint, 'must pass endpoint as first argument')
|
||||
opts = Object.assign({
|
||||
method: 'GET'
|
||||
}, opts);
|
||||
return this.request(endpoint, args, opts);
|
||||
}, opts)
|
||||
opts.headers = Object.assign({}, this.defaultHeaders, opts.headers)
|
||||
|
||||
let path = `/api/${endpoint}`
|
||||
if (quieries) path += `?${qs.stringify(quieries)}`
|
||||
|
||||
const url = new URL(path, this.url)
|
||||
return fetch(url, opts)
|
||||
}
|
||||
|
||||
post (endpoint, args, opts) {
|
||||
post (endpoint, formEntries, opts) {
|
||||
assert(endpoint, 'must pass endpoint as first argument')
|
||||
const form = new FormData()
|
||||
opts = Object.assign({
|
||||
method: 'POST'
|
||||
}, opts);
|
||||
return this.request(endpoint, args, opts);
|
||||
method: 'POST',
|
||||
body: form
|
||||
}, opts)
|
||||
|
||||
for (const { name, value } of formEntries) {
|
||||
form.append(name, value)
|
||||
}
|
||||
|
||||
opts.headers = Object.assign(
|
||||
{},
|
||||
this.defaultHeaders,
|
||||
form.getHeaders(),
|
||||
opts.headers)
|
||||
|
||||
const url = new URL(`/api/${endpoint}`, this.url)
|
||||
return fetch(url, opts)
|
||||
}
|
||||
|
||||
upload (files) {
|
||||
throw new Error('NOT IMPLEMENTED');
|
||||
}
|
||||
const formEntries = files.map(({ name, path }) => {
|
||||
return {
|
||||
name,
|
||||
value: createReadStream(path)
|
||||
}
|
||||
})
|
||||
|
||||
deploy (folder) {
|
||||
throw new Error('NOT IMPLEMENTED');
|
||||
return this.post('upload', formEntries).then(handleResponse)
|
||||
}
|
||||
|
||||
delete (filenames) {
|
||||
const args = {
|
||||
filenames
|
||||
};
|
||||
assert(filenames, 'filenames is a required first argument')
|
||||
assert(Array.isArray(filenames), 'filenames argument must be an array of file paths in your website')
|
||||
|
||||
return this.post('/delete', args);
|
||||
const formEntries = filenames.map(file => ({
|
||||
name: 'filenames[]',
|
||||
value: file
|
||||
}))
|
||||
|
||||
return this.post('delete', formEntries).then(handleResponse)
|
||||
}
|
||||
|
||||
list (args) {
|
||||
list (queries) {
|
||||
// args.path: Path to list
|
||||
return this.get('/list', args).then(handleResponse);
|
||||
return this.get('list', queries).then(handleResponse)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Object} args Querystring arguments to include
|
||||
* @return {Promise} Fetch request promise
|
||||
*/
|
||||
info (args) {
|
||||
info (queries) {
|
||||
// args.sitename: sitename to get info on
|
||||
return this.get('/info', args).then(handleResponse);
|
||||
return this.get('info', queries).then(handleResponse)
|
||||
}
|
||||
}
|
||||
|
||||
export async function handleResponse (response) {
|
||||
const contentType = response.headers.get('Content-Type');
|
||||
const isJSON = contentType && contentType.match(/json/);
|
||||
const data = isJSON ? await response.json() : await response.text();
|
||||
if (response.ok) return data;
|
||||
else {
|
||||
return isJSON
|
||||
? Promise.reject(new JSONHTTPError(response, data))
|
||||
: Promise.reject(new TextHTTPError(response, data));
|
||||
}
|
||||
}
|
||||
|
||||
export class HTTPError extends Error {
|
||||
constructor (response) {
|
||||
super(response.statusText);
|
||||
this.name = this.constructor.name;
|
||||
if (typeof Error.captureStackTrace === 'function') {
|
||||
Error.captureStackTrace(this, this.constructor);
|
||||
} else {
|
||||
this.stack = new Error(response.statusText).stack;
|
||||
}
|
||||
this.status = response.status;
|
||||
}
|
||||
}
|
||||
|
||||
export class TextHTTPError extends HTTPError {
|
||||
constructor (response, data) {
|
||||
super(response);
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
|
||||
export class JSONHTTPError extends HTTPError {
|
||||
constructor (response, json) {
|
||||
super(response);
|
||||
this.json = json;
|
||||
deploy (folder) {
|
||||
throw new Error('NOT IMPLEMENTED')
|
||||
}
|
||||
}
|
||||
module.exports = { NeocitiesAPIClient }
|
||||
|
||||
Reference in New Issue
Block a user