@pliancy/divvy-sdk
Version:
This SDK provides a way to interact with the Divvy API
288 lines (281 loc) • 8.09 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
Divvy: () => Divvy
});
module.exports = __toCommonJS(src_exports);
// src/common/common.ts
var import_axios = __toESM(require("axios"), 1);
var DivvyBase = class {
client;
constructor(config) {
const baseURL = config.env === "prod" ? `https://gateway.prod.bill.com/connect/${config.apiVersion}` : `https://gateway.stage.bill.com/connect/${config.apiVersion}`;
this.client = import_axios.default.create({
baseURL,
headers: {
"Content-Type": "application/json",
apiToken: config.apiToken
}
});
}
async paginate(endpoint, params) {
let allResults = [];
let currentParams = { ...params };
while (true) {
const response = await this.client.get(endpoint, { params: currentParams });
const { results, nextPage } = response.data;
allResults = allResults.concat(results);
if (nextPage) {
currentParams = { ...currentParams, nextPage };
} else {
break;
}
}
return allResults;
}
/**
* Generic function to stringify filter objects
* @param filters - The filter object to stringify
* @returns A string representation of the filters
*/
stringifyFilters(filters) {
const parts = [];
for (const [key, value] of Object.entries(filters)) {
if (typeof value === "object" && value !== null) {
for (const [op, opValue] of Object.entries(value)) {
if (opValue !== void 0) {
parts.push(`${key}:${op}:${this.stringifyValue(opValue)}`);
}
}
}
}
return parts.join(",");
}
/**
* Converts a value to a string, handling arrays and other types
* @param value - The value to stringify
* @returns The stringified value
*/
stringifyValue(value) {
if (Array.isArray(value)) {
return `"${value.join(",")}"`;
}
if (typeof value === "boolean") {
return value.toString();
}
if (typeof value === "number") {
return value.toString();
}
return `"${value}"`;
}
};
// src/transactions/transactions.ts
var Transactions = class extends DivvyBase {
/**
* List All Transactions (This method automatically paginates through the results)
* @param params
* @returns
*/
async list(params) {
const apiParams = { ...params, filters: "" };
if (params?.filters) {
apiParams.filters = this.stringifyFilters(params.filters);
}
return this.paginate("/spend/transactions", apiParams);
}
/**
* Get a transaction by id
* @param transactionId
* @returns
*/
async get(transactionId) {
const { data } = await this.client.get(`/spend/transactions/${transactionId}`);
return data;
}
};
// src/custom-fields/custom-fields.ts
var CustomFields = class extends DivvyBase {
/**
* List All Custom Fields (This method automatically paginates through the results)
* @param params
* @returns
*/
async list(params) {
const apiParams = { ...params, filters: "" };
if (params?.filters) {
apiParams.filters = this.stringifyFilters(params.filters);
}
return this.paginate("/spend/custom-fields", apiParams);
}
/**
* Get a custom field by id
* @param customFieldId
* @returns
*/
async get(customFieldId) {
const { data } = await this.client.get(`/spend/custom-fields/${customFieldId}`);
return data;
}
/**
* Create a custom field
* @param data
* @returns
*/
async create(data) {
const { data: res } = await this.client.post("/spend/custom-fields", data);
return res;
}
/**
* Update a custom field
* @param customFieldId
* @param data
* @returns
*/
async update(customFieldId, data) {
const { data: res } = await this.client.patch(
`/spend/custom-fields/${customFieldId}`,
data
);
return res;
}
/**
* List values for a custom field (This method automatically paginates through the results)
* @param customFieldId
* @param params
* @returns
*/
async listValues(customFieldId, params) {
const apiParams = {
...params,
filters: ""
};
if (params?.filters) {
apiParams.filters = this.stringifyFilters(params.filters);
}
return this.paginate(
`/spend/custom-fields/${customFieldId}/values`,
apiParams
);
}
/**
* Get a value for a custom field
* @param customFieldId
* @param customFieldValueId
* @returns
*/
async getValue(customFieldId, customFieldValueId) {
const { data } = await this.client.get(
`/spend/custom-fields/${customFieldId}/values/${customFieldValueId}`
);
return data;
}
/**
* Create values for a custom field
* @param customFieldId
* @param values
*/
async createValues(customFieldId, values) {
await this.client.post(`/spend/custom-fields/${customFieldId}/values`, { values });
}
/**
* Delete values from a custom field
* @param customFieldId
* @param customFieldValueIds - Array of custom field value ids to delete
*/
async deleteValues(customFieldId, customFieldValueIds) {
await this.client.delete(`/spend/custom-fields/${customFieldId}/values`, {
data: { values: customFieldValueIds }
});
}
};
// src/users/users.ts
var Users = class extends DivvyBase {
/**
* List All Users (This method automatically paginates through the results)
* @param params
* @returns
*/
async list(params) {
const apiParams = { ...params, filters: "" };
if (params?.filters) {
apiParams.filters = this.stringifyFilters(params.filters);
}
return this.paginate("/spend/users", apiParams);
}
/**
* Create a new user
* @param data
* @returns
*/
async create(data) {
const { data: res } = await this.client.post("/spend/users", data);
return res;
}
/**
* Get current user details
* @returns
*/
async getCurrent() {
const { data } = await this.client.get("/spend/users/current");
return data;
}
/**
* Get user details by ID
* @param userId
* @returns
*/
async get(userId) {
const { data } = await this.client.get(`/spend/users/${userId}`);
return data;
}
/**
* Delete a user
* @param userId
*/
async delete(userId) {
await this.client.delete(`/spend/users/${userId}`);
}
};
// src/divvy.ts
var Divvy = class {
transactions;
customFields;
users;
constructor(config) {
this.transactions = new Transactions(config);
this.customFields = new CustomFields(config);
this.users = new Users(config);
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Divvy
});