UNPKG

gemini-genai

Version:

A TypeScript based npm package providing a type-safe interface for integrating Google Gemini API text generation

2 lines (1 loc) 972 B
(function(t,n){typeof exports=="object"&&typeof module<"u"?n(exports):typeof define=="function"&&define.amd?define(["exports"],n):(t=typeof globalThis<"u"?globalThis:t||self,n(t.geminiGenai={}))})(this,function(t){"use strict";async function n(r,d){var o,i,s,a,u;if(!r)return{text:null,error:new Error("API key is required.")};const c=`https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${r}`;try{const e=await fetch(c,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({contents:[{parts:[{text:d}]}]})});if(!e.ok)throw new Error(`HTTP error! status: ${e.status}`);return{text:((u=(a=(s=(i=(o=(await e.json()).candidates)==null?void 0:o[0])==null?void 0:i.content)==null?void 0:s.parts)==null?void 0:a[0])==null?void 0:u.text)||null,error:null}}catch(e){return{text:null,error:e instanceof Error?e:new Error(String(e))}}}t.generateText=n,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"})});