Skip to content

Commit ab06832

Browse files
wobsorianoLekoArts
andauthored
feat(astro): Add support for type-safe environment variables (#5104)
Co-authored-by: Lennart <lekoarts@gmail.com>
1 parent 82cf230 commit ab06832

File tree

5 files changed

+293
-262
lines changed

5 files changed

+293
-262
lines changed

.changeset/chilly-suns-beg.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
'@clerk/astro': minor
3+
---
4+
5+
Add support for type-safe environment variables using the [`astro:env` API](https://docs.astro.build/en/reference/configuration-reference/#env).
6+
7+
The integration now provides a type-safe schema for all Clerk environment variables by default. You can use the environment variables like so:
8+
9+
```js
10+
import { PUBLIC_CLERK_PUBLISHABLE_KEY } from "astro:env/client"
11+
import { CLERK_SECRET_KEY } from "astro:env/server"
12+
```
13+
14+
To override this behavior, you can disable the feature by setting `enableEnvSchema` to `false`:
15+
16+
```js
17+
export default defineConfig({
18+
integrations: [clerk({ enableEnvSchema: false })],
19+
})
20+
```

packages/astro/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@
8787
"nanoid": "5.0.9",
8888
"nanostores": "0.11.3"
8989
},
90-
"devDependencies": {},
90+
"devDependencies": {
91+
"astro": "^5.2.5"
92+
},
9193
"peerDependencies": {
9294
"astro": "^4.15.0 || ^5.0.0"
9395
},

packages/astro/src/integration/create-integration.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ClerkOptions } from '@clerk/types';
22
import type { AstroIntegration } from 'astro';
3+
import { envField } from 'astro/config';
34

45
import { name as packageName, version as packageVersion } from '../../package.json';
56
import type { AstroClerkIntegrationParams } from '../types';
@@ -13,11 +14,12 @@ type HotloadAstroClerkIntegrationParams = AstroClerkIntegrationParams & {
1314
clerkJSUrl?: string;
1415
clerkJSVariant?: 'headless' | '';
1516
clerkJSVersion?: string;
17+
enableEnvSchema?: boolean;
1618
};
1719

1820
function createIntegration<Params extends HotloadAstroClerkIntegrationParams>() {
1921
return (params?: Params): AstroIntegration => {
20-
const { proxyUrl, isSatellite, domain, signInUrl, signUpUrl } = params || {};
22+
const { proxyUrl, isSatellite, domain, signInUrl, signUpUrl, enableEnvSchema = true } = params || {};
2123

2224
// These are not provided when the "bundled" integration is used
2325
const clerkJSUrl = (params as any)?.clerkJSUrl as string | undefined;
@@ -79,6 +81,11 @@ function createIntegration<Params extends HotloadAstroClerkIntegrationParams>()
7981
target: 'es2022',
8082
},
8183
},
84+
env: {
85+
schema: {
86+
...(enableEnvSchema ? createClerkEnvSchema() : {}),
87+
},
88+
},
8289
});
8390

8491
/**
@@ -158,4 +165,27 @@ function createIntegration<Params extends HotloadAstroClerkIntegrationParams>()
158165
};
159166
}
160167

168+
function createClerkEnvSchema() {
169+
return {
170+
PUBLIC_CLERK_PUBLISHABLE_KEY: envField.string({ context: 'client', access: 'public' }),
171+
PUBLIC_CLERK_SIGN_IN_URL: envField.string({ context: 'client', access: 'public', optional: true }),
172+
PUBLIC_CLERK_SIGN_UP_URL: envField.string({ context: 'client', access: 'public', optional: true }),
173+
PUBLIC_CLERK_IS_SATELLITE: envField.boolean({ context: 'client', access: 'public', optional: true }),
174+
PUBLIC_CLERK_PROXY_URL: envField.string({ context: 'client', access: 'public', optional: true, url: true }),
175+
PUBLIC_CLERK_DOMAIN: envField.string({ context: 'client', access: 'public', optional: true, url: true }),
176+
PUBLIC_CLERK_JS_URL: envField.string({ context: 'client', access: 'public', optional: true, url: true }),
177+
PUBLIC_CLERK_JS_VARIANT: envField.enum({
178+
context: 'client',
179+
access: 'public',
180+
optional: true,
181+
values: ['headless'],
182+
}),
183+
PUBLIC_CLERK_JS_VERSION: envField.string({ context: 'client', access: 'public', optional: true }),
184+
PUBLIC_CLERK_TELEMETRY_DISABLED: envField.boolean({ context: 'client', access: 'public', optional: true }),
185+
PUBLIC_CLERK_TELEMETRY_DEBUG: envField.boolean({ context: 'client', access: 'public', optional: true }),
186+
CLERK_SECRET_KEY: envField.string({ context: 'server', access: 'secret' }),
187+
CLERK_JWT_KEY: envField.string({ context: 'server', access: 'secret', optional: true }),
188+
};
189+
}
190+
161191
export { createIntegration };

packages/astro/tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"forceConsistentCasingInFileNames": true,
1010
"inlineSources": false,
1111
"isolatedModules": true,
12-
"moduleResolution": "node",
12+
"moduleResolution": "Bundler",
1313
"noUnusedLocals": false,
1414
"noUnusedParameters": false,
1515
"preserveWatchOutput": true,
@@ -22,7 +22,8 @@
2222
"outDir": "dist",
2323
"resolveJsonModule": true,
2424
"declarationDir": "dist/types",
25-
"noUncheckedIndexedAccess": true
25+
"noUncheckedIndexedAccess": true,
26+
"rootDir": "src"
2627
},
2728
"exclude": [
2829
"dist",

0 commit comments

Comments
 (0)