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

Compare commits

...

53 Commits

Author SHA1 Message Date
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
beerose
66ea6ec0cb Update package-lock 2022-05-12 17:47:17 +02:00
Dillon Raphael
8490b07246 test automated publish 2022-05-12 11:44:53 -04:00
Dillon Raphael
f15a519017 alpha.23 2022-05-12 11:39:15 -04:00
Aleksandra
cfcd3f83df Read custom server config from blitz config (#3359) 2022-05-12 17:15:50 +02:00
beerose
adabb11a0c 2.0.0-alpha.23 2022-05-12 16:06:10 +02:00
Dillon Raphael
909dc76087 merge 2022-05-12 09:35:11 -04:00
Dillon Raphael
c5c727cb67 add mounted check to withBlitz 2022-05-12 09:31:55 -04:00
Aleksandra
6ff9ec0d75 Upgrade @types/react, fix typings inside @blitzjs/next (#3356) 2022-05-12 13:04:05 +02:00
Aleksandra
da17cc8a24 Support prefetchBlitzQuery in gSP and gSSP (#3341) 2022-05-12 12:55:13 +02:00
Dillon Raphael
89bf993a1d Add db seed cli command (#3350)
* added db seed cli command

* Update .changeset/poor-penguins-look.md

* Add tsconfig-paths to blitz deps

Co-authored-by: Aleksandra <alexsandra.sikora@gmail.com>
2022-05-10 15:24:58 +02:00
Blake Bayer
68f129491c change password updated with try/catch (#3321)
* change password updated with try/catch

* Update changePassword.ts

* Update changePassword.ts

* Apply suggestions from code review

Co-authored-by: Aleksandra <alexsandra.sikora@gmail.com>
2022-05-10 11:26:39 +02:00
Dillon Raphael
c096891bf0 change queries & mutation test package.json to different name 2022-05-09 14:30:59 -04:00
Dillon Raphael
81b4b41a99 add mounted check in app generator (#3349)
* add mounted check in app generator

* add changeset
2022-05-09 14:26:30 -04:00
Dillon Raphael
e8271d579c React query tests (#3348)
* init tests

* fix custom blitz provider for testing-library render function

* fix mutation test setup and set globaThis react environment to true so the act function will work

* added query tests & moved blitz test utils to the utils dir

* clean queries and mutation test files

* remove .env file from qm tests

Co-authored-by: beerose <alexsandra.sikora@gmail.com>
2022-05-09 17:05:42 +02:00
GaneshMani
9e798b152b migrate react-query unit test to blitz-rpc (#3340) 2022-05-08 14:44:50 +02:00
Aleksandra
3c6f43a11d Add github workflow for automatic release (#3330) 2022-05-05 15:33:31 +02:00
Dillon Raphael
8107138e2f alpha.21 2022-05-04 13:58:20 -04:00
Dillon Raphael
8cb1134d94 Change template package.json to use latest tags on blitz/auth & blitz/rpc 2022-05-04 13:56:50 -04:00
Dillon Raphael
2073714f8d testing explicity setting dist-tag for changesets 2022-05-04 13:48:11 -04:00
Dillon Raphael
9631c45833 alpha.19 2022-05-04 12:23:32 -04:00
Aleksandra
2150dcc3e7 Setup Superjson for gssp and gsp (#3335) 2022-05-04 17:45:35 +02:00
Aleksandra
63605961b4 Use routes manifest in template app (#3334) 2022-05-04 17:18:23 +02:00
Brandon Bayer
d711948809 toolkit: fix suspense loading by throwing error on the server (but still have error popup) (#3332)
* wip

* cleanup

* upgrade next@canary
2022-05-02 17:07:41 -07:00
Dillon Raphael
1f6b0b54c2 alpha.18 2022-04-29 20:46:32 -04:00
Dillon Raphael
4603a2b7bd alpha.17 2022-04-29 20:04:23 -04:00
Dillon Raphael
01f3a03ea9 alpha.16 2022-04-29 18:21:07 -04:00
Dillon Raphael
36e26193b7 alpha.15 2022-04-29 18:13:17 -04:00
Dillon Raphael
4a9aa9f7ff alpha.14 2022-04-29 18:08:06 -04:00
Dillon Raphael
1e1bb73b2d alpha.13 2022-04-29 16:25:20 -04:00
beerose
99205f52d5 alpha.12 2022-04-29 20:10:31 +02:00
Aleksandra
d03a1165d7 Use alpha version for blitz dependency, fix package manager selection (#3327) 2022-04-29 20:08:10 +02:00
beerose
d87288a2e9 alpha.11 2022-04-29 16:00:15 +02:00
beerose
fdff45a592 Update isInBlitzMonorepo check in postinstall script 2022-04-29 15:58:48 +02:00
beerose
469f55a429 Fix pre-publish script to build packages after
changeset version
2022-04-29 15:47:01 +02:00
Dillon Raphael
e6c31704ed added postinstall script to blitz/next package json 2022-04-29 09:41:57 -04:00
Dillon Raphael
35a070ad7d alpha.10 2022-04-28 19:37:39 -04:00
Dillon Raphael
9674efc0bf alpha.9 2022-04-28 19:07:56 -04:00
Dillon Raphael
666a3ae3e6 fix cli versioning 2022-04-28 17:35:54 -04:00
142 changed files with 3841 additions and 815 deletions

View File

@@ -0,0 +1,6 @@
---
"@blitzjs/next": patch
"@blitzjs/generator": patch
---
add mounted check inside withBlitz

View File

@@ -0,0 +1,7 @@
---
"blitz": patch
"@blitzjs/next": patch
"@blitzjs/generator": patch
---
Fix codegen and postinstall to make work with pnpm

View File

@@ -7,5 +7,5 @@
"access": "restricted",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": ["web", "test-*"]
"ignore": ["web", "test-*", "toolkit-app"]
}

View File

@@ -0,0 +1,6 @@
---
"blitz": patch
"@blitzjs/generator": patch
---
Use alpha version for blitz dependency, fix package manager selection

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/generator": patch
---
fix app generator for pnpm unmet dependency

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/next": patch
---
added superjson

View File

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

View File

@@ -0,0 +1,5 @@
---
"blitz": patch
---
set default enviornment variable to development unless build and start command

View File

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

View File

@@ -0,0 +1,6 @@
---
"@blitzjs/next": patch
"@blitzjs/generator": patch
---
Upgrade @types/react, fix typings inside @blitzjs/next

View File

@@ -0,0 +1,5 @@
---
"blitz": patch
---
fix broken cli versioning

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/next": patch
---
Support `prefetchBlitzQuery` in gSSP and gSP

View File

@@ -0,0 +1,5 @@
---
"blitz": patch
---
test automated publish

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/generator": patch
---
add mounted check to app generator template

View File

@@ -0,0 +1,11 @@
---
"blitz": patch
"@blitzjs/next": patch
"@blitzjs/generator": patch
---
- Add mounted check to withBlitz
- Upgrade @types/react, fix typings inside @blitzjs/next
- Support prefetchBlitzQuery in gSP and gSSP
- Add db seed cli command
- Add try/catch to changePassword mutation

View File

@@ -0,0 +1,5 @@
---
"blitz": patch
---
add `db seed` cli command

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/next": patch
---
Setup SuperJson for GSSP and GSP

View File

@@ -13,15 +13,45 @@
"@blitzjs/config": "0.0.0",
"@blitzjs/generator": "2.0.0-alpha.0",
"template": "0.0.0",
"toolkit-app": "1.0.0"
"toolkit-app": "1.0.0",
"test-qm": "0.0.0"
},
"changesets": [
"big-phones-bow",
"breezy-cameras-double",
"dirty-monkeys-greet",
"empty-berries-rule",
"fair-wombats-sneeze",
"fast-trainers-kneel",
"flat-bees-approve",
"four-meals-fry",
"great-months-train",
"hot-drinks-approve",
"lovely-colts-share",
"modern-cameras-pull",
"moody-squids-cheer",
"nice-starfishes-live",
"nine-onions-admire",
"ninety-pets-heal",
"plenty-bottles-swim",
"poor-peas-lick",
"poor-penguins-look",
"poor-shrimps-think",
"quiet-feet-travel",
"rich-chairs-invent",
"sharp-falcons-begin",
"silent-colts-reply",
"small-socks-confess",
"stupid-walls-sell",
"swift-drinks-dress",
"ten-rivers-burn",
"twenty-beans-pump"
"tender-pianos-check",
"thirty-countries-build",
"twenty-beans-pump",
"two-kiwis-help",
"unlucky-papayas-sleep",
"weak-suns-shave",
"wicked-ghosts-cough",
"wise-frogs-give"
]
}

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/generator": patch
---
fix source path for templates

View File

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

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/generator": patch
---
Use routes manifest in template app

View File

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

View File

@@ -0,0 +1,8 @@
---
"blitz": patch
"@blitzjs/auth": patch
"@blitzjs/next": patch
"@blitzjs/rpc": patch
---
testing set dist-tag

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/next": patch
---
Fix postinstall script not being found

View File

@@ -0,0 +1,7 @@
---
"blitz": patch
"@blitzjs/next": patch
"@blitzjs/generator": patch
---
various improvements and fixes

View File

@@ -0,0 +1,5 @@
---
"blitz": patch
---
add @blitzjs/generator as external

View File

@@ -0,0 +1,5 @@
---
"blitz": patch
---
fix generate cli command

View File

@@ -0,0 +1,5 @@
---
"blitz": patch
---
remove console logs inside onPostInstall in the new cli command

View File

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

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/generator": patch
---
fix template sourcepath because of new env variable

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/generator": patch
---
use latest tag for generator template on rpc & auth packages

44
.github/workflows/release.yml vendored Normal file
View File

@@ -0,0 +1,44 @@
name: Release
on:
push:
branches:
- main
concurrency: ${{ github.workflow }}-${{ github.ref }}
jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v2
- name: Setup Node.js 16.x
uses: actions/setup-node@v2
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 build
- name: Create Release Pull Request
uses: changesets/action@v1
with:
publish: pnpm release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

1
.npmrc
View File

@@ -1,4 +1,5 @@
save-exact=true
strict-peer-dependencies=false
public-hoist-pattern[]=secure-password
public-hoist-pattern[]=*types*

View File

@@ -1,5 +1,156 @@
# toolkit-app
## 1.0.1-alpha.16
### Patch Changes
- blitz@2.0.0-alpha.21
- @blitzjs/auth@2.0.0-alpha.21
- @blitzjs/next@2.0.0-alpha.21
- @blitzjs/rpc@2.0.0-alpha.21
- @blitzjs/config@2.0.0-alpha.21
## 1.0.1-alpha.15
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.20
- @blitzjs/auth@2.0.0-alpha.20
- @blitzjs/next@2.0.0-alpha.20
- @blitzjs/rpc@2.0.0-alpha.20
- @blitzjs/config@2.0.0-alpha.20
## 1.0.1-alpha.14
### Patch Changes
- Updated dependencies
- Updated dependencies [2150dcc3]
- @blitzjs/next@2.0.0-alpha.19
- blitz@2.0.0-alpha.19
- @blitzjs/auth@2.0.0-alpha.19
- @blitzjs/rpc@2.0.0-alpha.19
- @blitzjs/config@2.0.0-alpha.19
## 1.0.1-alpha.13
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.18
- @blitzjs/auth@2.0.0-alpha.18
- @blitzjs/next@2.0.0-alpha.18
- @blitzjs/rpc@2.0.0-alpha.18
- @blitzjs/config@2.0.0-alpha.18
## 1.0.1-alpha.12
### Patch Changes
- blitz@2.0.0-alpha.17
- @blitzjs/auth@2.0.0-alpha.17
- @blitzjs/next@2.0.0-alpha.17
- @blitzjs/rpc@2.0.0-alpha.17
- @blitzjs/config@2.0.0-alpha.17
## 1.0.1-alpha.11
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.16
- @blitzjs/auth@2.0.0-alpha.16
- @blitzjs/next@2.0.0-alpha.16
- @blitzjs/rpc@2.0.0-alpha.16
- @blitzjs/config@2.0.0-alpha.16
## 1.0.1-alpha.10
### Patch Changes
- blitz@2.0.0-alpha.15
- @blitzjs/auth@2.0.0-alpha.15
- @blitzjs/next@2.0.0-alpha.15
- @blitzjs/rpc@2.0.0-alpha.15
- @blitzjs/config@2.0.0-alpha.15
## 1.0.1-alpha.9
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.14
- @blitzjs/auth@2.0.0-alpha.14
- @blitzjs/next@2.0.0-alpha.14
- @blitzjs/rpc@2.0.0-alpha.14
- @blitzjs/config@2.0.0-alpha.14
## 1.0.1-alpha.8
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.13
- @blitzjs/next@2.0.0-alpha.13
- @blitzjs/auth@2.0.0-alpha.13
- @blitzjs/rpc@2.0.0-alpha.13
- @blitzjs/config@2.0.0-alpha.13
## 1.0.1-alpha.7
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.12
- @blitzjs/auth@2.0.0-alpha.12
- @blitzjs/next@2.0.0-alpha.12
- @blitzjs/rpc@2.0.0-alpha.12
- @blitzjs/config@2.0.0-alpha.12
## 1.0.1-alpha.6
### Patch Changes
- Updated dependencies
- @blitzjs/next@2.0.0-alpha.11
- @blitzjs/auth@2.0.0-alpha.11
- @blitzjs/rpc@2.0.0-alpha.11
- @blitzjs/config@2.0.0-alpha.11
- blitz@2.0.0-alpha.11
## 1.0.1-alpha.5
### Patch Changes
- blitz@2.0.0-alpha.10
- @blitzjs/auth@2.0.0-alpha.10
- @blitzjs/next@2.0.0-alpha.10
- @blitzjs/rpc@2.0.0-alpha.10
- @blitzjs/config@2.0.0-alpha.10
## 1.0.1-alpha.4
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.9
- @blitzjs/auth@2.0.0-alpha.9
- @blitzjs/next@2.0.0-alpha.9
- @blitzjs/rpc@2.0.0-alpha.9
- @blitzjs/config@2.0.0-alpha.9
## 1.0.1-alpha.3
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.8
- @blitzjs/auth@2.0.0-alpha.8
- @blitzjs/next@2.0.0-alpha.8
- @blitzjs/rpc@2.0.0-alpha.8
- @blitzjs/config@2.0.0-alpha.8
## 1.0.1-alpha.2
### Patch Changes

View File

@@ -5,6 +5,7 @@ import { Form, FORM_ERROR } from "app/core/components/Form"
import login from "app/auth/mutations/login"
import { Login } from "app/auth/validations"
import { useMutation } from "@blitzjs/rpc"
import { Routes } from "@blitzjs/next"
type LoginFormProps = {
onSuccess?: (user: PromiseReturnType<typeof login>) => void
@@ -39,7 +40,7 @@ export const LoginForm = (props: LoginFormProps) => {
<LabeledTextField name="email" label="Email" placeholder="Email" />
<LabeledTextField name="password" label="Password" placeholder="Password" type="password" />
<div>
<Link href="/auth/forgot-password" passHref>
<Link href={Routes.ForgotPasswordPage()} passHref>
<a>Forgot your password?</a>
</Link>
</div>
@@ -47,7 +48,7 @@ export const LoginForm = (props: LoginFormProps) => {
<div style={{ marginTop: "1rem" }}>
Or{" "}
<Link href="/auth/signup" passHref>
<Link href={Routes.SignupPage()} passHref>
<a>Sign Up</a>
</Link>
</div>

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,
"hashedSessionToken" TEXT,
"antiCSRFToken" TEXT,
"publicData" TEXT,
"privateData" TEXT,
"userId" INTEGER,
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

@@ -1,4 +1,4 @@
// import db from "./index"
import { db } from "./index"
/*
* This seed function is executed when you run `blitz db seed`.
@@ -7,9 +7,15 @@
* to easily generate realistic data.
*/
const seed = async () => {
// for (let i = 0; i < 5; i++) {
// await db.project.create({ data: { name: "Project " + i } })
// }
await db.$reset()
for (let i = 0; i < 1; i++) {
await db.user.create({
data: {
email: "test@test.com",
},
})
}
}
export default seed

View File

@@ -1,9 +1,9 @@
{
"name": "toolkit-app",
"version": "1.0.1-alpha.2",
"version": "1.0.1-alpha.16",
"scripts": {
"start:dev": "pnpm run prisma:start && next dev",
"buildapp": "prisma generate && next build",
"buildapp": "pnpm i && pnpm prisma generate && next build",
"start": "next start",
"lint": "next lint",
"prisma:start": "prisma generate && prisma migrate deploy",
@@ -29,8 +29,8 @@
"@blitzjs/rpc": "workspace:*",
"@hookform/resolvers": "2.8.8",
"@prisma/client": "3.9.0",
"blitz": "workspace:2.0.0-alpha.7",
"next": "12.1.1",
"blitz": "workspace:2.0.0-alpha.26",
"next": "12.1.6-canary.17",
"prisma": "3.9.0",
"react": "18.0.0",
"react-dom": "18.0.0",
@@ -45,7 +45,7 @@
"@types/jest": "27.4.1",
"@types/node": "17.0.16",
"@types/preview-email": "2.0.1",
"@types/react": "17.0.43",
"@types/react": "18.0.1",
"eslint": "7.32.0",
"husky": "7.0.4",
"jest": "27.5.1",

View File

@@ -1,7 +1,6 @@
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, { Suspense } from "react"
import React from "react"
import { withBlitz } from "app/blitz-client"
function RootErrorFallback({ error }: ErrorFallbackProps) {
@@ -27,9 +26,7 @@ function RootErrorFallback({ error }: ErrorFallbackProps) {
function MyApp({ Component, pageProps }: AppProps) {
return (
<ErrorBoundary FallbackComponent={RootErrorFallback}>
<Suspense fallback="Loading...">
<Component {...pageProps} />
</Suspense>
<Component {...pageProps} />
</ErrorBoundary>
)
}

View File

@@ -1,13 +1,14 @@
import { useRouter } from "next/router"
import Layout from "app/core/layouts/Layout"
import { SignupForm } from "app/auth/components/SignupForm"
import { Routes } from "@blitzjs/next"
const SignupPage = () => {
const router = useRouter()
return (
<Layout title="Sign Up">
<SignupForm onSuccess={() => router.push("/")} />
<SignupForm onSuccess={() => router.push(Routes.Home())} />
</Layout>
)
}

View File

@@ -6,6 +6,7 @@ import { useCurrentUser } from "app/core/hooks/useCurrentUser"
import logout from "app/auth/mutations/logout"
import logo from "public/logo.png"
import { useMutation } from "@blitzjs/rpc"
import { Routes } from "@blitzjs/next"
/*
* This file is just for a pleasant getting started page for your new app.
@@ -37,12 +38,12 @@ const UserInfo = () => {
} else {
return (
<>
<Link href="/auth/signup" passHref>
<Link href={Routes.SignupPage()} passHref>
<a className="button small">
<strong>Sign Up</strong>
</a>
</Link>
<Link href="/auth/login" passHref>
<Link href={Routes.LoginPage()} passHref>
<a className="button small">
<strong>Login</strong>
</a>

View File

@@ -2,7 +2,7 @@
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"baseUrl": "./",
"baseUrl": ".",
"allowJs": true,
"skipLibCheck": true,
"strict": false,

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

@@ -5,4 +5,4 @@ const EnhancedPrisma = enhancePrisma(PrismaClient)
export * from "@prisma/client"
const prisma = new EnhancedPrisma()
export {prisma}
export default prisma

View File

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

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.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",
@@ -29,7 +35,7 @@
},
"devDependencies": {
"@next/bundle-analyzer": "12.0.8",
"@types/react": "17.0.43",
"@types/react": "18.0.1",
"eslint": "7.32.0",
"typescript": "^4.5.3"
}

View File

@@ -1,7 +1,6 @@
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, {Suspense} from "react"
import React from "react"
import {withBlitz} from "app/blitz-client"
function RootErrorFallback({error}: ErrorFallbackProps) {
@@ -27,9 +26,7 @@ function RootErrorFallback({error}: ErrorFallbackProps) {
function MyApp({Component, pageProps}: AppProps) {
return (
<ErrorBoundary FallbackComponent={RootErrorFallback}>
<Suspense fallback="Loading...">
<Component {...pageProps} />
</Suspense>
<Component {...pageProps} />
</ErrorBoundary>
)
}

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

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,7 +9,7 @@ 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},
})

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

@@ -1,14 +1,11 @@
import {gSP} from "app/blitz-server"
export const getStaticProps = gSP(async ({ctx}) => {
export const getStaticProps = gSP<{data: {test: string}}>(async ({ctx}) => {
return {
props: {
data: {
// userId: ctx?.session.userId,
// session: {
// id: session.userId,
// publicData: session.$publicData,
// },
test: "hello",
date: new Date(),
},
},
}

View File

@@ -12,6 +12,7 @@ export const getServerSideProps = gSSP<Props>(async ({ctx}) => {
props: {
userId: session.userId,
publicData: session.$publicData,
publishedAt: new Date(0),
},
}
})

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 PageWithGssp(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 PageWithGssp

View File

@@ -0,0 +1,28 @@
import {useQuery} from "@blitzjs/rpc"
import {gSSP} from "app/blitz-server"
import getUsers from "app/queries/getUsers"
export const getServerSideProps = gSSP(async ({ctx}) => {
const {prefetchQuery} = ctx
await prefetchQuery(getUsers, {}, {})
return {props: {}}
})
function PageWithGssp(props) {
const [users] = useQuery(getUsers, {})
return (
<div>
{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 PageWithGssp

View File

@@ -19,7 +19,7 @@
"@prisma/client": "3.9.0",
"blitz": "workspace:*",
"lowdb": "3.0.0",
"next": "12.1.1",
"next": "12.1.6-canary.17",
"prisma": "3.9.0",
"react": "18.0.0",
"react-dom": "18.0.0"
@@ -29,7 +29,7 @@
"@types/express": "4.17.13",
"@types/fs-extra": "9.0.13",
"@types/node-fetch": "2.6.1",
"@types/react": "17.0.43",
"@types/react": "18.0.1",
"b64-lite": "1.4.0",
"eslint": "7.32.0",
"fs-extra": "10.0.1",

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 @@
module.exports = require("@blitzjs/config/eslint")

3
integration-tests/qm/.gitignore vendored Normal file
View File

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

5
integration-tests/qm/next-env.d.ts vendored Normal file
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({
// update me
})

View File

@@ -0,0 +1,34 @@
{
"name": "test-qm",
"version": "0.0.0",
"private": true,
"scripts": {
"test": "vitest run",
"test-watch": "vitest",
"clean": "rm -rf .turbo && rm -rf node_modules"
},
"dependencies": {
"@blitzjs/auth": "workspace:*",
"@blitzjs/config": "workspace:*",
"@blitzjs/next": "workspace:*",
"@blitzjs/rpc": "workspace:*",
"@prisma/client": "3.9.0",
"blitz": "workspace:*",
"next": "12.1.6-canary.17",
"prisma": "3.9.0",
"react": "18.0.0",
"react-dom": "18.0.0",
"react-query": "3.39.0"
},
"devDependencies": {
"@testing-library/react": "13.0.0",
"@types/react": "18.0.1",
"@vitejs/plugin-react": "1.3.0",
"delay": "5.0.0",
"eslint": "7.32.0",
"eslint-config-next": "latest",
"eslint-plugin-testing-library": "5.0.1",
"jsdom": "^19.0.0",
"typescript": "^4.5.3"
}
}

View File

@@ -0,0 +1,5 @@
// Vitest Snapshot v1
exports[`useMutation > useMutation calls the resolver with the argument > shouldn't work with query function 1`] = `"\\"useMutation\\" was expected to be called with a mutation but was called with a \\"query\\""`;
exports[`useMutation > useMutation calls the resolver with the argument > shouldn't work with regular functions 1`] = `"Either the file path to your resolver is incorrect (must be in a \\"queries\\" or \\"mutations\\" folder that isn't nested inside \\"pages\\" or \\"api\\") or you are trying to use Blitz's useQuery to fetch from third-party APIs (to do that, import useQuery directly from \\"react-query\\")"`;

View File

@@ -0,0 +1,5 @@
// Vitest Snapshot v1
exports[`useQuery > a "query" that converts the string parameter to uppercase > shouldn't work with mutation function 1`] = `"Cannot read properties of null (reading 'isReady')"`;
exports[`useQuery > a "query" that converts the string parameter to uppercase > shouldn't work with regular functions 1`] = `"Cannot read properties of null (reading 'isReady')"`;

View File

@@ -0,0 +1,55 @@
import {describe, it, expect, beforeAll, vi} from "vitest"
import {act, screen} from "@testing-library/react"
import {useMutation} from "@blitzjs/rpc"
import React from "react"
import {buildMutationRpc, buildQueryRpc, render} from "../../utils/blitz-test-utils"
beforeAll(() => {
globalThis.__BLITZ_SESSION_COOKIE_PREFIX = "qm-test-cookie-prefix"
globalThis.IS_REACT_ACT_ENVIRONMENT = true
})
describe("useMutation", () => {
const setupHook = (resolver: (...args: any) => Promise<any>): [{mutate?: Function}, Function] => {
let res = {}
function TestHarness() {
const [mutate, {isSuccess}] = useMutation(resolver)
Object.assign(res, {mutate})
return <div id="harness">{isSuccess ? "Sent" : "Waiting"}</div>
}
const ui = () => <TestHarness />
const {rerender} = render(ui())
return [res, () => rerender(ui())]
}
describe("useMutation calls the resolver with the argument", () => {
// eslint-disable-next-line require-await
const mutateFn = vi.fn()
it("should work with Blitz mutations", async () => {
const [res] = setupHook(buildMutationRpc(mutateFn))
await act(async () => {
await res.mutate!("data")
})
await screen.findByText("Sent")
expect(mutateFn).toHaveBeenCalledTimes(1)
expect(mutateFn).toHaveBeenCalledWith("data", {fromQueryHook: true})
})
it("shouldn't work with regular functions", () => {
console.error = vi.fn()
expect(() => setupHook(mutateFn)).toThrowErrorMatchingSnapshot()
})
it("shouldn't work with query function", () => {
console.error = vi.fn()
const mutationFn = vi.fn()
expect(() => setupHook(buildQueryRpc(mutationFn))).toThrowErrorMatchingSnapshot()
})
})
})

View File

@@ -0,0 +1,166 @@
import {describe, it, expect, beforeAll, vi} from "vitest"
import {act, screen, waitForElementToBeRemoved, waitFor} from "@testing-library/react"
import {useQuery, useInfiniteQuery} from "@blitzjs/rpc"
import React from "react"
import delay from "delay"
import {buildMutationRpc, buildQueryRpc, render} from "../../utils/blitz-test-utils"
describe("useQuery", () => {
it("Placeholder", async () => {
console.log("placeholder")
})
})
// beforeAll(() => {
// globalThis.__BLITZ_SESSION_COOKIE_PREFIX = "qm-test-cookie-prefix"
// globalThis.IS_REACT_ACT_ENVIRONMENT = true
// })
// describe("useQuery", () => {
// const setupHook = (
// params: any,
// queryFn: (...args: any) => any,
// options: Parameters<typeof useQuery>[2] = {} as any,
// ): [{data?: any; setQueryData?: any}, Function] => {
// let res = {}
// function TestHarness() {
// const [data, {setQueryData}] = useQuery(queryFn, params, {
// suspense: true,
// ...options,
// } as any)
// Object.assign(res, {data, setQueryData})
// return (
// <div id="harness">
// <span>{data ? "Ready" : "No data"}</span>
// <span>{data}</span>
// </div>
// )
// }
// const ui = () => (
// <React.Suspense fallback="Loading...">
// <TestHarness />
// </React.Suspense>
// )
// const {rerender} = render(ui())
// return [res, () => rerender(ui())]
// }
// describe('a "query" that converts the string parameter to uppercase', () => {
// const upcase = async (args: string) => {
// await delay(1000)
// return args.toUpperCase()
// }
// it("should work with Blitz queries", async () => {
// const [res] = setupHook("test", buildQueryRpc(upcase))
// await waitForElementToBeRemoved(() => screen.getByText("Loading..."))
// await act(async () => {
// await screen.findByText("Ready")
// expect(res.data).toBe("TEST")
// })
// })
// it("should be able to change the data with setQueryData", async () => {
// const [res] = setupHook("test", buildQueryRpc(upcase))
// await waitForElementToBeRemoved(() => screen.getByText("Loading..."))
// await act(async () => {
// await screen.findByText("Ready")
// expect(res.data).toBe("TEST")
// res.setQueryData((p: string) => p.substr(1, 2), {refetch: false})
// await waitFor(() => screen.getByText("ES"))
// })
// })
// it("shouldn't work with regular functions", () => {
// console.error = vi.fn()
// expect(() => setupHook("test", upcase)).toThrowErrorMatchingSnapshot()
// })
// it("shouldn't work with mutation function", () => {
// console.error = vi.fn()
// expect(() => setupHook("test", buildMutationRpc(upcase))).toThrowErrorMatchingSnapshot()
// })
// it("suspense disabled if enabled is false", async () => {
// setupHook("test", buildQueryRpc(upcase), {enabled: false})
// await screen.findByText("No data")
// })
// it("suspense disabled if enabled is undefined", async () => {
// setupHook("test", buildQueryRpc(upcase), {enabled: undefined})
// await screen.findByText("No data")
// })
// it("suspense disabled if enabled is false and suspense set", async () => {
// setupHook("test", buildQueryRpc(upcase), {
// enabled: false,
// suspense: true,
// })
// await screen.findByText("No data")
// })
// })
// // it("works with options other than enabled & suspense without type error", () => {
// // const queryFn = ((() => true) as unknown) as () => Promise<boolean>
// // useQuery(queryFn, undefined, {refetchInterval: 10000})
// // })
// })
// describe("useInfiniteQuery", () => {
// const setupHook = (
// params: (arg?: any) => any,
// queryFn: (...args: any) => any,
// ): [{data?: any; setQueryData?: any}, Function] => {
// let res = {}
// function TestHarness() {
// // TODO - fix typing
// //@ts-ignore
// const [groupedData] = useInfiniteQuery(queryFn, params, {
// suspense: true,
// getNextPageParam: () => {},
// })
// Object.assign(res, {groupedData})
// return (
// <div id="harness">
// <span>{groupedData ? "Ready" : "No data"}</span>
// <div>
// {groupedData.map((data: any, i) => (
// <div key={i}>{data}</div>
// ))}
// </div>
// </div>
// )
// }
// const ui = () => (
// <React.Suspense fallback="Loading...">
// <TestHarness />
// </React.Suspense>
// )
// const {rerender} = render(ui())
// return [res, () => rerender(ui())]
// }
// const getItems = ({id}: {id: number}) => {
// if (id === 1) {
// return "item1"
// } else if (id === 2) {
// return "item2"
// } else {
// throw new Error("No item for this id")
// }
// }
// it("should work", async () => {
// setupHook(() => ({id: 1}), buildQueryRpc(getItems))
// await waitForElementToBeRemoved(() => screen.getByText("Loading..."))
// await act(async () => {
// await screen.findByText("item1")
// })
// setupHook(() => ({id: 2}), buildQueryRpc(getItems))
// await act(async () => {
// await screen.findByText("item2")
// })
// })
// })

View File

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

View File

@@ -0,0 +1,12 @@
/// <reference types="vitest" />
import { defineConfig } from 'vitest/config'
import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
test: {
environment: 'jsdom',
},
})

View File

@@ -1,4 +1,4 @@
const {withBlitz} = require("@blitzjs/next")
module.exports = withBlitz({
// update me
target: 'experimental-serverless-trace',
})

View File

@@ -12,7 +12,7 @@
"@blitzjs/next": "workspace:*",
"@blitzjs/rpc": "workspace:*",
"blitz": "workspace:*",
"next": "12.1.1",
"next": "12.1.6-canary.17",
"react": "18.0.0",
"react-dom": "18.0.0"
},
@@ -20,7 +20,7 @@
"@types/express": "4.17.13",
"@types/fs-extra": "9.0.13",
"@types/node-fetch": "2.6.1",
"@types/react": "17.0.43",
"@types/react": "18.0.1",
"b64-lite": "1.4.0",
"eslint": "7.32.0",
"fs-extra": "10.0.1",

View File

@@ -0,0 +1,123 @@
import {render as defaultRender} from "@testing-library/react"
import {NextRouter} from "next/router"
import {vi} from "vitest"
import {QueryClient, QueryClientProvider} from "react-query"
import React from "react"
import {BlitzRpcPlugin} from "@blitzjs/rpc"
const mockRouter: NextRouter = {
basePath: "",
pathname: "/",
route: "/",
asPath: "/",
query: {},
isReady: true,
isLocaleDomain: false,
isPreview: false,
push: vi.fn(),
replace: vi.fn(),
reload: vi.fn(),
back: vi.fn(),
prefetch: vi.fn(),
beforePopState: vi.fn(),
events: {
on: vi.fn(),
off: vi.fn(),
emit: vi.fn(),
},
isFallback: false,
}
type DefaultParams = Parameters<typeof defaultRender>
type RenderUI = DefaultParams[0]
type RenderOptions = DefaultParams[1] & {
router?: Partial<NextRouter>
}
export type BlitzProviderProps = {
client?: QueryClient
contextSharing?: boolean
}
const BlitzProvider = ({
client,
contextSharing = false,
children,
}: BlitzProviderProps & {children: JSX.Element}) => {
if (globalThis.queryClient) {
return (
<QueryClientProvider
client={client || globalThis.queryClient}
contextSharing={contextSharing}
>
{children}
</QueryClientProvider>
)
}
return children
}
export const RouterContext = React.createContext(null as any)
RouterContext.displayName = "RouterContext"
const compose =
(...rest) =>
(x: React.ComponentType<any>) =>
rest.reduceRight((y, f) => f(y), x)
const BlitzWrapper = ({plugins, children}) => {
const providers = plugins.reduce((acc, plugin) => {
return plugin.withProvider ? acc.concat(plugin.withProvider) : acc
}, [])
const withPlugins = compose(...providers)
const component = React.useMemo(() => withPlugins(children), [children])
return (
<BlitzProvider>
<RouterContext.Provider value={{...mockRouter}}>{component}</RouterContext.Provider>
</BlitzProvider>
)
}
export function render(ui: RenderUI, {wrapper, router, ...options}: RenderOptions = {}) {
if (!wrapper) {
wrapper = ({children}) => {
return (
<BlitzWrapper
plugins={[
BlitzRpcPlugin({
reactQueryOptions: {
queries: {
staleTime: 7000,
},
},
}),
]}
>
{children}
</BlitzWrapper>
)
}
}
return defaultRender(ui, {wrapper, ...options})
}
// This enhance fn does what buildRpcFunction does during build time
export function buildQueryRpc(fn: any) {
const newFn = (...args: any) => {
const [data, ...rest] = args
return fn(data, ...rest)
}
newFn._isRpcClient = true
newFn._resolverType = "query"
newFn._routePath = "/api/test/url/" + Math.random()
return newFn
}
// This enhance fn does what buildRpcFunction does during build time
export function buildMutationRpc(fn: any) {
const newFn = (...args: any) => fn(...args)
newFn._isRpcClient = true
newFn._resolverType = "mutation"
newFn._routePath = "/api/test/url"
return newFn
}

View File

@@ -12,6 +12,8 @@ export function waitFor(millis) {
return new Promise((resolve) => setTimeout(resolve, millis))
}
export {By}
const {
BROWSER_NAME: browserName = "chrome",
BROWSERSTACK,

View File

@@ -3,10 +3,13 @@
"version": "0.0.0",
"private": true,
"devDependencies": {
"@blitzjs/config": "workspace: *",
"@blitzjs/rpc": "workspace: *",
"@testing-library/react": "13.0.0",
"@types/express": "4.17.13",
"@types/fs-extra": "9.0.13",
"@types/node-fetch": "2.6.1",
"@types/react": "17.0.43",
"@types/react": "18.0.1",
"@types/rimraf": "3.0.2",
"@types/selenium-webdriver": "4.0.18",
"chromedriver": "100.0.0",
@@ -16,6 +19,9 @@
"fs-extra": "10.0.1",
"get-port": "6.1.2",
"node-fetch": "3.2.3",
"react": "18.0.0",
"react-dom": "18.0.0",
"react-query": "3.39.0",
"rimraf": "3.0.2",
"selenium-webdriver": "4.1.1",
"tree-kill": "1.2.2",

View File

@@ -0,0 +1,13 @@
{
"extends": "@blitzjs/config/tsconfig.nextjs.json",
"include": ["*.ts", "*.tsx"],
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"paths": {
"react": ["./node_modules/@types/react"]
}
},
"exclude": ["node_modules"],
"baseUrl": "."
}

View File

@@ -88,6 +88,10 @@ export default class Chain {
return this.updateChain(() => this.browser.findElements(By.css(sel)))
}
elementsById(sel) {
return this.updateChain(() => this.browser.findElements(By.id(sel)))
}
waitForElementByCss(sel, timeout) {
return this.updateChain(() => this.browser.wait(until.elementLocated(By.css(sel), timeout)))
}

View File

@@ -17,7 +17,8 @@
"test": "turbo run test",
"clean": "turbo run clean && rm -rf node_modules",
"format": "prettier --write \"**/*.{ts,tsx,md}\"",
"pre-publish": "pnpm i && pnpm build && changeset add && changeset version && git add . && git commit -v",
"pre-publish": "changeset add && changeset version && pnpm build && git add . && git commit -v",
"release": "pnpm build && changeset publish",
"publish-release": "changeset publish && git push --follow-tags"
},
"dependencies": {
@@ -25,9 +26,9 @@
"@changesets/cli": "2.22.0",
"eslint": "7.32.0",
"husky": "7.0.4",
"jsdom": "19.0.0",
"jsdom": "^19.0.0",
"lint-staged": "12.1.7",
"next": "12.1.1",
"next": "12.1.6-canary.17",
"only-allow": "1.1.0",
"patch-package": "6.4.7",
"prettier": "^2.5.1",

View File

@@ -1,5 +1,134 @@
# @blitzjs/auth
## 2.0.0-alpha.26
### Patch Changes
- blitz@2.0.0-alpha.26
## 2.0.0-alpha.25
### Patch Changes
- 1436e761: Add passport adapter to @blitzjs/auth
- Updated dependencies [1436e761]
- blitz@2.0.0-alpha.25
## 2.0.0-alpha.24
### Patch Changes
- Updated dependencies [8490b072]
- blitz@2.0.0-alpha.24
## 2.0.0-alpha.23
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.23
## 2.0.0-alpha.22
### Patch Changes
- Updated dependencies
- Updated dependencies [89bf993a]
- blitz@2.0.0-alpha.22
## 2.0.0-alpha.21
### Patch Changes
- blitz@2.0.0-alpha.21
## 2.0.0-alpha.20
### Patch Changes
- testing set dist-tag
- Updated dependencies
- blitz@2.0.0-alpha.20
## 2.0.0-alpha.19
### Patch Changes
- blitz@2.0.0-alpha.19
## 2.0.0-alpha.18
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.18
## 2.0.0-alpha.17
### Patch Changes
- blitz@2.0.0-alpha.17
## 2.0.0-alpha.16
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.16
## 2.0.0-alpha.15
### Patch Changes
- blitz@2.0.0-alpha.15
## 2.0.0-alpha.14
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.14
## 2.0.0-alpha.13
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.13
## 2.0.0-alpha.12
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.12
## 2.0.0-alpha.11
### Patch Changes
- blitz@2.0.0-alpha.11
## 2.0.0-alpha.10
### Patch Changes
- blitz@2.0.0-alpha.10
## 2.0.0-alpha.9
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.9
## 2.0.0-alpha.8
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.8
## 2.0.0-alpha.7
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/auth",
"version": "2.0.0-alpha.7",
"version": "2.0.0-alpha.26",
"scripts": {
"build": "unbuild",
"predev": "wait-on -d 250 ../blitz/dist/index-server.d.ts",
@@ -21,27 +21,31 @@
],
"dependencies": {
"@types/b64-lite": "1.3.0",
"@types/cookie-session": "2.0.44",
"@types/passport": "1.0.7",
"@types/secure-password": "3.1.1",
"b64-lite": "1.4.0",
"bad-behavior": "1.0.1",
"blitz": "2.0.0-alpha.7",
"blitz": "2.0.0-alpha.26",
"cookie": "0.4.1",
"cookie-session": "2.0.0",
"debug": "4.3.3",
"http": "0.0.1-security",
"jsonwebtoken": "8.5.1",
"nanoid": "3.2.0",
"passport": "0.5.2",
"path": "0.12.7",
"secure-password": "4.0.0",
"url": "0.11.0"
},
"devDependencies": {
"@blitzjs/config": "workspace:2.0.0-alpha.7",
"@blitzjs/config": "workspace:2.0.0-alpha.26",
"@testing-library/react": "13.0.0",
"@testing-library/react-hooks": "7.0.2",
"@types/cookie": "0.4.1",
"@types/debug": "4.1.7",
"@types/jsonwebtoken": "8.5.8",
"@types/react": "17.0.43",
"@types/react": "18.0.1",
"@types/react-dom": "17.0.14",
"react": "18.0.0",
"react-dom": "18.0.0",

View File

@@ -138,7 +138,10 @@ export const useSession = (options: UseSessionOptions = {}): ClientSession => {
initialState = {...options.initialPublicData, isLoading: false}
} else if (suspense) {
if (isServer) {
throw new Promise((_) => {})
const e = new Error()
e.name = "Rendering Suspense fallback..."
delete e.stack
throw e
} else {
initialState = {...getPublicDataStore().getData(), isLoading: false}
}

View File

@@ -1,3 +1,4 @@
export * from "./auth-sessions"
export * from "./auth-utils"
export * from "./auth-plugin"
export * from "./passport-adapter"

View File

@@ -0,0 +1,204 @@
/* @eslint-disable no-redeclare */
import cookieSession from "cookie-session"
import passport from "passport"
import type {AuthenticateOptions, Strategy} from "passport"
import {isLocalhost} from "./index"
import {
assert,
connectMiddleware,
Ctx,
handleRequestWithMiddleware,
Middleware,
MiddlewareResponse,
secureProxyMiddleware,
} from "blitz"
import {IncomingMessage, ServerResponse} from "http"
import {PublicData, SessionContext} from "../shared"
const isFunction = (functionToCheck: unknown): functionToCheck is Function =>
typeof functionToCheck === "function"
const isVerifyCallbackResult = (value: unknown): value is VerifyCallbackResult =>
typeof value === "object" && value !== null && "publicData" in value
const INTERNAL_REDIRECT_URL_KEY = "_redirectUrl"
export interface BlitzPassportConfigCallbackParams {
ctx: Ctx
req: IncomingMessage
res: ServerResponse
}
export type BlitzPassportConfigCallback = ({
ctx,
req,
res,
}: BlitzPassportConfigCallbackParams) => BlitzPassportConfigObject
export type BlitzPassportConfig = BlitzPassportConfigObject | BlitzPassportConfigCallback
export type BlitzPassportStrategy = {
authenticateOptions?: AuthenticateOptions
strategy: Strategy
}
export type BlitzPassportConfigObject = {
successRedirectUrl?: string
errorRedirectUrl?: string
strategies: BlitzPassportStrategy[]
secureProxy?: boolean
}
export type VerifyCallbackResult = {
publicData: PublicData
privateData?: Record<string, unknown>
redirectUrl?: string
}
export type ApiHandlerIncomingMessage = IncomingMessage & {
query: {
[key: string]: string | string[]
}
}
export type ApiHandler = (
req: ApiHandlerIncomingMessage,
res: MiddlewareResponse & {status: (statusCode: number) => any},
) => void | Promise<void>
export function passportAuth(config: BlitzPassportConfig): ApiHandler {
return async function authHandler(req, res) {
const configObject: BlitzPassportConfigObject = isFunction(config)
? config({ctx: res.blitzCtx, req, res})
: config
const cookieSessionMiddleware = cookieSession({
secret: process.env.SESSION_SECRET_KEY || "default-dev-secret",
secure: process.env.NODE_ENV === "production" && !isLocalhost(req),
})
const passportMiddleware = passport.initialize()
const middleware: Middleware<ApiHandlerIncomingMessage, MiddlewareResponse<Ctx>>[] = [
connectMiddleware(cookieSessionMiddleware as Middleware),
connectMiddleware(passportMiddleware as Middleware),
connectMiddleware(passport.session()),
]
if (configObject.secureProxy) {
middleware.push(secureProxyMiddleware)
}
assert(
req.query.auth,
"req.query.auth is not defined. Page must be named [...auth].ts/js. See more at https://blitzjs.com/docs/passportjs#1-add-the-passport-js-api-route",
)
assert(
Array.isArray(req.query.auth),
"req.query.auth must be an array. Page must be named [...auth].ts/js. See more at https://blitzjs.com/docs/passportjs#1-add-the-passport-js-api-route",
)
if (!req.query.auth.length) {
return res.status(404).end()
}
assert(
configObject.strategies.length,
"No Passport strategies found! Please add at least one strategy.",
)
const blitzStrategy = configObject.strategies.find(
({strategy}) => strategy.name === req.query?.auth?.[0] ?? "",
)
assert(blitzStrategy, `A passport strategy was not found for: ${req.query.auth[0]}`)
const {strategy, authenticateOptions} = blitzStrategy
passport.use(strategy)
const strategyName = strategy.name as string
if (req.query.auth.length === 1) {
console.info(`Starting authentication via ${strategyName}...`)
if (req.query.redirectUrl) {
// eslint-disable-next-line no-shadow
middleware.push(async (req, res, next) => {
const session = res.blitzCtx.session as SessionContext
assert(session, "Missing Blitz sessionMiddleware!")
await session.$setPublicData({
[INTERNAL_REDIRECT_URL_KEY]: req.query.redirectUrl,
} as any)
return next()
})
}
middleware.push(
connectMiddleware(passport.authenticate(strategyName, {...authenticateOptions})),
)
} else if (req.query.auth[1] === "callback") {
console.info(`Processing callback for ${strategyName}...`)
middleware.push(
// eslint-disable-next-line no-shadow
connectMiddleware((req, res, next) => {
const session = res.blitzCtx.session as SessionContext
assert(session, "Missing Blitz sessionMiddleware!")
passport.authenticate(strategyName, async (err: any, result: any) => {
try {
let error = err
if (!error && result === false) {
console.warn(
`Login via ${strategyName} failed - usually this means the user did not authenticate properly with the provider`,
)
error = `Login failed`
}
const redirectUrlFromVerifyResult =
result && typeof result === "object" && result.redirectUrl
let redirectUrl: string =
redirectUrlFromVerifyResult ||
(session.$publicData as any)[INTERNAL_REDIRECT_URL_KEY] ||
(error ? configObject.errorRedirectUrl : configObject.successRedirectUrl) ||
"/"
if (error) {
redirectUrl += "?authError=" + encodeURIComponent(error.toString())
res.setHeader("Location", redirectUrl)
res.statusCode = 302
res.end()
return
}
assert(
typeof result === "object" && result !== null,
`Your '${strategyName}' passport verify callback returned empty data. Ensure you call 'done(null, {publicData: {userId: 1}})' along with any other publicData fields you need)`,
)
assert(
(result as any).publicData,
`'publicData' is missing from your '${strategyName}' passport verify callback. Ensure you call 'done(null, {publicData: {userId: 1}})' along with any other publicData fields you need)`,
)
assert(isVerifyCallbackResult(result), "Passport verify callback is invalid")
delete (result.publicData as any)[INTERNAL_REDIRECT_URL_KEY]
await session.$create(result.publicData, result.privateData)
res.setHeader("Location", redirectUrl)
res.statusCode = 302
res.end()
} catch (error) {
console.error(error)
res.statusCode = 500
res.end()
}
})(req, res, next)
}),
)
}
await handleRequestWithMiddleware<ApiHandlerIncomingMessage, MiddlewareResponse<Ctx>>(
req,
res,
middleware,
)
}
}

View File

@@ -1,5 +1,137 @@
# @blitzjs/next
## 2.0.0-alpha.26
### Patch Changes
- 0e762fb5: export BlitzPage & BlitzLayout types from @blitzjs/next
- @blitzjs/rpc@2.0.0-alpha.26
## 2.0.0-alpha.25
### Patch Changes
- 931156c3: Rename prefetchBlitzQuery to prefetchQuery, add prefetchInfiniteQuery
- b0c21b07: Move blitz config to next.config.js
- @blitzjs/rpc@2.0.0-alpha.25
## 2.0.0-alpha.24
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.24
## 2.0.0-alpha.23
### Patch Changes
- various improvements and fixes
- @blitzjs/rpc@2.0.0-alpha.23
## 2.0.0-alpha.22
### Patch Changes
- c5c727cb: add mounted check inside withBlitz
- 6ff9ec0d: Upgrade @types/react, fix typings inside @blitzjs/next
- da17cc8a: Support `prefetchBlitzQuery` in gSSP and gSP
- - Add mounted check to withBlitz
- Upgrade @types/react, fix typings inside @blitzjs/next
- Support prefetchBlitzQuery in gSP and gSSP
- Add db seed cli command
- Add try/catch to changePassword mutation
- @blitzjs/rpc@2.0.0-alpha.22
## 2.0.0-alpha.21
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.21
## 2.0.0-alpha.20
### Patch Changes
- testing set dist-tag
- Updated dependencies
- @blitzjs/rpc@2.0.0-alpha.20
## 2.0.0-alpha.19
### Patch Changes
- added superjson
- 2150dcc3: Setup SuperJson for GSSP and GSP
- @blitzjs/rpc@2.0.0-alpha.19
## 2.0.0-alpha.18
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.18
## 2.0.0-alpha.17
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.17
## 2.0.0-alpha.16
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.16
## 2.0.0-alpha.15
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.15
## 2.0.0-alpha.14
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.14
## 2.0.0-alpha.13
### Patch Changes
- Fix codegen and postinstall to make work with pnpm
- @blitzjs/rpc@2.0.0-alpha.13
## 2.0.0-alpha.12
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.12
## 2.0.0-alpha.11
### Patch Changes
- Fix postinstall script not being found
- @blitzjs/rpc@2.0.0-alpha.11
## 2.0.0-alpha.10
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.10
## 2.0.0-alpha.9
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.9
## 2.0.0-alpha.8
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.8
## 2.0.0-alpha.7
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/next",
"version": "2.0.0-alpha.7",
"version": "2.0.0-alpha.26",
"scripts": {
"build": "unbuild",
"dev": "pnpm predev && pnpm watch unbuild src --wait=0.2",
@@ -9,7 +9,7 @@
"test": "vitest run",
"test-watch": "vitest",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
"postinstall": "node src/scripts/postinstall.js"
"postinstall": "node scripts/postinstall.js"
},
"main": "./dist/index-server.cjs",
"module": "./dist/index-server.mjs",
@@ -19,16 +19,20 @@
"license": "MIT",
"files": [
"dist/**",
"scripts/**",
"eslint.js"
],
"dependencies": {
"@blitzjs/rpc": "2.0.0-alpha.7",
"@blitzjs/rpc": "2.0.0-alpha.26",
"@types/hoist-non-react-statics": "3.3.1",
"debug": "4.3.3",
"fs-extra": "10.0.1",
"react-query": "3.21.1"
"hoist-non-react-statics": "3.3.2",
"react-query": "3.39.0",
"superjson": "1.8.0"
},
"devDependencies": {
"@blitzjs/config": "workspace:2.0.0-alpha.7",
"@blitzjs/config": "workspace:2.0.0-alpha.26",
"@testing-library/dom": "8.13.0",
"@testing-library/jest-dom": "5.16.3",
"@testing-library/react": "13.0.0",
@@ -36,14 +40,14 @@
"@testing-library/user-event": "13.5.0",
"@types/lodash.frompairs": "4.0.6",
"@types/node": "17.0.16",
"@types/react": "17.0.43",
"@types/react": "18.0.1",
"@types/react-dom": "17.0.14",
"@types/testing-library__react-hooks": "4.0.0",
"blitz": "2.0.0-alpha.7",
"blitz": "2.0.0-alpha.26",
"cross-spawn": "7.0.3",
"find-up": "4.1.0",
"lodash.frompairs": "4.0.1",
"next": "12.1.1",
"next": "12.1.6-canary.17",
"react": "18.0.0",
"react-dom": "18.0.0",
"resolve-from": "5.0.0",

View File

@@ -11,12 +11,12 @@ const stat = promisify(fs.stat)
const debug = require("debug")("blitz:postinstall")
const isInBlitzMonorepo = fs.existsSync(path.join(__dirname, "../../src"))
const isInBlitzMonorepo = fs.existsSync(path.join(__dirname, "../../blitz-next"))
let isInstalledGlobally = isInBlitzMonorepo ? false : true // default
try {
const maybeGlobalBlitzPath = resolveFrom(__dirname, "blitz")
const localBlitzPath = resolveFrom.silent(process.cwd(), "blitz")
const localBlitzPath = resolveFrom.silent(process.cwd(), "blitz/dist/index.cjs")
isInstalledGlobally = maybeGlobalBlitzPath !== localBlitzPath
} catch (error) {
// noop
@@ -27,6 +27,16 @@ async function findNodeModulesRoot(src) {
let root
if (isInBlitzMonorepo) {
root = path.join(src, "node_modules")
} else if (src.includes(".pnpm")) {
const blitzPkgLocation = path.dirname(
(await findUp("package.json", {
cwd: resolveFrom(src, "blitz"),
})) || "",
)
if (!blitzPkgLocation) {
throw new Error("Internal Blitz Error: unable to find 'blitz' package location")
}
root = path.join(blitzPkgLocation, "../../../../")
} else {
const blitzPkgLocation = path.dirname(
(await findUp("package.json", {
@@ -95,8 +105,12 @@ function codegen() {
try {
const packagePath = require.resolve("blitz/package.json")
if (packagePath) {
const blitzPkg = require.resolve("blitz")
return path.join(blitzPkg, "/dist/index.cjs")
const blitzPkg = require.resolve("blitz/dist/index.cjs")
if (blitzPkg.includes(".pnpm")) {
return path.join(blitzPkg, "../../../../../../blitz/dist/index.cjs")
} else {
return path.join(blitzPkg)
}
}
} catch (e) {
//
@@ -323,6 +337,6 @@ function codegen() {
const UNABLE_TO_FIND_POSTINSTALL_TRIGGER_JSON_SCHEMA_ERROR = 'UNABLE_TO_FIND_POSTINSTALL_TRIGGER_JSON_SCHEMA_ERROR'
}
if (!isInstalledGlobally) {
codegen()
}
// if (!isInstalledGlobally) {
codegen()
// }

View File

@@ -5,11 +5,15 @@ import type {
UnionToIntersection,
Simplify,
} from "blitz"
import {AppProps} from "next/app"
import Head from "next/head"
import React from "react"
import {QueryClient, QueryClientProvider} from "react-query"
import {Hydrate, HydrateOptions} from "react-query/hydration"
import {withSuperJSONPage} from "./superjson"
import {Ctx} from "blitz"
import {UrlObject} from "url"
import {AppPropsType} from "next/dist/shared/lib/utils"
import {Router} from "next/router"
export * from "./error-boundary"
export * from "./error-component"
@@ -30,8 +34,12 @@ const buildWithBlitz = <TPlugins extends readonly ClientPlugin<object>[]>(plugin
const BlitzOuterRoot = (props: AppProps) => {
const component = React.useMemo(() => withPlugins(props.Component), [props.Component])
// supress first render flicker
const [mounted, setMounted] = React.useState(false)
React.useEffect(() => {
// Current workaround to fix react 18 suspense error issue
setMounted(true)
// supress first render flicker
setTimeout(() => {
document.documentElement.classList.add("blitz-first-render-complete")
})
@@ -42,29 +50,55 @@ const buildWithBlitz = <TPlugins extends readonly ClientPlugin<object>[]>(plugin
<>
{/* @ts-ignore todo */}
{props.Component.suppressFirstRenderFlicker && <NoPageFlicker />}
<UserAppRoot {...props} Component={component} />
{mounted && (
<React.Suspense fallback="Loading...">
<UserAppRoot {...props} Component={component} />
</React.Suspense>
)}
</>
</BlitzProvider>
)
}
return BlitzOuterRoot
return withSuperJSONPage(BlitzOuterRoot)
}
}
export type BlitzProviderProps = {
children: JSX.Element
client?: QueryClient
contextSharing?: boolean
dehydratedState?: unknown
hydrateOptions?: HydrateOptions
}
interface RouteUrlObject extends Pick<UrlObject, "pathname" | "query"> {
pathname: string
}
type RedirectAuthenticatedTo = string | RouteUrlObject | false
type RedirectAuthenticatedToFnCtx = {
session: Ctx["session"]["$publicData"]
}
type RedirectAuthenticatedToFn = (args: RedirectAuthenticatedToFnCtx) => RedirectAuthenticatedTo
export type BlitzPage<P = {}> = React.ComponentType<P> & {
getLayout?: (component: JSX.Element) => JSX.Element
authenticate?: boolean | {redirectTo?: string}
suppressFirstRenderFlicker?: boolean
redirectAuthenticatedTo?: RedirectAuthenticatedTo | RedirectAuthenticatedToFn
}
export type BlitzLayout<P = {}> = React.ComponentType<P> & {
authenticate?: boolean | {redirectTo?: string | RouteUrlObject}
redirectAuthenticatedTo?: RedirectAuthenticatedTo | RedirectAuthenticatedToFn
}
export type AppProps<P = {}> = AppPropsType<Router, P> & {
Component: BlitzPage
}
const BlitzProvider = ({
client,
contextSharing = false,
dehydratedState,
hydrateOptions,
children,
}: BlitzProviderProps & {children: JSX.Element}) => {
}: BlitzProviderProps) => {
if (globalThis.queryClient) {
return (
<QueryClientProvider

View File

@@ -1,6 +1,27 @@
import {GetServerSideProps, GetStaticProps, NextApiRequest, NextApiResponse} from "next"
import type {Ctx as BlitzCtx, BlitzServerPlugin, Middleware, MiddlewareResponse} from "blitz"
import {
GetServerSideProps,
GetServerSidePropsResult,
GetStaticProps,
GetStaticPropsResult,
NextApiRequest,
NextApiResponse,
} from "next"
import type {
Ctx as BlitzCtx,
BlitzServerPlugin,
Middleware,
MiddlewareResponse,
AsyncFunc,
FirstParam,
AddParameters,
} from "blitz"
import {handleRequestWithMiddleware} from "blitz"
import type {NextConfig} from "next"
import {getQueryKey, getInfiniteQueryKey, installWebpackConfig} from "@blitzjs/rpc"
import {dehydrate} from "@blitzjs/rpc"
import {DefaultOptions, QueryClient} from "react-query"
import {IncomingMessage, ServerResponse} from "http"
import {withSuperJsonProps} from "./superjson"
export * from "./index-browser"
@@ -8,8 +29,8 @@ export * from "./index-browser"
export interface Ctx extends BlitzCtx {}
export interface BlitzNextApiResponse
extends NextApiResponse,
Omit<MiddlewareResponse, keyof NextApiResponse> {}
extends MiddlewareResponse,
Omit<NextApiResponse, keyof MiddlewareResponse> {}
export type NextApiHandler = (
req: NextApiRequest,
@@ -27,10 +48,10 @@ export type BlitzGSSPHandler<TProps> = ({
...args
}: Parameters<GetServerSideProps<TProps>>[0] & {ctx: Ctx}) => ReturnType<GetServerSideProps<TProps>>
export type BlitzGSPHandler = ({
export type BlitzGSPHandler<TProps> = ({
ctx,
...args
}: Parameters<GetStaticProps>[0] & {ctx: Ctx}) => ReturnType<GetServerSideProps>
}: Parameters<GetStaticProps<TProps>>[0] & {ctx: Ctx}) => ReturnType<GetStaticProps<TProps>>
export type BlitzAPIHandler = (
req: Parameters<NextApiHandler>[0],
@@ -45,19 +66,55 @@ export const setupBlitzServer = ({plugins}: SetupBlitzOptions) => {
const gSSP =
<TProps>(handler: BlitzGSSPHandler<TProps>): GetServerSideProps<TProps> =>
async ({req, res, ...rest}) => {
await handleRequestWithMiddleware(req, res, middlewares)
await handleRequestWithMiddleware<IncomingMessage, ServerResponse>(req, res, middlewares)
const ctx = contextMiddleware.reduceRight(
(y, f) => (f ? f(y) : y),
(res as MiddlewareResponse).blitzCtx,
)
return handler({req, res, ctx, ...rest})
let queryClient: null | QueryClient = null
const prefetchQuery: AddParameters<PrefetchQueryFn, [boolean?]> = async (
fn,
input,
defaultOptions = {},
infinite = false,
) => {
queryClient = new QueryClient({defaultOptions})
const queryKey = infinite ? getQueryKey(fn, input) : getInfiniteQueryKey(fn, input)
await queryClient.prefetchQuery(queryKey, () => fn(input, ctx))
}
ctx.prefetchQuery = prefetchQuery
ctx.prefetchInfiniteQuery = (...args) => prefetchQuery(...args, true)
const result = await handler({req, res, ctx, ...rest})
return withSuperJsonProps(withDehydratedState(result, queryClient))
}
const gSP =
(handler: BlitzGSPHandler): GetStaticProps =>
<TProps>(handler: BlitzGSPHandler<TProps>): GetStaticProps<TProps> =>
async (context) => {
const ctx = contextMiddleware.reduceRight((y, f) => (f ? f(y) : y), {} as Ctx)
return handler({...context, ctx: ctx})
let queryClient: null | QueryClient = null
const prefetchQuery: AddParameters<PrefetchQueryFn, [boolean?]> = async (
fn,
input,
defaultOptions = {},
infinite = false,
) => {
queryClient = new QueryClient({defaultOptions})
const queryKey = infinite ? getQueryKey(fn, input) : getInfiniteQueryKey(fn, input)
await queryClient.prefetchQuery(queryKey, () => fn(input, ctx))
}
ctx.prefetchQuery = prefetchQuery
ctx.prefetchInfiniteQuery = (...args) => prefetchQuery(...args, true)
const result = await handler({...context, ctx: ctx})
return withSuperJsonProps(withDehydratedState(result, queryClient))
}
const api =
@@ -74,10 +131,15 @@ export const setupBlitzServer = ({plugins}: SetupBlitzOptions) => {
return {gSSP, gSP, api}
}
import type {NextConfig} from "next"
import {installWebpackConfig} from "@blitzjs/rpc"
export interface BlitzConfig extends NextConfig {
blitz?: {
customServer?: {
hotReload?: boolean
}
}
}
export function withBlitz(nextConfig: NextConfig = {}) {
export function withBlitz(nextConfig: BlitzConfig = {}) {
return Object.assign({}, nextConfig, {
webpack: (config: any, options: any) => {
installWebpackConfig(config)
@@ -88,3 +150,26 @@ export function withBlitz(nextConfig: NextConfig = {}) {
},
} as NextConfig)
}
export type PrefetchQueryFn = <T extends AsyncFunc, TInput = FirstParam<T>>(
resolver: T,
params: TInput,
options?: DefaultOptions,
) => Promise<void>
type Result = Partial<GetServerSidePropsResult<any> & GetStaticPropsResult<any>>
function withDehydratedState<T extends Result>(result: T, queryClient: QueryClient | null) {
if (!queryClient) {
return result
}
const dehydratedProps = dehydrate(queryClient)
return {...result, props: {...("props" in result ? result.props : undefined), dehydratedProps}}
}
declare module "blitz" {
export interface Ctx {
prefetchQuery: PrefetchQueryFn
prefetchInfiniteQuery: PrefetchQueryFn
}
}

View File

@@ -0,0 +1,62 @@
import hoistNonReactStatics from "hoist-non-react-statics"
import type {GetServerSidePropsResult, GetStaticPropsResult} from "next"
import * as React from "react"
import SuperJSON from "superjson"
export type SuperJSONProps<P = any> = P & {
_superjson?: any
}
type Result = Partial<GetServerSidePropsResult<any> & GetStaticPropsResult<any>>
export function withSuperJsonProps<T extends Result>(result: T, exclude: string[] = []) {
if (!("props" in result)) {
return result
}
if (!result.props) {
return result
}
const excludedPropValues = exclude.map((propKey) => {
const value = (result.props as any)[propKey]
delete (result.props as any)[propKey]
return value
})
const {json, meta} = SuperJSON.serialize(result.props)
const props = json as any
if (meta) {
props._superjson = meta
}
exclude.forEach((key, index) => {
const excludedPropValue = excludedPropValues[index]
if (typeof excludedPropValue !== "undefined") {
props[key] = excludedPropValue
}
})
return {
...result,
props,
}
}
export function deserializeProps<P>(serializedProps: SuperJSONProps<P>): P {
const {_superjson, ...props} = serializedProps
return SuperJSON.deserialize({json: props as any, meta: _superjson})
}
export function withSuperJSONPage<P>(
Page: React.ComponentType<P>,
): React.ComponentType<SuperJSONProps<P>> {
function WithSuperJSON(serializedProps: SuperJSONProps<P>) {
return <Page {...deserializeProps<P>(serializedProps)} />
}
hoistNonReactStatics(WithSuperJSON, Page)
return WithSuperJSON
}

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