gemini-genai
Version:
A TypeScript based npm package providing a type-safe interface for integrating Google Gemini API text generation
29 lines (28 loc) • 916 B
JavaScript
async function g(e, i) {
var r, n, o, a, s;
if (!e)
return { text: null, error: new Error("API key is required.") };
const c = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${e}`;
try {
const t = await fetch(c, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
contents: [{ parts: [{ text: i }] }]
})
});
if (!t.ok)
throw new Error(`HTTP error! status: ${t.status}`);
return { text: ((s = (a = (o = (n = (r = (await t.json()).candidates) == null ? void 0 : r[0]) == null ? void 0 : n.content) == null ? void 0 : o.parts) == null ? void 0 : a[0]) == null ? void 0 : s.text) || null, error: null };
} catch (t) {
return {
text: null,
error: t instanceof Error ? t : new Error(String(t))
};
}
}
export {
g as generateText
};