Skip to content
This repository has been archived by the owner on Jan 10, 2021. It is now read-only.

Add a type for TranscriptEvent #3

Merged
merged 1 commit into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/StreamingClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/StreamingClient.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/examples/stream-from-microphone.ts
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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) {
Expand Down
26 changes: 26 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,29 @@ export interface AwsEventMessage {
}
body: Buffer
}

interface TranscribeItem {
Content: string,
EndTime: number,
StartTime: number,
Type: 'pronunciation' | 'punctuation'
};

interface TranscribeAlternative {
Items: Array<TranscribeItem>,
Transcript: string
};

interface TranscribeResult {
Alternatives: Array<TranscribeAlternative>,
EndTime: number,
IsPartial: Boolean,
ResultId: string,
StartTime: number
};

export interface TranscriptEvent {
Transcript: {
Results: Array<TranscribeResult>
}
}