1
0
mirror of synced 2026-02-03 18:01:02 -05:00

Compare commits

...

72 Commits

Author SHA1 Message Date
beerose
30bb474abb Update pnpm-lock.yaml 2022-06-23 13:32:30 +02:00
github-actions[bot]
135b30efde Version Packages (alpha) (#3456)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-23 13:31:59 +02:00
Aleksandra
527e48ac3e Fix running bin commands with Blitz CLI (#3455) 2022-06-23 13:29:22 +02:00
beerose
b905270875 Update pnpm-lock.yaml 2022-06-23 12:56:21 +02:00
github-actions[bot]
96ea5291e4 Version Packages (alpha) (#3454)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-23 12:54:23 +02:00
Aleksandra
9c2e7d372c Import useRouter form next/router in useParams.tsx (#3453) 2022-06-23 12:47:01 +02:00
beerose
493d505b24 Update pnpm-lock 2022-06-22 12:12:57 +02:00
github-actions[bot]
09da992bef Version Packages (alpha) (#3450)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-22 12:11:54 +02:00
Dillon Raphael
bbac7906e8 fix codemod for wrapping \_app arrow function and nested pages directory (#3443)
Co-authored-by: Aleksandra <alexsandra.sikora@gmail.com>
2022-06-22 10:55:35 +02:00
Dillon Raphael
21ca3a9b02 pnpmlock 2022-06-21 10:27:09 -04:00
github-actions[bot]
32274803d9 Version Packages (alpha) (#3449)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-21 10:25:53 -04:00
Dillon Raphael
9ded8dacba Export useParam & useParams from @blitzjs/next (#3448) 2022-06-21 14:45:29 +02:00
beerose
80ffbeaa4c Update pnpm-lock.yaml 2022-06-19 18:20:49 +02:00
github-actions[bot]
6bde1b07da Version Packages (alpha) (#3445)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-19 18:18:57 +02:00
Datner
b918055bf3 Add aliases for Blitz CLI commands (#3410)
Co-authored-by: Aleksandra <alexsandra.sikora@gmail.com>
2022-06-19 18:13:43 +02:00
Aleksandra
f9a2971f05 Improve typings in blitz cli package (#3444) 2022-06-19 15:48:27 +02:00
Dillon Raphael
72b08f2269 pnpmlock 2022-06-13 15:45:47 -04:00
github-actions[bot]
2124a4d0c5 Version Packages (alpha) (#3440)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-13 15:41:12 -04:00
Dillon Raphael
8aee25c58a changeset 2022-06-13 15:37:27 -04:00
Dillon Raphael
f1003faf94 update generator template (#3439) 2022-06-13 15:35:38 -04:00
Andreas Asprou
50468a3bb0 Fix duplicate react-query clients (#3431)
* Fix duplicate react-query clients

* fix tests

* add codemod & remove queryClient export from generator templates

Co-authored-by: Dillon Raphael <dillon@creatorsneverdie.com>
2022-06-13 11:07:41 -04:00
Dillon Raphael
891d91bf4d pnpmlock 2022-06-10 16:19:50 -04:00
github-actions[bot]
f96c953457 Version Packages (alpha) (#3430)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-10 16:18:48 -04:00
Dillon Raphael
a80d2a8f77 Rename Blitz server plugin type from Middleware to RequestMiddleware (#3428)
* rename to requestMiddleware

* update plugins

* merge main

* changeset
2022-06-10 16:14:21 -04:00
beerose
b336ad05f4 Update pnpm-lock.yaml 2022-06-10 11:32:17 -07:00
github-actions[bot]
39ca0ef8bf Version Packages (alpha) (#3429)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-10 11:31:27 -07:00
Aleksandra
4cad9cca25 Add queryClient to RPC plugin exports (#3424) 2022-06-10 10:18:50 -07:00
Dillon Raphael
b6fc940bf2 pnpmlock 2022-06-10 11:21:33 -04:00
github-actions[bot]
a946dd5889 Version Packages (alpha) (#3427)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-10 11:20:25 -04:00
Dillon Raphael
e3750b049d Codemod fixes (#3420)
* change BlitzApiRequest to NextApiRequest & update import map & fail catch for custom server

* fix page consolidation when there are sub directories

* Update prisma db export/import

* Update db imports in templates

* update root types file

* update error messages + merge main + changeset

Co-authored-by: beerose <alexsandra.sikora@gmail.com>
2022-06-10 11:16:18 -04:00
Dillon Raphael
fb01cc7788 pnpmlock 2022-06-10 10:34:59 -04:00
github-actions[bot]
ac8c412da2 Version Packages (alpha) (#3419)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-10 10:31:19 -04:00
David
dfd2408e95 Added resolverBasePath option to rpc loaders (#3380)
Co-authored-by: beerose <alexsandra.sikora@gmail.com>
2022-06-08 14:50:00 -07:00
Dillon Raphael
9741287050 pnpmlock 2022-06-07 18:28:53 -04:00
github-actions[bot]
0e9c81abdc Version Packages (alpha) (#3409)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-07 18:28:29 -04:00
Dillon Raphael
9e05d6e155 Filter file extensions in codemod (#3408)
* add allowedExt to getAllFiles for codemod

* pnpmlock

* codemod
2022-06-07 18:24:10 -04:00
beerose
17f70e65ef Update pnpm-lock.yaml 2022-06-07 13:21:28 -07:00
github-actions[bot]
0ddc5a8169 Version Packages (alpha) (#3407)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-07 13:20:44 -07:00
Aleksandra
e6fb09d494 Use dist/templates for rpc template (#3406) 2022-06-07 13:02:13 -07:00
beerose
d846fc6be9 Update pnpm-lock.yaml 2022-06-07 11:47:05 -07:00
github-actions[bot]
f5e80e3835 Version Packages (alpha) (#3405)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-07 11:46:31 -07:00
Aleksandra
17ce29e5e4 Update rpc plugin setup in templates (#3404) 2022-06-07 11:33:06 -07:00
Aleksandra
46d9f81adf Update templates directory for codemod (#3402) 2022-06-07 11:04:34 -07:00
Dillon Raphael
994cfc6292 pnpmlock 2022-06-07 12:09:09 -04:00
github-actions[bot]
7811748526 Version Packages (alpha) (#3401)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-07 12:08:45 -04:00
Dillon Raphael
ce45368334 Add codemods to upgrade from framework to toolkit (#3353)
* init migrate cli command

* make sure migrate command can only be run in legacy blitz directory

* extract codemods into its own package/cli

* removed unused dependencies

* renamed codemods bin directory

* update next.config file to use withBlitz

* codemods update package.json step

* codemod for blitz server & client setup, as well as the blitz rpc route in pages dir

* fix ignore previous step if statement to check for less than instead of equal

* move files from app/pages to root pages directory

* remove blitz babel plugin & update imports inside app dir

* consolidate pages from app dir to root pages dir

* move all api directories to the root pages dir

* use generator templates for blitz rpc route & server/client setup files

* update custom server file

* make custom server step work with require statement & import statements

* useRouterQuery to useRouter().query

* pkg dependecy & import map updates

* Change import map

* Add BlitzLayout to import map

* import withBlitz and wrap App function in _app

* modify _document to use next.js imports

* fix default import for next modules & add useParam to source map

* gssp/gsp

* api routes

* dont run api wrap on rpc/[[...blitz]] & error on usage of local middleware

* DRY cleanup

* update codemod steps

* add ignore extension to getAllFiles

* add more imports to source map

* check for invokeWithMiddleware

* add error counter to middleware checker

* rename codemod & fix silly typo error

* update bin file & change all invokeWithMiddleware to invoke

* add logging from blitz

* manypkg fix

* add codemod test

* lockfile

* fix test

* show errors for invokeWithMiddleware

* update invokeWithMiddleware error message

* line break in new app generator before your new blitz app is ready

* Apply suggestions from code review

* Add changeset

* changeset

* pnpmlock

Co-authored-by: beerose <alexsandra.sikora@gmail.com>
2022-06-07 12:03:37 -04:00
Dillon Raphael
4e9c1f60b6 pnpm lock 2022-06-07 11:36:45 -04:00
github-actions[bot]
508682c8f8 Version Packages (alpha) (#3400)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-07 11:35:42 -04:00
Dillon Raphael
962eb58af6 Migrate printEnvInfo to CLI (#3394)
* init printEnvInfo function

* changeset
2022-06-07 11:31:56 -04:00
Dillon Raphael
17669b3af8 pnpmlock 2022-06-07 11:06:31 -04:00
github-actions[bot]
ec6299c36a Version Packages (alpha) (#3399)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-07 11:05:15 -04:00
Dillon Raphael
6ac2d3412a Update generator template npmrc & next versions (#3398)
* add strict-peer-dependencies=false
side-effects-cache=false to npmrc generator template

* add comment to generated npmrc file

* update versions of next for full and minimal templates

* pinned next version to 12.1.6 in generator templates

* add next back to root package

* fix lock file

* add changeset

Co-authored-by: Aleksandra <alexsandra.sikora@gmail.com>
2022-06-07 10:59:28 -04:00
Dillon Raphael
85f9959d1f update pnpm lock file 2022-06-07 10:23:51 -04:00
github-actions[bot]
354f0440d6 Version Packages (alpha) (#3392)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-07 10:21:41 -04:00
Dillon Raphael
ac365a0656 No suspense test and Trailing slash test (#3390)
* no suspense test and trailing slash test

* use playwright for integration tests

* remove seed from server mode test in auth integration

* change pkg-dir version for blitz cli

* use pkg-dir 5.0.0

* clean up

* move seed location in auth test

* explict timeout for auth test

Co-authored-by: beerose <alexsandra.sikora@gmail.com>
2022-06-01 20:54:29 -04:00
Aleksandra
0729291099 Add invokeWithCtx (#3391) 2022-05-31 16:06:45 -04:00
Dillon Raphael
9cf924ee86 Regenerate route manifest on page changes (Working build) (#3385) 2022-05-30 10:09:20 -04:00
Dillon Raphael
b545a38b87 pnpmlock 2022-05-24 15:11:18 -04:00
github-actions[bot]
1463a20471 Version Packages (alpha) (#3378)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-05-24 15:09:03 -04:00
Dillon Raphael
0e762fb557 export BlitzPage & BlitzLayout types from @blitzjs/next (#3376)
* Export types

* Add changeset

* export AppProps

Co-authored-by: Aleksandra <alexsandra.sikora@gmail.com>
2022-05-24 21:02:17 +02:00
github-actions[bot]
6fe860512c Version Packages (alpha) (#3377)
* Version Packages (alpha)

* empty

* pnpm lock

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Dillon Raphael <dillon@creatorsneverdie.com>
2022-05-24 14:48:47 -04:00
Dillon Raphael
adfe8ff919 run build after pnpm i --frozen-lockfile so lint can work 2022-05-24 14:22:02 -04:00
Dillon Raphael
980869fbc8 add npm auth token to npmrc file in action step 2022-05-24 14:15:50 -04:00
Dillon Raphael
00f6d5576a merge main branch 2022-05-24 13:52:40 -04:00
Dillon Raphael
1945d85db2 Set strict peer dependencies to false 2022-05-24 13:47:56 -04:00
Aleksandra
b0c21b0706 Move blitz config to next.config.js (#3370) 2022-05-19 15:34:37 +02:00
Aleksandra
931156c352 Add support for prefetchInfiniteQuery (#3369) 2022-05-17 16:29:12 +02:00
Aleksandra
1436e76180 Export zod-utils from blitz core (#3368) 2022-05-17 15:49:42 +02:00
Aleksandra
c3bb5cd95b Add passport adapter (#3365) 2022-05-16 18:02:44 +02:00
beerose
8b08fe4e38 Update release workflow 2022-05-12 18:12:46 +02:00
beerose
604dc3b345 Update pnpm-lock after manually updating packages versions 2022-05-12 17:58:21 +02:00
beerose
20fb3b9427 Update packages versions manually 2022-05-12 17:52:00 +02:00
184 changed files with 7921 additions and 1965 deletions

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/rpc": patch
---
Add queryClient to RPC Plugin exports

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/rpc": patch
---
Add invokeWithCtx function

View File

@@ -0,0 +1,6 @@
---
"@blitzjs/codemod": patch
"@blitzjs/generator": patch
---
codemod fixes

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/next": patch
---
Use `useRouter` from next/router in useParams function

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/generator": patch
---
updated nextjs version in generator & npmrc file

View File

@@ -0,0 +1,5 @@
---
"blitz": patch
---
Export Zod utils from blitz core package

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/next": patch
---
export BlitzPage & BlitzLayout types from @blitzjs/next

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/codemod": patch
---
fix codemod for wrapping \_app arrow function & fix codemod for nested pages directory

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/codemod": patch
---
Update queryClient import in codemod

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/generator": patch
---
Update codemod and template with a new queryClient import location

View File

@@ -0,0 +1,5 @@
---
"blitz": patch
---
detailed print env info

View File

@@ -0,0 +1,7 @@
---
"blitz": patch
"@blitzjs/auth": patch
"@blitzjs/next": patch
---
rename middleware type for blitz server plugin

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/next": patch
---
useParam & useParams functions now accessible from @blitzjs/next

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/codemod": patch
---
Fix templates source in RPC codemod step

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/codemod": patch
---
Add codemod to upgrade from legacy framework to the Blitz Toolkit

View File

@@ -12,39 +12,69 @@
"@blitzjs/rpc": "2.0.0-alpha.0",
"@blitzjs/config": "0.0.0",
"@blitzjs/generator": "2.0.0-alpha.0",
"@blitzjs/codemod": "2.0.0-alpha.0",
"template": "0.0.0",
"toolkit-app": "1.0.0",
"test-qm": "0.0.0"
"test-qm": "0.0.0",
"test-no-suspense": "0.0.0",
"test-trailing-slash": "0.0.0"
},
"changesets": [
"big-phones-bow",
"breezy-cameras-double",
"bright-mangos-run",
"cool-doors-invent",
"dirty-monkeys-greet",
"eleven-humans-sort",
"empty-berries-rule",
"empty-turkeys-wave",
"fair-wombats-sneeze",
"famous-kings-explain",
"fast-trainers-kneel",
"flat-bees-approve",
"four-meals-fry",
"fuzzy-jars-admire",
"gentle-dogs-reply",
"great-months-train",
"green-papayas-do",
"healthy-rice-shout",
"hot-drinks-approve",
"lovely-colts-share",
"lucky-cows-try",
"modern-cameras-pull",
"moody-squids-cheer",
"nervous-beds-travel",
"nice-starfishes-live",
"nine-onions-admire",
"ninety-pets-heal",
"olive-bees-buy",
"olive-feet-rhyme",
"plenty-bottles-swim",
"poor-peas-lick",
"poor-penguins-look",
"poor-shrimps-think",
"purple-singers-greet",
"quiet-feet-travel",
"rich-chairs-invent",
"sharp-falcons-begin",
"shy-olives-hang",
"silent-colts-reply",
"small-socks-confess",
"stupid-walls-sell",
"swift-drinks-dress",
"tame-keys-reply",
"tasty-news-collect",
"ten-rivers-burn",
"tender-pianos-check",
"thick-parrots-float",
"thirty-countries-build",
"twenty-beans-pump",
"two-kiwis-help",
"two-tigers-type",
"unlucky-papayas-sleep",
"violet-bags-leave",
"violet-lions-help",
"weak-suns-shave",
"wicked-ghosts-cough",
"wise-frogs-give"
]

View File

@@ -0,0 +1,7 @@
---
"@blitzjs/rpc": patch
"@blitzjs/codemod": patch
"@blitzjs/generator": patch
---
getQueryClient function & queryClient codemod updates & shared plugin config

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/next": patch
---
Rename prefetchBlitzQuery to prefetchQuery, add prefetchInfiniteQuery

View File

@@ -0,0 +1,6 @@
---
"@blitzjs/rpc": patch
"@blitzjs/generator": patch
---
Update RPC plugin setup in templates

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/auth": patch
---
Add passport adapter to @blitzjs/auth

View File

@@ -0,0 +1,5 @@
---
"blitz": patch
---
Add aliases for Blitz CLI commands

View File

@@ -0,0 +1,6 @@
---
"blitz": patch
"@blitzjs/codemod": patch
---
init codemod generator

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/codemod": patch
---
allow extension catch in getAllFiles codemod util

View File

@@ -0,0 +1,5 @@
---
"blitz": patch
---
Fix running bin commands with Blitz CLI

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/codemod": patch
---
Update templates directory for codemod

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/rpc": patch
---
Add resolverBasePath to Blitz config to change the way rpc routes are generated

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/next": patch
---
Move blitz config to next.config.js

View File

@@ -20,18 +20,25 @@ jobs:
with:
node-version: 16.x
- name: Creating .npmrc
run: |
cat << EOF > "$HOME/.npmrc"
//registry.npmjs.org/:_authToken=$NPM_TOKEN
EOF
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Pre-publish
uses: pnpm/action-setup@646cdf48217256a3d0b80361c5a50727664284f2
with:
version: 6.32.6
- run: pnpm install --frozen-lockfile
- run: pnpm changeset version
- run: pnpm build
- name: Create Release Pull Request
uses: changesets/action@v1
with:
publish: pnpm changeset publish
publish: pnpm release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

2
.npmrc
View File

@@ -1,7 +1,9 @@
save-exact=true
strict-peer-dependencies=false
public-hoist-pattern[]=secure-password
public-hoist-pattern[]=*types*
public-hoist-pattern[]=*eslint*
public-hoist-pattern[]=@prettier/plugin-*
public-hoist-pattern[]=*prettier-plugin-*
strict-peer-dependencies=false

View File

@@ -7,12 +7,6 @@ export const { withBlitz } = setupBlitzClient({
AuthClientPlugin({
cookiePrefix: "web-cookie-prefix",
}),
BlitzRpcPlugin({
reactQueryOptions: {
queries: {
staleTime: 7000,
},
},
}),
BlitzRpcPlugin({}),
],
})

View File

@@ -3,7 +3,7 @@
"version": "1.0.1-alpha.16",
"scripts": {
"start:dev": "pnpm run prisma:start && next dev",
"buildapp": "pnpm i && pnpm prisma generate && next build",
"buildapp": "NODE_ENV=production pnpm blitz codegen && pnpm prisma generate && next build",
"start": "next start",
"lint": "next lint",
"prisma:start": "prisma generate && prisma migrate deploy",
@@ -29,7 +29,7 @@
"@blitzjs/rpc": "workspace:*",
"@hookform/resolvers": "2.8.8",
"@prisma/client": "3.9.0",
"blitz": "workspace:2.0.0-alpha.23",
"blitz": "workspace:2.0.0-alpha.43",
"next": "12.1.6-canary.17",
"prisma": "3.9.0",
"react": "18.0.0",

View File

@@ -1,6 +1,5 @@
import { ErrorFallbackProps, ErrorComponent, ErrorBoundary } from "@blitzjs/next"
import { ErrorFallbackProps, ErrorComponent, ErrorBoundary, AppProps } from "@blitzjs/next"
import { AuthenticationError, AuthorizationError } from "blitz"
import type { AppProps } from "next/app"
import React from "react"
import { withBlitz } from "app/blitz-client"

View File

@@ -1,6 +1,6 @@
import {setupBlitzServer} from "@blitzjs/next"
import {AuthServerPlugin, PrismaStorage} from "@blitzjs/auth"
import {prisma as db} from "../prisma/index"
import db from "db"
import {simpleRolesIsAuthorized} from "@blitzjs/auth"
const {gSSP, gSP, api} = setupBlitzServer({

View File

@@ -1,6 +1,5 @@
import {Ctx} from "blitz"
import {prisma} from "../../prisma"
import {User} from "prisma"
import db, {User} from "db"
export default async function createUser(
input: {name: string; email: string},
@@ -8,7 +7,7 @@ export default async function createUser(
): Promise<User> {
ctx.session.$authorize()
const user = await prisma.user.create({data: {name: input.name, email: input.email}})
const user = await db.user.create({data: {name: input.name, email: input.email}})
return user
}

View File

@@ -0,0 +1,27 @@
import {resolver} from "@blitzjs/rpc"
import {paginate} from "blitz"
import db, {Prisma} from "db"
interface GetUsersInput
extends Pick<Prisma.UserFindManyArgs, "where" | "orderBy" | "skip" | "take"> {}
export default resolver.pipe(async ({where, orderBy, skip = 0, take = 100}: GetUsersInput) => {
const {
items: users,
hasMore,
nextPage,
count,
} = await paginate({
skip,
take,
count: () => db.user.count({where}),
query: (paginateArgs) => db.user.findMany({...paginateArgs, where, orderBy}),
})
return {
users,
nextPage,
hasMore,
count,
}
})

View File

@@ -1,11 +1,10 @@
import {Ctx} from "blitz"
import {prisma} from "../../prisma"
import {User} from "prisma"
import db, {User} from "db"
export default async function getUsers(_input: {}, ctx: Ctx): Promise<User[]> {
ctx.session.$authorize()
const users = await prisma.user.findMany()
const users = await db.user.findMany()
return users
}

View File

@@ -0,0 +1,8 @@
import {resolver} from "@blitzjs/rpc"
import db from "db"
export default resolver.pipe(resolver.authorize(), async () => {
const users = await db.user.findMany()
return users
})

8
apps/web/db/index.ts Normal file
View File

@@ -0,0 +1,8 @@
import {enhancePrisma} from "blitz"
import {PrismaClient} from "@prisma/client"
const EnhancedPrisma = enhancePrisma(PrismaClient)
export * from "@prisma/client"
const prisma = new EnhancedPrisma()
export default prisma

View File

@@ -6,5 +6,11 @@ const {withBlitz} = require("@blitzjs/next")
module.exports = withBlitz(
withBundleAnalyzer({
reactStrictMode: true,
blitz: {
customServer: {
hotReload: false,
},
resolverBasePath: "root",
},
}),
)

View File

@@ -12,6 +12,9 @@
"prisma:studio": "prisma studio",
"test": "jest"
},
"prisma": {
"schema": "./db/schema.prisma"
},
"dependencies": {
"@blitzjs/auth": "workspace:*",
"@blitzjs/config": "workspace:*",
@@ -19,9 +22,12 @@
"@blitzjs/rpc": "workspace:*",
"@prisma/client": "3.9.0",
"@types/jest": "27.4.1",
"@types/passport-twitter": "1.0.37",
"blitz": "workspace:*",
"jest": "27.5.1",
"next": "12.1.6-canary.17",
"passport-mock-strategy": "2.0.0",
"passport-twitter": "1.0.4",
"prisma": "3.9.0",
"react": "18.0.0",
"react-dom": "18.0.0",

View File

@@ -1,6 +1,5 @@
import {ErrorFallbackProps, ErrorComponent, ErrorBoundary} from "@blitzjs/next"
import {ErrorFallbackProps, ErrorComponent, ErrorBoundary, AppProps} from "@blitzjs/next"
import {AuthenticationError, AuthorizationError} from "blitz"
import type {AppProps} from "next/app"
import React from "react"
import {withBlitz} from "app/blitz-client"

View File

@@ -0,0 +1,44 @@
import {passportAuth} from "@blitzjs/auth"
import {api} from "app/blitz-server"
import db from "db"
import {Strategy as TwitterStrategy} from "passport-twitter"
export default api(
passportAuth({
successRedirectUrl: "/",
errorRedirectUrl: "/",
strategies: [
{
strategy: new TwitterStrategy(
{
consumerKey: process.env.TWITTER_CONSUMER_KEY as string,
consumerSecret: process.env.TWITTER_CONSUMER_SECRET as string,
accessTokenURL: "https://api.twitter.com/oauth/access_token",
callbackURL: "http://127.0.0.1:3000/api/auth/twitter/callback",
includeEmail: true,
},
async function (_token, _tokenSecret, profile, done) {
const email = profile.emails?.[0]?.value ?? "blitz@test.com"
const user = await db.user.upsert({
where: {email},
create: {
email,
name: profile.displayName,
},
update: {email},
})
const publicData = {
userId: user.id,
roles: [user.role],
source: "twitter",
}
done(undefined, {publicData})
},
),
},
],
}),
)

View File

@@ -1,14 +1,13 @@
import {api} from "app/blitz-server"
import {SessionContext} from "@blitzjs/auth"
import {prisma} from "../../prisma/index"
import db from "db"
export default api(async (_req, res, ctx) => {
const blitzContext = ctx
const publicData = blitzContext.session.$publicData
const sessions = await prisma.session.findMany({})
const sessionsCount = await prisma.session.count({})
const sessions = await db.session.findMany({})
const sessionsCount = await db.session.count({})
res.status(200).json({
userId: blitzContext.session.userId,

View File

@@ -1,8 +1,8 @@
import {NextApiRequest, NextApiResponse} from "next"
import {prisma} from "../../prisma/index"
import db from "db"
export default async function handle(req: NextApiRequest, res: NextApiResponse) {
const session = await prisma.session.findFirst({
const session = await db.session.findFirst({
where: {
handle: "test",
},

View File

@@ -1,9 +1,9 @@
import {api} from "app/blitz-server"
import {prisma} from "../../prisma/index"
import db from "db"
export default api(async (_req, res) => {
const sessions = await prisma.session.deleteMany()
const sessionsCount = await prisma.session.count()
const sessions = await db.session.deleteMany()
const sessionsCount = await db.session.count()
res.status(200).json({
activeSessions: sessions,

View File

@@ -1,12 +1,12 @@
import {setPublicDataForUser} from "@blitzjs/auth"
import {api} from "app/blitz-server"
import {prisma} from "../../prisma/index"
import db from "db"
export default api(async (req, res, ctx) => {
if (ctx.session.$thisIsAuthorized()) {
ctx.session.$publicData
await prisma.user.update({
await db.user.update({
where: {id: ctx.session.userId as number},
data: {role: req.query.role as string},
})

View File

@@ -1,9 +1,9 @@
import {api} from "app/blitz-server"
import {prisma} from "../../prisma/index"
import db from "db"
import {SecurePassword} from "@blitzjs/auth"
export const authenticateUser = async (email: string, password: string) => {
const user = await prisma.user.findFirst({where: {email}})
const user = await db.user.findFirst({where: {email}})
if (!user) throw new Error("Authentication Error")
const result = await SecurePassword.verify(user.hashedPassword, password)
@@ -11,7 +11,7 @@ export const authenticateUser = async (email: string, password: string) => {
if (result === SecurePassword.VALID_NEEDS_REHASH) {
// Upgrade hashed password with a more secure hash
const improvedHash = await SecurePassword.hash(password)
await prisma.user.update({where: {id: user.id}, data: {hashedPassword: improvedHash}})
await db.user.update({where: {id: user.id}, data: {hashedPassword: improvedHash}})
}
const {hashedPassword, ...rest} = user
@@ -25,6 +25,7 @@ export default api(async (req, res, ctx) => {
await blitzContext.session.$create({
userId: user.id,
role: "USER",
})
res.status(200).json({email: req.query.email, userId: blitzContext.session.userId})

View File

@@ -1,5 +1,5 @@
import {api} from "app/blitz-server"
import {prisma} from "../../prisma/index"
import db from "db"
import {SecurePassword} from "@blitzjs/auth"
export default api(async (req, res, ctx) => {
@@ -9,13 +9,14 @@ export default api(async (req, res, ctx) => {
(req.query.password as string) || "test-password",
)
const email = (req.query.email as string) || "test" + Math.random() + "@test.com"
const user = await prisma.user.create({
const user = await db.user.create({
data: {email, hashedPassword, role: "user"},
select: {id: true, name: true, email: true, role: true},
})
await blitzContext.session.$create({
userId: user.id,
role: "USER",
})
res.status(200).json({userId: blitzContext.session.userId, ...user, email: req.query.email})

View File

@@ -1,8 +1,7 @@
import {useState} from "react"
import {SessionContext} from "@blitzjs/auth"
import {useMutation} from "@blitzjs/rpc"
import createUser from "app/mutations/createUser"
import {User} from "prisma"
import {User} from "db"
function Page() {
const [name, setName] = useState("")

View File

@@ -0,0 +1,32 @@
import {useInfiniteQuery} from "@blitzjs/rpc"
import {gSSP} from "app/blitz-server"
import getInfiniteUsers from "app/queries/getInfiniteUsers"
export const getServerSideProps = gSSP(async ({ctx}) => {
const {prefetchInfiniteQuery} = ctx
await prefetchInfiniteQuery(getInfiniteUsers, {}, {})
return {props: {}}
})
function PageWithPrefetchInfiniteQuery(props) {
const [usersPages] = useInfiniteQuery(getInfiniteUsers, (page = {take: 3, skip: 0}) => page, {
getNextPageParam: (lastPage) => lastPage.nextPage,
})
return (
<div>
{usersPages.map((usersPage) =>
usersPage?.users.map((u) => (
<div key={u.createdAt.toDateString()}>
<p>name: {u.name}</p>
<p>role: {u.role}</p>
<p>email: {u.email}</p>
<hr />
</div>
)),
)}
</div>
)
}
export default PageWithPrefetchInfiniteQuery

View File

@@ -0,0 +1,31 @@
import {SessionContext} from "@blitzjs/auth"
import {invokeWithCtx} from "@blitzjs/rpc"
import {gSSP} from "app/blitz-server"
import getUsersAuth from "app/queries/getUsersAuth"
type Props = {
userId: unknown
publicData: SessionContext["$publicData"]
}
export const getServerSideProps = gSSP<Props>(async ({ctx}) => {
const {session} = ctx
const users = await invokeWithCtx(getUsersAuth, {}, ctx)
console.log({users})
return {
props: {
userId: session.userId,
publicData: session.$publicData,
publishedAt: new Date(0),
},
}
})
function PageWithInvokeCtx(props: Props) {
return <div>{JSON.stringify(props, null, 2)}</div>
}
export default PageWithInvokeCtx

View File

@@ -3,13 +3,13 @@ import {gSSP} from "app/blitz-server"
import getUsers from "app/queries/getUsers"
export const getServerSideProps = gSSP(async ({ctx}) => {
const {prefetchBlitzQuery} = ctx
const {prefetchQuery} = ctx
await prefetchBlitzQuery(getUsers, {})
await prefetchQuery(getUsers, {}, {})
return {props: {}}
})
function PageWithGssp(props) {
function PageWithPrefetch(props) {
const [users] = useQuery(getUsers, {})
return (
<div>
@@ -25,4 +25,4 @@ function PageWithGssp(props) {
)
}
export default PageWithGssp
export default PageWithPrefetch

15
apps/web/types.ts Normal file
View File

@@ -0,0 +1,15 @@
import {SimpleRolesIsAuthorized} from "@blitzjs/auth"
import {User} from "db"
export type Role = "ADMIN" | "USER"
declare module "@blitzjs/auth" {
export interface Session {
isAuthorized: SimpleRolesIsAuthorized<Role>
PublicData: {
userId: User["id"]
role: Role
views?: number
}
}
}

View File

@@ -1,12 +1,11 @@
import {ErrorFallbackProps, ErrorComponent, ErrorBoundary} from "@blitzjs/next"
import {ErrorFallbackProps, ErrorComponent, ErrorBoundary, AppProps} from "@blitzjs/next"
import {AuthenticationError, AuthorizationError} from "blitz"
import type {AppProps} from "next/app"
import React from "react"
import {withBlitz} from "../app/blitz-client"
function RootErrorFallback({error}: ErrorFallbackProps) {
if (error instanceof AuthenticationError) {
return <div>Error: You are not authenticated</div>
return <div id="error">Error: You are not authenticated</div>
} else if (error instanceof AuthorizationError) {
return (
<ErrorComponent

View File

@@ -35,20 +35,24 @@ const runTests = (mode?: string) => {
"should render error for protected query",
async () => {
const browser = await webdriver(appPort, "/authenticated-page")
let errorMsg = await browser.elementByXpath(`//*[@id="__next"]/div`)
let errorMsg = await browser.elementById(`error`).text()
expect(errorMsg).toMatch(/Error: You are not authenticated/)
if (browser) browser.close()
},
5000 * 60 * 2,
)
it("should render result for open query", async () => {
const res = await fetch(`http://localhost:${appPort}/api/noauth`, {
method: "GET",
headers: {"Content-Type": "application/json; charset=utf-8"},
})
expect(res.status).toBe(200)
})
it(
"should render result for open query",
async () => {
const res = await fetch(`http://localhost:${appPort}/api/noauth`, {
method: "GET",
headers: {"Content-Type": "application/json; charset=utf-8"},
})
expect(res.status).toBe(200)
},
5000 * 60 * 2,
)
it("sets correct cookie", async () => {
const res = await fetch(`http://localhost:${appPort}/api/noauth`, {
@@ -118,31 +122,33 @@ const runTests = (mode?: string) => {
})
}
describe("dev mode", () => {
beforeAll(async () => {
try {
appPort = await findPort()
app = await launchApp(appDir, appPort, {})
await seed()
} catch (error) {
console.log(error)
}
}, 5000 * 60 * 2)
afterAll(async () => await killApp(app))
runTests()
})
describe("Auth Tests", () => {
describe("dev mode", () => {
beforeAll(async () => {
try {
appPort = await findPort()
app = await launchApp(appDir, appPort, {cwd: process.cwd()})
await seed()
} catch (error) {
console.log(error)
}
}, 5000 * 60 * 2)
afterAll(async () => await killApp(app))
runTests()
})
describe("server mode", () => {
beforeAll(async () => {
try {
appPort = await findPort()
await nextBuild(appDir)
app = await nextStart(appDir, appPort)
} catch (err) {
console.log(err)
}
}, 5000 * 60 * 2)
afterAll(async () => await killApp(app))
describe("server mode", () => {
beforeAll(async () => {
try {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort, {cwd: process.cwd()})
} catch (err) {
console.log(err)
}
}, 5000 * 60 * 2)
afterAll(async () => await killApp(app))
runTests()
runTests()
})
})

View File

@@ -0,0 +1,2 @@
SESSION_SECRET_KEY=hsdenhJfpLHrGjgdgg3jdF8g2bYD2PaQ
HEADLESS=true

View File

@@ -0,0 +1 @@
module.exports = require("@blitzjs/config/eslint")

View File

@@ -0,0 +1,3 @@
node_modules
# Keep environment variables out of version control
*.sqlite

View File

@@ -0,0 +1,14 @@
import {BlitzRpcPlugin} from "@blitzjs/rpc"
import {setupBlitzClient} from "@blitzjs/next"
import {AuthClientPlugin} from "@blitzjs/auth"
const {withBlitz} = setupBlitzClient({
plugins: [
AuthClientPlugin({
cookiePrefix: "no-suspense-tests-cookie-prefix",
}),
BlitzRpcPlugin({}),
],
})
export {withBlitz}

View File

@@ -0,0 +1,16 @@
import {setupBlitzServer} from "@blitzjs/next"
import {AuthServerPlugin, PrismaStorage} from "@blitzjs/auth"
import {simpleRolesIsAuthorized} from "@blitzjs/auth"
import {prisma as db} from "../prisma/index"
const {gSSP, gSP, api} = setupBlitzServer({
plugins: [
AuthServerPlugin({
cookiePrefix: "no-suspense-tests-cookie-prefix",
storage: PrismaStorage(db as any),
isAuthorized: simpleRolesIsAuthorized,
}),
],
})
export {gSSP, gSP, api}

View File

@@ -0,0 +1,3 @@
export default async function getBasic() {
return "basic-result"
}

View File

@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />
// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.

View File

@@ -0,0 +1,2 @@
const {withBlitz} = require("@blitzjs/next")
module.exports = withBlitz({})

View File

@@ -0,0 +1,41 @@
{
"name": "test-no-suspense",
"version": "0.0.0",
"private": true,
"scripts": {
"start:dev": "pnpm run prisma:start && next dev",
"test": "pnpm run prisma:start && vitest run",
"test-watch": "vitest",
"start": "next start",
"lint": "next lint",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf .next",
"prisma:start": "prisma generate && prisma migrate deploy",
"prisma:studio": "prisma studio"
},
"dependencies": {
"@blitzjs/auth": "workspace:*",
"@blitzjs/next": "workspace:*",
"@blitzjs/rpc": "workspace:*",
"@prisma/client": "3.9.0",
"blitz": "workspace:*",
"lowdb": "3.0.0",
"next": "12.1.6-canary.17",
"prisma": "3.9.0",
"react": "18.0.0",
"react-dom": "18.0.0"
},
"devDependencies": {
"@blitzjs/config": "workspace:*",
"@next/bundle-analyzer": "12.0.8",
"@types/express": "4.17.13",
"@types/fs-extra": "9.0.13",
"@types/node-fetch": "2.6.1",
"@types/react": "18.0.1",
"b64-lite": "1.4.0",
"eslint": "7.32.0",
"fs-extra": "10.0.1",
"get-port": "6.1.2",
"node-fetch": "3.2.3",
"typescript": "^4.5.3"
}
}

View File

@@ -0,0 +1,34 @@
import {ErrorFallbackProps, ErrorComponent, ErrorBoundary, AppProps} from "@blitzjs/next"
import {AuthenticationError, AuthorizationError} from "blitz"
import React from "react"
import {withBlitz} from "../app/blitz-client"
function RootErrorFallback({error}: ErrorFallbackProps) {
if (error instanceof AuthenticationError) {
return <div>Error: You are not authenticated</div>
} else if (error instanceof AuthorizationError) {
return (
<ErrorComponent
statusCode={error.statusCode}
title="Sorry, you are not authorized to access this"
/>
)
} else {
return (
<ErrorComponent
statusCode={(error as any)?.statusCode || 400}
title={error.message || error.name}
/>
)
}
}
function MyApp({Component, pageProps}: AppProps) {
return (
<ErrorBoundary FallbackComponent={RootErrorFallback}>
<Component {...pageProps} />
</ErrorBoundary>
)
}
export default withBlitz(MyApp)

View File

@@ -0,0 +1,4 @@
import {rpcHandler} from "@blitzjs/rpc"
import {api} from "../../../app/blitz-server"
export default api(rpcHandler({onError: console.log}))

View File

@@ -0,0 +1,23 @@
import getBasic from "../app/queries/getBasic"
import {useQuery} from "@blitzjs/rpc"
import React from "react"
function Content() {
const [result, {isFetching}] = useQuery(getBasic, undefined)
if (isFetching) {
return <>Loading...</>
} else {
return <div id="content">{result}</div>
}
}
function Page() {
return (
<div id="page">
<Content />
</div>
)
}
export default Page

View File

@@ -0,0 +1,47 @@
-- CreateTable
CREATE TABLE "User" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"name" TEXT,
"email" TEXT NOT NULL,
"hashedPassword" TEXT,
"role" TEXT NOT NULL DEFAULT 'user'
);
-- CreateTable
CREATE TABLE "Session" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"expiresAt" DATETIME,
"handle" TEXT NOT NULL,
"userId" INTEGER,
"hashedSessionToken" TEXT,
"antiCSRFToken" TEXT,
"publicData" TEXT,
"privateData" TEXT,
CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);
-- CreateTable
CREATE TABLE "Token" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"hashedToken" TEXT NOT NULL,
"type" TEXT NOT NULL,
"expiresAt" DATETIME NOT NULL,
"sentTo" TEXT NOT NULL,
"userId" INTEGER NOT NULL,
CONSTRAINT "Token_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);
-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
-- CreateIndex
CREATE UNIQUE INDEX "Session_handle_key" ON "Session"("handle");
-- CreateIndex
CREATE UNIQUE INDEX "Token_hashedToken_type_key" ON "Token"("hashedToken", "type");

View File

@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "sqlite"

View File

@@ -0,0 +1,50 @@
datasource sqlite {
provider = "sqlite"
url = "file:./db.sqlite"
}
generator client {
provider = "prisma-client-js"
}
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String?
email String @unique
hashedPassword String?
role String @default("user")
sessions Session[]
tokens Token[]
}
model Session {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
expiresAt DateTime?
handle String @unique
user User? @relation(fields: [userId], references: [id])
userId Int?
hashedSessionToken String?
antiCSRFToken String?
publicData String?
privateData String?
}
model Token {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
hashedToken String
type String
expiresAt DateTime
sentTo String
user User @relation(fields: [userId], references: [id])
userId Int
@@unique([hashedToken, type])
}

View File

@@ -0,0 +1,7 @@
import {prisma} from "./index"
const seed = async () => {
await prisma.$reset()
}
export default seed

View File

@@ -0,0 +1,57 @@
import {describe, it, expect, beforeAll, afterAll} from "vitest"
import {killApp, findPort, launchApp, nextBuild, nextStart} from "../../utils/next-test-utils"
import webdriver from "../../utils/next-webdriver"
import {join} from "path"
let app: any
let appPort: number
const appDir = join(__dirname, "../")
const runTests = (mode?: string) => {
describe("useQuery without suspense", () => {
it(
"should render query result",
async () => {
const browser = await webdriver(appPort, "/use-query")
let text
browser.waitForElementByCss("#content", 0)
text = await browser.elementByCss("#content").text()
expect(text).toMatch(/basic-result/)
if (browser) await browser.close()
},
5000 * 60 * 2,
)
})
}
describe("No Suspense Tests", () => {
describe("dev mode", () => {
beforeAll(async () => {
try {
appPort = await findPort()
app = await launchApp(appDir, appPort, {cwd: process.cwd()})
} catch (error) {
console.log(error)
}
}, 5000 * 60 * 2)
afterAll(async () => await killApp(app))
runTests()
})
describe("server mode", () => {
beforeAll(async () => {
try {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort, {cwd: process.cwd()})
} catch (err) {
console.log(err)
}
}, 5000 * 60 * 2)
afterAll(async () => await killApp(app))
runTests()
})
})

View File

@@ -0,0 +1,11 @@
{
"extends": "@blitzjs/config/tsconfig.nextjs.json",
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"compilerOptions": {
"paths": {
"react": ["./node_modules/@types/react"]
}
},
"exclude": ["node_modules"],
"baseUrl": "."
}

View File

@@ -143,7 +143,7 @@ describe("RPC", () => {
beforeAll(async () => {
try {
appPort = await findPort()
app = await launchApp(appDir, appPort)
app = await launchApp(appDir, appPort, {cwd: process.cwd()})
} catch (err) {
console.log(err)
}
@@ -162,7 +162,7 @@ describe("RPC", () => {
await nextBuild(appDir)
mode = "server"
appPort = await findPort()
app = await nextStart(appDir, appPort)
app = await nextStart(appDir, appPort, {cwd: process.cwd()})
})
afterAll(() => killApp(app))
@@ -185,7 +185,7 @@ describe("RPC", () => {
await nextBuild(appDir)
mode = "serverless"
appPort = await findPort()
app = await nextStart(appDir, appPort)
app = await nextStart(appDir, appPort, {cwd: process.cwd()})
})
afterAll(async () => {
await killApp(app)

View File

@@ -0,0 +1,2 @@
SESSION_SECRET_KEY=hsdenhJfpLHrGjgdgg3jdF8g2bYD2PaQ
HEADLESS=true

View File

@@ -0,0 +1 @@
module.exports = require("@blitzjs/config/eslint")

View File

@@ -0,0 +1,3 @@
node_modules
# Keep environment variables out of version control
*.sqlite

View File

@@ -0,0 +1,14 @@
import {BlitzRpcPlugin} from "@blitzjs/rpc"
import {setupBlitzClient} from "@blitzjs/next"
import {AuthClientPlugin} from "@blitzjs/auth"
const {withBlitz} = setupBlitzClient({
plugins: [
AuthClientPlugin({
cookiePrefix: "trailing-slash-tests-cookie-prefix",
}),
BlitzRpcPlugin({}),
],
})
export {withBlitz}

View File

@@ -0,0 +1,16 @@
import {setupBlitzServer} from "@blitzjs/next"
import {AuthServerPlugin, PrismaStorage} from "@blitzjs/auth"
import {simpleRolesIsAuthorized} from "@blitzjs/auth"
import {prisma as db} from "../prisma/index"
const {gSSP, gSP, api} = setupBlitzServer({
plugins: [
AuthServerPlugin({
cookiePrefix: "trailing-slash-tests-cookie-prefix",
storage: PrismaStorage(db as any),
isAuthorized: simpleRolesIsAuthorized,
}),
],
})
export {gSSP, gSP, api}

View File

@@ -0,0 +1,3 @@
export default async function getBasic() {
return "basic-result"
}

View File

@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />
// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.

View File

@@ -0,0 +1,4 @@
const {withBlitz} = require("@blitzjs/next")
module.exports = withBlitz({
trailingSlash: true,
})

View File

@@ -0,0 +1,41 @@
{
"name": "test-trailing-slash",
"version": "0.0.0",
"private": true,
"scripts": {
"start:dev": "pnpm run prisma:start && next dev",
"test": "pnpm run prisma:start && vitest run",
"test-watch": "vitest",
"start": "next start",
"lint": "next lint",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf .next",
"prisma:start": "prisma generate && prisma migrate deploy",
"prisma:studio": "prisma studio"
},
"dependencies": {
"@blitzjs/auth": "workspace:*",
"@blitzjs/next": "workspace:*",
"@blitzjs/rpc": "workspace:*",
"@prisma/client": "3.9.0",
"blitz": "workspace:*",
"lowdb": "3.0.0",
"next": "12.1.6-canary.17",
"prisma": "3.9.0",
"react": "18.0.0",
"react-dom": "18.0.0"
},
"devDependencies": {
"@blitzjs/config": "workspace:*",
"@next/bundle-analyzer": "12.0.8",
"@types/express": "4.17.13",
"@types/fs-extra": "9.0.13",
"@types/node-fetch": "2.6.1",
"@types/react": "18.0.1",
"b64-lite": "1.4.0",
"eslint": "7.32.0",
"fs-extra": "10.0.1",
"get-port": "6.1.2",
"node-fetch": "3.2.3",
"typescript": "^4.5.3"
}
}

View File

@@ -0,0 +1,34 @@
import {ErrorFallbackProps, ErrorComponent, ErrorBoundary, AppProps} from "@blitzjs/next"
import {AuthenticationError, AuthorizationError} from "blitz"
import React from "react"
import {withBlitz} from "../app/blitz-client"
function RootErrorFallback({error}: ErrorFallbackProps) {
if (error instanceof AuthenticationError) {
return <div>Error: You are not authenticated</div>
} else if (error instanceof AuthorizationError) {
return (
<ErrorComponent
statusCode={error.statusCode}
title="Sorry, you are not authorized to access this"
/>
)
} else {
return (
<ErrorComponent
statusCode={(error as any)?.statusCode || 400}
title={error.message || error.name}
/>
)
}
}
function MyApp({Component, pageProps}: AppProps) {
return (
<ErrorBoundary FallbackComponent={RootErrorFallback}>
<Component {...pageProps} />
</ErrorBoundary>
)
}
export default withBlitz(MyApp)

View File

@@ -0,0 +1,4 @@
import {rpcHandler} from "@blitzjs/rpc"
import {api} from "../../../app/blitz-server"
export default api(rpcHandler({onError: console.log}))

View File

@@ -0,0 +1,20 @@
import getBasic from "../app/queries/getBasic"
import {useQuery} from "@blitzjs/rpc"
import {Suspense} from "react"
function Content() {
const [result] = useQuery(getBasic, undefined)
return <div id="content">{result}</div>
}
function Page() {
return (
<div id="page">
<Suspense fallback={"Loading..."}>
<Content />
</Suspense>
</div>
)
}
export default Page

View File

@@ -0,0 +1,8 @@
import {enhancePrisma} from "blitz"
import {PrismaClient} from "@prisma/client"
const EnhancedPrisma = enhancePrisma(PrismaClient)
export * from "@prisma/client"
const prisma = new EnhancedPrisma()
export {prisma}

View File

@@ -0,0 +1,47 @@
-- CreateTable
CREATE TABLE "User" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"name" TEXT,
"email" TEXT NOT NULL,
"hashedPassword" TEXT,
"role" TEXT NOT NULL DEFAULT 'user'
);
-- CreateTable
CREATE TABLE "Session" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"expiresAt" DATETIME,
"handle" TEXT NOT NULL,
"userId" INTEGER,
"hashedSessionToken" TEXT,
"antiCSRFToken" TEXT,
"publicData" TEXT,
"privateData" TEXT,
CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);
-- CreateTable
CREATE TABLE "Token" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"hashedToken" TEXT NOT NULL,
"type" TEXT NOT NULL,
"expiresAt" DATETIME NOT NULL,
"sentTo" TEXT NOT NULL,
"userId" INTEGER NOT NULL,
CONSTRAINT "Token_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);
-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
-- CreateIndex
CREATE UNIQUE INDEX "Session_handle_key" ON "Session"("handle");
-- CreateIndex
CREATE UNIQUE INDEX "Token_hashedToken_type_key" ON "Token"("hashedToken", "type");

View File

@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "sqlite"

View File

@@ -0,0 +1,50 @@
datasource sqlite {
provider = "sqlite"
url = "file:./db.sqlite"
}
generator client {
provider = "prisma-client-js"
}
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String?
email String @unique
hashedPassword String?
role String @default("user")
sessions Session[]
tokens Token[]
}
model Session {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
expiresAt DateTime?
handle String @unique
user User? @relation(fields: [userId], references: [id])
userId Int?
hashedSessionToken String?
antiCSRFToken String?
publicData String?
privateData String?
}
model Token {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
hashedToken String
type String
expiresAt DateTime
sentTo String
user User @relation(fields: [userId], references: [id])
userId Int
@@unique([hashedToken, type])
}

View File

@@ -0,0 +1,7 @@
import {prisma} from "./index"
const seed = async () => {
await prisma.$reset()
}
export default seed

View File

@@ -0,0 +1,56 @@
import {describe, it, expect, beforeAll, afterAll} from "vitest"
import {killApp, findPort, launchApp, nextBuild, nextStart} from "../../utils/next-test-utils"
import webdriver from "../../utils/next-webdriver"
import {join} from "path"
let app: any
let appPort: number
const appDir = join(__dirname, "../")
const runTests = (mode?: string) => {
describe("Trailing Slash", () => {
it(
"should render query result",
async () => {
const browser = await webdriver(appPort, "/use-query")
let text = await browser.elementByCss("#page").text()
expect(text).toMatch(/Loading/)
await browser.waitForElementByCss("#content", 0)
text = await browser.elementByCss("#content").text()
expect(text).toMatch(/basic-result/)
if (browser) await browser.close()
},
5000 * 60 * 2,
)
})
}
describe("Trailing Slash Tests", () => {
describe("dev mode", () => {
beforeAll(async () => {
try {
appPort = await findPort()
app = await launchApp(appDir, appPort, {cwd: process.cwd()})
} catch (error) {
console.log(error)
}
}, 5000 * 60 * 2)
afterAll(async () => await killApp(app))
runTests()
})
describe("server mode", () => {
beforeAll(async () => {
try {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort, {cwd: process.cwd()})
} catch (err) {
console.log(err)
}
}, 5000 * 60 * 2)
afterAll(async () => await killApp(app))
runTests()
})
})

View File

@@ -0,0 +1,11 @@
{
"extends": "@blitzjs/config/tsconfig.nextjs.json",
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"compilerOptions": {
"paths": {
"react": ["./node_modules/@types/react"]
}
},
"exclude": ["node_modules"],
"baseUrl": "."
}

View File

@@ -80,21 +80,7 @@ const BlitzWrapper = ({plugins, children}) => {
export function render(ui: RenderUI, {wrapper, router, ...options}: RenderOptions = {}) {
if (!wrapper) {
wrapper = ({children}) => {
return (
<BlitzWrapper
plugins={[
BlitzRpcPlugin({
reactQueryOptions: {
queries: {
staleTime: 7000,
},
},
}),
]}
>
{children}
</BlitzWrapper>
)
return <BlitzWrapper plugins={[BlitzRpcPlugin({})]}>{children}</BlitzWrapper>
}
}

View File

@@ -0,0 +1,100 @@
export type Event = "request"
// This is the base Browser interface all browser
// classes should build off of, it is the bare
// methods we aim to support across tests
export class BrowserInterface {
private promise: any
private then: any
private catch: any
protected chain(nextCall: any): BrowserInterface {
if (!this.promise) {
this.promise = Promise.resolve(this)
}
this.promise = this.promise.then(nextCall)
this.then = (...args) => this.promise.then(...args)
this.catch = (...args) => this.promise.catch(...args)
return this
}
async setup(browserName: string, locale?: string): Promise<void> {}
async close(): Promise<void> {}
async quit(): Promise<void> {}
elementsByCss(selector: string): BrowserInterface[] {
return [this]
}
elementByCss(selector: string): BrowserInterface {
return this
}
elementById(selector: string): BrowserInterface {
return this
}
click(opts?: {modifierKey?: boolean}): BrowserInterface {
return this
}
keydown(key: string): BrowserInterface {
return this
}
keyup(key: string): BrowserInterface {
return this
}
focusPage(): BrowserInterface {
return this
}
type(text: string): BrowserInterface {
return this
}
waitForElementByCss(selector: string, timeout?: number): BrowserInterface {
return this
}
waitForCondition(snippet: string, timeout?: number): BrowserInterface {
return this
}
back(): BrowserInterface {
return this
}
forward(): BrowserInterface {
return this
}
refresh(): BrowserInterface {
return this
}
setDimensions(opts: {height: number; width: number}): BrowserInterface {
return this
}
addCookie(opts: {name: string; value: string}): BrowserInterface {
return this
}
deleteCookies(): BrowserInterface {
return this
}
on(event: Event, cb: (...args: any[]) => void) {}
off(event: Event, cb: (...args: any[]) => void) {}
async loadPage(url: string, {disableCache: boolean, beforePageLoad: Function}): Promise<void> {}
async get(url: string): Promise<void> {}
async getValue(): Promise<any> {}
async getAttribute(name: string): Promise<any> {}
async eval(snippet: string | Function): Promise<any> {}
async evalAsync(snippet: string | Function): Promise<any> {}
async text(): Promise<string> {
return ""
}
async getComputedCss(prop: string): Promise<string> {
return ""
}
async hasElementByCssSelector(selector: string): Promise<boolean> {
return false
}
async log(): Promise<any[]> {
return []
}
async websocketFrames(): Promise<any[]> {
return []
}
async url(): Promise<string> {
return ""
}
}

View File

@@ -0,0 +1,312 @@
import {BrowserInterface, Event} from "./base"
import {
chromium,
webkit,
firefox,
Browser,
BrowserContext,
Page,
ElementHandle,
} from "playwright-chromium"
let page: Page
let browser: Browser | undefined
let context: BrowserContext | undefined
let pageLogs: Array<{source: string; message: string}> = []
let websocketFrames: Array<{payload: string | Buffer}> = []
export async function quit() {
await context?.close()
await browser?.close()
context = undefined
browser = undefined
}
class Playwright extends BrowserInterface {
private eventCallbacks: Record<Event, Set<(...args: any[]) => void>> = {
request: new Set(),
}
on(event: Event, cb: (...args: any[]) => void) {
if (!this.eventCallbacks[event]) {
throw new Error(
`Invalid event passed to browser.on, received ${event}. Valid events are ${Object.keys(
event,
)}`,
)
}
this.eventCallbacks[event]?.add(cb)
}
off(event: Event, cb: (...args: any[]) => void) {
this.eventCallbacks[event]?.delete(cb)
}
async setup(browserName: string, locale?: string) {
if (browser) return
const headless = !!process.env.HEADLESS || true
if (browserName === "safari") {
browser = await webkit.launch({headless})
} else if (browserName === "firefox") {
browser = await firefox.launch({headless})
} else {
browser = await chromium.launch({headless, devtools: !headless})
}
context = await browser.newContext({locale})
}
async get(url: string): Promise<void> {
return page.goto(url) as any
}
async loadPage(
url: string,
opts?: {disableCache: boolean; beforePageLoad?: (...args: any[]) => void},
) {
// clean-up existing pages
for (const oldPage of context!.pages()) {
await oldPage.close()
}
page = await context!.newPage()
pageLogs = []
websocketFrames = []
page.on("console", (msg) => {
console.log("browser log:", msg)
pageLogs.push({source: msg.type(), message: msg.text()})
})
page.on("crash", (page) => {
console.error("page crashed")
})
page.on("pageerror", (error) => {
console.error("page error", error)
})
page.on("request", (req) => {
this.eventCallbacks.request.forEach((cb) => cb(req))
})
if (opts?.disableCache) {
// TODO: this doesn't seem to work (dev tools does not check the box as expected)
const session = await context!.newCDPSession(page)
session.send("Network.setCacheDisabled", {cacheDisabled: true})
}
page.on("websocket", (ws) => {
ws.on("framereceived", (frame) => {
websocketFrames.push({payload: frame.payload})
})
})
opts?.beforePageLoad?.(page)
await page.goto(url, {waitUntil: "load"})
}
back(): BrowserInterface {
return this.chain(() => {
return page.goBack()
})
}
forward(): BrowserInterface {
return this.chain(() => {
return page.goForward()
})
}
refresh(): BrowserInterface {
return this.chain(() => {
return page.reload()
})
}
setDimensions({width, height}: {height: number; width: number}): BrowserInterface {
return this.chain(() => page.setViewportSize({width, height}))
}
addCookie(opts: {name: string; value: string}): BrowserInterface {
return this.chain(async () =>
context!.addCookies([
{
path: "/",
domain: await page.evaluate("window.location.hostname"),
...opts,
},
]),
)
}
deleteCookies(): BrowserInterface {
return this.chain(async () => context!.clearCookies())
}
focusPage() {
return this.chain(() => page.bringToFront())
}
private wrapElement(el: ElementHandle, selector: string) {
;(el as any).selector = selector
;(el as any).text = () => el.innerText()
;(el as any).getComputedCss = (prop) =>
page.evaluate(
function (args) {
return (
getComputedStyle(document.querySelector(args.selector) as Element)[args.prop] || null
)
},
{selector, prop},
)
;(el as any).getCssValue = (el as any).getComputedCss
;(el as any).getValue = () =>
page.evaluate(
function (args) {
return (document.querySelector(args.selector) as any).value
},
{selector},
)
return el
}
elementByCss(selector: string) {
return this.waitForElementByCss(selector)
}
elementById(sel) {
return this.elementByCss(`#${sel}`)
}
getValue() {
return this.chain((el) =>
page.evaluate(
function (args) {
return document.querySelector(args.selector).value
},
{selector: el.selector},
),
) as any
}
text() {
return this.chain((el) => el.text()) as any
}
type(text) {
return this.chain((el) => el.type(text))
}
moveTo() {
return this.chain((el) => {
return page.hover(el.selector).then(() => el)
})
}
async getComputedCss(prop: string) {
return this.chain((el) => {
return el.getCssValue(prop)
}) as any
}
async getAttribute(attr) {
return this.chain((el) => el.getAttribute(attr))
}
async hasElementByCssSelector(selector: string) {
return this.eval(`!!document.querySelector('${selector}')`) as any
}
keydown(key: string): BrowserInterface {
return this.chain((el) => {
return page.keyboard.down(key).then(() => el)
})
}
keyup(key: string): BrowserInterface {
return this.chain((el) => {
return page.keyboard.up(key).then(() => el)
})
}
click() {
return this.chain((el) => {
return el.click().then(() => el)
})
}
elementsByCss(sel) {
return this.chain(() =>
page.$$(sel).then((els) => {
return els.map((el) => {
const origGetAttribute = el.getAttribute.bind(el)
el.getAttribute = (name) => {
// ensure getAttribute defaults to empty string to
// match selenium
return origGetAttribute(name).then((val) => val || "")
}
return el
})
}),
) as any as BrowserInterface[]
}
waitForElementByCss(selector, timeout?: number) {
return this.chain(() => {
return page.waitForSelector(selector, {timeout, state: "attached"}).then(async (el) => {
// it seems selenium waits longer and tests rely on this behavior
// so we wait for the load event fire before returning
await page.waitForLoadState()
return this.wrapElement(el, selector)
})
})
}
waitForCondition(condition, timeout) {
return this.chain(() => {
return page.waitForFunction(condition, {timeout})
})
}
async eval(snippet) {
// TODO: should this and evalAsync be chained? Might lead
// to bad chains
return page
.evaluate(snippet)
.catch((err) => {
console.error("eval error:", err)
return null
})
.then(async (val) => {
await page.waitForLoadState()
return val
})
}
async evalAsync(snippet) {
if (typeof snippet === "function") {
snippet = snippet.toString()
}
if (snippet.includes(`var callback = arguments[arguments.length - 1]`)) {
snippet = `(function() {
return new Promise((resolve, reject) => {
const origFunc = ${snippet}
try {
origFunc(resolve)
} catch (err) {
reject(err)
}
})
})()`
}
return page.evaluate(snippet).catch(() => null)
}
async log() {
return this.chain(() => pageLogs) as any
}
async websocketFrames() {
return this.chain(() => websocketFrames) as any
}
async url() {
return this.chain(() => page.evaluate("window.location.href")) as any
}
}
export default Playwright

Some files were not shown because too many files have changed in this diff Show More