-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(repo): Add integration test for express + vite
- Loading branch information
Showing
15 changed files
with
282 additions
and
5 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,17 @@ | ||
import { constants } from '../constants'; | ||
import { applicationConfig } from '../models/applicationConfig'; | ||
import { templates } from '../templates'; | ||
|
||
const vite = applicationConfig() | ||
.setName('express-vite') | ||
.useTemplate(templates['express-vite']) | ||
.setEnvFormatter('public', key => `VITE_${key}`) | ||
.addScript('setup', 'npm i --prefer-offline') | ||
.addScript('dev', 'npm run dev') | ||
.addScript('build', 'npm run build') | ||
.addScript('serve', 'npm run start') | ||
.addDependency('@clerk/clerk-sdk-node', constants.E2E_CLERK_VERSION); | ||
|
||
export const express = { | ||
vite, | ||
} as const; |
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,24 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
pnpm-debug.log* | ||
lerna-debug.log* | ||
|
||
node_modules | ||
dist | ||
dist-ssr | ||
*.local | ||
|
||
# Editor directories and files | ||
.vscode/* | ||
!.vscode/extensions.json | ||
.idea | ||
.DS_Store | ||
*.suo | ||
*.ntvs* | ||
*.njsproj | ||
*.sln | ||
*.sw? |
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,25 @@ | ||
{ | ||
"name": "express-vite", | ||
"private": true, | ||
"version": "0.0.0", | ||
"scripts": { | ||
"dev": "PORT=$PORT ts-node src/server/main.ts", | ||
"start": "PORT=$PORT ts-node src/server/main.ts", | ||
"build": "vite build", | ||
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0", | ||
"preview": "vite preview --port $PORT --no-open" | ||
}, | ||
"dependencies": { | ||
"ejs": "^3.1.6", | ||
"express": "^4.18.2", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^4.9.3", | ||
"vite-express": "*" | ||
}, | ||
"devDependencies": { | ||
"@types/express": "^4.17.15", | ||
"@types/node": "^18.11.18", | ||
"nodemon": "^2.0.20", | ||
"vite": "^4.0.4" | ||
} | ||
} |
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,60 @@ | ||
// Should be at the top of the file - used to load clerk secret key | ||
import * as dotenv from 'dotenv'; | ||
dotenv.config(); | ||
|
||
import { clerkClient } from '@clerk/clerk-sdk-node'; | ||
import express from 'express'; | ||
import ViteExpress from 'vite-express'; | ||
|
||
const app = express(); | ||
|
||
app.set('view engine', 'ejs'); | ||
app.set('views', 'src/views'); | ||
|
||
app.get('/api/protected', [clerkClient.expressRequireAuth()], (_req: any, res: any) => { | ||
res.send('Protected API response').end(); | ||
}); | ||
|
||
app.get('/sign-in', (_req: any, res: any) => { | ||
return res.render('sign-in.ejs', { | ||
publishableKey: process.env.VITE_CLERK_PUBLISHABLE_KEY, | ||
signInUrl: process.env.CLERK_SIGN_IN_URL, | ||
}); | ||
}); | ||
|
||
app.get('/', (_req: any, res: any) => { | ||
return res.render('index.ejs', { | ||
publishableKey: process.env.VITE_CLERK_PUBLISHABLE_KEY, | ||
signInUrl: process.env.CLERK_SIGN_IN_URL, | ||
}); | ||
}); | ||
|
||
app.get('/sign-up', (_req: any, res: any) => { | ||
return res.render('sign-up.ejs', { | ||
publishableKey: process.env.VITE_CLERK_PUBLISHABLE_KEY, | ||
signUpUrl: process.env.CLERK_SIGN_UP_URL, | ||
}); | ||
}); | ||
|
||
app.get('/protected', (_req: any, res: any) => { | ||
return res.render('protected.ejs', { | ||
publishableKey: process.env.VITE_CLERK_PUBLISHABLE_KEY, | ||
signInUrl: process.env.CLERK_SIGN_IN_URL, | ||
signUpUrl: process.env.CLERK_SIGN_UP_URL, | ||
}); | ||
}); | ||
|
||
// Handle authentication error, otherwise application will crash | ||
// @ts-ignore | ||
app.use((err, req, res, next) => { | ||
if (err) { | ||
console.error(err); | ||
res.status(401).end(); | ||
return; | ||
} | ||
|
||
return next(); | ||
}); | ||
|
||
const port = parseInt(process.env.PORT as string) || 3002; | ||
ViteExpress.listen(app, port, () => console.log(`Server is listening on port ${port}...`)); |
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,32 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<script | ||
data-clerk-publishable-key="<%= publishableKey %>" | ||
onLoad="startClerk()" | ||
crossorigin="anonymous" | ||
async="" | ||
src="https://clerk.clerk.com/npm/@clerk/clerk-js@4/dist/clerk.browser.js" | ||
></script> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
<div id="user-state"></div> | ||
|
||
<script> | ||
window.startClerk = async () => { | ||
await Clerk.load({ signInUrl: '<%= signInUrl %>' }); | ||
const appEl = document.querySelector('#app'); | ||
const controlStateEl = document.querySelector('#user-state'); | ||
if (Clerk.user) { | ||
Clerk.mountUserButton(appEl); | ||
controlStateEl.innerHTML = 'SignedIn'; | ||
} else { | ||
controlStateEl.innerHTML = 'SignedOut'; | ||
} | ||
}; | ||
</script> | ||
</body> | ||
</html> |
32 changes: 32 additions & 0 deletions
32
integration/templates/express-vite/src/views/protected.ejs
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,32 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<script | ||
data-clerk-publishable-key="<%= publishableKey %>" | ||
onLoad="startClerk()" | ||
crossorigin="anonymous" | ||
async="" | ||
src="https://clerk.clerk.com/npm/@clerk/clerk-js@4/dist/clerk.browser.js" | ||
></script> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
<div id="user-state"></div> | ||
|
||
<script> | ||
window.startClerk = async () => { | ||
await Clerk.load({ signInUrl: '<%= signInUrl %>' }); | ||
if (Clerk.user) { | ||
const apiResponse = await fetch('/api/protected').then(res => res.text()); | ||
const div = document.createElement('div'); | ||
div.setAttribute('data-test-id', 'protected-api-response'); | ||
div.innerText = apiResponse; | ||
document.body.appendChild(div); | ||
} | ||
}; | ||
</script> | ||
</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,24 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<script | ||
data-clerk-publishable-key="<%= publishableKey %>" | ||
onLoad="startClerk()" | ||
crossorigin="anonymous" | ||
async="" | ||
src="https://clerk.clerk.com/npm/@clerk/clerk-js@4/dist/clerk.browser.js" | ||
></script> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
<div id="user-state"></div> | ||
|
||
<script> | ||
window.startClerk = async () => { | ||
await Clerk.load({ signInUrl: '<%= signInUrl %>' }); | ||
Clerk.mountSignIn(document.querySelector('#app')); | ||
}; | ||
</script> | ||
</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,24 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<script | ||
data-clerk-publishable-key="<%= publishableKey %>" | ||
onLoad="startClerk()" | ||
crossorigin="anonymous" | ||
async="" | ||
src="https://clerk.clerk.com/npm/@clerk/clerk-js@4/dist/clerk.browser.js" | ||
></script> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
<div id="user-state"></div> | ||
|
||
<script> | ||
window.startClerk = async () => { | ||
await Clerk.load({ signInUrl: '<%= signInUrl %>' }); | ||
Clerk.mountSignUp(document.querySelector('#app')); | ||
}; | ||
</script> | ||
</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,19 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"useDefineForClassFields": true, | ||
"lib": ["DOM", "DOM.Iterable", "ESNext"], | ||
"allowJs": false, | ||
"skipLibCheck": true, | ||
"esModuleInterop": true, | ||
"allowSyntheticDefaultImports": true, | ||
"strict": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"module": "CommonJS", | ||
"moduleResolution": "Node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"noEmit": true | ||
}, | ||
"include": ["src"] | ||
} |
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,4 @@ | ||
import { defineConfig } from 'vite'; | ||
|
||
// https://vitejs.dev/config/ | ||
export default defineConfig({}); |
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