diff --git a/.eslintignore b/.eslintignore index 44edcd6..1adec14 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,3 +1,4 @@ fixtures coverage playground +*.d.ts diff --git a/.gitignore b/.gitignore index ccb6dcf..c0263b1 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ node_modules coverage playground *.tgz +package-lock.json diff --git a/fetch.d.ts b/fetch.d.ts new file mode 100644 index 0000000..f0dd38a --- /dev/null +++ b/fetch.d.ts @@ -0,0 +1,40 @@ +// Type definitions for fetch.js + +/** + * Options for fetchRecorder + */ +export interface FetchRecorderOptions { + /** + * Base fetch implementation to use. Defaults to globalThis.fetch + */ + fetch?: typeof fetch +} + +/** + * Creates a fetch implementation that records all requests to a log array + * @param log - Array to append request/response logs to + * @param options - Optional configuration + * @returns A fetch function that records requests + */ +export function fetchRecorder( + log: any[], + options?: FetchRecorderOptions +): typeof fetch + +/** + * Creates a fetch implementation that replays requests from a log array + * @param log - Array of recorded request/response logs + * @returns A fetch function that replays recorded requests + */ +export function fetchReplayer(log: any[]): typeof fetch + +/** + * Creates a custom fetch implementation using a lookup function + * @param lookup - Function to lookup request/response data + * @param fallback - Optional fallback fetch function + * @returns A fetch function + */ +export function fetchApi( + lookup: (resource: RequestInfo | URL, options?: RequestInit) => Promise, + fallback?: typeof fetch +): typeof fetch diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..10ed925 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,16 @@ +// Re-export types and functions from individual modules +export type { FetchRecorderOptions } from './fetch.js' + +export { fetchRecorder, fetchReplayer, fetchApi } from './fetch.js' + +export type { + WebSocketRecorderOptions, + WebSocketReplayerOptions, +} from './websocket.js' + +export { WebSocketRecorder, WebSocketReplayer } from './websocket.js' + +export type { PrettyJSONOptions } from './util.js' + +export { prettyJSON } from './util.js' + diff --git a/package.json b/package.json index 78677d2..ec30794 100644 --- a/package.json +++ b/package.json @@ -21,16 +21,30 @@ "license": "MIT", "type": "module", "main": "./index.js", + "types": "./index.d.ts", "exports": { - ".": "./index.js", - "./fetch": "./fetch.js", - "./websocket": "./websocket.js" + ".": { + "types": "./index.d.ts", + "default": "./index.js" + }, + "./fetch": { + "types": "./fetch.d.ts", + "default": "./fetch.js" + }, + "./websocket": { + "types": "./websocket.d.ts", + "default": "./websocket.js" + } }, "files": [ "fetch.js", + "fetch.d.ts", "index.js", + "index.d.ts", "util.js", + "util.d.ts", "websocket.js", + "websocket.d.ts", "CHANGELOG.md" ], "scripts": { diff --git a/util.d.ts b/util.d.ts new file mode 100644 index 0000000..416f21e --- /dev/null +++ b/util.d.ts @@ -0,0 +1,20 @@ +// Type definitions for util.js + +/** + * Options for prettyJSON formatting + */ +export interface PrettyJSONOptions { + /** + * Maximum width for inline formatting. Default: 120 + */ + width?: number +} + +/** + * Pretty-prints JSON data for human-readable logs + * @param data - Data to format + * @param options - Formatting options + * @returns Pretty-printed JSON string + */ +export function prettyJSON(data: any, options?: PrettyJSONOptions): string + diff --git a/websocket.d.ts b/websocket.d.ts new file mode 100644 index 0000000..4bcfb34 --- /dev/null +++ b/websocket.d.ts @@ -0,0 +1,46 @@ +// Type definitions for websocket.js + +/** + * Options for WebSocketRecorder + */ +export interface WebSocketRecorderOptions { + /** + * Base WebSocket implementation to use. Defaults to globalThis.WebSocket + */ + WebSocket?: typeof WebSocket +} + +/** + * Creates a WebSocket class that records all sessions to a log array + * @param log - Array to append session logs to + * @param options - Optional configuration + * @returns A WebSocket class that records sessions + */ +export function WebSocketRecorder( + log: any[], + options?: WebSocketRecorderOptions +): typeof WebSocket + +/** + * Options for WebSocketReplayer + */ +export interface WebSocketReplayerOptions { + /** + * Delay interval for replaying events + * - 0: immediate (default) + * - Infinity: match original timing + * - number: maximum delay between events + */ + interval?: number +} + +/** + * Creates a WebSocket class that replays sessions from a log array + * @param log - Array of recorded session logs + * @param options - Optional configuration + * @returns A WebSocket class that replays recorded sessions + */ +export function WebSocketReplayer( + log: any[], + options?: WebSocketReplayerOptions +): typeof WebSocket