UNPKG

neuwo-api

Version:

TypeScript/JavaScript SDK client for the Neuwo content classification API

108 lines 3.78 kB
/** * Utility functions and HTTP request handler for Neuwo API. * * Provides low-level HTTP communication, parameter encoding, * validation, and response parsing. */ import { NeuwoAPIError } from "./errors.js"; import { RequestOptions } from "./types.js"; /** * Validate URL format. * Throws ValidationError if URL is invalid. * * @param url - URL string to validate * @throws {ValidationError} If URL is invalid or empty */ export declare function validateUrl(url: string): void; /** * Parse JSON response from API. * Throws ContentNotAvailableError if response contains an error field. * * @param response - Fetch API Response object * @returns Parsed JSON data (caller should validate/cast to expected type) * @throws {ContentNotAvailableError} If response contains an error field * @throws {Error} If response is not valid JSON */ export declare function parseJsonResponse(response: Response): Promise<unknown>; /** * Validate and sanitise content string. * Throws ValidationError if content is empty or only whitespace. * * @param content - Content string to validate * @returns Original content if valid * @throws {ValidationError} If content is empty or only whitespace */ export declare function sanitiseContent(content: string): string; /** * Sleep for specified milliseconds. * * @param ms - Number of milliseconds to sleep * @returns Promise that resolves after the specified time */ export declare function sleep(ms: number): Promise<void>; /** * Format a Date to YYYY-MM-DD string. * * @param date - Date object to format * @returns Formatted date string in YYYY-MM-DD format */ export declare function formatDate(date: Date): string; /** * Request handler for making HTTP requests to Neuwo API. */ export declare class RequestHandler { private readonly token; private readonly baseUrl; private readonly timeout; /** * @param token - API authentication token * @param baseUrl - Base URL for API requests * @param timeout - Request timeout in seconds */ constructor(token: string, baseUrl: string, timeout: number); /** * Build full URL with query parameters including token. * * @param endpoint - API endpoint path * @param params - Optional query parameters to append * @returns Complete URL string with token and parameters */ private buildUrl; /** * Encode a value for form data. * * @param value - Value to encode (string, number, boolean, Date, etc.) * @returns String representation of the value */ private encodeValue; /** * Encode data as application/x-www-form-urlencoded. * * Handles arrays by repeating the parameter name for each value. * For example: {tags: ['a', 'b']} becomes 'tags=a&tags=b' * * @param data - Data object to encode * @returns URL-encoded string */ private encodeFormData; /** * Handle API error responses by parsing and creating appropriate exceptions. * * Parses the error response, extracts relevant error information, and maps * HTTP status codes to specific exception types for better error handling. * * @param response - HTTP response object with status code >= 400 * @returns Appropriate exception instance based on the status code and error content */ static handleAPIError(response: Response): Promise<NeuwoAPIError>; /** * Make an HTTP request to the API. * * @param options - Request configuration options * @returns Fetch API Response object * @throws {NetworkError} On network failure or timeout * @throws {NeuwoAPIError} On API error responses */ request(options: RequestOptions): Promise<Response>; } //# sourceMappingURL=utils.d.ts.map