-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
196 lines (187 loc) 路 5.05 KB
/
Copy pathindex.js
File metadata and controls
196 lines (187 loc) 路 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const fs = require("fs");
const https = require("https");
const url = new URL("https://cataas.com");
const paths = {
cat: "cat",
cats: "api/cats",
allTags: "api/tags",
};
class Cataas {
/**
* @param {{
* Gif: boolean
* Tag: string
* Text: string
* Width: number
* Height: number
* TextSize: number
* TextColor: string
* Size: "or" | "sq" | "md" | "sm"
* Filter: "pixel" | "paint" | "mono" | "blur" | "sepia" | "negative"
* }} options it is used in requests and url encoding
*/
constructor(options) {
this.options = options ? options : { Gif: false };
}
/**
* Encodes the base url with current options.
* @returns {URL} Encoded URL object
* @example
* const cataas = new Cataas({ Gif: true, Tag: 'cute' });
* const encodedUrl = cataas.encode();
*/
encode() {
url.pathname = paths.cat;
if (this.options.Gif === true) {
url.pathname += `/gif`;
}
if (this.options.Tag) {
url.pathname += `/${this.options.Tag}`;
}
if (this.options.Text) {
url.pathname += `/says/${this.options.Text}`;
url.searchParams.set("size", this.options.TextSize);
if (this.options.TextColor) {
url.searchParams.set("color", this.options.TextColor);
}
}
if (this.options.Size) {
url.searchParams.set("type", this.options.Size);
}
if (this.options.Filter) {
url.searchParams.set("filter", this.options.Filter);
}
if (this.options.Width) {
url.searchParams.set("width", this.options.Width);
}
if (this.options.Height) {
url.searchParams.set("height", this.options.Height);
}
return url;
}
/**
* Encodes the base URL with the given cat image ID.
* @param {string} id - ID of the cat image
* @returns {URL} Encoded URL object
* @throws {Error} If id is undefined
* @example
* const cataas = new Cataas();
* const url = cataas.encodeById('abc123');
*/
encodeById(id) {
if (!id) throw new Error("Argument undefined");
url.pathname = paths.cat + "/" + id;
return url;
}
/**
* Sends a GET request to the encoded URL and returns the response stream.
* @returns {Promise<IncomingMessage>} Response stream
* @throws {Error} If request fails or status code is not 200
* @example
* const cataas = new Cataas();
* cataas.encode();
* cataas.get().then(stream => {
* const file = fs.createWriteStream('cat.png');
* stream.pipe(file);
* });
*/
async get() {
return new Promise((resolve, reject) => {
https
.get(url, async (res) => {
if (res.statusCode !== 200) {
reject(new Error(`Request Failed.\nStatus Code: ${statusCode}`));
}
resolve(res);
})
.on("error", reject);
});
}
/**
* Downloads the image from the encoded URL and saves it to the specified path.
* @param {string} path - Path to save the image file
* @returns {Promise<boolean>} True if successful
* @throws {Error} If path is undefined or request fails
* @example
* const cataas = new Cataas();
* cataas.encode();
* cataas.download('cat.png').then(success => {
* if (success) console.log('Downloaded file successfully');
* });
*/
async download(path) {
return new Promise((resolve, reject) => {
if (!path) throw new Error("Argument undefined");
const file = fs.createWriteStream(path);
https
.get(url, (res) => {
if (res.statusCode !== 200) {
reject(new Error(`Request Failed.\nStatus Code: ${statusCode}`));
}
res
.pipe(file)
.on("finish", () => {
file.close();
resolve(true);
})
.on("error", reject);
})
.on("error", reject);
});
}
/**
* Retrieves all available tags from the Cataas API.
* @returns {Promise<string[]>} Array of tag strings
* @throws {Error} If request fails
* @example
* const cataas = new Cataas();
* cataas.getAllTags().then(tags => {
* console.log('Tags:', tags);
* });
*/
async getAllTags() {
return new Promise((resolve, reject) => {
url.pathname = paths.allTags;
this.get()
.then((stream) => stream.read())
.then((buffer) => buffer.toString())
.then(JSON.parse)
.then(resolve)
.catch(reject);
});
}
/**
* Retrieves cats that include the given tag(s).
* @param {string[]} tags - Array of tags to filter cats
* @param {{Skip?: number, Limit?: number}} [options] - Options for pagination
* @returns {Promise<Object[]>} Array of cat objects
* @throws {Error} If tags are undefined or request fails
* @example
* const cataas = new Cataas();
* cataas.getCats(['cute'], { Limit: 10 }).then(cats => {
* console.log('Cats:', cats);
* });
*/
async getCats(tags, options) {
return new Promise((resolve, reject) => {
if (!tags) throw new Error("Argument undefined");
if (options) {
if (options.Skip) {
url.searchParams.set("skip", options.Skip);
}
if (options.Limit) {
url.searchParams.set("limit", options.Limit);
}
}
url.pathname = paths.cats;
url.searchParams.set("tags", tags.join(","));
this.get()
.then((stream) => stream.read())
.then((buffer) => buffer.toString())
.then(JSON.parse)
.then(resolve)
.catch(reject);
});
}
}
module.exports = Cataas;