diff --git a/examples/node-prerecorded/index.js b/examples/node-prerecorded/index.js index d15f128c..0c2d784f 100644 --- a/examples/node-prerecorded/index.js +++ b/examples/node-prerecorded/index.js @@ -21,10 +21,7 @@ const readstream = async () => { const deepgram = createClient(process.env.DEEPGRAM_API_KEY); const { result, error } = await deepgram.listen.prerecorded.transcribeFile( - { - stream: fs.createReadStream("./examples/nasa.mp4"), - mimetype: "audio/mp4", - }, + fs.createReadStream("./examples/nasa.mp4"), { model: "nova", } @@ -38,10 +35,7 @@ const buffer = async () => { const deepgram = createClient(process.env.DEEPGRAM_API_KEY); const { result, error } = await deepgram.listen.prerecorded.transcribeFile( - { - buffer: fs.readFileSync("./examples/nasa.mp4"), - mimetype: "audio/mp4", - }, + fs.readFileSync("./examples/nasa.mp4"), { model: "nova", } @@ -51,6 +45,8 @@ const buffer = async () => { if (!error) console.log(result); }; -url(); -readstream(); -buffer(); +(async () => { + await url(); + await readstream(); + await buffer(); +})(); diff --git a/src/lib/constants.ts b/src/lib/constants.ts index 4cab1aa0..34e3b57e 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -4,7 +4,6 @@ import { version } from "./version"; export const DEFAULT_HEADERS = { "X-Client-Info": `@deepgram/sdk; ${isBrowser() ? "browser" : "server"}; v${version}`, "User-Agent": `@deepgram/sdk/${version}`, - "Content-Type": "application/json", }; export const DEFAULT_URL = "api.deepgram.com"; diff --git a/src/lib/helpers.ts b/src/lib/helpers.ts index 1c008027..18463284 100644 --- a/src/lib/helpers.ts +++ b/src/lib/helpers.ts @@ -1,11 +1,6 @@ import { Headers as CrossFetchHeaders } from "cross-fetch"; -import { - BufferSource, - DeepgramClientOptions, - PrerecordedSource, - ReadStreamSource, - UrlSource, -} from "./types"; +import { DeepgramClientOptions, FileSource, PrerecordedSource, UrlSource } from "./types"; +import { Readable } from "stream"; export function stripTrailingSlash(url: string): string { return url.replace(/\/$/, ""); @@ -59,18 +54,20 @@ export const isUrlSource = (providedSource: PrerecordedSource): providedSource i return false; }; -export const isBufferSource = ( - providedSource: PrerecordedSource -): providedSource is BufferSource => { - if ((providedSource as BufferSource).buffer) return true; +export const isFileSource = (providedSource: PrerecordedSource): providedSource is FileSource => { + if (isReadStreamSource(providedSource) || isBufferSource(providedSource)) return true; return false; }; -export const isReadStreamSource = ( - providedSource: PrerecordedSource -): providedSource is ReadStreamSource => { - if ((providedSource as ReadStreamSource).stream) return true; +const isBufferSource = (providedSource: PrerecordedSource): providedSource is Buffer => { + if (providedSource as Buffer) return true; + + return false; +}; + +const isReadStreamSource = (providedSource: PrerecordedSource): providedSource is Readable => { + if (providedSource as Readable) return true; return false; }; diff --git a/src/lib/types/PrerecordedSource.ts b/src/lib/types/PrerecordedSource.ts index 8eb3864d..e85d5c02 100644 --- a/src/lib/types/PrerecordedSource.ts +++ b/src/lib/types/PrerecordedSource.ts @@ -1,19 +1,9 @@ import { Readable } from "stream"; -export type PrerecordedSource = UrlSource | BufferSource | ReadStreamSource; +export type PrerecordedSource = UrlSource | Buffer | Readable; -export type FileSource = BufferSource | ReadStreamSource; - -export interface ReadStreamSource { - stream: Readable; - mimetype: string; -} +export type FileSource = Buffer | Readable; export interface UrlSource { url: string; } - -export interface BufferSource { - buffer: Buffer; - mimetype: string; -} diff --git a/src/lib/types/index.ts b/src/lib/types/index.ts index ae3e9873..104eff9d 100644 --- a/src/lib/types/index.ts +++ b/src/lib/types/index.ts @@ -32,13 +32,7 @@ export type { LiveConfigOptions } from "./LiveConfigOptions"; export type { LiveMetadataEvent } from "./LiveMetadataEvent"; export type { LiveTranscriptionEvent } from "./LiveTranscriptionEvent"; export type { MessageResponse } from "./MessageResponse"; -export type { - BufferSource, - FileSource, - PrerecordedSource, - ReadStreamSource, - UrlSource, -} from "./PrerecordedSource"; +export type { FileSource, PrerecordedSource, UrlSource } from "./PrerecordedSource"; export type { SendProjectInviteOptions } from "./SendProjectInviteOptions"; export type { SyncPrerecordedResponse } from "./SyncPrerecordedResponse"; export type { TranscriptionOptions, PrerecordedOptions, LiveOptions } from "./TranscriptionOptions"; diff --git a/src/packages/AbstractRestfulClient.ts b/src/packages/AbstractRestfulClient.ts index 8185ff75..1411ef10 100644 --- a/src/packages/AbstractRestfulClient.ts +++ b/src/packages/AbstractRestfulClient.ts @@ -48,7 +48,7 @@ export class AbstractRestfulClient { return params; } - params.headers = { "Content-Type": "application/json", ...options?.headers }; + params.headers = { ...options?.headers }; params.body = body; return { ...params, ...parameters, duplex: "half" }; } diff --git a/src/packages/PrerecordedClient.ts b/src/packages/PrerecordedClient.ts index f36a7f6d..8342fef6 100644 --- a/src/packages/PrerecordedClient.ts +++ b/src/packages/PrerecordedClient.ts @@ -1,11 +1,5 @@ import { AbstractRestfulClient } from "./AbstractRestfulClient"; -import { - CallbackUrl, - appendSearchParams, - isBufferSource, - isReadStreamSource, - isUrlSource, -} from "../lib/helpers"; +import { CallbackUrl, appendSearchParams, isFileSource, isUrlSource } from "../lib/helpers"; import { DeepgramError, isDeepgramError } from "../lib/errors"; import type { AsyncPrerecordedResponse, @@ -24,7 +18,10 @@ export class PrerecordedClient extends AbstractRestfulClient { endpoint = "v1/listen" ): Promise> { try { + this.headers["Content-Type"] = "application/json"; + let body; + if (isUrlSource(source)) { body = JSON.stringify(source); } else { @@ -62,19 +59,13 @@ export class PrerecordedClient extends AbstractRestfulClient { endpoint = "v1/listen" ): Promise> { try { - if (source.mimetype === undefined || source.mimetype.length === 0) { - throw new DeepgramError( - "Mimetype must be provided if the source is a Buffer or a Readable" - ); - } - - this.headers["Content-Type"] = source.mimetype; + // deepgram ignores the mimetype if it's not `application/json` + this.headers["Content-Type"] = "deepgram/audio+video"; let body; - if (isBufferSource(source)) { - body = source.buffer; - } else if (isReadStreamSource(source)) { - body = source.stream; + + if (isFileSource(source)) { + body = source; } else { throw new DeepgramError("Unknown transcription source type"); } @@ -111,7 +102,10 @@ export class PrerecordedClient extends AbstractRestfulClient { endpoint = "v1/listen" ): Promise> { try { + this.headers["Content-Type"] = "application/json"; + let body; + if (isUrlSource(source)) { body = JSON.stringify(source); } else { @@ -147,19 +141,13 @@ export class PrerecordedClient extends AbstractRestfulClient { endpoint = "v1/listen" ): Promise> { try { - if (source.mimetype === undefined || source.mimetype.length === 0) { - throw new DeepgramError( - "Mimetype must be provided if the source is a Buffer or a Readable" - ); - } - - this.headers["Content-Type"] = source.mimetype; + // deepgram ignores the mimetype if it's not `application/json` + this.headers["Content-Type"] = "deepgram/audio+video"; let body; - if (isBufferSource(source)) { - body = source.buffer; - } else if (isReadStreamSource(source)) { - body = source.stream; + + if (isFileSource(source)) { + body = source; } else { throw new DeepgramError("Unknown transcription source type"); } diff --git a/test/prerecorded.test.ts b/test/prerecorded.test.ts index bf885704..df7aaa6e 100644 --- a/test/prerecorded.test.ts +++ b/test/prerecorded.test.ts @@ -3,12 +3,9 @@ import { createClient } from "../src"; import { faker } from "@faker-js/faker"; import DeepgramClient from "../src/DeepgramClient"; import { CallbackUrl } from "../src/lib/helpers"; -import { BufferSource, UrlSource } from "../src/lib/types"; +import { UrlSource } from "../src/lib/types"; -const bufferSource: BufferSource = { - buffer: Buffer.from("string"), - mimetype: "video/mpeg", -}; +const bufferSource: Buffer = Buffer.from("string"); const urlSource: UrlSource = { url: faker.internet.url({ appendSlash: false }) + "/nasa.wav",