From 4936d4a09d0516bfcdd9d7bfa6a15daa1f53a0a1 Mon Sep 17 00:00:00 2001 From: Giora Guttsait Date: Thu, 14 Mar 2024 19:40:28 +0000 Subject: [PATCH 1/2] add type definitions --- package.json | 1 + types.d.ts | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 types.d.ts diff --git a/package.json b/package.json index 969cd2b..a5edb85 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "1.9.4", "description": "WebVTT parser, compiler, and segmenter with HLS support", "main": "index.js", + "types": "types.d.ts", "scripts": { "eslint": "eslint *.js **/*.js", "test": "npm run eslint -s && nyc mocha -R dot" diff --git a/types.d.ts b/types.d.ts new file mode 100644 index 0000000..b855913 --- /dev/null +++ b/types.d.ts @@ -0,0 +1,101 @@ +export type ParseOptions = { + /** + * if true, will parse the WebVTT metadata. + * + * if false, will throw if the WebVTT metadata exists. + */ + meta?: boolean; + strict?: boolean; +}; + +export type Cue = { + identifier: string; + start: number; + end: number; + text: string; + styles: string; +}; + +export type Meta = Record; + +export type ParseOutput = { + valid: boolean; + strict: T["strict"] extends boolean ? T["strict"] : boolean; + cues: Cue[]; + errors: unknown[]; +} & (T extends { meta: true } ? Meta : {}); + +export declare class ParserError extends Error { + readonly message: string; + constructor(message: string, error?: unknown); +} + +/** + * @throws {ParserError} + */ +export function parse( + input: string, + options?: O +): ParseOutput; + +export type CompileInput = { + valid: boolean; + meta?: Meta; + cues: Cue[]; +}; + +export declare class CompilerError extends Error { + readonly error: unknown; + constructor(message: string, error?: unknown); +} + +export function compile(input: CompileInput): string; + +type Segment = { + cues: Cue[]; + duration: number; +}; + +/** + * @throws {ParserError} + */ +export function segment( + input: string, + segmentLength?: number | undefined +): Segment[]; + +type HlsSegment = { + filename: string; + content: string; +}; + +/** + * @throws {ParserError} + */ +declare function hlsSegment( + input: string, + segmentLength?: number | undefined, + startOffset?: string | undefined +): HlsSegment[]; + +/** + * @throws {ParserError} + */ +declare function hlsSegmentPlaylist( + input: string, + segmentLength?: number | undefined +): string; + +export const hls: { + hlsSegment: typeof hlsSegment; + hlsSegmentPlaylist: typeof hlsSegmentPlaylist; +}; + +declare const _default: { + parse: typeof parse; + compile: typeof compile; + segment: typeof segment; + hls: typeof hls; +}; + +export default _default; From f3850004aba382cf7759a847be814e991eba5682 Mon Sep 17 00:00:00 2001 From: Giora Guttsait Date: Thu, 14 Mar 2024 19:47:07 +0000 Subject: [PATCH 2/2] un-declare errors as they're not actually exported --- types.d.ts | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/types.d.ts b/types.d.ts index b855913..a2879a9 100644 --- a/types.d.ts +++ b/types.d.ts @@ -25,14 +25,6 @@ export type ParseOutput = { errors: unknown[]; } & (T extends { meta: true } ? Meta : {}); -export declare class ParserError extends Error { - readonly message: string; - constructor(message: string, error?: unknown); -} - -/** - * @throws {ParserError} - */ export function parse( input: string, options?: O @@ -44,11 +36,6 @@ export type CompileInput = { cues: Cue[]; }; -export declare class CompilerError extends Error { - readonly error: unknown; - constructor(message: string, error?: unknown); -} - export function compile(input: CompileInput): string; type Segment = { @@ -56,9 +43,6 @@ type Segment = { duration: number; }; -/** - * @throws {ParserError} - */ export function segment( input: string, segmentLength?: number | undefined @@ -69,18 +53,12 @@ type HlsSegment = { content: string; }; -/** - * @throws {ParserError} - */ declare function hlsSegment( input: string, segmentLength?: number | undefined, startOffset?: string | undefined ): HlsSegment[]; -/** - * @throws {ParserError} - */ declare function hlsSegmentPlaylist( input: string, segmentLength?: number | undefined