Skip to content

Commit b7e1e2f

Browse files
committed
nextjs example
1 parent f3f1a1a commit b7e1e2f

31 files changed

Lines changed: 4435 additions & 0 deletions

examples/nextjs/.env.development

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OVERRIDE_FROM_ENV_SPECIFIC_FILE=override-from-env-file--dev

examples/nextjs/.env.production

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
OVERRIDE_FROM_ENV_SPECIFIC_FILE=override-from-env-file--prod
2+
SECRET_OVERRIDE_FROM_ENV_SPECIFIC_FILE=secret-override-from-env-file--prod

examples/nextjs/.env.schema

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# @currentEnv=$APP_ENV
2+
# @defaultSensitive=false
3+
# @generateTypes(lang='ts', path='env.d.ts')
4+
# ---
5+
6+
APP_ENV=development
7+
8+
NEXT_PUBLIC_FOO=next-public-foo
9+
10+
PUBLIC_FOO=public-foo
11+
# @sensitive
12+
SECRET_FOO=secret-foo
13+
14+
DEFAULT_FROM_SCHEMA=default-val-from-schema
15+
OVERRIDE_FROM_ENV_SPECIFIC_FILE=default-val-from-schema
16+
# @sensitive
17+
SECRET_OVERRIDE_FROM_ENV_SPECIFIC_FILE=default-val-from-schema
18+
OVERRIDE_FROM_UI=default-val-from-schema
19+
20+
21+
# --- check type generation for non-string items ---
22+
# @type=enum(a, b, c)
23+
STRING_ENUM=a
24+
# @type=boolean
25+
BOOL_ITEM=true
26+
# @type=number
27+
NUMBER_ITEM=3000

examples/nextjs/.gitignore

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.*
7+
.yarn/*
8+
!.yarn/patches
9+
!.yarn/plugins
10+
!.yarn/releases
11+
!.yarn/versions
12+
13+
# testing
14+
/coverage
15+
16+
# next.js
17+
/.next/
18+
/out/
19+
20+
# production
21+
/build
22+
23+
# misc
24+
.DS_Store
25+
*.pem
26+
27+
# debug
28+
npm-debug.log*
29+
yarn-debug.log*
30+
yarn-error.log*
31+
.pnpm-debug.log*
32+
33+
# env files (can opt-in for committing if needed)
34+
.env.local
35+
.env.*.local
36+
37+
# vercel
38+
.vercel
39+
40+
# typescript
41+
*.tsbuildinfo
42+
next-env.d.ts

examples/nextjs/.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
22.15.1

examples/nextjs/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app).
2+
3+
## Getting Started
4+
5+
First, run the development server:
6+
7+
```bash
8+
npm run dev
9+
# or
10+
yarn dev
11+
# or
12+
pnpm dev
13+
# or
14+
bun dev
15+
```
16+
17+
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
18+
19+
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
20+
21+
This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.
22+
23+
## Learn More
24+
25+
To learn more about Next.js, take a look at the following resources:
26+
27+
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
28+
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
29+
30+
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome!
31+
32+
## Deploy on Vercel
33+
34+
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
35+
36+
Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use client';
2+
3+
import { useState } from 'react';
4+
5+
export default function ActionForm({
6+
getEnvAction,
7+
}: {
8+
getEnvAction: (leak: boolean) => Promise<Record<string, string | undefined>>;
9+
}) {
10+
const [result, setResult] = useState<Record<string, string | undefined> | null>(null);
11+
12+
return (
13+
<div>
14+
<div style={{ display: 'flex', gap: '8px' }}>
15+
<button onClick={async () => setResult(await getEnvAction(false))}>
16+
Call action (safe)
17+
</button>
18+
<button onClick={async () => setResult(await getEnvAction(true))}>
19+
Call action (leak)
20+
</button>
21+
</div>
22+
{result && (
23+
<pre style={{ marginTop: '16px' }}>
24+
{JSON.stringify(result, null, 2)}
25+
</pre>
26+
)}
27+
</div>
28+
);
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { ENV } from 'varlock/env';
2+
import ActionForm from './action-form';
3+
4+
async function getEnvAction(leak: boolean) {
5+
'use server';
6+
return {
7+
PUBLIC_FOO: ENV.PUBLIC_FOO,
8+
NEXT_PUBLIC_FOO: ENV.NEXT_PUBLIC_FOO,
9+
...leak && {
10+
SECRET_FOO: ENV.SECRET_FOO,
11+
},
12+
};
13+
}
14+
15+
export default function ActionPage() {
16+
return (
17+
<div className="grid grid-rows-[20px_1fr_20px] items-center justify-items-center min-h-screen p-8 pb-20 gap-16 sm:p-20 font-[family-name:var(--font-geist-sans)]">
18+
<main className="flex flex-col gap-[32px] row-start-2 items-center sm:items-start">
19+
<h2>Server Action</h2>
20+
<ActionForm getEnvAction={getEnvAction} />
21+
</main>
22+
<footer className="row-start-3 flex gap-[24px] flex-wrap items-center justify-center">
23+
testing env vars in nextjs
24+
</footer>
25+
</div>
26+
);
27+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { ENV } from 'varlock/env';
2+
3+
export const runtime = 'edge';
4+
5+
export async function GET(request: Request) {
6+
const url = new URL(request.url);
7+
const query = new URLSearchParams(url.searchParams);
8+
9+
return Response.json({
10+
PUBLIC_FOO: ENV.PUBLIC_FOO,
11+
...query.get('leak') && {
12+
SECRET_FOO: ENV.SECRET_FOO,
13+
},
14+
});
15+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { NextResponse } from "next/server";
2+
import { ENV } from 'varlock/env';
3+
4+
5+
export const runtime = 'nodejs';
6+
7+
console.log('secret foo = '+ENV.SECRET_FOO);
8+
// console.log('secret static = '+ENV.SECRET_STATIC);
9+
10+
export async function GET(request: Request) {
11+
console.log('secret foo = '+ENV.SECRET_FOO);
12+
// console.log('secret static = '+ENV.SECRET_STATIC);
13+
14+
const url = new URL(request.url);
15+
const query = new URLSearchParams(url.searchParams);
16+
17+
const r = Response.json({
18+
PUBLIC_FOO: ENV.PUBLIC_FOO,
19+
...query.get('leak') && {
20+
SECRET_FOO: ENV.SECRET_FOO,
21+
}
22+
});
23+
24+
// console.log(r.constructor);
25+
// console.log(r.constructor.prototype);
26+
// console.log(r instanceof Response);
27+
// console.log(Response, globalThis.Response, (globalThis as any).DmnoPatchedResponse);
28+
29+
return r;
30+
}

0 commit comments

Comments
 (0)