This repository has been archived by the owner on Feb 10, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cloudflare): support astro:env (#258)
Co-authored-by: Alexander Niebuhr <alexander@nbhr.io> Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
- Loading branch information
1 parent
4a28bf6
commit 033847d
Showing
16 changed files
with
203 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@astrojs/cloudflare': minor | ||
--- | ||
|
||
Adds support for experimental `astro:env` released in Astro 4.10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
functions | ||
.mf | ||
.wrangler | ||
.astro |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { GetEnv } from 'astro/env/setup'; | ||
|
||
export const createGetEnv = | ||
(env: Record<string, unknown>): GetEnv => | ||
(key) => { | ||
const v = env[key]; | ||
if (typeof v === 'undefined' || typeof v === 'string') { | ||
return v; | ||
} | ||
if (typeof v === 'boolean' || typeof v === 'number') { | ||
// let astro:env handle the validation and transformation | ||
return v.toString(); | ||
} | ||
return undefined; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import * as assert from 'node:assert/strict'; | ||
import { after, before, describe, it } from 'node:test'; | ||
import { fileURLToPath } from 'node:url'; | ||
import * as cheerio from 'cheerio'; | ||
import { astroCli, wranglerCli } from './_test-utils.js'; | ||
|
||
const root = new URL('./fixtures/astro-env/', import.meta.url); | ||
|
||
describe('AstroEnv', () => { | ||
let wrangler; | ||
|
||
before(async () => { | ||
process.env.PUBLIC_API_URL = 'https://google.de'; | ||
process.env.PUBLIC_PORT = '4322'; | ||
await astroCli(fileURLToPath(root), 'build'); | ||
|
||
wrangler = wranglerCli(fileURLToPath(root)); | ||
await new Promise((resolve) => { | ||
wrangler.stdout.on('data', (data) => { | ||
// console.log('[stdout]', data.toString()); | ||
if (data.toString().includes('http://127.0.0.1:8788')) resolve(); | ||
}); | ||
wrangler.stderr.on('data', (data) => { | ||
// console.log('[stderr]', data.toString()); | ||
}); | ||
}); | ||
}); | ||
|
||
after((done) => { | ||
wrangler.kill(); | ||
}); | ||
|
||
it('runtime', async () => { | ||
const res = await fetch('http://127.0.0.1:8788/'); | ||
const html = await res.text(); | ||
const $ = cheerio.load(html); | ||
assert.equal( | ||
$('#runtime').text().includes('https://google.de') && | ||
$('#runtime').text().includes('4322') && | ||
$('#runtime').text().includes('123456789'), | ||
true | ||
); | ||
}); | ||
|
||
it('client', async () => { | ||
const res = await fetch('http://127.0.0.1:8788/'); | ||
const html = await res.text(); | ||
const $ = cheerio.load(html); | ||
assert.equal($('#client').text().includes('https://google.de'), true); | ||
}); | ||
|
||
it('server', async () => { | ||
const res = await fetch('http://127.0.0.1:8788/'); | ||
const html = await res.text(); | ||
const $ = cheerio.load(html); | ||
assert.equal($('#server').text().includes('4322'), true); | ||
}); | ||
|
||
it('secret', async () => { | ||
const res = await fetch('http://127.0.0.1:8788/'); | ||
const html = await res.text(); | ||
const $ = cheerio.load(html); | ||
assert.equal($('#secret').text().includes('123456789'), true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
API_SECRET=123456789 |
21 changes: 21 additions & 0 deletions
21
packages/cloudflare/test/fixtures/astro-env/astro.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import cloudflare from '@astrojs/cloudflare'; | ||
import { defineConfig, envField } from 'astro/config'; | ||
|
||
export default defineConfig({ | ||
experimental: { | ||
rewriting: false, | ||
env: { | ||
schema: { | ||
PUBLIC_API_URL: envField.string({ context: 'client', access: 'public', optional: true }), | ||
PUBLIC_PORT: envField.number({ context: 'server', access: 'public', default: 4321 }), | ||
// API_SECRET: envField.string({ context: 'server', access: 'secret' }), | ||
}, | ||
}, | ||
}, | ||
adapter: cloudflare({ | ||
platformProxy: { | ||
enabled: true, | ||
}, | ||
}), | ||
output: 'server', | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "@test/astro-cloudflare-astro-env", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"@astrojs/cloudflare": "workspace:*", | ||
"astro": "^4.10.1" | ||
}, | ||
"devDependencies": { | ||
"wrangler": "^3.15.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/// <reference path="../.astro/env.d.ts" /> | ||
/// <reference types="astro/client" /> | ||
|
||
type Runtime = import('@astrojs/cloudflare').Runtime; | ||
|
||
declare namespace App { | ||
interface Locals extends Runtime { | ||
otherLocals: { | ||
test: string; | ||
}; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/cloudflare/test/fixtures/astro-env/src/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
import { PUBLIC_API_URL } from "astro:env/client" | ||
import { PUBLIC_PORT, getSecret } from "astro:env/server" | ||
const runtime = Astro.locals.runtime; | ||
--- | ||
<html> | ||
<head> | ||
<title>Astro Env</title> | ||
</head> | ||
<body> | ||
<h1>Astro Env</h1> | ||
<pre id="runtime">{JSON.stringify(runtime.env, null, 2)}</pre> | ||
<div> | ||
<span>PUBLIC_API_URL</span> | ||
<span id="client">{PUBLIC_API_URL}</span> | ||
</div> | ||
<div> | ||
<span>PUBLIC_PORT</span> | ||
<span id="server">{PUBLIC_PORT}</span> | ||
</div> | ||
<div> | ||
<span>getSecret</span> | ||
<span id="secret">{getSecret("API_SECRET")}</span> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "astro/tsconfigs/strict" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
name = "astro-env" | ||
|
||
[vars] | ||
PUBLIC_API_URL = "https://google.de" | ||
PUBLIC_PORT = 4322 |
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.