123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- import request from "request-promise-native";
- import { Response } from "request";
- import { Dict } from "./util";
- enum ReqMethod {
- GET = "get",
- POST = "post",
- DELETE = "delete"
- }
- export interface RequestError {
- code: string;
- message: string;
- params: { key: string; value: any; }[];
- }
- export type RequestErrorSet = { errors: RequestError[] };
- export class XenforoClient {
- constructor(private endpoint: string, private userKey: string) {
- }
- private async makeRequest<T>(uri: string, method: ReqMethod, data?: any) {
- let result = await request(`${this.endpoint}/${uri}`, {
- method: method,
- headers: {
- "XF-Api-Key": this.userKey
- },
- form: data || undefined,
- resolveWithFullResponse: true
- }) as Response;
- if (result.statusCode != 200) {
- throw await JSON.parse(result.body) as RequestErrorSet;
- } else {
- return await JSON.parse(result.body) as T;
- }
- }
- async getMe() {
- let { me } = await this.makeRequest<{me: User}>(`me/`, ReqMethod.GET);
- return me;
- }
- async postReply(thread_id: number, message: string, attachment_key?: string) {
- return await this.makeRequest<void>(`posts/`, ReqMethod.POST, {
- thread_id,
- message,
- attachment_key
- });
- }
- async editThread(id: number, opts?: EditThreadOptions) {
- return await this.makeRequest<CreateThreadResponse>(`threads/${id}`, ReqMethod.POST, opts || {});
- }
- async editPost(id: number, opts?: EditPostOptions) {
- return await this.makeRequest<EditPostResponse>(`posts/${id}`, ReqMethod.POST, opts || {});
- }
- async getThread(id: number, opts?: GetThreadOptions) {
- return await this.makeRequest<GetThreadResponse>(`threads/${id}`, ReqMethod.GET, opts || {});
- }
- async deleteThread(id: number, opts?: DeleteThreadOptions) {
- return await this.makeRequest<SuccessResponse>(`threads/${id}`, ReqMethod.DELETE, opts || {});
- }
- async createThread(forumId: number, title: string, message: string, opts?: CreateThreadOptions) {
- return await this.makeRequest<CreateThreadResponse>(`threads/`, ReqMethod.POST, {
- node_id: forumId,
- title: title,
- message: message,
- ...opts
- });
- }
- async getPost(id: number) {
- let post = await this.makeRequest<{post: Post}>(`posts/${id}`, ReqMethod.GET);
- return post.post;
- }
- async getForumThreads(id: number) {
- return await this.makeRequest<GetForumThreadsResponse>(`forums/${id}/threads`, ReqMethod.GET);
- }
- }
- //#region Request types
- interface DeleteThreadOptions {
- hard_delete?: boolean;
- reason?: boolean;
- starter_alert?: boolean;
- starter_alert_reason?: boolean;
- }
- interface GetThreadOptions {
- with_posts?: boolean;
- page?: number;
- }
- interface CreateThreadOptions {
- prefix_id?: number;
- tags?: string[];
- custom_fields?: Dict<string>;
- discussion_open?: boolean;
- sticky?: boolean;
- attachment_key?: boolean;
- }
- interface EditThreadOptions {
- prefix_id?: number;
- title?: string;
- discussion_open?: boolean;
- sticky?: boolean;
- custom_fields?: Dict<string>;
- add_tags?: any[];
- remove_tags?: any[];
- }
- interface EditPostOptions {
- message?: string;
- silent?: boolean;
- clear_edit?: boolean;
- author_alert?: boolean;
- author_alert_reason?: string;
- attachment_key?: string;
- }
- //#endregion
- //#region Response types
- type GetThreadResponse = {
- thread: Thread;
- messages: Post[];
- pagination: any;
- };
- type SuccessResponse = {
- success: boolean;
- }
- type EditPostResponse = SuccessResponse & { post: Post };
- type CreateThreadResponse = SuccessResponse & { thread: Thread; };
- type GetForumThreadsResponse = {
- threads: Thread[];
- pagination: object;
- sticky: Thread[];
- };
- //#endregion
- //#region Data types
- export interface Forum {
- allow_posting: boolean;
- allow_poll: boolean;
- require_prefix: boolean;
- min_tags: number;
- }
- export interface User {
- about?: string;
- activity_visible?: boolean;
- age?: number;
- alert_optout?: any[];
- allow_post_profile?: string;
- allow_receive_news_feed?: string;
- allow_send_personal_conversation?: string;
- allow_view_identities: string;
- allow_view_profile?: string;
- avatar_urls: object;
- can_ban: boolean;
- can_converse: boolean;
- can_edit: boolean;
- can_follow: boolean;
- can_ignore: boolean;
- can_post_profile: boolean;
- can_view_profile: boolean;
- can_view_profile_posts: boolean;
- can_warn: boolean;
- content_show_signature?: boolean;
- creation_watch_state?: string;
- custom_fields?: object;
- custom_title?: string;
- dob?: object;
- email?: string;
- email_on_conversation?: boolean;
- gravatar?: string;
- interaction_watch_state?: boolean;
- is_admin?: boolean;
- is_banned?: boolean;
- is_discouraged?: boolean;
- is_followed?: boolean;
- is_ignored?: boolean;
- is_moderator?: boolean;
- is_super_admin?: boolean;
- last_activity?: number;
- location: string;
- push_on_conversation?: boolean;
- push_optout?: any[];
- receive_admin_email?: boolean;
- secondary_group_ids?: any[];
- show_dob_date?: boolean;
- show_dob_year?: boolean;
- signature: string;
- timezone?: string;
- use_tfa?: any[];
- user_group_id?: number;
- user_state?: string;
- user_title: string;
- visible?: boolean;
- warning_points?: number;
- website?: string;
- user_id: number;
- username: string;
- message_count: number;
- register_date: number;
- trophy_points: number;
- is_staff: boolean;
- reaction_score: number;
- }
- export interface Node {
- breadcrumbs: any[];
- type_data: object;
- node_id: number;
- title: string;
- node_name: string;
- description: string;
- node_type_id: string;
- parent_node_id: number;
- display_order: number;
- display_in_list: boolean;
- }
- export interface Thread {
- username: string;
- is_watching?: boolean;
- visitor_post_count?: number;
- custom_fields: object;
- tags: any[];
- prefix?: string;
- can_edit: boolean;
- can_edit_tags: boolean;
- can_reply: boolean;
- can_soft_delete: boolean;
- can_hard_delete: boolean;
- can_view_attachments: boolean;
- Forum?: Node;
- thread_id: number;
- node_id: number;
- title: string;
- reply_count: number;
- view_count: number;
- user_id: number;
- post_date: number;
- sticky: boolean;
- discussion_state: string;
- discussion_open: boolean;
- discussion_type: string;
- first_post_id: number;
- last_post_date: number;
- last_post_id: number;
- last_post_user_id: number;
- last_post_username: string;
- first_post_reaction_score: number;
- prefix_id: number;
- }
- export interface Attachment {
- filename: string;
- file_size: number;
- height: number;
- width: number;
- thumbnail_url: string;
- video_url: string;
- attachment_id: number;
- content_type: string;
- content_id: number;
- attach_date: number;
- view_count: number;
- }
- export interface Post {
- username: string;
- is_first_post: boolean;
- is_last_post: boolean;
- can_edit: boolean;
- can_soft_delete: boolean;
- can_hard_delete: boolean;
- can_react: boolean;
- can_view_attachments: boolean;
- Thread?: Thread;
- Attachments?: Attachment[];
- is_reacted_to: boolean;
- visitor_reaction_id: number;
- post_id: number;
- thread_id: number;
- user_id: number;
- post_date: number;
- message: string;
- message_state: string;
- attach_count: number;
- warning_message: string;
- position: number;
- last_edit_date: number;
- reaction_score: number;
- User: User;
- }
- //#endregion
|