mirror of
https://github.com/Tony0410/News-reader-pro.git
synced 2026-05-24 21:31:44 +08:00
Use Vite env key for Gemini
This commit is contained in:
@@ -15,6 +15,6 @@ View your app in AI Studio: https://ai.studio/apps/drive/1a8wkyYOUvPDWvUXbrtN2dz
|
|||||||
|
|
||||||
1. Install dependencies:
|
1. Install dependencies:
|
||||||
`npm install`
|
`npm install`
|
||||||
2. Set the `GEMINI_API_KEY` in [.env.local](.env.local) to your Gemini API key
|
2. Set the `VITE_API_KEY` in [.env.local](.env.local) to your Gemini API key (e.g., `VITE_API_KEY=your_key_here`)
|
||||||
3. Run the app:
|
3. Run the app:
|
||||||
`npm run dev`
|
`npm run dev`
|
||||||
|
|||||||
@@ -2,10 +2,14 @@ import { GoogleGenAI, Modality } from '@google/genai';
|
|||||||
import { VoiceName } from '../types';
|
import { VoiceName } from '../types';
|
||||||
|
|
||||||
const getAiClient = () => {
|
const getAiClient = () => {
|
||||||
const apiKey = process.env.API_KEY;
|
const apiKey = import.meta.env.VITE_API_KEY;
|
||||||
|
|
||||||
if (!apiKey) {
|
if (!apiKey) {
|
||||||
throw new Error("API Key is missing");
|
throw new Error(
|
||||||
|
"Gemini API key is missing. Set VITE_API_KEY in your .env.local file (e.g., VITE_API_KEY=your_key_here)."
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new GoogleGenAI({ apiKey });
|
return new GoogleGenAI({ apiKey });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,6 @@
|
|||||||
"noUnusedParameters": false,
|
"noUnusedParameters": false,
|
||||||
"noFallthroughCasesInSwitch": true
|
"noFallthroughCasesInSwitch": true
|
||||||
},
|
},
|
||||||
"include": ["**/*.ts", "**/*.tsx"],
|
"include": ["**/*.ts", "**/*.tsx", "**/*.d.ts"],
|
||||||
"exclude": ["node_modules"]
|
"exclude": ["node_modules"]
|
||||||
}
|
}
|
||||||
1
vite-env.d.ts
vendored
Normal file
1
vite-env.d.ts
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/// <reference types="vite/client" />
|
||||||
@@ -1,16 +1,6 @@
|
|||||||
import { defineConfig, loadEnv } from 'vite';
|
import { defineConfig } from 'vite';
|
||||||
import react from '@vitejs/plugin-react';
|
import react from '@vitejs/plugin-react';
|
||||||
|
|
||||||
export default defineConfig(({ mode }) => {
|
export default defineConfig({
|
||||||
// Load env file based on `mode` in the current working directory.
|
plugins: [react()]
|
||||||
const env = loadEnv(mode, (process as any).cwd(), '');
|
|
||||||
|
|
||||||
return {
|
|
||||||
plugins: [react()],
|
|
||||||
define: {
|
|
||||||
// This allows the app code to continue using process.env.API_KEY
|
|
||||||
// even though it is running in the browser.
|
|
||||||
'process.env.API_KEY': JSON.stringify(env.API_KEY)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user