1
0
mirror of synced 2026-02-04 12:08:33 -05:00

Compare commits

...

15 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
58 changed files with 1238 additions and 221 deletions

View File

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

View File

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

View File

@@ -22,11 +22,14 @@
"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",
@@ -35,8 +38,10 @@
"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",
@@ -45,6 +50,7 @@
"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/next": patch
---
Rename prefetchBlitzQuery to prefetchQuery, add prefetchInfiniteQuery

View File

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

View File

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

View File

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

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

@@ -29,7 +29,7 @@
"@blitzjs/rpc": "workspace:*",
"@hookform/resolvers": "2.8.8",
"@prisma/client": "3.9.0",
"blitz": "workspace:2.0.0-alpha.23",
"blitz": "workspace:2.0.0-alpha.26",
"next": "12.1.6-canary.17",
"prisma": "3.9.0",
"react": "18.0.0",

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -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.6-canary.17",
"passport-mock-strategy": "2.0.0",
"passport-twitter": "1.0.4",
"prisma": "3.9.0",
"react": "18.0.0",
"react-dom": "18.0.0",

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,9 +1,9 @@
import {api} from "app/blitz-server"
import {prisma} from "../../prisma/index"
import db from "db"
import {SecurePassword} from "@blitzjs/auth"
export const authenticateUser = async (email: string, password: string) => {
const user = await prisma.user.findFirst({where: {email}})
const user = await db.user.findFirst({where: {email}})
if (!user) throw new Error("Authentication Error")
const result = await SecurePassword.verify(user.hashedPassword, password)
@@ -11,7 +11,7 @@ export const authenticateUser = async (email: string, password: string) => {
if (result === SecurePassword.VALID_NEEDS_REHASH) {
// Upgrade hashed password with a more secure hash
const improvedHash = await SecurePassword.hash(password)
await prisma.user.update({where: {id: user.id}, data: {hashedPassword: improvedHash}})
await db.user.update({where: {id: user.id}, data: {hashedPassword: improvedHash}})
}
const {hashedPassword, ...rest} = user

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

@@ -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

@@ -3,9 +3,9 @@ import {gSSP} from "app/blitz-server"
import getUsers from "app/queries/getUsers"
export const getServerSideProps = gSSP(async ({ctx}) => {
const {prefetchBlitzQuery} = ctx
const {prefetchQuery} = ctx
await prefetchBlitzQuery(getUsers, {})
await prefetchQuery(getUsers, {}, {})
return {props: {}}
})

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

@@ -18,6 +18,7 @@
"clean": "turbo run clean && rm -rf node_modules",
"format": "prettier --write \"**/*.{ts,tsx,md}\"",
"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": {

View File

@@ -1,5 +1,26 @@
# @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

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/auth",
"version": "2.0.0-alpha.23",
"version": "2.0.0-alpha.26",
"scripts": {
"build": "unbuild",
"predev": "wait-on -d 250 ../blitz/dist/index-server.d.ts",
@@ -21,21 +21,25 @@
],
"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.23",
"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.23",
"@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",

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,26 @@
# @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

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/next",
"version": "2.0.0-alpha.23",
"version": "2.0.0-alpha.26",
"scripts": {
"build": "unbuild",
"dev": "pnpm predev && pnpm watch unbuild src --wait=0.2",
@@ -23,7 +23,7 @@
"eslint.js"
],
"dependencies": {
"@blitzjs/rpc": "2.0.0-alpha.23",
"@blitzjs/rpc": "2.0.0-alpha.26",
"@types/hoist-non-react-statics": "3.3.1",
"debug": "4.3.3",
"fs-extra": "10.0.1",
@@ -32,7 +32,7 @@
"superjson": "1.8.0"
},
"devDependencies": {
"@blitzjs/config": "workspace:2.0.0-alpha.23",
"@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",
@@ -43,7 +43,7 @@
"@types/react": "18.0.1",
"@types/react-dom": "17.0.14",
"@types/testing-library__react-hooks": "4.0.0",
"blitz": "2.0.0-alpha.23",
"blitz": "2.0.0-alpha.26",
"cross-spawn": "7.0.3",
"find-up": "4.1.0",
"lodash.frompairs": "4.0.1",

View File

@@ -5,12 +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"
@@ -68,6 +71,27 @@ export type BlitzProviderProps = {
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,

View File

@@ -13,8 +13,14 @@ import type {
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"
@@ -23,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,
@@ -60,20 +66,28 @@ 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,
)
let queryClient: null | QueryClient = null
ctx.prefetchBlitzQuery = async (fn, input, defaultOptions = {}) => {
const prefetchQuery: AddParameters<PrefetchQueryFn, [boolean?]> = async (
fn,
input,
defaultOptions = {},
infinite = false,
) => {
queryClient = new QueryClient({defaultOptions})
const queryKey = getQueryKey(fn, input)
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))
}
@@ -84,13 +98,21 @@ export const setupBlitzServer = ({plugins}: SetupBlitzOptions) => {
const ctx = contextMiddleware.reduceRight((y, f) => (f ? f(y) : y), {} as Ctx)
let queryClient: null | QueryClient = null
ctx.prefetchBlitzQuery = async (fn, input, defaultOptions = {}) => {
const prefetchQuery: AddParameters<PrefetchQueryFn, [boolean?]> = async (
fn,
input,
defaultOptions = {},
infinite = false,
) => {
queryClient = new QueryClient({defaultOptions})
const queryKey = getQueryKey(fn, input)
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))
}
@@ -109,12 +131,15 @@ export const setupBlitzServer = ({plugins}: SetupBlitzOptions) => {
return {gSSP, gSP, api}
}
import type {NextConfig} from "next"
import {getQueryKey, installWebpackConfig} from "@blitzjs/rpc"
import {dehydrate} from "@blitzjs/rpc"
import {DefaultOptions, QueryClient} from "react-query"
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)
@@ -126,7 +151,7 @@ export function withBlitz(nextConfig: NextConfig = {}) {
} as NextConfig)
}
type PrefetchQueryFn = <T extends AsyncFunc, TInput = FirstParam<T>>(
export type PrefetchQueryFn = <T extends AsyncFunc, TInput = FirstParam<T>>(
resolver: T,
params: TInput,
options?: DefaultOptions,
@@ -144,6 +169,7 @@ function withDehydratedState<T extends Result>(result: T, queryClient: QueryClie
declare module "blitz" {
export interface Ctx {
prefetchBlitzQuery: PrefetchQueryFn
prefetchQuery: PrefetchQueryFn
prefetchInfiniteQuery: PrefetchQueryFn
}
}

View File

@@ -1,5 +1,29 @@
# @blitzjs/rpc
## 2.0.0-alpha.26
### Patch Changes
- @blitzjs/auth@2.0.0-alpha.26
- blitz@2.0.0-alpha.26
## 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

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/rpc",
"version": "2.0.0-alpha.23",
"version": "2.0.0-alpha.26",
"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,7 +20,7 @@
"dist/**"
],
"dependencies": {
"@blitzjs/auth": "2.0.0-alpha.23",
"@blitzjs/auth": "2.0.0-alpha.26",
"b64-lite": "1.4.0",
"bad-behavior": "1.0.1",
"chalk": "^4.1.0",
@@ -30,11 +30,11 @@
"zod": "3.10.1"
},
"devDependencies": {
"@blitzjs/config": "workspace:2.0.0-alpha.23",
"@blitzjs/config": "workspace:2.0.0-alpha.26",
"@types/debug": "4.1.7",
"@types/react": "18.0.1",
"@types/react-dom": "17.0.14",
"blitz": "2.0.0-alpha.23",
"blitz": "2.0.0-alpha.26",
"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.23",
"blitz": "2.0.0-alpha.26",
"next": "*"
},
"publishConfig": {

View File

@@ -1,5 +1,25 @@
# blitz
## 2.0.0-alpha.26
### Patch Changes
- @blitzjs/generator@2.0.0-alpha.26
## 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

View File

@@ -1,6 +1,6 @@
{
"name": "blitz",
"version": "2.0.0-alpha.23",
"version": "2.0.0-alpha.26",
"scripts": {
"build": "unbuild",
"dev": "watch unbuild src --wait=0.2",
@@ -23,7 +23,7 @@
"blitz": "bin/blitz"
},
"dependencies": {
"@blitzjs/generator": "2.0.0-alpha.23",
"@blitzjs/generator": "2.0.0-alpha.26",
"arg": "5.0.1",
"chalk": "^4.1.0",
"console-table-printer": "2.10.0",
@@ -49,7 +49,7 @@
"tslog": "3.3.1"
},
"devDependencies": {
"@blitzjs/config": "workspace:2.0.0-alpha.23",
"@blitzjs/config": "workspace:2.0.0-alpha.26",
"@types/cookie": "0.4.1",
"@types/cross-spawn": "6.0.2",
"@types/debug": "4.1.7",

View File

@@ -82,3 +82,4 @@ export * from "./utils"
export * from "./ts-utils"
export * from "./types"
export * from "./errors"
export * from "./zod-utils"

View File

@@ -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<

View File

@@ -2,11 +2,11 @@ import * as fs from "fs"
import * as path from "path"
export function readBlitzConfig(rootFolder: string = process.cwd()) {
const packageJsonFile = fs.readFileSync(path.join(rootFolder, "package.json"), {
const nextConfigFile = fs.readFileSync(path.join(rootFolder, "next.config.js"), {
encoding: "utf8",
flag: "r",
})
const packageJson = JSON.parse(packageJsonFile)
const nextConfig = eval(nextConfigFile)
return packageJson.blitz || {}
return nextConfig.blitz || {}
}

View File

@@ -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>

View File

@@ -76,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!")
}

View File

@@ -1,5 +1,11 @@
# @blitzjs/config
## 2.0.0-alpha.26
## 2.0.0-alpha.25
## 2.0.0-alpha.24
## 2.0.0-alpha.23
## 2.0.0-alpha.22

View File

@@ -1,7 +1,7 @@
{
"name": "@blitzjs/config",
"private": true,
"version": "2.0.0-alpha.23",
"version": "2.0.0-alpha.26",
"license": "MIT",
"dependencies": {
"@typescript-eslint/eslint-plugin": "5.9.1",

View File

@@ -1,5 +1,11 @@
# @blitzjs/generator
## 2.0.0-alpha.26
## 2.0.0-alpha.25
## 2.0.0-alpha.24
## 2.0.0-alpha.23
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/generator",
"version": "2.0.0-alpha.23",
"version": "2.0.0-alpha.26",
"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.23",
"@blitzjs/config": "2.0.0-alpha.26",
"@juanm04/cpx": "2.0.1",
"@types/babel__core": "7.1.19",
"@types/diff": "5.0.2",

View File

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

View File

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

View File

@@ -25,7 +25,7 @@
"@typescript-eslint/parser": "5.9.1"
},
"devDependencies": {
"@blitzjs/config": "2.0.0-alpha.23",
"@blitzjs/config": "2.0.0-alpha.26",
"@types/react": "18.0.1",
"@types/react-dom": "17.0.14",
"react": "18.0.0",

745
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff