-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
193 lines (184 loc) · 4.29 KB
/
index.js
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
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 `this.Options`
@returns encoded url
@example
```
var cataas = new Cataas()
var 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 with the given id
* @param {String} id id of cat image
* @returns encoded url
*/
encodeById(id) {
if (!id) throw new Error('Argument undefined')
url.pathname = paths.cat + "/" + id
return url
}
/** sends get request then returns the response
@example
```
var cataas = new Cataas()
cataas.encode()
cataas.get()
.then(readable => {
const stream = new fs.createWriteStream('cat.png')
readable.pipe(stream)
})
.catch(e => console.error(e))
```
*/
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)
})
}
/** Download image
@param {String} path path to save image file
@returns {Boolean} successful
@example
```
var cataas = new Cataas()
cataas.encode()
cataas.download('cat.png')
.then(successful => {
if (successful) {
console.log('Downloaded file successfully')
}
})
.catch(e => console.error(e))
```
*/
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)
})
}
/** get all tags
@example
var cataas = new Cataas()
cataas.encode()
cataas.getAllTags()
.then(tags => {
console.log('Tags length:', tags.length)
})
.catch(e => console.error(e))
*/
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)
})
}
/** get cats which include the given tag(s)
@param {String[]} tags
@param {{
Skip: number
Limit: number
}} options get cats options
@example
var cataas = new Cataas()
cataas.encode()
cataas.getCats(['cute'])
.then(cats => console.log('Results length:', cats.length))
.catch(e => console.error(e))
*/
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