@bunnyapp/api-client 2.2.3 → 3.0.0

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.
Files changed (70) hide show
  1. package/README.md +304 -66
  2. package/dist/generated/graphql.d.ts +9410 -0
  3. package/dist/generated/graphql.d.ts.map +1 -0
  4. package/dist/generated/graphql.js +3 -0
  5. package/dist/generated/graphql.js.map +1 -0
  6. package/dist/helpers/account-update-by-tenant-code.d.ts +10 -0
  7. package/dist/helpers/account-update-by-tenant-code.d.ts.map +1 -0
  8. package/dist/helpers/account-update-by-tenant-code.js +71 -0
  9. package/dist/helpers/account-update-by-tenant-code.js.map +1 -0
  10. package/dist/helpers/feature-usage-create.d.ts +12 -0
  11. package/dist/helpers/feature-usage-create.d.ts.map +1 -0
  12. package/dist/helpers/feature-usage-create.js +55 -0
  13. package/dist/helpers/feature-usage-create.js.map +1 -0
  14. package/dist/helpers/portal-session-create.d.ts +10 -0
  15. package/dist/helpers/portal-session-create.d.ts.map +1 -0
  16. package/dist/helpers/portal-session-create.js +37 -0
  17. package/dist/helpers/portal-session-create.js.map +1 -0
  18. package/dist/helpers/subscription-cancel.d.ts +8 -0
  19. package/dist/helpers/subscription-cancel.d.ts.map +1 -0
  20. package/dist/helpers/subscription-cancel.js +29 -0
  21. package/dist/helpers/subscription-cancel.js.map +1 -0
  22. package/dist/helpers/subscription-create.d.ts +39 -0
  23. package/dist/helpers/subscription-create.d.ts.map +1 -0
  24. package/dist/helpers/subscription-create.js +112 -0
  25. package/dist/helpers/subscription-create.js.map +1 -0
  26. package/dist/helpers/tenant-by-code.d.ts +9 -0
  27. package/dist/helpers/tenant-by-code.d.ts.map +1 -0
  28. package/{src → dist}/helpers/tenant-by-code.js +18 -17
  29. package/dist/helpers/tenant-by-code.js.map +1 -0
  30. package/dist/helpers/tenant-create.d.ts +13 -0
  31. package/dist/helpers/tenant-create.d.ts.map +1 -0
  32. package/dist/helpers/tenant-create.js +52 -0
  33. package/dist/helpers/tenant-create.js.map +1 -0
  34. package/dist/helpers/tenant-metrics-update.d.ts +22 -0
  35. package/dist/helpers/tenant-metrics-update.d.ts.map +1 -0
  36. package/dist/helpers/tenant-metrics-update.js +36 -0
  37. package/dist/helpers/tenant-metrics-update.js.map +1 -0
  38. package/dist/helpers/tenant-update.d.ts +11 -0
  39. package/dist/helpers/tenant-update.d.ts.map +1 -0
  40. package/dist/helpers/tenant-update.js +45 -0
  41. package/dist/helpers/tenant-update.js.map +1 -0
  42. package/dist/index.d.ts +58 -0
  43. package/dist/index.d.ts.map +1 -0
  44. package/dist/index.js +127 -0
  45. package/dist/index.js.map +1 -0
  46. package/dist/types/graphql.d.ts +2 -0
  47. package/dist/types/graphql.d.ts.map +1 -0
  48. package/dist/types/graphql.js +19 -0
  49. package/dist/types/graphql.js.map +1 -0
  50. package/dist/types/index.d.ts +2 -0
  51. package/dist/types/index.d.ts.map +1 -0
  52. package/dist/types/index.js +18 -0
  53. package/dist/types/index.js.map +1 -0
  54. package/dist/webhooks.d.ts +7 -0
  55. package/dist/webhooks.d.ts.map +1 -0
  56. package/dist/webhooks.js +31 -0
  57. package/dist/webhooks.js.map +1 -0
  58. package/package.json +26 -9
  59. package/index.js +0 -1
  60. package/src/helpers/account-update-by-tenant-code.js +0 -63
  61. package/src/helpers/feature-usage-create.js +0 -58
  62. package/src/helpers/portal-session-create.js +0 -38
  63. package/src/helpers/product-create.js +0 -40
  64. package/src/helpers/subscription-cancel.js +0 -29
  65. package/src/helpers/subscription-create.js +0 -107
  66. package/src/helpers/tenant-create.js +0 -55
  67. package/src/helpers/tenant-metrics-update.js +0 -34
  68. package/src/helpers/tenant-update.js +0 -38
  69. package/src/index.js +0 -102
  70. package/src/webhooks.js +0 -24
package/README.md CHANGED
@@ -17,8 +17,22 @@ Create a Bunny api client using either a valid access token or client credential
17
17
  The benefit of providing an accessToken is the request will be faster as an access token does not need to be generated. The
18
18
  downside of this approach is that if the token expires then your requests will start to fail.
19
19
 
20
- ```js
20
+ **TypeScript:**
21
+
22
+ ```typescript
23
+ import Bunny from "@bunnyapp/api-client";
24
+
25
+ const bunny = new Bunny({
26
+ baseUrl: "https://<subdomain>.bunny.com",
27
+ accessToken: "<bunny-access-token>",
28
+ });
29
+ ```
30
+
31
+ **JavaScript:**
32
+
33
+ ```javascript
21
34
  const BunnyClient = require("@bunnyapp/api-client");
35
+
22
36
  const bunny = new BunnyClient({
23
37
  baseUrl: "https://<subdomain>.bunny.com",
24
38
  accessToken: "<bunny-access-token>",
@@ -29,8 +43,24 @@ const bunny = new BunnyClient({
29
43
 
30
44
  Alternately you can provide clientId, clientSecret, & scope. In this case the client will generate an access token and if the token expires it will generate another one.
31
45
 
32
- ```js
46
+ **TypeScript:**
47
+
48
+ ```typescript
49
+ import Bunny from "@bunnyapp/api-client";
50
+
51
+ const bunny = new Bunny({
52
+ baseUrl: "https://<subdomain>.bunny.com",
53
+ clientId: "<bunny-client-id>",
54
+ clientSecret: "<bunny-client-secret>",
55
+ scope: "standard:read standard:write",
56
+ });
57
+ ```
58
+
59
+ **JavaScript:**
60
+
61
+ ```javascript
33
62
  const BunnyClient = require("@bunnyapp/api-client");
63
+
34
64
  const bunny = new BunnyClient({
35
65
  baseUrl: "https://<subdomain>.bunny.com",
36
66
  clientId: "<bunny-client-id>",
@@ -41,101 +71,286 @@ const bunny = new BunnyClient({
41
71
 
42
72
  ## Helper methods
43
73
 
44
- This SDK wrappers several of the common Bunny API requests. The response varies based on the method that is used.
74
+ This SDK provides helper methods for common Bunny API operations. The response types match the GraphQL schema.
45
75
 
46
- ```js
47
- // Create a new subscription
48
- const res = await bunny.subscriptionCreate("priceListCode", {
49
- trial: true,
50
- accountName: "accountName",
51
- firstName: "firstName",
52
- lastName: "lastName",
53
- email: "email@example.com",
54
- tenantCode: "remoteId",
55
- });
76
+ **TypeScript:**
56
77
 
57
- // On success res will be a subscription object
58
- {
59
- id: '17',
60
- account: { id: '18', name: 'accountName', contacts: [ [Object] ] },
61
- trialStartDate: '2023-07-17',
62
- trialEndDate: '2023-07-30',
63
- startDate: '2023-07-31',
64
- endDate: '2024-07-30',
65
- state: 'TRIAL',
66
- plan: { code: 'bunny_medium', name: 'Medium' },
67
- priceList: { code: 'bunny_medium_monthly', name: 'Monthly' },
68
- tenant: { id: '12', code: 'remoteId', name: 'accountName' }
69
- }
78
+ ```typescript
79
+ import {
80
+ SubscriptionOptions,
81
+ Subscription,
82
+ Mutation,
83
+ Tenants,
84
+ } from "@bunnyapp/api-client";
70
85
 
86
+ // Create a new subscription
87
+ const subscription: Subscription = await bunny.subscriptionCreate(
88
+ "priceListCode",
89
+ {
90
+ // Subscription settings
91
+ trial: false, // Optional: Start with a trial period
92
+ evergreen: true, // Optional: Auto-renew subscription
93
+
94
+ // Account identification (Must use either accountId OR account details)
95
+ accountId: "existing-123", // Required unless using accountName: Use existing account
96
+
97
+ // Account details (used if accountId is not provided)
98
+ accountName: "Acme Corp", // Required unless using accountId: Company/Account name
99
+ ownerUserId: "user-123",
100
+ phone: "555-0123",
101
+ fax: "555-0124",
102
+ website: "www.acme.com",
103
+
104
+ // Optional: Billing address
105
+ billingStreet: "123 Main St",
106
+ billingCity: "San Francisco",
107
+ billingZip: "94105",
108
+ billingState: "CA",
109
+ billingCountry: "US",
110
+
111
+ // Billing contact information
112
+ firstName: "John", // Required
113
+ lastName: "Doe",
114
+ email: "john@acme.com", // Required
115
+ mobile: "555-0125",
116
+ salutation: "Mr",
117
+ title: "CEO",
118
+
119
+ // Optional: Billing contact mailing address
120
+ mailingStreet: "456 Market St",
121
+ mailingCity: "San Francisco",
122
+ mailingZip: "94105",
123
+ mailingState: "CA",
124
+ mailingCountry: "US",
125
+
126
+ // Optional: Tenant information
127
+ tenantCode: "tenant-123",
128
+ tenantName: "Acme Team",
129
+ }
130
+ );
71
131
 
72
132
  // Get a session token for the Bunny customer portal
73
- const res = await bunny.portalSessionCreate("tenantCode");
133
+ const portalSession = await bunny.portalSessionCreate("tenantCode");
74
134
 
75
135
  // Optionally supply a return url to get back to your app
76
- const res = await bunny.portalSessionCreate(
136
+ const portalSessionWithUrl = await bunny.portalSessionCreate(
77
137
  "tenantCode",
78
138
  "https://example.com"
79
139
  );
80
140
 
81
141
  // Default session length is 24 hours but you can change it. e.g 12 hours
82
- const res = await bunny.portalSessionCreate(
142
+ const portalSessionWithDuration = await bunny.portalSessionCreate(
83
143
  "tenantCode",
84
144
  "https://example.com",
85
145
  12
86
146
  );
87
147
 
88
148
  // Track usage for billing
89
- const res = await bunny.featureUsageCreate(
90
- featureCode,
91
- quantity,
92
- subscriptionId,
93
- usageAt
149
+ const usageTracking = await bunny.featureUsageCreate(
150
+ "featureCode",
151
+ 1,
152
+ "subscriptionId",
153
+ new Date()
94
154
  );
95
155
 
96
- // Update account details including billing address for acount
97
- const res = await bunny.accountUpdateByTenantCode(
156
+ // Update account details including billing address for account
157
+ const updatedAccount = await bunny.accountUpdateByTenantCode("tenantCode", {
158
+ billingStreet: "123 Main Street",
159
+ billingCity: "Pleasantville",
160
+ billingState: "CA",
161
+ billingZip: "90210",
162
+ billingCountry: "US",
163
+ });
164
+ ```
165
+
166
+ **JavaScript:**
167
+
168
+ ```javascript
169
+ // Create a new subscription
170
+ const subscription = await bunny.subscriptionCreate("priceListCode", {
171
+ // Subscription settings
172
+ trial: false, // Optional: Start with a trial period
173
+ evergreen: true, // Optional: Auto-renew subscription
174
+
175
+ // Account identification (Must use either accountId OR account details)
176
+ accountId: "existing-123", // Required unless using accountName: Use existing account
177
+
178
+ // Account details (used if accountId is not provided)
179
+ accountName: "Acme Corp", // Required unless using accountId: Company/Account name
180
+ ownerUserId: "user-123",
181
+ phone: "555-0123",
182
+ fax: "555-0124",
183
+ website: "www.acme.com",
184
+
185
+ // Optional: Billing address
186
+ billingStreet: "123 Main St",
187
+ billingCity: "San Francisco",
188
+ billingZip: "94105",
189
+ billingState: "CA",
190
+ billingCountry: "US",
191
+
192
+ // Billing contact information
193
+ firstName: "John", // Required
194
+ lastName: "Doe",
195
+ email: "john@acme.com", // Required
196
+ mobile: "555-0125",
197
+ salutation: "Mr",
198
+ title: "CEO",
199
+
200
+ // Optional: Billing contact mailing address
201
+ mailingStreet: "456 Market St",
202
+ mailingCity: "San Francisco",
203
+ mailingZip: "94105",
204
+ mailingState: "CA",
205
+ mailingCountry: "US",
206
+
207
+ // Optional: Tenant information
208
+ tenantCode: "tenant-123",
209
+ tenantName: "Acme Team",
210
+ });
211
+
212
+ // Get a session token for the Bunny customer portal
213
+ const portalSession = await bunny.portalSessionCreate("tenantCode");
214
+
215
+ // Optionally supply a return url to get back to your app
216
+ const portalSessionWithUrl = await bunny.portalSessionCreate(
98
217
  "tenantCode",
99
- {
100
- billingStreet: "123 Main Street",
101
- billingCity: "Pleasantville",
102
- billingState: "CA",
103
- billingZip: "90210",
104
- billingCountry: "US"
105
- }
106
- ).then(console.log).catch(console.error)
218
+ "https://example.com"
219
+ );
220
+
221
+ // Default session length is 24 hours but you can change it. e.g 12 hours
222
+ const portalSessionWithDuration = await bunny.portalSessionCreate(
223
+ "tenantCode",
224
+ "https://example.com",
225
+ 12
226
+ );
227
+
228
+ // Track usage for billing
229
+ const usageTracking = await bunny.featureUsageCreate(
230
+ "featureCode",
231
+ 1,
232
+ "subscriptionId",
233
+ new Date()
234
+ );
107
235
 
236
+ // Update account details including billing address for account
237
+ const updatedAccount = await bunny.accountUpdateByTenantCode("tenantCode", {
238
+ billingStreet: "123 Main Street",
239
+ billingCity: "Pleasantville",
240
+ billingState: "CA",
241
+ billingZip: "90210",
242
+ billingCountry: "US",
243
+ });
108
244
  ```
109
245
 
110
246
  ## Error handling
111
247
 
112
248
  If there is an error with a helper method an exception will be raised.
113
249
 
114
- ```js
250
+ **TypeScript:**
251
+
252
+ ```typescript
253
+ import { Subscription } from "@bunnyapp/api-client";
254
+
115
255
  try {
116
- const res = await bunny.subscriptionCreate(...);
117
- // Do something
256
+ const subscription: Subscription = await bunny.subscriptionCreate(
257
+ "priceListCode",
258
+ {
259
+ trial: true,
260
+ evergreen: true,
261
+ accountName: "Test Account",
262
+ }
263
+ );
264
+ // Handle successful subscription
118
265
  } catch (error) {
119
- console.log(error);
266
+ if (error instanceof Error) {
267
+ console.error(error.message);
268
+ }
120
269
  }
121
270
 
122
- or
271
+ // Or using promises
272
+ bunny
273
+ .subscriptionCreate("priceListCode", {
274
+ trial: true,
275
+ evergreen: true,
276
+ accountName: "Test Account",
277
+ })
278
+ .then((subscription: Subscription) => {
279
+ // Handle successful subscription
280
+ })
281
+ .catch((error: Error) => {
282
+ console.error(error.message);
283
+ });
284
+ ```
123
285
 
124
- bunny.subscriptionCreate(...).then((res) => {
125
- // Do something
126
- }).catch((error) => {
127
- console.log(error);
128
- })
286
+ **JavaScript:**
287
+
288
+ ```javascript
289
+ try {
290
+ const subscription = await bunny.subscriptionCreate("priceListCode", {
291
+ trial: true,
292
+ evergreen: true,
293
+ accountName: "Test Account",
294
+ });
295
+ // Handle successful subscription
296
+ } catch (error) {
297
+ console.error(error.message);
298
+ }
299
+
300
+ // Or using promises
301
+ bunny
302
+ .subscriptionCreate("priceListCode", {
303
+ trial: true,
304
+ evergreen: true,
305
+ accountName: "Test Account",
306
+ })
307
+ .then((subscription) => {
308
+ // Handle successful subscription
309
+ })
310
+ .catch((error) => {
311
+ console.error(error.message);
312
+ });
129
313
  ```
130
314
 
131
- ## Perform a query
315
+ ## Direct GraphQL queries
316
+
317
+ If the helper methods don't cover your needs, you can make direct GraphQL queries. The SDK provides full TypeScript support for the GraphQL schema.
318
+
319
+ **TypeScript:**
320
+
321
+ ```typescript
322
+ import { Tenants } from "@bunnyapp/api-client";
323
+
324
+ // Example using generated GraphQL types
325
+ const query = `query tenants ($filter: String, $limit: Int) {
326
+ tenants (filter: $filter, limit: $limit) {
327
+ platform {
328
+ id
329
+ name
330
+ code
331
+ }
332
+ id
333
+ name
334
+ code
335
+ }
336
+ }`;
337
+
338
+ const variables = {
339
+ filter: "",
340
+ limit: 10,
341
+ };
132
342
 
133
- If the convenience methods on this SDK are not enough and you need more control over queries or mutations then you can make an async request against the Bunny GraphQL API.
343
+ // Use the generated Tenants type instead of custom TenantsResponse interface
344
+ const response = await bunny.query<{ data: { tenants: Tenants[] } }>(
345
+ query,
346
+ variables
347
+ );
348
+ ```
134
349
 
135
- The response will contain the raw graphql json.
350
+ **JavaScript:**
136
351
 
137
- ```js
138
- let query = `query tenants ($filter: String, $limit: Int) {
352
+ ```javascript
353
+ const query = `query tenants ($filter: String, $limit: Int) {
139
354
  tenants (filter: $filter, limit: $limit) {
140
355
  platform {
141
356
  id
@@ -148,12 +363,12 @@ let query = `query tenants ($filter: String, $limit: Int) {
148
363
  }
149
364
  }`;
150
365
 
151
- let variables = {
366
+ const variables = {
152
367
  filter: "",
153
368
  limit: 10,
154
369
  };
155
370
 
156
- let res = await bunny.query(query, variables);
371
+ const response = await bunny.query(query, variables);
157
372
  ```
158
373
 
159
374
  ## Validate a webhook payload
@@ -162,12 +377,35 @@ When Bunny sends a webhook request it includes a `x-bunny-signature` header whic
162
377
 
163
378
  Bunny will provide a signing token which you will need to store in your application and use for validating the webhook.
164
379
 
165
- ```js
166
- let signature = req.headers["x-bunny-signature"];
167
- let payload = req.body;
168
- let signingToken = "<secret signing token>";
380
+ **TypeScript:**
381
+
382
+ ```typescript
383
+ interface WebhookRequest {
384
+ headers: {
385
+ "x-bunny-signature": string;
386
+ };
387
+ body: unknown;
388
+ }
389
+
390
+ const signature: string = req.headers["x-bunny-signature"];
391
+ const payload: unknown = req.body;
392
+ const signingToken: string = "<secret signing token>";
393
+
394
+ const valid: boolean = bunny.webhooks.validate(
395
+ signature,
396
+ payload,
397
+ signingToken
398
+ );
399
+ ```
400
+
401
+ **JavaScript:**
402
+
403
+ ```javascript
404
+ const signature = req.headers["x-bunny-signature"];
405
+ const payload = req.body;
406
+ const signingToken = "<secret signing token>";
169
407
 
170
- let valid = bunny.webhooks.validate(signature, payload, signingToken);
408
+ const valid = bunny.webhooks.validate(signature, payload, signingToken);
171
409
  ```
172
410
 
173
411
  ## Test