Skip to content

Commit

Permalink
Revert "Simplify dev setup (#127)"
Browse files Browse the repository at this point in the history
This reverts commit 28b4f02.
  • Loading branch information
franknoirot committed Apr 2, 2024
1 parent 28b4f02 commit 6db7b3c
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 49 deletions.
2 changes: 0 additions & 2 deletions .env.development

This file was deleted.

5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
VITE_ZOO_DEV_TOKEN="your-token-from-dev.zoo.dev" # A dev API token from dev.zoo.dev
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"
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '/')
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 4 additions & 24 deletions src/routes/+page.server.ts
Original file line number Diff line number Diff line change
@@ -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.'
)
}
}
4 changes: 1 addition & 3 deletions src/routes/api/convert/[output_format]/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/routes/api/get-generation/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
4 changes: 1 addition & 3 deletions src/routes/api/submit-feedback/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 1 addition & 3 deletions src/routes/api/submit-prompt/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 6db7b3c

Please sign in to comment.