diff --git a/Readme.md b/Readme.md index 7fa555e..8662265 100644 --- a/Readme.md +++ b/Readme.md @@ -12,7 +12,7 @@ With YARN install the module with: `yarn add aws-transcribe` An example of streaming from microphone can be found in src/examples/stream-from-microphone.ts ```typescript -import { AwsTranscribe, StreamingClient } from "aws-transcribe" +import { AwsTranscribe, StreamingClient, TranscriptEvent } from "aws-transcribe" const client = new AwsTranscribe({ // if these aren't provided, they will be taken from the environment @@ -30,7 +30,7 @@ const transcribeStream = client .on(StreamingClient.EVENTS.OPEN, () => console.log(`transcribe connection opened`)) .on(StreamingClient.EVENTS.ERROR, console.error) .on(StreamingClient.EVENTS.CLOSE, () => console.log(`transcribe connection closed`)) - .on(StreamingClient.EVENTS.DATA, (data) => { + .on(StreamingClient.EVENTS.DATA, (data: TranscriptEvent) => { const results = data.Transcript.Results if (!results || results.length === 0) { diff --git a/src/StreamingClient.ts b/src/StreamingClient.ts index fc26b60..9b1e8c6 100644 --- a/src/StreamingClient.ts +++ b/src/StreamingClient.ts @@ -4,6 +4,7 @@ import { Writable } from "stream" import { createDebugger } from "./utils" import { TranscribeException } from "./TranscribeException" import { fromBinary, toBinary } from "./aws-message-utils" +import { TranscriptEvent } from "."; const debugLog = createDebugger(__filename) @@ -63,7 +64,7 @@ export class StreamingClient extends Writable { if (wrapper.headers[":message-type"].value === "event") { const eventType = wrapper.headers[":event-type"].value debugLog(`${eventType}: `, body) - this.emit(StreamingClient.EVENTS.DATA, body, eventType) + this.emit(StreamingClient.EVENTS.DATA, body as TranscriptEvent, eventType) } else { // message type is exception // exception type is supposed to be one from EXCEPTIONS diff --git a/src/__tests__/StreamingClient.test.ts b/src/__tests__/StreamingClient.test.ts index 838ee44..90baba7 100644 --- a/src/__tests__/StreamingClient.test.ts +++ b/src/__tests__/StreamingClient.test.ts @@ -1,7 +1,7 @@ jest.mock("ws") import WsServer from "jest-websocket-mock" -import { StreamingClient } from "../index" +import { StreamingClient, TranscriptEvent } from "../index" import { TranscribeException } from "../TranscribeException" import { getBinaryEvent, @@ -80,7 +80,7 @@ describe("StreamingClient", () => { const body = createBodyForTranscriptionEvent(`a random transcription`, false) const event = "TranscriptionEvent" const message = getBinaryEvent(event, body) - client.on(StreamingClient.EVENTS.DATA, (data, eventName) => { + client.on(StreamingClient.EVENTS.DATA, (data: TranscriptEvent, eventName: string) => { expect(data).toEqual(body) expect(eventName).toBe(event) done() diff --git a/src/examples/stream-from-microphone.ts b/src/examples/stream-from-microphone.ts index 6a3da87..a1705d4 100644 --- a/src/examples/stream-from-microphone.ts +++ b/src/examples/stream-from-microphone.ts @@ -1,4 +1,4 @@ -import { AwsTranscribe, StreamingClient } from "../index" +import { AwsTranscribe, StreamingClient, TranscriptEvent } from "../index" // eslint-disable-next-line @typescript-eslint/no-var-requires const recorder = require("node-record-lpcm16") @@ -41,7 +41,7 @@ const transcribeStream = client .on(StreamingClient.EVENTS.OPEN, () => console.log(`transcribe connection opened`)) .on(StreamingClient.EVENTS.ERROR, console.error) .on(StreamingClient.EVENTS.CLOSE, () => console.log(`transcribe connection closed`)) - .on(StreamingClient.EVENTS.DATA, (data) => { + .on(StreamingClient.EVENTS.DATA, (data: TranscriptEvent) => { const results = data.Transcript.Results if (!results || results.length === 0) { diff --git a/src/types.ts b/src/types.ts index 0911a82..660588d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -71,3 +71,29 @@ export interface AwsEventMessage { } body: Buffer } + +interface TranscribeItem { + Content: string, + EndTime: number, + StartTime: number, + Type: 'pronunciation' | 'punctuation' +}; + +interface TranscribeAlternative { + Items: Array, + Transcript: string +}; + +interface TranscribeResult { + Alternatives: Array, + EndTime: number, + IsPartial: Boolean, + ResultId: string, + StartTime: number +}; + +export interface TranscriptEvent { + Transcript: { + Results: Array + } +}