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

Commit

Permalink
Add a type for TranscriptEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
orgads committed May 24, 2020
1 parent e2f23e2 commit 739971d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
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>
}
}

0 comments on commit 739971d

Please sign in to comment.