Skip to content

Commit 40c5d51

Browse files
authored
chore: add vercel example (#117)
* chore: initial vercel example * chore: improved readme * Update README.md * Update README.md * Update tsconfig.json
1 parent 0ca954c commit 40c5d51

File tree

19 files changed

+528
-2
lines changed

19 files changed

+528
-2
lines changed

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ node_modules/
66
docs/
77
.yarn
88
coverage
9+
.next
10+
.vscode

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"packages/sdk/server-node",
88
"packages/sdk/cloudflare",
99
"packages/sdk/cloudflare/example",
10-
"packages/sdk/vercel"
10+
"packages/sdk/vercel",
11+
"packages/sdk/example-vercel"
1112
],
1213
"private": true,
1314
"scripts": {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# next.js
12+
/.next/
13+
/out/
14+
15+
# production
16+
/build
17+
18+
# misc
19+
.DS_Store
20+
*.pem
21+
22+
# debug
23+
npm-debug.log*
24+
yarn-debug.log*
25+
yarn-error.log*
26+
27+
# local env files
28+
.env*.local
29+
30+
# vercel
31+
.vercel
32+
33+
# typescript
34+
*.tsbuildinfo
35+
next-env.d.ts
36+
.vscode
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Example test app for Vercel LaunchDarkly SDK
2+
3+
This is an example test app to showcase the usage of the Vercel LaunchDarkly
4+
SDK. This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
5+
6+
## Prerequisites
7+
8+
A node environment of version 16 and yarn are required to develop in this repository.
9+
You will also need the vercel cli installed and a Vercel account to setup
10+
the test data required by this example. See the [Vercel docs](https://vercel.com/docs/storage/edge-config/get-started) on how to setup your edge config store.
11+
12+
## Usage
13+
14+
1. Follow the [Vercel docs](https://vercel.com/docs/storage/edge-config/get-started) to insert [testData.json](https://github.com/launchdarkly/js-core/blob/main/packages/sdk/vercel/testData.json) to your edge config store.
15+
16+
2. After completing the guide above, you should have linked this example app to your Vercel project and created an `.env.development.local`.
17+
18+
3. At the root of the js-core repo:
19+
20+
```shell
21+
yarn && yarn build
22+
```
23+
24+
4. Then back in this example folder:
25+
26+
```shell
27+
yarn dev
28+
```
29+
30+
5. Open [http://localhost:3000/api/hello](http://localhost:3000/api/hello).
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/** @type {import('next').NextConfig} */
2+
const nextConfig = {
3+
experimental: {
4+
appDir: true,
5+
},
6+
};
7+
8+
module.exports = nextConfig;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "example-vercel",
3+
"version": "0.0.0",
4+
"packageManager": "[email protected]",
5+
"scripts": {
6+
"dev": "next dev",
7+
"build": "tsc",
8+
"start": "next start",
9+
"lint": "next lint"
10+
},
11+
"dependencies": {
12+
"@launchdarkly/vercel-server-sdk": "0.2.2",
13+
"@vercel/edge-config": "^0.1.8",
14+
"next": "13.3.1",
15+
"react": "18.2.0",
16+
"react-dom": "18.2.0"
17+
},
18+
"devDependencies": {
19+
"@types/node": "18.16.2",
20+
"@types/react": "18.2.0",
21+
"@types/react-dom": "18.2.1",
22+
"autoprefixer": "10.4.14",
23+
"postcss": "8.4.23",
24+
"tailwindcss": "3.3.2",
25+
"typescript": "5.0.4"
26+
}
27+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
plugins: {
3+
tailwindcss: {},
4+
autoprefixer: {},
5+
},
6+
};
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { NextResponse } from 'next/server';
2+
import { createClient } from '@vercel/edge-config';
3+
import { init as initLD } from '@launchdarkly/vercel-server-sdk';
4+
5+
export const config = {
6+
runtime: 'edge',
7+
};
8+
9+
export async function GET() {
10+
const sdkKey = 'test-sdk-key';
11+
const flagKey = 'testFlag1';
12+
13+
// default fallthrough evaluates to true
14+
const contextFallthrough = { kind: 'user', key: 'test-user-key-1' };
15+
16+
// matches email targeting rule evaluates to false
17+
const contextEmailRule = { kind: 'user', key: 'test-user-key-1', email: '[email protected]' };
18+
19+
const vercelClient = createClient(process.env.EDGE_CONFIG);
20+
21+
// start using ld
22+
const client = initLD(sdkKey, vercelClient);
23+
await client.waitForInitialization();
24+
const fallthrough = await client.variation(flagKey, contextFallthrough, false);
25+
const emailRule = await client.variation(flagKey, contextEmailRule, false);
26+
const flagDetail = await client.variationDetail(flagKey, contextEmailRule, false);
27+
const allFlags = await client.allFlagsState(contextEmailRule);
28+
29+
return NextResponse.json({
30+
flagKey: `${flagKey}`,
31+
contextKey: `${contextFallthrough.key}`,
32+
fallthrough,
33+
emailRule,
34+
emailRuleDetail: `${JSON.stringify(flagDetail)}`,
35+
allFlags: `${JSON.stringify(allFlags)}`,
36+
});
37+
}

0 commit comments

Comments
 (0)