Created
June 13, 2018 08:19
-
-
Save davidmh/eb99b5cc92c6a5b29f03ac519bb8df67 to your computer and use it in GitHub Desktop.
discourse api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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