Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove mimetype requirement from batch requests (rfc #4) #177

Merged
merged 2 commits into from
Oct 18, 2023
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
18 changes: 7 additions & 11 deletions examples/node-prerecorded/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
Expand All @@ -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",
}
Expand All @@ -51,6 +45,8 @@ const buffer = async () => {
if (!error) console.log(result);
};

url();
readstream();
buffer();
(async () => {
await url();
await readstream();
await buffer();
})();
1 change: 0 additions & 1 deletion src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
27 changes: 12 additions & 15 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
@@ -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(/\/$/, "");
Expand Down Expand Up @@ -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;
};
Expand Down
14 changes: 2 additions & 12 deletions src/lib/types/PrerecordedSource.ts
Original file line number Diff line number Diff line change
@@ -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;
}
8 changes: 1 addition & 7 deletions src/lib/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion src/packages/AbstractRestfulClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" };
}
Expand Down
46 changes: 17 additions & 29 deletions src/packages/PrerecordedClient.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -24,7 +18,10 @@ export class PrerecordedClient extends AbstractRestfulClient {
endpoint = "v1/listen"
): Promise<DeepgramResponse<SyncPrerecordedResponse>> {
try {
this.headers["Content-Type"] = "application/json";

let body;

if (isUrlSource(source)) {
body = JSON.stringify(source);
} else {
Expand Down Expand Up @@ -62,19 +59,13 @@ export class PrerecordedClient extends AbstractRestfulClient {
endpoint = "v1/listen"
): Promise<DeepgramResponse<SyncPrerecordedResponse>> {
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");
}
Expand Down Expand Up @@ -111,7 +102,10 @@ export class PrerecordedClient extends AbstractRestfulClient {
endpoint = "v1/listen"
): Promise<DeepgramResponse<AsyncPrerecordedResponse>> {
try {
this.headers["Content-Type"] = "application/json";

let body;

if (isUrlSource(source)) {
body = JSON.stringify(source);
} else {
Expand Down Expand Up @@ -147,19 +141,13 @@ export class PrerecordedClient extends AbstractRestfulClient {
endpoint = "v1/listen"
): Promise<DeepgramResponse<AsyncPrerecordedResponse>> {
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");
}
Expand Down
7 changes: 2 additions & 5 deletions test/prerecorded.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down