@paddle/paddle-node-sdk 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -11,11 +11,11 @@ For working with Paddle in your frontend, use [Paddle.js](https://developer.padd
11
11
 
12
12
  ## Before you begin
13
13
 
14
- This SDK is in early access, which means that we're actively developing it based on feedback from our developer community. **We don't recommend that you use it in production.**
14
+ This SDK is in early access, which means that we're actively developing it based on feedback from our developer community.
15
15
 
16
16
  If you've used this SDK, we'd love to hear how you found it! Email us at [team-dx@paddle.com](mailto:team-dx@paddle.com) with any thoughts.
17
17
 
18
- While in early access, we may introduce breaking changes. Where we can, we'll tag breaking changes and communicate ahead of time.
18
+ While in early access, we may introduce breaking changes. Where we can, we'll tag breaking changes and communicate ahead of time. Please pin the version of the SDK in your `package.json` to avoid downloading newer versions with potential breaking changes.
19
19
 
20
20
  ## Installation
21
21
 
@@ -51,8 +51,12 @@ const paddle = new Paddle('API_KEY')
51
51
  ...
52
52
 
53
53
  async function getProducts() {
54
- const products = await paddle.products.list()
55
- return products.next()
54
+ try {
55
+ const productCollection = await paddle.products.list()
56
+ return productCollection.next()
57
+ } catch (e) {
58
+ // Handle Network/API errors
59
+ }
56
60
  }
57
61
 
58
62
  const products = await getProducts()
@@ -87,19 +91,23 @@ const paddle = new Paddle('API_KEY')
87
91
 
88
92
  async function getProducts() {
89
93
  // creates an iterator instance
90
- const products = await paddle.products.list();
91
- return products;
94
+ const productCollection = await paddle.products.list();
95
+ return productCollection;
92
96
  }
93
97
 
94
- // Creates an iterator to loop through paginated data.
95
- // At this point, It does not hold any data.
96
- const products = await getProducts();
97
-
98
- // Call `next()` to retrieve first page of the dataset
99
- const firstPage = await products.next()
100
-
101
- // Calling `next()` again will retrieve subsequent pages.
102
- const secondPage = await products.next()
98
+ try {
99
+ // Creates an iterator to loop through paginated data.
100
+ // At this point, It does not hold any data.
101
+ const productCollection = await getProducts();
102
+
103
+ // Call `next()` to retrieve first page of the dataset
104
+ const firstPage = await productCollection.next()
105
+
106
+ // Calling `next()` again will retrieve subsequent pages.
107
+ const secondPage = await productCollection.next()
108
+ } catch (e) {
109
+ // Handle Network/API errors
110
+ }
103
111
  ```
104
112
 
105
113
  ### Create an entity
@@ -114,10 +122,14 @@ const paddle = new Paddle('API_KEY')
114
122
  ...
115
123
 
116
124
  async function createProduct(requestBody: CreateProductRequestBody) {
117
- // Pass the request body with the attributes required to create
118
- const product = await paddle.products.create(requestBody)
119
- // Returns a product entity
120
- return product
125
+ try {
126
+ // Pass the request body with the attributes required to create
127
+ const product = await paddle.products.create(requestBody)
128
+ // Returns a product entity
129
+ return product
130
+ } catch (e) {
131
+ // Handle Network/API errors
132
+ }
121
133
  }
122
134
 
123
135
  const product = await createProduct({... object with required attributes})
@@ -135,10 +147,14 @@ const paddle = new Paddle('API_KEY')
135
147
  ...
136
148
 
137
149
  async function updateProduct(productId: string, requestBody: UpdateProductRequestBody) {
138
- // Pass the product id and request body with the attributes to update
139
- const product = await paddle.products.update(productId, requestBody)
140
- // Returns an updated product entity
141
- return product
150
+ try {
151
+ // Pass the product id and request body with the attributes to update
152
+ const product = await paddle.products.update(productId, requestBody)
153
+ // Returns an updated product entity
154
+ return product
155
+ } catch (e) {
156
+ // Handle Network/API errors
157
+ }
142
158
  }
143
159
 
144
160
  const product = await updateProduct('id', {... object with attributes to be updated})
@@ -162,10 +178,14 @@ const paddle = new Paddle('API_KEY')
162
178
  ...
163
179
 
164
180
  async function getProduct(productId: string) {
165
- // Pass the product id get
166
- const product = await paddle.products.get(productId)
167
- // Returns an updated product entity
168
- return product
181
+ try {
182
+ // Pass the product id get
183
+ const product = await paddle.products.get(productId)
184
+ // Returns an updated product entity
185
+ return product
186
+ } catch (e) {
187
+ // Handle Network/API errors
188
+ }
169
189
  }
170
190
 
171
191
  const product = await getProduct('id')
@@ -229,7 +249,7 @@ This table shows which operations are available as of the latest release.
229
249
  | Get a transaction | ✅ |
230
250
  | Update a transaction | ✅ |
231
251
  | Preview a transaction | ❌ |
232
- | Get a PDF invoice for a transaction | ❌ |
252
+ | Get a PDF invoice for a transaction | ✅ |
233
253
  | **Subscriptions** | **Full** |
234
254
  | List subscriptions | ✅ |
235
255
  | Get a subscription | ✅ |
@@ -21,7 +21,7 @@ export declare class SubscriptionIncludes {
21
21
  readonly current_billing_period?: SubscriptionTimePeriod | null | undefined;
22
22
  readonly billing_cycle: TimePeriod;
23
23
  readonly scheduled_change?: SubscriptionScheduledChange | null | undefined;
24
- readonly management_urls: SubscriptionManagement;
24
+ readonly management_urls?: SubscriptionManagement | null | undefined;
25
25
  readonly items: SubscriptionItem[];
26
26
  readonly custom_data?: CustomData | null | undefined;
27
27
  readonly next_transaction?: NextTransaction | null | undefined;
@@ -27,7 +27,9 @@ class SubscriptionIncludes {
27
27
  this.scheduled_change = subscription.scheduled_change
28
28
  ? new index_1.SubscriptionScheduledChange(subscription.scheduled_change)
29
29
  : null;
30
- this.management_urls = new index_1.SubscriptionManagement(subscription.management_urls);
30
+ this.management_urls = subscription.management_urls
31
+ ? new index_1.SubscriptionManagement(subscription.management_urls)
32
+ : null;
31
33
  this.items = subscription.items.map((item) => new index_1.SubscriptionItem(item));
32
34
  this.custom_data = subscription.custom_data;
33
35
  this.next_transaction = subscription.next_transaction ? new index_1.NextTransaction(subscription.next_transaction) : null;
@@ -21,7 +21,7 @@ export declare class Subscription {
21
21
  readonly current_billing_period?: SubscriptionTimePeriod | undefined | null;
22
22
  readonly billing_cycle: TimePeriod;
23
23
  readonly scheduled_change?: SubscriptionScheduledChange | undefined | null;
24
- readonly management_urls: SubscriptionManagement;
24
+ readonly management_urls?: SubscriptionManagement | undefined | null;
25
25
  readonly items: SubscriptionItem[];
26
26
  readonly custom_data?: CustomData | undefined | null;
27
27
  constructor(subscription: ISubscription);
@@ -27,7 +27,9 @@ class Subscription {
27
27
  this.scheduled_change = subscription.scheduled_change
28
28
  ? new index_1.SubscriptionScheduledChange(subscription.scheduled_change)
29
29
  : undefined;
30
- this.management_urls = new index_1.SubscriptionManagement(subscription.management_urls);
30
+ this.management_urls = subscription.management_urls
31
+ ? new index_1.SubscriptionManagement(subscription.management_urls)
32
+ : undefined;
31
33
  this.items = subscription.items.map((item) => new index_1.SubscriptionItem(item));
32
34
  this.custom_data = subscription.custom_data;
33
35
  }
@@ -10,3 +10,4 @@ export * from './adjustment-totals';
10
10
  export * from './transaction-includes';
11
11
  export * from './transaction';
12
12
  export * from './transaction-collection';
13
+ export * from './transaction-invoice-pdf';
@@ -26,3 +26,4 @@ __exportStar(require("./adjustment-totals"), exports);
26
26
  __exportStar(require("./transaction-includes"), exports);
27
27
  __exportStar(require("./transaction"), exports);
28
28
  __exportStar(require("./transaction-collection"), exports);
29
+ __exportStar(require("./transaction-invoice-pdf"), exports);
@@ -0,0 +1,6 @@
1
+ import { type ITransactionInvoicePDF } from '../../types';
2
+ export declare class TransactionInvoicePDF {
3
+ readonly url: string;
4
+ constructor(transaction: ITransactionInvoicePDF);
5
+ toJson(): ITransactionInvoicePDF;
6
+ }
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TransactionInvoicePDF = void 0;
4
+ class TransactionInvoicePDF {
5
+ constructor(transaction) {
6
+ this.url = transaction.url;
7
+ }
8
+ toJson() {
9
+ return {
10
+ url: this.url,
11
+ };
12
+ }
13
+ }
14
+ exports.TransactionInvoicePDF = TransactionInvoicePDF;
@@ -17,6 +17,8 @@ const constants_1 = require("./constants");
17
17
  const node_fetch_1 = __importDefault(require("node-fetch"));
18
18
  const version_1 = require("../../version");
19
19
  const environment_1 = require("./environment");
20
+ const crypto_1 = require("crypto");
21
+ const logger_1 = require("../base/logger");
20
22
  class Client {
21
23
  constructor(apiKey, options) {
22
24
  this.apiKey = apiKey;
@@ -28,9 +30,11 @@ class Client {
28
30
  return urlBasedOnEnv || '';
29
31
  }
30
32
  getHeaders() {
33
+ const uuid = (0, crypto_1.randomUUID)();
31
34
  return {
32
35
  Authorization: `bearer ${this.apiKey}`,
33
36
  'user-agent': `PaddleSDK/node ${version_1.SDK_VERSION}`,
37
+ 'X-Transaction-ID': uuid,
34
38
  };
35
39
  }
36
40
  get(url, queryParams) {
@@ -39,29 +43,41 @@ class Client {
39
43
  if (!finalUrl.includes('?') && queryParams) {
40
44
  finalUrl += queryParams.toQueryString();
41
45
  }
46
+ const logUrl = url.split('?')[0];
47
+ const headers = this.getHeaders();
48
+ logger_1.Logger.logRequest('GET', logUrl, headers);
42
49
  const rawResponse = yield (0, node_fetch_1.default)(finalUrl, {
43
- headers: this.getHeaders(),
50
+ headers,
44
51
  });
52
+ logger_1.Logger.logResponse('GET', logUrl, headers, rawResponse);
45
53
  return rawResponse.json();
46
54
  });
47
55
  }
48
56
  post(url, requestBody) {
49
57
  return __awaiter(this, void 0, void 0, function* () {
58
+ const logUrl = url.split('?')[0];
59
+ const headers = this.getHeaders();
60
+ logger_1.Logger.logRequest('POST', logUrl, headers);
50
61
  const rawResponse = yield (0, node_fetch_1.default)(`${this.baseUrl}${url}`, {
51
62
  method: 'POST',
52
63
  body: JSON.stringify(requestBody),
53
- headers: this.getHeaders(),
64
+ headers,
54
65
  });
66
+ logger_1.Logger.logResponse('POST', logUrl, headers, rawResponse);
55
67
  return rawResponse.json();
56
68
  });
57
69
  }
58
70
  patch(url, requestBody) {
59
71
  return __awaiter(this, void 0, void 0, function* () {
72
+ const logUrl = url.split('?')[0];
73
+ const headers = this.getHeaders();
74
+ logger_1.Logger.logRequest('PATCH', logUrl, headers);
60
75
  const rawResponse = yield (0, node_fetch_1.default)(`${this.baseUrl}${url}`, {
61
76
  method: 'PATCH',
62
77
  body: JSON.stringify(requestBody),
63
- headers: this.getHeaders(),
78
+ headers,
64
79
  });
80
+ logger_1.Logger.logResponse('PATCH', logUrl, headers, rawResponse);
65
81
  return rawResponse.json();
66
82
  });
67
83
  }
@@ -0,0 +1,10 @@
1
+ import { type Response } from 'node-fetch';
2
+ type LogInputProps = Array<string | undefined | null>;
3
+ export declare class Logger {
4
+ static log(...args: LogInputProps): void;
5
+ static warn(...args: LogInputProps): void;
6
+ static error(...args: LogInputProps): void;
7
+ static logRequest(method: string, url: string | undefined, headers: Record<string, string>): void;
8
+ static logResponse(method: string, url: string | undefined, headers: Record<string, string>, promise: Response): void;
9
+ }
10
+ export {};
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Logger = void 0;
4
+ class Logger {
5
+ static log(...args) {
6
+ console.log('[Paddle] [LOG]', ...args);
7
+ }
8
+ static warn(...args) {
9
+ console.warn('[Paddle] [WARN]', ...args);
10
+ }
11
+ static error(...args) {
12
+ console.error('[Paddle] [ERROR]', ...args);
13
+ }
14
+ static logRequest(method, url, headers) {
15
+ Logger.log('[Request]', method, url, 'Transaction ID:', headers['X-Transaction-ID']);
16
+ }
17
+ static logResponse(method, url, headers, promise) {
18
+ Logger.log('[Response]', method, url, promise.status.toString(), 'Transaction ID:', headers['X-Transaction-ID'], 'Request ID:', promise.headers.get('Request-Id'));
19
+ }
20
+ }
21
+ exports.Logger = Logger;
@@ -1,10 +1,11 @@
1
- import { Transaction, TransactionCollection, TransactionIncludes } from '../../entities';
1
+ import { Transaction, TransactionCollection, TransactionIncludes, TransactionInvoicePDF } from '../../entities';
2
2
  import { BaseResource } from '../../internal/base';
3
- import { type CreateTransactionRequestBody, type CreateTransactionQueryParameters, type GetTransactionQueryParameters, type ListTransactionQueryParameters, type UpdateTransactionRequestBody } from './operations';
3
+ import { type CreateTransactionQueryParameters, type CreateTransactionRequestBody, type GetTransactionQueryParameters, type ListTransactionQueryParameters, type UpdateTransactionRequestBody } from './operations';
4
4
  export * from './operations';
5
5
  export declare class TransactionsResource extends BaseResource {
6
6
  list(queryParams?: ListTransactionQueryParameters): Promise<TransactionCollection>;
7
7
  create(createTransactionParameters: CreateTransactionRequestBody, queryParams?: CreateTransactionQueryParameters): Promise<TransactionIncludes>;
8
8
  update(transactionId: string, updateTransaction: UpdateTransactionRequestBody): Promise<Transaction>;
9
9
  get(transactionId: string, queryParams?: GetTransactionQueryParameters): Promise<TransactionIncludes>;
10
+ getInvoicePDF(transactionId: string): Promise<TransactionInvoicePDF>;
10
11
  }
@@ -31,6 +31,7 @@ const TransactionPaths = {
31
31
  create: '/transactions',
32
32
  get: '/transactions/{transaction_id}',
33
33
  update: '/transactions/{transaction_id}',
34
+ getInvoicePDF: '/transactions/{transaction_id}/invoice',
34
35
  };
35
36
  __exportStar(require("./operations"), exports);
36
37
  class TransactionsResource extends base_1.BaseResource {
@@ -69,5 +70,15 @@ class TransactionsResource extends base_1.BaseResource {
69
70
  return new entities_1.TransactionIncludes(data);
70
71
  });
71
72
  }
73
+ getInvoicePDF(transactionId) {
74
+ return __awaiter(this, void 0, void 0, function* () {
75
+ const urlWithPathParams = new base_1.PathParameters(TransactionPaths.getInvoicePDF, {
76
+ transaction_id: transactionId,
77
+ }).deriveUrl();
78
+ const response = yield this.client.get(urlWithPathParams);
79
+ const data = this.handleResponse(response);
80
+ return new entities_1.TransactionInvoicePDF(data);
81
+ });
82
+ }
72
83
  }
73
84
  exports.TransactionsResource = TransactionsResource;
@@ -20,7 +20,7 @@ export interface ISubscription {
20
20
  current_billing_period?: ISubscriptionTimePeriod | undefined | null;
21
21
  billing_cycle: ITimePeriod;
22
22
  scheduled_change?: ISubscriptionScheduledChange | undefined | null;
23
- management_urls: ISubscriptionManagement;
23
+ management_urls: ISubscriptionManagement | undefined | null;
24
24
  items: ISubscriptionItem[];
25
25
  custom_data?: ICustomData | undefined | null;
26
26
  next_transaction?: INextTransaction | undefined | null;
@@ -8,3 +8,4 @@ export * from './transaction-adjustment';
8
8
  export * from './adjustment-totals-breakdown';
9
9
  export * from './adjustment-totals';
10
10
  export * from './transaction';
11
+ export * from './transaction-invoice-pdf';
@@ -24,3 +24,4 @@ __exportStar(require("./transaction-adjustment"), exports);
24
24
  __exportStar(require("./adjustment-totals-breakdown"), exports);
25
25
  __exportStar(require("./adjustment-totals"), exports);
26
26
  __exportStar(require("./transaction"), exports);
27
+ __exportStar(require("./transaction-invoice-pdf"), exports);
@@ -0,0 +1,3 @@
1
+ export interface ITransactionInvoicePDF {
2
+ url: string;
3
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const SDK_VERSION = "0.1.4";
1
+ export declare const SDK_VERSION = "0.1.6";
package/dist/version.js CHANGED
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SDK_VERSION = void 0;
4
- exports.SDK_VERSION = '0.1.4';
4
+ exports.SDK_VERSION = '0.1.6';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@paddle/paddle-node-sdk",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "A NodeJS SDK for Paddle Billing API",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",