Skip to content

Commit

Permalink
Remove as many project dependencies as possible:
Browse files Browse the repository at this point in the history
 chai -> assert
 mocha -> node:test
 node-fetch -> [optional]
 @stamp/ -> stampit
  • Loading branch information
koresar committed Feb 6, 2023
1 parent 6c8d8c2 commit a27a7c0
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12
18
41 changes: 29 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,54 @@
# trustspot

Node.js library for trustspot.io API

Works in Node.js 6 and above.
Works in Node.js 8 and above.

## Install

```bash
npm i -S trustspot
npm i trustspot
```

## Find your API key

Go to your [Account Settings](https://trustspot.io/index.php/merchant/dashboard/settings) and find the key there.
Go to your [Account Settings](https://trustspot.io/account_settings/general) and find the key there.

## Create API client instance

```js
import Trustspot from 'trustspot';
import Trustspot from "trustspot";
// or
const Trustspot = require("trustspot");
```

In Node.js 18 and above:

```js
const trustspot = Trustspot({ key: MY_KEY });
```

In Node.js 16 and below:

const trustspot = Trustspot({key: MY_KEY});
```js
const trustspot = Trustspot({ key: MY_KEY, fetch: require("node-fetch") });
```

## Fetch data

### getCompanyReviews({limit=10, offset=0, sort='date desc'})

* `limit` - number
* `offset` - number
* `sort` - string, one of: `'date desc'`, `'rating desc'`, `'rating asc'`
- `limit` - number
- `offset` - number
- `sort` - string, one of: `'date desc'`, `'rating desc'`, `'rating asc'`

```js
const reviews = await trustspot.getCompanyReviews({offset: 12});
const reviews = await trustspot.getCompanyReviews({ offset: 12 });
console.log(reviews);
```

Will print something like this:

```js
{
error: '',
Expand Down Expand Up @@ -72,17 +87,19 @@ Will print something like this:
If your API is running not on the default domain here is how to use this module against a different URL.

```js
const Trustspot = require('trustspot').compose({properties: {baseUrl: 'localhost:8081'}});
const Trustspot = require("trustspot").props({
baseUrl: "localhost:8081",
});

const trustspot = Trustspot({key: MY_KEY});
const trustspot = Trustspot({ key: MY_KEY });
```

## Hardcode the key

If you don't want to pass the API key every time you can set the default API key for all object instances.

```js
const Trustspot = require('trustspot').compose({properties: {key: MY_KEY}});
const Trustspot = require("trustspot").props({ key: MY_KEY });

const trustspot = Trustspot(); // No need to pass the key any more!
```
Expand Down
70 changes: 36 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
const assert = require("assert");
const querystring = require("querystring");

module.exports = require("@stamp/arg-over-prop")
.argOverProp({
module.exports = require("stampit")({
props: {
// assigns passed `baseUrl` and `key` to `this` object
baseUrl: "https://trustspot.io/api/pub/",
key: "",
fetch: require("node-fetch"),
})
.compose({
methods: {
_request({ urlSuffix, params }) {
assert(this.key && typeof this.key === "string", '"key" option is mandatory');
fetch: global.fetch,
},

init({ baseUrl, key, fetch }) {
if (baseUrl) this.baseUrl = baseUrl;
if (key) this.key = key;
if (fetch) this.fetch = fetch;
},

let body = Object.assign({ key: this.key }, params);
Object.keys(body).forEach((key) => {
if (!body[key]) delete body[key];
});
methods: {
async _request({ urlSuffix, params }) {
if (!this.key || typeof this.key !== "string") throw new Error('"key" option is mandatory');

return 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: querystring.encode(body),
}).then((response) => response.json());
},
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')}
*/
getCompanyReviews({ limit, offset, sort } = {}) {
return this._request({ urlSuffix: "get_company_reviews", params: { limit, offset, sort } });
},
/**
* 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 } });
},
});
},
});
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Node.js library for trustspot.io API",
"main": "index.js",
"scripts": {
"test": "mocha"
"test": "node --test"
},
"repository": {
"type": "git",
Expand All @@ -21,13 +21,10 @@
},
"homepage": "https://github.com/flash-oss/trustspot#readme",
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^7.2.0",
"prettier": "^2.1.2"
},
"dependencies": {
"@stamp/arg-over-prop": "^1.0.4",
"node-fetch": "^2.6.1"
"stampit": "^4.3.2"
},
"files": [
"/index.js"
Expand Down
38 changes: 23 additions & 15 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
const { expect } = require("chai");
const { describe, it } = require("node:test");
const assert = require("assert");
const Trustspot = require("..");

describe("#getCompanyReviews", () => {
it("throws if key is missing", () => {
expect(() => Trustspot().getCompanyReviews()).to.throw(/key/);
it("throws if key is missing", async () => {
try {
await Trustspot().getCompanyReviews();
assert.fail("should have thrown");
} catch (err) {
assert(err.message.includes("key"));
}
});

it("change default key", () => {
it("change default key", async () => {
const MockedTrustspot = Trustspot.compose({
properties: {
key: "1234",
fetch: () => Promise.resolve({ json: () => ({}) }),
},
});

expect(() => MockedTrustspot().getCompanyReviews()).not.to.throw();
await MockedTrustspot().getCompanyReviews();
});

it("fetch data", () => {
it("fetch data", async () => {
const returnedJson = {
error: "",
company_name: "Acme Inc",
Expand Down Expand Up @@ -136,23 +142,25 @@ describe("#getCompanyReviews", () => {
const MockedTrustspot = Trustspot.compose({
properties: {
fetch(url, { method, headers, body }) {
expect(url).to.equal("https://trustspot.io/api/pub/get_company_reviews");
expect(method).to.equal("POST");
expect(headers).to.deep.equal({
assert.strictEqual(url, "https://trustspot.io/api/pub/get_company_reviews");
assert.strictEqual(method, "POST");
assert.deepStrictEqual(headers, {
Accept: "application/json, application/xml, text/plain, text/html, *.*",
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
});
expect(body).to.equal("key=1234&limit=11&offset=12&sort=rating%20asc");
assert.strictEqual(body, "limit=11&offset=12&sort=rating+asc&key=1234");

return Promise.resolve({ json: () => returnedJson });
},
},
});

return MockedTrustspot({ key: "1234" })
.getCompanyReviews({ limit: 11, offset: 12, sort: "rating asc" })
.then((reviews) => {
expect(reviews).to.equal(returnedJson);
});
const reviews = await MockedTrustspot({ key: "1234" }).getCompanyReviews({
limit: 11,
offset: 12,
sort: "rating asc",
});

assert.strictEqual(reviews, returnedJson);
});
});

0 comments on commit a27a7c0

Please sign in to comment.