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

Compare commits

...

41 Commits

Author SHA1 Message Date
Dillon Raphael
9631c45833 alpha.19 2022-05-04 12:23:32 -04:00
Aleksandra
2150dcc3e7 Setup Superjson for gssp and gsp (#3335) 2022-05-04 17:45:35 +02:00
Aleksandra
63605961b4 Use routes manifest in template app (#3334) 2022-05-04 17:18:23 +02:00
Brandon Bayer
d711948809 toolkit: fix suspense loading by throwing error on the server (but still have error popup) (#3332)
* wip

* cleanup

* upgrade next@canary
2022-05-02 17:07:41 -07:00
Dillon Raphael
1f6b0b54c2 alpha.18 2022-04-29 20:46:32 -04:00
Dillon Raphael
4603a2b7bd alpha.17 2022-04-29 20:04:23 -04:00
Dillon Raphael
01f3a03ea9 alpha.16 2022-04-29 18:21:07 -04:00
Dillon Raphael
36e26193b7 alpha.15 2022-04-29 18:13:17 -04:00
Dillon Raphael
4a9aa9f7ff alpha.14 2022-04-29 18:08:06 -04:00
Dillon Raphael
1e1bb73b2d alpha.13 2022-04-29 16:25:20 -04:00
beerose
99205f52d5 alpha.12 2022-04-29 20:10:31 +02:00
Aleksandra
d03a1165d7 Use alpha version for blitz dependency, fix package manager selection (#3327) 2022-04-29 20:08:10 +02:00
beerose
d87288a2e9 alpha.11 2022-04-29 16:00:15 +02:00
beerose
fdff45a592 Update isInBlitzMonorepo check in postinstall script 2022-04-29 15:58:48 +02:00
beerose
469f55a429 Fix pre-publish script to build packages after
changeset version
2022-04-29 15:47:01 +02:00
Dillon Raphael
e6c31704ed added postinstall script to blitz/next package json 2022-04-29 09:41:57 -04:00
Dillon Raphael
35a070ad7d alpha.10 2022-04-28 19:37:39 -04:00
Dillon Raphael
9674efc0bf alpha.9 2022-04-28 19:07:56 -04:00
Dillon Raphael
666a3ae3e6 fix cli versioning 2022-04-28 17:35:54 -04:00
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
Brandon Bayer
00bd849eef 2.0.0-alpha.5 2022-04-15 09:03:01 -04:00
Dillon Raphael
c8db1a0b7e Toolkit template (#3298)
* migrate generate command

* tidy up the help command

* Convert legacy full template to new working example

* change db to prisma for templates

* change toolkit-app to use useQuery instead of invoke

* new app & generator templates working for npm only

* fixed npm install

* set blitz config package version

* assert session id

* add type assert to template
2022-04-15 08:35:47 -04:00
Brandon Bayer
fb32903bf9 2.0.0-alpha.4 fix more cli stuff 2022-04-14 21:13:57 -04:00
201 changed files with 6141 additions and 2177 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

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

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

View File

@@ -2,17 +2,40 @@
"mode": "pre",
"tag": "alpha",
"initialVersions": {
"toolkit-app": "1.0.1-alpha.13",
"web": "0.0.0",
"test-auth": "0.0.0",
"test-rpc": "0.0.0",
"test-utils": "0.0.0",
"blitz": "2.0.0-alpha.0",
"@blitzjs/auth": "2.0.0-alpha.0",
"@blitzjs/next": "2.0.0-alpha.0",
"@blitzjs/rpc": "2.0.0-alpha.0",
"@blitzjs/config": "0.0.0",
"@blitzjs/generator": "2.0.0-alpha.0",
"blitz": "2.0.0-alpha.18",
"@blitzjs/auth": "2.0.0-alpha.18",
"@blitzjs/next": "2.0.0-alpha.18",
"@blitzjs/rpc": "2.0.0-alpha.18",
"@blitzjs/config": "2.0.0-alpha.18",
"@blitzjs/generator": "2.0.0-alpha.18",
"template": "0.0.0"
},
"changesets": ["nine-onions-admire", "ninety-pets-heal", "poor-peas-lick"]
"changesets": [
"breezy-cameras-double",
"dirty-monkeys-greet",
"empty-berries-rule",
"fair-wombats-sneeze",
"flat-bees-approve",
"great-months-train",
"lovely-colts-share",
"nine-onions-admire",
"ninety-pets-heal",
"poor-peas-lick",
"poor-shrimps-think",
"quiet-feet-travel",
"sharp-falcons-begin",
"silent-colts-reply",
"swift-drinks-dress",
"ten-rivers-burn",
"thirty-countries-build",
"twenty-beans-pump",
"two-kiwis-help",
"unlucky-papayas-sleep",
"wicked-ghosts-cough"
]
}

View File

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

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,5 @@
---
"blitz": patch
---
fix more cli problems

View File

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

View File

@@ -0,0 +1,10 @@
---
"blitz": patch
"@blitzjs/auth": patch
"@blitzjs/next": patch
"@blitzjs/rpc": patch
"@blitzjs/config": patch
"@blitzjs/generator": patch
---
new app template

View File

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

View File

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

View File

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

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

@@ -0,0 +1,11 @@
# https://EditorConfig.org
root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

3
apps/toolkit-app/.env Normal file
View File

@@ -0,0 +1,3 @@
# This env file should be checked into source control
# This is the place for default values for all environments
# Values in `.env.local` and `.env.production` will override these values

View File

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

56
apps/toolkit-app/.gitignore vendored Normal file
View File

@@ -0,0 +1,56 @@
# dependencies
node_modules
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
.pnp.*
.npm
web_modules/
# blitz
/.blitz/
/.next/
*.sqlite
*.sqlite-journal
.now
.blitz**
blitz-log.log
# misc
.DS_Store
# local env files
.env.local
.env.*.local
.envrc
# Logs
logs
*.log
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Testing
.coverage
*.lcov
.nyc_output
lib-cov
# Caches
*.tsbuildinfo
.eslintcache
.node_repl_history
.yarn-integrity
# Serverless directories
.serverless/
# Stores VSCode versions used for testing VSCode extensions
.vscode-test

View File

@@ -0,0 +1,9 @@
.gitkeep
.env*
*.ico
*.lock
db/migrations
.next
.yarn
.pnp.*
node_modules

View File

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

175
apps/toolkit-app/README.md Normal file
View File

@@ -0,0 +1,175 @@
TODO
[![Blitz.js](https://raw.githubusercontent.com/blitz-js/art/master/github-cover-photo.png)](https://blitzjs.com)
This is a [Blitz.js](https://github.com/blitz-js/blitz) app.
# ****name****
## Getting Started
Run your app in the development mode.
```
blitz dev
```
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
## Environment Variables
Ensure the `.env.local` file has required environment variables:
```
DATABASE_URL=postgresql://<YOUR_DB_USERNAME>@localhost:5432/__name__
```
Ensure the `.env.test.local` file has required environment variables:
```
DATABASE_URL=postgresql://<YOUR_DB_USERNAME>@localhost:5432/__name___test
```
## Tests
Runs your tests using Jest.
```
yarn test
```
Blitz comes with a test setup using [Jest](https://jestjs.io/) and [react-testing-library](https://testing-library.com/).
## Commands
Blitz comes with a powerful CLI that is designed to make development easy and fast. You can install it with `npm i -g blitz`
```
blitz [COMMAND]
dev Start a development server
build Create a production build
start Start a production server
export Export your Blitz app as a static application
prisma Run prisma commands
generate Generate new files for your Blitz project
console Run the Blitz console REPL
install Install a recipe
help Display help for blitz
test Run project tests
```
You can read more about it on the [CLI Overview](https://blitzjs.com/docs/cli-overview) documentation.
## What's included?
Here is the starting structure of your app.
```
__name__
├── app/
│ ├── api/
│ ├── auth/
│ │ ├── components/
│ │ │ ├── LoginForm.tsx
│ │ │ └── SignupForm.tsx
│ │ ├── mutations/
│ │ │ ├── changePassword.ts
│ │ │ ├── forgotPassword.test.ts
│ │ │ ├── forgotPassword.ts
│ │ │ ├── login.ts
│ │ │ ├── logout.ts
│ │ │ ├── resetPassword.test.ts
│ │ │ ├── resetPassword.ts
│ │ │ └── signup.ts
│ │ ├── pages/
│ │ │ ├── forgot-password.tsx
│ │ │ ├── login.tsx
│ │ │ ├── reset-password.tsx
│ │ │ └── signup.tsx
│ │ └── validations.ts
│ ├── core/
│ │ ├── components/
│ │ │ ├── Form.tsx
│ │ │ └── LabeledTextField.tsx
│ │ ├── hooks/
│ │ │ └── useCurrentUser.ts
│ │ └── layouts/
│ │ └── Layout.tsx
│ ├── pages/
│ │ ├── _app.tsx
│ │ ├── _document.tsx
│ │ ├── 404.tsx
│ │ ├── index.test.tsx
│ │ └── index.tsx
│ └── users/
│ └── queries/
│ └── getCurrentUser.ts
├── db/
│ ├── migrations/
│ ├── index.ts
│ ├── schema.prisma
│ └── seeds.ts
├── integrations/
├── mailers/
│ └── forgotPasswordMailer.ts
├── public/
│ ├── favicon.ico
│ └── logo.png
├── test/
│ ├── setup.ts
│ └── utils.tsx
├── .eslintrc.js
├── babel.config.js
├── blitz.config.ts
├── jest.config.ts
├── package.json
├── README.md
├── tsconfig.json
└── types.ts
```
These files are:
- The `app/` folder is a container for most of your project. This is where youll put any pages or API routes.
- `db/` is where your database configuration goes. If youre writing models or checking migrations, this is where to go.
- `public/` is a folder where you will put any static assets. If you have images, files, or videos which you want to use in your app, this is where to put them.
- `integrations/` is a folder to put all third-party integrations like with Stripe, Sentry, etc.
- `test/` is a folder where you can put test utilities and integration tests.
- `package.json` contains information about your dependencies and devDependencies. If youre using a tool like `npm` or `yarn`, you wont have to worry about this much.
- `tsconfig.json` is our recommended setup for TypeScript.
- `.babel.config.js`, `.eslintrc.js`, `.env`, etc. ("dotfiles") are configuration files for various bits of JavaScript tooling.
- `blitz.config.ts` is for advanced custom configuration of Blitz. [Here you can learn how to use it](https://blitzjs.com/docs/blitz-config).
- `jest.config.js` contains config for Jest tests. You can [customize it if needed](https://jestjs.io/docs/en/configuration).
You can read more about it in the [File Structure](https://blitzjs.com/docs/file-structure) section of the documentation.
### Tools included
Blitz comes with a set of tools that corrects and formats your code, facilitating its future maintenance. You can modify their options and even uninstall them.
- **ESLint**: It lints your code: searches for bad practices and tell you about it. You can customize it via the `.eslintrc.js`, and you can install (or even write) plugins to have it the way you like it. It already comes with the [`blitz`](https://github.com/blitz-js/blitz/tree/canary/packages/eslint-config) config, but you can remove it safely. [Learn More](https://blitzjs.com/docs/eslint-config).
- **Husky**: It adds [githooks](https://git-scm.com/docs/githooks), little pieces of code that get executed when certain Git events are triggerd. For example, `pre-commit` is triggered just before a commit is created. You can see the current hooks inside `.husky/`. If are having problems commiting and pushing, check out ther [troubleshooting](https://typicode.github.io/husky/#/?id=troubleshoot) guide. [Learn More](https://blitzjs.com/docs/husky-config).
- **Prettier**: It formats your code to look the same everywhere. You can configure it via the `.prettierrc` file. The `.prettierignore` contains the files that should be ignored by Prettier; useful when you have large files or when you want to keep a custom formatting. [Learn More](https://blitzjs.com/docs/prettier-config).
## Learn more
Read the [Blitz.js Documentation](https://blitzjs.com/docs/getting-started) to learn more.
The Blitz community is warm, safe, diverse, inclusive, and fun! Feel free to reach out to us in any of our communication channels.
- [Website](https://blitzjs.com)
- [Discord](https://blitzjs.com/discord)
- [Report an issue](https://github.com/blitz-js/blitz/issues/new/choose)
- [Forum discussions](https://github.com/blitz-js/blitz/discussions)
- [How to Contribute](https://blitzjs.com/docs/contributing)
- [Sponsor or donate](https://github.com/blitz-js/blitz#sponsors-and-donations)

View File

@@ -0,0 +1,59 @@
import { AuthenticationError, PromiseReturnType } from "blitz"
import Link from "next/link"
import { LabeledTextField } from "app/core/components/LabeledTextField"
import { Form, FORM_ERROR } from "app/core/components/Form"
import login from "app/auth/mutations/login"
import { Login } from "app/auth/validations"
import { useMutation } from "@blitzjs/rpc"
import { Routes } from "@blitzjs/next"
type LoginFormProps = {
onSuccess?: (user: PromiseReturnType<typeof login>) => void
}
export const LoginForm = (props: LoginFormProps) => {
const [loginMutation] = useMutation(login)
return (
<div>
<h1>Login</h1>
<Form
submitText="Login"
schema={Login}
initialValues={{ email: "", password: "" }}
onSubmit={async (values) => {
try {
const user = await loginMutation(values)
props.onSuccess?.(user)
} catch (error: any) {
if (error instanceof AuthenticationError) {
return { [FORM_ERROR]: "Sorry, those credentials are invalid" }
} else {
return {
[FORM_ERROR]:
"Sorry, we had an unexpected error. Please try again. - " + error.toString(),
}
}
}
}}
>
<LabeledTextField name="email" label="Email" placeholder="Email" />
<LabeledTextField name="password" label="Password" placeholder="Password" type="password" />
<div>
<Link href={Routes.ForgotPasswordPage()} passHref>
<a>Forgot your password?</a>
</Link>
</div>
</Form>
<div style={{ marginTop: "1rem" }}>
Or{" "}
<Link href={Routes.SignupPage()} passHref>
<a>Sign Up</a>
</Link>
</div>
</div>
)
}
export default LoginForm

View File

@@ -0,0 +1,42 @@
import { LabeledTextField } from "app/core/components/LabeledTextField"
import { Form, FORM_ERROR } from "app/core/components/Form"
import signup from "app/auth/mutations/signup"
import { Signup } from "app/auth/validations"
import { useMutation } from "@blitzjs/rpc"
type SignupFormProps = {
onSuccess?: () => void
}
export const SignupForm = (props: SignupFormProps) => {
const [signupMutation] = useMutation(signup)
return (
<div>
<h1>Create an Account</h1>
<Form
submitText="Create Account"
schema={Signup}
initialValues={{ email: "", password: "" }}
onSubmit={async (values) => {
try {
await signupMutation(values)
props.onSuccess?.()
} catch (error: any) {
if (error.code === "P2002" && error.meta?.target?.includes("email")) {
// This error comes from Prisma
return { email: "This email is already being used" }
} else {
return { [FORM_ERROR]: error.toString() }
}
}
}}
>
<LabeledTextField name="email" label="Email" placeholder="Email" />
<LabeledTextField name="password" label="Password" placeholder="Password" type="password" />
</Form>
</div>
)
}
export default SignupForm

View File

@@ -0,0 +1,25 @@
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 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()
await authenticateUser(user.email, currentPassword)
const hashedPassword = await SecurePassword.hash(newPassword.trim())
await db.user.update({
where: { id: user.id },
data: { hashedPassword },
})
return true
}
)

View File

@@ -0,0 +1,42 @@
import { generateToken, hash256 } from "@blitzjs/auth"
import { resolver } from "@blitzjs/rpc"
import { db } from "db"
import { forgotPasswordMailer } from "mailers/forgotPasswordMailer"
import { ForgotPassword } from "../validations"
const RESET_PASSWORD_TOKEN_EXPIRATION_IN_HOURS = 4
export default resolver.pipe(resolver.zod(ForgotPassword), async ({ email }) => {
// 1. Get the user
const user = await db.user.findFirst({ where: { email: email.toLowerCase() } })
// 2. Generate the token and expiration date.
const token = generateToken()
const hashedToken = hash256(token)
const expiresAt = new Date()
expiresAt.setHours(expiresAt.getHours() + RESET_PASSWORD_TOKEN_EXPIRATION_IN_HOURS)
// 3. If user with this email was found
if (user) {
// 4. Delete any existing password reset tokens
await db.token.deleteMany({ where: { type: "RESET_PASSWORD", userId: user.id } })
// 5. Save this new token in the database.
await db.token.create({
data: {
user: { connect: { id: user.id } },
type: "RESET_PASSWORD",
expiresAt,
hashedToken,
sentTo: user.email,
},
})
// 6. Send the email
await forgotPasswordMailer({ to: user.email, token }).send()
} else {
// 7. If no user found wait the same time so attackers can't tell the difference
await new Promise((resolve) => setTimeout(resolve, 750))
}
// 8. Return the same result whether a password reset email was sent or not
return
})

View File

@@ -0,0 +1,32 @@
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 db.user.findFirst({ where: { email } })
if (!user) throw new AuthenticationError()
const result = await SecurePassword.verify(user.hashedPassword, password)
if (result === SecurePassword.VALID_NEEDS_REHASH) {
// Upgrade hashed password with a more secure hash
const improvedHash = await SecurePassword.hash(password)
await db.user.update({ where: { id: user.id }, data: { hashedPassword: improvedHash } })
}
const { hashedPassword, ...rest } = user
return rest
}
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

@@ -0,0 +1,5 @@
import { Ctx } from "blitz"
export default async function logout(_: any, ctx: Ctx) {
return await ctx.session.$revoke()
}

View File

@@ -0,0 +1,48 @@
import { SecurePassword, hash256 } from "@blitzjs/auth"
import { db } from "db"
import { ResetPassword } from "../validations"
import login from "./login"
export class ResetPasswordError extends Error {
name = "ResetPasswordError"
message = "Reset password link is invalid or it has expired."
}
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 db.token.findFirst({
where: { hashedToken, type: "RESET_PASSWORD" },
include: { user: true },
})
// 2. If token not found, error
if (!possibleToken) {
throw new ResetPasswordError()
}
const savedToken = possibleToken
// 3. Delete token so it can't be used again
await db.token.delete({ where: { id: savedToken.id } })
// 4. If token has expired, error
if (savedToken.expiresAt < new Date()) {
throw new ResetPasswordError()
}
// 5. Since token is valid, now we can update the user's password
const hashedPassword = await SecurePassword.hash(input.password.trim())
const user = await db.user.update({
where: { id: savedToken.userId },
data: { hashedPassword },
})
// 6. Revoke all existing login sessions for this user
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)
return true
}

View File

@@ -0,0 +1,19 @@
import { db } from "db"
import { SecurePassword } from "@blitzjs/auth"
export default async function signup(input, ctx) {
const blitzContext = 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 db.user.create({
data: { email, hashedPassword, role: "user" },
select: { id: true, name: true, email: true, role: true },
})
await blitzContext.session.$create({
userId: user.id,
})
return { userId: blitzContext.session.userId, ...user, email: input.email }
}

View File

@@ -0,0 +1,42 @@
import { z } from "zod"
export const email = z
.string()
.email()
.transform((str) => str.toLowerCase().trim())
export const password = z
.string()
.min(10)
.max(100)
.transform((str) => str.trim())
export const Signup = z.object({
email,
password,
})
export const Login = z.object({
email,
password: z.string(),
})
export const ForgotPassword = z.object({
email,
})
export const ResetPassword = z
.object({
password: password,
passwordConfirmation: password,
token: z.string(),
})
.refine((data) => data.password === data.passwordConfirmation, {
message: "Passwords don't match",
path: ["passwordConfirmation"], // set the path of the error
})
export const ChangePassword = z.object({
currentPassword: z.string(),
newPassword: password,
})

View File

@@ -0,0 +1,18 @@
import { AuthClientPlugin } from "@blitzjs/auth"
import { setupBlitzClient } from "@blitzjs/next"
import { BlitzRpcPlugin } from "@blitzjs/rpc"
export const { withBlitz } = setupBlitzClient({
plugins: [
AuthClientPlugin({
cookiePrefix: "web-cookie-prefix",
}),
BlitzRpcPlugin({
reactQueryOptions: {
queries: {
staleTime: 7000,
},
},
}),
],
})

View File

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

View File

@@ -0,0 +1,83 @@
import { useState, ReactNode, PropsWithoutRef } from "react"
import { FormProvider, useForm, UseFormProps } from "react-hook-form"
import { zodResolver } from "@hookform/resolvers/zod"
import { z } from "zod"
export interface FormProps<S extends z.ZodType<any, any>>
extends Omit<PropsWithoutRef<JSX.IntrinsicElements["form"]>, "onSubmit"> {
/** All your form fields */
children?: ReactNode
/** Text to display in the submit button */
submitText?: string
schema?: S
onSubmit: (values: z.infer<S>) => Promise<void | OnSubmitResult>
initialValues?: UseFormProps<z.infer<S>>["defaultValues"]
}
interface OnSubmitResult {
FORM_ERROR?: string
[prop: string]: any
}
export const FORM_ERROR = "FORM_ERROR"
export function Form<S extends z.ZodType<any, any>>({
children,
submitText,
schema,
initialValues,
onSubmit,
...props
}: FormProps<S>) {
const ctx = useForm<z.infer<S>>({
mode: "onBlur",
resolver: schema ? zodResolver(schema) : undefined,
defaultValues: initialValues,
})
const [formError, setFormError] = useState<string | null>(null)
return (
<FormProvider {...ctx}>
<form
onSubmit={ctx.handleSubmit(async (values) => {
const result = (await onSubmit(values)) || {}
for (const [key, value] of Object.entries(result)) {
if (key === FORM_ERROR) {
setFormError(value)
} else {
ctx.setError(key as any, {
type: "submit",
message: value,
})
}
}
})}
className="form"
{...props}
>
{/* Form fields supplied as children are rendered here */}
{children}
{formError && (
<div role="alert" style={{ color: "red" }}>
{formError}
</div>
)}
{submitText && (
<button type="submit" disabled={ctx.formState.isSubmitting}>
{submitText}
</button>
)}
<style global jsx>{`
.form > * + * {
margin-top: 1rem;
}
`}</style>
</form>
</FormProvider>
)
}
export default Form

View File

@@ -0,0 +1,59 @@
import { forwardRef, PropsWithoutRef, ComponentPropsWithoutRef } from "react"
import { useFormContext } from "react-hook-form"
export interface LabeledTextFieldProps extends PropsWithoutRef<JSX.IntrinsicElements["input"]> {
/** Field name. */
name: string
/** Field label. */
label: string
/** Field type. Doesn't include radio buttons and checkboxes */
type?: "text" | "password" | "email" | "number"
outerProps?: PropsWithoutRef<JSX.IntrinsicElements["div"]>
labelProps?: ComponentPropsWithoutRef<"label">
}
export const LabeledTextField = forwardRef<HTMLInputElement, LabeledTextFieldProps>(
({ label, outerProps, labelProps, name, ...props }, ref) => {
const {
register,
formState: { isSubmitting, errors },
} = useFormContext()
const error = Array.isArray(errors[name])
? errors[name].join(", ")
: errors[name]?.message || errors[name]
return (
<div {...outerProps}>
<label {...labelProps}>
{label}
<input disabled={isSubmitting} {...register(name)} {...props} />
</label>
{error && (
<div role="alert" style={{ color: "red" }}>
{error}
</div>
)}
<style jsx>{`
label {
display: flex;
flex-direction: column;
align-items: start;
font-size: 1rem;
}
input {
font-size: 1rem;
padding: 0.25rem 0.5rem;
border-radius: 3px;
border: 1px solid purple;
appearance: none;
margin-top: 0.5rem;
}
`}</style>
</div>
)
}
)
export default LabeledTextField

View File

@@ -0,0 +1,7 @@
import { useQuery } from "@blitzjs/rpc"
import getCurrentUser from "app/users/queries/getCurrentUser"
export const useCurrentUser = () => {
const [user] = useQuery(getCurrentUser, null)
return user
}

View File

@@ -0,0 +1,17 @@
import Head from "next/head"
import React, { FC } from "react"
const Layout: FC<{ title?: string; children?: React.ReactNode }> = ({ title, children }) => {
return (
<>
<Head>
<title>{title || "__name__"}</title>
<link rel="icon" href="/favicon.ico" />
</Head>
{children}
</>
)
}
export default Layout

View File

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

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,65 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
datasource db {
provider = "sqlite"
url = "file:./db.sqlite"
}
generator client {
provider = "prisma-client-js"
}
// --------------------------------------
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String?
email String @unique
hashedPassword String?
role String @default("USER")
tokens Token[]
sessions Session[]
}
model Session {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
expiresAt DateTime?
handle String @unique
hashedSessionToken String?
antiCSRFToken String?
publicData String?
privateData String?
user User? @relation(fields: [userId], references: [id])
userId Int?
}
model Token {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
hashedToken String
type String
// See note below about TokenType enum
// type TokenType
expiresAt DateTime
sentTo String
user User @relation(fields: [userId], references: [id])
userId Int
@@unique([hashedToken, type])
}
// NOTE: It's highly recommended to use an enum for the token type
// but enums only work in Postgres.
// See: https://blitzjs.com/docs/database-overview#switch-to-postgre-sql
// enum TokenType {
// RESET_PASSWORD
// }

View File

@@ -0,0 +1,15 @@
// import db from "./index"
/*
* This seed function is executed when you run `blitz db seed`.
*
* Probably you want to use a library like https://chancejs.com
* to easily generate realistic data.
*/
const seed = async () => {
// for (let i = 0; i < 5; i++) {
// await db.project.create({ data: { name: "Project " + i } })
// }
}
export default seed

View File

View File

@@ -0,0 +1,11 @@
const nextJest = require("@blitzjs/next/jest")
const createJestConfig = nextJest({
dir: "./",
})
const customJestConfig = {
testEnvironment: "jest-environment-jsdom",
}
module.exports = createJestConfig(customJestConfig)

View File

@@ -0,0 +1,5 @@
{
"compilerOptions": {
"baseUrl": "."
}
}

View File

View File

@@ -0,0 +1,45 @@
/* TODO - You need to add a mailer integration in `integrations/` and import here.
*
* The integration file can be very simple. Instantiate the email client
* and then export it. That way you can import here and anywhere else
* and use it straight away.
*/
type ResetPasswordMailer = {
to: string
token: string
}
export function forgotPasswordMailer({ to, token }: ResetPasswordMailer) {
// In production, set APP_ORIGIN to your production server origin
const origin = process.env.APP_ORIGIN || process.env.BLITZ_DEV_SERVER_ORIGIN
const resetUrl = `${origin}/reset-password?token=${token}`
const msg = {
from: "TODO@example.com",
to,
subject: "Your Password Reset Instructions",
html: `
<h1>Reset Your Password</h1>
<h3>NOTE: You must set up a production email integration in mailers/forgotPasswordMailer.ts</h3>
<a href="${resetUrl}">
Click here to set a new password
</a>
`,
}
return {
async send() {
if (process.env.NODE_ENV === "production") {
// TODO - send the production email, like this:
// await postmark.sendEmail(msg)
throw new Error("No production email implementation in mailers/forgotPasswordMailer")
} else {
// Preview email in the browser
const previewEmail = (await import("preview-email")).default
await previewEmail(msg)
}
},
}
}

View File

@@ -1,5 +1,4 @@
/// <reference types="next" />
/// <reference types="next/types/global" />
/// <reference types="next/image-types/global" />
// NOTE: This file should not be edited

View File

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

7
apps/toolkit-app/npmrc Normal file
View File

@@ -0,0 +1,7 @@
save-exact=true
legacy-peer-deps=true
public-hoist-pattern[]=next
public-hoist-pattern[]=secure-password
public-hoist-pattern[]=*jest*
public-hoist-pattern[]=@testing-library/*

View File

@@ -0,0 +1,60 @@
{
"name": "toolkit-app",
"version": "1.0.1-alpha.14",
"scripts": {
"start:dev": "pnpm run prisma:start && next dev",
"buildapp": "pnpm i && pnpm prisma generate && next build",
"start": "next start",
"lint": "next lint",
"prisma:start": "prisma generate && prisma migrate deploy",
"prisma:studio": "prisma studio",
"test:local": "jest"
},
"prisma": {
"schema": "db/schema.prisma"
},
"prettier": {
"semi": false,
"printWidth": 100
},
"lint-staged": {
"*.{js}": [
"eslint --fix"
]
},
"dependencies": {
"@blitzjs/auth": "workspace:*",
"@blitzjs/config": "workspace:*",
"@blitzjs/next": "workspace:*",
"@blitzjs/rpc": "workspace:*",
"@hookform/resolvers": "2.8.8",
"@prisma/client": "3.9.0",
"blitz": "workspace:2.0.0-alpha.19",
"next": "12.1.6-canary.17",
"prisma": "3.9.0",
"react": "18.0.0",
"react-dom": "18.0.0",
"react-hook-form": "7.29.0",
"ts-node": "10.7.0",
"zod": "3.10.1"
},
"devDependencies": {
"@next/bundle-analyzer": "12.0.8",
"@testing-library/react": "13.0.0",
"@testing-library/react-hooks": "7.0.2",
"@types/jest": "27.4.1",
"@types/node": "17.0.16",
"@types/preview-email": "2.0.1",
"@types/react": "17.0.43",
"eslint": "7.32.0",
"husky": "7.0.4",
"jest": "27.5.1",
"lint-staged": "12.1.7",
"prettier": "^2.5.1",
"prettier-plugin-prisma": "3.8.0",
"pretty-quick": "3.1.3",
"preview-email": "3.x",
"typescript": "^4.5.3"
},
"private": true
}

View File

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

View File

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

View File

@@ -1,15 +1,15 @@
import { BlitzPage, useMutation } from "blitz"
import Layout from "app/core/layouts/Layout"
import { LabeledTextField } from "app/core/components/LabeledTextField"
import { Form, FORM_ERROR } from "app/core/components/Form"
import { ForgotPassword } from "app/auth/validations"
import forgotPassword from "app/auth/mutations/forgotPassword"
import { useMutation } from "@blitzjs/rpc"
const ForgotPasswordPage: BlitzPage = () => {
const ForgotPasswordPage = () => {
const [forgotPasswordMutation, { isSuccess }] = useMutation(forgotPassword)
return (
<div>
<Layout title="Forgot Your Password?">
<h1>Forgot your password?</h1>
{isSuccess ? (
@@ -38,11 +38,8 @@ const ForgotPasswordPage: BlitzPage = () => {
<LabeledTextField name="email" label="Email" placeholder="Email" />
</Form>
)}
</div>
</Layout>
)
}
ForgotPasswordPage.redirectAuthenticatedTo = "/"
ForgotPasswordPage.getLayout = (page) => <Layout title="Forgot Your Password?">{page}</Layout>
export default ForgotPasswordPage

View File

@@ -1,23 +1,20 @@
import { useRouter, BlitzPage } from "blitz"
import Layout from "app/core/layouts/Layout"
import { LoginForm } from "app/auth/components/LoginForm"
import { useRouter } from "next/router"
const LoginPage: BlitzPage = () => {
const LoginPage = () => {
const router = useRouter()
return (
<div>
<Layout title="Log In">
<LoginForm
onSuccess={(_user) => {
const next = router.query.next ? decodeURIComponent(router.query.next as string) : "/"
router.push(next)
return router.push(next)
}}
/>
</div>
</Layout>
)
}
LoginPage.redirectAuthenticatedTo = "/"
LoginPage.getLayout = (page) => <Layout title="Log In">{page}</Layout>
export default LoginPage

View File

@@ -1,18 +1,16 @@
import { useRouter, BlitzPage, Routes } from "blitz"
import { useRouter } from "next/router"
import Layout from "app/core/layouts/Layout"
import { SignupForm } from "app/auth/components/SignupForm"
import { Routes } from "@blitzjs/next"
const SignupPage: BlitzPage = () => {
const SignupPage = () => {
const router = useRouter()
return (
<div>
<Layout title="Sign Up">
<SignupForm onSuccess={() => router.push(Routes.Home())} />
</div>
</Layout>
)
}
SignupPage.redirectAuthenticatedTo = "/"
SignupPage.getLayout = (page) => <Layout title="Sign Up">{page}</Layout>
export default SignupPage

View File

@@ -0,0 +1,274 @@
import { Suspense } from "react"
import Image from "next/image"
import Link from "next/link"
import Layout from "app/core/layouts/Layout"
import { useCurrentUser } from "app/core/hooks/useCurrentUser"
import logout from "app/auth/mutations/logout"
import logo from "public/logo.png"
import { useMutation } from "@blitzjs/rpc"
import { Routes } from "@blitzjs/next"
/*
* This file is just for a pleasant getting started page for your new app.
* You can delete everything in here and start from scratch if you like.
*/
const UserInfo = () => {
const currentUser = useCurrentUser()
const [logoutMutation] = useMutation(logout)
if (currentUser) {
return (
<>
<button
className="button small"
onClick={async () => {
await logoutMutation()
}}
>
Logout
</button>
<div>
User id: <code>{currentUser.id}</code>
<br />
User role: <code>{currentUser.role}</code>
</div>
</>
)
} else {
return (
<>
<Link href={Routes.SignupPage()} passHref>
<a className="button small">
<strong>Sign Up</strong>
</a>
</Link>
<Link href={Routes.LoginPage()} passHref>
<a className="button small">
<strong>Login</strong>
</a>
</Link>
</>
)
}
}
const Home = () => {
return (
<Layout title="Home">
<div className="container">
<main>
<div className="logo">
<Image src={`${logo.src}`} alt="blitzjs" width="256px" height="118px" layout="fixed" />
</div>
<p>
<strong>Congrats!</strong> Your app is ready, including user sign-up and log-in.
</p>
<div className="buttons" style={{ marginTop: "1rem", marginBottom: "1rem" }}>
<Suspense fallback="Loading...">
<UserInfo />
</Suspense>
</div>
<p>
<strong>
To add a new model to your app, <br />
run the following in your terminal:
</strong>
</p>
<pre>
<code>blitz generate all project name:string</code>
</pre>
<div style={{ marginBottom: "1rem" }}>(And select Yes to run prisma migrate)</div>
<div>
<p>
Then <strong>restart the server</strong>
</p>
<pre>
<code>Ctrl + c</code>
</pre>
<pre>
<code>blitz dev</code>
</pre>
<p>
and go to{" "}
<Link href="/projects">
<a>/projects</a>
</Link>
</p>
</div>
<div className="buttons" style={{ marginTop: "5rem" }}>
<a
className="button"
href="https://blitzjs.com/docs/getting-started?utm_source=blitz-new&utm_medium=app-template&utm_campaign=blitz-new"
target="_blank"
rel="noopener noreferrer"
>
Documentation
</a>
<a
className="button-outline"
href="https://github.com/blitz-js/blitz"
target="_blank"
rel="noopener noreferrer"
>
Github Repo
</a>
<a
className="button-outline"
href="https://discord.blitzjs.com"
target="_blank"
rel="noopener noreferrer"
>
Discord Community
</a>
</div>
</main>
<footer>
<a
href="https://blitzjs.com?utm_source=blitz-new&utm_medium=app-template&utm_campaign=blitz-new"
target="_blank"
rel="noopener noreferrer"
>
Powered by Blitz.js
</a>
</footer>
<style jsx global>{`
@import url("https://fonts.googleapis.com/css2?family=Libre+Franklin:wght@300;700&display=swap");
html,
body {
padding: 0;
margin: 0;
font-family: "Libre Franklin", -apple-system, BlinkMacSystemFont, Segoe UI, Roboto,
Oxygen, Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
}
* {
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
box-sizing: border-box;
}
.container {
min-height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
main {
padding: 5rem 0;
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
main p {
font-size: 1.2rem;
}
p {
text-align: center;
}
footer {
width: 100%;
height: 60px;
border-top: 1px solid #eaeaea;
display: flex;
justify-content: center;
align-items: center;
background-color: #45009d;
}
footer a {
display: flex;
justify-content: center;
align-items: center;
}
footer a {
color: #f4f4f4;
text-decoration: none;
}
.logo {
margin-bottom: 2rem;
}
.logo img {
width: 300px;
}
.buttons {
display: grid;
grid-auto-flow: column;
grid-gap: 0.5rem;
}
.button {
font-size: 1rem;
background-color: #6700eb;
padding: 1rem 2rem;
color: #f4f4f4;
text-align: center;
}
.button.small {
padding: 0.5rem 1rem;
}
.button:hover {
background-color: #45009d;
}
.button-outline {
border: 2px solid #6700eb;
padding: 1rem 2rem;
color: #6700eb;
text-align: center;
}
.button-outline:hover {
border-color: #45009d;
color: #45009d;
}
pre {
background: #fafafa;
border-radius: 5px;
padding: 0.75rem;
text-align: center;
}
code {
font-size: 0.9rem;
font-family: Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono,
Bitstream Vera Sans Mono, Courier New, monospace;
}
.grid {
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
max-width: 800px;
margin-top: 3rem;
}
@media (max-width: 600px) {
.grid {
width: 100%;
flex-direction: column;
}
}
`}</style>
</div>
</Layout>
)
}
export default Home

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

View File

@@ -0,0 +1,4 @@
// This is the jest 'setupFilesAfterEnv' setup file
// It's a good place to set globals, add global before/after hooks, etc
export {} // so TS doesn't complain

View File

@@ -0,0 +1,24 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"baseUrl": ".",
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"strictNullChecks": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"noUncheckedIndexedAccess": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"tsBuildInfoFile": ".tsbuildinfo"
},
"exclude": ["node_modules", "**/*.e2e.ts", "cypress"],
"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

@@ -21,7 +21,7 @@
"@types/jest": "27.4.1",
"blitz": "workspace:*",
"jest": "27.5.1",
"next": "12.1.1",
"next": "12.1.6-canary.17",
"prisma": "3.9.0",
"react": "18.0.0",
"react-dom": "18.0.0",

View File

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

View File

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

View File

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

@@ -19,7 +19,7 @@
"@prisma/client": "3.9.0",
"blitz": "workspace:*",
"lowdb": "3.0.0",
"next": "12.1.1",
"next": "12.1.6-canary.17",
"prisma": "3.9.0",
"react": "18.0.0",
"react-dom": "18.0.0"

View File

@@ -12,7 +12,7 @@
"@blitzjs/next": "workspace:*",
"@blitzjs/rpc": "workspace:*",
"blitz": "workspace:*",
"next": "12.1.1",
"next": "12.1.6-canary.17",
"react": "18.0.0",
"react-dom": "18.0.0"
},

View File

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

View File

@@ -1,5 +1,114 @@
# @blitzjs/auth
## 2.0.0-alpha.19
### Patch Changes
- blitz@2.0.0-alpha.19
## 2.0.0-alpha.18
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.18
## 2.0.0-alpha.17
### Patch Changes
- blitz@2.0.0-alpha.17
## 2.0.0-alpha.16
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.16
## 2.0.0-alpha.15
### Patch Changes
- blitz@2.0.0-alpha.15
## 2.0.0-alpha.14
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.14
## 2.0.0-alpha.13
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.13
## 2.0.0-alpha.12
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.12
## 2.0.0-alpha.11
### Patch Changes
- blitz@2.0.0-alpha.11
## 2.0.0-alpha.10
### Patch Changes
- blitz@2.0.0-alpha.10
## 2.0.0-alpha.9
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.9
## 2.0.0-alpha.8
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.8
## 2.0.0-alpha.7
### Patch Changes
- 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
- new app template
- Updated dependencies
- blitz@2.0.0-alpha.5
## 2.0.0-alpha.4
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.4
## 2.0.0-alpha.3
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/auth",
"version": "2.0.0-alpha.3",
"version": "2.0.0-alpha.19",
"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.19",
"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.19",
"@testing-library/react": "13.0.0",
"@testing-library/react-hooks": "7.0.2",
"@types/cookie": "0.4.1",

View File

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

View File

@@ -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,109 @@
# @blitzjs/next
## 2.0.0-alpha.19
### Patch Changes
- added superjson
- 2150dcc3: Setup SuperJson for GSSP and GSP
- @blitzjs/rpc@2.0.0-alpha.19
## 2.0.0-alpha.18
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.18
## 2.0.0-alpha.17
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.17
## 2.0.0-alpha.16
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.16
## 2.0.0-alpha.15
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.15
## 2.0.0-alpha.14
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.14
## 2.0.0-alpha.13
### Patch Changes
- Fix codegen and postinstall to make work with pnpm
- @blitzjs/rpc@2.0.0-alpha.13
## 2.0.0-alpha.12
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.12
## 2.0.0-alpha.11
### Patch Changes
- Fix postinstall script not being found
- @blitzjs/rpc@2.0.0-alpha.11
## 2.0.0-alpha.10
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.10
## 2.0.0-alpha.9
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.9
## 2.0.0-alpha.8
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.8
## 2.0.0-alpha.7
### Patch Changes
- @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
- new app template
- Updated dependencies
- @blitzjs/rpc@2.0.0-alpha.5
## 2.0.0-alpha.4
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.4
## 2.0.0-alpha.3
### 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.3",
"version": "2.0.0-alpha.19",
"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 scripts/postinstall.js"
},
"main": "./dist/index-server.cjs",
"module": "./dist/index-server.mjs",
@@ -17,16 +18,21 @@
"sideEffects": false,
"license": "MIT",
"files": [
"dist/**"
"dist/**",
"scripts/**",
"eslint.js"
],
"dependencies": {
"@blitzjs/rpc": "2.0.0-alpha.3",
"@blitzjs/rpc": "2.0.0-alpha.19",
"@types/hoist-non-react-statics": "3.3.1",
"debug": "4.3.3",
"fs-extra": "10.0.1",
"react-query": "3.21.1"
"hoist-non-react-statics": "3.3.2",
"react-query": "3.21.1",
"superjson": "1.8.0"
},
"devDependencies": {
"@blitzjs/config": "workspace:*",
"@blitzjs/config": "workspace:2.0.0-alpha.19",
"@testing-library/dom": "8.13.0",
"@testing-library/jest-dom": "5.16.3",
"@testing-library/react": "13.0.0",
@@ -37,11 +43,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.19",
"cross-spawn": "7.0.3",
"find-up": "4.1.0",
"lodash.frompairs": "4.0.1",
"next": "12.1.1",
"next": "12.1.6-canary.17",
"react": "18.0.0",
"react-dom": "18.0.0",
"resolve-from": "5.0.0",
"ts-jest": "27.1.4",
"typescript": "^4.5.3",
"unbuild": "0.6.9",

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,342 @@
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, "../../blitz-next"))
let isInstalledGlobally = isInBlitzMonorepo ? false : true // default
try {
const maybeGlobalBlitzPath = resolveFrom(__dirname, "blitz")
const localBlitzPath = resolveFrom.silent(process.cwd(), "blitz/dist/index.cjs")
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 if (src.includes(".pnpm")) {
const blitzPkgLocation = path.dirname(
(await findUp("package.json", {
cwd: resolveFrom(src, "blitz"),
})) || "",
)
if (!blitzPkgLocation) {
throw new Error("Internal Blitz Error: unable to find 'blitz' package location")
}
root = path.join(blitzPkgLocation, "../../../../")
} else {
const blitzPkgLocation = path.dirname(
(await findUp("package.json", {
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/dist/index.cjs")
if (blitzPkg.includes(".pnpm")) {
return path.join(blitzPkg, "../../../../../../blitz/dist/index.cjs")
} else {
return path.join(blitzPkg)
}
}
} 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

@@ -10,9 +10,11 @@ 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'
export * from "./error-boundary"
export * from "./error-component"
export {Routes} from ".blitz"
const compose =
(...rest: BlitzProviderType[]) =>
@@ -46,7 +48,7 @@ const buildWithBlitz = <TPlugins extends readonly ClientPlugin<object>[]>(plugin
</BlitzProvider>
)
}
return BlitzOuterRoot
return withSuperJSONPage(BlitzOuterRoot)
}
}
@@ -88,7 +90,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 +123,7 @@ const setupClient = <TPlugins extends readonly ClientPlugin<object>[]>({
}
}
export {setupClient}
export {setupBlitzClient}
const customCSS = `
body::before {

View File

@@ -1,6 +1,7 @@
import {GetServerSideProps, GetStaticProps, NextApiRequest, NextApiResponse} from "next"
import type {Ctx as BlitzCtx, BlitzServerPlugin, Middleware, MiddlewareResponse} from "blitz"
import {handleRequestWithMiddleware} from "blitz"
import {withSuperJSONPropsGsp, withSuperJSONPropsGssp} from "./superjson"
export * from "./index-browser"
@@ -27,10 +28,10 @@ export type BlitzGSSPHandler<TProps> = ({
...args
}: Parameters<GetServerSideProps<TProps>>[0] & {ctx: Ctx}) => ReturnType<GetServerSideProps<TProps>>
export type BlitzGSPHandler = ({
export type BlitzGSPHandler<TProps> = ({
ctx,
...args
}: Parameters<GetStaticProps>[0] & {ctx: Ctx}) => ReturnType<GetServerSideProps>
}: Parameters<GetStaticProps<TProps>>[0] & {ctx: Ctx}) => ReturnType<GetStaticProps<TProps>>
export type BlitzAPIHandler = (
req: Parameters<NextApiHandler>[0],
@@ -38,27 +39,25 @@ 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)
const gSSP =
<TProps>(handler: BlitzGSSPHandler<TProps>): GetServerSideProps<TProps> =>
async ({req, res, ...rest}) => {
const gSSP = <TProps>(handler: BlitzGSSPHandler<TProps>): GetServerSideProps<TProps> =>
withSuperJSONPropsGssp<TProps>(async ({req, res, ...rest}) => {
await handleRequestWithMiddleware(req, res, middlewares)
const ctx = contextMiddleware.reduceRight(
(y, f) => (f ? f(y) : y),
(res as MiddlewareResponse).blitzCtx,
)
return handler({req, res, ctx, ...rest})
}
})
const gSP =
(handler: BlitzGSPHandler): GetStaticProps =>
async (context) => {
const gSP = <TProps>(handler: BlitzGSPHandler<TProps>): GetStaticProps<TProps> =>
withSuperJSONPropsGsp<TProps>(async (context) => {
const ctx = contextMiddleware.reduceRight((y, f) => (f ? f(y) : y), {} as Ctx)
return handler({...context, ctx: ctx})
}
})
const api =
(handler: BlitzAPIHandler): NextApiHandler =>

View File

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

View File

@@ -1,5 +1,130 @@
# @blitzjs/rpc
## 2.0.0-alpha.19
### Patch Changes
- blitz@2.0.0-alpha.19
- @blitzjs/auth@2.0.0-alpha.19
## 2.0.0-alpha.18
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.18
- @blitzjs/auth@2.0.0-alpha.18
## 2.0.0-alpha.17
### Patch Changes
- blitz@2.0.0-alpha.17
- @blitzjs/auth@2.0.0-alpha.17
## 2.0.0-alpha.16
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.16
- @blitzjs/auth@2.0.0-alpha.16
## 2.0.0-alpha.15
### Patch Changes
- blitz@2.0.0-alpha.15
- @blitzjs/auth@2.0.0-alpha.15
## 2.0.0-alpha.14
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.14
- @blitzjs/auth@2.0.0-alpha.14
## 2.0.0-alpha.13
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.13
- @blitzjs/auth@2.0.0-alpha.13
## 2.0.0-alpha.12
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.12
- @blitzjs/auth@2.0.0-alpha.12
## 2.0.0-alpha.11
### Patch Changes
- @blitzjs/auth@2.0.0-alpha.11
- blitz@2.0.0-alpha.11
## 2.0.0-alpha.10
### Patch Changes
- blitz@2.0.0-alpha.10
- @blitzjs/auth@2.0.0-alpha.10
## 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
- new app template
- Updated dependencies
- blitz@2.0.0-alpha.5
- @blitzjs/auth@2.0.0-alpha.5
## 2.0.0-alpha.4
### Patch Changes
- Updated dependencies
- blitz@2.0.0-alpha.4
- @blitzjs/auth@2.0.0-alpha.4
## 2.0.0-alpha.3
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/rpc",
"version": "2.0.0-alpha.3",
"version": "2.0.0-alpha.19",
"scripts": {
"build": "unbuild",
"predev": "wait-on -d 250 ../blitz/dist/index-server.d.ts && wait-on -d 250 ../blitz-auth/dist/index-browser.d.ts",
@@ -20,21 +20,22 @@
"dist/**"
],
"dependencies": {
"@blitzjs/auth": "2.0.0-alpha.3",
"@blitzjs/auth": "2.0.0-alpha.19",
"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.19",
"@types/debug": "4.1.7",
"@types/react": "17.0.43",
"@types/react-dom": "17.0.14",
"blitz": "2.0.0-alpha.3",
"next": "12.1.1",
"blitz": "2.0.0-alpha.19",
"next": "12.1.6-canary.17",
"react": "18.0.0",
"react-dom": "18.0.0",
"typescript": "^4.5.3",
@@ -42,7 +43,7 @@
"watch": "1.0.2"
},
"peerDependencies": {
"blitz": "2.0.0-alpha.3",
"blitz": "2.0.0-alpha.19",
"next": "*"
},
"publishConfig": {

View File

@@ -99,7 +99,10 @@ export function useQuery<
(!options || !("suspense" in options) || options.suspense) &&
(!options || !("enabled" in options) || options.enabled)
) {
throw new Promise(() => {})
const e = new Error()
e.name = "Rendering Suspense fallback..."
delete e.stack
throw e
}
const rest = {
@@ -182,7 +185,10 @@ export function usePaginatedQuery<
(!options || !("suspense" in options) || options.suspense) &&
(!options || !("enabled" in options) || options.enabled)
) {
throw new Promise(() => {})
const e = new Error()
e.name = "Rendering Suspense fallback..."
delete e.stack
throw e
}
const rest = {
@@ -278,7 +284,10 @@ export function useInfiniteQuery<
(!options || !("suspense" in options) || options.suspense) &&
(!options || !("enabled" in options) || options.enabled)
) {
throw new Promise(() => {})
const e = new Error()
e.name = "Rendering Suspense fallback..."
delete e.stack
throw e
}
const rest = {

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")
}

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