-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (38 loc) · 1.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
module.exports = require("stampit")({
props: {
// assigns passed `baseUrl` and `key` to `this` object
baseUrl: "https://trustspot.io/api/pub/",
key: "",
fetch: global.fetch,
},
init({ baseUrl, key, fetch }) {
if (baseUrl) this.baseUrl = baseUrl;
if (key) this.key = key;
if (fetch) this.fetch = fetch;
},
methods: {
async _request({ urlSuffix, params }) {
if (!this.key || typeof this.key !== "string") throw new Error('"key" option is mandatory');
const body = Object.fromEntries(Object.entries(params).filter(([k, v]) => Boolean(v)));
body.key = this.key;
const response = await this.fetch(this.baseUrl + urlSuffix, {
method: "POST",
headers: {
Accept: "application/json, application/xml, text/plain, text/html, *.*",
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
},
body: new URLSearchParams(body).toString(),
});
return await response.json();
},
/**
* Fetch company reviews
* @param [limit] {Number} Max number of reviews to return
* @param [offset] {Number} Offset of the reviews list
* @param [sort] {('date desc'|'rating desc'|'rating asc')}
*/
async getCompanyReviews({ limit, offset, sort } = {}) {
return await this._request({ urlSuffix: "get_company_reviews", params: { limit, offset, sort } });
},
},
});