UNPKG

@flaresocial/api

Version:

API wrapper for the Flare Social API

148 lines 5.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PostsEndpoint = exports.PostEntity = void 0; const endpoint_1 = require("./endpoint"); const index_1 = require("./index"); const entity_1 = require("./entity"); const utils_1 = require("./utils"); class PostEntity extends entity_1.Entity { constructor(api, data) { super(api); this.id = data.id; this.author_id = data.author_id; this.body = data.body; this.reply_to = data.reply_to; this.repost_of = data.repost_of; this.reply_count = data.reply_count; this.repost_count = data.repost_count; this.like_count = data.like_count; this.bookmark_count = data.bookmark_count; this.created_at = data.created_at; this.createdAt = new Date(data.created_at); } async getAuthor() { return await this.api.users.getById(this.author_id); } async delete() { return await this.api.posts.delete(this.id); } async like() { const res = await this.api.request('PUT', `/posts/${this.id}/like`); return res.success; } async unlike() { const res = await this.api.request('DELETE', `/posts/${this.id}/like`); return res.success; } async isLiked() { const res = await this.api.request('GET', `/posts/${this.id}/like`); return res.liked; } async getLikes(limit = 50, page = 0) { const res = await this.api.request('GET', `/posts/${this.id}/likes?limit=${limit}&page=${page}`); return { data: res.data.map((it) => new index_1.UserEntity(this.api, it)), nextPage: res.nextPage, }; } async bookmark() { const res = await this.api.request('PUT', `/posts/${this.id}/bookmark`); return res.success; } async unbookmark() { const res = await this.api.request('DELETE', `/posts/${this.id}/bookmark`); return res.success; } async isBookmarked() { const res = await this.api.request('GET', `/posts/${this.id}/bookmark`); return res.bookmarked; } async getReplies(limit = 50, page = 0) { const res = await this.api.request('GET', `/posts/${this.id}/replies?limit=${limit}&page=${page}`); return { data: res.data.map((it) => new PostEntity(this.api, it)), nextPage: res.nextPage, }; } async reply(data) { const newData = { ...data, reply_to: this.id, }; return await this.api.posts.create(newData); } async getReposters(limit = 50, page = 0) { const res = await this.api.request('GET', `/posts/${this.id}/reposters?limit=${limit}&page=${page}`); return { data: res.data.map((it) => new index_1.UserEntity(this.api, it)), nextPage: res.nextPage, }; } async repost() { const res = await this.api.request('PUT', `/posts/${this.id}/repost`); return res.success; } async unrepost() { const res = await this.api.request('DELETE', `/posts/${this.id}/repost`); return res.success; } async isReposted() { const res = await this.api.request('GET', `/posts/${this.id}/repost`); return res.reposted; } async getQuotes(limit = 50, page = 0) { const res = await this.api.request('GET', `/posts/${this.id}/quotes?limit=${limit}&page=${page}`); return { data: res.data.map((it) => new PostEntity(this.api, it)), nextPage: res.nextPage, }; } async quote(data) { const formData = (0, utils_1.createFormData)(data); return new PostEntity(this.api, await this.api.request('POST', `/posts/${this.id}/quote`, {}, { body: formData, })); } } exports.PostEntity = PostEntity; class PostsEndpoint extends endpoint_1.Endpoint { constructor(flareApi) { super(flareApi, '/posts'); } async getAll(limit = 50, page = 0) { const res = await this.api.request('GET', `${this.path}?limit=${limit}&page=${page}`); return { data: res.data.map((it) => new PostEntity(this.api, it)), nextPage: res.nextPage, }; } async getById(id) { return new PostEntity(this.api, await this.api.request('GET', `${this.path}/${id}`)); } async getByAuthor(authorId, limit = 50, page = 0) { const res = await this.api.request('GET', `${this.path}/by_author/${authorId}?limit=${limit}&page=${page}`); return { data: res.data.map((it) => new PostEntity(this.api, it)), nextPage: res.nextPage, }; } async create(data) { const formData = (0, utils_1.createFormData)(data); return new PostEntity(this.api, await this.api.request('POST', `${this.path}/create`, {}, { body: formData, })); } async delete(id) { const res = await this.api.request('DELETE', `${this.path}/${id}`); return res.success; } async getMyBookmarks(limit = 50, page = 0) { const res = await this.api.request('GET', `/users/me/bookmarks?limit=${limit}&page=${page}`); return { data: res.data.map((it) => new PostEntity(this.api, it)), nextPage: res.nextPage, }; } } exports.PostsEndpoint = PostsEndpoint; //# sourceMappingURL=posts.js.map