react-native-deepgram
Version:
React Native SDK for Deepgram's AI-powered speech-to-text, real-time transcription, and text intelligence APIs. Supports live audio streaming, file transcription, sentiment analysis, and topic detection for iOS and Android.
166 lines (155 loc) • 6.05 kB
JavaScript
;
import { useRef, useCallback, useEffect, useMemo } from 'react';
import { DEEPGRAM_BASEURL } from "./constants/index.js";
import { dgPath } from "./helpers/index.js";
/**
* Append a query‐param object to a path:
* buildUrl('/projects/123/requests', { page:2, after:'…' })
* → '/projects/123/requests?page=2&after=…'
*/
function buildUrl(path, query) {
if (!query) return path;
const qp = new URLSearchParams();
for (const [k, v] of Object.entries(query)) {
if (v != null) qp.set(k, String(v));
}
const qs = qp.toString();
return qs ? `${path}?${qs}` : path;
}
export function useDeepgramManagement() {
const controllersRef = useRef(new Set());
const dgRequest = useCallback(async (path, init = {}) => {
const ctrl = new AbortController();
controllersRef.current.add(ctrl);
const key = globalThis.__DEEPGRAM_API_KEY__;
if (!key) throw new Error('Deepgram API key missing');
try {
const res = await fetch(`${DEEPGRAM_BASEURL}${path}`, {
signal: ctrl.signal,
headers: {
Authorization: `Token ${key}`,
...(init.headers || {})
},
...init
});
if (!res.ok) throw new Error(`DG ${res.status}: ${await res.text()}`);
return await res.json();
} finally {
controllersRef.current.delete(ctrl);
}
}, []);
/** ------------------- MODELS ------------------- */
const models = useMemo(() => ({
list: (includeOutdated = false, query) => {
let path = dgPath('models');
if (includeOutdated) path += '?include_outdated=true';
return dgRequest(buildUrl(path, query));
},
get: (modelId, query) => dgRequest(buildUrl(dgPath('models', modelId), query))
}), [dgRequest]);
/** ------------------- PROJECTS ------------------- */
const projects = useMemo(() => ({
list: async query => {
const res = await dgRequest(buildUrl(dgPath('projects'), query));
return res.projects;
},
get: (id, query) => dgRequest(buildUrl(dgPath('projects', id), query)),
delete: (id, query) => dgRequest(buildUrl(dgPath('projects', id), query), {
method: 'DELETE'
}),
patch: (id, body, query) => dgRequest(buildUrl(dgPath('projects', id), query), {
method: 'PATCH',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
}),
listModels: (id, query) => dgRequest(buildUrl(dgPath('projects', id, 'models'), query)),
getModel: (pid, mid, query) => dgRequest(buildUrl(dgPath('projects', pid, 'models', mid), query))
}), [dgRequest]);
/** ------------------- KEYS ------------------- */
const keys = useMemo(() => ({
list: (pid, query) => dgRequest(buildUrl(dgPath('projects', pid, 'keys'), query)),
create: (pid, body) => dgRequest(dgPath('projects', pid, 'keys'), {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
}),
get: (pid, kid, query) => dgRequest(buildUrl(dgPath('projects', pid, 'keys', kid), query)),
delete: (pid, kid, query) => dgRequest(buildUrl(dgPath('projects', pid, 'keys', kid), query), {
method: 'DELETE'
})
}), [dgRequest]);
/** ------------------- MEMBERS ------------------- */
const members = useMemo(() => ({
list: (pid, query) => dgRequest(buildUrl(dgPath('projects', pid, 'members'), query)),
delete: (pid, mid) => dgRequest(dgPath('projects', pid, 'members', mid), {
method: 'DELETE'
})
}), [dgRequest]);
/** ------------------- SCOPES ------------------- */
const scopes = useMemo(() => ({
list: (pid, mid, query) => dgRequest(buildUrl(dgPath('projects', pid, 'members', mid, 'scopes'), query)),
update: (pid, mid, body) => dgRequest(dgPath('projects', pid, 'members', mid, 'scopes'), {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
})
}), [dgRequest]);
/** ------------------- INVITATIONS ------------------- */
const invitations = useMemo(() => ({
list: (pid, query) => dgRequest(buildUrl(dgPath('projects', pid, 'invitations'), query)),
create: (pid, body) => dgRequest(dgPath('projects', pid, 'invitations'), {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
}),
delete: (pid, inviteId) => dgRequest(dgPath('projects', pid, 'invitations', inviteId), {
method: 'DELETE'
}),
leave: (pid, query) => dgRequest(buildUrl(dgPath('projects', pid, 'invitations'), query), {
method: 'DELETE'
})
}), [dgRequest]);
/** ------------------- USAGE ------------------- */
const usage = useMemo(() => ({
listRequests: (pid, query) => dgRequest(buildUrl(dgPath('projects', pid, 'requests'), query)),
getRequest: (pid, rid, query) => dgRequest(buildUrl(dgPath('projects', pid, 'requests', rid), query)),
listFields: (pid, query) => dgRequest(buildUrl(dgPath('projects', pid, 'usage', 'fields'), query)),
getBreakdown: (pid, query) => dgRequest(buildUrl(dgPath('projects', pid, 'usage', 'breakdown'), query))
}), [dgRequest]);
/** ------------------- PURCHASES ------------------- */
const purchases = useMemo(() => ({
list: (pid, query) => dgRequest(buildUrl(dgPath('projects', pid, 'purchases'), query))
}), [dgRequest]);
/** ------------------- BALANCES ------------------- */
const balances = useMemo(() => ({
list: (pid, query) => dgRequest(buildUrl(dgPath('projects', pid, 'balances'), query)),
get: (pid, bid, query) => dgRequest(buildUrl(dgPath('projects', pid, 'balances', bid), query))
}), [dgRequest]);
useEffect(() => {
const controllers = controllersRef.current;
return () => {
controllers.forEach(c => c.abort());
controllers.clear();
};
}, []);
return {
models,
projects,
keys,
members,
scopes,
invitations,
usage,
purchases,
balances
};
}
//# sourceMappingURL=useDeepgramManagement.js.map