UNPKG

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.

169 lines (159 loc) 6.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useDeepgramManagement = useDeepgramManagement; var _react = require("react"); var _index = require("./constants/index.js"); var _index2 = require("./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; } function useDeepgramManagement() { const controllersRef = (0, _react.useRef)(new Set()); const dgRequest = (0, _react.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(`${_index.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 = (0, _react.useMemo)(() => ({ list: (includeOutdated = false, query) => { let path = (0, _index2.dgPath)('models'); if (includeOutdated) path += '?include_outdated=true'; return dgRequest(buildUrl(path, query)); }, get: (modelId, query) => dgRequest(buildUrl((0, _index2.dgPath)('models', modelId), query)) }), [dgRequest]); /** ------------------- PROJECTS ------------------- */ const projects = (0, _react.useMemo)(() => ({ list: async query => { const res = await dgRequest(buildUrl((0, _index2.dgPath)('projects'), query)); return res.projects; }, get: (id, query) => dgRequest(buildUrl((0, _index2.dgPath)('projects', id), query)), delete: (id, query) => dgRequest(buildUrl((0, _index2.dgPath)('projects', id), query), { method: 'DELETE' }), patch: (id, body, query) => dgRequest(buildUrl((0, _index2.dgPath)('projects', id), query), { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }), listModels: (id, query) => dgRequest(buildUrl((0, _index2.dgPath)('projects', id, 'models'), query)), getModel: (pid, mid, query) => dgRequest(buildUrl((0, _index2.dgPath)('projects', pid, 'models', mid), query)) }), [dgRequest]); /** ------------------- KEYS ------------------- */ const keys = (0, _react.useMemo)(() => ({ list: (pid, query) => dgRequest(buildUrl((0, _index2.dgPath)('projects', pid, 'keys'), query)), create: (pid, body) => dgRequest((0, _index2.dgPath)('projects', pid, 'keys'), { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }), get: (pid, kid, query) => dgRequest(buildUrl((0, _index2.dgPath)('projects', pid, 'keys', kid), query)), delete: (pid, kid, query) => dgRequest(buildUrl((0, _index2.dgPath)('projects', pid, 'keys', kid), query), { method: 'DELETE' }) }), [dgRequest]); /** ------------------- MEMBERS ------------------- */ const members = (0, _react.useMemo)(() => ({ list: (pid, query) => dgRequest(buildUrl((0, _index2.dgPath)('projects', pid, 'members'), query)), delete: (pid, mid) => dgRequest((0, _index2.dgPath)('projects', pid, 'members', mid), { method: 'DELETE' }) }), [dgRequest]); /** ------------------- SCOPES ------------------- */ const scopes = (0, _react.useMemo)(() => ({ list: (pid, mid, query) => dgRequest(buildUrl((0, _index2.dgPath)('projects', pid, 'members', mid, 'scopes'), query)), update: (pid, mid, body) => dgRequest((0, _index2.dgPath)('projects', pid, 'members', mid, 'scopes'), { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }) }), [dgRequest]); /** ------------------- INVITATIONS ------------------- */ const invitations = (0, _react.useMemo)(() => ({ list: (pid, query) => dgRequest(buildUrl((0, _index2.dgPath)('projects', pid, 'invitations'), query)), create: (pid, body) => dgRequest((0, _index2.dgPath)('projects', pid, 'invitations'), { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }), delete: (pid, inviteId) => dgRequest((0, _index2.dgPath)('projects', pid, 'invitations', inviteId), { method: 'DELETE' }), leave: (pid, query) => dgRequest(buildUrl((0, _index2.dgPath)('projects', pid, 'invitations'), query), { method: 'DELETE' }) }), [dgRequest]); /** ------------------- USAGE ------------------- */ const usage = (0, _react.useMemo)(() => ({ listRequests: (pid, query) => dgRequest(buildUrl((0, _index2.dgPath)('projects', pid, 'requests'), query)), getRequest: (pid, rid, query) => dgRequest(buildUrl((0, _index2.dgPath)('projects', pid, 'requests', rid), query)), listFields: (pid, query) => dgRequest(buildUrl((0, _index2.dgPath)('projects', pid, 'usage', 'fields'), query)), getBreakdown: (pid, query) => dgRequest(buildUrl((0, _index2.dgPath)('projects', pid, 'usage', 'breakdown'), query)) }), [dgRequest]); /** ------------------- PURCHASES ------------------- */ const purchases = (0, _react.useMemo)(() => ({ list: (pid, query) => dgRequest(buildUrl((0, _index2.dgPath)('projects', pid, 'purchases'), query)) }), [dgRequest]); /** ------------------- BALANCES ------------------- */ const balances = (0, _react.useMemo)(() => ({ list: (pid, query) => dgRequest(buildUrl((0, _index2.dgPath)('projects', pid, 'balances'), query)), get: (pid, bid, query) => dgRequest(buildUrl((0, _index2.dgPath)('projects', pid, 'balances', bid), query)) }), [dgRequest]); (0, _react.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