Compare commits
30 Commits
blitz@2.0.
...
@blitzjs/r
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6fe860512c | ||
|
|
adfe8ff919 | ||
|
|
980869fbc8 | ||
|
|
00f6d5576a | ||
|
|
1945d85db2 | ||
|
|
b0c21b0706 | ||
|
|
931156c352 | ||
|
|
1436e76180 | ||
|
|
c3bb5cd95b | ||
|
|
8b08fe4e38 | ||
|
|
604dc3b345 | ||
|
|
20fb3b9427 | ||
|
|
66ea6ec0cb | ||
|
|
8490b07246 | ||
|
|
f15a519017 | ||
|
|
cfcd3f83df | ||
|
|
adabb11a0c | ||
|
|
909dc76087 | ||
|
|
c5c727cb67 | ||
|
|
6ff9ec0d75 | ||
|
|
da17cc8a24 | ||
|
|
89bf993a1d | ||
|
|
68f129491c | ||
|
|
c096891bf0 | ||
|
|
81b4b41a99 | ||
|
|
e8271d579c | ||
|
|
9e798b152b | ||
|
|
3c6f43a11d | ||
|
|
8107138e2f | ||
|
|
8cb1134d94 |
6
.changeset/big-phones-bow.md
Normal file
6
.changeset/big-phones-bow.md
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"@blitzjs/next": patch
|
||||
"@blitzjs/generator": patch
|
||||
---
|
||||
|
||||
add mounted check inside withBlitz
|
||||
@@ -7,5 +7,5 @@
|
||||
"access": "restricted",
|
||||
"baseBranch": "main",
|
||||
"updateInternalDependencies": "patch",
|
||||
"ignore": ["web", "test-*"]
|
||||
"ignore": ["web", "test-*", "toolkit-app"]
|
||||
}
|
||||
|
||||
5
.changeset/fast-trainers-kneel.md
Normal file
5
.changeset/fast-trainers-kneel.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"blitz": patch
|
||||
---
|
||||
|
||||
Export Zod utils from blitz core package
|
||||
6
.changeset/hot-drinks-approve.md
Normal file
6
.changeset/hot-drinks-approve.md
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"@blitzjs/next": patch
|
||||
"@blitzjs/generator": patch
|
||||
---
|
||||
|
||||
Upgrade @types/react, fix typings inside @blitzjs/next
|
||||
5
.changeset/modern-cameras-pull.md
Normal file
5
.changeset/modern-cameras-pull.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@blitzjs/next": patch
|
||||
---
|
||||
|
||||
Support `prefetchBlitzQuery` in gSSP and gSP
|
||||
5
.changeset/moody-squids-cheer.md
Normal file
5
.changeset/moody-squids-cheer.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"blitz": patch
|
||||
---
|
||||
|
||||
test automated publish
|
||||
5
.changeset/nice-starfishes-live.md
Normal file
5
.changeset/nice-starfishes-live.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@blitzjs/generator": patch
|
||||
---
|
||||
|
||||
add mounted check to app generator template
|
||||
11
.changeset/plenty-bottles-swim.md
Normal file
11
.changeset/plenty-bottles-swim.md
Normal 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
|
||||
5
.changeset/poor-penguins-look.md
Normal file
5
.changeset/poor-penguins-look.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"blitz": patch
|
||||
---
|
||||
|
||||
add `db seed` cli command
|
||||
@@ -13,30 +13,44 @@
|
||||
"@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",
|
||||
"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",
|
||||
"tender-pianos-check",
|
||||
"thirty-countries-build",
|
||||
"twenty-beans-pump",
|
||||
"two-kiwis-help",
|
||||
"unlucky-papayas-sleep",
|
||||
"wicked-ghosts-cough"
|
||||
"weak-suns-shave",
|
||||
"wicked-ghosts-cough",
|
||||
"wise-frogs-give"
|
||||
]
|
||||
}
|
||||
|
||||
5
.changeset/rich-chairs-invent.md
Normal file
5
.changeset/rich-chairs-invent.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@blitzjs/next": patch
|
||||
---
|
||||
|
||||
Rename prefetchBlitzQuery to prefetchQuery, add prefetchInfiniteQuery
|
||||
5
.changeset/small-socks-confess.md
Normal file
5
.changeset/small-socks-confess.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@blitzjs/auth": patch
|
||||
---
|
||||
|
||||
Add passport adapter to @blitzjs/auth
|
||||
7
.changeset/tender-pianos-check.md
Normal file
7
.changeset/tender-pianos-check.md
Normal file
@@ -0,0 +1,7 @@
|
||||
---
|
||||
"blitz": patch
|
||||
"@blitzjs/next": patch
|
||||
"@blitzjs/generator": patch
|
||||
---
|
||||
|
||||
various improvements and fixes
|
||||
5
.changeset/weak-suns-shave.md
Normal file
5
.changeset/weak-suns-shave.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@blitzjs/next": patch
|
||||
---
|
||||
|
||||
Move blitz config to next.config.js
|
||||
5
.changeset/wise-frogs-give.md
Normal file
5
.changeset/wise-frogs-give.md
Normal 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
44
.github/workflows/release.yml
vendored
Normal 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
1
.npmrc
@@ -1,4 +1,5 @@
|
||||
save-exact=true
|
||||
strict-peer-dependencies=false
|
||||
|
||||
public-hoist-pattern[]=secure-password
|
||||
public-hoist-pattern[]=*types*
|
||||
|
||||
@@ -1,5 +1,15 @@
|
||||
# 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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "toolkit-app",
|
||||
"version": "1.0.1-alpha.15",
|
||||
"version": "1.0.1-alpha.16",
|
||||
"scripts": {
|
||||
"start:dev": "pnpm run prisma:start && next dev",
|
||||
"buildapp": "pnpm i && pnpm prisma generate && next build",
|
||||
@@ -29,7 +29,7 @@
|
||||
"@blitzjs/rpc": "workspace:*",
|
||||
"@hookform/resolvers": "2.8.8",
|
||||
"@prisma/client": "3.9.0",
|
||||
"blitz": "workspace:2.0.0-alpha.20",
|
||||
"blitz": "workspace:2.0.0-alpha.25",
|
||||
"next": "12.1.6-canary.17",
|
||||
"prisma": "3.9.0",
|
||||
"react": "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",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { ErrorFallbackProps, ErrorComponent, ErrorBoundary } 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 +27,7 @@ function RootErrorFallback({ error }: ErrorFallbackProps) {
|
||||
function MyApp({ Component, pageProps }: AppProps) {
|
||||
return (
|
||||
<ErrorBoundary FallbackComponent={RootErrorFallback}>
|
||||
<Suspense fallback="Loading...">
|
||||
<Component {...pageProps} />
|
||||
</Suspense>
|
||||
<Component {...pageProps} />
|
||||
</ErrorBoundary>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
27
apps/web/app/queries/getInfiniteUsers.ts
Normal file
27
apps/web/app/queries/getInfiniteUsers.ts
Normal 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,
|
||||
}
|
||||
})
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -5,4 +5,4 @@ const EnhancedPrisma = enhancePrisma(PrismaClient)
|
||||
|
||||
export * from "@prisma/client"
|
||||
const prisma = new EnhancedPrisma()
|
||||
export {prisma}
|
||||
export default prisma
|
||||
@@ -6,5 +6,10 @@ const {withBlitz} = require("@blitzjs/next")
|
||||
module.exports = withBlitz(
|
||||
withBundleAnalyzer({
|
||||
reactStrictMode: true,
|
||||
blitz: {
|
||||
customServer: {
|
||||
hotReload: false,
|
||||
},
|
||||
},
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -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",
|
||||
@@ -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"
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import {ErrorFallbackProps, ErrorComponent, ErrorBoundary} 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 +27,7 @@ function RootErrorFallback({error}: ErrorFallbackProps) {
|
||||
function MyApp({Component, pageProps}: AppProps) {
|
||||
return (
|
||||
<ErrorBoundary FallbackComponent={RootErrorFallback}>
|
||||
<Suspense fallback="Loading...">
|
||||
<Component {...pageProps} />
|
||||
</Suspense>
|
||||
<Component {...pageProps} />
|
||||
</ErrorBoundary>
|
||||
)
|
||||
}
|
||||
|
||||
44
apps/web/pages/api/auth/[...auth].ts
Normal file
44
apps/web/pages/api/auth/[...auth].ts
Normal 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})
|
||||
},
|
||||
),
|
||||
},
|
||||
],
|
||||
}),
|
||||
)
|
||||
@@ -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,
|
||||
|
||||
@@ -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",
|
||||
},
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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},
|
||||
})
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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},
|
||||
})
|
||||
|
||||
@@ -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("")
|
||||
|
||||
@@ -12,7 +12,7 @@ export const getServerSideProps = gSSP<Props>(async ({ctx}) => {
|
||||
props: {
|
||||
userId: session.userId,
|
||||
publicData: session.$publicData,
|
||||
publishedAt: new Date(0)
|
||||
publishedAt: new Date(0),
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
32
apps/web/pages/page-with-inf-prefetch.tsx
Normal file
32
apps/web/pages/page-with-inf-prefetch.tsx
Normal 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
|
||||
28
apps/web/pages/page-with-prefetch.tsx
Normal file
28
apps/web/pages/page-with-prefetch.tsx
Normal 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
|
||||
@@ -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",
|
||||
|
||||
1
integration-tests/qm/.eslintrc.js
Normal file
1
integration-tests/qm/.eslintrc.js
Normal file
@@ -0,0 +1 @@
|
||||
module.exports = require("@blitzjs/config/eslint")
|
||||
3
integration-tests/qm/.gitignore
vendored
Normal file
3
integration-tests/qm/.gitignore
vendored
Normal 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
5
integration-tests/qm/next-env.d.ts
vendored
Normal 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.
|
||||
4
integration-tests/qm/next.config.js
Normal file
4
integration-tests/qm/next.config.js
Normal file
@@ -0,0 +1,4 @@
|
||||
const {withBlitz} = require("@blitzjs/next")
|
||||
module.exports = withBlitz({
|
||||
// update me
|
||||
})
|
||||
34
integration-tests/qm/package.json
Normal file
34
integration-tests/qm/package.json
Normal 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"
|
||||
}
|
||||
}
|
||||
@@ -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\\")"`;
|
||||
@@ -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')"`;
|
||||
55
integration-tests/qm/test/use-mutation.test.tsx
Normal file
55
integration-tests/qm/test/use-mutation.test.tsx
Normal 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()
|
||||
})
|
||||
})
|
||||
})
|
||||
166
integration-tests/qm/test/use-query.test.tsx
Normal file
166
integration-tests/qm/test/use-query.test.tsx
Normal 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")
|
||||
// })
|
||||
// })
|
||||
// })
|
||||
13
integration-tests/qm/tsconfig.json
Normal file
13
integration-tests/qm/tsconfig.json
Normal 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": "."
|
||||
}
|
||||
12
integration-tests/qm/vitest.config.ts
Normal file
12
integration-tests/qm/vitest.config.ts
Normal 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',
|
||||
},
|
||||
})
|
||||
@@ -1,4 +1,4 @@
|
||||
const {withBlitz} = require("@blitzjs/next")
|
||||
module.exports = withBlitz({
|
||||
// update me
|
||||
target: 'experimental-serverless-trace',
|
||||
})
|
||||
|
||||
@@ -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",
|
||||
|
||||
123
integration-tests/utils/blitz-test-utils.tsx
Normal file
123
integration-tests/utils/blitz-test-utils.tsx
Normal 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
|
||||
}
|
||||
@@ -12,6 +12,8 @@ export function waitFor(millis) {
|
||||
return new Promise((resolve) => setTimeout(resolve, millis))
|
||||
}
|
||||
|
||||
export {By}
|
||||
|
||||
const {
|
||||
BROWSER_NAME: browserName = "chrome",
|
||||
BROWSERSTACK,
|
||||
|
||||
@@ -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",
|
||||
|
||||
13
integration-tests/utils/tsconfig.json
Normal file
13
integration-tests/utils/tsconfig.json
Normal 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": "."
|
||||
}
|
||||
@@ -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)))
|
||||
}
|
||||
|
||||
@@ -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 && changeset add && changeset version && pnpm build && 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,7 +26,7 @@
|
||||
"@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.6-canary.17",
|
||||
"only-allow": "1.1.0",
|
||||
|
||||
@@ -1,5 +1,41 @@
|
||||
# @blitzjs/auth
|
||||
|
||||
## 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
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@blitzjs/auth",
|
||||
"version": "2.0.0-alpha.20",
|
||||
"version": "2.0.0-alpha.25",
|
||||
"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.20",
|
||||
"blitz": "2.0.0-alpha.25",
|
||||
"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.20",
|
||||
"@blitzjs/config": "workspace:2.0.0-alpha.25",
|
||||
"@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",
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from "./auth-sessions"
|
||||
export * from "./auth-utils"
|
||||
export * from "./auth-plugin"
|
||||
export * from "./passport-adapter"
|
||||
|
||||
204
packages/blitz-auth/src/server/passport-adapter.ts
Normal file
204
packages/blitz-auth/src/server/passport-adapter.ts
Normal 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,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,46 @@
|
||||
# @blitzjs/next
|
||||
|
||||
## 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
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@blitzjs/next",
|
||||
"version": "2.0.0-alpha.20",
|
||||
"version": "2.0.0-alpha.25",
|
||||
"scripts": {
|
||||
"build": "unbuild",
|
||||
"dev": "pnpm predev && pnpm watch unbuild src --wait=0.2",
|
||||
@@ -23,16 +23,16 @@
|
||||
"eslint.js"
|
||||
],
|
||||
"dependencies": {
|
||||
"@blitzjs/rpc": "2.0.0-alpha.20",
|
||||
"@blitzjs/rpc": "2.0.0-alpha.25",
|
||||
"@types/hoist-non-react-statics": "3.3.1",
|
||||
"debug": "4.3.3",
|
||||
"fs-extra": "10.0.1",
|
||||
"hoist-non-react-statics": "3.3.2",
|
||||
"react-query": "3.21.1",
|
||||
"react-query": "3.39.0",
|
||||
"superjson": "1.8.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@blitzjs/config": "workspace:2.0.0-alpha.20",
|
||||
"@blitzjs/config": "workspace:2.0.0-alpha.25",
|
||||
"@testing-library/dom": "8.13.0",
|
||||
"@testing-library/jest-dom": "5.16.3",
|
||||
"@testing-library/react": "13.0.0",
|
||||
@@ -40,10 +40,10 @@
|
||||
"@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.20",
|
||||
"blitz": "2.0.0-alpha.25",
|
||||
"cross-spawn": "7.0.3",
|
||||
"find-up": "4.1.0",
|
||||
"lodash.frompairs": "4.0.1",
|
||||
|
||||
@@ -10,7 +10,7 @@ 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 {withSuperJSONPage} from "./superjson"
|
||||
|
||||
export * from "./error-boundary"
|
||||
export * from "./error-component"
|
||||
@@ -31,8 +31,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")
|
||||
})
|
||||
@@ -43,7 +47,11 @@ 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>
|
||||
)
|
||||
@@ -53,6 +61,7 @@ const buildWithBlitz = <TPlugins extends readonly ClientPlugin<object>[]>(plugin
|
||||
}
|
||||
|
||||
export type BlitzProviderProps = {
|
||||
children: JSX.Element
|
||||
client?: QueryClient
|
||||
contextSharing?: boolean
|
||||
dehydratedState?: unknown
|
||||
@@ -65,7 +74,7 @@ const BlitzProvider = ({
|
||||
dehydratedState,
|
||||
hydrateOptions,
|
||||
children,
|
||||
}: BlitzProviderProps & {children: JSX.Element}) => {
|
||||
}: BlitzProviderProps) => {
|
||||
if (globalThis.queryClient) {
|
||||
return (
|
||||
<QueryClientProvider
|
||||
|
||||
@@ -1,7 +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 {withSuperJSONPropsGsp, withSuperJSONPropsGssp} from "./superjson"
|
||||
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"
|
||||
|
||||
@@ -9,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,
|
||||
@@ -43,21 +63,59 @@ export const setupBlitzServer = ({plugins}: SetupBlitzOptions) => {
|
||||
const middlewares = plugins.flatMap((p) => p.middlewares)
|
||||
const contextMiddleware = plugins.flatMap((p) => p.contextMiddleware).filter(Boolean)
|
||||
|
||||
const gSSP = <TProps>(handler: BlitzGSSPHandler<TProps>): GetServerSideProps<TProps> =>
|
||||
withSuperJSONPropsGssp<TProps>(async ({req, res, ...rest}) => {
|
||||
await handleRequestWithMiddleware(req, res, middlewares)
|
||||
const gSSP =
|
||||
<TProps>(handler: BlitzGSSPHandler<TProps>): GetServerSideProps<TProps> =>
|
||||
async ({req, res, ...rest}) => {
|
||||
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 gSP = <TProps>(handler: BlitzGSPHandler<TProps>): GetStaticProps<TProps> =>
|
||||
withSuperJSONPropsGsp<TProps>(async (context) => {
|
||||
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 =
|
||||
<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 =
|
||||
(handler: BlitzAPIHandler): NextApiHandler =>
|
||||
@@ -73,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)
|
||||
@@ -87,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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
import hoistNonReactStatics from "hoist-non-react-statics"
|
||||
import type {
|
||||
GetServerSideProps,
|
||||
GetServerSidePropsResult,
|
||||
GetStaticProps,
|
||||
GetStaticPropsResult,
|
||||
} from "next"
|
||||
import type {GetServerSidePropsResult, GetStaticPropsResult} from "next"
|
||||
import * as React from "react"
|
||||
import SuperJSON from "superjson"
|
||||
|
||||
@@ -12,10 +7,9 @@ export type SuperJSONProps<P = any> = P & {
|
||||
_superjson?: any
|
||||
}
|
||||
|
||||
function excludeProps<P>(
|
||||
result: GetServerSidePropsResult<P> | GetStaticPropsResult<P>,
|
||||
exclude: string[] = [],
|
||||
) {
|
||||
type Result = Partial<GetServerSidePropsResult<any> & GetStaticPropsResult<any>>
|
||||
|
||||
export function withSuperJsonProps<T extends Result>(result: T, exclude: string[] = []) {
|
||||
if (!("props" in result)) {
|
||||
return result
|
||||
}
|
||||
@@ -50,27 +44,6 @@ function excludeProps<P>(
|
||||
}
|
||||
}
|
||||
|
||||
export function withSuperJSONPropsGssp<P>(
|
||||
handler: GetServerSideProps<P>,
|
||||
exclude: string[] = [],
|
||||
): GetServerSideProps<SuperJSONProps<P>> {
|
||||
return async function withSuperJSON(...args) {
|
||||
const result = await handler(...args)
|
||||
return excludeProps(result, exclude)
|
||||
}
|
||||
}
|
||||
|
||||
export function withSuperJSONPropsGsp<P>(
|
||||
handler: GetStaticProps<P>,
|
||||
exclude: string[] = [],
|
||||
): GetStaticProps<P> {
|
||||
return async function withSuperJSON(...args) {
|
||||
const result = await handler(...args)
|
||||
|
||||
return excludeProps<any>(result, exclude)
|
||||
}
|
||||
}
|
||||
|
||||
export function deserializeProps<P>(serializedProps: SuperJSONProps<P>): P {
|
||||
const {_superjson, ...props} = serializedProps
|
||||
return SuperJSON.deserialize({json: props as any, meta: _superjson})
|
||||
|
||||
@@ -1,5 +1,46 @@
|
||||
# @blitzjs/rpc
|
||||
|
||||
## 2.0.0-alpha.25
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [1436e761]
|
||||
- Updated dependencies [1436e761]
|
||||
- blitz@2.0.0-alpha.25
|
||||
- @blitzjs/auth@2.0.0-alpha.25
|
||||
|
||||
## 2.0.0-alpha.24
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [8490b072]
|
||||
- blitz@2.0.0-alpha.24
|
||||
- @blitzjs/auth@2.0.0-alpha.24
|
||||
|
||||
## 2.0.0-alpha.23
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies
|
||||
- blitz@2.0.0-alpha.23
|
||||
- @blitzjs/auth@2.0.0-alpha.23
|
||||
|
||||
## 2.0.0-alpha.22
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies
|
||||
- Updated dependencies [89bf993a]
|
||||
- blitz@2.0.0-alpha.22
|
||||
- @blitzjs/auth@2.0.0-alpha.22
|
||||
|
||||
## 2.0.0-alpha.21
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- blitz@2.0.0-alpha.21
|
||||
- @blitzjs/auth@2.0.0-alpha.21
|
||||
|
||||
## 2.0.0-alpha.20
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@blitzjs/rpc",
|
||||
"version": "2.0.0-alpha.20",
|
||||
"version": "2.0.0-alpha.25",
|
||||
"scripts": {
|
||||
"build": "unbuild",
|
||||
"predev": "wait-on -d 250 ../blitz/dist/index-server.d.ts && wait-on -d 250 ../blitz-auth/dist/index-browser.d.ts",
|
||||
@@ -20,21 +20,21 @@
|
||||
"dist/**"
|
||||
],
|
||||
"dependencies": {
|
||||
"@blitzjs/auth": "2.0.0-alpha.20",
|
||||
"@blitzjs/auth": "2.0.0-alpha.25",
|
||||
"b64-lite": "1.4.0",
|
||||
"bad-behavior": "1.0.1",
|
||||
"chalk": "^4.1.0",
|
||||
"debug": "4.3.3",
|
||||
"react-query": "3.21.1",
|
||||
"react-query": "3.39.0",
|
||||
"superjson": "1.8.0",
|
||||
"zod": "3.10.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@blitzjs/config": "workspace:2.0.0-alpha.20",
|
||||
"@blitzjs/config": "workspace:2.0.0-alpha.25",
|
||||
"@types/debug": "4.1.7",
|
||||
"@types/react": "17.0.43",
|
||||
"@types/react": "18.0.1",
|
||||
"@types/react-dom": "17.0.14",
|
||||
"blitz": "2.0.0-alpha.20",
|
||||
"blitz": "2.0.0-alpha.25",
|
||||
"next": "12.1.6-canary.17",
|
||||
"react": "18.0.0",
|
||||
"react-dom": "18.0.0",
|
||||
@@ -43,7 +43,7 @@
|
||||
"watch": "1.0.2"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"blitz": "2.0.0-alpha.20",
|
||||
"blitz": "2.0.0-alpha.25",
|
||||
"next": "*"
|
||||
},
|
||||
"publishConfig": {
|
||||
|
||||
20
packages/blitz-rpc/test/blitz-test-utils.ts
Normal file
20
packages/blitz-rpc/test/blitz-test-utils.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
// 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
|
||||
}
|
||||
98
packages/blitz-rpc/test/unit/react-query-utils.unit.test.ts
Normal file
98
packages/blitz-rpc/test/unit/react-query-utils.unit.test.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
/**
|
||||
* @vitest-environment jsdom
|
||||
*/
|
||||
import {assert, expect, test, beforeEach, describe, spyOn, it} from "vitest"
|
||||
|
||||
import {queryClient, invalidateQuery, setQueryData} from "../../src/data-client"
|
||||
|
||||
import {getQueryCacheFunctions} from "../../src/data-client/react-query-utils"
|
||||
import {buildQueryRpc} from "../blitz-test-utils"
|
||||
|
||||
// eslint-disable-next-line require-await
|
||||
const isEmpty = async (arg: string): Promise<boolean> => {
|
||||
return Boolean(arg)
|
||||
}
|
||||
|
||||
describe("getQueryCacheFunctions", () => {
|
||||
const spyRefetchQueries = spyOn(queryClient, "invalidateQueries")
|
||||
|
||||
beforeEach(() => {
|
||||
spyRefetchQueries.mockReset()
|
||||
})
|
||||
|
||||
it("returns a setQueryData function with working options", async () => {
|
||||
window.requestIdleCallback = undefined as any
|
||||
|
||||
const {setQueryData} = getQueryCacheFunctions(buildQueryRpc(isEmpty), "a")
|
||||
expect(setQueryData).toBeTruthy()
|
||||
await setQueryData(true)
|
||||
expect(spyRefetchQueries).toBeCalledTimes(1)
|
||||
await setQueryData(true, {refetch: false})
|
||||
expect(spyRefetchQueries).toBeCalledTimes(1)
|
||||
await setQueryData(true, {refetch: true})
|
||||
expect(spyRefetchQueries).toBeCalledTimes(2)
|
||||
})
|
||||
|
||||
it("works even when requestIdleCallback is undefined", async () => {
|
||||
window.requestIdleCallback = undefined as any
|
||||
const {setQueryData} = getQueryCacheFunctions(buildQueryRpc(isEmpty), "a")
|
||||
expect(setQueryData).toBeTruthy()
|
||||
await setQueryData(true)
|
||||
expect(spyRefetchQueries).toBeCalledTimes(1)
|
||||
await setQueryData(true, {refetch: false})
|
||||
expect(spyRefetchQueries).toBeCalledTimes(1)
|
||||
await setQueryData(true, {refetch: true})
|
||||
expect(spyRefetchQueries).toBeCalledTimes(2)
|
||||
})
|
||||
})
|
||||
|
||||
describe("invalidateQuery", () => {
|
||||
const spyRefetchQueries = spyOn(queryClient, "invalidateQueries")
|
||||
|
||||
beforeEach(() => {
|
||||
spyRefetchQueries.mockReset()
|
||||
})
|
||||
|
||||
it("invalidates a query given resolver and params", async () => {
|
||||
await invalidateQuery(buildQueryRpc(isEmpty), "a")
|
||||
expect(spyRefetchQueries).toBeCalledTimes(1)
|
||||
const calledWith = spyRefetchQueries.mock.calls[0][0] as any
|
||||
// json of the queryKey is "a"
|
||||
expect(calledWith[1].json).toEqual("a")
|
||||
})
|
||||
})
|
||||
|
||||
describe("setQueryData", () => {
|
||||
const spyRefetchQueries = spyOn(queryClient, "invalidateQueries")
|
||||
const spySetQueryData = spyOn(queryClient, "setQueryData")
|
||||
|
||||
beforeEach(() => {
|
||||
spyRefetchQueries.mockReset()
|
||||
spySetQueryData.mockReset()
|
||||
})
|
||||
|
||||
it("without refetch will not invalidate queries", async () => {
|
||||
await setQueryData(buildQueryRpc(isEmpty), "params", "newValue", {
|
||||
refetch: false,
|
||||
})
|
||||
expect(spyRefetchQueries).toBeCalledTimes(0)
|
||||
expect(spySetQueryData).toBeCalledTimes(1)
|
||||
|
||||
const calledWith = spySetQueryData.mock.calls[0] as Array<any>
|
||||
expect(calledWith[0][1].json).toEqual("params")
|
||||
expect(calledWith[1]).toEqual("newValue")
|
||||
})
|
||||
|
||||
it("will invalidate queries by default", async () => {
|
||||
await setQueryData(buildQueryRpc(isEmpty), "params", "newValue")
|
||||
expect(spyRefetchQueries).toBeCalledTimes(1)
|
||||
expect(spySetQueryData).toBeCalledTimes(1)
|
||||
|
||||
const invalidateCalledWith = spyRefetchQueries.mock.calls[0][0] as any
|
||||
expect(invalidateCalledWith[1].json).toEqual("params")
|
||||
|
||||
const calledWith = spySetQueryData.mock.calls[0] as Array<any>
|
||||
expect(calledWith[0][1].json).toEqual("params")
|
||||
expect(calledWith[1]).toEqual("newValue")
|
||||
})
|
||||
})
|
||||
@@ -1,5 +1,50 @@
|
||||
# blitz
|
||||
|
||||
## 2.0.0-alpha.25
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 1436e761: Export Zod utils from blitz core package
|
||||
- @blitzjs/generator@2.0.0-alpha.25
|
||||
|
||||
## 2.0.0-alpha.24
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 8490b072: test automated publish
|
||||
- @blitzjs/generator@2.0.0-alpha.24
|
||||
|
||||
## 2.0.0-alpha.23
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- various improvements and fixes
|
||||
- Updated dependencies
|
||||
- @blitzjs/generator@2.0.0-alpha.23
|
||||
|
||||
## 2.0.0-alpha.22
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- - 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
|
||||
- 89bf993a: add `db seed` cli command
|
||||
- Updated dependencies [c5c727cb]
|
||||
- Updated dependencies [6ff9ec0d]
|
||||
- Updated dependencies [81b4b41a]
|
||||
- Updated dependencies
|
||||
- @blitzjs/generator@2.0.0-alpha.22
|
||||
|
||||
## 2.0.0-alpha.21
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies
|
||||
- @blitzjs/generator@2.0.0-alpha.21
|
||||
|
||||
## 2.0.0-alpha.20
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "blitz",
|
||||
"version": "2.0.0-alpha.20",
|
||||
"version": "2.0.0-alpha.25",
|
||||
"scripts": {
|
||||
"build": "unbuild",
|
||||
"dev": "watch unbuild src --wait=0.2",
|
||||
@@ -23,7 +23,7 @@
|
||||
"blitz": "bin/blitz"
|
||||
},
|
||||
"dependencies": {
|
||||
"@blitzjs/generator": "2.0.0-alpha.20",
|
||||
"@blitzjs/generator": "2.0.0-alpha.25",
|
||||
"arg": "5.0.1",
|
||||
"chalk": "^4.1.0",
|
||||
"console-table-printer": "2.10.0",
|
||||
@@ -44,10 +44,12 @@
|
||||
"resolve-cwd": "3.0.0",
|
||||
"resolve-from": "5.0.0",
|
||||
"superjson": "1.8.0",
|
||||
"ts-node": "10.7.0",
|
||||
"tsconfig-paths": "4.0.0",
|
||||
"tslog": "3.3.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@blitzjs/config": "workspace:2.0.0-alpha.20",
|
||||
"@blitzjs/config": "workspace:2.0.0-alpha.25",
|
||||
"@types/cookie": "0.4.1",
|
||||
"@types/cross-spawn": "6.0.2",
|
||||
"@types/debug": "4.1.7",
|
||||
@@ -58,7 +60,7 @@
|
||||
"@types/node-fetch": "2.6.1",
|
||||
"@types/npm-which": "3.0.1",
|
||||
"@types/prompts": "2.0.14",
|
||||
"@types/react": "17.0.43",
|
||||
"@types/react": "18.0.1",
|
||||
"@types/react-dom": "17.0.14",
|
||||
"@types/test-listen": "1.1.0",
|
||||
"express": "4.17.3",
|
||||
|
||||
90
packages/blitz/src/cli/commands/db.ts
Normal file
90
packages/blitz/src/cli/commands/db.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
import {CliCommand} from "../index"
|
||||
import arg from "arg"
|
||||
import {join} from "path"
|
||||
import {REGISTER_INSTANCE} from "ts-node"
|
||||
import chalk from "chalk"
|
||||
|
||||
const args = arg(
|
||||
{
|
||||
// Types
|
||||
"--help": Boolean,
|
||||
"--env": String,
|
||||
"--file": String,
|
||||
|
||||
// Aliases
|
||||
"-h": "--help",
|
||||
"-e": "--env",
|
||||
"-f": "--file",
|
||||
},
|
||||
{
|
||||
permissive: true,
|
||||
},
|
||||
)
|
||||
|
||||
const runSeed = async (seedBasePath: string) => {
|
||||
if (!process[REGISTER_INSTANCE]) {
|
||||
// During blitz interal dev, oclif automaticaly sets up ts-node so we have to check
|
||||
require("ts-node").register({compilerOptions: {module: "commonjs"}})
|
||||
}
|
||||
require("tsconfig-paths/register")
|
||||
|
||||
const seedPath = join(process.cwd(), seedBasePath)
|
||||
const dbPath = join(process.cwd(), "db/index")
|
||||
console.log(chalk.magenta("Seeding database"))
|
||||
|
||||
let seeds: Function | undefined
|
||||
try {
|
||||
seeds = require(seedPath).default
|
||||
if (seeds === undefined) {
|
||||
throw new Error(`Couldn't find default export from ${seedBasePath}`)
|
||||
}
|
||||
} catch (err) {
|
||||
console.log(chalk.red(`Couldn't import default from ${seedBasePath}`))
|
||||
throw err
|
||||
}
|
||||
|
||||
try {
|
||||
console.log("\n" + "Seeding...")
|
||||
seeds && (await seeds())
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
throw err
|
||||
}
|
||||
|
||||
const db = require(dbPath).db
|
||||
await db.$disconnect()
|
||||
console.log("Done Seeding")
|
||||
}
|
||||
|
||||
const db: CliCommand = async () => {
|
||||
let filePath = "db/seeds"
|
||||
|
||||
if (args["--file"]) {
|
||||
filePath = args["--file"]
|
||||
}
|
||||
|
||||
if (args["--help"]) {
|
||||
console.log(`Run database commands
|
||||
${chalk.bold("seed")} Generated seeded data in database via Prisma.
|
||||
`)
|
||||
}
|
||||
|
||||
if (args["_"].slice(1)[0] && args["_"].slice(1)[0] === "seed") {
|
||||
try {
|
||||
return await runSeed(filePath)
|
||||
} catch (err) {
|
||||
console.log(chalk.red("Could not seed database:"))
|
||||
console.log(err)
|
||||
process.exit(1)
|
||||
}
|
||||
} else {
|
||||
if (!args["--help"]) {
|
||||
console.log("\nThat command is no longer available..")
|
||||
console.log("For any prisma related commands, use the `blitz prisma` command instead:")
|
||||
console.log("\n `blitz prisma COMMAND`\n")
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
export {db}
|
||||
@@ -27,6 +27,7 @@ const commands: {[command: string]: () => Promise<CliCommand>} = {
|
||||
new: () => import("./commands/new").then((i) => i.newApp),
|
||||
generate: () => import("./commands/generate").then((i) => i.generate),
|
||||
codegen: () => import("./commands/codegen").then((i) => i.codegen),
|
||||
db: () => import("./commands/db").then((i) => i.db),
|
||||
}
|
||||
|
||||
const args = arg(commonArgs, {
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
startCustomServer,
|
||||
buildCustomServer,
|
||||
} from "./next-utils"
|
||||
import {readBlitzConfig} from "../../server-utils"
|
||||
|
||||
export async function build(config: ServerConfig) {
|
||||
const {rootFolder, nextBin, watch} = await normalize(config)
|
||||
@@ -21,11 +22,8 @@ export async function dev(config: ServerConfig) {
|
||||
if (customServerExists()) {
|
||||
console.log("Using your custom server")
|
||||
|
||||
// todo
|
||||
// const {loadConfigProduction} = await import("next/dist/server/config-shared")
|
||||
// const blitzConfig = loadConfigProduction(config.rootFolder)
|
||||
// const watch = blitzConfig.customServer?.hotReload ?? true
|
||||
const watch = true
|
||||
const blitzConfig = readBlitzConfig(rootFolder)
|
||||
const watch = blitzConfig.customServer?.hotReload ?? true
|
||||
|
||||
await startCustomServer(rootFolder, config, {watch})
|
||||
} else {
|
||||
|
||||
@@ -82,3 +82,4 @@ export * from "./utils"
|
||||
export * from "./ts-utils"
|
||||
export * from "./types"
|
||||
export * from "./errors"
|
||||
export * from "./zod-utils"
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import {IncomingMessage, ServerResponse} from "http"
|
||||
import {compose, Ctx, Middleware, MiddlewareNext, MiddlewareResponse} from "./index-server"
|
||||
|
||||
export async function handleRequestWithMiddleware(
|
||||
req: IncomingMessage,
|
||||
res: ServerResponse,
|
||||
middleware: Middleware[],
|
||||
export async function handleRequestWithMiddleware<
|
||||
Req extends IncomingMessage = IncomingMessage,
|
||||
Res extends ServerResponse = ServerResponse,
|
||||
>(
|
||||
req: Req,
|
||||
res: Res,
|
||||
middleware: Middleware<Req, Res>[],
|
||||
{
|
||||
throwOnError = true,
|
||||
stackPrintOnError = true,
|
||||
@@ -13,8 +16,8 @@ export async function handleRequestWithMiddleware(
|
||||
stackPrintOnError?: boolean
|
||||
} = {},
|
||||
) {
|
||||
if (!(res as MiddlewareResponse).blitzCtx) {
|
||||
;(res as MiddlewareResponse).blitzCtx = {} as Ctx
|
||||
if (!(res as unknown as MiddlewareResponse).blitzCtx) {
|
||||
;(res as unknown as MiddlewareResponse).blitzCtx = {} as Ctx
|
||||
}
|
||||
if (!(res as any)._blitz) {
|
||||
;(res as any)._blitz = {}
|
||||
@@ -23,7 +26,7 @@ export async function handleRequestWithMiddleware(
|
||||
let handler = compose(middleware)
|
||||
|
||||
try {
|
||||
await handler(req as IncomingMessage, res as MiddlewareResponse, (error) => {
|
||||
await handler(req, res, (error) => {
|
||||
if (error) {
|
||||
throw error
|
||||
}
|
||||
@@ -111,12 +114,10 @@ export async function handleRequestWithMiddleware(
|
||||
* If the middleware function doesn't declare receiving the `next` callback
|
||||
* assume that it's synchronous and invoke `next` ourselves
|
||||
*/
|
||||
function noCallbackHandler(
|
||||
req: IncomingMessage,
|
||||
res: MiddlewareResponse,
|
||||
next: MiddlewareNext,
|
||||
middleware: Middleware,
|
||||
) {
|
||||
export function noCallbackHandler<
|
||||
Req extends IncomingMessage = IncomingMessage,
|
||||
Res = MiddlewareResponse,
|
||||
>(req: Req, res: Res, next: MiddlewareNext, middleware: Middleware<Req, Res>) {
|
||||
// Cast to any to call with two arguments for connect compatibility
|
||||
;(middleware as any)(req, res)
|
||||
return next()
|
||||
@@ -127,12 +128,10 @@ function noCallbackHandler(
|
||||
* the Promise when it's called. If it's never called, the middleware stack
|
||||
* completion will stall
|
||||
*/
|
||||
function withCallbackHandler(
|
||||
req: IncomingMessage,
|
||||
res: MiddlewareResponse,
|
||||
next: MiddlewareNext,
|
||||
middleware: Middleware,
|
||||
) {
|
||||
export function withCallbackHandler<
|
||||
Req extends IncomingMessage = IncomingMessage,
|
||||
Res = MiddlewareResponse,
|
||||
>(req: Req, res: Res, next: MiddlewareNext, middleware: Middleware<Req, Res>) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// Rule doesn't matter since we are inside new Promise()
|
||||
//eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||
@@ -148,13 +147,14 @@ function withCallbackHandler(
|
||||
* given middleware function declares at least 3 parameters, i.e. includes
|
||||
* the `next` callback function
|
||||
*/
|
||||
export function connectMiddleware(
|
||||
middleware: Middleware<IncomingMessage, ServerResponse>,
|
||||
): Middleware<IncomingMessage, MiddlewareResponse> {
|
||||
export function connectMiddleware<
|
||||
Req extends IncomingMessage = IncomingMessage,
|
||||
Res extends MiddlewareResponse = MiddlewareResponse,
|
||||
>(middleware: Middleware<Req, Res>): Middleware<Req, Res> {
|
||||
const handler = middleware.length < 3 ? noCallbackHandler : withCallbackHandler
|
||||
return function connectHandler(req: IncomingMessage, res, next) {
|
||||
return function connectHandler(req: Req, res, next) {
|
||||
return handler(req, res, next, middleware)
|
||||
} as Middleware<IncomingMessage, MiddlewareResponse>
|
||||
} as Middleware<Req, Res>
|
||||
}
|
||||
|
||||
export const secureProxyMiddleware: Middleware<
|
||||
|
||||
12
packages/blitz/src/server-utils.ts
Normal file
12
packages/blitz/src/server-utils.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import * as fs from "fs"
|
||||
import * as path from "path"
|
||||
|
||||
export function readBlitzConfig(rootFolder: string = process.cwd()) {
|
||||
const nextConfigFile = fs.readFileSync(path.join(rootFolder, "next.config.js"), {
|
||||
encoding: "utf8",
|
||||
flag: "r",
|
||||
})
|
||||
const nextConfig = eval(nextConfigFile)
|
||||
|
||||
return nextConfig.blitz || {}
|
||||
}
|
||||
@@ -31,3 +31,8 @@ export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) ex
|
||||
: never
|
||||
|
||||
export type Simplify<T> = {[P in keyof T]: T[P]}
|
||||
|
||||
export type AddParameters<
|
||||
TFunction extends (...args: any) => any,
|
||||
TParameters extends [...args: any],
|
||||
> = (...args: [...Parameters<TFunction>, ...TParameters]) => ReturnType<TFunction>
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import {Middleware} from "./index-server"
|
||||
import * as path from "path"
|
||||
import * as fs from "fs"
|
||||
|
||||
export function assert(condition: any, message: string): asserts condition {
|
||||
if (!condition) throw new Error(message)
|
||||
@@ -74,7 +76,7 @@ export const setCookie = (name: string, value: string, expires: string) => {
|
||||
}
|
||||
export const deleteCookie = (name: string) => setCookie(name, "", "Thu, 01 Jan 1970 00:00:01 GMT")
|
||||
|
||||
export function compose(middleware: Middleware[]) {
|
||||
export function compose(middleware: Middleware<any, any>[]) {
|
||||
if (!Array.isArray(middleware)) {
|
||||
throw new TypeError("Middleware stack must be an array!")
|
||||
}
|
||||
|
||||
@@ -1,5 +1,15 @@
|
||||
# @blitzjs/config
|
||||
|
||||
## 2.0.0-alpha.25
|
||||
|
||||
## 2.0.0-alpha.24
|
||||
|
||||
## 2.0.0-alpha.23
|
||||
|
||||
## 2.0.0-alpha.22
|
||||
|
||||
## 2.0.0-alpha.21
|
||||
|
||||
## 2.0.0-alpha.20
|
||||
|
||||
## 2.0.0-alpha.19
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@blitzjs/config",
|
||||
"private": true,
|
||||
"version": "2.0.0-alpha.20",
|
||||
"version": "2.0.0-alpha.25",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@typescript-eslint/eslint-plugin": "5.9.1",
|
||||
|
||||
@@ -1,5 +1,34 @@
|
||||
# @blitzjs/generator
|
||||
|
||||
## 2.0.0-alpha.25
|
||||
|
||||
## 2.0.0-alpha.24
|
||||
|
||||
## 2.0.0-alpha.23
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- various improvements and fixes
|
||||
|
||||
## 2.0.0-alpha.22
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- c5c727cb: add mounted check inside withBlitz
|
||||
- 6ff9ec0d: Upgrade @types/react, fix typings inside @blitzjs/next
|
||||
- 81b4b41a: add mounted check to app generator template
|
||||
- - 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
|
||||
|
||||
## 2.0.0-alpha.21
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- use latest tag for generator template on rpc & auth packages
|
||||
|
||||
## 2.0.0-alpha.20
|
||||
|
||||
## 2.0.0-alpha.19
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@blitzjs/generator",
|
||||
"version": "2.0.0-alpha.20",
|
||||
"version": "2.0.0-alpha.25",
|
||||
"scripts": {
|
||||
"dev": "watch unbuild src --wait=0.2",
|
||||
"build": "unbuild && pnpm build:templates",
|
||||
@@ -45,7 +45,7 @@
|
||||
"vinyl": "2.2.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@blitzjs/config": "2.0.0-alpha.20",
|
||||
"@blitzjs/config": "2.0.0-alpha.25",
|
||||
"@juanm04/cpx": "2.0.1",
|
||||
"@types/babel__core": "7.1.19",
|
||||
"@types/diff": "5.0.2",
|
||||
@@ -55,7 +55,7 @@
|
||||
"@types/mem-fs-editor": "7.0.1",
|
||||
"@types/pluralize": "0.0.29",
|
||||
"@types/prettier": "2.4.4",
|
||||
"@types/react": "17.0.43",
|
||||
"@types/react": "18.0.1",
|
||||
"@types/react-dom": "17.0.14",
|
||||
"@types/vinyl": "2.0.6",
|
||||
"@typescript-eslint/eslint-plugin": "5.9.1",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { NotFoundError } from "blitz"
|
||||
import { NotFoundError, AuthenticationError } from "blitz"
|
||||
import { db } from "db"
|
||||
import { authenticateUser } from "./login"
|
||||
import { ChangePassword } from "../validations"
|
||||
@@ -12,7 +12,14 @@ export default resolver.pipe(
|
||||
const user = await db.user.findFirst({ where: { id: ctx.session.userId as number } })
|
||||
if (!user) throw new NotFoundError()
|
||||
|
||||
await authenticateUser(user.email, currentPassword)
|
||||
try {
|
||||
await authenticateUser(user.email, currentPassword)
|
||||
} catch (error: any) {
|
||||
if (error instanceof AuthenticationError) {
|
||||
throw new Error("Invalid Password")
|
||||
}
|
||||
throw error
|
||||
}
|
||||
|
||||
const hashedPassword = await SecurePassword.hash(newPassword.trim())
|
||||
await db.user.update({
|
||||
|
||||
@@ -22,9 +22,9 @@
|
||||
"*.{js}": ["eslint --fix"]
|
||||
},
|
||||
"dependencies": {
|
||||
"@blitzjs/auth": "alpha",
|
||||
"@blitzjs/auth": "latest",
|
||||
"@blitzjs/next": "alpha",
|
||||
"@blitzjs/rpc": "alpha",
|
||||
"@blitzjs/rpc": "latest",
|
||||
"@prisma/client": "3.9.0",
|
||||
"blitz": "alpha",
|
||||
"next": "12.1.1",
|
||||
@@ -38,7 +38,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",
|
||||
|
||||
@@ -22,9 +22,9 @@
|
||||
"*.{js,ts,tsx}": ["eslint --fix"]
|
||||
},
|
||||
"dependencies": {
|
||||
"@blitzjs/auth": "alpha",
|
||||
"@blitzjs/auth": "latest",
|
||||
"@blitzjs/next": "alpha",
|
||||
"@blitzjs/rpc": "alpha",
|
||||
"@blitzjs/rpc": "latest",
|
||||
"@prisma/client": "3.9.0",
|
||||
"blitz": "alpha",
|
||||
"next": "12.1.1",
|
||||
@@ -38,7 +38,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",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import {ErrorFallbackProps, ErrorComponent, ErrorBoundary} 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 +27,7 @@ function RootErrorFallback({error}: ErrorFallbackProps) {
|
||||
function MyApp({Component, pageProps}: AppProps) {
|
||||
return (
|
||||
<ErrorBoundary FallbackComponent={RootErrorFallback}>
|
||||
<Suspense fallback="Loading...">
|
||||
<Component {...pageProps} />
|
||||
</Suspense>
|
||||
<Component {...pageProps} />
|
||||
</ErrorBoundary>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
"@next/bundle-analyzer": "12.0.8",
|
||||
"@types/jest": "27.4.1",
|
||||
"@types/node": "17.0.16",
|
||||
"@types/react": "17.0.43",
|
||||
"@types/react": "18.0.1",
|
||||
"eslint": "7.32.0",
|
||||
"husky": "7.0.4",
|
||||
"jest": "27.5.1",
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
"@next/bundle-analyzer": "12.0.8",
|
||||
"@types/jest": "27.4.1",
|
||||
"@types/node": "17.0.16",
|
||||
"@types/react": "17.0.43",
|
||||
"@types/react": "18.0.1",
|
||||
"eslint": "7.32.0",
|
||||
"husky": "7.0.4",
|
||||
"jest": "27.5.1",
|
||||
|
||||
@@ -25,8 +25,8 @@
|
||||
"@typescript-eslint/parser": "5.9.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@blitzjs/config": "2.0.0-alpha.20",
|
||||
"@types/react": "17.0.43",
|
||||
"@blitzjs/config": "2.0.0-alpha.25",
|
||||
"@types/react": "18.0.1",
|
||||
"@types/react-dom": "17.0.14",
|
||||
"react": "18.0.0",
|
||||
"typescript": "^4.5.3",
|
||||
|
||||
1124
pnpm-lock.yaml
generated
1124
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user