@bunnyapp/api-client
Version:
Node.js client for Bunny CRM
127 lines • 6.03 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const assert = require("assert");
const axios_1 = __importDefault(require("axios"));
const webhooks_1 = __importDefault(require("./webhooks"));
// Import your generated GraphQL types
const subscription_create_1 = __importDefault(require("./helpers/subscription-create"));
const subscription_cancel_1 = __importDefault(require("./helpers/subscription-cancel"));
const tenant_by_code_1 = __importDefault(require("./helpers/tenant-by-code"));
const tenant_create_1 = __importDefault(require("./helpers/tenant-create"));
const tenant_update_1 = __importDefault(require("./helpers/tenant-update"));
const feature_usage_create_1 = __importDefault(require("./helpers/feature-usage-create"));
const portal_session_create_1 = __importDefault(require("./helpers/portal-session-create"));
const account_update_by_tenant_code_1 = __importDefault(require("./helpers/account-update-by-tenant-code"));
const tenant_metrics_update_1 = __importDefault(require("./helpers/tenant-metrics-update"));
// Export all types for consumers
__exportStar(require("./types"), exports);
class Bunny {
constructor(options = {}) {
if (!(this instanceof Bunny))
return new Bunny(options);
assert(options.baseUrl, "Bunny base url required");
this.options = options;
this.retryEnabled = false;
if (!options.accessToken) {
assert(options.clientId, "Bunny API clientId required");
assert(options.clientSecret, "Bunny API clientSecret required");
assert(options.scope, "Bunny API scope required");
this.retryEnabled = true;
}
this.client = axios_1.default.create({
headers: {
"User-Agent": "Bunny-node",
},
baseURL: options.baseUrl,
});
this.client.interceptors.response.use(null, async (error) => {
var _a, _b, _c;
if (this.retryEnabled &&
error.config &&
error.response &&
error.response.status === 401 &&
!error.config.retry &&
error.config.url !== "/oauth/token") {
const accessToken = await this.fetchAccessToken();
error.config.retry = true;
error.config.headers["Authorization"] = `bearer ${accessToken}`;
return axios_1.default.request(error.config);
}
if ((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.error_description) {
return Promise.reject(error.response.data.error_description);
}
return Promise.reject((_c = error.response) === null || _c === void 0 ? void 0 : _c.data);
});
this.webhooks = new webhooks_1.default(options.webhookSigningToken);
}
async fetchAccessToken() {
var _a;
const params = new URLSearchParams({
grant_type: "client_credentials",
client_id: this.options.clientId,
client_secret: this.options.clientSecret,
scope: this.options.scope,
});
const res = await this.client.post("/oauth/token", params, {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
});
return (_a = res === null || res === void 0 ? void 0 : res.data) === null || _a === void 0 ? void 0 : _a.access_token;
}
/**
* Execute a GraphQL query against the Bunny API
* @template TData The expected shape of the response data
* @template TVariables The shape of the variables object
* @param {string} query The GraphQL query string
* @param {TVariables} [variables] Optional variables for the query
* @returns {Promise<{ data?: TData; errors?: Array<{ message: string }> }>}
*/
async query(query, variables) {
const body = {
query,
variables,
};
if (!this.options.accessToken) {
this.options.accessToken = await this.fetchAccessToken();
}
const res = await this.client.post("/graphql", body, {
headers: {
Authorization: `bearer ${this.options.accessToken}`,
},
});
return res.data;
}
}
// Attach helper methods (you'll need to type these too, see below)
Bunny.prototype.subscriptionCreate = subscription_create_1.default;
Bunny.prototype.subscriptionCancel = subscription_cancel_1.default;
Bunny.prototype.tenantByCode = tenant_by_code_1.default;
Bunny.prototype.tenantCreate = tenant_create_1.default;
Bunny.prototype.tenantUpdate = tenant_update_1.default;
Bunny.prototype.featureUsageCreate = feature_usage_create_1.default;
Bunny.prototype.portalSessionCreate = portal_session_create_1.default;
Bunny.prototype.accountUpdateByTenantCode = account_update_by_tenant_code_1.default;
Bunny.prototype.tenantMetricsUpdate = tenant_metrics_update_1.default;
// Support both default export and CommonJS module.exports
exports.default = Bunny;
module.exports = Bunny;
module.exports.default = Bunny;
//# sourceMappingURL=index.js.map