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.
95 lines (94 loc) • 2.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useDeepgramTextIntelligence = useDeepgramTextIntelligence;
var _react = require("react");
var _index = require("./constants/index.js");
var _index2 = require("./helpers/index.js");
function useDeepgramTextIntelligence({
onBeforeAnalyze = () => {},
onAnalyzeSuccess = () => {},
onAnalyzeError = () => {},
options = {}
} = {}) {
const abortCtrl = (0, _react.useRef)(null);
const {
summarize,
topics,
customTopic,
customTopicMode,
intents,
customIntent,
customIntentMode,
sentiment,
language,
callback,
callbackMethod
} = options;
const analyze = (0, _react.useCallback)(async input => {
onBeforeAnalyze();
try {
const apiKey = globalThis.__DEEPGRAM_API_KEY__;
if (!apiKey) throw new Error('Deepgram API key missing');
const {
text,
url: sourceUrl
} = input;
if (!text && !sourceUrl) {
throw new Error('Either `text` or `url` is required to analyze content.');
}
const paramMap = {
summarize,
topics,
intents,
sentiment,
language,
custom_topic: customTopic,
custom_topic_mode: customTopicMode,
custom_intent: customIntent,
custom_intent_mode: customIntentMode,
callback,
callback_method: callbackMethod
};
const params = (0, _index2.buildParams)(paramMap);
const url = `${_index.DEEPGRAM_BASEURL}/read${params ? `?${params}` : ''}`;
abortCtrl.current?.abort();
abortCtrl.current = new AbortController();
const res = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Token ${apiKey}`
},
body: JSON.stringify({
...(text ? {
text
} : {}),
...(sourceUrl ? {
url: sourceUrl
} : {})
}),
signal: abortCtrl.current.signal
});
if (!res.ok) {
const errText = await res.text();
throw new Error(`HTTP ${res.status}: ${errText}`);
}
const json = await res.json();
onAnalyzeSuccess(json);
} catch (err) {
if (err.name === 'AbortError') return;
onAnalyzeError(err);
}
}, [onBeforeAnalyze, onAnalyzeSuccess, onAnalyzeError, summarize, topics, customTopic, customTopicMode, intents, customIntent, customIntentMode, sentiment, language, callback, callbackMethod]);
(0, _react.useEffect)(() => {
return () => {
abortCtrl.current?.abort();
};
}, []);
return {
analyze
};
}
//# sourceMappingURL=useDeepgramTextIntelligence.js.map