-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
83 lines (72 loc) · 2.38 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
const request = require('request-promise-native')
function classFix() {
this.random = (this.random == false) ? false : this.random || this.param.random
this.username = (this.username == false) ? false : this.username || this.param.username
this.user = (this.user == false) ? false : this.user || this.param.user
this.like = (this.like == false) ? false : this.like || this.param.like
this.collection = (this.collection == false) ? false : this.collection || this.param.collection
this.collection_id = (this.collection_id == false) ? false : this.collection_id || this.param.collection_id
this.daily = (this.daily == false) ? false : this.daily || this.param.daily
this.weekly = (this.weekly == false) ? false : this.weekly || this.param.weekly
this.search = (this.search == false) ? false : this.search || this.param.search
this.keyword = (this.keyword == false) ? false : this.keyword || this.param.keyword
this.width = (this.width == false) ? false : this.width || this.param.width
this.height = (this.height == false) ? false : this.height || this.param.height
this.photo = (this.photo == false) ? false : this.photo || this.param.photo
this.photo_id = (this.photo_id == false) ? false : this.photo_id || this.param.photo_id
return module.exports(this)
}
async function redirectURL(url, uri) {
const data = await request({
baseUrl: url,
uri: uri,
resolveWithFullResponse: true
})
return data.request.uri.href
}
module.exports = function(param) {
const url = 'https://source.unsplash.com'
let uri = '/'
if (param.random) {
uri += `random/`
}
if (param.photo) {
uri += `${param.photo}/`
} else if (param.photo_id) {
uri += `${param.photo_id}/`
}
if (param.username) {
uri += `user/${param.username}/`
} else if (param.user) {
uri += `user/${param.user}/`
}
if (param.likes) {
uri += `likes/`
}
if (param.collection) {
uri += `collection/${param.collection}/`
} else if (param.collection_id) {
uri += `collection/${param.collection_id}/`
}
if (param.daily) {
uri += `daily/`
} else if (param.weekly) {
uri += 'weekly/'
}
if (param.width && param.height) {
uri += `${param.width}x${param.height}/`
}
if (param.search) {
uri += `?${param.search}/`
} else if (param.keyword) {
uri += `?${param.keyword}/`
}
//Class
this.param = param
this.get = classFix
if (param.redirectURL) {
return redirectURL(url, uri)
} else {
return `${url}${uri}`
}
}