Skip to content

Commit bbc0445

Browse files
jamiecobbettclaude
andcommitted
Add ESM-first dual module support
This change adds full ES Module support while maintaining backward compatibility with CommonJS. Key Changes: - Package is now "type": "module" with ESM as primary format - Dual compilation: ESM (dist/esm/) and CJS (dist/cjs/) - Package.json exports map for conditional resolution - Updated all imports to include .js extensions for ESM compatibility Module Support: - ESM: import gocardless from 'gocardless-nodejs' - CJS: const gocardless = require('gocardless-nodejs') - Subpath exports: import { parse } from 'gocardless-nodejs/webhooks' - TypeScript types for both formats - Tree-shaking support for modern bundlers Build Output: - dist/esm/ - ES Modules (primary) - dist/cjs/ - CommonJS (compatibility layer) - dist/types/ - Shared TypeScript definitions Fixes #124 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent e67da94 commit bbc0445

371 files changed

Lines changed: 14647 additions & 275 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,31 @@ To initialise the client, you must provide:
2020
- The environment that this token is for (see [here](https://github.com/gocardless/gocardless-nodejs/blob/master/src/constants.ts) for a list of available environments).
2121
- Any additional options (see [here](#available-client-options) for a list of supported options).
2222

23-
<!-- prettier-ignore -->
2423
```js
25-
const gocardless = require('gocardless-nodejs/client');
26-
const constants = require('gocardless-nodejs/constants');
27-
24+
import gocardless from 'gocardless-nodejs';
25+
import { Environments } from 'gocardless-nodejs/constants';
2826

29-
// Initialise the client.
27+
// Initialize the client
3028
const client = gocardless(
3129
process.env.GC_ACCESS_TOKEN,
32-
constants.Environments.Sandbox,
33-
{ raiseOnIdempotencyConflict: true },
30+
Environments.Sandbox,
31+
{ raiseOnIdempotencyConflict: true }
32+
);
33+
```
34+
35+
TypeScript:
36+
37+
```typescript
38+
import gocardless, { GoCardlessClient } from 'gocardless-nodejs';
39+
import { Environments } from 'gocardless-nodejs/constants';
40+
import type { Payment } from 'gocardless-nodejs/types';
41+
42+
const client: GoCardlessClient = gocardless(
43+
process.env.GC_ACCESS_TOKEN,
44+
Environments.Sandbox
3445
);
46+
47+
const payment: Payment = await client.payments.find('PM123');
3548
```
3649

3750
### The Basics
@@ -42,7 +55,7 @@ For a full list of available resources, visit the [GoCardless API reference](htt
4255

4356
<!-- prettier-ignore -->
4457
```js
45-
const uuidv4 = require('uuid/v4');
58+
import { v4 as uuidv4 } from 'uuid';
4659

4760
// Create a new payment.
4861
const payment = await client.payments.create(
@@ -86,3 +99,13 @@ for await (const payment of client.payments.all()) {
8699
### Available client options
87100

88101
- `raiseOnIdempotencyConflict`: set to `true` to raise exceptions on [idempotency](https://developer.gocardless.com/api-reference/#making-requests-idempotency-keys) conflicts. Defaults to `false`.
102+
103+
104+
### CommonJS backwards compatibility
105+
106+
We provide a CommonJS implementation for backwards compatibility. For CommonJS, change the imports to:
107+
108+
```js
109+
const gocardless = require('gocardless-nodejs');
110+
const { Environments } = require('gocardless-nodejs/constants');
111+
```

dist-cjs-package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "commonjs"
3+
}

dist-esm-package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "module"
3+
}

dist/cjs/api/api.js

Lines changed: 182 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/cjs/api/api.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)