1 | "use strict";
|
2 | var __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 | };
|
11 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
12 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
13 | };
|
14 | Object.defineProperty(exports, "__esModule", { value: true });
|
15 | const FormData = require("form-data");
|
16 | const node_fetch_1 = __importDefault(require("node-fetch"));
|
17 | class 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 |
|
73 |
|
74 |
|
75 | getFlags() {
|
76 | return this._fetch(this.baseUrl + 'flags', {}, 'json');
|
77 | }
|
78 | |
79 |
|
80 |
|
81 |
|
82 |
|
83 | getFlag(flag = 'pride') {
|
84 | return this._fetch(this.baseUrl + 'icon/' + flag, {}, 'img');
|
85 | }
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
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 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
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 | }
|
125 | exports.default = PfP;
|