Skip to content

Instantly share code, notes, and snippets.

@davidmh
Created June 13, 2018 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidmh/eb99b5cc92c6a5b29f03ac519bb8df67 to your computer and use it in GitHub Desktop.
Save davidmh/eb99b5cc92c6a5b29f03ac519bb8df67 to your computer and use it in GitHub Desktop.
discourse api
require('isomorphic-fetch')
const { createFetch, base, accept, method, params, parse } = require('http-client')
module.exports = (base_url, api_username, api_key) => {
const auth = params({ api_username, api_key })
const requestFactory = method_name => (pathname, data = {}) => {
const req = createFetch(
base(base_url),
accept('application/json'),
method(method_name),
auth,
params(data)
)
return req(pathname).then(data => data.json())
}
const get = requestFactory('GET')
const post = requestFactory('POST')
const put = requestFactory('PUT')
return { get, post, put }
}
/* example
const discourse = require('./index')(
'https://test.com',
'username',
'key'
);
discourse
.get('/groups.json')
.then(groups => console.log('groups', groups));
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment