Skip to content

Commit

Permalink
fix: Fix CommonJS and ESM exports
Browse files Browse the repository at this point in the history
  • Loading branch information
avaly committed Mar 30, 2023
1 parent 9bf16c3 commit 0529f9c
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/workflows/tests.yaml
Expand Up @@ -35,3 +35,4 @@ jobs:
- run: yarn test:pretty
- run: yarn test
- run: yarn test:types
- run: yarn test:build
12 changes: 7 additions & 5 deletions .gitignore
Expand Up @@ -7,8 +7,10 @@ Thumbs.db
*.sublime-workspace
*.log
.eslintcache
cjs/
coverage/
docs/
esm/
tmp/
/build
/cjs
/coverage
/docs
/esm
/tests/*/yarn.lock
/tmp
9 changes: 9 additions & 0 deletions build.sh
Expand Up @@ -2,6 +2,15 @@

set -euxo pipefail

# Version

VERSION=$(node -e "console.log(require('./package.json').version)")

cat << EOF > ./src/version.ts
// Generated by yarn build
export const VERSION = '${VERSION}';
EOF

# ESM

tsc --project ./tsconfig-esm.json
Expand Down
10 changes: 7 additions & 3 deletions package.json
Expand Up @@ -3,8 +3,11 @@
"description": "The Paddle.com Node.js SDK",
"version": "4.0.0",
"type": "module",
"main": "./cjs/sdk.js",
"exports": "./esm/sdk.js",
"main": "./cjs/index.js",
"exports": {
"import": "./esm/index.js",
"require": "./cjs/index.js"
},
"types": "./esm/sdk.d.ts",
"author": "Valentin Agachi <github-com@agachi.name>",
"repository": {
Expand Down Expand Up @@ -32,9 +35,10 @@
"lint": "eslint . --cache --fix --ignore-pattern '!.eslintrc.cjs'",
"prepack": "yarn build",
"pretty": "prettier --write --list-different \"**/*.ts\"",
"release": "standard-version -a",
"release": "yarn build && git add ./src/version.ts && standard-version -a",
"test": "yarn test:jest",
"test:coverage": "jest --coverage",
"test:build": "./tests/build.sh",
"test:jest": "jest",
"test:pretty": "prettier --check \"**/*.ts\"",
"test:types": "tsc --noEmit",
Expand Down
6 changes: 2 additions & 4 deletions src/sdk.ts
@@ -1,6 +1,5 @@
import crypto from 'crypto';
import got from 'got';
import fs from 'fs';

import serialize from './serialize';
import { OptionsOfDefaultResponseBody } from 'got/dist/source/create';
Expand All @@ -25,8 +24,7 @@ import {
UpdateSubscriptionUserBody,
UpdateSubscriptionUserResponse,
} from './types';

const PACKAGE = JSON.parse(fs.readFileSync('./package.json', 'utf-8'));
import { VERSION } from './version';

const VENDOR_SANDBOX_URL = 'https://sandbox-vendors.paddle.com/api/2.0';
const VENDOR_SERVER_URL = 'https://vendors.paddle.com/api/2.0';
Expand Down Expand Up @@ -528,7 +526,7 @@ s * @example

const options: OptionsOfDefaultResponseBody = {
headers: {
'User-Agent': `paddle-sdk/${PACKAGE.version} (${PACKAGE.repository.url})`,
'User-Agent': `paddle-sdk/${VERSION} (https://github.com/avaly/paddle-sdk)`,
...(headers || {}),
},
method,
Expand Down
2 changes: 2 additions & 0 deletions src/version.ts
@@ -0,0 +1,2 @@
// Generated by yarn build
export const VERSION = '4.0.0';
19 changes: 19 additions & 0 deletions tests/build.sh
@@ -0,0 +1,19 @@
#!/bin/bash

set -euo pipefail

npm pack

mkdir ./build 2>/dev/null || true
mv -f paddle-sdk*.tgz ./build/paddle-sdk.tgz
tar xf ./build/paddle-sdk.tgz --directory=./build/

cd tests/esm/
rm -f package-lock.json
yarn
node ./index.js

cd ../cjs/
rm -f package-lock.json
yarn
node ./index.js
65 changes: 65 additions & 0 deletions tests/cjs/index.js
@@ -0,0 +1,65 @@
const assert = require('assert');
const nock = require('nock');
const { PaddleSDK } = require('paddle-sdk');

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

async function run() {
// https://paddle.com/docs/api-list-products
nock(SERVER, {
reqheaders: {
'user-agent': /paddle-sdk\/\d+/,
},
}).post('/product/get_products', {
vendor_id: 'test-id',
vendor_auth_code: 'test-key',
}).reply(200, {
success: true,
response: {
total: 2,
count: 2,
products: [
{
id: 10000,
name: 'A Product',
description: 'A description of the product.',
base_price: 58,
sale_price: null,
screenshots: [],
icon: 'https://paddle-static.s3.amazonaws.com/email/2013-04-10/og.png',
},
{
id: 20000,
name: 'Another Product',
description: null,
base_price: 39.99,
sale_price: null,
screenshots: [],
icon: 'https://paddle.s3.amazonaws.com/user/91/489278geekbench.png',
},
],
},
});;

const paddle = new PaddleSDK('test-id', 'test-key', '', {
server: SERVER,
});

const products = await paddle.getProducts();

assert.strictEqual(typeof products, 'object');
assert.strictEqual(products.count, 2);
assert.strictEqual(products.products.length, 2);
assert.strictEqual(products.products[0].id, 10000);
assert.strictEqual(products.products[1].id, 20000);

nock.cleanAll();
}

run().then(() => {
console.log('OK');
}).catch((err) => {
console.error(err);
process.exit(1);
});

11 changes: 11 additions & 0 deletions tests/cjs/package.json
@@ -0,0 +1,11 @@
{
"name": "test-cjs",
"type": "commonjs",
"dependencies": {
"paddle-sdk": "file:../../build/package"
},
"volta": {
"node": "14.21.3",
"yarn": "1.22.10"
}
}
61 changes: 61 additions & 0 deletions tests/esm/index.js
@@ -0,0 +1,61 @@
import assert from 'assert';
import nock from 'nock';
import { PaddleSDK } from 'paddle-sdk';

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

async function run() {
// https://paddle.com/docs/api-list-products
nock(SERVER, {
reqheaders: {
'user-agent': /paddle-sdk\/\d+/,
},
}).post('/product/get_products', {
vendor_id: 'test-id',
vendor_auth_code: 'test-key',
}).reply(200, {
success: true,
response: {
total: 2,
count: 2,
products: [
{
id: 10000,
name: 'A Product',
description: 'A description of the product.',
base_price: 58,
sale_price: null,
screenshots: [],
icon: 'https://paddle-static.s3.amazonaws.com/email/2013-04-10/og.png',
},
{
id: 20000,
name: 'Another Product',
description: null,
base_price: 39.99,
sale_price: null,
screenshots: [],
icon: 'https://paddle.s3.amazonaws.com/user/91/489278geekbench.png',
},
],
},
});;

const paddle = new PaddleSDK('test-id', 'test-key', '', {
server: SERVER,
});

const products = await paddle.getProducts();

assert.strictEqual(typeof products, 'object');
assert.strictEqual(products.count, 2);
assert.strictEqual(products.products.length, 2);
assert.strictEqual(products.products[0].id, 10000);
assert.strictEqual(products.products[1].id, 20000);

nock.cleanAll();
}

await run();

console.log('OK');
11 changes: 11 additions & 0 deletions tests/esm/package.json
@@ -0,0 +1,11 @@
{
"name": "test-esm",
"type": "module",
"dependencies": {
"paddle-sdk": "file:../../build/package"
},
"volta": {
"node": "14.21.3",
"yarn": "1.22.10"
}
}

0 comments on commit 0529f9c

Please sign in to comment.