This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
178 lines (164 loc) · 5.56 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
var fetch = require('node-fetch');
var BASE_URL = "https://api.pexels.com/";
var DIRECTORY = {
SEARCH_URL: BASE_URL + "v1/search",
POPULAR_URL: BASE_URL + "v1/popular",
CURATED_URL: BASE_URL + "v1/curated",
VIDEO_SEARCH_URL: BASE_URL + "videos/search",
POPULAR_VIDEO_URL: BASE_URL + "videos/popular",
PHOTO_URL: BASE_URL + "v1/photos/"
};
/**
* Pexels API wrapper which exposes Promise factories to interact with the Pexels API endpoints
* @param {string} apiKey API Key provided by Pexels (Required)
*/
function PexelsApi(apiKey) {
if (!apiKey) {
throw new Error("API Key missing");
}
var self = this;
self.apiKey = apiKey;
self.headers = {
'Authorization': apiKey
};
}
/**
* Prepare Pexels API request url and parameters
* @param {string} directory
* @param {string} query
* @param {number} perPage
* @param {number} page
* @param {number} minWidth
* @param {number} maxWidth
* @param {number} minDuration
* @param {number} maxDuration
* @returns {string}
*/
function prepareUrl(
directory, query, perPage, page, minWidth, maxWidth, minDuration, maxDuration
) {
var search = (query ? "query=" + (query ? encodeURIComponent(query) : "") : "")
+ "&per_page=" + (perPage && !isNaN(perPage) ? +perPage : 10)
+ "&page=" + (page && !isNaN(page) ? +page : 1);
if (minWidth && !isNaN(minWidth)) {
search += "&min_width=" + minWidth;
}
if (maxWidth && !isNaN(maxWidth)) {
search += "&max_width=" + maxWidth;
}
if (minDuration && !isNaN(minDuration)) {
search += "&min_duration=" + minDuration;
}
if (maxDuration && !isNaN(maxDuration)) {
search += "&max_duration=" + maxDuration;
}
return directory + "?" + search;
}
/**
* Send request to Pexels API and return response
* @param {PexelsApi} self
* @param {string} url
* @returns {Promise}
*/
function request(self, url) {
return fetch(url, {
headers: self.headers
})
.then(function (res) {
return res.json();
}).catch(function (err) {
return Promise.reject(err);
});
}
/**
* Promise factory to interact with Pexels Search API
* @param {string} query Search term
* @param {number} perPage Specifies the number of items per page (Defaults to 10)
* @param {number} page Specifies the page being requested (Defaults to 1)
* @returns {Promise}
*/
PexelsApi.prototype.search = function (query, perPage, page) {
var url = prepareUrl(DIRECTORY.SEARCH_URL, query, perPage, page);
return request(this, url);
};
/**
* Promise factory to interact with Pexels Popular Photos API
* @param {number} perPage Specifies the number of items per page (Defaults to 10)
* @param {number} page Specifies the page being requested (Defaults to 1)
* @returns {Promise}
*/
PexelsApi.prototype.getPopularPhotos = function (perPage, page) {
var url = prepareUrl(DIRECTORY.POPULAR_URL, null, perPage, page);
return request(this, url);
};
/**
* Promise factory to interact with Pexels Curated Photos API
* @param {number} perPage Specifies the number of items per page (Defaults to 10)
* @param {number} page Specifies the page being requested (Defaults to 1)
* @returns {Promise}
*/
PexelsApi.prototype.getCuratedPhotos = function (perPage, page) {
var url = prepareUrl(DIRECTORY.CURATED_URL, null, perPage, page);
return request(this, url);
};
/**
* Promise factory to interact with Pexels Videos API
* @param {string} query Search term
* @param {number} perPage Specifies the number of items per page (Defaults to 10)
* @param {number} page Specifies the page being requested (Defaults to 1)
* @param {number} minWidth Specifies the minimum width in pixels of the returned videos
* @param {number} maxWidth Specifies the maximum width in pixels of the returned videos
* @param {number} minDuration Specifies the minimum duration in seconds of the returned videos
* @param {number} maxDuration Specifies the maximum duration in seconds of the returned videos
* @returns {Promise}
*/
PexelsApi.prototype.searchVideos = function (
query, perPage, page, minWidth, maxWidth, minDuration, maxDuration
) {
var url = prepareUrl(
DIRECTORY.VIDEO_SEARCH_URL,
query,
perPage,
page,
minWidth,
maxWidth,
minDuration,
maxDuration
);
return request(this, url);
};
/**
* Promise factory to interact with Pexels Popular Videos API
* @param {number} perPage Specifies the number of items per page (Defaults to 10)
* @param {number} page Specifies the page being requested (Defaults to 1)
* @param {number} minWidth Specifies the minimum width in pixels of the returned videos
* @param {number} maxWidth Specifies the maximum width in pixels of the returned videos
* @param {number} minDuration Specifies the minimum duration in seconds of the returned videos
* @param {number} maxDuration Specifies the maximum duration in seconds of the returned videos
* @returns {Promise}
*/
PexelsApi.prototype.getPopularVideos = function (
perPage, page, minWidth, maxWidth, minDuration, maxDuration
) {
var url = prepareUrl(
DIRECTORY.POPULAR_VIDEO_URL,
null,
perPage,
page,
minWidth,
maxWidth,
minDuration,
maxDuration
);
return request(this, url);
};
/**
* Promise factory to interact with Pexels API to request a specific photo by ID
* @param {number} id
* @returns {Promise}
*/
PexelsApi.prototype.getPhoto = function (id) {
var url = DIRECTORY.PHOTO_URL + id;
return request(this, url);
};
module.exports = PexelsApi;