@bunnyapp/api-client 1.0.16 → 2.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.
package/README.md CHANGED
@@ -39,38 +39,88 @@ const bunny = new BunnyClient({
39
39
  });
40
40
  ```
41
41
 
42
- ## Convenience methods
42
+ ## Helper methods
43
43
 
44
- This SDK wrappers several of the common Bunny API requests.
44
+ This SDK wrappers several of the common Bunny API requests. The response varies based on the method that is used.
45
45
 
46
46
  ```js
47
47
  // Create a new subscription
48
- bunny.createSubscription("priceListCode", {
48
+ const res = await bunny.subscriptionCreate("priceListCode", {
49
49
  trial: true,
50
50
  accountName: "accountName",
51
51
  firstName: "firstName",
52
52
  lastName: "lastName",
53
- email: "email",
53
+ email: "email@example.com",
54
54
  tenantCode: "remoteId",
55
55
  });
56
56
 
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
+ }
70
+
71
+
57
72
  // Get a session token for the Bunny customer portal
58
- bunny.createPortalSession("tenantCode");
73
+ const res = await bunny.portalSessionCreate("tenantCode");
59
74
 
60
75
  // Optionally supply a return url to get back to your app
61
- bunny.createPortalSession("tenantCode", "https://example.com");
76
+ const res = await bunny.portalSessionCreate(
77
+ "tenantCode",
78
+ "https://example.com"
79
+ );
62
80
 
63
81
  // Default session length is 24 hours but you can change it. e.g 12 hours
64
- bunny.createPortalSession("tenantCode", "https://example.com", 12);
82
+ const res = await bunny.portalSessionCreate(
83
+ "tenantCode",
84
+ "https://example.com",
85
+ 12
86
+ );
65
87
 
66
88
  // Track usage for billing
67
- bunny.trackUsage(featureCode, quantity, tenantCode, usageAt);
89
+ const res = await bunny.tenantUsageCreate(
90
+ featureCode,
91
+ quantity,
92
+ tenantCode,
93
+ usageAt
94
+ );
95
+ ```
96
+
97
+ ## Error handling
98
+
99
+ If there is an error with a helper method an exception will be raised.
100
+
101
+ ```js
102
+ try {
103
+ const res = await bunny.subscriptionCreate(...);
104
+ // Do something
105
+ } catch (error) {
106
+ console.log(error);
107
+ }
108
+
109
+ or
110
+
111
+ bunny.subscriptionCreate(...).then((res) => {
112
+ // Do something
113
+ }).catch((error) => {
114
+ console.log(error);
115
+ })
68
116
  ```
69
117
 
70
118
  ## Perform a query
71
119
 
72
120
  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.
73
121
 
122
+ The response will contain the raw graphql json.
123
+
74
124
  ```js
75
125
  let query = `query tenants ($filter: String, $limit: Int) {
76
126
  tenants (filter: $filter, limit: $limit) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bunnyapp/api-client",
3
- "version": "1.0.16",
3
+ "version": "2.0.0",
4
4
  "description": "Node.js client for Bunny CRM",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -19,6 +19,14 @@ const query = `mutation featureUsageCreate ($attributes: FeatureUsageAttributes!
19
19
  }
20
20
  }`;
21
21
 
22
+ /**
23
+ * Record feature usage for a tenant
24
+ * @param {string} featureCode Code for the feature that is being used
25
+ * @param {number} quantity Amount of usage to record
26
+ * @param {string} tenantCode Code for the tenant that has the usage
27
+ * @param {string} usageAt ISO8601 date string. Deafults to now
28
+ * @returns
29
+ */
22
30
  module.exports = async function (
23
31
  featureCode,
24
32
  quantity,
@@ -37,5 +45,11 @@ module.exports = async function (
37
45
  variables["attributes"]["usageAt"] = usageAt;
38
46
  }
39
47
 
40
- return this.query(query, variables);
48
+ const res = await this.query(query, variables);
49
+
50
+ if (res?.errors) {
51
+ throw new Error(res.errors.map((e) => e.message).join());
52
+ }
53
+
54
+ return res?.data?.featureUsageCreate?.featureUsage;
41
55
  };
@@ -0,0 +1,33 @@
1
+ const query = `mutation portalSessionCreate ($tenantCode: String!, $expiry: Int!, $returnUrl: String!) {
2
+ portalSessionCreate (tenantCode: $tenantCode, expiry: $expiry, returnUrl: $returnUrl) {
3
+ errors
4
+ token
5
+ }
6
+ }`;
7
+
8
+ /**
9
+ * Generate a session token for customer portal
10
+ * @param {string} tenantCode The unique code that represents the tenant
11
+ * @param {string} returnUrl A URL to redirect the user back to the original app after using the portal
12
+ * @param {number} expiryInHours Length of time in hours before the token expires. Deafults to 24
13
+ * @returns {string} The session token
14
+ */
15
+ module.exports = async function (
16
+ tenantCode,
17
+ returnUrl = null,
18
+ expiryInHours = 24
19
+ ) {
20
+ let variables = {
21
+ tenantCode: tenantCode,
22
+ returnUrl: returnUrl,
23
+ expiry: expiryInHours,
24
+ };
25
+
26
+ const res = await this.query(query, variables);
27
+
28
+ if (res?.errors) {
29
+ throw new Error(res.errors.map((e) => e.message).join());
30
+ }
31
+
32
+ return res?.data?.portalSessionCreate?.token;
33
+ };
@@ -0,0 +1,24 @@
1
+ const query = `mutation subscriptionCancel ($ids: [ID!]!) {
2
+ subscriptionCancel (ids: $ids) {
3
+ errors
4
+ }
5
+ }`;
6
+
7
+ /**
8
+ * Cancel a subsription
9
+ * @param {number} subscriptionId
10
+ * @returns {boolean} Success
11
+ */
12
+ module.exports = async function (subscriptionId) {
13
+ let variables = {
14
+ ids: [subscriptionId],
15
+ };
16
+
17
+ const res = await this.query(query, variables);
18
+
19
+ if (res?.errors) {
20
+ throw new Error(res.errors.map((e) => e.message).join());
21
+ }
22
+
23
+ return true;
24
+ };
@@ -34,12 +34,18 @@ const query = `mutation subscriptionCreate ($attributes: SubscriptionAttributes!
34
34
  }
35
35
  }`;
36
36
 
37
+ /**
38
+ * Create a new subscription for a tenant
39
+ * @param {string} priceListCode The code for the plan to subscribe to
40
+ * @param {Object} options
41
+ * @returns {Object} The subscription object
42
+ */
37
43
  module.exports = async function (priceListCode, options = {}) {
38
44
  let variables = {
39
45
  attributes: {
40
46
  priceListCode: priceListCode,
41
47
  trial: options["trial"] || false,
42
- evergreen: options["evergreen"] || false,
48
+ evergreen: options["evergreen"] || true,
43
49
  },
44
50
  };
45
51
 
@@ -63,5 +69,13 @@ module.exports = async function (priceListCode, options = {}) {
63
69
  };
64
70
  }
65
71
 
66
- return this.query(query, variables);
72
+ const res = await this.query(query, variables);
73
+
74
+ console.log(res);
75
+
76
+ if (res?.errors) {
77
+ throw new Error(res.errors.map((e) => e.message).join());
78
+ }
79
+
80
+ return res?.data?.subscriptionCreate?.subscription;
67
81
  };
@@ -0,0 +1,51 @@
1
+ const query = `mutation tenantCreate ($attributes: TenantAttributes!, $subscriptionId: ID!) {
2
+ tenantCreate (attributes: $attributes, subscriptionId: $subscriptionId) {
3
+ tenant {
4
+ code
5
+ id
6
+ name
7
+ platform {
8
+ id
9
+ name
10
+ code
11
+ }
12
+ }
13
+ errors
14
+ }
15
+ }`;
16
+
17
+ /**
18
+ *
19
+ * @param {string} name Account name of the tenant
20
+ * @param {string} code Unique code for the tenant
21
+ * @param {string} platformCode Code for the platform that the tenant is on
22
+ * @param {number} accountId Id of the account that the tenant is being created for
23
+ * @param {number} subscriptionId Id of the subscription that the tenant is being created for
24
+ * @returns
25
+ */
26
+ module.exports = async function (
27
+ name,
28
+ code,
29
+ platformCode,
30
+ accountId,
31
+ subscriptionId
32
+ ) {
33
+ let variables = {
34
+ attributes: {
35
+ name: name,
36
+ code: code,
37
+ accountId: accountId,
38
+ platformCode: platformCode,
39
+ },
40
+ subscriptionId: subscriptionId,
41
+ };
42
+
43
+ const res = await this.query(query, variables);
44
+
45
+ console.log(res);
46
+ if (res?.errors) {
47
+ throw new Error(res.errors.map((e) => e.message).join());
48
+ }
49
+
50
+ return res?.data?.tenantCreate?.tenant;
51
+ };
@@ -10,6 +10,7 @@ const query = `mutation tenantUpdate ($id: ID!, $attributes: TenantAttributes!)
10
10
  code
11
11
  }
12
12
  }
13
+ errors
13
14
  }
14
15
  }`;
15
16
 
@@ -21,5 +22,12 @@ module.exports = async function (id, code, name) {
21
22
  name: name,
22
23
  },
23
24
  };
24
- return this.query(query, variables);
25
+
26
+ const res = await this.query(query, variables);
27
+
28
+ if (res?.errors) {
29
+ throw new Error(res.errors.map((e) => e.message).join());
30
+ }
31
+
32
+ return res?.data?.tenantUpdate?.tenant;
25
33
  };
package/src/index.js CHANGED
@@ -84,10 +84,11 @@ class Bunny {
84
84
  }
85
85
  }
86
86
 
87
- Bunny.prototype.createSubscription = require("./helpers/create-subscription.js");
88
- Bunny.prototype.createTenant = require("./helpers/create-tenant.js");
89
- Bunny.prototype.updateTenant = require("./helpers/update-tenant.js");
90
- Bunny.prototype.trackUsage = require("./helpers/track-usage.js");
91
- Bunny.prototype.createPortalSession = require("./helpers/create-portal-session.js");
87
+ Bunny.prototype.subscriptionCreate = require("./helpers/subscription-create.js");
88
+ Bunny.prototype.subscriptionCancel = require("./helpers/subscription-cancel.js");
89
+ Bunny.prototype.tenantCreate = require("./helpers/tenant-create.js");
90
+ Bunny.prototype.tenantUpdate = require("./helpers/tenant-update.js");
91
+ Bunny.prototype.featureUsageCreate = require("./helpers/feature-usage-create.js");
92
+ Bunny.prototype.portalSessionCreate = require("./helpers/portal-session-create.js");
92
93
 
93
94
  module.exports = Bunny;
@@ -1,19 +0,0 @@
1
- const query = `mutation portalSessionCreate ($tenantCode: String!, $expiry: Int!, $returnUrl: String!) {
2
- portalSessionCreate (tenantCode: $tenantCode, expiry: $expiry, returnUrl: $returnUrl) {
3
- errors
4
- token
5
- }
6
- }`;
7
-
8
- module.exports = async function (
9
- tenantCode,
10
- returnUrl = null,
11
- expiryInHours = 24
12
- ) {
13
- let variables = {
14
- tenantCode: tenantCode,
15
- returnUrl: returnUrl,
16
- expiry: expiryInHours,
17
- };
18
- return this.query(query, variables);
19
- };
@@ -1,26 +0,0 @@
1
- const query = `mutation tenantCreate ($attributes: TenantAttributes!, $subscriptionId: ID!) {
2
- tenantCreate (attributes: $attributes, subscriptionId: $subscriptionId) {
3
- tenant {
4
- code
5
- id
6
- name
7
- platform {
8
- id
9
- name
10
- code
11
- }
12
- }
13
- }
14
- }`;
15
-
16
- module.exports = async function (name, code, platformCode, subscriptionId) {
17
- let variables = {
18
- attributes: {
19
- name: name,
20
- code: code,
21
- platformCode: platformCode,
22
- },
23
- subscriptionId: subscriptionId,
24
- };
25
- return this.query(query, variables);
26
- };