Skip to content

Commit

Permalink
feat: Use axios for HTTP requests (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
avaly committed Apr 6, 2023
1 parent 2e0bedd commit 13b8351
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 215 deletions.
1 change: 1 addition & 0 deletions jest.config.ts
Expand Up @@ -2,6 +2,7 @@ import type { Config } from 'jest';

const config: Config = {
preset: 'ts-jest',
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
testEnvironment: 'node',
};

Expand Down
3 changes: 3 additions & 0 deletions jest.setup.ts
@@ -0,0 +1,3 @@
import nock from 'nock';

nock.disableNetConnect();
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -46,7 +46,7 @@
"test:watch": "jest --watch"
},
"dependencies": {
"got": "^10.2.0"
"axios": "1.3.5"
},
"devDependencies": {
"@types/jest": "29.5.0",
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/coupons.test.ts
Expand Up @@ -59,7 +59,7 @@ describe('coupons methods', () => {
const scope = nock().post(path, expectedBody).reply(400, DEFAULT_ERROR);

await expect(instance.getProductCoupons(productID)).rejects.toThrow(
'Response code 400'
'Request failed with status code 400'
);

expect(scope.isDone()).toBeTruthy();
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/orders.test.ts
Expand Up @@ -84,7 +84,7 @@ describe('orders methods', () => {
const scope = nock().get(path).reply(400, DEFAULT_ERROR);

await expect(instance.getOrderDetails(checkoutId)).rejects.toThrow(
'Response code 400'
'Request failed with status code 400'
);

expect(scope.isDone()).toBeTruthy();
Expand Down
6 changes: 4 additions & 2 deletions src/__tests__/products.test.ts
Expand Up @@ -60,7 +60,9 @@ describe('products methods', () => {
test('rejects on error response', async () => {
const scope = nock().post(path, EXPECTED_BODY).reply(400, DEFAULT_ERROR);

await expect(instance.getProducts()).rejects.toThrow('Response code 400');
await expect(instance.getProducts()).rejects.toThrow(
'Request failed with status code 400'
);

expect(scope.isDone()).toBeTruthy();
});
Expand All @@ -69,7 +71,7 @@ describe('products methods', () => {
const scope = nock().post(path, EXPECTED_BODY).reply(200, DEFAULT_ERROR);

await expect(instance.getProducts()).rejects.toThrow(
'Request http://test.paddle.com/product/get_products returned an error!'
'Request https://test.paddle.com/product/get_products returned an error!'
);

expect(scope.isDone()).toBeTruthy();
Expand Down
16 changes: 8 additions & 8 deletions src/__tests__/subscriptions.test.ts
Expand Up @@ -79,7 +79,7 @@ describe('subscription methods', () => {
const scope = nock().post(path, EXPECTED_BODY).reply(400, DEFAULT_ERROR);

await expect(instance.getSubscriptionPlans()).rejects.toThrow(
'Response code 400'
'Request failed with status code 400'
);

expect(scope.isDone()).toBeTruthy();
Expand Down Expand Up @@ -136,7 +136,7 @@ describe('subscription methods', () => {
const scope = nock().post(path, expectedBody).reply(400, DEFAULT_ERROR);

await expect(instance.getSubscriptionPlan(PLAN_ID)).rejects.toThrow(
'Response code 400'
'Request failed with status code 400'
);

expect(scope.isDone()).toBeTruthy();
Expand Down Expand Up @@ -190,7 +190,7 @@ describe('subscription methods', () => {
const scope = nock().post(path, expectedBody).reply(400, DEFAULT_ERROR);

await expect(instance.getUsers({ planID: PLAN_ID })).rejects.toThrow(
'Response code 400'
'Request failed with status code 400'
);

expect(scope.isDone()).toBeTruthy();
Expand Down Expand Up @@ -234,7 +234,7 @@ describe('subscription methods', () => {
const scope = nock().post(path, expectedBody).reply(400, DEFAULT_ERROR);

await expect(instance.getSubscriptionPayments(PLAN_ID)).rejects.toThrow(
'Response code 400'
'Request failed with status code 400'
);

expect(scope.isDone()).toBeTruthy();
Expand Down Expand Up @@ -276,7 +276,7 @@ describe('subscription methods', () => {

await expect(
instance.reschedulePayment(PAYMENT_ID, NEW_PAYMENT_DATE)
).rejects.toThrow('Response code 400');
).rejects.toThrow('Request failed with status code 400');

expect(scope.isDone()).toBeTruthy();
});
Expand Down Expand Up @@ -382,7 +382,7 @@ describe('subscription methods', () => {

await expect(
instance.updateSubscription(SUBSCRIPTION_ID, { planID: PLAN_ID })
).rejects.toThrow('Response code 400');
).rejects.toThrow('Request failed with status code 400');

expect(scope.isDone()).toBeTruthy();
});
Expand Down Expand Up @@ -414,7 +414,7 @@ describe('subscription methods', () => {

await expect(
instance.cancelSubscription(SUBSCRIPTION_ID)
).rejects.toThrow('Response code 400');
).rejects.toThrow('Request failed with status code 400');

expect(scope.isDone()).toBeTruthy();
});
Expand Down Expand Up @@ -479,7 +479,7 @@ describe('subscription methods', () => {

await expect(
instance.createSubscriptionModifier(SUBSCRIPTION_ID, 10)
).rejects.toThrow('Response code 400');
).rejects.toThrow('Request failed with status code 400');

expect(scope.isDone()).toBeTruthy();
});
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/transactions.test.ts
Expand Up @@ -96,7 +96,7 @@ describe('transactions methods', () => {
const scope = nock().post(path, EXPECTED_BODY).reply(400, DEFAULT_ERROR);

await expect(instance.getUserTransactions(123)).rejects.toThrow(
'Response code 400'
'Request failed with status code 400'
);

expect(scope.isDone()).toBeTruthy();
Expand All @@ -106,7 +106,7 @@ describe('transactions methods', () => {
const scope = nock().post(path, EXPECTED_BODY).reply(200, DEFAULT_ERROR);

await expect(instance.getUserTransactions(123)).rejects.toThrow(
'Request http://test.paddle.com/user/123/transactions returned an error!'
'Request https://test.paddle.com/user/123/transactions returned an error!'
);

expect(scope.isDone()).toBeTruthy();
Expand Down
6 changes: 4 additions & 2 deletions src/__tests__/users.test.ts
Expand Up @@ -73,7 +73,9 @@ describe('users methods', () => {
test('rejects on error response', async () => {
const scope = nock().post(path, expectedBody).reply(400, DEFAULT_ERROR);

await expect(instance.getUsers()).rejects.toThrow('Response code 400');
await expect(instance.getUsers()).rejects.toThrow(
'Request failed with status code 400'
);

expect(scope.isDone()).toBeTruthy();
});
Expand All @@ -82,7 +84,7 @@ describe('users methods', () => {
const scope = nock().post(path, expectedBody).reply(200, DEFAULT_ERROR);

await expect(instance.getUsers()).rejects.toThrow(
'Request http://test.paddle.com/subscription/users returned an error!'
'Request https://test.paddle.com/subscription/users returned an error!'
);

expect(scope.isDone()).toBeTruthy();
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/webhooks.test.ts
Expand Up @@ -97,7 +97,7 @@ describe('webhooks methods', () => {
const scope = nock().post(path, EXPECTED_BODY).reply(400, DEFAULT_ERROR);

await expect(instance.getWebhooksHistory()).rejects.toThrow(
'Response code 400'
'Request failed with status code 400'
);

expect(scope.isDone()).toBeTruthy();
Expand Down
25 changes: 9 additions & 16 deletions src/sdk.ts
@@ -1,8 +1,7 @@
import crypto from 'crypto';
import got from 'got';
import axios, { AxiosRequestConfig } from 'axios';

import serialize from './serialize';
import { OptionsOfDefaultResponseBody } from 'got/dist/source/create';
import {
CreateSubscriptionModifierBody,
CreateSubscriptionModifierResponse,
Expand Down Expand Up @@ -495,16 +494,14 @@ s * @example
* @param {object} options
* @param {object} [options.body] - body parameters / object
* @param {object} [options.headers] - header parameters
* @param {boolean} [options.form] - form parameter (ref: got package)
* @param {boolean} [options.json] - json parameter (ref: got package)
* @param {boolean} [options.form] - serialize the data object to urlencoded format
*/
private async _request<TResponse, TBody extends object = object>(
path: string,
{
body,
body: requestBody,
headers,
form = true,
json = false,
checkoutAPI = false,
}: {
body?: TBody;
Expand All @@ -518,29 +515,25 @@ s * @example
// Requests to Checkout API are using only GET,
const method = checkoutAPI ? 'GET' : 'POST';

const fullBody = {
const fullRequestBody = {
vendor_id: this.vendorID,
vendor_auth_code: this.apiKey,
...body,
...requestBody,
};

const options: OptionsOfDefaultResponseBody = {
const options: AxiosRequestConfig = {
headers: {
'User-Agent': `paddle-sdk/${VERSION} (https://github.com/avaly/paddle-sdk)`,
...(form && { 'Content-Type': 'application/x-www-form-urlencoded' }),
...(headers || {}),
},
method,
};
if (method !== 'GET') {
if (form) {
options.form = fullBody;
}
if (json) {
options.json = fullBody;
}
options.data = fullRequestBody;
}

const data = await got(url, options).json<PaddleResponseWrap<TResponse>>();
const { data } = await axios<PaddleResponseWrap<TResponse>>(url, options);

if ('success' in data && typeof data.success === 'boolean') {
if (data.success) {
Expand Down
2 changes: 1 addition & 1 deletion tests/cjs/index.js
Expand Up @@ -2,7 +2,7 @@ const assert = require('assert');
const nock = require('nock');
const { PaddleSDK } = require('paddle-sdk');

const SERVER = 'http://test.paddle.com';
const SERVER = 'https://test.paddle.com';

async function run() {
// https://paddle.com/docs/api-list-products
Expand Down
2 changes: 1 addition & 1 deletion tests/esm/index.js
Expand Up @@ -2,7 +2,7 @@ import assert from 'assert';
import nock from 'nock';
import { PaddleSDK } from 'paddle-sdk';

const SERVER = 'http://test.paddle.com';
const SERVER = 'https://test.paddle.com';

async function run() {
// https://paddle.com/docs/api-list-products
Expand Down
2 changes: 1 addition & 1 deletion utils/nock.ts
@@ -1,6 +1,6 @@
import nock from 'nock';

export const SERVER = 'http://test.paddle.com';
export const SERVER = 'https://test.paddle.com';

export default function getNock() {
return nock(SERVER, {
Expand Down

0 comments on commit 13b8351

Please sign in to comment.