UNPKG

@cutls/megalodon

Version:

Mastodon, Pleroma, Misskey API client for node.js and browser

684 lines (683 loc) 18.3 kB
var __awaiter = (this && this.__awaiter) || ((thisArg, _arguments, P, generator) => { function adopt(value) { return value instanceof P ? value : new P((resolve) => { resolve(value) }) } return new (P || (P = Promise))((resolve, reject) => { function fulfilled(value) { try { step(generator.next(value)) } catch (e) { reject(e) } } function rejected(value) { try { step(generator['throw'](value)) } catch (e) { reject(e) } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected) } step((generator = generator.apply(thisArg, _arguments || [])).next()) }) }) var __generator = (this && this.__generator) || ((thisArg, body) => { var _ = { label: 0, sent: () => { if (t[0] & 1) throw t[1] return t[1] }, trys: [], ops: [] }, f, y, t, g return ( (g = { next: verb(0), throw: verb(1), return: verb(2) }), typeof Symbol === 'function' && (g[Symbol.iterator] = function () { return this }), g ) function verb(n) { return (v) => step([n, v]) } function step(op) { if (f) throw new TypeError('Generator is already executing.') while ((g && ((g = 0), op[0] && (_ = 0)), _)) try { if (((f = 1), y && (t = op[0] & 2 ? y['return'] : op[0] ? y['throw'] || ((t = y['return']) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done)) return t if (((y = 0), t)) op = [op[0] & 2, t.value] switch (op[0]) { case 0: case 1: t = op break case 4: _.label++ return { value: op[1], done: false } case 5: _.label++ y = op[1] op = [0] continue case 7: op = _.ops.pop() _.trys.pop() continue default: if (!((t = _.trys), (t = t.length > 0 && t[t.length - 1])) && (op[0] === 6 || op[0] === 2)) { _ = 0 continue } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1] break } if (op[0] === 6 && _.label < t[1]) { _.label = t[1] t = op break } if (t && _.label < t[2]) { _.label = t[2] _.ops.push(op) break } if (t[2]) _.ops.pop() _.trys.pop() continue } op = body.call(thisArg, _) } catch (e) { op = [6, e] y = 0 } finally { f = t = 0 } if (op[0] & 5) throw op[1] return { value: op[0] ? op[1] : void 0, done: true } } }) import axios from 'axios' import objectAssignDeep from 'object-assign-deep' import Streaming from './web_socket' import { RequestCanceledError } from '../cancel' import { NO_REDIRECT, DEFAULT_SCOPE, DEFAULT_UA } from '../default' import NotificationType, { UnknownNotificationTypeError } from '../notification' import MastodonNotificationType from './notification' var MastodonAPI ;((MastodonAPI) => { var Client = (() => { function Client(baseUrl, accessToken, userAgent) { if (accessToken === void 0) { accessToken = null } if (userAgent === void 0) { userAgent = DEFAULT_UA } this.accessToken = accessToken this.baseUrl = baseUrl this.userAgent = userAgent this.abortController = new AbortController() axios.defaults.signal = this.abortController.signal } Client.prototype.get = function (path_1) { return __awaiter(this, arguments, void 0, function (path, params, headers, pathIsFullyQualified) { var options if (params === void 0) { params = {} } if (headers === void 0) { headers = {} } if (pathIsFullyQualified === void 0) { pathIsFullyQualified = false } return __generator(this, function (_a) { options = { params: params, headers: headers, maxContentLength: Infinity, maxBodyLength: Infinity } if (this.accessToken) { options = objectAssignDeep({}, options, { headers: { Authorization: 'Bearer '.concat(this.accessToken) } }) } return [ 2, axios .get((pathIsFullyQualified ? '' : this.baseUrl) + path, options) .catch((err) => { if (axios.isCancel(err)) { throw new RequestCanceledError(err.message) } else { throw err } }) .then((resp) => { var res = { data: resp.data, status: resp.status, statusText: resp.statusText, headers: resp.headers } return res }) ] }) }) } Client.prototype.put = function (path_1) { return __awaiter(this, arguments, void 0, function (path, params, headers) { var options if (params === void 0) { params = {} } if (headers === void 0) { headers = {} } return __generator(this, function (_a) { options = { headers: headers, maxContentLength: Infinity, maxBodyLength: Infinity } if (this.accessToken) { options = objectAssignDeep({}, options, { headers: { Authorization: 'Bearer '.concat(this.accessToken) } }) } return [ 2, axios .put(this.baseUrl + path, params, options) .catch((err) => { if (axios.isCancel(err)) { throw new RequestCanceledError(err.message) } else { throw err } }) .then((resp) => { var res = { data: resp.data, status: resp.status, statusText: resp.statusText, headers: resp.headers } return res }) ] }) }) } Client.prototype.putForm = function (path_1) { return __awaiter(this, arguments, void 0, function (path, params, headers) { var options if (params === void 0) { params = {} } if (headers === void 0) { headers = {} } return __generator(this, function (_a) { options = { headers: headers, maxContentLength: Infinity, maxBodyLength: Infinity } if (this.accessToken) { options = objectAssignDeep({}, options, { headers: { Authorization: 'Bearer '.concat(this.accessToken) } }) } return [ 2, axios .putForm(this.baseUrl + path, params, options) .catch((err) => { if (axios.isCancel(err)) { throw new RequestCanceledError(err.message) } else { throw err } }) .then((resp) => { var res = { data: resp.data, status: resp.status, statusText: resp.statusText, headers: resp.headers } return res }) ] }) }) } Client.prototype.patch = function (path_1) { return __awaiter(this, arguments, void 0, function (path, params, headers) { var options if (params === void 0) { params = {} } if (headers === void 0) { headers = {} } return __generator(this, function (_a) { options = { headers: headers, maxContentLength: Infinity, maxBodyLength: Infinity } if (this.accessToken) { options = objectAssignDeep({}, options, { headers: { Authorization: 'Bearer '.concat(this.accessToken) } }) } return [ 2, axios .patch(this.baseUrl + path, params, options) .catch((err) => { if (axios.isCancel(err)) { throw new RequestCanceledError(err.message) } else { throw err } }) .then((resp) => { var res = { data: resp.data, status: resp.status, statusText: resp.statusText, headers: resp.headers } return res }) ] }) }) } Client.prototype.patchForm = function (path_1) { return __awaiter(this, arguments, void 0, function (path, params, headers) { var options if (params === void 0) { params = {} } if (headers === void 0) { headers = {} } return __generator(this, function (_a) { options = { headers: headers, maxContentLength: Infinity, maxBodyLength: Infinity } if (this.accessToken) { options = objectAssignDeep({}, options, { headers: { Authorization: 'Bearer '.concat(this.accessToken) } }) } return [ 2, axios .patchForm(this.baseUrl + path, params, options) .catch((err) => { if (axios.isCancel(err)) { throw new RequestCanceledError(err.message) } else { throw err } }) .then((resp) => { var res = { data: resp.data, status: resp.status, statusText: resp.statusText, headers: resp.headers } return res }) ] }) }) } Client.prototype.post = function (path_1) { return __awaiter(this, arguments, void 0, function (path, params, headers) { var options if (params === void 0) { params = {} } if (headers === void 0) { headers = {} } return __generator(this, function (_a) { options = { headers: headers, maxContentLength: Infinity, maxBodyLength: Infinity } if (this.accessToken) { options = objectAssignDeep({}, options, { headers: { Authorization: 'Bearer '.concat(this.accessToken) } }) } return [ 2, axios.post(this.baseUrl + path, params, options).then((resp) => { var res = { data: resp.data, status: resp.status, statusText: resp.statusText, headers: resp.headers } return res }) ] }) }) } Client.prototype.postForm = function (path_1) { return __awaiter(this, arguments, void 0, function (path, params, headers) { var options if (params === void 0) { params = {} } if (headers === void 0) { headers = {} } return __generator(this, function (_a) { options = { headers: headers, maxContentLength: Infinity, maxBodyLength: Infinity } if (this.accessToken) { options = objectAssignDeep({}, options, { headers: { Authorization: 'Bearer '.concat(this.accessToken) } }) } return [ 2, axios.postForm(this.baseUrl + path, params, options).then((resp) => { var res = { data: resp.data, status: resp.status, statusText: resp.statusText, headers: resp.headers } return res }) ] }) }) } Client.prototype.del = function (path_1) { return __awaiter(this, arguments, void 0, function (path, params, headers) { var options if (params === void 0) { params = {} } if (headers === void 0) { headers = {} } return __generator(this, function (_a) { options = { data: params, headers: headers, maxContentLength: Infinity, maxBodyLength: Infinity } if (this.accessToken) { options = objectAssignDeep({}, options, { headers: { Authorization: 'Bearer '.concat(this.accessToken) } }) } return [ 2, axios .delete(this.baseUrl + path, options) .catch((err) => { if (axios.isCancel(err)) { throw new RequestCanceledError(err.message) } else { throw err } }) .then((resp) => { var res = { data: resp.data, status: resp.status, statusText: resp.statusText, headers: resp.headers } return res }) ] }) }) } Client.prototype.cancel = function () { return this.abortController.abort() } Client.prototype.socket = function (url, stream, params) { if (!this.accessToken) { throw new Error('accessToken is required') } var streaming = new Streaming(url, stream, params, this.accessToken, this.userAgent) streaming.start() return streaming } Client.DEFAULT_SCOPE = DEFAULT_SCOPE Client.DEFAULT_URL = 'https://mastodon.social' Client.NO_REDIRECT = NO_REDIRECT return Client })() MastodonAPI.Client = Client var Converter ;((Converter) => { Converter.encodeNotificationType = (t) => { switch (t) { case NotificationType.Follow: return MastodonNotificationType.Follow case NotificationType.Favourite: return MastodonNotificationType.Favourite case NotificationType.Reblog: return MastodonNotificationType.Reblog case NotificationType.Mention: return MastodonNotificationType.Mention case NotificationType.FollowRequest: return MastodonNotificationType.FollowRequest case NotificationType.Status: return MastodonNotificationType.Status case NotificationType.PollExpired: return MastodonNotificationType.Poll case NotificationType.Update: return MastodonNotificationType.Update case NotificationType.AdminSignup: return MastodonNotificationType.AdminSignup case NotificationType.AdminReport: return MastodonNotificationType.AdminReport default: return new UnknownNotificationTypeError() } } Converter.decodeNotificationType = (t) => { switch (t) { case MastodonNotificationType.Follow: return NotificationType.Follow case MastodonNotificationType.Favourite: return NotificationType.Favourite case MastodonNotificationType.Mention: return NotificationType.Mention case MastodonNotificationType.Reblog: return NotificationType.Reblog case MastodonNotificationType.FollowRequest: return NotificationType.FollowRequest case MastodonNotificationType.Status: return NotificationType.Status case MastodonNotificationType.Poll: return NotificationType.PollExpired case MastodonNotificationType.Update: return NotificationType.Update case MastodonNotificationType.AdminSignup: return NotificationType.AdminSignup case MastodonNotificationType.AdminReport: return NotificationType.AdminReport default: return new UnknownNotificationTypeError() } } Converter.account = (a) => a Converter.activity = (a) => a Converter.announcement = (a) => a Converter.application = (a) => a Converter.attachment = (a) => a Converter.async_attachment = (a) => { if (a.url) { return { id: a.id, type: a.type, url: a.url, remote_url: a.remote_url, preview_url: a.preview_url, text_url: a.text_url, meta: a.meta, description: a.description, blurhash: a.blurhash } } else { return a } } Converter.card = (c) => c Converter.context = (c) => ({ ancestors: Array.isArray(c.ancestors) ? c.ancestors.map((a) => Converter.status(a)) : [], descendants: Array.isArray(c.descendants) ? c.descendants.map((d) => Converter.status(d)) : [] }) Converter.conversation = (c) => ({ id: c.id, accounts: Array.isArray(c.accounts) ? c.accounts.map((a) => Converter.account(a)) : [], last_status: c.last_status ? Converter.status(c.last_status) : null, unread: c.unread }) Converter.emoji = (e) => e Converter.featured_tag = (e) => e Converter.field = (f) => f Converter.filter = (f) => f Converter.history = (h) => h Converter.identity_proof = (i) => i Converter.instance = (i) => i Converter.list = (l) => l Converter.marker = (m) => m Converter.mention = (m) => m Converter.notification = (n) => { var notificationType = Converter.decodeNotificationType(n.type) if (notificationType instanceof UnknownNotificationTypeError) return notificationType if (n.status) { return { account: Converter.account(n.account), created_at: n.created_at, id: n.id, status: Converter.status(n.status), type: notificationType } } else { return { account: Converter.account(n.account), created_at: n.created_at, id: n.id, type: notificationType } } } Converter.poll = (p) => p Converter.poll_option = (p) => p Converter.preferences = (p) => p Converter.push_subscription = (p) => p Converter.relationship = (r) => r Converter.report = (r) => r Converter.results = (r) => ({ accounts: Array.isArray(r.accounts) ? r.accounts.map((a) => Converter.account(a)) : [], statuses: Array.isArray(r.statuses) ? r.statuses.map((s) => Converter.status(s)) : [], hashtags: Array.isArray(r.hashtags) ? r.hashtags.map((h) => Converter.tag(h)) : [] }) Converter.scheduled_status = (s) => s Converter.source = (s) => s Converter.stats = (s) => s Converter.status = (s) => ({ id: s.id, uri: s.uri, url: s.url, account: Converter.account(s.account), in_reply_to_id: s.in_reply_to_id, in_reply_to_account_id: s.in_reply_to_account_id, reblog: s.reblog ? Converter.status(s.reblog) : s.quote ? Converter.status(s.quote) : null, content: s.content, plain_content: null, created_at: s.created_at, edited_at: s.edited_at, emojis: Array.isArray(s.emojis) ? s.emojis.map((e) => Converter.emoji(e)) : [], replies_count: s.replies_count, reblogs_count: s.reblogs_count, favourites_count: s.favourites_count, reblogged: s.reblogged, favourited: s.favourited, muted: s.muted, sensitive: s.sensitive, spoiler_text: s.spoiler_text, visibility: s.visibility, media_attachments: Array.isArray(s.media_attachments) ? s.media_attachments.map((m) => Converter.attachment(m)) : [], mentions: Array.isArray(s.mentions) ? s.mentions.map((m) => Converter.mention(m)) : [], tags: s.tags, card: s.card ? Converter.card(s.card) : null, poll: s.poll ? Converter.poll(s.poll) : null, application: s.application ? Converter.application(s.application) : null, language: s.language, pinned: s.pinned, emoji_reactions: [], bookmarked: s.bookmarked ? s.bookmarked : false, quote: s.quote !== undefined && s.quote !== null }) Converter.status_params = (s) => s Converter.status_source = (s) => s Converter.tag = (t) => t Converter.token = (t) => t Converter.urls = (u) => u })((Converter = MastodonAPI.Converter || (MastodonAPI.Converter = {}))) })(MastodonAPI || (MastodonAPI = {})) export default MastodonAPI