UNPKG

5.31 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11var __importDefault = (this && this.__importDefault) || function (mod) {
12 return (mod && mod.__esModule) ? mod : { "default": mod };
13};
14Object.defineProperty(exports, "__esModule", { value: true });
15const FormData = require("form-data");
16const node_fetch_1 = __importDefault(require("node-fetch"));
17class PfP {
18 constructor() {
19 this.baseUrl = 'https://api.pfp.lgbt/v3/';
20 this.rateLimit = false;
21 this.rateLimitEnd = 0;
22 }
23 _fetch(url, init, type = 'json') {
24 return new Promise((resolve, reject) => {
25 if (this.rateLimit)
26 reject('Rate limit reached!');
27 node_fetch_1.default(url, init).then((res) => __awaiter(this, void 0, void 0, function* () {
28 const rateLimitRemaining = res.headers.get('x-ratelimit-remaining');
29 let rateLimitReset = res.headers.get('x-ratelimit-reset');
30 if (rateLimitRemaining === '0') {
31 this.rateLimit = true;
32 const currentTimeStamp = Date.now();
33 rateLimitReset = rateLimitReset ? parseInt(rateLimitReset) * 1000 : currentTimeStamp + 5000;
34 this.rateLimitEnd = rateLimitReset;
35 if (this.resetTimeout)
36 clearTimeout(this.resetTimeout);
37 this.resetTimeout = setTimeout(() => (this.rateLimit = false), rateLimitReset - currentTimeStamp);
38 }
39 if (res.status > 299 || res.status < 200)
40 reject(`${res.status}: ${res.statusText}`);
41 try {
42 if (type === 'img')
43 yield res.buffer().then(resolve);
44 else if (type === 'json')
45 yield res.json().then(resolve);
46 else
47 reject(`${type} is not a valid mime type`);
48 }
49 catch (error) {
50 reject(error);
51 }
52 }));
53 });
54 }
55 _fetchImage(url, buf, alpha) {
56 const data = new FormData();
57 data.append('file', buf, 'image.png');
58 if (alpha)
59 data.append('alpha', alpha);
60 return this._fetch(url, {
61 method: 'POST',
62 body: data,
63 headers: Object.assign({}, data.getHeaders())
64 }, 'img');
65 }
66 _urlToBuf(url) {
67 return node_fetch_1.default(url)
68 .then(res => res.buffer())
69 .catch(() => null);
70 }
71 /**
72 * Get an object containing all valid flags and their defaults
73 * @returns `Promise<FlagResponse>`
74 */
75 getFlags() {
76 return this._fetch(this.baseUrl + 'flags', {}, 'json');
77 }
78 /**
79 * Get an image of the provided flag
80 * @param flag The target flag. You can get a list of all flags with the `getFlags()` method
81 * @returns `Promise<Buffer>`
82 */
83 getFlag(flag = 'pride') {
84 return this._fetch(this.baseUrl + 'icon/' + flag, {}, 'img');
85 }
86 /**
87 * Create a static lgbtifed image
88 * @param image Buffer/Url of the image to lgbtify
89 * @param flag The Pride flag to add
90 * @param type The effect type
91 * @param style The effect style
92 * @param format The format of the resulting image
93 * @param alpha The alpha the effect will have
94 * @returns `Promise<Buffer>`
95 */
96 createStatic(image, flag, type = 'circle', style = 'solid', format = 'png', alpha) {
97 return __awaiter(this, void 0, void 0, function* () {
98 const url = `${this.baseUrl}image/static/${type}/${style}/${flag}.${format}`;
99 if (!(image instanceof Buffer))
100 image = (yield this._urlToBuf(image));
101 if (!image)
102 throw new Error('Invalid image provided');
103 return this._fetchImage(url, image, alpha);
104 });
105 }
106 /**
107 * Create an animated lgbtifed image
108 * @param image Buffer/Url of the image to lgbtify
109 * @param flag The Pride flag to add
110 * @param type The effect type
111 * @param alpha The alpha the effect will have
112 * @returns `Promise<Buffer>`
113 */
114 createAnimated(image, flag, type = 'circle', alpha) {
115 return __awaiter(this, void 0, void 0, function* () {
116 const url = `${this.baseUrl}image/animated/${type}/${flag}`;
117 if (!(image instanceof Buffer))
118 image = (yield this._urlToBuf(image));
119 if (!image)
120 throw new Error('Invalid image provided');
121 return this._fetchImage(url, image, alpha);
122 });
123 }
124}
125exports.default = PfP;