@flaresocial/api
Version:
API wrapper for the Flare Social API
97 lines • 3.67 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UsersEndpoint = exports.UserEntity = void 0;
const index_1 = require("./index");
const endpoint_1 = require("./endpoint");
const entity_1 = require("./entity");
const utils_1 = require("./utils");
class UserEntity extends entity_1.Entity {
constructor(api, data) {
super(api);
this.id = data.id;
this.created_at = data.created_at;
this.username = data.username;
this.display_name = data.display_name;
this.bio = data.bio;
this.location = data.location;
this.link = data.link;
this.pronouns = data.pronouns;
this.avatar = data.avatar;
this.banner = data.banner;
this.admin = data.admin;
this.follower_count = data.follower_count;
this.following_count = data.following_count;
this.post_count = data.post_count;
this.createdAt = new Date(data.created_at);
}
async getPosts(limit = 50, page = 0) {
return await this.api.posts.getByAuthor(this.id, limit, page);
}
async getReplies(limit = 50, page = 0) {
const replies = await this.api.request('GET', `/users/${this.id}/replies?limit=${limit}&page=${page}`);
return {
data: replies.data.map((it) => new index_1.PostEntity(this.api, it)),
nextPage: replies.nextPage,
};
}
async getFollowers() {
const followers = await this.api.request('GET', `/users/${this.id}/followers`);
return {
data: followers.data.map((it) => new UserEntity(this.api, it)),
nextPage: followers.nextPage,
};
}
async getFollowing() {
const following = await this.api.request('GET', `/users/${this.id}/following`);
return {
data: following.data.map((it) => new UserEntity(this.api, it)),
nextPage: following.nextPage,
};
}
async follow() {
const response = await this.api.request('PUT', `/users/${this.id}/follow`);
return response.success;
}
async unfollow() {
const response = await this.api.request('DELETE', `/users/${this.id}/follow`);
return response.success;
}
async following() {
return await this.api.users.isFollowing(this);
}
}
exports.UserEntity = UserEntity;
class UsersEndpoint extends endpoint_1.Endpoint {
constructor(flareApi) {
super(flareApi, '/users');
}
async getAll() {
return (await this.api.request('GET', this.path)).map((it) => new UserEntity(this.api, it));
}
async getById(id) {
return new UserEntity(this.api, await this.api.request('GET', `${this.path}/${id}`));
}
async getByHandle(handle) {
return new UserEntity(this.api, await this.api.request('GET', `${this.path}/by_handle/${handle}`));
}
// Self-related methods
async getMe() {
return new UserEntity(this.api, await this.api.request('GET', `${this.path}/me`));
}
async updateMe(data) {
const formData = (0, utils_1.createFormData)(data);
await this.api.request('PATCH', `${this.path}/me`, {}, // headers
{ body: formData });
}
async removeFollower(id) {
const response = await this.api.request('DELETE', `/users/me/followers/${id}`);
return response.success;
}
async isFollowing(user) {
const userId = typeof user === 'string' ? user : user.id;
const response = await this.api.request('GET', `/users/${userId}/follow`);
return response.following;
}
}
exports.UsersEndpoint = UsersEndpoint;
//# sourceMappingURL=users.js.map