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

Compare commits

...

21 Commits

Author SHA1 Message Date
Dillon Raphael
9674efc0bf alpha.9 2022-04-28 19:07:56 -04:00
Dillon Raphael
666a3ae3e6 fix cli versioning 2022-04-28 17:35:54 -04:00
Dillon Raphael
c9cf7adc33 alpha.7 changeset 2022-04-28 17:18:04 -04:00
Dillon Raphael
f4c2234c4d add index.cjs to blitz externals 2022-04-28 17:12:55 -04:00
Dillon Raphael
82916b21c2 remove config from changeset 2022-04-28 15:19:52 -04:00
Dillon Raphael
bf1b2c8244 add alpha.6 changeset 2022-04-28 15:07:05 -04:00
Dillon Raphael
043c3498d0 Add routes manifest (#3317)
* Export routes from @blitzjs/next

* change to setupBlitzClient & setupBlitzServer in integrationtests

* update README

* a+d fixing stuff
2022-04-28 10:33:58 -04:00
Aleksandra
a2ebdbe7d6 make blitz CMD run the associated .bin file (#3310) 2022-04-27 11:38:55 +02:00
Dillon Raphael
de4e8084ef Fix generated blitz rpc api route in app generator 2022-04-26 12:45:27 -04:00
Dillon Raphael
bb9eaed520 remove withBundleAnalyzer 2022-04-26 12:15:10 -04:00
Dillon Raphael
ffe85b5ab6 fix toolkit-app example 2022-04-26 12:08:45 -04:00
Dillon Raphael
6edeed7c5a Change export to db instead of prisma in login mutation for app template 2022-04-26 12:02:19 -04:00
Dillon Raphael
1d9d890d9c use projectName variable for postInstallSteps in new app generator 2022-04-20 17:21:38 -04:00
Dillon Raphael
b8f51a2354 remove name flag from new app generator cli 2022-04-20 17:09:19 -04:00
Dillon Raphael
6552b11b94 Make server and client setup consistent with naming 2022-04-20 16:25:36 -04:00
Dillon Raphael
d1dd2bc56e fix generator typescript config to make customTsParser work 2022-04-20 15:51:57 -04:00
Dillon Raphael
09b732860d export eslint file from blitz next package for use in app generator 2022-04-20 15:28:28 -04:00
Dillon Raphael
ccb6cfd2a0 Merge branch 'main' of github.com:blitz-js/blitz
merge
2022-04-20 15:14:05 -04:00
Dillon Raphael
502b8f7820 rename blitz rpc api route during precommit 2022-04-20 15:13:43 -04:00
Aleksandra
3888d7018a Add rpc resolver and update template app (#3307) 2022-04-20 14:33:41 +02:00
Dillon Raphael
02f7822437 Prisma CLI command (#3305)
* blitz prisma command & tidy up templates

* update lockfile

* update lockfile

* hardcode versions

* update ci pnpm version

* update ci pnpm version

* revert ci pnpm version

* fresh clone with updated pnpm-lock

* update packages
2022-04-15 18:38:53 -04:00
95 changed files with 1790 additions and 522 deletions

View File

@@ -0,0 +1,9 @@
---
"blitz": patch
"@blitzjs/next": patch
"@blitzjs/auth": patch
"@blitzjs/rpc": patch
"@blitzjs/generator": patch
---
fix route manifest codegen

View File

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

View File

@@ -16,10 +16,14 @@
"toolkit-app": "1.0.0"
},
"changesets": [
"great-months-train",
"lovely-colts-share",
"nine-onions-admire",
"ninety-pets-heal",
"poor-peas-lick",
"silent-colts-reply",
"ten-rivers-burn",
"thirty-countries-build",
"twenty-beans-pump"
]
}

View File

@@ -0,0 +1,5 @@
---
"blitz": patch
---
added index.cjs to blitz externals

View File

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

1
.gitignore vendored
View File

@@ -62,6 +62,7 @@ examples/auth2
.idea
.ultra.cache.json
db.sqlite-journal
**/db/db.sqlite
test/integration/**/db.json
test/**/*/out
test/**/blitz-env.d.ts

View File

@@ -174,6 +174,7 @@ Your financial contributions help ensure Blitz continues to be developed and mai
<tr>
<td align="center"><a href="https://twitter.com/flybayer"><img src="https://avatars3.githubusercontent.com/u/8813276?v=4" width="100px;" alt=""/><br /><sub><b>Brandon Bayer</b></sub></a><br />Creator</td>
<td align="center"><a href="http://aleksandra.codes"><img src="https://avatars.githubusercontent.com/u/9019397?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Aleksandra Sikora</b></sub></a><br />Lead Maintainer</td>
<td align="center"><a href="http://twitter.com/dillonraphael"><img src="https://avatars.githubusercontent.com/u/3496193?v=4" width="100px;" alt=""/><br /><sub><b>Dillon Raphael</b></sub></a><br />Senior Maintainer</td>
</tr>
</table>

View File

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

View File

@@ -1,5 +1,49 @@
# toolkit-app
## 1.0.1-alpha.4
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.9
- @blitzjs/auth@2.0.0-alpha.9
- @blitzjs/next@2.0.0-alpha.9
- @blitzjs/rpc@2.0.0-alpha.9
- @blitzjs/config@2.0.0-alpha.9
## 1.0.1-alpha.3
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.8
- @blitzjs/auth@2.0.0-alpha.8
- @blitzjs/next@2.0.0-alpha.8
- @blitzjs/rpc@2.0.0-alpha.8
- @blitzjs/config@2.0.0-alpha.8
## 1.0.1-alpha.2
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.7
- @blitzjs/auth@2.0.0-alpha.7
- @blitzjs/next@2.0.0-alpha.7
- @blitzjs/rpc@2.0.0-alpha.7
- @blitzjs/config@2.0.0-alpha.7
## 1.0.1-alpha.1
### Patch Changes
- Updated dependencies
- @blitzjs/next@2.0.0-alpha.6
- blitz@2.0.0-alpha.6
- @blitzjs/auth@2.0.0-alpha.6
- @blitzjs/rpc@2.0.0-alpha.6
- @blitzjs/config@2.0.0-alpha.6
## 1.0.1-alpha.0
### Patch Changes

View File

@@ -1,26 +1,25 @@
import { NotFoundError, Ctx } from "blitz"
import { prisma } from "db"
import { NotFoundError } from "blitz"
import { db } from "db"
import { authenticateUser } from "./login"
import { ChangePassword } from "../validations"
import { resolver } from "@blitzjs/rpc"
import { SecurePassword } from "@blitzjs/auth"
export default async function changePassword(input, ctx: Ctx) {
ChangePassword.parse(input)
ctx.session.$isAuthorized()
export default resolver.pipe(
resolver.zod(ChangePassword),
resolver.authorize(),
async ({ currentPassword, newPassword }, ctx) => {
const user = await db.user.findFirst({ where: { id: ctx.session.userId as number } })
if (!user) throw new NotFoundError()
const user = await prisma.user.findFirst({
where: {
id: ctx.session.userId as number,
},
})
await authenticateUser(user.email, currentPassword)
if (!user) throw new NotFoundError()
await authenticateUser(user.email, input.currentPassword)
const hashedPassword = await SecurePassword.hash(newPassword.trim())
await db.user.update({
where: { id: user.id },
data: { hashedPassword },
})
const hashedPassword = await SecurePassword.hash(input.newPassword.trim())
await prisma.user.update({
where: { id: user.id },
data: { hashedPassword },
})
}
return true
}
)

View File

@@ -1,15 +1,14 @@
import { prisma } from "db"
import { generateToken, hash256 } from "@blitzjs/auth"
import { resolver } from "@blitzjs/rpc"
import { db } from "db"
import { forgotPasswordMailer } from "mailers/forgotPasswordMailer"
import { ForgotPassword } from "../validations"
import { Ctx } from "@blitzjs/next"
const RESET_PASSWORD_TOKEN_EXPIRATION_IN_HOURS = 4
export default async function forgotPassword(input, ctx: Ctx) {
ForgotPassword.parse(input)
export default resolver.pipe(resolver.zod(ForgotPassword), async ({ email }) => {
// 1. Get the user
const user = await prisma.user.findFirst({ where: { email: input.email.toLowerCase() } })
const user = await db.user.findFirst({ where: { email: email.toLowerCase() } })
// 2. Generate the token and expiration date.
const token = generateToken()
@@ -20,9 +19,9 @@ export default async function forgotPassword(input, ctx: Ctx) {
// 3. If user with this email was found
if (user) {
// 4. Delete any existing password reset tokens
await prisma.token.deleteMany({ where: { type: "RESET_PASSWORD", userId: user.id } })
await db.token.deleteMany({ where: { type: "RESET_PASSWORD", userId: user.id } })
// 5. Save this new token in the database.
await prisma.token.create({
await db.token.create({
data: {
user: { connect: { id: user.id } },
type: "RESET_PASSWORD",
@@ -40,4 +39,4 @@ export default async function forgotPassword(input, ctx: Ctx) {
// 8. Return the same result whether a password reset email was sent or not
return
}
})

View File

@@ -1,11 +1,13 @@
import { AuthenticationError } from "blitz"
import { prisma } from "db"
import { Login } from "../validations"
import { SecurePassword } from "@blitzjs/auth"
import { resolver } from "@blitzjs/rpc"
import { AuthenticationError } from "blitz"
import { db } from "db"
import { Role } from "types"
import { Login } from "../validations"
export const authenticateUser = async (rawEmail: string, rawPassword: string) => {
const { email, password } = Login.parse({ email: rawEmail, password: rawPassword })
const user = await prisma.user.findFirst({ where: { email } })
const user = await db.user.findFirst({ where: { email } })
if (!user) throw new AuthenticationError()
const result = await SecurePassword.verify(user.hashedPassword, password)
@@ -13,16 +15,18 @@ export const authenticateUser = async (rawEmail: string, rawPassword: 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
return rest
}
export default async function login(input, ctx) {
const user = await authenticateUser(input.email, input.password)
await ctx.session.$create({ userId: user.id, role: user.role })
export default resolver.pipe(resolver.zod(Login), async ({ email, password }, ctx) => {
// This throws an error if credentials are invalid
const user = await authenticateUser(email, password)
await ctx.session.$create({ userId: user.id, role: user.role as Role })
return user
}
})

View File

@@ -1,5 +1,5 @@
import { SecurePassword, hash256 } from "@blitzjs/auth"
import { prisma } from "db"
import { db } from "db"
import { ResetPassword } from "../validations"
import login from "./login"
@@ -12,7 +12,7 @@ export default async function resetPassword(input, ctx) {
ResetPassword.parse(input)
// 1. Try to find this token in the database
const hashedToken = hash256(input.token)
const possibleToken = await prisma.token.findFirst({
const possibleToken = await db.token.findFirst({
where: { hashedToken, type: "RESET_PASSWORD" },
include: { user: true },
})
@@ -24,7 +24,7 @@ export default async function resetPassword(input, ctx) {
const savedToken = possibleToken
// 3. Delete token so it can't be used again
await prisma.token.delete({ where: { id: savedToken.id } })
await db.token.delete({ where: { id: savedToken.id } })
// 4. If token has expired, error
if (savedToken.expiresAt < new Date()) {
@@ -33,13 +33,13 @@ export default async function resetPassword(input, ctx) {
// 5. Since token is valid, now we can update the user's password
const hashedPassword = await SecurePassword.hash(input.password.trim())
const user = await prisma.user.update({
const user = await db.user.update({
where: { id: savedToken.userId },
data: { hashedPassword },
})
// 6. Revoke all existing login sessions for this user
await prisma.session.deleteMany({ where: { userId: user.id } })
await db.session.deleteMany({ where: { userId: user.id } })
// 7. Now log the user in with the new credentials
await login({ email: user.email, password: input.password }, ctx)

View File

@@ -1,4 +1,4 @@
import { prisma } from "db"
import { db } from "db"
import { SecurePassword } from "@blitzjs/auth"
export default async function signup(input, ctx) {
@@ -6,7 +6,7 @@ export default async function signup(input, ctx) {
const hashedPassword = await SecurePassword.hash((input.password as string) || "test-password")
const email = (input.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,8 @@
import { AuthClientPlugin } from "@blitzjs/auth"
import { setupClient } from "@blitzjs/next"
import { setupBlitzClient } from "@blitzjs/next"
import { BlitzRpcPlugin } from "@blitzjs/rpc"
const { withBlitz } = setupClient({
export const { withBlitz } = setupBlitzClient({
plugins: [
AuthClientPlugin({
cookiePrefix: "web-cookie-prefix",
@@ -16,5 +16,3 @@ const { withBlitz } = setupClient({
}),
],
})
export { withBlitz }

View File

@@ -1,9 +1,9 @@
import { setupBlitz } from "@blitzjs/next"
import { setupBlitzServer } from "@blitzjs/next"
import { AuthServerPlugin, PrismaStorage } from "@blitzjs/auth"
import { prisma as db } from "../db/index"
import { db } from "db"
import { simpleRolesIsAuthorized } from "@blitzjs/auth"
const { gSSP, gSP, api } = setupBlitz({
const { gSSP, gSP, api } = setupBlitzServer({
plugins: [
AuthServerPlugin({
cookiePrefix: "web-cookie-prefix",

View File

@@ -1,10 +1,10 @@
import { Ctx } from "blitz"
import { prisma } from "db"
import { db } from "db"
export default async function getCurrentUser(_ = null, { session }: Ctx) {
if (!session.userId) return null
const user = await prisma.user.findFirst({
const user = await db.user.findFirst({
where: { id: session.userId as number },
select: { id: true, name: true, email: true, role: true },
})

View File

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

View File

@@ -1,6 +1,6 @@
{
"name": "toolkit-app",
"version": "1.0.1-alpha.0",
"version": "1.0.1-alpha.4",
"scripts": {
"start:dev": "pnpm run prisma:start && next dev",
"buildapp": "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.5",
"blitz": "workspace:2.0.0-alpha.9",
"next": "12.1.1",
"prisma": "3.9.0",
"react": "18.0.0",

View File

@@ -20,5 +20,5 @@
"tsBuildInfoFile": ".tsbuildinfo"
},
"exclude": ["node_modules", "**/*.e2e.ts", "cypress"],
"include": ["blitz-env.d.ts", "**/*.ts", "**/*.tsx"]
"include": ["blitz-env.d.ts", "**/*.ts", "**/*.tsx", "types"]
}

15
apps/toolkit-app/types.ts Normal file
View File

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

View File

@@ -1,8 +1,8 @@
import {AuthClientPlugin} from "@blitzjs/auth"
import {setupClient} from "@blitzjs/next"
import {setupBlitzClient} from "@blitzjs/next"
import {BlitzRpcPlugin} from "@blitzjs/rpc"
const {withBlitz} = setupClient({
const {withBlitz} = setupBlitzClient({
plugins: [
AuthClientPlugin({
cookiePrefix: "webapp-cookie-prefix",

View File

@@ -1,9 +1,9 @@
import {setupBlitz} from "@blitzjs/next"
import {setupBlitzServer} from "@blitzjs/next"
import {AuthServerPlugin, PrismaStorage} from "@blitzjs/auth"
import {prisma as db} from "../prisma/index"
import {simpleRolesIsAuthorized} from "@blitzjs/auth"
const {gSSP, gSP, api} = setupBlitz({
const {gSSP, gSP, api} = setupBlitzServer({
plugins: [
AuthServerPlugin({
cookiePrefix: "webapp-cookie-prefix",

View File

@@ -1,7 +1,7 @@
import {AuthClientPlugin} from "@blitzjs/auth"
import {setupClient} from "@blitzjs/next"
import {setupBlitzClient} from "@blitzjs/next"
const {withBlitz} = setupClient({
const {withBlitz} = setupBlitzClient({
plugins: [
AuthClientPlugin({
cookiePrefix: "auth-tests-cookie-prefix",

View File

@@ -1,9 +1,9 @@
import {setupBlitz} from "@blitzjs/next"
import {setupBlitzServer} from "@blitzjs/next"
import {AuthServerPlugin, PrismaStorage} from "@blitzjs/auth"
import {simpleRolesIsAuthorized} from "@blitzjs/auth"
import {prisma as db} from "../prisma/index"
const {gSSP, gSP, api} = setupBlitz({
const {gSSP, gSP, api} = setupBlitzServer({
plugins: [
AuthServerPlugin({
cookiePrefix: "auth-tests-cookie-prefix",

View File

@@ -1,5 +1,34 @@
# @blitzjs/auth
## 2.0.0-alpha.9
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.9
## 2.0.0-alpha.8
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.8
## 2.0.0-alpha.7
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.7
## 2.0.0-alpha.6
### Patch Changes
- fix route manifest codegen
- Updated dependencies
- blitz@2.0.0-alpha.6
## 2.0.0-alpha.5
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/auth",
"version": "2.0.0-alpha.5",
"version": "2.0.0-alpha.9",
"scripts": {
"build": "unbuild",
"predev": "wait-on -d 250 ../blitz/dist/index-server.d.ts",
@@ -24,7 +24,7 @@
"@types/secure-password": "3.1.1",
"b64-lite": "1.4.0",
"bad-behavior": "1.0.1",
"blitz": "workspace:*",
"blitz": "2.0.0-alpha.9",
"cookie": "0.4.1",
"debug": "4.3.3",
"http": "0.0.1-security",
@@ -35,7 +35,7 @@
"url": "0.11.0"
},
"devDependencies": {
"@blitzjs/config": "workspace:*",
"@blitzjs/config": "workspace:2.0.0-alpha.9",
"@testing-library/react": "13.0.0",
"@testing-library/react-hooks": "7.0.2",
"@types/cookie": "0.4.1",

View File

@@ -2,10 +2,4 @@ import "./global"
export * from "./client"
export * from "./shared/constants"
export type {
SessionContextBase,
SessionContext,
AuthenticatedSessionContext,
ClientSession,
AuthenticatedClientSession,
} from "./shared/types"
export * from "./shared/types"

View File

@@ -1,5 +1,31 @@
# @blitzjs/next
## 2.0.0-alpha.9
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.9
## 2.0.0-alpha.8
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.8
## 2.0.0-alpha.7
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.7
## 2.0.0-alpha.6
### Patch Changes
- fix route manifest codegen
- Updated dependencies
- @blitzjs/rpc@2.0.0-alpha.6
## 2.0.0-alpha.5
### Patch Changes

View File

@@ -2,7 +2,7 @@ import {BuildConfig} from "unbuild"
const config: BuildConfig = {
entries: ["./src/index-browser", "./src/index-server"],
externals: ["index-browser.cjs", "index-browser.mjs", "blitz"],
externals: ["index-browser.cjs", "index-browser.mjs", "blitz", ".blitz"],
declaration: true,
rollup: {
emitCJS: true,

View File

@@ -0,0 +1,29 @@
module.exports = {
extends: ["eslint-config-next", "prettier"],
ignorePatterns: ["*.d.ts"],
settings: {
next: {
rootDir: ["./apps/*/", "./packages/*/"],
},
},
rules: {
"@next/next/no-html-link-for-pages": "off",
},
overrides: [
{
files: ["**/*.ts?(x)"],
plugins: ["@typescript-eslint"],
parserOptions: {
project: "./tsconfig.json",
},
rules: {
"@typescript-eslint/no-floating-promises": "error",
"no-use-before-define": "off",
"@typescript-eslint/no-use-before-define": ["off"],
"no-redeclare": "off",
"@typescript-eslint/no-redeclare": ["error"],
"react/display-name": "off",
},
},
],
}

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/next",
"version": "2.0.0-alpha.5",
"version": "2.0.0-alpha.9",
"scripts": {
"build": "unbuild",
"dev": "pnpm predev && pnpm watch unbuild src --wait=0.2",
@@ -8,7 +8,8 @@
"lint": "eslint . --fix",
"test": "vitest run",
"test-watch": "vitest",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
"postinstall": "node src/scripts/postinstall.js"
},
"main": "./dist/index-server.cjs",
"module": "./dist/index-server.mjs",
@@ -17,16 +18,17 @@
"sideEffects": false,
"license": "MIT",
"files": [
"dist/**"
"dist/**",
"eslint.js"
],
"dependencies": {
"@blitzjs/rpc": "2.0.0-alpha.5",
"@blitzjs/rpc": "2.0.0-alpha.9",
"debug": "4.3.3",
"fs-extra": "10.0.1",
"react-query": "3.21.1"
},
"devDependencies": {
"@blitzjs/config": "workspace:*",
"@blitzjs/config": "workspace:2.0.0-alpha.9",
"@testing-library/dom": "8.13.0",
"@testing-library/jest-dom": "5.16.3",
"@testing-library/react": "13.0.0",
@@ -37,11 +39,14 @@
"@types/react": "17.0.43",
"@types/react-dom": "17.0.14",
"@types/testing-library__react-hooks": "4.0.0",
"blitz": "workspace:*",
"blitz": "2.0.0-alpha.9",
"cross-spawn": "7.0.3",
"find-up": "4.1.0",
"lodash.frompairs": "4.0.1",
"next": "12.1.1",
"react": "18.0.0",
"react-dom": "18.0.0",
"resolve-from": "5.0.0",
"ts-jest": "27.1.4",
"typescript": "^4.5.3",
"unbuild": "0.6.9",

View File

@@ -13,6 +13,7 @@ import {Hydrate, HydrateOptions} from "react-query/hydration"
export * from "./error-boundary"
export * from "./error-component"
export {Routes} from ".blitz"
const compose =
(...rest: BlitzProviderType[]) =>
@@ -88,7 +89,7 @@ export type PluginsExports<TPlugins extends readonly ClientPlugin<object>[]> = S
>
>
const setupClient = <TPlugins extends readonly ClientPlugin<object>[]>({
const setupBlitzClient = <TPlugins extends readonly ClientPlugin<object>[]>({
plugins,
}: {
plugins: TPlugins
@@ -121,7 +122,7 @@ const setupClient = <TPlugins extends readonly ClientPlugin<object>[]>({
}
}
export {setupClient}
export {setupBlitzClient}
const customCSS = `
body::before {

View File

@@ -38,7 +38,7 @@ export type BlitzAPIHandler = (
ctx: Ctx,
) => ReturnType<NextApiHandler>
export const setupBlitz = ({plugins}: SetupBlitzOptions) => {
export const setupBlitzServer = ({plugins}: SetupBlitzOptions) => {
const middlewares = plugins.flatMap((p) => p.middlewares)
const contextMiddleware = plugins.flatMap((p) => p.contextMiddleware).filter(Boolean)

View File

@@ -0,0 +1,3 @@
exports.Routes = {
ThisFileHasNotYetBeenGeneratedPleaseRunBlitzCodeGen: (query) => ({pathname: "⚡️", query}),
}

View File

@@ -0,0 +1,6 @@
import type {ParsedUrlQueryInput} from "querystring"
import type {UrlObject} from "url"
export const Routes: {
ThisFileHasNotYetBeenGeneratedPleaseRunBlitzCodeGen(query?: ParsedUrlQueryInput): UrlObject
}

View File

@@ -0,0 +1,3 @@
exports.Routes = {
ThisFileHasNotYetBeenGeneratedPleaseRunBlitzCodeGen: (query) => ({pathname: "⚡️", query}),
}

View File

@@ -0,0 +1,328 @@
const childProcess = require("cross-spawn")
const {promisify} = require("util")
const fs = require("fs")
const path = require("path")
const resolveFrom = require("resolve-from")
const findUp = require("find-up")
const copyFile = promisify(fs.copyFile)
const mkdir = promisify(fs.mkdir)
const stat = promisify(fs.stat)
const debug = require("debug")("blitz:postinstall")
const isInBlitzMonorepo = fs.existsSync(path.join(__dirname, "../../src"))
let isInstalledGlobally = isInBlitzMonorepo ? false : true // default
try {
const maybeGlobalBlitzPath = resolveFrom(__dirname, "blitz")
const localBlitzPath = resolveFrom.silent(process.cwd(), "blitz")
isInstalledGlobally = maybeGlobalBlitzPath !== localBlitzPath
} catch (error) {
// noop
}
// todo: we should reuse `findNodeModulesRoot` from /nextjs/packages/next/build/routes.ts
async function findNodeModulesRoot(src) {
let root
if (isInBlitzMonorepo) {
root = path.join(src, "node_modules")
} else {
const blitzPkgLocation = path.dirname(
(await findUp("package.json", {
cwd: resolveFrom(src, "blitz"),
})) || "",
)
if (!blitzPkgLocation) {
throw new Error("Internal Blitz Error: unable to find 'blitz' package location")
}
root = path.join(blitzPkgLocation, "../")
}
return path.join(root, ".blitz")
}
/*
Adapted from https://github.com/prisma/prisma/blob/974cbeff4a7f616137ce540d0ec88a2a86365892/src/packages/client/scripts/postinstall.js
*/
function codegen() {
async function main() {
if (process.env.INIT_CWD) {
process.chdir(process.env.INIT_CWD) // necessary, because npm chooses __dirname as process.cwd()
// in the postinstall hook
}
await ensureEmptyDotBlitz()
const localPath = getLocalPackagePath()
// Only execute if !localpath
const installedGlobally = localPath ? undefined : await isInstalledGlobally()
debug({
localPath,
installedGlobally,
init_cwd: process.env.INIT_CWD,
})
try {
if (localPath) {
await run("node", [
localPath,
"codegen",
"--postinstall",
doubleQuote(getPostInstallTrigger()),
])
return
}
if (installedGlobally) {
await run("blitz", ["codegen", "--postinstall", doubleQuote(getPostInstallTrigger())])
return
}
} catch (e) {
// if exit code = 1 do not print
if (e && e !== 1) {
console.error(e)
}
debug(e)
}
if (!localPath && !installedGlobally) {
console.error(`Please install Blitz CLI. You can install it with "npm add -D blitz".`)
}
}
function getLocalPackagePath() {
try {
const packagePath = require.resolve("blitz/package.json")
if (packagePath) {
const blitzPkg = require.resolve("blitz")
return path.join(blitzPkg, "/dist/index.cjs")
}
} catch (e) {
//
}
return null
}
async function isInstalledGlobally() {
try {
await run("blitz", ["-v"], process.cwd(), ["ignore"])
return true
} catch (e) {
return false
}
}
if (!process.env.BLITZ_SKIP_POSTINSTALL_GENERATE) {
main()
.catch((e) => {
console.error(e)
process.exit(0)
})
.finally(() => {
debug(`postinstall trigger: ${getPostInstallTrigger()}`)
})
}
function run(cmd, params, cwd = process.cwd(), stdio = ["pipe", "inherit", "inherit"]) {
const child = childProcess.spawn(cmd, params, {
stdio,
cwd,
})
return new Promise((resolve, reject) => {
child.on("close", () => {
resolve()
})
child.on("exit", (code) => {
if (code === 0) {
resolve()
} else {
reject(code)
}
})
child.on("error", () => {
reject()
})
})
}
async function ensureEmptyDotBlitz() {
try {
const dotBlitzDir = isInBlitzMonorepo
? path.join(process.cwd(), "node_modules/.blitz")
: await findNodeModulesRoot(__dirname)
await makeDir(dotBlitzDir)
const defaultIndexJsPath = path.join(dotBlitzDir, "index.js")
const defaultIndexBrowserJSPath = path.join(dotBlitzDir, "index-browser.js")
const defaultIndexDTSPath = path.join(dotBlitzDir, "index.d.ts")
if (!fs.existsSync(defaultIndexJsPath)) {
await copyFile(path.join(__dirname, "default-index.js"), defaultIndexJsPath)
}
if (!fs.existsSync(defaultIndexBrowserJSPath)) {
await copyFile(path.join(__dirname, "default-index-browser.js"), defaultIndexBrowserJSPath)
}
if (!fs.existsSync(defaultIndexDTSPath)) {
await copyFile(path.join(__dirname, "default-index.d.ts"), defaultIndexDTSPath)
}
} catch (e) {
console.error(e)
}
}
async function makeDir(input) {
const make = async (pth) => {
try {
await mkdir(pth)
return pth
} catch (error) {
if (error.code === "EPERM") {
throw error
}
if (error.code === "ENOENT") {
if (path.dirname(pth) === pth) {
throw new Error(`operation not permitted, mkdir '${pth}'`)
}
if (error.message.includes("null bytes")) {
throw error
}
await make(path.dirname(pth))
return make(pth)
}
try {
const stats = await stat(pth)
if (!stats.isDirectory()) {
throw new Error("The path is not a directory")
}
} catch (_) {
throw error
}
return pth
}
}
return await make(path.resolve(input))
}
/**
* Get the command that triggered this postinstall script being run. If there is
* an error while attempting to get this value then the string constant
* 'ERROR_WHILE_FINDING_POSTINSTALL_TRIGGER' is returned.
* This information is just necessary for telemetry.
* This get's passed in to Generate, which then automatically get's propagated to telemetry.
*/
function getPostInstallTrigger() {
/*
npm_config_argv` is not officially documented so here are our (Prisma's) research notes
`npm_config_argv` is available to the postinstall script when the containing package has been installed by npm into some project.
An example of its value:
```
npm_config_argv: '{"remain":["../test"],"cooked":["add","../test"],"original":["add","../test"]}',
```
We are interesting in the data contained in the "original" field.
Trivia/Note: `npm_config_argv` is not available when running e.g. `npm install` on the containing package itself (e.g. when working on it)
Yarn mimics this data and environment variable. Here is an example following `yarn add` for the same package:
```
npm_config_argv: '{"remain":[],"cooked":["add"],"original":["add","../test"]}'
```
Other package managers like `pnpm` have not been tested.
*/
const maybe_npm_config_argv_string = process.env.npm_config_argv
if (maybe_npm_config_argv_string === undefined) {
return UNABLE_TO_FIND_POSTINSTALL_TRIGGER__ENVAR_MISSING
}
let npm_config_argv
try {
npm_config_argv = JSON.parse(maybe_npm_config_argv_string)
} catch (e) {
return `${UNABLE_TO_FIND_POSTINSTALL_TRIGGER_JSON_PARSE_ERROR}: ${maybe_npm_config_argv_string}`
}
if (typeof npm_config_argv !== "object" || npm_config_argv === null) {
return `${UNABLE_TO_FIND_POSTINSTALL_TRIGGER_JSON_SCHEMA_ERROR}: ${maybe_npm_config_argv_string}`
}
const npm_config_arv_original_arr = npm_config_argv.original
if (!Array.isArray(npm_config_arv_original_arr)) {
return `${UNABLE_TO_FIND_POSTINSTALL_TRIGGER_JSON_SCHEMA_ERROR}: ${maybe_npm_config_argv_string}`
}
const npm_config_arv_original = npm_config_arv_original_arr
.filter((arg) => arg !== "")
.join(" ")
const command =
npm_config_arv_original === ""
? getPackageManagerName()
: [getPackageManagerName(), npm_config_arv_original].join(" ")
return command
}
/**
* Wrap double quotes around the given string.
*/
function doubleQuote(x) {
return `"${x}"`
}
/**
* Get the package manager name currently being used. If parsing fails, then the following pattern is returned:
* UNKNOWN_NPM_CONFIG_USER_AGENT(<string received>).
*/
function getPackageManagerName() {
const userAgent = process.env.npm_config_user_agent
if (!userAgent) return "MISSING_NPM_CONFIG_USER_AGENT"
const name = parsePackageManagerName(userAgent)
if (!name) return `UNKNOWN_NPM_CONFIG_USER_AGENT(${userAgent})`
return name
}
/**
* Parse package manager name from useragent. If parsing fails, `null` is returned.
*/
function parsePackageManagerName(userAgent) {
let packageManager = null
// example: 'yarn/1.22.4 npm/? node/v13.11.0 darwin x64'
// References:
// - https://pnpm.js.org/en/3.6/only-allow-pnpm
// - https://github.com/cameronhunter/npm-config-user-agent-parser
if (userAgent) {
const matchResult = userAgent.match(/^([^/]+)\/.+/)
if (matchResult) {
packageManager = matchResult[1].trim()
}
}
return packageManager
}
// prettier-ignore
const UNABLE_TO_FIND_POSTINSTALL_TRIGGER__ENVAR_MISSING = 'UNABLE_TO_FIND_POSTINSTALL_TRIGGER__ENVAR_MISSING'
// prettier-ignore
const UNABLE_TO_FIND_POSTINSTALL_TRIGGER_JSON_PARSE_ERROR = 'UNABLE_TO_FIND_POSTINSTALL_TRIGGER_JSON_PARSE_ERROR'
// prettier-ignore
const UNABLE_TO_FIND_POSTINSTALL_TRIGGER_JSON_SCHEMA_ERROR = 'UNABLE_TO_FIND_POSTINSTALL_TRIGGER_JSON_SCHEMA_ERROR'
}
if (!isInstalledGlobally) {
codegen()
}

View File

@@ -1,5 +1,38 @@
# @blitzjs/rpc
## 2.0.0-alpha.9
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.9
- @blitzjs/auth@2.0.0-alpha.9
## 2.0.0-alpha.8
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.8
- @blitzjs/auth@2.0.0-alpha.8
## 2.0.0-alpha.7
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.7
- @blitzjs/auth@2.0.0-alpha.7
## 2.0.0-alpha.6
### Patch Changes
- fix route manifest codegen
- Updated dependencies
- blitz@2.0.0-alpha.6
- @blitzjs/auth@2.0.0-alpha.6
## 2.0.0-alpha.5
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/rpc",
"version": "2.0.0-alpha.5",
"version": "2.0.0-alpha.9",
"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,20 +20,21 @@
"dist/**"
],
"dependencies": {
"@blitzjs/auth": "2.0.0-alpha.5",
"@blitzjs/auth": "2.0.0-alpha.9",
"b64-lite": "1.4.0",
"bad-behavior": "1.0.1",
"chalk": "^4.1.0",
"debug": "4.3.3",
"react-query": "3.21.1",
"superjson": "1.8.0"
"superjson": "1.8.0",
"zod": "3.10.1"
},
"devDependencies": {
"@blitzjs/config": "workspace:*",
"@blitzjs/config": "workspace:2.0.0-alpha.9",
"@types/debug": "4.1.7",
"@types/react": "17.0.43",
"@types/react-dom": "17.0.14",
"blitz": "2.0.0-alpha.5",
"blitz": "2.0.0-alpha.9",
"next": "12.1.1",
"react": "18.0.0",
"react-dom": "18.0.0",
@@ -42,7 +43,7 @@
"watch": "1.0.2"
},
"peerDependencies": {
"blitz": "2.0.0-alpha.5",
"blitz": "2.0.0-alpha.9",
"next": "*"
},
"publishConfig": {

View File

@@ -6,6 +6,8 @@ import chalk from "chalk"
// TODO - optimize end user server bundles by not exporting all client stuff here
export * from "./index-browser"
export * from "./resolver"
// Mechanism used by Vite/Next/Nuxt plugins for automatically loading query and mutation resolvers
function isObject(value: unknown): value is Record<string | symbol, unknown> {
return typeof value === "object" && value !== null

View File

@@ -0,0 +1,63 @@
import {Ctx} from "blitz"
import {describe, it, expect} from "vitest"
import {z} from "zod"
import {ParserType, resolver} from "./resolver"
describe("resolver", () => {
it("should typecheck and pass along value", async () => {
await resolverTest({})
})
it("should typecheck and pass along value if sync resolver is specified", async () => {
await resolverTest({type: "sync"})
})
it("should typecheck and pass along value if async resolver is specified", async () => {
await resolverTest({type: "async"})
})
})
const syncResolver = resolver.pipe(
resolver.zod(
z.object({
email: z.string().email(),
}),
"sync",
),
resolver.authorize({}),
(input) => {
return input.email
},
)
const asyncResolver = resolver.pipe(
resolver.zod(
z.object({
email: z.string().email(),
}),
"async",
),
resolver.authorize({}),
(input) => {
return input.email
},
)
const resolverTest = async ({type}: {type?: ParserType}) => {
const resolver1 = type === "sync" ? syncResolver : asyncResolver
const result1 = await resolver1(
{email: "test@example.com"},
{session: {$authorize: () => undefined} as Ctx},
)
expect(result1).toBe("test@example.com")
const resolver2 = resolver.pipe(
/*resolver.authorize(), */ (input: {email: string}) => {
return input.email
},
)
const result2 = await resolver2(
{email: "test@example.com"},
{session: {$authorize: () => undefined} as Ctx},
)
expect(result2).toBe("test@example.com")
}

View File

@@ -0,0 +1,321 @@
import {AuthenticatedSessionContext, SessionContext, SessionContextBase} from "@blitzjs/auth"
import {Await, Ctx, EnsurePromise} from "blitz"
import type {input as zInput, output as zOutput, ZodTypeAny} from "zod"
export type ParserType = "sync" | "async"
interface ResultWithContext<Result = unknown, Context = unknown> {
__blitz: true
value: Result
ctx: Context
}
function isResultWithContext(x: unknown): x is ResultWithContext {
return (
typeof x === "object" && x !== null && "ctx" in x && (x as ResultWithContext).__blitz === true
)
}
export interface AuthenticatedMiddlewareCtx extends Omit<Ctx, "session"> {
session: AuthenticatedSessionContext
}
type PipeFn<Prev, Next, PrevCtx, NextCtx = PrevCtx> = (
i: Await<Prev>,
c: PrevCtx,
) => Next extends ResultWithContext ? never : Next | ResultWithContext<Next, NextCtx>
function pipe<A, Z>(ab: (i: A, c: Ctx) => Z): (input: A, ctx: Ctx) => EnsurePromise<Z>
function pipe<A, B, C, CA = Ctx, CB = CA, CC = CB>(
ab: PipeFn<A, B, CA, CB>,
bc: PipeFn<B, C, CB, CC>,
): (input: A, ctx: CA) => EnsurePromise<C>
function pipe<A, B, C, D, CA = Ctx, CB = CA, CC = CB, CD = CC>(
ab: PipeFn<A, B, CA, CB>,
bc: PipeFn<B, C, CB, CC>,
cd: PipeFn<C, D, CC, CD>,
): (input: A, ctx: CA) => EnsurePromise<D>
function pipe<A, B, C, D, E, CA = Ctx, CB = CA, CC = CB, CD = CC, CE = CD>(
ab: PipeFn<A, B, CA, CB>,
bc: PipeFn<B, C, CB, CC>,
cd: PipeFn<C, D, CC, CD>,
de: PipeFn<D, E, CD, CE>,
): (input: A, ctx: CA) => EnsurePromise<E>
function pipe<A, B, C, D, E, F, CA = Ctx, CB = CA, CC = CB, CD = CC, CE = CD, CF = CE>(
ab: PipeFn<A, B, CA, CB>,
bc: PipeFn<B, C, CB, CC>,
cd: PipeFn<C, D, CC, CD>,
de: PipeFn<D, E, CD, CE>,
ef: PipeFn<E, F, CE, CF>,
): (input: A, ctx: CA) => EnsurePromise<F>
function pipe<A, B, C, D, E, F, G, CA = Ctx, CB = CA, CC = CB, CD = CC, CE = CD, CF = CE, CG = CF>(
ab: PipeFn<A, B, CA, CB>,
bc: PipeFn<B, C, CB, CC>,
cd: PipeFn<C, D, CC, CD>,
de: PipeFn<D, E, CD, CE>,
ef: PipeFn<E, F, CE, CF>,
fg: PipeFn<F, G, CF, CG>,
): (input: A, ctx: CA) => EnsurePromise<CG>
function pipe<
A,
B,
C,
D,
E,
F,
G,
H,
CA = Ctx,
CB = CA,
CC = CB,
CD = CC,
CE = CD,
CF = CE,
CG = CF,
CH = CG,
>(
ab: PipeFn<A, B, CA, CB>,
bc: PipeFn<B, C, CB, CC>,
cd: PipeFn<C, D, CC, CD>,
de: PipeFn<D, E, CD, CE>,
ef: PipeFn<E, F, CE, CF>,
fg: PipeFn<F, G, CF, CG>,
gh: PipeFn<G, H, CG, CH>,
): (input: A, ctx: CA) => EnsurePromise<H>
function pipe<
A,
B,
C,
D,
E,
F,
G,
H,
I,
CA = Ctx,
CB = CA,
CC = CB,
CD = CC,
CE = CD,
CF = CE,
CG = CF,
CH = CG,
CI = CH,
>(
ab: PipeFn<A, B, CA, CB>,
bc: PipeFn<B, C, CB, CC>,
cd: PipeFn<C, D, CC, CD>,
de: PipeFn<D, E, CD, CE>,
ef: PipeFn<E, F, CE, CF>,
fg: PipeFn<F, G, CF, CG>,
gh: PipeFn<G, H, CG, CH>,
hi: PipeFn<H, I, CH, CI>,
): (input: A, ctx: CA) => EnsurePromise<I>
function pipe<
A,
B,
C,
D,
E,
F,
G,
H,
I,
J,
CA = Ctx,
CB = CA,
CC = CB,
CD = CC,
CE = CD,
CF = CE,
CG = CF,
CH = CG,
CI = CH,
CJ = CI,
>(
ab: PipeFn<A, B, CA, CB>,
bc: PipeFn<B, C, CB, CC>,
cd: PipeFn<C, D, CC, CD>,
de: PipeFn<D, E, CD, CE>,
ef: PipeFn<E, F, CE, CF>,
fg: PipeFn<F, G, CF, CG>,
gh: PipeFn<G, H, CG, CH>,
hi: PipeFn<H, I, CH, CI>,
ij: PipeFn<I, J, CI, CJ>,
): (input: A, ctx: CA) => EnsurePromise<J>
function pipe<
A,
B,
C,
D,
E,
F,
G,
H,
I,
J,
K,
CA = Ctx,
CB = CA,
CC = CB,
CD = CC,
CE = CD,
CF = CE,
CG = CF,
CH = CG,
CI = CH,
CJ = CI,
CK = CJ,
>(
ab: PipeFn<A, B, CA, CB>,
bc: PipeFn<B, C, CB, CC>,
cd: PipeFn<C, D, CC, CD>,
de: PipeFn<D, E, CD, CE>,
ef: PipeFn<E, F, CE, CF>,
fg: PipeFn<F, G, CF, CG>,
gh: PipeFn<G, H, CG, CH>,
hi: PipeFn<H, I, CH, CI>,
ij: PipeFn<I, J, CI, CJ>,
jk: PipeFn<J, K, CJ, CK>,
): (input: A, ctx: CA) => EnsurePromise<K>
function pipe<
A,
B,
C,
D,
E,
F,
G,
H,
I,
J,
K,
L,
CA = Ctx,
CB = CA,
CC = CB,
CD = CC,
CE = CD,
CF = CE,
CG = CF,
CH = CG,
CI = CH,
CJ = CI,
CK = CJ,
CL = CK,
>(
ab: PipeFn<A, B, CA, CB>,
bc: PipeFn<B, C, CB, CC>,
cd: PipeFn<C, D, CC, CD>,
de: PipeFn<D, E, CD, CE>,
ef: PipeFn<E, F, CE, CF>,
fg: PipeFn<F, G, CF, CG>,
gh: PipeFn<G, H, CG, CH>,
hi: PipeFn<H, I, CH, CI>,
ij: PipeFn<I, J, CI, CJ>,
jk: PipeFn<J, K, CJ, CK>,
kl: PipeFn<K, L, CK, CL>,
): (input: A, ctx: CA) => EnsurePromise<L>
function pipe<
A,
B,
C,
D,
E,
F,
G,
H,
I,
J,
K,
L,
M,
CA = Ctx,
CB = CA,
CC = CB,
CD = CC,
CE = CD,
CF = CE,
CG = CF,
CH = CG,
CI = CH,
CJ = CI,
CK = CJ,
CL = CK,
CM = CL,
>(
ab: PipeFn<A, B, CA, CB>,
bc: PipeFn<B, C, CB, CC>,
cd: PipeFn<C, D, CC, CD>,
de: PipeFn<D, E, CD, CE>,
ef: PipeFn<E, F, CE, CF>,
fg: PipeFn<F, G, CF, CG>,
gh: PipeFn<G, H, CG, CH>,
hi: PipeFn<H, I, CH, CI>,
ij: PipeFn<I, J, CI, CJ>,
jk: PipeFn<J, K, CJ, CK>,
kl: PipeFn<K, L, CK, CL>,
lm: PipeFn<L, M, CL, CM>,
): (input: A, ctx: CA) => EnsurePromise<M>
function pipe(...args: unknown[]): unknown {
const functions = args as PipeFn<unknown, unknown, Ctx>[]
return async function (input: unknown, ctx: Ctx) {
let lastResult = input
for (let fn of functions) {
lastResult = await fn(lastResult, ctx)
if (isResultWithContext(lastResult)) {
ctx = lastResult.ctx as Ctx
lastResult = lastResult.value
}
}
return lastResult
}
}
interface ResolverAuthorize {
<T, C = Ctx>(...args: Parameters<SessionContextBase["$authorize"]>): (
input: T,
ctx: C,
) => ResultWithContext<T, AuthenticatedMiddlewareCtx>
}
const authorize: ResolverAuthorize = (...args) => {
return function _innerAuthorize(input, ctx) {
const session: SessionContext = (ctx as any).session
session.$authorize(...args)
return {
__blitz: true,
value: input,
// we could use {...ctx, session} instead of `as any` just for TypeScript's sake
ctx: ctx as any,
}
}
}
function zod<Schema extends ZodTypeAny, InputType = zInput<Schema>, OutputType = zOutput<Schema>>(
schema: Schema,
parserType: "sync",
): (input: InputType) => OutputType
function zod<Schema extends ZodTypeAny, InputType = zInput<Schema>, OutputType = zOutput<Schema>>(
schema: Schema,
parserType: "async",
): (input: InputType) => Promise<OutputType>
function zod<Schema extends ZodTypeAny, InputType = zInput<Schema>, OutputType = zOutput<Schema>>(
schema: Schema,
): (input: InputType) => Promise<OutputType>
function zod<Schema extends ZodTypeAny, InputType = zInput<Schema>, OutputType = zOutput<Schema>>(
schema: Schema,
parserType: ParserType = "async",
) {
if (parserType === "sync") {
return (input: InputType): OutputType => schema.parse(input)
} else {
return (input: InputType): Promise<OutputType> => schema.parseAsync(input)
}
}
export const resolver = {
pipe,
zod,
authorize,
}

View File

@@ -1,5 +1,34 @@
# blitz
## 2.0.0-alpha.9
### Patch Changes
- add @blitzjs/generator as external
- @blitzjs/generator@2.0.0-alpha.9
## 2.0.0-alpha.8
### Patch Changes
- fix broken cli versioning
- @blitzjs/generator@2.0.0-alpha.8
## 2.0.0-alpha.7
### Patch Changes
- added index.cjs to blitz externals
- @blitzjs/generator@2.0.0-alpha.7
## 2.0.0-alpha.6
### Patch Changes
- fix route manifest codegen
- Updated dependencies
- @blitzjs/generator@2.0.0-alpha.6
## 2.0.0-alpha.5
### Patch Changes

View File

@@ -2,7 +2,7 @@ import {BuildConfig} from "unbuild"
const config: BuildConfig = {
entries: ["./src/index-browser", "./src/index-server", "./src/cli/index"],
externals: ["index-browser.cjs", "index-browser.mjs", "zod"],
externals: ["index-browser.cjs", "index-browser.mjs", "index.cjs", "zod", "@blitzjs/generator"],
declaration: true,
rollup: {
emitCJS: true,

View File

@@ -1,6 +1,6 @@
{
"name": "blitz",
"version": "2.0.0-alpha.5",
"version": "2.0.0-alpha.9",
"scripts": {
"build": "unbuild",
"dev": "watch unbuild src --wait=0.2",
@@ -23,7 +23,7 @@
"blitz": "bin/blitz"
},
"dependencies": {
"@blitzjs/generator": "workspace:*",
"@blitzjs/generator": "2.0.0-alpha.9",
"arg": "5.0.1",
"chalk": "^4.1.0",
"console-table-printer": "2.10.0",
@@ -33,6 +33,7 @@
"dotenv": "16.0.0",
"dotenv-expand": "8.0.3",
"esbuild": "0.14.34",
"find-up": "4.1.0",
"fs-extra": "10.0.1",
"hasbin": "1.2.3",
"npm-which": "3.0.1",
@@ -41,11 +42,12 @@
"pkg-dir": "5.0.0",
"prompts": "2.4.2",
"resolve-cwd": "3.0.0",
"resolve-from": "5.0.0",
"superjson": "1.8.0",
"tslog": "3.3.1"
},
"devDependencies": {
"@blitzjs/config": "workspace:*",
"@blitzjs/config": "workspace:2.0.0-alpha.9",
"@types/cookie": "0.4.1",
"@types/cross-spawn": "6.0.2",
"@types/debug": "4.1.7",

View File

@@ -1,7 +0,0 @@
import {CliCommand} from "../index"
const dev: CliCommand = (argv) => {
console.log("dev hit")
}
export {dev}

View File

@@ -74,6 +74,13 @@ const args = arg(
"--parent": String,
"--dry-run": Boolean,
"--env": String,
// Aliases
"-e": "--env",
"-n": "--name",
"-t": "--type",
"-c": "--context",
"-p": "--dry-run",
},
{
permissive: true,

View File

@@ -48,7 +48,6 @@ const PREFERABLE_PKG_MANAGER: TPkgManager = IS_PNPM_INSTALLED
const args = arg(
{
// Types
"--name": String,
"--npm": Boolean,
"--yarn": Boolean,
"--pnpm": Boolean,
@@ -59,6 +58,8 @@ const args = arg(
"--dry-run": Boolean,
"--no-git": Boolean,
"--skip-upgrade": Boolean,
// Aliases
},
{
permissive: true,
@@ -74,7 +75,7 @@ let projectPkgManger: TPkgManager = PREFERABLE_PKG_MANAGER
let shouldInstallDeps: boolean = true
const determineProjectName = async () => {
if (!args["--name"]) {
if (args._.slice(1).length < 1) {
const res = await prompts({
type: "text",
name: "name",
@@ -85,7 +86,7 @@ const determineProjectName = async () => {
projectName = res.name.trim().replaceAll(" ", "-")
projectPath = path.resolve(projectName)
} else {
projectName = args["--name"]
projectName = args._.slice(1)[0] as string
projectPath = path.resolve(projectName)
}
}
@@ -233,7 +234,7 @@ const newApp: CliCommand = async (argv) => {
try {
const latestBlitzVersion = (await getLatestVersion("blitz")).value
const requireManualInstall = args["--dry-run"] || !shouldInstallDeps
const postInstallSteps = args["--name"] === "." ? [] : [`cd ${projectName}`]
const postInstallSteps = projectName === "." ? [] : [`cd ${projectName}`]
const generatorOpts: AppGeneratorOptions = {
template: projectTemplate,

View File

@@ -0,0 +1,56 @@
import {Readable} from "stream"
import {getCommandBin} from "../utils/config"
import {CliCommand} from "../index"
import arg from "arg"
let prismaBin: string
export const runPrisma = async (args: string[], silent = false) => {
if (!prismaBin) {
try {
prismaBin = await getCommandBin("prisma")
} catch (err) {
throw err
}
}
const cp = require("cross-spawn").spawn(prismaBin, args, {
stdio: silent ? "pipe" : "inherit",
env: process.env,
})
const cp_stderr: string[] = []
if (silent) {
cp.stderr.on("data", (chunk: Readable) => {
cp_stderr.push(chunk.toString())
})
}
const code = await require("p-event")(cp, "exit", {rejectionEvents: []})
return {
success: code === 0,
stderr: silent ? cp_stderr.join("") : undefined,
}
}
export const runPrismaExitOnError = async (...args: Parameters<typeof runPrisma>) => {
const result = await runPrisma(...args)
if (!result.success) {
process.exit(1)
}
}
const prisma: CliCommand = async () => {
const args = arg(
{},
{
permissive: true,
},
)
await runPrismaExitOnError(args._.slice(1))
}
export {prisma}

View File

@@ -2,6 +2,8 @@ import {NON_STANDARD_NODE_ENV} from "./utils/constants"
import arg from "arg"
import packageJson from "../../package.json"
import {loadEnvConfig} from "../env-utils"
import {getCommandBin} from "./utils/config"
import spawn from "cross-spawn"
const commonArgs = {
// Types
@@ -22,13 +24,6 @@ const commands: {[command: string]: () => Promise<CliCommand>} = {
dev: () => import("./commands/next/dev").then((i) => i.dev),
build: () => import("./commands/next/build").then((i) => i.build),
start: () => import("./commands/next/start").then((i) => i.start),
next: async () => (argv) => {
if (argv?.[0] && ["dev", "start", "build"].includes(argv[0])) {
const command = argv[0] as "dev" | "start" | "build"
return import("./commands/next").then((i) => i[command]())
}
console.error(`Invalid command provided: "blitz next ${argv?.[0]}".`)
},
new: () => import("./commands/new").then((i) => i.newApp),
generate: () => import("./commands/generate").then((i) => i.generate),
codegen: () => import("./commands/codegen").then((i) => i.codegen),
@@ -52,36 +47,17 @@ if (args["--version"]) {
const foundCommand = Boolean(commands[args._[0] as string])
if (!foundCommand && args["--help"]) {
console.log(`
Usage
$ blitz <command>
Available commands
${Object.keys(commands).join(", ")}
Options
--env, -e App environment name
--version, -v Version number
--help, -h Displays this message
For more information run a command with the --help flag
$ blitz build --help
`)
process.exit(0)
}
const command = foundCommand ? (args._[0] as string) : defaultCommand
const forwardedArgs = foundCommand ? args._.slice(1) : args._
if (args["--help"]) {
forwardedArgs.push("--help")
}
if (args["--env"]) {
process.env.APP_ENV = args["--env"]
}
if (args["--help"]) {
forwardedArgs.push("--help")
}
const defaultEnv = command === "dev" ? "development" : "production"
const standardEnv = ["production", "development", "test"]
@@ -94,15 +70,56 @@ if (process.env.NODE_ENV && !standardEnv.includes(process.env.NODE_ENV)) {
process.on("SIGTERM", () => process.exit(0))
process.on("SIGINT", () => process.exit(0))
commands[command]?.()
.then((exec: any) => exec(forwardedArgs))
.then(() => {
if (command === "build") {
// ensure process exits after build completes so open handles/connections
// don't cause process to hang
process.exit(0)
}
})
.catch((err) => {
console.log(err)
})
if (foundCommand) {
commands[command]?.()
.then((exec: any) => exec(forwardedArgs))
.then(() => {
if (command === "build") {
// ensure process exits after build completes so open handles/connections
// don't cause process to hang
process.exit(0)
}
})
.catch((err) => {
console.log(err)
})
} else {
if (args["--help"] && args._.length === 0) {
console.log(`
Usage
$ blitz <command>
Available commands
${Object.keys(commands).join(", ")}
Options
--env, -e App environment name
--version, -v Version number
--help, -h Displays this message
For more information run a command with the --help flag
$ blitz build --help
`)
process.exit(0)
} else {
// If the command is not found, we assume it is a command from the bin
void runCommandFromBin()
}
}
async function runCommandFromBin() {
const command = args._[0] as string
let commandBin: string | null = null
try {
commandBin = await getCommandBin(command)
} catch (e: any) {
console.error(`Error: ${e.message}`)
}
if (!commandBin) {
process.exit(1)
}
const result = spawn.sync(commandBin, process.argv.slice(3), {stdio: "inherit"})
process.exit(result.status || 0)
}

View File

@@ -83,14 +83,17 @@ export async function normalize(config: ServerConfig): Promise<NormalizedConfig>
watch: config.watch ?? env === "dev",
clean: config.clean,
// -
nextBin: await getNextBin(rootFolder, env === "dev"),
nextBin: await getCommandBin("next", rootFolder, env === "dev"),
}
}
async function getNextBin(rootFolder: string, _usePatched: boolean = false): Promise<string> {
const nextBinPkg = "next"
const nextBin = await resolveBinAsync(nextBinPkg)
return resolve(rootFolder, nextBin)
export async function getCommandBin(
command: string,
rootFolder: string = process.cwd(),
_usePatched: boolean = false,
): Promise<string> {
const bin = await resolveBinAsync(command)
return resolve(rootFolder, bin)
}
async function getIsTypeScript(rootFolder: string): Promise<boolean> {

View File

@@ -1,8 +1,10 @@
import {join, dirname} from "path"
import os from "os"
import {readdirSync, promises} from "fs"
import {promises} from "fs"
const readFile = promises.readFile
import {outputFile} from "fs-extra"
import findUp from "find-up"
import resolveFrom from "resolve-from"
export const CONFIG_FILE = ".blitz.config.compiled.js"
export const NEXT_CONFIG_FILE = "next.config.js"
@@ -495,7 +497,7 @@ export async function generateManifest() {
const {declaration, implementation} = setupManifest(routes)
const dotBlitz = join(process.cwd(), ".blitz")
const dotBlitz = join(await findNodeModulesRoot(process.cwd()), ".blitz")
await outputFile(join(dotBlitz, "index.js"), implementation, {
encoding: "utf-8",
@@ -507,9 +509,37 @@ export async function generateManifest() {
encoding: "utf-8",
})
}
// export const findBlitzConfigDirectory = () => {
// let blitzDir = readdirSync(join(process.cwd(), ".blitz"))
// if (blitzDir.length) {
// return join(process.cwd(), ".blitz/index.js")
// }
// }
export const isInternalBlitzMonorepoDevelopment = __dirname.match(
/[\\/]packages[\\/]blitz[\\/]dist[\\/]chunks$/,
)
async function findNodeModulesRoot(src: string) {
/*
* Because of our package structure, and because of how things like pnpm link modules,
* we must first find blitz package, and then find `next` and then
* the root of `next`
*
* This is because we import from `.blitz` inside `next/stdlib`.
* If that changes, then this logic here will need to change
*/
let root: string
if (isInternalBlitzMonorepoDevelopment) {
root = join(__dirname, "..", "..", "..", "..", "/node_modules")
} else {
const blitzPkgLocation = dirname(
(await findUp("package.json", {
cwd: resolveFrom(src, "blitz"),
})) ?? "",
)
if (!blitzPkgLocation) {
throw new Error("Internal Blitz Error: unable to find 'blitz' package location")
}
root = join(blitzPkgLocation, "../")
}
return root
}

View File

@@ -1,5 +1,17 @@
# @blitzjs/config
## 2.0.0-alpha.9
## 2.0.0-alpha.8
## 2.0.0-alpha.7
## 2.0.0-alpha.6
### Patch Changes
- fix route manifest codegen
## 2.0.0-alpha.5
### Patch Changes

View File

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

View File

@@ -1,5 +1,17 @@
# @blitzjs/generator
## 2.0.0-alpha.9
## 2.0.0-alpha.8
## 2.0.0-alpha.7
## 2.0.0-alpha.6
### Patch Changes
- fix route manifest codegen
## 2.0.0-alpha.5
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/generator",
"version": "2.0.0-alpha.5",
"version": "2.0.0-alpha.9",
"scripts": {
"dev": "watch unbuild src --wait=0.2",
"build": "unbuild && pnpm build:templates",
@@ -45,7 +45,7 @@
"vinyl": "2.2.1"
},
"devDependencies": {
"@blitzjs/config": "workspace:*",
"@blitzjs/config": "2.0.0-alpha.9",
"@juanm04/cpx": "2.0.1",
"@types/babel__core": "7.1.19",
"@types/diff": "5.0.2",

View File

@@ -40,7 +40,7 @@ export interface SourceRootType {
const alwaysIgnoreFiles = [".blitz", ".DS_Store", ".git", ".next", ".now", "node_modules"]
const ignoredExtensions = [".ico", ".png", ".jpg"]
const tsExtension = /\.(tsx?)$/
const tsExtension = /\.(tsx?|ts?)$/
const codeFileExtensions = /\.(tsx?|jsx?)$/
function getStatements(node: j.BlockStatement | j.Statement): j.Statement[] {
@@ -269,11 +269,6 @@ export abstract class Generator<
)
this.fs.write(this.destinationPath(pathSuffix), newContent)
// this.fs.copy(this.sourcePath(filePath), this.destinationPath(pathSuffix), {
// process: (input) =>
// // this.process(input, pathSuffix, templateValues, prettierOptions ?? undefined),
// this.process(input, pathSuffix, templateValues, undefined),
// })
if (!this.useTs && tsExtension.test(this.destinationPath(pathSuffix))) {
templatedPathSuffix = templatedPathSuffix.replace(tsExtension, ".js")
}

View File

@@ -76,6 +76,10 @@ export class AppGenerator extends Generator<AppGeneratorOptions> {
this.destinationPath(this.options.useTs ? "package.ts.json" : "package.js.json"),
this.destinationPath("package.json"),
)
this.fs.move(
this.destinationPath(`pages/api/rpc/blitzrpcroute.${this.options.useTs ? "ts" : "js"}`),
this.destinationPath(`pages/api/rpc/[[...blitz]].${this.options.useTs ? "ts" : "js"}`),
)
if (!this.options.template.skipForms) {
this.updateForms()
@@ -231,6 +235,14 @@ export class AppGenerator extends Generator<AppGeneratorOptions> {
}
}
// if(this.options.useTs) {
// if(templatedPathSuffix === "pages/api/rpc/blitzrpcroute.js") {
// this.fs.write(this.destinationPath("pages/api/rpc/[...blitz].js"), this.sourcePath("pages/api/rpc/blitzrpcroute.js"))
// } else {
// }
// }
if (!this.options.skipGit && gitInitSuccessful) {
this.commitChanges()
}

View File

@@ -0,0 +1,8 @@
# This env file should NOT be checked into source control
# This is the place for values that changed for every developer
# SQLite is ready to go out of the box, but you can switch to Postgres
# by first changing the provider from "sqlite" to "postgres" in the Prisma
# schema file and by second swapping the DATABASE_URL below.
DATABASE_URL="file:./db.sqlite"
# DATABASE_URL=postgresql://__username__@localhost:5432/__name__

View File

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

View File

@@ -7,3 +7,4 @@ db/migrations
.yarn
.pnp.*
node_modules
README.md

View File

@@ -39,7 +39,7 @@ export const LoginForm = (props: LoginFormProps) => {
<LabeledTextField name="email" label="Email" placeholder="Email" />
<LabeledTextField name="password" label="Password" placeholder="Password" type="password" />
<div>
<Link href="/auth/forgot-password" passHref>
<Link href="/auth/forgot-password">
<a>Forgot your password?</a>
</Link>
</div>
@@ -47,7 +47,7 @@ export const LoginForm = (props: LoginFormProps) => {
<div style={{ marginTop: "1rem" }}>
Or{" "}
<Link href="/auth/signup" passHref>
<Link href="/auth/signup">
<a>Sign Up</a>
</Link>
</div>

View File

@@ -1,26 +1,25 @@
import { NotFoundError, Ctx } from "blitz"
import { prisma } from "db"
import { NotFoundError } from "blitz"
import { db } from "db"
import { authenticateUser } from "./login"
import { ChangePassword } from "../validations"
import { resolver } from "@blitzjs/rpc"
import { SecurePassword } from "@blitzjs/auth"
export default async function changePassword(input, ctx: Ctx) {
ChangePassword.parse(input)
ctx.session.$isAuthorized()
export default resolver.pipe(
resolver.zod(ChangePassword),
resolver.authorize(),
async ({ currentPassword, newPassword }, ctx) => {
const user = await db.user.findFirst({ where: { id: ctx.session.userId as number } })
if (!user) throw new NotFoundError()
const user = await prisma.user.findFirst({
where: {
id: ctx.session.userId as number,
},
})
await authenticateUser(user.email, currentPassword)
if (!user) throw new NotFoundError()
await authenticateUser(user.email, input.currentPassword)
const hashedPassword = await SecurePassword.hash(newPassword.trim())
await db.user.update({
where: { id: user.id },
data: { hashedPassword },
})
const hashedPassword = await SecurePassword.hash(input.newPassword.trim())
await prisma.user.update({
where: { id: user.id },
data: { hashedPassword },
})
}
return true
}
)

View File

@@ -1,11 +1,11 @@
import { prisma } from "db"
import { db } from "db"
import { hash256 } from "@blitzjs/auth"
import forgotPassword from "./forgotPassword"
import previewEmail from "preview-email"
import { Ctx } from "@blitzjs/next"
beforeEach(async () => {
await prisma.$reset()
await db.$reset()
})
const generatedToken = "plain-token"
@@ -22,7 +22,7 @@ describe("forgotPassword mutation", () => {
it("works correctly", async () => {
// Create test user
const user = await prisma.user.create({
const user = await db.user.create({
data: {
email: "user@example.com",
tokens: {
@@ -41,7 +41,7 @@ describe("forgotPassword mutation", () => {
// Invoke the mutation
await forgotPassword({ email: user.email }, {} as Ctx)
const tokens = await prisma.token.findMany({ where: { userId: user.id } })
const tokens = await db.token.findMany({ where: { userId: user.id } })
const token = tokens[0]
if (!user.tokens[0]) throw new Error("Missing user token")
if (!token) throw new Error("Missing token")

View File

@@ -1,15 +1,14 @@
import { prisma } from "db"
import { generateToken, hash256 } from "@blitzjs/auth"
import { resolver } from "@blitzjs/rpc"
import { db } from "db"
import { forgotPasswordMailer } from "mailers/forgotPasswordMailer"
import { ForgotPassword } from "../validations"
import { Ctx } from "@blitzjs/next"
const RESET_PASSWORD_TOKEN_EXPIRATION_IN_HOURS = 4
export default async function forgotPassword(input, ctx: Ctx) {
ForgotPassword.parse(input)
export default resolver.pipe(resolver.zod(ForgotPassword), async ({ email }) => {
// 1. Get the user
const user = await prisma.user.findFirst({ where: { email: input.email.toLowerCase() } })
const user = await db.user.findFirst({ where: { email: email.toLowerCase() } })
// 2. Generate the token and expiration date.
const token = generateToken()
@@ -20,9 +19,9 @@ export default async function forgotPassword(input, ctx: Ctx) {
// 3. If user with this email was found
if (user) {
// 4. Delete any existing password reset tokens
await prisma.token.deleteMany({ where: { type: "RESET_PASSWORD", userId: user.id } })
await db.token.deleteMany({ where: { type: "RESET_PASSWORD", userId: user.id } })
// 5. Save this new token in the database.
await prisma.token.create({
await db.token.create({
data: {
user: { connect: { id: user.id } },
type: "RESET_PASSWORD",
@@ -40,4 +39,4 @@ export default async function forgotPassword(input, ctx: Ctx) {
// 8. Return the same result whether a password reset email was sent or not
return
}
})

View File

@@ -1,11 +1,13 @@
import { AuthenticationError } from "blitz"
import { prisma } from "db"
import { Login } from "../validations"
import { SecurePassword } from "@blitzjs/auth"
import { resolver } from "@blitzjs/rpc"
import { AuthenticationError } from "blitz"
import { db} from "db"
import { Role } from "types"
import { Login } from "../validations"
export const authenticateUser = async (rawEmail: string, rawPassword: string) => {
const { email, password } = Login.parse({ email: rawEmail, password: rawPassword })
const user = await prisma.user.findFirst({ where: { email } })
const user = await db.user.findFirst({ where: { email } })
if (!user) throw new AuthenticationError()
const result = await SecurePassword.verify(user.hashedPassword, password)
@@ -13,16 +15,18 @@ export const authenticateUser = async (rawEmail: string, rawPassword: 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
return rest
}
export default async function login(input, ctx) {
const user = await authenticateUser(input.email, input.password)
await ctx.session.$create({ userId: user.id, role: user.role })
export default resolver.pipe(resolver.zod(Login), async ({ email, password }, ctx) => {
// This throws an error if credentials are invalid
const user = await authenticateUser(email, password)
await ctx.session.$create({ userId: user.id, role: user.role as Role})
return user
}
})

View File

@@ -1,9 +1,9 @@
import resetPassword from "./resetPassword"
import { prisma } from "db"
import { db } from "db"
import { SecurePassword, hash256 } from "@blitzjs/auth"
beforeEach(async () => {
await prisma.$reset()
await db.$reset()
})
const mockCtx: any = {
@@ -24,7 +24,7 @@ describe("resetPassword mutation", () => {
const past = new Date()
past.setHours(past.getHours() - 4)
const user = await prisma.user.create({
const user = await db.user.create({
data: {
email: "user@example.com",
tokens: {
@@ -70,11 +70,11 @@ describe("resetPassword mutation", () => {
)
// Delete's the token
const numberOfTokens = await prisma.token.count({ where: { userId: user.id } })
const numberOfTokens = await db.token.count({ where: { userId: user.id } })
expect(numberOfTokens).toBe(0)
// Updates user's password
const updatedUser = await prisma.user.findFirst({ where: { id: user.id } })
const updatedUser = await db.user.findFirst({ where: { id: user.id } })
expect(await SecurePassword.verify(updatedUser!.hashedPassword, newPassword)).toBe(
SecurePassword.VALID
)

View File

@@ -1,5 +1,5 @@
import { SecurePassword, hash256 } from "@blitzjs/auth"
import { prisma } from "db"
import { db } from "db"
import { ResetPassword } from "../validations"
import login from "./login"
@@ -12,7 +12,7 @@ export default async function resetPassword(input, ctx) {
ResetPassword.parse(input)
// 1. Try to find this token in the database
const hashedToken = hash256(input.token)
const possibleToken = await prisma.token.findFirst({
const possibleToken = await db.token.findFirst({
where: { hashedToken, type: "RESET_PASSWORD" },
include: { user: true },
})
@@ -24,7 +24,7 @@ export default async function resetPassword(input, ctx) {
const savedToken = possibleToken
// 3. Delete token so it can't be used again
await prisma.token.delete({ where: { id: savedToken.id } })
await db.token.delete({ where: { id: savedToken.id } })
// 4. If token has expired, error
if (savedToken.expiresAt < new Date()) {
@@ -33,13 +33,13 @@ export default async function resetPassword(input, ctx) {
// 5. Since token is valid, now we can update the user's password
const hashedPassword = await SecurePassword.hash(input.password.trim())
const user = await prisma.user.update({
const user = await db.user.update({
where: { id: savedToken.userId },
data: { hashedPassword },
})
// 6. Revoke all existing login sessions for this user
await prisma.session.deleteMany({ where: { userId: user.id } })
await db.session.deleteMany({ where: { userId: user.id } })
// 7. Now log the user in with the new credentials
await login({ email: user.email, password: input.password }, ctx)

View File

@@ -1,4 +1,4 @@
import { prisma } from "db"
import { db } from "db"
import { SecurePassword } from "@blitzjs/auth"
export default async function signup(input, ctx) {
@@ -6,7 +6,7 @@ export default async function signup(input, ctx) {
const hashedPassword = await SecurePassword.hash((input.password as string) || "test-password")
const email = (input.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,8 @@
import { AuthClientPlugin } from "@blitzjs/auth"
import { setupClient } from "@blitzjs/next"
import { setupBlitzClient } from "@blitzjs/next"
import { BlitzRpcPlugin } from "@blitzjs/rpc"
const { withBlitz } = setupClient({
export const { withBlitz } = setupBlitzClient({
plugins: [
AuthClientPlugin({
cookiePrefix: "__safeNameSlug__-cookie-prefix",
@@ -16,5 +16,3 @@ const { withBlitz } = setupClient({
}),
],
})
export { withBlitz }

View File

@@ -1,17 +1,15 @@
import { setupBlitz } from "@blitzjs/next"
import { setupBlitzServer } from "@blitzjs/next"
import { AuthServerPlugin, PrismaStorage } from "@blitzjs/auth"
import { prisma as db } from "../db/index"
import {db} from "db"
import { simpleRolesIsAuthorized } from "@blitzjs/auth"
const { gSSP, gSP, api } = setupBlitz({
export const { gSSP, gSP, api } = setupBlitzServer({
plugins: [
AuthServerPlugin({
cookiePrefix: "__safeNameSlug__-cookie-prefix",
// TODO fix type
storage: PrismaStorage(db as any),
isAuthorized: simpleRolesIsAuthorized,
}),
],
})
export { gSSP, gSP, api }

View File

@@ -1,10 +1,10 @@
import { Ctx } from "blitz"
import { prisma } from "db"
import { db } from "db"
export default async function getCurrentUser(_ = null, { session }: Ctx) {
if (!session.userId) return null
const user = await prisma.user.findFirst({
const user = await db.user.findFirst({
where: { id: session.userId as number },
select: { id: true, name: true, email: true, role: true },
})

View File

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

View File

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

View File

@@ -3,7 +3,7 @@
datasource db {
provider = "sqlite"
url = "file:./db.sqlite"
url = env("DATABASE_URL")
}
generator client {

View File

@@ -1,10 +1,3 @@
const withBundleAnalyzer = require("@next/bundle-analyzer")({
enabled: process.env.ANALYZE === "true",
})
const {withBlitz} = require("@blitzjs/next")
module.exports = withBlitz(
withBundleAnalyzer({
reactStrictMode: true,
}),
)
module.exports = withBlitz()

View File

@@ -22,18 +22,15 @@
"*.{js}": ["eslint --fix"]
},
"dependencies": {
"@blitzjs/auth": "latest",
"@blitzjs/config": "latest",
"@blitzjs/next": "latest",
"@blitzjs/rpc": "latest",
"@hookform/resolvers": "2.8.8",
"@blitzjs/auth": "alpha",
"@blitzjs/next": "alpha",
"@blitzjs/rpc": "alpha",
"@prisma/client": "3.9.0",
"blitz": "latest",
"blitz": "alpha",
"next": "12.1.1",
"prisma": "3.9.0",
"react": "18.0.0",
"react-dom": "18.0.0",
"ts-node": "10.7.0",
"zod": "3.10.1"
},
"devDependencies": {

View File

@@ -22,18 +22,15 @@
"*.{js,ts,tsx}": ["eslint --fix"]
},
"dependencies": {
"@blitzjs/auth": "latest",
"@blitzjs/config": "latest",
"@blitzjs/next": "latest",
"@blitzjs/rpc": "latest",
"@hookform/resolvers": "2.8.8",
"@blitzjs/auth": "alpha",
"@blitzjs/next": "alpha",
"@blitzjs/rpc": "alpha",
"@prisma/client": "3.9.0",
"blitz": "latest",
"blitz": "alpha",
"next": "12.1.1",
"prisma": "3.9.0",
"react": "18.0.0",
"react-dom": "18.0.0",
"ts-node": "10.7.0",
"zod": "3.10.1"
},
"devDependencies": {

View File

@@ -37,12 +37,12 @@ const UserInfo = () => {
} else {
return (
<>
<Link href="/auth/signup" passHref>
<Link href="/auth/signup">
<a className="button small">
<strong>Sign Up</strong>
</a>
</Link>
<Link href="/auth/login" passHref>
<Link href="/auth/login">
<a className="button small">
<strong>Login</strong>
</a>

View File

@@ -20,5 +20,5 @@
"tsBuildInfoFile": ".tsbuildinfo"
},
"exclude": ["node_modules", "**/*.e2e.ts", "cypress"],
"include": ["blitz-env.d.ts", "**/*.ts", "**/*.tsx"]
"include": ["blitz-env.d.ts", "**/*.ts", "**/*.tsx", "types"]
}

View File

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

View File

@@ -1,7 +1,5 @@
import {setupClient} from "@blitzjs/next"
import {setupBlitzClient} from "@blitzjs/next"
const {withBlitz} = setupClient({
export const {withBlitz} = setupBlitzClient({
plugins: []
})
export {withBlitz}

View File

@@ -18,9 +18,8 @@
"*.{js}": ["eslint --fix"]
},
"dependencies": {
"@blitzjs/config": "latest",
"@blitzjs/next": "latest",
"blitz": "latest",
"@blitzjs/next": "alpha",
"blitz": "alpha",
"next": "12.1.1",
"react": "18.0.0",
"react-dom": "18.0.0",

View File

@@ -20,9 +20,8 @@
]
},
"dependencies": {
"@blitzjs/config": "latest",
"@blitzjs/next": "latest",
"blitz": "latest",
"@blitzjs/next": "alpha",
"blitz": "alpha",
"next": "12.1.1",
"react": "18.0.0",
"react-dom": "18.0.0",

View File

@@ -1,5 +1,5 @@
import {Ctx} from "blitz"
import { prisma } from "db"
import { db } from "db"
import {z} from "zod"
if (process.env.parentModel) {
@@ -18,7 +18,7 @@ export default async function Create__ModelName__(input, ctx: Ctx) {
ctx.session.$isAuthorized()
// TODO: in multi-tenant app, you must add validation to ensure correct tenant
const __modelName__ = await prisma.__modelName__.create({data: input})
const __modelName__ = await db.__modelName__.create({data: input})
return __modelName__

View File

@@ -1,5 +1,5 @@
import {Ctx} from "blitz"
import { prisma } from "db"
import { db } from "db"
import {z} from "zod"
const Delete__ModelName__Input = z.object({
@@ -11,7 +11,7 @@ export default async function Delete__ModelName__(input, ctx: Ctx) {
ctx.session.$isAuthorized()
// TODO: in multi-tenant app, you must add validation to ensure correct tenant
const __modelName__ = await prisma.__modelName__.deleteMany({where: {id: input.id}})
const __modelName__ = await db.__modelName__.deleteMany({where: {id: input.id}})
return __modelName__

View File

@@ -1,5 +1,4 @@
import {resolver} from "blitz"
import { prisma } from "db"
import { db } from "db"
import {z} from "zod"
const Update__ModelName__Input = z.object({
@@ -12,7 +11,7 @@ export default async function Update__ModelName__(input, ctx: Ctx) {
ctx.session.$isAuthorized()
// TODO: in multi-tenant app, you must add validation to ensure correct tenant
const __modelName__ = await prisma.__modelName__.update({where: {id: input.id}, input})
const __modelName__ = await db.__modelName__.update({where: {id: input.id}, input})
return __modelName__

View File

@@ -1,5 +1,5 @@
import {Ctx, NotFoundError} from "blitz"
import { prisma } from "db"
import { db } from "db"
import {z} from "zod"
const Get__ModelName__Input = z.object({
@@ -12,7 +12,7 @@ export default async function Get__ModelName__(input, ctx: Ctx) {
ctx.session.$isAuthorized()
// TODO: in multi-tenant app, you must add validation to ensure correct tenant
const __modelName__ = await prisma.__modelName__.findFirst({where: {id: input.id}})
const __modelName__ = await db.__modelName__.findFirst({where: {id: input.id}})
if (!__modelName__) throw new NotFoundError()

View File

@@ -1,5 +1,5 @@
import {paginate, Ctx} from "blitz"
import { prisma } from "db"
import { db } from "db"
import { Prisma } from "@prisma/client"
interface Get__ModelNames__Input
@@ -12,8 +12,8 @@ export default async function Get__ModelNames(input: Get__ModelNames__Input, ctx
const {items: __modelNames__, hasMore, nextPage, count} = await paginate({
skip: input.skip,
take: input.take,
count: () => prisma.__modelName__.count({where: input.where}),
query: (paginateArgs) => prisma.__modelName__.findMany({...paginateArgs, where: input.where, orderBy: input.orderBy}),
count: () => db.__modelName__.count({where: input.where}),
query: (paginateArgs) => db.__modelName__.findMany({...paginateArgs, where: input.where, orderBy: input.orderBy}),
})
return {

View File

@@ -1,7 +1,10 @@
{
"extends": "@blitzjs/config/tsconfig.library.json",
"compilerOptions": {
"lib": ["ES2019"]
"lib": ["ES2019"],
"declaration": true,
"baseUrl": ".",
"preserveSymlinks": true
},
"include": ["."],
"exclude": ["dist", "build", "node_modules", "./templates/**"]

View File

@@ -25,7 +25,7 @@
"@typescript-eslint/parser": "5.9.1"
},
"devDependencies": {
"@blitzjs/config": "workspace:*",
"@blitzjs/config": "2.0.0-alpha.9",
"@types/react": "17.0.43",
"@types/react-dom": "17.0.14",
"react": "18.0.0",

622
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff