Skip to content

Commit

Permalink
feat: refactor lib to use axios
Browse files Browse the repository at this point in the history
BREAKING CHANGE: replaces request-promise with axios
  • Loading branch information
tpfrne committed Apr 4, 2024
1 parent ac94570 commit 16d7acf
Show file tree
Hide file tree
Showing 9 changed files with 11,150 additions and 8,455 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
dist
output
7 changes: 7 additions & 0 deletions .prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
trailingComma: 'all',
singleQuote: true,
printWidth: 120,
tabWidth: 2,
endOfLine: 'auto',
};
31 changes: 22 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
# trustpilot

[![Build Status](https://travis-ci.org/trustpilot/node-trustpilot.svg?branch=master)](https://travis-ci.org/trustpilot/node-trustpilot) [![npm](https://img.shields.io/npm/v/trustpilot.svg)](https://www.npmjs.com/package/trustpilot)

This a node API wrapper for accessing the Trustpilot APIs. You can learn all about the Trustpilot API at [https://developers.trustpilot.com/](https://developers.trustpilot.com/).
This a NodeJS API wrapper for accessing the Trustpilot APIs. You can learn all about the Trustpilot API at [https://developers.trustpilot.com/](https://developers.trustpilot.com/).

## Installation

This module is built using Typescript and Node.js `v20.x`.
This module is built using Typescript and NodeJS `v20.x`.

Install using `npm install trustpilot`

## Usage

As of version 3, the project has been converted to Typescript.
The trustpilot module is async/await based. It provides [request-promise-native](https://github.com/request/request-promise-native) objects with sane defaults.
The trustpilot module is async/await based. It provides an [axios instance](https://github.com/axios/axios?tab=readme-ov-file#creating-an-instance) with sane defaults.

### Basic Usage

Expand All @@ -31,6 +29,7 @@ async run() {
// For basic calls authentified by API key, use client.apiRequest
try {
const response = await client.apiRequest('/v1/resources/images');
console.log(response.data);
} catch(error) {
// handle the error
});
Expand All @@ -39,7 +38,7 @@ async run() {

### Usage with OAuth

For calls authentified by OAuth token, use the `authenticate()` promise, which resolves with a `request-promise-native`
For calls authentified by OAuth token, use the `authenticate()` promise, which resolves with an `axiosInstance`
object with everything you need.

```ts
Expand All @@ -54,9 +53,8 @@ async run() {
}).authenticate();

try {
await client(`/v1/private/business-units/${YOUR_BUSINESS_UNIT_ID}/reviews`);
// same as
await client.get(`/v1/private/business-units/${YOUR_BUSINESS_UNIT_ID}/reviews`);
const response = await client.get(`/v1/private/business-units/${YOUR_BUSINESS_UNIT_ID}/reviews`);
console.log(response.data);
} catch(error) {
// handle the error
});
Expand Down Expand Up @@ -100,3 +98,18 @@ async run() {
// Use client
}
```

### Migration from v3 to v4

As of version 4, [request-promise-native](https://github.com/request/request-promise-native) has been replaced by [axios](https://github.com/axios/axios)

This means all responses are now following [this response schema](https://github.com/axios/axios?tab=readme-ov-file#response-schema)
so code that previously used the response body directly, will need to add a `.data`

```ts
const json = await client.apiRequest('...');
```

```ts
const json = (await client.apiRequest('...')).data;
```
Loading

0 comments on commit 16d7acf

Please sign in to comment.