From 6db7b3c79db539f2c6736a3c1afceb007b80d40f Mon Sep 17 00:00:00 2001 From: Frank Noirot Date: Tue, 2 Apr 2024 09:37:11 -0400 Subject: [PATCH] Revert "Simplify dev setup (#127)" This reverts commit 28b4f02cceb63f1754e8f7af9fe850b58a2c3014. --- .env.development | 2 -- .env.example | 5 +++- README.md | 21 +++++++++----- package.json | 1 - playwright.config.ts | 2 +- src/hooks.server.ts | 4 +-- src/routes/+layout.server.ts | 2 +- src/routes/+page.server.ts | 28 +++---------------- .../api/convert/[output_format]/+server.ts | 4 +-- src/routes/api/get-generation/+server.ts | 2 +- src/routes/api/submit-feedback/+server.ts | 4 +-- src/routes/api/submit-prompt/+server.ts | 4 +-- 12 files changed, 30 insertions(+), 49 deletions(-) delete mode 100644 .env.development diff --git a/.env.development b/.env.development deleted file mode 100644 index ae628d2..0000000 --- a/.env.development +++ /dev/null @@ -1,2 +0,0 @@ -VITE_API_BASE_URL=https://api.dev.zoo.dev -VITE_SITE_BASE_URL=https://dev.zoo.dev \ No newline at end of file diff --git a/.env.example b/.env.example index 2f7a9c2..6e08406 100644 --- a/.env.example +++ b/.env.example @@ -1 +1,4 @@ -VITE_ZOO_DEV_TOKEN="your-token-from-dev.zoo.dev" # A dev API token from dev.zoo.dev \ No newline at end of file +VITE_API_BASE_URL=https://api.dev.zoo.dev +VITE_SITE_BASE_URL=https://dev.zoo.dev +PLAYWRIGHT_SESSION_COOKIE="your-token-from-dev.zoo.dev" +VITE_TOKEN="your-token-from-dev.zoo.dev" \ No newline at end of file diff --git a/README.md b/README.md index 434355b..dda5cc0 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,10 @@ This repository is an open-source example of how to quickly get up and running w ## Developing 1. Generate a dev API token from https://dev.zoo.dev -2. Set the `VITE_ZOO_DEV_TOKEN` environment variable to the generated dev API token in a new file `./.env` in the root of the repo. See `./.env.example` for an example file. -3. Install [yarn](https://yarnpkg.com/getting-started/install) +2. Set the `VITE_TOKEN` environment variable in `./.env.development` to the generated dev API token +3. Install yarn 4. Install dependencies with `yarn global add vite` and `yarn install` -5. Run the dev server with `yarn dev --open` +5. Run the dev server with `yarn dev -- --open` The full collection of scripts are listed in package.json. @@ -24,11 +24,18 @@ You can preview the production build with `yarn preview`. ## Before submitting a PR -Please run the `yarn prep` to lint, format, type-check and test your code to ensure it's as ready for code review as possible. +Please run the following commands to ensure that your code is as ready for review as it can be: + +```bash +yarn fmt --fix && yarn test +``` ### Running Playwright E2E tests locally -If you've set a `VITE_ZOO_DEV_TOKEN` in `/.env` as described above, you should be able to run the `yarn test` command successfully, which runs `yarn test:integration` and `yarn test:unit` in series. +In order to run our Playwright testing suite locally, please set the `PLAYWRIGHT_SESSION_COOKIE` variable within `.env.development` to a token from a logged in local development session. You can retrieve it by: + +1. logging in to the project locally using the method outlined above +2. opening the Application tab in your browser developer tools +3. copying out the value of the cookie titled `__Secure-next-auth.session-token` with the domain of `localhost` -- We use [Playwright](https://playwright.dev) for end-to-end testing. Try running `yarn test:integration --ui` for a handy visualizer of your tests as they run! -- We use [Vitest](https://vitest.dev) for unit and component testing. +Now you should be able to run the `yarn test:integration` and `yarn test` commands successfully. diff --git a/package.json b/package.json index 022f129..c26a648 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,6 @@ "dev": "vite dev", "build": "vite build", "preview": "vite preview", - "prep": "yarn fmt --fix && yarn check && yarn lint && yarn test", "test": "npm run test:integration && npm run test:unit run", "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch", diff --git a/playwright.config.ts b/playwright.config.ts index 46f683d..34d9b41 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -14,7 +14,7 @@ const config: PlaywrightTestConfig = { cookies: [ { name: AUTH_COOKIE_NAME, - value: process.env.VITE_ZOO_DEV_TOKEN ?? '', + value: process.env.PLAYWRIGHT_SESSION_COOKIE ?? '', domain: 'localhost', path: '/', expires: expiration.getTime() / 1000, diff --git a/src/hooks.server.ts b/src/hooks.server.ts index 138acab..3cf9a35 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -11,7 +11,7 @@ export const handle = async ({ event, resolve }) => { const mock = event.request.headers.get(PLAYWRIGHT_MOCKING_HEADER) const token = import.meta.env.PROD ? event.cookies.get(AUTH_COOKIE_NAME) - : import.meta.env.VITE_ZOO_DEV_TOKEN + : import.meta.env.VITE_TOKEN if (!token && !unProtectedRoutes.includes(event.url.pathname)) { throw redirect(303, '/') @@ -32,7 +32,7 @@ export const handle = async ({ event, resolve }) => { throw error(500, e) }) - if (!currentUser || 'message' in currentUser) { + if (!currentUser) { event.locals.user = undefined if (!unProtectedRoutes.includes(event.url.pathname)) throw redirect(303, '/') } else { diff --git a/src/routes/+layout.server.ts b/src/routes/+layout.server.ts index 31bf1a5..44fcef9 100644 --- a/src/routes/+layout.server.ts +++ b/src/routes/+layout.server.ts @@ -5,7 +5,7 @@ export const load = async ({ locals, cookies }) => { const token = import.meta.env.MODE === 'production' ? cookies.get(AUTH_COOKIE_NAME) - : import.meta.env.VITE_ZOO_DEV_TOKEN + : import.meta.env.VITE_TOKEN return { user: !locals.user || 'error_code' in locals.user ? undefined : locals.user, diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts index 5253874..4c231dc 100644 --- a/src/routes/+page.server.ts +++ b/src/routes/+page.server.ts @@ -1,30 +1,10 @@ import { AUTH_COOKIE_NAME } from '$lib/cookies.js' -import { error, redirect } from '@sveltejs/kit' -import type { Models } from '@kittycad/lib' +import { redirect } from '@sveltejs/kit' -export const load = async ({ cookies, url, fetch }) => { - const token = import.meta.env.PROD - ? cookies.get(AUTH_COOKIE_NAME) - : import.meta.env.VITE_ZOO_DEV_TOKEN +export const load = async ({ cookies, url }) => { + const token = import.meta.env.PROD ? cookies.get(AUTH_COOKIE_NAME) : import.meta.env.VITE_TOKEN - const currentUser = await fetch(import.meta.env.VITE_API_BASE_URL + '/user', { - method: 'GET', - headers: { - 'Content-Type': 'application/json', - Authorization: `Bearer ${token}` - } - }) - .then(async (res) => (await res.json()) as Models['User_type'] | Models['Error_type']) - .catch((e) => { - throw error(500, e) - }) - - // Redirect to the dashboard if the user is already logged in - if (currentUser && 'email' in currentUser) { + if (token) { throw redirect(302, '/dashboard' + (url.search || '')) - } else if (import.meta.env.DEV) { - console.warn( - 'You might be using an invalid or expired token for your VITE_ZOO_DEV_TOKEN environment variable. Please check your .env file.' - ) } } diff --git a/src/routes/api/convert/[output_format]/+server.ts b/src/routes/api/convert/[output_format]/+server.ts index ebbd64c..ba3192e 100644 --- a/src/routes/api/convert/[output_format]/+server.ts +++ b/src/routes/api/convert/[output_format]/+server.ts @@ -9,9 +9,7 @@ export type ConvertResponse = Models['FileConversion_type'] & { } export const POST: RequestHandler = async ({ cookies, fetch, request, params }) => { - const token = import.meta.env.PROD - ? cookies.get(AUTH_COOKIE_NAME) - : import.meta.env.VITE_ZOO_DEV_TOKEN + const token = import.meta.env.PROD ? cookies.get(AUTH_COOKIE_NAME) : import.meta.env.VITE_TOKEN if (!token) throw error(401, 'You must be logged in to use this API.') const body = await request.text() diff --git a/src/routes/api/get-generation/+server.ts b/src/routes/api/get-generation/+server.ts index adeaf51..f167ade 100644 --- a/src/routes/api/get-generation/+server.ts +++ b/src/routes/api/get-generation/+server.ts @@ -11,7 +11,7 @@ export const POST: RequestHandler = async ({ cookies, fetch, request }) => { const token = import.meta.env.MODE === 'production' ? cookies.get(AUTH_COOKIE_NAME) - : import.meta.env.VITE_ZOO_DEV_TOKEN + : import.meta.env.VITE_TOKEN const body = await request.json() diff --git a/src/routes/api/submit-feedback/+server.ts b/src/routes/api/submit-feedback/+server.ts index 013464d..9b51ccb 100644 --- a/src/routes/api/submit-feedback/+server.ts +++ b/src/routes/api/submit-feedback/+server.ts @@ -8,9 +8,7 @@ export type LoadResponse = { } export const POST: RequestHandler = async ({ cookies, fetch, request }) => { - const token = import.meta.env.PROD - ? cookies.get(AUTH_COOKIE_NAME) - : import.meta.env.VITE_ZOO_DEV_TOKEN + const token = import.meta.env.PROD ? cookies.get(AUTH_COOKIE_NAME) : import.meta.env.VITE_TOKEN const body = await request.json() if (!(body?.id && body?.feedback)) diff --git a/src/routes/api/submit-prompt/+server.ts b/src/routes/api/submit-prompt/+server.ts index 5723470..1d3e401 100644 --- a/src/routes/api/submit-prompt/+server.ts +++ b/src/routes/api/submit-prompt/+server.ts @@ -9,9 +9,7 @@ export type PromptLoadResponse = { } export const POST: RequestHandler = async ({ cookies, fetch, request }) => { - const token = import.meta.env.PROD - ? cookies.get(AUTH_COOKIE_NAME) - : import.meta.env.VITE_ZOO_DEV_TOKEN + const token = import.meta.env.PROD ? cookies.get(AUTH_COOKIE_NAME) : import.meta.env.VITE_TOKEN if (!token) throw error(401, 'You must be logged in to use this API.') const body = await request.json()