Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ https://sheets-json-api.netlify.app/1vufOODlks7O9PGak54hMNP4LWBUAoP-XB9n3VW_aw5Y

The Edge Function lives in `netlify/edge-functions/opensheet.ts`.

### Environment variables

The function looks for a `GOOGLE_API_KEY` value using `process.env` in Node or
`Deno.env.get` in Netlify's Edge runtime. When the variable is absent, a default
API key is used.

### Running tests

```sh
Expand Down
5 changes: 4 additions & 1 deletion netlify/edge-functions/opensheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import type { Context } from "@netlify/edge-functions";

export default async function handler(request: Request, context: Context) {
const GOOGLE_API_KEY = process.env.GOOGLE_API_KEY?.trim() || "AIzaSyC6y93Jo0fA1GH54L7VOkJzgZkiaXSSOOU";
const GOOGLE_API_KEY =
((globalThis as any).process?.env?.GOOGLE_API_KEY ??
(globalThis as any).Deno?.env?.get("GOOGLE_API_KEY"))?.trim() ||
"AIzaSyC6y93Jo0fA1GH54L7VOkJzgZkiaXSSOOU";
if (!GOOGLE_API_KEY) {
return error("Missing GOOGLE_API_KEY environment variable", 500);
}
Expand Down
30 changes: 30 additions & 0 deletions test/opensheet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,33 @@ test('falls back to static content on root path', async () => {
const res = await handler(req, { next: () => nextResponse, waitUntil: () => {} } as any);
assert.strictEqual(res, nextResponse);
});

test('uses Deno env when process env missing', async () => {
const originalKey = process.env.GOOGLE_API_KEY;
delete process.env.GOOGLE_API_KEY;

(globalThis as any).Deno = { env: { get: () => 'FAKE_KEY' } };
(globalThis as any).caches = {
default: {
async match() {
return undefined;
},
async put() {},
},
};

const originalFetch = globalThis.fetch;
globalThis.fetch = async () =>
new Response(
JSON.stringify({ values: [["headline"], ["It's working!"]] })
);

const req = new Request('https://example.com/test-sheet/Sheet1');
const res = await handler(req, context);
const data = await res.json();
assert.deepStrictEqual(data, [{ headline: "It's working!" }]);

globalThis.fetch = originalFetch;
delete (globalThis as any).Deno;
process.env.GOOGLE_API_KEY = originalKey;
});