UNPKG

@pliancy/divvy-sdk

Version:

This SDK provides a way to interact with the Divvy API

251 lines (246 loc) 6.44 kB
// src/common/common.ts import axios from "axios"; 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 = axios.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); } }; export { Divvy };