pocketsmith-ts
Version:
TypeScript client library for PocketSmith API
131 lines • 4.11 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.serializeError = serializeError;
exports.createPocketSmithClient = createPocketSmithClient;
const openapi_fetch_1 = __importDefault(require("openapi-fetch"));
/**
* Serializes error objects for better debugging
*/
function serializeError(error) {
if (error === null || error === undefined) {
return String(error);
}
if (typeof error === 'string') {
return error;
}
if (typeof error === 'object') {
try {
return JSON.stringify(error, null, 2);
}
catch {
// Fallback for non-serializable objects
return String(error);
}
}
return String(error);
}
/**
* Creates convenience methods for working with transactions
*/
function createTransactionMethods(client) {
return {
/**
* Get transactions for a specific account (high-level account grouping)
*
* @param accountId - The account ID
* @param options - Query parameters for filtering transactions
* @example
* ```typescript
* const { data: transactions } = await client.transactions.getByAccount(123, {
* start_date: '2024-01-01',
* end_date: '2024-12-31'
* });
* ```
*/
getByAccount: async (accountId, options) => {
return client.GET('/accounts/{id}/transactions', {
params: {
path: { id: accountId },
query: options || {}
}
});
},
/**
* Get transactions for a specific transaction account (individual account where transactions are posted)
*
* @param transactionAccountId - The transaction account ID
* @param options - Query parameters for filtering transactions
* @example
* ```typescript
* const { data: transactions } = await client.transactions.getByTransactionAccount(456, {
* start_date: '2024-01-01',
* end_date: '2024-12-31'
* });
* ```
*/
getByTransactionAccount: async (transactionAccountId, options) => {
return client.GET('/transaction_accounts/{id}/transactions', {
params: {
path: { id: transactionAccountId },
query: options || {}
}
});
}
};
}
/**
* Creates a PocketSmith API client with convenience methods
*
* @example
* ```typescript
* // Using API Key
* const client = createPocketSmithClient({
* apiKey: 'your-developer-key'
* });
*
* // Using OAuth2
* const client = createPocketSmithClient({
* accessToken: 'your-oauth-token'
* });
*
* // Fetch current user (standard openapi-fetch method)
* const { data: user, error } = await client.GET('/me');
* if (error) {
* console.error('API Error:', serializeError(error)); // Properly serialized error
* }
*
* // Use convenience methods
* const { data: transactions } = await client.transactions.getByAccount(123, {
* start_date: '2024-01-01',
* end_date: '2024-12-31'
* });
* ```
*/
function createPocketSmithClient(config = {}) {
const { baseUrl = 'https://api.pocketsmith.com/v2', apiKey, accessToken, } = config;
// Add authentication headers
const headers = {};
if (apiKey) {
headers['X-Developer-Key'] = apiKey;
}
else if (accessToken) {
headers['Authorization'] = `Bearer ${accessToken}`;
}
// Create client with headers
const client = (0, openapi_fetch_1.default)({
baseUrl,
headers,
});
// Add convenience methods
const clientWithConvenience = {
...client,
transactions: createTransactionMethods(client)
};
return clientWithConvenience;
}
// Default export
exports.default = createPocketSmithClient;
//# sourceMappingURL=index.js.map