Skip to content

Instantly share code, notes, and snippets.

@geta6
Created March 11, 2020 08:54
Show Gist options
  • Save geta6/4363d9daaa790c0abb3ff9b79a3c0f57 to your computer and use it in GitHub Desktop.
Save geta6/4363d9daaa790c0abb3ff9b79a3c0f57 to your computer and use it in GitHub Desktop.
Esa
import axios, { AxiosInstance } from 'axios';
import { EsaPost, EsaCreatePostBody, EsaUpdatePostBody } from './types';
// https://docs.esa.io/posts/102
export class Esa {
private teamName: string;
private request: AxiosInstance;
constructor({ token, teamName }: { token: string; teamName: string }) {
this.teamName = teamName;
this.request = axios.create({
baseURL: 'https://api.esa.io/',
headers: {
Authorization: `Bearer ${token}`,
},
});
}
getPosts = async ({ q }: { q?: string }) => {
const res = await this.request.get(`/v1/teams/${this.teamName}/posts`, { params: { ...(q ? { q } : {}) } });
process.stdout.write(`RateLimit: ${res.headers['x-ratelimit-remaining']} / ${res.headers['x-ratelimit-limit']}\n`);
return res.data as { posts: EsaPost[] };
};
createPost = async (body: EsaCreatePostBody) => {
const res = await this.request.post(`/v1/teams/${this.teamName}/posts`, { ...body });
process.stdout.write(`RateLimit: ${res.headers['x-ratelimit-remaining']} / ${res.headers['x-ratelimit-limit']}\n`);
return res.data as EsaPost;
};
updatePost = async (number: number, body: EsaUpdatePostBody) => {
const res = await this.request.patch(`/v1/teams/${this.teamName}/posts/${number}`, { ...body });
process.stdout.write(`RateLimit: ${res.headers['x-ratelimit-remaining']} / ${res.headers['x-ratelimit-limit']}\n`);
return res.data as EsaPost;
};
}
export interface EsaPost {
number: number;
name: string;
full_name: string;
wip: boolean;
body_md: string;
body_html: string;
created_at: string;
message: string;
kind: string;
comments_count: number;
tasks_count: number;
done_tasks_count: number;
url: string;
updated_at: string;
tags: string[];
category: string;
revision_number: number;
created_by: {
name: string;
screen_name: string;
icon: string;
};
updated_by: {
name: string;
screen_name: string;
icon: string;
};
stargazers_count: number;
watchers_count: number;
star: boolean;
watch: boolean;
sharing_urls: null;
}
export interface EsaPostBody {
name: string;
body_md?: string;
tags?: string[];
category?: string;
wip?: boolean;
message?: string;
}
export interface EsaCreatePostBody extends EsaPostBody {
user?: string;
template_post_id?: number;
}
export interface EsaUpdatePostBody extends Partial<EsaPostBody> {
created_by?: string;
updated_by?: string;
original_revision?: {
body_md: string;
number: number;
user: string;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment