discourse-js
Advanced tools
Comparing version 2.0.3 to 2.0.4
'use strict'; | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function ownKeys(object, enumerableOnly) { | ||
var keys = Object.keys(object); | ||
if (Object.getOwnPropertySymbols) { | ||
var symbols = Object.getOwnPropertySymbols(object); | ||
if (enumerableOnly) symbols = symbols.filter(function (sym) { | ||
return Object.getOwnPropertyDescriptor(object, sym).enumerable; | ||
}); | ||
keys.push.apply(keys, symbols); | ||
} | ||
return keys; | ||
} | ||
function _objectSpread2(target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
if (i % 2) { | ||
ownKeys(Object(source), true).forEach(function (key) { | ||
_defineProperty(target, key, source[key]); | ||
}); | ||
} else if (Object.getOwnPropertyDescriptors) { | ||
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); | ||
} else { | ||
ownKeys(Object(source)).forEach(function (key) { | ||
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); | ||
}); | ||
} | ||
} | ||
return target; | ||
} | ||
function _objectWithoutPropertiesLoose(source, excluded) { | ||
if (source == null) return {}; | ||
var target = {}; | ||
var sourceKeys = Object.keys(source); | ||
var key, i; | ||
for (i = 0; i < sourceKeys.length; i++) { | ||
key = sourceKeys[i]; | ||
if (excluded.indexOf(key) >= 0) continue; | ||
target[key] = source[key]; | ||
} | ||
return target; | ||
} | ||
function _objectWithoutProperties(source, excluded) { | ||
if (source == null) return {}; | ||
var target = _objectWithoutPropertiesLoose(source, excluded); | ||
var key, i; | ||
if (Object.getOwnPropertySymbols) { | ||
var sourceSymbolKeys = Object.getOwnPropertySymbols(source); | ||
for (i = 0; i < sourceSymbolKeys.length; i++) { | ||
key = sourceSymbolKeys[i]; | ||
if (excluded.indexOf(key) >= 0) continue; | ||
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; | ||
target[key] = source[key]; | ||
} | ||
} | ||
return target; | ||
} | ||
const createBody = params => { | ||
const form = new FormData(); | ||
Object.keys(params).forEach(paramKey => { | ||
@@ -11,3 +95,3 @@ const paramValue = params[paramKey]; | ||
paramValue.forEach(param => { | ||
form.append(`${paramKey}[]`, param); | ||
form.append("".concat(paramKey, "[]"), param); | ||
}); | ||
@@ -20,65 +104,66 @@ } else { | ||
}; | ||
const buildQueryString = (uri, params) => { | ||
const queryString = Object.keys(params) | ||
.map(key => { | ||
const value = params[key]; | ||
const queryString = Object.keys(params).map(key => { | ||
const value = params[key]; | ||
if (Array.isArray(value)) { | ||
return value.map(sub => `${key}[]=${sub}`).join('&'); | ||
} | ||
if (Array.isArray(value)) { | ||
return value.map(sub => "".concat(key, "[]=").concat(sub)).join('&'); | ||
} | ||
return `${key}=${params[key]}`; | ||
}) | ||
.join('&'); | ||
const separator = !!queryString ? (uri.indexOf('?') !== -1 ? '&' : '?') : ''; | ||
return `${uri}${separator}${queryString}`; | ||
return "".concat(key, "=").concat(params[key]); | ||
}).join('&'); | ||
const separator = !!queryString ? uri.indexOf('?') !== -1 ? '&' : '?' : ''; | ||
return "".concat(uri).concat(separator).concat(queryString); | ||
}; | ||
class ApiError extends Error { | ||
constructor(status, statusText, error, errorArray = []) { | ||
constructor(status, statusText, error) { | ||
let errorArray = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : []; | ||
super(); | ||
const errorMessage = error || errorArray.join(', '); | ||
this.name = 'ApiError'; | ||
this.status = status; | ||
this.statusText = statusText; | ||
this.message = `${status} - ${statusText || errorMessage}`; | ||
this.message = "".concat(status, " - ").concat(statusText || errorMessage); | ||
this.error = errorMessage; | ||
} | ||
} | ||
function Categories(discourse) { | ||
this.getCategory = async ( | ||
{ cat_id, latest, ...inputs } = { latest: false }, | ||
) => { | ||
this.getCategory = async function () { | ||
let _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { | ||
latest: false | ||
}, | ||
{ | ||
cat_id, | ||
latest | ||
} = _ref, | ||
inputs = _objectWithoutProperties(_ref, ["cat_id", "latest"]); | ||
if (!cat_id) { | ||
throw new Error( | ||
'No id defined. You must pass an `cat_id` to the getCategory function.', | ||
); | ||
throw new Error('No id defined. You must pass an `cat_id` to the getCategory function.'); | ||
} | ||
return discourse.get({ | ||
path: buildQueryString( | ||
`c/${cat_id}${latest ? '/l/latest' : ''}.json`, | ||
inputs, | ||
), | ||
path: buildQueryString("c/".concat(cat_id).concat(latest ? '/l/latest' : '', ".json"), inputs) | ||
}); | ||
}; | ||
this.getSubcategory = async ( | ||
{ cat_id, subcat_id, latest, ...inputs } = { latest: false }, | ||
) => { | ||
this.getSubcategory = async function () { | ||
let _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { | ||
latest: false | ||
}, | ||
{ | ||
cat_id, | ||
subcat_id, | ||
latest | ||
} = _ref2, | ||
inputs = _objectWithoutProperties(_ref2, ["cat_id", "subcat_id", "latest"]); | ||
if (!cat_id || !subcat_id) { | ||
throw new Error( | ||
'No id defined. You must pass an id to the getSubcategory function.', | ||
); | ||
throw new Error('No id defined. You must pass an id to the getSubcategory function.'); | ||
} | ||
return discourse.get({ | ||
path: buildQueryString( | ||
`c/${cat_id}/${subcat_id}${latest ? '/l/latest' : ''}.json`, | ||
inputs, | ||
), | ||
path: buildQueryString("c/".concat(cat_id, "/").concat(subcat_id).concat(latest ? '/l/latest' : '', ".json"), inputs) | ||
}); | ||
@@ -89,5 +174,8 @@ }; | ||
function Groups(discourse) { | ||
this.getMembers = async ({ group_name }) => { | ||
this.getMembers = async (_ref) => { | ||
let { | ||
group_name | ||
} = _ref; | ||
return discourse.get({ | ||
path: `groups/${group_name}/members.json`, | ||
path: "groups/".concat(group_name, "/members.json") | ||
}); | ||
@@ -100,12 +188,15 @@ }; | ||
return discourse.get({ | ||
path: `topics/private-messages/${discourse._API_USERNAME}.json`, | ||
path: "topics/private-messages/".concat(discourse._API_USERNAME, ".json") | ||
}); | ||
}; | ||
this.getGroupMessages = async ({ group_name }) => { | ||
this.getGroupMessages = async (_ref) => { | ||
let { | ||
group_name | ||
} = _ref; | ||
return discourse.get({ | ||
path: `topics/private-messages-group/${discourse._API_USERNAME}/${group_name}.json`, | ||
path: "topics/private-messages-group/".concat(discourse._API_USERNAME, "/").concat(group_name, ".json"), | ||
headers: { | ||
Accept: 'application/json', | ||
}, | ||
Accept: 'application/json' | ||
} | ||
}); | ||
@@ -116,3 +207,3 @@ }; | ||
return discourse.get({ | ||
path: `topics/private-messages-sent/${discourse._API_USERNAME}.json`, | ||
path: "topics/private-messages-sent/".concat(discourse._API_USERNAME, ".json") | ||
}); | ||
@@ -124,7 +215,10 @@ }; | ||
const getSentMessages = this.getSentMessages(); | ||
return Promise.all([getMessages, getSentMessages]); | ||
}; | ||
this.send = async ({ topic_id, raw }) => { | ||
this.send = async (_ref2) => { | ||
let { | ||
topic_id, | ||
raw | ||
} = _ref2; | ||
return discourse.post({ | ||
@@ -135,4 +229,4 @@ path: 'posts', | ||
raw, | ||
archetype: 'private_message', | ||
}, | ||
archetype: 'private_message' | ||
} | ||
}); | ||
@@ -143,20 +237,21 @@ }; | ||
function Notifications(discourse) { | ||
this.get = async (inputs = {}) => { | ||
this.get = async function () { | ||
let inputs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return discourse.get({ | ||
method: 'GET', | ||
path: buildQueryString('notifications.json', inputs), | ||
path: buildQueryString('notifications.json', inputs) | ||
}); | ||
}; | ||
this.markRead = async ({ id }) => { | ||
return discourse.put({ | ||
path: 'notifications/mark-read', | ||
...(id | ||
? { | ||
body: { | ||
id, | ||
}, | ||
} | ||
: {}), | ||
}); | ||
this.markRead = async (_ref) => { | ||
let { | ||
id | ||
} = _ref; | ||
return discourse.put(_objectSpread2({ | ||
path: 'notifications/mark-read' | ||
}, id ? { | ||
body: { | ||
id | ||
} | ||
} : {})); | ||
}; | ||
@@ -166,29 +261,32 @@ } | ||
function Posts(discourse) { | ||
this.create = async (inputs = {}) => { | ||
this.create = async function () { | ||
let inputs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
// If an imageUri has been passed, upload the image first. | ||
if (inputs.imageUri) { | ||
const { url, width, height, short_url } = await discourse.uploads.create({ | ||
const { | ||
url, | ||
width, | ||
height, | ||
short_url | ||
} = await discourse.uploads.create({ | ||
'files[]': { | ||
uri: inputs.imageUri, | ||
name: 'photo.jpeg', | ||
type: 'image/jpeg', | ||
type: 'image/jpeg' | ||
}, | ||
type: 'composer', | ||
synchronous: true, | ||
synchronous: true | ||
}); | ||
if (url) { | ||
const body = {}; | ||
const body = {}; // Remove the imageUri from the inputs as it's not used in the next request. | ||
// Remove the imageUri from the inputs as it's not used in the next request. | ||
delete inputs.imageUri; | ||
Object.keys(inputs).forEach(key => body[key] = inputs[key]); // Prepend the raw message with the image. | ||
Object.keys(inputs).forEach(key => (body[key] = inputs[key])); | ||
// Prepend the raw message with the image. | ||
body.raw = `![${width}x${height}](${short_url})\n${body.raw}`; | ||
body.raw = "![".concat(width, "x").concat(height, "](").concat(short_url, ")\n").concat(body.raw); | ||
return discourse.post({ | ||
path: 'posts', | ||
body, | ||
body | ||
}); | ||
@@ -199,3 +297,3 @@ } | ||
path: 'posts', | ||
body: inputs, | ||
body: inputs | ||
}); | ||
@@ -205,7 +303,11 @@ } | ||
this.reply = async ({ topic_id, raw, reply_to_post_number }) => { | ||
this.reply = async (_ref) => { | ||
let { | ||
topic_id, | ||
raw, | ||
reply_to_post_number | ||
} = _ref; | ||
if (!topic_id) { | ||
throw new Error( | ||
'No topic_id defined. You must pass a topic to reply function.', | ||
); | ||
throw new Error('No topic_id defined. You must pass a topic to reply function.'); | ||
} | ||
@@ -220,7 +322,6 @@ | ||
archetype: 'regular', | ||
nested_post: true, | ||
}, | ||
nested_post: true | ||
} | ||
}); | ||
}; | ||
/** | ||
@@ -236,27 +337,71 @@ * post_action_type_id values | ||
*/ | ||
this.postAction = async ({ method = 'post', body = {}, id = null }) => { | ||
this.postAction = async (_ref2) => { | ||
let { | ||
method = 'post', | ||
body = {}, | ||
id = null | ||
} = _ref2; | ||
return discourse[method]({ | ||
path: id ? `post_actions/${id}` : 'post_actions', | ||
body, | ||
path: id ? "post_actions/".concat(id) : 'post_actions', | ||
body | ||
}); | ||
}; | ||
this.like = ({ id }) => | ||
this.postAction({ body: { id, post_action_type_id: 2 } }); | ||
this.like = (_ref3) => { | ||
let { | ||
id | ||
} = _ref3; | ||
return this.postAction({ | ||
body: { | ||
id, | ||
post_action_type_id: 2 | ||
} | ||
}); | ||
}; | ||
this.unlike = ({ id }) => | ||
this.postAction({ method: 'delete', body: { post_action_type_id: 2 }, id }); | ||
this.unlike = (_ref4) => { | ||
let { | ||
id | ||
} = _ref4; | ||
return this.postAction({ | ||
method: 'delete', | ||
body: { | ||
post_action_type_id: 2 | ||
}, | ||
id | ||
}); | ||
}; | ||
this.flag = ({ id, post_action_type_id, message, flag_topic }) => | ||
this.postAction({ body: { id, post_action_type_id, message, flag_topic } }); | ||
this.flag = (_ref5) => { | ||
let { | ||
id, | ||
post_action_type_id, | ||
message, | ||
flag_topic | ||
} = _ref5; | ||
return this.postAction({ | ||
body: { | ||
id, | ||
post_action_type_id, | ||
message, | ||
flag_topic | ||
} | ||
}); | ||
}; | ||
} | ||
function Preferences(discourse) { | ||
this.pickAvatar = async ({ username, upload_id } = {}) => { | ||
this.pickAvatar = async function () { | ||
let { | ||
username, | ||
upload_id | ||
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return discourse.put({ | ||
path: `u/${username}/preferences/avatar/pick`, | ||
path: "u/".concat(username, "/preferences/avatar/pick"), | ||
body: { | ||
upload_id, | ||
type: 'uploaded', | ||
}, | ||
type: 'uploaded' | ||
} | ||
}); | ||
@@ -267,19 +412,25 @@ }; | ||
function Tags(discourse) { | ||
this.getTopic = async ({ tag, ...inputs } = {}) => { | ||
this.getTopic = async function () { | ||
let _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
{ | ||
tag | ||
} = _ref, | ||
inputs = _objectWithoutProperties(_ref, ["tag"]); | ||
return discourse.get({ | ||
path: buildQueryString(`tags/${tag}.json`, inputs), | ||
path: buildQueryString("tags/".concat(tag, ".json"), inputs) | ||
}); | ||
}; | ||
this.getTopicsForCategory = async ({ | ||
tag, | ||
category, | ||
subcategory, | ||
...inputs | ||
} = {}) => { | ||
this.getTopicsForCategory = async function () { | ||
let _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
{ | ||
tag, | ||
category, | ||
subcategory | ||
} = _ref2, | ||
inputs = _objectWithoutProperties(_ref2, ["tag", "category", "subcategory"]); | ||
return discourse.get({ | ||
path: buildQueryString( | ||
`tags/c/${category}/${subcategory ? `${subcategory}/` : ''}${tag}.json`, | ||
inputs, | ||
), | ||
path: buildQueryString("tags/c/".concat(category, "/").concat(subcategory ? "".concat(subcategory, "/") : '').concat(tag, ".json"), inputs) | ||
}); | ||
@@ -290,28 +441,52 @@ }; | ||
function Topics(discourse) { | ||
this.getTopic = async ({ id, reverse, ...inputs } = {}) => { | ||
this.getTopic = async function () { | ||
let _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
{ | ||
id, | ||
reverse | ||
} = _ref, | ||
inputs = _objectWithoutProperties(_ref, ["id", "reverse"]); | ||
return discourse.get({ | ||
path: buildQueryString(`t/${id}${reverse ? '/last' : ''}.json`, inputs), | ||
path: buildQueryString("t/".concat(id).concat(reverse ? '/last' : '', ".json"), inputs) | ||
}); | ||
}; | ||
this.getTopicPosts = async ({ id, posts, ...inputs } = {}) => { | ||
const params = { | ||
post_ids: posts, | ||
...inputs, | ||
}; | ||
this.getTopicPosts = async function () { | ||
let _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
{ | ||
id, | ||
posts | ||
} = _ref2, | ||
inputs = _objectWithoutProperties(_ref2, ["id", "posts"]); | ||
const params = _objectSpread2({ | ||
post_ids: posts | ||
}, inputs); | ||
return discourse.get({ | ||
path: buildQueryString(`t/${id}/posts.json`, params), | ||
path: buildQueryString("t/".concat(id, "/posts.json"), params) | ||
}); | ||
}; | ||
this.deleteTopic = async ({ id, ...inputs } = {}) => { | ||
this.deleteTopic = async function () { | ||
let _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
{ | ||
id | ||
} = _ref3, | ||
inputs = _objectWithoutProperties(_ref3, ["id"]); | ||
return discourse.delete({ | ||
path: buildQueryString(`t/${id}`, inputs), | ||
path: buildQueryString("t/".concat(id), inputs) | ||
}); | ||
}; | ||
this.getTopicsByUsername = async ({ username, ...inputs }) => { | ||
this.getTopicsByUsername = async (_ref4) => { | ||
let { | ||
username | ||
} = _ref4, | ||
inputs = _objectWithoutProperties(_ref4, ["username"]); | ||
return discourse.get({ | ||
path: buildQueryString(`topics/created-by/${username}.json`, inputs), | ||
path: buildQueryString("topics/created-by/".concat(username, ".json"), inputs) | ||
}); | ||
@@ -326,6 +501,7 @@ }; | ||
function Uploads(discourse) { | ||
this.create = async (body = {}) => { | ||
this.create = async function () { | ||
let body = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return discourse.post({ | ||
path: 'uploads.json', | ||
body, | ||
body | ||
}); | ||
@@ -336,9 +512,9 @@ }; | ||
function Users(discourse) { | ||
this.getUser = async ({ username } = {}) => { | ||
return discourse | ||
.get({ | ||
path: `users/${username}.json`, | ||
}) | ||
.then(response => response) | ||
.catch(error => error); | ||
this.getUser = async function () { | ||
let { | ||
username | ||
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return discourse.get({ | ||
path: "users/".concat(username, ".json") | ||
}).then(response => response).catch(error => error); | ||
}; | ||
@@ -359,110 +535,83 @@ } | ||
Uploads, | ||
Users, | ||
Users | ||
}; | ||
class Discourse { | ||
constructor(userApiKey, baseUrl, apiKey = null) { | ||
this._BASE_URL = baseUrl; | ||
this._USER_API_Key = userApiKey; | ||
constructor(_userApiKey, _baseUrl) { | ||
var _this = this; | ||
// Admin User API | ||
this._API_KEY = apiKey; | ||
let _apiKey = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; | ||
for (let resource in resources) { | ||
this[resource.toLowerCase()] = new resources[resource](this); | ||
} | ||
} | ||
_defineProperty(this, "config", function () { | ||
let { | ||
userApiKey, | ||
baseUrl, | ||
apiUsername, | ||
apiKey | ||
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { | ||
apiUsername: null, | ||
apiKey: null | ||
}; | ||
_this._USER_API_KEY = userApiKey; | ||
_this._BASE_URL = baseUrl; // Admin User API | ||
config = ( | ||
{ userApiKey, baseUrl, apiUsername, apiKey } = { | ||
apiUsername: null, | ||
apiKey: null, | ||
}, | ||
) => { | ||
this._USER_API_KEY = userApiKey; | ||
this._BASE_URL = baseUrl; | ||
_this._API_KEY = apiKey; | ||
_this._API_USERNAME = apiUsername; // If we are using the Admin API then we'll need to include | ||
// the API key and username in each request either as part | ||
// of our URL params or as part of our POST body | ||
// Admin User API | ||
this._API_KEY = apiKey; | ||
this._API_USERNAME = apiUsername; | ||
// If we are using the Admin API then we'll need to include | ||
// the API key and username in each request either as part | ||
// of our URL params or as part of our POST body | ||
this.isUsingAdminAPI = this._API_KEY && this._API_USERNAME; | ||
}; | ||
requestHeaders() { | ||
return { | ||
'User-Agent': `DiscourseJS ${VERSION}`, | ||
...(this._USER_API_KEY ? { 'User-Api-Key': this._USER_API_KEY } : {}), | ||
}; | ||
} | ||
createBody = body => { | ||
return this.isUsingAdminAPI | ||
? createBody({ | ||
...body, | ||
api_key: this._API_KEY, | ||
api_username: this._API_USERNAME, | ||
}) | ||
: createBody(body); | ||
}; | ||
get = ({ path, headers } = {}) => { | ||
return this.request({ | ||
method: 'GET', | ||
headers, | ||
path: this.isUsingAdminAPI | ||
? buildQueryString(path, { | ||
api_key: this._API_KEY, | ||
api_username: this._API_USERNAME, | ||
}) | ||
: path, | ||
_this.isUsingAdminAPI = _this._API_KEY && _this._API_USERNAME; | ||
}); | ||
}; | ||
post = ({ path, headers, body }) => { | ||
return this.request({ | ||
method: 'POST', | ||
headers, | ||
path, | ||
body: this.createBody(body), | ||
_defineProperty(this, "createBody", body => { | ||
return this.isUsingAdminAPI ? createBody(_objectSpread2(_objectSpread2({}, body), {}, { | ||
api_key: this._API_KEY, | ||
api_username: this._API_USERNAME | ||
})) : createBody(body); | ||
}); | ||
}; | ||
put({ path, headers, body }) { | ||
return this.request({ | ||
method: 'PUT', | ||
headers, | ||
path, | ||
body: this.createBody(body), | ||
_defineProperty(this, "get", function () { | ||
let { | ||
path, | ||
headers | ||
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return _this.request({ | ||
method: 'GET', | ||
headers, | ||
path: _this.isUsingAdminAPI ? buildQueryString(path, { | ||
api_key: _this._API_KEY, | ||
api_username: _this._API_USERNAME | ||
}) : path | ||
}); | ||
}); | ||
} | ||
delete({ path, headers, body }) { | ||
return this.request({ | ||
method: 'DELETE', | ||
headers, | ||
path, | ||
body: this.createBody(body), | ||
_defineProperty(this, "post", (_ref) => { | ||
let { | ||
path, | ||
headers, | ||
body | ||
} = _ref; | ||
return this.request({ | ||
method: 'POST', | ||
headers, | ||
path, | ||
body: this.createBody(body) | ||
}); | ||
}); | ||
} | ||
request = options => { | ||
const { body, method, path, headers } = options; | ||
_defineProperty(this, "request", options => { | ||
const { | ||
body, | ||
method, | ||
path, | ||
headers | ||
} = options; | ||
const fetchOptions = { | ||
method, | ||
headers: _objectSpread2(_objectSpread2({}, this.requestHeaders()), headers), | ||
body, | ||
mimeType: 'multipart/form-data' | ||
}; | ||
return fetch("".concat(this._BASE_URL, "/").concat(path), fetchOptions).then(response => { | ||
const contentType = response.headers.get('content-type'); | ||
const fetchOptions = { | ||
method, | ||
headers: { | ||
...this.requestHeaders(), | ||
...headers, | ||
}, | ||
body, | ||
mimeType: 'multipart/form-data', | ||
}; | ||
return fetch(`${this._BASE_URL}/${path}`, fetchOptions) | ||
.then(response => { | ||
const contentType = response.headers.get('content-type'); | ||
if (response.ok) { | ||
@@ -481,3 +630,7 @@ if (contentType && contentType.indexOf('application/json') !== -1) { | ||
} else { | ||
const { status, statusText } = response; | ||
const { | ||
status, | ||
statusText | ||
} = response; | ||
if (contentType && contentType.indexOf('application/json') !== -1) { | ||
@@ -493,10 +646,56 @@ return response.json().then(json => { | ||
} | ||
}) | ||
.catch(error => { | ||
}).catch(error => { | ||
if (error instanceof ApiError) throw error; | ||
throw new Error(error); | ||
}); | ||
}; | ||
}); | ||
this._BASE_URL = _baseUrl; | ||
this._USER_API_Key = _userApiKey; // Admin User API | ||
this._API_KEY = _apiKey; | ||
for (let resource in resources) { | ||
this[resource.toLowerCase()] = new resources[resource](this); | ||
} | ||
} | ||
requestHeaders() { | ||
return _objectSpread2({ | ||
'User-Agent': "DiscourseJS ".concat(VERSION) | ||
}, this._USER_API_KEY ? { | ||
'User-Api-Key': this._USER_API_KEY | ||
} : {}); | ||
} | ||
put(_ref2) { | ||
let { | ||
path, | ||
headers, | ||
body | ||
} = _ref2; | ||
return this.request({ | ||
method: 'PUT', | ||
headers, | ||
path, | ||
body: this.createBody(body) | ||
}); | ||
} | ||
delete(_ref3) { | ||
let { | ||
path, | ||
headers, | ||
body | ||
} = _ref3; | ||
return this.request({ | ||
method: 'DELETE', | ||
headers, | ||
path, | ||
body: this.createBody(body) | ||
}); | ||
} | ||
} | ||
module.exports = Discourse; |
@@ -0,4 +1,88 @@ | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function ownKeys(object, enumerableOnly) { | ||
var keys = Object.keys(object); | ||
if (Object.getOwnPropertySymbols) { | ||
var symbols = Object.getOwnPropertySymbols(object); | ||
if (enumerableOnly) symbols = symbols.filter(function (sym) { | ||
return Object.getOwnPropertyDescriptor(object, sym).enumerable; | ||
}); | ||
keys.push.apply(keys, symbols); | ||
} | ||
return keys; | ||
} | ||
function _objectSpread2(target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
if (i % 2) { | ||
ownKeys(Object(source), true).forEach(function (key) { | ||
_defineProperty(target, key, source[key]); | ||
}); | ||
} else if (Object.getOwnPropertyDescriptors) { | ||
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); | ||
} else { | ||
ownKeys(Object(source)).forEach(function (key) { | ||
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); | ||
}); | ||
} | ||
} | ||
return target; | ||
} | ||
function _objectWithoutPropertiesLoose(source, excluded) { | ||
if (source == null) return {}; | ||
var target = {}; | ||
var sourceKeys = Object.keys(source); | ||
var key, i; | ||
for (i = 0; i < sourceKeys.length; i++) { | ||
key = sourceKeys[i]; | ||
if (excluded.indexOf(key) >= 0) continue; | ||
target[key] = source[key]; | ||
} | ||
return target; | ||
} | ||
function _objectWithoutProperties(source, excluded) { | ||
if (source == null) return {}; | ||
var target = _objectWithoutPropertiesLoose(source, excluded); | ||
var key, i; | ||
if (Object.getOwnPropertySymbols) { | ||
var sourceSymbolKeys = Object.getOwnPropertySymbols(source); | ||
for (i = 0; i < sourceSymbolKeys.length; i++) { | ||
key = sourceSymbolKeys[i]; | ||
if (excluded.indexOf(key) >= 0) continue; | ||
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; | ||
target[key] = source[key]; | ||
} | ||
} | ||
return target; | ||
} | ||
const createBody = params => { | ||
const form = new FormData(); | ||
Object.keys(params).forEach(paramKey => { | ||
@@ -9,3 +93,3 @@ const paramValue = params[paramKey]; | ||
paramValue.forEach(param => { | ||
form.append(`${paramKey}[]`, param); | ||
form.append("".concat(paramKey, "[]"), param); | ||
}); | ||
@@ -18,65 +102,66 @@ } else { | ||
}; | ||
const buildQueryString = (uri, params) => { | ||
const queryString = Object.keys(params) | ||
.map(key => { | ||
const value = params[key]; | ||
const queryString = Object.keys(params).map(key => { | ||
const value = params[key]; | ||
if (Array.isArray(value)) { | ||
return value.map(sub => `${key}[]=${sub}`).join('&'); | ||
} | ||
if (Array.isArray(value)) { | ||
return value.map(sub => "".concat(key, "[]=").concat(sub)).join('&'); | ||
} | ||
return `${key}=${params[key]}`; | ||
}) | ||
.join('&'); | ||
const separator = !!queryString ? (uri.indexOf('?') !== -1 ? '&' : '?') : ''; | ||
return `${uri}${separator}${queryString}`; | ||
return "".concat(key, "=").concat(params[key]); | ||
}).join('&'); | ||
const separator = !!queryString ? uri.indexOf('?') !== -1 ? '&' : '?' : ''; | ||
return "".concat(uri).concat(separator).concat(queryString); | ||
}; | ||
class ApiError extends Error { | ||
constructor(status, statusText, error, errorArray = []) { | ||
constructor(status, statusText, error) { | ||
let errorArray = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : []; | ||
super(); | ||
const errorMessage = error || errorArray.join(', '); | ||
this.name = 'ApiError'; | ||
this.status = status; | ||
this.statusText = statusText; | ||
this.message = `${status} - ${statusText || errorMessage}`; | ||
this.message = "".concat(status, " - ").concat(statusText || errorMessage); | ||
this.error = errorMessage; | ||
} | ||
} | ||
function Categories(discourse) { | ||
this.getCategory = async ( | ||
{ cat_id, latest, ...inputs } = { latest: false }, | ||
) => { | ||
this.getCategory = async function () { | ||
let _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { | ||
latest: false | ||
}, | ||
{ | ||
cat_id, | ||
latest | ||
} = _ref, | ||
inputs = _objectWithoutProperties(_ref, ["cat_id", "latest"]); | ||
if (!cat_id) { | ||
throw new Error( | ||
'No id defined. You must pass an `cat_id` to the getCategory function.', | ||
); | ||
throw new Error('No id defined. You must pass an `cat_id` to the getCategory function.'); | ||
} | ||
return discourse.get({ | ||
path: buildQueryString( | ||
`c/${cat_id}${latest ? '/l/latest' : ''}.json`, | ||
inputs, | ||
), | ||
path: buildQueryString("c/".concat(cat_id).concat(latest ? '/l/latest' : '', ".json"), inputs) | ||
}); | ||
}; | ||
this.getSubcategory = async ( | ||
{ cat_id, subcat_id, latest, ...inputs } = { latest: false }, | ||
) => { | ||
this.getSubcategory = async function () { | ||
let _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { | ||
latest: false | ||
}, | ||
{ | ||
cat_id, | ||
subcat_id, | ||
latest | ||
} = _ref2, | ||
inputs = _objectWithoutProperties(_ref2, ["cat_id", "subcat_id", "latest"]); | ||
if (!cat_id || !subcat_id) { | ||
throw new Error( | ||
'No id defined. You must pass an id to the getSubcategory function.', | ||
); | ||
throw new Error('No id defined. You must pass an id to the getSubcategory function.'); | ||
} | ||
return discourse.get({ | ||
path: buildQueryString( | ||
`c/${cat_id}/${subcat_id}${latest ? '/l/latest' : ''}.json`, | ||
inputs, | ||
), | ||
path: buildQueryString("c/".concat(cat_id, "/").concat(subcat_id).concat(latest ? '/l/latest' : '', ".json"), inputs) | ||
}); | ||
@@ -87,5 +172,8 @@ }; | ||
function Groups(discourse) { | ||
this.getMembers = async ({ group_name }) => { | ||
this.getMembers = async (_ref) => { | ||
let { | ||
group_name | ||
} = _ref; | ||
return discourse.get({ | ||
path: `groups/${group_name}/members.json`, | ||
path: "groups/".concat(group_name, "/members.json") | ||
}); | ||
@@ -98,12 +186,15 @@ }; | ||
return discourse.get({ | ||
path: `topics/private-messages/${discourse._API_USERNAME}.json`, | ||
path: "topics/private-messages/".concat(discourse._API_USERNAME, ".json") | ||
}); | ||
}; | ||
this.getGroupMessages = async ({ group_name }) => { | ||
this.getGroupMessages = async (_ref) => { | ||
let { | ||
group_name | ||
} = _ref; | ||
return discourse.get({ | ||
path: `topics/private-messages-group/${discourse._API_USERNAME}/${group_name}.json`, | ||
path: "topics/private-messages-group/".concat(discourse._API_USERNAME, "/").concat(group_name, ".json"), | ||
headers: { | ||
Accept: 'application/json', | ||
}, | ||
Accept: 'application/json' | ||
} | ||
}); | ||
@@ -114,3 +205,3 @@ }; | ||
return discourse.get({ | ||
path: `topics/private-messages-sent/${discourse._API_USERNAME}.json`, | ||
path: "topics/private-messages-sent/".concat(discourse._API_USERNAME, ".json") | ||
}); | ||
@@ -122,7 +213,10 @@ }; | ||
const getSentMessages = this.getSentMessages(); | ||
return Promise.all([getMessages, getSentMessages]); | ||
}; | ||
this.send = async ({ topic_id, raw }) => { | ||
this.send = async (_ref2) => { | ||
let { | ||
topic_id, | ||
raw | ||
} = _ref2; | ||
return discourse.post({ | ||
@@ -133,4 +227,4 @@ path: 'posts', | ||
raw, | ||
archetype: 'private_message', | ||
}, | ||
archetype: 'private_message' | ||
} | ||
}); | ||
@@ -141,20 +235,21 @@ }; | ||
function Notifications(discourse) { | ||
this.get = async (inputs = {}) => { | ||
this.get = async function () { | ||
let inputs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return discourse.get({ | ||
method: 'GET', | ||
path: buildQueryString('notifications.json', inputs), | ||
path: buildQueryString('notifications.json', inputs) | ||
}); | ||
}; | ||
this.markRead = async ({ id }) => { | ||
return discourse.put({ | ||
path: 'notifications/mark-read', | ||
...(id | ||
? { | ||
body: { | ||
id, | ||
}, | ||
} | ||
: {}), | ||
}); | ||
this.markRead = async (_ref) => { | ||
let { | ||
id | ||
} = _ref; | ||
return discourse.put(_objectSpread2({ | ||
path: 'notifications/mark-read' | ||
}, id ? { | ||
body: { | ||
id | ||
} | ||
} : {})); | ||
}; | ||
@@ -164,29 +259,32 @@ } | ||
function Posts(discourse) { | ||
this.create = async (inputs = {}) => { | ||
this.create = async function () { | ||
let inputs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
// If an imageUri has been passed, upload the image first. | ||
if (inputs.imageUri) { | ||
const { url, width, height, short_url } = await discourse.uploads.create({ | ||
const { | ||
url, | ||
width, | ||
height, | ||
short_url | ||
} = await discourse.uploads.create({ | ||
'files[]': { | ||
uri: inputs.imageUri, | ||
name: 'photo.jpeg', | ||
type: 'image/jpeg', | ||
type: 'image/jpeg' | ||
}, | ||
type: 'composer', | ||
synchronous: true, | ||
synchronous: true | ||
}); | ||
if (url) { | ||
const body = {}; | ||
const body = {}; // Remove the imageUri from the inputs as it's not used in the next request. | ||
// Remove the imageUri from the inputs as it's not used in the next request. | ||
delete inputs.imageUri; | ||
Object.keys(inputs).forEach(key => body[key] = inputs[key]); // Prepend the raw message with the image. | ||
Object.keys(inputs).forEach(key => (body[key] = inputs[key])); | ||
// Prepend the raw message with the image. | ||
body.raw = `![${width}x${height}](${short_url})\n${body.raw}`; | ||
body.raw = "![".concat(width, "x").concat(height, "](").concat(short_url, ")\n").concat(body.raw); | ||
return discourse.post({ | ||
path: 'posts', | ||
body, | ||
body | ||
}); | ||
@@ -197,3 +295,3 @@ } | ||
path: 'posts', | ||
body: inputs, | ||
body: inputs | ||
}); | ||
@@ -203,7 +301,11 @@ } | ||
this.reply = async ({ topic_id, raw, reply_to_post_number }) => { | ||
this.reply = async (_ref) => { | ||
let { | ||
topic_id, | ||
raw, | ||
reply_to_post_number | ||
} = _ref; | ||
if (!topic_id) { | ||
throw new Error( | ||
'No topic_id defined. You must pass a topic to reply function.', | ||
); | ||
throw new Error('No topic_id defined. You must pass a topic to reply function.'); | ||
} | ||
@@ -218,7 +320,6 @@ | ||
archetype: 'regular', | ||
nested_post: true, | ||
}, | ||
nested_post: true | ||
} | ||
}); | ||
}; | ||
/** | ||
@@ -234,27 +335,71 @@ * post_action_type_id values | ||
*/ | ||
this.postAction = async ({ method = 'post', body = {}, id = null }) => { | ||
this.postAction = async (_ref2) => { | ||
let { | ||
method = 'post', | ||
body = {}, | ||
id = null | ||
} = _ref2; | ||
return discourse[method]({ | ||
path: id ? `post_actions/${id}` : 'post_actions', | ||
body, | ||
path: id ? "post_actions/".concat(id) : 'post_actions', | ||
body | ||
}); | ||
}; | ||
this.like = ({ id }) => | ||
this.postAction({ body: { id, post_action_type_id: 2 } }); | ||
this.like = (_ref3) => { | ||
let { | ||
id | ||
} = _ref3; | ||
return this.postAction({ | ||
body: { | ||
id, | ||
post_action_type_id: 2 | ||
} | ||
}); | ||
}; | ||
this.unlike = ({ id }) => | ||
this.postAction({ method: 'delete', body: { post_action_type_id: 2 }, id }); | ||
this.unlike = (_ref4) => { | ||
let { | ||
id | ||
} = _ref4; | ||
return this.postAction({ | ||
method: 'delete', | ||
body: { | ||
post_action_type_id: 2 | ||
}, | ||
id | ||
}); | ||
}; | ||
this.flag = ({ id, post_action_type_id, message, flag_topic }) => | ||
this.postAction({ body: { id, post_action_type_id, message, flag_topic } }); | ||
this.flag = (_ref5) => { | ||
let { | ||
id, | ||
post_action_type_id, | ||
message, | ||
flag_topic | ||
} = _ref5; | ||
return this.postAction({ | ||
body: { | ||
id, | ||
post_action_type_id, | ||
message, | ||
flag_topic | ||
} | ||
}); | ||
}; | ||
} | ||
function Preferences(discourse) { | ||
this.pickAvatar = async ({ username, upload_id } = {}) => { | ||
this.pickAvatar = async function () { | ||
let { | ||
username, | ||
upload_id | ||
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return discourse.put({ | ||
path: `u/${username}/preferences/avatar/pick`, | ||
path: "u/".concat(username, "/preferences/avatar/pick"), | ||
body: { | ||
upload_id, | ||
type: 'uploaded', | ||
}, | ||
type: 'uploaded' | ||
} | ||
}); | ||
@@ -265,19 +410,25 @@ }; | ||
function Tags(discourse) { | ||
this.getTopic = async ({ tag, ...inputs } = {}) => { | ||
this.getTopic = async function () { | ||
let _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
{ | ||
tag | ||
} = _ref, | ||
inputs = _objectWithoutProperties(_ref, ["tag"]); | ||
return discourse.get({ | ||
path: buildQueryString(`tags/${tag}.json`, inputs), | ||
path: buildQueryString("tags/".concat(tag, ".json"), inputs) | ||
}); | ||
}; | ||
this.getTopicsForCategory = async ({ | ||
tag, | ||
category, | ||
subcategory, | ||
...inputs | ||
} = {}) => { | ||
this.getTopicsForCategory = async function () { | ||
let _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
{ | ||
tag, | ||
category, | ||
subcategory | ||
} = _ref2, | ||
inputs = _objectWithoutProperties(_ref2, ["tag", "category", "subcategory"]); | ||
return discourse.get({ | ||
path: buildQueryString( | ||
`tags/c/${category}/${subcategory ? `${subcategory}/` : ''}${tag}.json`, | ||
inputs, | ||
), | ||
path: buildQueryString("tags/c/".concat(category, "/").concat(subcategory ? "".concat(subcategory, "/") : '').concat(tag, ".json"), inputs) | ||
}); | ||
@@ -288,28 +439,52 @@ }; | ||
function Topics(discourse) { | ||
this.getTopic = async ({ id, reverse, ...inputs } = {}) => { | ||
this.getTopic = async function () { | ||
let _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
{ | ||
id, | ||
reverse | ||
} = _ref, | ||
inputs = _objectWithoutProperties(_ref, ["id", "reverse"]); | ||
return discourse.get({ | ||
path: buildQueryString(`t/${id}${reverse ? '/last' : ''}.json`, inputs), | ||
path: buildQueryString("t/".concat(id).concat(reverse ? '/last' : '', ".json"), inputs) | ||
}); | ||
}; | ||
this.getTopicPosts = async ({ id, posts, ...inputs } = {}) => { | ||
const params = { | ||
post_ids: posts, | ||
...inputs, | ||
}; | ||
this.getTopicPosts = async function () { | ||
let _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
{ | ||
id, | ||
posts | ||
} = _ref2, | ||
inputs = _objectWithoutProperties(_ref2, ["id", "posts"]); | ||
const params = _objectSpread2({ | ||
post_ids: posts | ||
}, inputs); | ||
return discourse.get({ | ||
path: buildQueryString(`t/${id}/posts.json`, params), | ||
path: buildQueryString("t/".concat(id, "/posts.json"), params) | ||
}); | ||
}; | ||
this.deleteTopic = async ({ id, ...inputs } = {}) => { | ||
this.deleteTopic = async function () { | ||
let _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
{ | ||
id | ||
} = _ref3, | ||
inputs = _objectWithoutProperties(_ref3, ["id"]); | ||
return discourse.delete({ | ||
path: buildQueryString(`t/${id}`, inputs), | ||
path: buildQueryString("t/".concat(id), inputs) | ||
}); | ||
}; | ||
this.getTopicsByUsername = async ({ username, ...inputs }) => { | ||
this.getTopicsByUsername = async (_ref4) => { | ||
let { | ||
username | ||
} = _ref4, | ||
inputs = _objectWithoutProperties(_ref4, ["username"]); | ||
return discourse.get({ | ||
path: buildQueryString(`topics/created-by/${username}.json`, inputs), | ||
path: buildQueryString("topics/created-by/".concat(username, ".json"), inputs) | ||
}); | ||
@@ -324,6 +499,7 @@ }; | ||
function Uploads(discourse) { | ||
this.create = async (body = {}) => { | ||
this.create = async function () { | ||
let body = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return discourse.post({ | ||
path: 'uploads.json', | ||
body, | ||
body | ||
}); | ||
@@ -334,9 +510,9 @@ }; | ||
function Users(discourse) { | ||
this.getUser = async ({ username } = {}) => { | ||
return discourse | ||
.get({ | ||
path: `users/${username}.json`, | ||
}) | ||
.then(response => response) | ||
.catch(error => error); | ||
this.getUser = async function () { | ||
let { | ||
username | ||
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return discourse.get({ | ||
path: "users/".concat(username, ".json") | ||
}).then(response => response).catch(error => error); | ||
}; | ||
@@ -357,110 +533,83 @@ } | ||
Uploads, | ||
Users, | ||
Users | ||
}; | ||
class Discourse { | ||
constructor(userApiKey, baseUrl, apiKey = null) { | ||
this._BASE_URL = baseUrl; | ||
this._USER_API_Key = userApiKey; | ||
constructor(_userApiKey, _baseUrl) { | ||
var _this = this; | ||
// Admin User API | ||
this._API_KEY = apiKey; | ||
let _apiKey = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; | ||
for (let resource in resources) { | ||
this[resource.toLowerCase()] = new resources[resource](this); | ||
} | ||
} | ||
_defineProperty(this, "config", function () { | ||
let { | ||
userApiKey, | ||
baseUrl, | ||
apiUsername, | ||
apiKey | ||
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { | ||
apiUsername: null, | ||
apiKey: null | ||
}; | ||
_this._USER_API_KEY = userApiKey; | ||
_this._BASE_URL = baseUrl; // Admin User API | ||
config = ( | ||
{ userApiKey, baseUrl, apiUsername, apiKey } = { | ||
apiUsername: null, | ||
apiKey: null, | ||
}, | ||
) => { | ||
this._USER_API_KEY = userApiKey; | ||
this._BASE_URL = baseUrl; | ||
_this._API_KEY = apiKey; | ||
_this._API_USERNAME = apiUsername; // If we are using the Admin API then we'll need to include | ||
// the API key and username in each request either as part | ||
// of our URL params or as part of our POST body | ||
// Admin User API | ||
this._API_KEY = apiKey; | ||
this._API_USERNAME = apiUsername; | ||
// If we are using the Admin API then we'll need to include | ||
// the API key and username in each request either as part | ||
// of our URL params or as part of our POST body | ||
this.isUsingAdminAPI = this._API_KEY && this._API_USERNAME; | ||
}; | ||
requestHeaders() { | ||
return { | ||
'User-Agent': `DiscourseJS ${VERSION}`, | ||
...(this._USER_API_KEY ? { 'User-Api-Key': this._USER_API_KEY } : {}), | ||
}; | ||
} | ||
createBody = body => { | ||
return this.isUsingAdminAPI | ||
? createBody({ | ||
...body, | ||
api_key: this._API_KEY, | ||
api_username: this._API_USERNAME, | ||
}) | ||
: createBody(body); | ||
}; | ||
get = ({ path, headers } = {}) => { | ||
return this.request({ | ||
method: 'GET', | ||
headers, | ||
path: this.isUsingAdminAPI | ||
? buildQueryString(path, { | ||
api_key: this._API_KEY, | ||
api_username: this._API_USERNAME, | ||
}) | ||
: path, | ||
_this.isUsingAdminAPI = _this._API_KEY && _this._API_USERNAME; | ||
}); | ||
}; | ||
post = ({ path, headers, body }) => { | ||
return this.request({ | ||
method: 'POST', | ||
headers, | ||
path, | ||
body: this.createBody(body), | ||
_defineProperty(this, "createBody", body => { | ||
return this.isUsingAdminAPI ? createBody(_objectSpread2(_objectSpread2({}, body), {}, { | ||
api_key: this._API_KEY, | ||
api_username: this._API_USERNAME | ||
})) : createBody(body); | ||
}); | ||
}; | ||
put({ path, headers, body }) { | ||
return this.request({ | ||
method: 'PUT', | ||
headers, | ||
path, | ||
body: this.createBody(body), | ||
_defineProperty(this, "get", function () { | ||
let { | ||
path, | ||
headers | ||
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return _this.request({ | ||
method: 'GET', | ||
headers, | ||
path: _this.isUsingAdminAPI ? buildQueryString(path, { | ||
api_key: _this._API_KEY, | ||
api_username: _this._API_USERNAME | ||
}) : path | ||
}); | ||
}); | ||
} | ||
delete({ path, headers, body }) { | ||
return this.request({ | ||
method: 'DELETE', | ||
headers, | ||
path, | ||
body: this.createBody(body), | ||
_defineProperty(this, "post", (_ref) => { | ||
let { | ||
path, | ||
headers, | ||
body | ||
} = _ref; | ||
return this.request({ | ||
method: 'POST', | ||
headers, | ||
path, | ||
body: this.createBody(body) | ||
}); | ||
}); | ||
} | ||
request = options => { | ||
const { body, method, path, headers } = options; | ||
_defineProperty(this, "request", options => { | ||
const { | ||
body, | ||
method, | ||
path, | ||
headers | ||
} = options; | ||
const fetchOptions = { | ||
method, | ||
headers: _objectSpread2(_objectSpread2({}, this.requestHeaders()), headers), | ||
body, | ||
mimeType: 'multipart/form-data' | ||
}; | ||
return fetch("".concat(this._BASE_URL, "/").concat(path), fetchOptions).then(response => { | ||
const contentType = response.headers.get('content-type'); | ||
const fetchOptions = { | ||
method, | ||
headers: { | ||
...this.requestHeaders(), | ||
...headers, | ||
}, | ||
body, | ||
mimeType: 'multipart/form-data', | ||
}; | ||
return fetch(`${this._BASE_URL}/${path}`, fetchOptions) | ||
.then(response => { | ||
const contentType = response.headers.get('content-type'); | ||
if (response.ok) { | ||
@@ -479,3 +628,7 @@ if (contentType && contentType.indexOf('application/json') !== -1) { | ||
} else { | ||
const { status, statusText } = response; | ||
const { | ||
status, | ||
statusText | ||
} = response; | ||
if (contentType && contentType.indexOf('application/json') !== -1) { | ||
@@ -491,10 +644,56 @@ return response.json().then(json => { | ||
} | ||
}) | ||
.catch(error => { | ||
}).catch(error => { | ||
if (error instanceof ApiError) throw error; | ||
throw new Error(error); | ||
}); | ||
}; | ||
}); | ||
this._BASE_URL = _baseUrl; | ||
this._USER_API_Key = _userApiKey; // Admin User API | ||
this._API_KEY = _apiKey; | ||
for (let resource in resources) { | ||
this[resource.toLowerCase()] = new resources[resource](this); | ||
} | ||
} | ||
requestHeaders() { | ||
return _objectSpread2({ | ||
'User-Agent': "DiscourseJS ".concat(VERSION) | ||
}, this._USER_API_KEY ? { | ||
'User-Api-Key': this._USER_API_KEY | ||
} : {}); | ||
} | ||
put(_ref2) { | ||
let { | ||
path, | ||
headers, | ||
body | ||
} = _ref2; | ||
return this.request({ | ||
method: 'PUT', | ||
headers, | ||
path, | ||
body: this.createBody(body) | ||
}); | ||
} | ||
delete(_ref3) { | ||
let { | ||
path, | ||
headers, | ||
body | ||
} = _ref3; | ||
return this.request({ | ||
method: 'DELETE', | ||
headers, | ||
path, | ||
body: this.createBody(body) | ||
}); | ||
} | ||
} | ||
export default Discourse; |
@@ -7,5 +7,89 @@ (function (global, factory) { | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function ownKeys(object, enumerableOnly) { | ||
var keys = Object.keys(object); | ||
if (Object.getOwnPropertySymbols) { | ||
var symbols = Object.getOwnPropertySymbols(object); | ||
if (enumerableOnly) symbols = symbols.filter(function (sym) { | ||
return Object.getOwnPropertyDescriptor(object, sym).enumerable; | ||
}); | ||
keys.push.apply(keys, symbols); | ||
} | ||
return keys; | ||
} | ||
function _objectSpread2(target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
if (i % 2) { | ||
ownKeys(Object(source), true).forEach(function (key) { | ||
_defineProperty(target, key, source[key]); | ||
}); | ||
} else if (Object.getOwnPropertyDescriptors) { | ||
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); | ||
} else { | ||
ownKeys(Object(source)).forEach(function (key) { | ||
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); | ||
}); | ||
} | ||
} | ||
return target; | ||
} | ||
function _objectWithoutPropertiesLoose(source, excluded) { | ||
if (source == null) return {}; | ||
var target = {}; | ||
var sourceKeys = Object.keys(source); | ||
var key, i; | ||
for (i = 0; i < sourceKeys.length; i++) { | ||
key = sourceKeys[i]; | ||
if (excluded.indexOf(key) >= 0) continue; | ||
target[key] = source[key]; | ||
} | ||
return target; | ||
} | ||
function _objectWithoutProperties(source, excluded) { | ||
if (source == null) return {}; | ||
var target = _objectWithoutPropertiesLoose(source, excluded); | ||
var key, i; | ||
if (Object.getOwnPropertySymbols) { | ||
var sourceSymbolKeys = Object.getOwnPropertySymbols(source); | ||
for (i = 0; i < sourceSymbolKeys.length; i++) { | ||
key = sourceSymbolKeys[i]; | ||
if (excluded.indexOf(key) >= 0) continue; | ||
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; | ||
target[key] = source[key]; | ||
} | ||
} | ||
return target; | ||
} | ||
const createBody = params => { | ||
const form = new FormData(); | ||
Object.keys(params).forEach(paramKey => { | ||
@@ -16,3 +100,3 @@ const paramValue = params[paramKey]; | ||
paramValue.forEach(param => { | ||
form.append(`${paramKey}[]`, param); | ||
form.append("".concat(paramKey, "[]"), param); | ||
}); | ||
@@ -25,65 +109,66 @@ } else { | ||
}; | ||
const buildQueryString = (uri, params) => { | ||
const queryString = Object.keys(params) | ||
.map(key => { | ||
const value = params[key]; | ||
const queryString = Object.keys(params).map(key => { | ||
const value = params[key]; | ||
if (Array.isArray(value)) { | ||
return value.map(sub => `${key}[]=${sub}`).join('&'); | ||
} | ||
if (Array.isArray(value)) { | ||
return value.map(sub => "".concat(key, "[]=").concat(sub)).join('&'); | ||
} | ||
return `${key}=${params[key]}`; | ||
}) | ||
.join('&'); | ||
const separator = !!queryString ? (uri.indexOf('?') !== -1 ? '&' : '?') : ''; | ||
return `${uri}${separator}${queryString}`; | ||
return "".concat(key, "=").concat(params[key]); | ||
}).join('&'); | ||
const separator = !!queryString ? uri.indexOf('?') !== -1 ? '&' : '?' : ''; | ||
return "".concat(uri).concat(separator).concat(queryString); | ||
}; | ||
class ApiError extends Error { | ||
constructor(status, statusText, error, errorArray = []) { | ||
constructor(status, statusText, error) { | ||
let errorArray = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : []; | ||
super(); | ||
const errorMessage = error || errorArray.join(', '); | ||
this.name = 'ApiError'; | ||
this.status = status; | ||
this.statusText = statusText; | ||
this.message = `${status} - ${statusText || errorMessage}`; | ||
this.message = "".concat(status, " - ").concat(statusText || errorMessage); | ||
this.error = errorMessage; | ||
} | ||
} | ||
function Categories(discourse) { | ||
this.getCategory = async ( | ||
{ cat_id, latest, ...inputs } = { latest: false }, | ||
) => { | ||
this.getCategory = async function () { | ||
let _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { | ||
latest: false | ||
}, | ||
{ | ||
cat_id, | ||
latest | ||
} = _ref, | ||
inputs = _objectWithoutProperties(_ref, ["cat_id", "latest"]); | ||
if (!cat_id) { | ||
throw new Error( | ||
'No id defined. You must pass an `cat_id` to the getCategory function.', | ||
); | ||
throw new Error('No id defined. You must pass an `cat_id` to the getCategory function.'); | ||
} | ||
return discourse.get({ | ||
path: buildQueryString( | ||
`c/${cat_id}${latest ? '/l/latest' : ''}.json`, | ||
inputs, | ||
), | ||
path: buildQueryString("c/".concat(cat_id).concat(latest ? '/l/latest' : '', ".json"), inputs) | ||
}); | ||
}; | ||
this.getSubcategory = async ( | ||
{ cat_id, subcat_id, latest, ...inputs } = { latest: false }, | ||
) => { | ||
this.getSubcategory = async function () { | ||
let _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { | ||
latest: false | ||
}, | ||
{ | ||
cat_id, | ||
subcat_id, | ||
latest | ||
} = _ref2, | ||
inputs = _objectWithoutProperties(_ref2, ["cat_id", "subcat_id", "latest"]); | ||
if (!cat_id || !subcat_id) { | ||
throw new Error( | ||
'No id defined. You must pass an id to the getSubcategory function.', | ||
); | ||
throw new Error('No id defined. You must pass an id to the getSubcategory function.'); | ||
} | ||
return discourse.get({ | ||
path: buildQueryString( | ||
`c/${cat_id}/${subcat_id}${latest ? '/l/latest' : ''}.json`, | ||
inputs, | ||
), | ||
path: buildQueryString("c/".concat(cat_id, "/").concat(subcat_id).concat(latest ? '/l/latest' : '', ".json"), inputs) | ||
}); | ||
@@ -94,5 +179,8 @@ }; | ||
function Groups(discourse) { | ||
this.getMembers = async ({ group_name }) => { | ||
this.getMembers = async (_ref) => { | ||
let { | ||
group_name | ||
} = _ref; | ||
return discourse.get({ | ||
path: `groups/${group_name}/members.json`, | ||
path: "groups/".concat(group_name, "/members.json") | ||
}); | ||
@@ -105,12 +193,15 @@ }; | ||
return discourse.get({ | ||
path: `topics/private-messages/${discourse._API_USERNAME}.json`, | ||
path: "topics/private-messages/".concat(discourse._API_USERNAME, ".json") | ||
}); | ||
}; | ||
this.getGroupMessages = async ({ group_name }) => { | ||
this.getGroupMessages = async (_ref) => { | ||
let { | ||
group_name | ||
} = _ref; | ||
return discourse.get({ | ||
path: `topics/private-messages-group/${discourse._API_USERNAME}/${group_name}.json`, | ||
path: "topics/private-messages-group/".concat(discourse._API_USERNAME, "/").concat(group_name, ".json"), | ||
headers: { | ||
Accept: 'application/json', | ||
}, | ||
Accept: 'application/json' | ||
} | ||
}); | ||
@@ -121,3 +212,3 @@ }; | ||
return discourse.get({ | ||
path: `topics/private-messages-sent/${discourse._API_USERNAME}.json`, | ||
path: "topics/private-messages-sent/".concat(discourse._API_USERNAME, ".json") | ||
}); | ||
@@ -129,7 +220,10 @@ }; | ||
const getSentMessages = this.getSentMessages(); | ||
return Promise.all([getMessages, getSentMessages]); | ||
}; | ||
this.send = async ({ topic_id, raw }) => { | ||
this.send = async (_ref2) => { | ||
let { | ||
topic_id, | ||
raw | ||
} = _ref2; | ||
return discourse.post({ | ||
@@ -140,4 +234,4 @@ path: 'posts', | ||
raw, | ||
archetype: 'private_message', | ||
}, | ||
archetype: 'private_message' | ||
} | ||
}); | ||
@@ -148,20 +242,21 @@ }; | ||
function Notifications(discourse) { | ||
this.get = async (inputs = {}) => { | ||
this.get = async function () { | ||
let inputs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return discourse.get({ | ||
method: 'GET', | ||
path: buildQueryString('notifications.json', inputs), | ||
path: buildQueryString('notifications.json', inputs) | ||
}); | ||
}; | ||
this.markRead = async ({ id }) => { | ||
return discourse.put({ | ||
path: 'notifications/mark-read', | ||
...(id | ||
? { | ||
body: { | ||
id, | ||
}, | ||
} | ||
: {}), | ||
}); | ||
this.markRead = async (_ref) => { | ||
let { | ||
id | ||
} = _ref; | ||
return discourse.put(_objectSpread2({ | ||
path: 'notifications/mark-read' | ||
}, id ? { | ||
body: { | ||
id | ||
} | ||
} : {})); | ||
}; | ||
@@ -171,29 +266,32 @@ } | ||
function Posts(discourse) { | ||
this.create = async (inputs = {}) => { | ||
this.create = async function () { | ||
let inputs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
// If an imageUri has been passed, upload the image first. | ||
if (inputs.imageUri) { | ||
const { url, width, height, short_url } = await discourse.uploads.create({ | ||
const { | ||
url, | ||
width, | ||
height, | ||
short_url | ||
} = await discourse.uploads.create({ | ||
'files[]': { | ||
uri: inputs.imageUri, | ||
name: 'photo.jpeg', | ||
type: 'image/jpeg', | ||
type: 'image/jpeg' | ||
}, | ||
type: 'composer', | ||
synchronous: true, | ||
synchronous: true | ||
}); | ||
if (url) { | ||
const body = {}; | ||
const body = {}; // Remove the imageUri from the inputs as it's not used in the next request. | ||
// Remove the imageUri from the inputs as it's not used in the next request. | ||
delete inputs.imageUri; | ||
Object.keys(inputs).forEach(key => body[key] = inputs[key]); // Prepend the raw message with the image. | ||
Object.keys(inputs).forEach(key => (body[key] = inputs[key])); | ||
// Prepend the raw message with the image. | ||
body.raw = `![${width}x${height}](${short_url})\n${body.raw}`; | ||
body.raw = "![".concat(width, "x").concat(height, "](").concat(short_url, ")\n").concat(body.raw); | ||
return discourse.post({ | ||
path: 'posts', | ||
body, | ||
body | ||
}); | ||
@@ -204,3 +302,3 @@ } | ||
path: 'posts', | ||
body: inputs, | ||
body: inputs | ||
}); | ||
@@ -210,7 +308,11 @@ } | ||
this.reply = async ({ topic_id, raw, reply_to_post_number }) => { | ||
this.reply = async (_ref) => { | ||
let { | ||
topic_id, | ||
raw, | ||
reply_to_post_number | ||
} = _ref; | ||
if (!topic_id) { | ||
throw new Error( | ||
'No topic_id defined. You must pass a topic to reply function.', | ||
); | ||
throw new Error('No topic_id defined. You must pass a topic to reply function.'); | ||
} | ||
@@ -225,7 +327,6 @@ | ||
archetype: 'regular', | ||
nested_post: true, | ||
}, | ||
nested_post: true | ||
} | ||
}); | ||
}; | ||
/** | ||
@@ -241,27 +342,71 @@ * post_action_type_id values | ||
*/ | ||
this.postAction = async ({ method = 'post', body = {}, id = null }) => { | ||
this.postAction = async (_ref2) => { | ||
let { | ||
method = 'post', | ||
body = {}, | ||
id = null | ||
} = _ref2; | ||
return discourse[method]({ | ||
path: id ? `post_actions/${id}` : 'post_actions', | ||
body, | ||
path: id ? "post_actions/".concat(id) : 'post_actions', | ||
body | ||
}); | ||
}; | ||
this.like = ({ id }) => | ||
this.postAction({ body: { id, post_action_type_id: 2 } }); | ||
this.like = (_ref3) => { | ||
let { | ||
id | ||
} = _ref3; | ||
return this.postAction({ | ||
body: { | ||
id, | ||
post_action_type_id: 2 | ||
} | ||
}); | ||
}; | ||
this.unlike = ({ id }) => | ||
this.postAction({ method: 'delete', body: { post_action_type_id: 2 }, id }); | ||
this.unlike = (_ref4) => { | ||
let { | ||
id | ||
} = _ref4; | ||
return this.postAction({ | ||
method: 'delete', | ||
body: { | ||
post_action_type_id: 2 | ||
}, | ||
id | ||
}); | ||
}; | ||
this.flag = ({ id, post_action_type_id, message, flag_topic }) => | ||
this.postAction({ body: { id, post_action_type_id, message, flag_topic } }); | ||
this.flag = (_ref5) => { | ||
let { | ||
id, | ||
post_action_type_id, | ||
message, | ||
flag_topic | ||
} = _ref5; | ||
return this.postAction({ | ||
body: { | ||
id, | ||
post_action_type_id, | ||
message, | ||
flag_topic | ||
} | ||
}); | ||
}; | ||
} | ||
function Preferences(discourse) { | ||
this.pickAvatar = async ({ username, upload_id } = {}) => { | ||
this.pickAvatar = async function () { | ||
let { | ||
username, | ||
upload_id | ||
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return discourse.put({ | ||
path: `u/${username}/preferences/avatar/pick`, | ||
path: "u/".concat(username, "/preferences/avatar/pick"), | ||
body: { | ||
upload_id, | ||
type: 'uploaded', | ||
}, | ||
type: 'uploaded' | ||
} | ||
}); | ||
@@ -272,19 +417,25 @@ }; | ||
function Tags(discourse) { | ||
this.getTopic = async ({ tag, ...inputs } = {}) => { | ||
this.getTopic = async function () { | ||
let _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
{ | ||
tag | ||
} = _ref, | ||
inputs = _objectWithoutProperties(_ref, ["tag"]); | ||
return discourse.get({ | ||
path: buildQueryString(`tags/${tag}.json`, inputs), | ||
path: buildQueryString("tags/".concat(tag, ".json"), inputs) | ||
}); | ||
}; | ||
this.getTopicsForCategory = async ({ | ||
tag, | ||
category, | ||
subcategory, | ||
...inputs | ||
} = {}) => { | ||
this.getTopicsForCategory = async function () { | ||
let _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
{ | ||
tag, | ||
category, | ||
subcategory | ||
} = _ref2, | ||
inputs = _objectWithoutProperties(_ref2, ["tag", "category", "subcategory"]); | ||
return discourse.get({ | ||
path: buildQueryString( | ||
`tags/c/${category}/${subcategory ? `${subcategory}/` : ''}${tag}.json`, | ||
inputs, | ||
), | ||
path: buildQueryString("tags/c/".concat(category, "/").concat(subcategory ? "".concat(subcategory, "/") : '').concat(tag, ".json"), inputs) | ||
}); | ||
@@ -295,28 +446,52 @@ }; | ||
function Topics(discourse) { | ||
this.getTopic = async ({ id, reverse, ...inputs } = {}) => { | ||
this.getTopic = async function () { | ||
let _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
{ | ||
id, | ||
reverse | ||
} = _ref, | ||
inputs = _objectWithoutProperties(_ref, ["id", "reverse"]); | ||
return discourse.get({ | ||
path: buildQueryString(`t/${id}${reverse ? '/last' : ''}.json`, inputs), | ||
path: buildQueryString("t/".concat(id).concat(reverse ? '/last' : '', ".json"), inputs) | ||
}); | ||
}; | ||
this.getTopicPosts = async ({ id, posts, ...inputs } = {}) => { | ||
const params = { | ||
post_ids: posts, | ||
...inputs, | ||
}; | ||
this.getTopicPosts = async function () { | ||
let _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
{ | ||
id, | ||
posts | ||
} = _ref2, | ||
inputs = _objectWithoutProperties(_ref2, ["id", "posts"]); | ||
const params = _objectSpread2({ | ||
post_ids: posts | ||
}, inputs); | ||
return discourse.get({ | ||
path: buildQueryString(`t/${id}/posts.json`, params), | ||
path: buildQueryString("t/".concat(id, "/posts.json"), params) | ||
}); | ||
}; | ||
this.deleteTopic = async ({ id, ...inputs } = {}) => { | ||
this.deleteTopic = async function () { | ||
let _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
{ | ||
id | ||
} = _ref3, | ||
inputs = _objectWithoutProperties(_ref3, ["id"]); | ||
return discourse.delete({ | ||
path: buildQueryString(`t/${id}`, inputs), | ||
path: buildQueryString("t/".concat(id), inputs) | ||
}); | ||
}; | ||
this.getTopicsByUsername = async ({ username, ...inputs }) => { | ||
this.getTopicsByUsername = async (_ref4) => { | ||
let { | ||
username | ||
} = _ref4, | ||
inputs = _objectWithoutProperties(_ref4, ["username"]); | ||
return discourse.get({ | ||
path: buildQueryString(`topics/created-by/${username}.json`, inputs), | ||
path: buildQueryString("topics/created-by/".concat(username, ".json"), inputs) | ||
}); | ||
@@ -331,6 +506,7 @@ }; | ||
function Uploads(discourse) { | ||
this.create = async (body = {}) => { | ||
this.create = async function () { | ||
let body = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return discourse.post({ | ||
path: 'uploads.json', | ||
body, | ||
body | ||
}); | ||
@@ -341,9 +517,9 @@ }; | ||
function Users(discourse) { | ||
this.getUser = async ({ username } = {}) => { | ||
return discourse | ||
.get({ | ||
path: `users/${username}.json`, | ||
}) | ||
.then(response => response) | ||
.catch(error => error); | ||
this.getUser = async function () { | ||
let { | ||
username | ||
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return discourse.get({ | ||
path: "users/".concat(username, ".json") | ||
}).then(response => response).catch(error => error); | ||
}; | ||
@@ -364,110 +540,83 @@ } | ||
Uploads, | ||
Users, | ||
Users | ||
}; | ||
class Discourse { | ||
constructor(userApiKey, baseUrl, apiKey = null) { | ||
this._BASE_URL = baseUrl; | ||
this._USER_API_Key = userApiKey; | ||
constructor(_userApiKey, _baseUrl) { | ||
var _this = this; | ||
// Admin User API | ||
this._API_KEY = apiKey; | ||
let _apiKey = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; | ||
for (let resource in resources) { | ||
this[resource.toLowerCase()] = new resources[resource](this); | ||
} | ||
} | ||
_defineProperty(this, "config", function () { | ||
let { | ||
userApiKey, | ||
baseUrl, | ||
apiUsername, | ||
apiKey | ||
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { | ||
apiUsername: null, | ||
apiKey: null | ||
}; | ||
_this._USER_API_KEY = userApiKey; | ||
_this._BASE_URL = baseUrl; // Admin User API | ||
config = ( | ||
{ userApiKey, baseUrl, apiUsername, apiKey } = { | ||
apiUsername: null, | ||
apiKey: null, | ||
}, | ||
) => { | ||
this._USER_API_KEY = userApiKey; | ||
this._BASE_URL = baseUrl; | ||
_this._API_KEY = apiKey; | ||
_this._API_USERNAME = apiUsername; // If we are using the Admin API then we'll need to include | ||
// the API key and username in each request either as part | ||
// of our URL params or as part of our POST body | ||
// Admin User API | ||
this._API_KEY = apiKey; | ||
this._API_USERNAME = apiUsername; | ||
// If we are using the Admin API then we'll need to include | ||
// the API key and username in each request either as part | ||
// of our URL params or as part of our POST body | ||
this.isUsingAdminAPI = this._API_KEY && this._API_USERNAME; | ||
}; | ||
requestHeaders() { | ||
return { | ||
'User-Agent': `DiscourseJS ${VERSION}`, | ||
...(this._USER_API_KEY ? { 'User-Api-Key': this._USER_API_KEY } : {}), | ||
}; | ||
} | ||
createBody = body => { | ||
return this.isUsingAdminAPI | ||
? createBody({ | ||
...body, | ||
api_key: this._API_KEY, | ||
api_username: this._API_USERNAME, | ||
}) | ||
: createBody(body); | ||
}; | ||
get = ({ path, headers } = {}) => { | ||
return this.request({ | ||
method: 'GET', | ||
headers, | ||
path: this.isUsingAdminAPI | ||
? buildQueryString(path, { | ||
api_key: this._API_KEY, | ||
api_username: this._API_USERNAME, | ||
}) | ||
: path, | ||
_this.isUsingAdminAPI = _this._API_KEY && _this._API_USERNAME; | ||
}); | ||
}; | ||
post = ({ path, headers, body }) => { | ||
return this.request({ | ||
method: 'POST', | ||
headers, | ||
path, | ||
body: this.createBody(body), | ||
_defineProperty(this, "createBody", body => { | ||
return this.isUsingAdminAPI ? createBody(_objectSpread2(_objectSpread2({}, body), {}, { | ||
api_key: this._API_KEY, | ||
api_username: this._API_USERNAME | ||
})) : createBody(body); | ||
}); | ||
}; | ||
put({ path, headers, body }) { | ||
return this.request({ | ||
method: 'PUT', | ||
headers, | ||
path, | ||
body: this.createBody(body), | ||
_defineProperty(this, "get", function () { | ||
let { | ||
path, | ||
headers | ||
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return _this.request({ | ||
method: 'GET', | ||
headers, | ||
path: _this.isUsingAdminAPI ? buildQueryString(path, { | ||
api_key: _this._API_KEY, | ||
api_username: _this._API_USERNAME | ||
}) : path | ||
}); | ||
}); | ||
} | ||
delete({ path, headers, body }) { | ||
return this.request({ | ||
method: 'DELETE', | ||
headers, | ||
path, | ||
body: this.createBody(body), | ||
_defineProperty(this, "post", (_ref) => { | ||
let { | ||
path, | ||
headers, | ||
body | ||
} = _ref; | ||
return this.request({ | ||
method: 'POST', | ||
headers, | ||
path, | ||
body: this.createBody(body) | ||
}); | ||
}); | ||
} | ||
request = options => { | ||
const { body, method, path, headers } = options; | ||
_defineProperty(this, "request", options => { | ||
const { | ||
body, | ||
method, | ||
path, | ||
headers | ||
} = options; | ||
const fetchOptions = { | ||
method, | ||
headers: _objectSpread2(_objectSpread2({}, this.requestHeaders()), headers), | ||
body, | ||
mimeType: 'multipart/form-data' | ||
}; | ||
return fetch("".concat(this._BASE_URL, "/").concat(path), fetchOptions).then(response => { | ||
const contentType = response.headers.get('content-type'); | ||
const fetchOptions = { | ||
method, | ||
headers: { | ||
...this.requestHeaders(), | ||
...headers, | ||
}, | ||
body, | ||
mimeType: 'multipart/form-data', | ||
}; | ||
return fetch(`${this._BASE_URL}/${path}`, fetchOptions) | ||
.then(response => { | ||
const contentType = response.headers.get('content-type'); | ||
if (response.ok) { | ||
@@ -486,3 +635,7 @@ if (contentType && contentType.indexOf('application/json') !== -1) { | ||
} else { | ||
const { status, statusText } = response; | ||
const { | ||
status, | ||
statusText | ||
} = response; | ||
if (contentType && contentType.indexOf('application/json') !== -1) { | ||
@@ -498,8 +651,54 @@ return response.json().then(json => { | ||
} | ||
}) | ||
.catch(error => { | ||
}).catch(error => { | ||
if (error instanceof ApiError) throw error; | ||
throw new Error(error); | ||
}); | ||
}; | ||
}); | ||
this._BASE_URL = _baseUrl; | ||
this._USER_API_Key = _userApiKey; // Admin User API | ||
this._API_KEY = _apiKey; | ||
for (let resource in resources) { | ||
this[resource.toLowerCase()] = new resources[resource](this); | ||
} | ||
} | ||
requestHeaders() { | ||
return _objectSpread2({ | ||
'User-Agent': "DiscourseJS ".concat(VERSION) | ||
}, this._USER_API_KEY ? { | ||
'User-Api-Key': this._USER_API_KEY | ||
} : {}); | ||
} | ||
put(_ref2) { | ||
let { | ||
path, | ||
headers, | ||
body | ||
} = _ref2; | ||
return this.request({ | ||
method: 'PUT', | ||
headers, | ||
path, | ||
body: this.createBody(body) | ||
}); | ||
} | ||
delete(_ref3) { | ||
let { | ||
path, | ||
headers, | ||
body | ||
} = _ref3; | ||
return this.request({ | ||
method: 'DELETE', | ||
headers, | ||
path, | ||
body: this.createBody(body) | ||
}); | ||
} | ||
} | ||
@@ -506,0 +705,0 @@ |
{ | ||
"name": "discourse-js", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"description": "A client-side javascript wrapper for the discourse API.", | ||
@@ -35,2 +35,5 @@ "main": "dist/index.cjs.js", | ||
"@babel/core": "^7.8.7", | ||
"@babel/plugin-proposal-class-properties": "^7.8.3", | ||
"@babel/preset-env": "^7.8.7", | ||
"@rollup/plugin-babel": "^5.2.0", | ||
"@rollup/plugin-commonjs": "^15.0.0", | ||
@@ -37,0 +40,0 @@ "@rollup/plugin-node-resolve": "^9.0.0", |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
71858
2222
4
15