UNPKG

1.94 kBPlain TextView Raw
1import { get } from 'https';
2
3class NekoChxn {
4 private readonly baseUrl = 'https://api.neko-chxn.xyz/V1/';
5
6 public blush = () => this.fetch(`${this.baseUrl}BLUSH/IMG`);
7 public cry = () => this.fetch(`${this.baseUrl}CRY/IMG`);
8 public cuddle = () => this.fetch(`${this.baseUrl}CUDDLE/IMG`);
9 public dance = () => this.fetch(`${this.baseUrl}DANCE/IMG`);
10 public hug = () => this.fetch(`${this.baseUrl}HUG/IMG`);
11 public lick = () => this.fetch(`${this.baseUrl}LICK/IMG`);
12 public kiss = () => this.fetch(`${this.baseUrl}KISS/IMG`);
13 public love = () => this.fetch(`${this.baseUrl}LOVE/IMG`);
14 public pat = () => this.fetch(`${this.baseUrl}PAT/IMG`);
15 public punch = () => this.fetch(`${this.baseUrl}PUNCH/IMG`);
16 public smirk = () => this.fetch(`${this.baseUrl}SMIRK/IMG`);
17 public tickle = () => this.fetch(`${this.baseUrl}TICKLE/IMG`);
18 public yell = () => this.fetch(`${this.baseUrl}YELL/IMG`);
19
20 private fetch(url: string): Promise<ApiImageResponse> {
21 return new Promise((resolve, reject) => {
22 get(url, res => {
23 if (!res.statusCode || res.statusCode > 299 || res.statusCode < 200) {
24 res.resume();
25 reject(`Error while fetching Neko-Chxn: ${res.statusCode} - ${res.statusMessage ?? 'Unknown error while fetching Neko-Chxn'}`);
26 }
27 res.setEncoding('utf8');
28 let rawData = '';
29 res.on('data', chunk => {
30 rawData += chunk;
31 });
32 res.on('end', () => {
33 try {
34 const parsedData = JSON.parse(rawData);
35 resolve(parsedData);
36 } catch (e) {
37 reject(`Error: ${e.message}`);
38 }
39 });
40 }).on('error', err => {
41 reject(`Error while fetching Neko-Chxn: ${err.message}`);
42 });
43 });
44 }
45}
46
47export interface ApiImageResponse {
48 URL: string;
49}
50export type ApiImageFetch = (url: string) => Promise<ApiImageResponse>;
51export type ApiImageEndpoints = keyof NekoChxn;
52
53const nekoChxn = new NekoChxn();
54
55export default nekoChxn;
56module.exports = nekoChxn;
57export { nekoChxn };