watchtower-node-sdk
Version:
A TypeScript Node.js SDK for the Watchtower API, providing API key management, connection string generation, and more
81 lines (74 loc) • 3.06 kB
text/typescript
import { BaseEndpoint } from '../base';
import { PDFExportRequest, PDFExportResponse, PDFExportStatus } from './types';
import { InvalidRequestError, AuthenticationError, NotFoundError, ServerError } from '../../errors';
export class PDFEndpoint extends BaseEndpoint {
constructor(client: any) {
super(client, '/api/pdf');
}
private validateRequiredKeys(data: { organization_apikey?: string; app_apikey?: string }) {
if (!data.organization_apikey) {
throw new InvalidRequestError('organization_apikey is required');
}
if (!data.app_apikey) {
throw new InvalidRequestError('app_apikey is required');
}
}
/**
* Generate a PDF export for a specific item
* @param data - The PDF export request parameters
* @returns Promise with the PDF export response
* @throws {InvalidRequestError} If required fields are missing or invalid
* @throws {AuthenticationError} If API keys are invalid
* @throws {ServerError} If server encounters an error
*/
async generatePDF(data: PDFExportRequest): Promise<PDFExportResponse> {
this.validateRequiredKeys(data);
if (!data.item_id) {
throw new InvalidRequestError('item_id is required');
}
try {
return await this.post<PDFExportResponse>('/export', data);
} catch (error: any) {
if (error.response?.status === 400) {
throw new InvalidRequestError(error.response.data?.message || 'Invalid request');
}
if (error.response?.status === 401) {
throw new AuthenticationError('Invalid API keys');
}
if (error.response?.status === 500) {
throw new ServerError('Internal server error');
}
throw error;
}
}
/**
* Get the status of a PDF export
* @param exportId - The ID of the export to check
* @param data - The request parameters containing API keys
* @returns Promise with the export status
* @throws {InvalidRequestError} If required fields are missing
* @throws {AuthenticationError} If API keys are invalid
* @throws {NotFoundError} If export is not found
* @throws {ServerError} If server encounters an error
*/
async getExportStatus(exportId: string, data: { organization_apikey: string; app_apikey: string; tenant_apikey?: string }): Promise<PDFExportStatus> {
this.validateRequiredKeys(data);
try {
return await this.get<PDFExportStatus>(`/export/${exportId}/status`, { params: data });
} catch (error: any) {
if (error.response?.status === 400) {
throw new InvalidRequestError(error.response.data?.message || 'Invalid request');
}
if (error.response?.status === 401) {
throw new AuthenticationError('Invalid API keys');
}
if (error.response?.status === 404) {
throw new NotFoundError(`Export with ID ${exportId} not found`);
}
if (error.response?.status === 500) {
throw new ServerError('Internal server error');
}
throw error;
}
}
}