Compare commits
24 Commits
@blitzjs/a
...
@blitzjs/a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9674efc0bf | ||
|
|
666a3ae3e6 | ||
|
|
c9cf7adc33 | ||
|
|
f4c2234c4d | ||
|
|
82916b21c2 | ||
|
|
bf1b2c8244 | ||
|
|
043c3498d0 | ||
|
|
a2ebdbe7d6 | ||
|
|
de4e8084ef | ||
|
|
bb9eaed520 | ||
|
|
ffe85b5ab6 | ||
|
|
6edeed7c5a | ||
|
|
1d9d890d9c | ||
|
|
b8f51a2354 | ||
|
|
6552b11b94 | ||
|
|
d1dd2bc56e | ||
|
|
09b732860d | ||
|
|
ccb6cfd2a0 | ||
|
|
502b8f7820 | ||
|
|
3888d7018a | ||
|
|
02f7822437 | ||
|
|
00bd849eef | ||
|
|
c8db1a0b7e | ||
|
|
fb32903bf9 |
9
.changeset/great-months-train.md
Normal file
9
.changeset/great-months-train.md
Normal file
@@ -0,0 +1,9 @@
|
||||
---
|
||||
"blitz": patch
|
||||
"@blitzjs/next": patch
|
||||
"@blitzjs/auth": patch
|
||||
"@blitzjs/rpc": patch
|
||||
"@blitzjs/generator": patch
|
||||
---
|
||||
|
||||
fix route manifest codegen
|
||||
5
.changeset/lovely-colts-share.md
Normal file
5
.changeset/lovely-colts-share.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"blitz": patch
|
||||
---
|
||||
|
||||
fix broken cli versioning
|
||||
@@ -12,7 +12,18 @@
|
||||
"@blitzjs/rpc": "2.0.0-alpha.0",
|
||||
"@blitzjs/config": "0.0.0",
|
||||
"@blitzjs/generator": "2.0.0-alpha.0",
|
||||
"template": "0.0.0"
|
||||
"template": "0.0.0",
|
||||
"toolkit-app": "1.0.0"
|
||||
},
|
||||
"changesets": ["nine-onions-admire", "ninety-pets-heal", "poor-peas-lick"]
|
||||
"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"
|
||||
]
|
||||
}
|
||||
|
||||
5
.changeset/silent-colts-reply.md
Normal file
5
.changeset/silent-colts-reply.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"blitz": patch
|
||||
---
|
||||
|
||||
added index.cjs to blitz externals
|
||||
5
.changeset/ten-rivers-burn.md
Normal file
5
.changeset/ten-rivers-burn.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"blitz": patch
|
||||
---
|
||||
|
||||
fix more cli problems
|
||||
5
.changeset/thirty-countries-build.md
Normal file
5
.changeset/thirty-countries-build.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"blitz": patch
|
||||
---
|
||||
|
||||
add @blitzjs/generator as external
|
||||
10
.changeset/twenty-beans-pump.md
Normal file
10
.changeset/twenty-beans-pump.md
Normal 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
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -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
|
||||
|
||||
@@ -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>
|
||||
|
||||
11
apps/toolkit-app/.editorconfig
Normal file
11
apps/toolkit-app/.editorconfig
Normal 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
3
apps/toolkit-app/.env
Normal 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
|
||||
1
apps/toolkit-app/.eslintrc.js
Normal file
1
apps/toolkit-app/.eslintrc.js
Normal file
@@ -0,0 +1 @@
|
||||
module.exports = require("@blitzjs/next/eslint")
|
||||
56
apps/toolkit-app/.gitignore
vendored
Normal file
56
apps/toolkit-app/.gitignore
vendored
Normal 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
|
||||
9
apps/toolkit-app/.prettierignore
Normal file
9
apps/toolkit-app/.prettierignore
Normal file
@@ -0,0 +1,9 @@
|
||||
.gitkeep
|
||||
.env*
|
||||
*.ico
|
||||
*.lock
|
||||
db/migrations
|
||||
.next
|
||||
.yarn
|
||||
.pnp.*
|
||||
node_modules
|
||||
56
apps/toolkit-app/CHANGELOG.md
Normal file
56
apps/toolkit-app/CHANGELOG.md
Normal file
@@ -0,0 +1,56 @@
|
||||
# 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
|
||||
|
||||
- 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
175
apps/toolkit-app/README.md
Normal file
@@ -0,0 +1,175 @@
|
||||
TODO
|
||||
|
||||
[](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 you’ll put any pages or API routes.
|
||||
|
||||
- `db/` is where your database configuration goes. If you’re 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 you’re using a tool like `npm` or `yarn`, you won’t 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)
|
||||
58
apps/toolkit-app/app/auth/components/LoginForm.tsx
Normal file
58
apps/toolkit-app/app/auth/components/LoginForm.tsx
Normal file
@@ -0,0 +1,58 @@
|
||||
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"
|
||||
|
||||
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="/auth/forgot-password" passHref>
|
||||
<a>Forgot your password?</a>
|
||||
</Link>
|
||||
</div>
|
||||
</Form>
|
||||
|
||||
<div style={{ marginTop: "1rem" }}>
|
||||
Or{" "}
|
||||
<Link href="/auth/signup" passHref>
|
||||
<a>Sign Up</a>
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default LoginForm
|
||||
42
apps/toolkit-app/app/auth/components/SignupForm.tsx
Normal file
42
apps/toolkit-app/app/auth/components/SignupForm.tsx
Normal 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
|
||||
25
apps/toolkit-app/app/auth/mutations/changePassword.ts
Normal file
25
apps/toolkit-app/app/auth/mutations/changePassword.ts
Normal 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
|
||||
}
|
||||
)
|
||||
42
apps/toolkit-app/app/auth/mutations/forgotPassword.ts
Normal file
42
apps/toolkit-app/app/auth/mutations/forgotPassword.ts
Normal 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
|
||||
})
|
||||
32
apps/toolkit-app/app/auth/mutations/login.ts
Normal file
32
apps/toolkit-app/app/auth/mutations/login.ts
Normal 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
|
||||
})
|
||||
5
apps/toolkit-app/app/auth/mutations/logout.ts
Normal file
5
apps/toolkit-app/app/auth/mutations/logout.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { Ctx } from "blitz"
|
||||
|
||||
export default async function logout(_: any, ctx: Ctx) {
|
||||
return await ctx.session.$revoke()
|
||||
}
|
||||
48
apps/toolkit-app/app/auth/mutations/resetPassword.ts
Normal file
48
apps/toolkit-app/app/auth/mutations/resetPassword.ts
Normal 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
|
||||
}
|
||||
19
apps/toolkit-app/app/auth/mutations/signup.ts
Normal file
19
apps/toolkit-app/app/auth/mutations/signup.ts
Normal 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 }
|
||||
}
|
||||
42
apps/toolkit-app/app/auth/validations.ts
Normal file
42
apps/toolkit-app/app/auth/validations.ts
Normal 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,
|
||||
})
|
||||
18
apps/toolkit-app/app/blitz-client.ts
Normal file
18
apps/toolkit-app/app/blitz-client.ts
Normal 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,
|
||||
},
|
||||
},
|
||||
}),
|
||||
],
|
||||
})
|
||||
17
apps/toolkit-app/app/blitz-server.ts
Normal file
17
apps/toolkit-app/app/blitz-server.ts
Normal 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 }
|
||||
83
apps/toolkit-app/app/core/components/Form.tsx
Normal file
83
apps/toolkit-app/app/core/components/Form.tsx
Normal 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
|
||||
59
apps/toolkit-app/app/core/components/LabeledTextField.tsx
Normal file
59
apps/toolkit-app/app/core/components/LabeledTextField.tsx
Normal 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
|
||||
7
apps/toolkit-app/app/core/hooks/useCurrentUser.ts
Normal file
7
apps/toolkit-app/app/core/hooks/useCurrentUser.ts
Normal 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
|
||||
}
|
||||
17
apps/toolkit-app/app/core/layouts/Layout.tsx
Normal file
17
apps/toolkit-app/app/core/layouts/Layout.tsx
Normal 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
|
||||
13
apps/toolkit-app/app/users/queries/getCurrentUser.ts
Normal file
13
apps/toolkit-app/app/users/queries/getCurrentUser.ts
Normal 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
|
||||
}
|
||||
8
apps/toolkit-app/db/index.ts
Normal file
8
apps/toolkit-app/db/index.ts
Normal 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 }
|
||||
3
apps/toolkit-app/db/migrations/migration_lock.toml
Normal file
3
apps/toolkit-app/db/migrations/migration_lock.toml
Normal 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"
|
||||
65
apps/toolkit-app/db/schema.prisma
Normal file
65
apps/toolkit-app/db/schema.prisma
Normal 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
|
||||
// }
|
||||
15
apps/toolkit-app/db/seeds.ts
Normal file
15
apps/toolkit-app/db/seeds.ts
Normal 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
|
||||
0
apps/toolkit-app/integrations/.keep
Normal file
0
apps/toolkit-app/integrations/.keep
Normal file
11
apps/toolkit-app/jest.config.js
Normal file
11
apps/toolkit-app/jest.config.js
Normal 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)
|
||||
5
apps/toolkit-app/jsconfig.json
Normal file
5
apps/toolkit-app/jsconfig.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"baseUrl": "."
|
||||
}
|
||||
}
|
||||
0
apps/toolkit-app/mailers/.keep
Normal file
0
apps/toolkit-app/mailers/.keep
Normal file
45
apps/toolkit-app/mailers/forgotPasswordMailer.ts
Normal file
45
apps/toolkit-app/mailers/forgotPasswordMailer.ts
Normal 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)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
10
apps/toolkit-app/next.config.js
Normal file
10
apps/toolkit-app/next.config.js
Normal 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
7
apps/toolkit-app/npmrc
Normal 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/*
|
||||
60
apps/toolkit-app/package.json
Normal file
60
apps/toolkit-app/package.json
Normal file
@@ -0,0 +1,60 @@
|
||||
{
|
||||
"name": "toolkit-app",
|
||||
"version": "1.0.1-alpha.4",
|
||||
"scripts": {
|
||||
"start:dev": "pnpm run prisma:start && next dev",
|
||||
"buildapp": "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.9",
|
||||
"next": "12.1.1",
|
||||
"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
|
||||
}
|
||||
37
apps/toolkit-app/pages/_app.tsx
Normal file
37
apps/toolkit-app/pages/_app.tsx
Normal 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)
|
||||
4
apps/toolkit-app/pages/api/rpc/[[...blitz]].ts
Normal file
4
apps/toolkit-app/pages/api/rpc/[[...blitz]].ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { rpcHandler } from "@blitzjs/rpc"
|
||||
import { api } from "app/blitz-server"
|
||||
|
||||
export default api(rpcHandler({ onError: console.log }))
|
||||
@@ -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
|
||||
@@ -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
|
||||
15
apps/toolkit-app/pages/auth/signup.tsx
Normal file
15
apps/toolkit-app/pages/auth/signup.tsx
Normal file
@@ -0,0 +1,15 @@
|
||||
import { useRouter } from "next/router"
|
||||
import Layout from "app/core/layouts/Layout"
|
||||
import { SignupForm } from "app/auth/components/SignupForm"
|
||||
|
||||
const SignupPage = () => {
|
||||
const router = useRouter()
|
||||
|
||||
return (
|
||||
<Layout title="Sign Up">
|
||||
<SignupForm onSuccess={() => router.push("/")} />
|
||||
</Layout>
|
||||
)
|
||||
}
|
||||
|
||||
export default SignupPage
|
||||
273
apps/toolkit-app/pages/index.tsx
Normal file
273
apps/toolkit-app/pages/index.tsx
Normal file
@@ -0,0 +1,273 @@
|
||||
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"
|
||||
|
||||
/*
|
||||
* 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="/auth/signup" passHref>
|
||||
<a className="button small">
|
||||
<strong>Sign Up</strong>
|
||||
</a>
|
||||
</Link>
|
||||
<Link href="/auth/login" 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
|
||||
BIN
apps/toolkit-app/public/favicon.ico
Executable file
BIN
apps/toolkit-app/public/favicon.ico
Executable file
Binary file not shown.
|
After Width: | Height: | Size: 4.2 KiB |
BIN
apps/toolkit-app/public/logo.png
Normal file
BIN
apps/toolkit-app/public/logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 33 KiB |
4
apps/toolkit-app/test/setup.ts
Normal file
4
apps/toolkit-app/test/setup.ts
Normal 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
|
||||
24
apps/toolkit-app/tsconfig.json
Normal file
24
apps/toolkit-app/tsconfig.json
Normal 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
15
apps/toolkit-app/types.ts
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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 && pnpm build && changeset add && changeset version && git add . && git commit -v",
|
||||
"publish-release": "changeset publish && git push --follow-tags"
|
||||
},
|
||||
"dependencies": {
|
||||
|
||||
@@ -1,5 +1,49 @@
|
||||
# @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
|
||||
|
||||
- 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
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@blitzjs/auth",
|
||||
"version": "2.0.0-alpha.3",
|
||||
"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",
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -1,5 +1,45 @@
|
||||
# @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
|
||||
|
||||
- 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
|
||||
|
||||
@@ -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,
|
||||
|
||||
29
packages/blitz-next/eslint.js
Normal file
29
packages/blitz-next/eslint.js
Normal 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",
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@blitzjs/next",
|
||||
"version": "2.0.0-alpha.3",
|
||||
"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.3",
|
||||
"@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",
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
3
packages/blitz-next/src/scripts/default-index-browser.js
Normal file
3
packages/blitz-next/src/scripts/default-index-browser.js
Normal file
@@ -0,0 +1,3 @@
|
||||
exports.Routes = {
|
||||
ThisFileHasNotYetBeenGeneratedPleaseRunBlitzCodeGen: (query) => ({pathname: "⚡️", query}),
|
||||
}
|
||||
6
packages/blitz-next/src/scripts/default-index.d.ts
vendored
Normal file
6
packages/blitz-next/src/scripts/default-index.d.ts
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
import type {ParsedUrlQueryInput} from "querystring"
|
||||
import type {UrlObject} from "url"
|
||||
|
||||
export const Routes: {
|
||||
ThisFileHasNotYetBeenGeneratedPleaseRunBlitzCodeGen(query?: ParsedUrlQueryInput): UrlObject
|
||||
}
|
||||
3
packages/blitz-next/src/scripts/default-index.js
Normal file
3
packages/blitz-next/src/scripts/default-index.js
Normal file
@@ -0,0 +1,3 @@
|
||||
exports.Routes = {
|
||||
ThisFileHasNotYetBeenGeneratedPleaseRunBlitzCodeGen: (query) => ({pathname: "⚡️", query}),
|
||||
}
|
||||
328
packages/blitz-next/src/scripts/postinstall.js
Normal file
328
packages/blitz-next/src/scripts/postinstall.js
Normal 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()
|
||||
}
|
||||
@@ -1,5 +1,55 @@
|
||||
# @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
|
||||
|
||||
- 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
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@blitzjs/rpc",
|
||||
"version": "2.0.0-alpha.3",
|
||||
"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.3",
|
||||
"@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.3",
|
||||
"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.3",
|
||||
"blitz": "2.0.0-alpha.9",
|
||||
"next": "*"
|
||||
},
|
||||
"publishConfig": {
|
||||
|
||||
@@ -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
|
||||
|
||||
63
packages/blitz-rpc/src/resolver.test.ts
Normal file
63
packages/blitz-rpc/src/resolver.test.ts
Normal 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")
|
||||
}
|
||||
321
packages/blitz-rpc/src/resolver.ts
Normal file
321
packages/blitz-rpc/src/resolver.ts
Normal 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,
|
||||
}
|
||||
@@ -1,5 +1,49 @@
|
||||
# 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
|
||||
|
||||
- new app template
|
||||
- Updated dependencies
|
||||
- @blitzjs/generator@2.0.0-alpha.5
|
||||
|
||||
## 2.0.0-alpha.4
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- fix more cli problems
|
||||
- @blitzjs/generator@2.0.0-alpha.4
|
||||
|
||||
## 2.0.0-alpha.3
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "blitz",
|
||||
"version": "2.0.0-alpha.3",
|
||||
"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",
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
import {CliCommand} from "../index"
|
||||
|
||||
const dev: CliCommand = (argv) => {
|
||||
console.log("dev hit")
|
||||
}
|
||||
|
||||
export {dev}
|
||||
@@ -19,12 +19,7 @@ import prompts from "prompts"
|
||||
import chalk from "chalk"
|
||||
|
||||
const getIsTypeScript = async () =>
|
||||
require("fs").existsSync(
|
||||
require("path").join(
|
||||
await require("next/dist/server/lib/utils").getProjectRoot(process.cwd()),
|
||||
"tsconfig.json",
|
||||
),
|
||||
)
|
||||
require("fs").existsSync(require("path").join(process.cwd(), "tsconfig.json"))
|
||||
|
||||
enum ResourceType {
|
||||
All = "all",
|
||||
@@ -79,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,
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -202,7 +203,7 @@ const determinePkgManagerToInstallDeps = async () => {
|
||||
if (res.pkgManager === "skip") {
|
||||
shouldInstallDeps = false
|
||||
} else {
|
||||
shouldInstallDeps = res.pkgManager
|
||||
shouldInstallDeps = true
|
||||
}
|
||||
} else {
|
||||
const res = await prompts({
|
||||
@@ -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,
|
||||
@@ -261,6 +262,7 @@ const newApp: CliCommand = async (argv) => {
|
||||
{ignoreCache: true},
|
||||
)
|
||||
const result = await runPrisma(["migrate", "dev", "--name", "Initial migration"], true)
|
||||
console.log("primsa result", result)
|
||||
if (!result.success) throw new Error()
|
||||
} catch (error) {
|
||||
postInstallSteps.push(
|
||||
|
||||
56
packages/blitz/src/cli/commands/prisma.ts
Normal file
56
packages/blitz/src/cli/commands/prisma.ts
Normal 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}
|
||||
@@ -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,47 +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._
|
||||
|
||||
// Don't check for react or react-dom when running blitz new
|
||||
if (command !== "new") {
|
||||
;["react", "react-dom"].forEach((dependency) => {
|
||||
try {
|
||||
// When 'npm link' is used it checks the clone location. Not the project.
|
||||
require.resolve(dependency)
|
||||
} catch (err) {
|
||||
console.warn(
|
||||
`The module '${dependency}' was not found. Blitz.js requires that you include it in 'dependencies' of your 'package.json'. To add it, run 'npm install ${dependency}'`,
|
||||
)
|
||||
}
|
||||
})
|
||||
if (args["--env"]) {
|
||||
process.env.APP_ENV = args["--env"]
|
||||
}
|
||||
|
||||
if (args["--help"]) {
|
||||
forwardedArgs.push("--help")
|
||||
}
|
||||
|
||||
if (args["--env"]) {
|
||||
process.env.APP_ENV = args["--env"]
|
||||
}
|
||||
|
||||
const defaultEnv = command === "dev" ? "development" : "production"
|
||||
|
||||
const standardEnv = ["production", "development", "test"]
|
||||
@@ -105,33 +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>
|
||||
|
||||
if (command === "dev") {
|
||||
const {watchFile} = require("fs")
|
||||
watchFile(`${process.cwd()}/blitz.config.js`, (cur: any, prev: any) => {
|
||||
if (cur.size > 0 || prev.size > 0) {
|
||||
console.log(
|
||||
`\n> Found a change in blitz.config.js. Restart the server to see the changes in effect.`,
|
||||
)
|
||||
}
|
||||
})
|
||||
watchFile(`${process.cwd()}/blitz.config.ts`, (cur: any, prev: any) => {
|
||||
if (cur.size > 0 || prev.size > 0) {
|
||||
console.log(
|
||||
`\n> Found a change in blitz.config.ts. Restart the server to see the changes in effect.`,
|
||||
)
|
||||
}
|
||||
})
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -2,12 +2,12 @@ import fs, {promises} from "fs"
|
||||
import {join, resolve} from "path"
|
||||
import {readJSON} from "fs-extra"
|
||||
import path from "path"
|
||||
import {packageDirectory} from "pkg-dir"
|
||||
import pkgDir from "pkg-dir"
|
||||
import resolveCwd from "resolve-cwd"
|
||||
const debug = require("debug")("blitz:utils")
|
||||
|
||||
export async function resolveBinAsync(pkg: string, executable = pkg) {
|
||||
const packageDir = await packageDirectory({cwd: resolveCwd(pkg)})
|
||||
const packageDir = await pkgDir(resolveCwd(pkg))
|
||||
if (!packageDir) throw new Error(`Could not find package.json for '${pkg}'`)
|
||||
|
||||
const {bin} = await readJSON(path.join(packageDir, "package.json"))
|
||||
@@ -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> {
|
||||
|
||||
@@ -4,7 +4,7 @@ import detect from "detect-port"
|
||||
import path from "path"
|
||||
import {existsSync, readJSONSync} from "fs-extra"
|
||||
import * as esbuild from "esbuild"
|
||||
import {packageDirectorySync} from "pkg-dir"
|
||||
import pkgDir from "pkg-dir"
|
||||
import type {ServerConfig} from "./config"
|
||||
|
||||
const debug = require("debug")("blitz:utils")
|
||||
@@ -50,7 +50,7 @@ export function getCustomServerBuildPath() {
|
||||
}
|
||||
|
||||
const getEsbuildOptions = (): esbuild.BuildOptions => {
|
||||
const pkg = readJSONSync(path.join(packageDirectorySync()!, "package.json"))
|
||||
const pkg = readJSONSync(path.join(pkgDir.sync()!, "package.json"))
|
||||
return {
|
||||
entryPoints: [getCustomServerPath()],
|
||||
outfile: getCustomServerBuildPath(),
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -1,5 +1,25 @@
|
||||
# @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
|
||||
|
||||
- new app template
|
||||
|
||||
## 2.0.0-alpha.4
|
||||
|
||||
## 2.0.0-alpha.3
|
||||
|
||||
## 2.0.0-alpha.2
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@blitzjs/config",
|
||||
"private": true,
|
||||
"version": "2.0.0-alpha.3",
|
||||
"version": "2.0.0-alpha.9",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@typescript-eslint/eslint-plugin": "5.9.1",
|
||||
|
||||
@@ -1,5 +1,25 @@
|
||||
# @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
|
||||
|
||||
- new app template
|
||||
|
||||
## 2.0.0-alpha.4
|
||||
|
||||
## 2.0.0-alpha.3
|
||||
|
||||
## 2.0.0-alpha.2
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@blitzjs/generator",
|
||||
"version": "2.0.0-alpha.3",
|
||||
"version": "2.0.0-alpha.9",
|
||||
"scripts": {
|
||||
"dev": "watch unbuild src --wait=0.2",
|
||||
"build": "unbuild && pnpm build:templates",
|
||||
@@ -26,6 +26,7 @@
|
||||
"@babel/types": "7.12.10",
|
||||
"@mrleebo/prisma-ast": "0.2.6",
|
||||
"chalk": "^4.1.0",
|
||||
"console-table-printer": "2.10.0",
|
||||
"cross-spawn": "7.0.3",
|
||||
"diff": "5.0.0",
|
||||
"enquirer": "2.3.6",
|
||||
@@ -35,14 +36,16 @@
|
||||
"mem-fs": "1.2.0",
|
||||
"mem-fs-editor": "8.0.0",
|
||||
"npm-which": "3.0.1",
|
||||
"ora": "5.3.0",
|
||||
"pluralize": "8.0.0",
|
||||
"prettier": "^2.5.1",
|
||||
"recast": "0.20.5",
|
||||
"tslog": "3.3.1",
|
||||
"username": "5.1.0",
|
||||
"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",
|
||||
@@ -59,6 +62,7 @@
|
||||
"@typescript-eslint/parser": "5.9.1",
|
||||
"babylon": "6.18.0",
|
||||
"debug": "4.3.3",
|
||||
"eslint": "7.32.0",
|
||||
"react": "18.0.0",
|
||||
"typescript": "^4.5.3",
|
||||
"unbuild": "0.6.9",
|
||||
|
||||
@@ -13,6 +13,7 @@ import * as babelParser from "recast/parsers/babel"
|
||||
import {ConflictChecker} from "./conflict-checker"
|
||||
import {pipe} from "./utils/pipe"
|
||||
import {readdirRecursive} from "./utils/readdir-recursive"
|
||||
import prettier from "prettier"
|
||||
const debug = require("debug")("blitz:generator")
|
||||
|
||||
export const customTsParser = {
|
||||
@@ -39,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[] {
|
||||
@@ -152,6 +153,7 @@ export abstract class Generator<
|
||||
this.store = createStore()
|
||||
this.fs = createEditor(this.store)
|
||||
this.enquirer = new Enquirer()
|
||||
this.prettier = prettier
|
||||
this.useTs =
|
||||
typeof this.options.useTs === "undefined"
|
||||
? fs.existsSync(path.resolve("tsconfig.json"))
|
||||
@@ -207,6 +209,7 @@ export abstract class Generator<
|
||||
}
|
||||
const inputStr = input.toString("utf-8")
|
||||
let templatedFile = inputStr
|
||||
|
||||
if (codeFileExtensions.test(pathEnding)) {
|
||||
templatedFile = this.replaceConditionals(inputStr, templateValues, prettierOptions || {})
|
||||
}
|
||||
@@ -247,9 +250,7 @@ export abstract class Generator<
|
||||
const additionalFilesToIgnore = this.filesToIgnore()
|
||||
return ![...alwaysIgnoreFiles, ...additionalFilesToIgnore].includes(name)
|
||||
})
|
||||
try {
|
||||
this.prettier = await import("prettier")
|
||||
} catch {}
|
||||
|
||||
const prettierOptions = await this.prettier?.resolveConfig(sourcePath)
|
||||
|
||||
for (let filePath of paths) {
|
||||
@@ -258,11 +259,16 @@ export abstract class Generator<
|
||||
pathSuffix = path.join(this.getTargetDirectory(), pathSuffix)
|
||||
const templateValues = await this.getTemplateValues()
|
||||
|
||||
this.fs.copy(this.sourcePath(filePath), this.destinationPath(pathSuffix), {
|
||||
process: (input) =>
|
||||
this.process(input, pathSuffix, templateValues, prettierOptions ?? undefined),
|
||||
})
|
||||
let templatedPathSuffix = this.replaceTemplateValues(pathSuffix, templateValues)
|
||||
|
||||
const newContent = this.process(
|
||||
this.fs.read(this.sourcePath(filePath), {raw: true}) as any,
|
||||
pathSuffix,
|
||||
templateValues,
|
||||
prettierOptions ?? undefined,
|
||||
)
|
||||
this.fs.write(this.destinationPath(pathSuffix), newContent)
|
||||
|
||||
if (!this.useTs && tsExtension.test(this.destinationPath(pathSuffix))) {
|
||||
templatedPathSuffix = templatedPathSuffix.replace(tsExtension, ".js")
|
||||
}
|
||||
@@ -295,6 +301,7 @@ export abstract class Generator<
|
||||
} else {
|
||||
return path.join(
|
||||
__dirname,
|
||||
"..",
|
||||
process.env.NODE_ENV === "test" ? "../templates" : "./templates",
|
||||
this.sourceRoot.path,
|
||||
...paths,
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import spawn from "cross-spawn"
|
||||
import chalk from "chalk"
|
||||
import {readJSONSync, writeJson} from "fs-extra"
|
||||
import {join} from "path"
|
||||
import username from "username"
|
||||
import {Generator, GeneratorOptions, SourceRootType} from "../generator"
|
||||
import {fetchLatestVersionsFor} from "../utils/fetch-latest-version-for"
|
||||
import {getBlitzDependencyVersion} from "../utils/get-blitz-dependency-version"
|
||||
import {baseLogger, log} from "../utils/log"
|
||||
|
||||
function assert(condition: any, message: string): asserts condition {
|
||||
if (!condition) throw new Error(message)
|
||||
@@ -74,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()
|
||||
@@ -84,22 +90,23 @@ export class AppGenerator extends Generator<AppGeneratorOptions> {
|
||||
const {pkgManager} = this
|
||||
let gitInitSuccessful
|
||||
if (!this.options.skipGit) {
|
||||
console.log({spawn})
|
||||
const initResult = spawn.sync("git", ["init"], {
|
||||
stdio: "ignore",
|
||||
})
|
||||
|
||||
gitInitSuccessful = initResult.status === 0
|
||||
if (!gitInitSuccessful) {
|
||||
console.warn("Failed to run git init.")
|
||||
console.warn("Find out more about how to install git here: https://git-scm.com/downloads.")
|
||||
baseLogger({displayDateTime: false}).warn("Failed to run git init.")
|
||||
baseLogger({displayDateTime: false}).warn(
|
||||
"Find out more about how to install git here: https://git-scm.com/downloads.",
|
||||
)
|
||||
}
|
||||
}
|
||||
const pkgJsonLocation = join(this.destinationPath(), "package.json")
|
||||
const pkg = readJSONSync(pkgJsonLocation)
|
||||
|
||||
console.log("") // New line needed
|
||||
// todo const spinner = log.spinner(log.withBrand("Retrieving the freshest of dependencies")).start()
|
||||
const spinner = log.spinner(log.withBrand("Retrieving the freshest of dependencies")).start()
|
||||
|
||||
const [
|
||||
{value: newDependencies, isFallback: dependenciesUsedFallback},
|
||||
@@ -121,7 +128,7 @@ export class AppGenerator extends Generator<AppGeneratorOptions> {
|
||||
await writeJson(pkgJsonLocation, pkg, {spaces: 2})
|
||||
|
||||
if (!fallbackUsed && !this.options.skipInstall) {
|
||||
// spinner.succeed()
|
||||
spinner.succeed()
|
||||
|
||||
await new Promise<void>((resolve) => {
|
||||
const logFlag = pkgManager === "yarn" ? "--json" : "--loglevel=error"
|
||||
@@ -140,10 +147,10 @@ export class AppGenerator extends Generator<AppGeneratorOptions> {
|
||||
const spinners: any[] = []
|
||||
|
||||
if (pkgManager !== "yarn") {
|
||||
// const spinner = log
|
||||
// .spinner(log.withBrand("Installing those dependencies (this will take a few minutes)"))
|
||||
// .start()
|
||||
// spinners.push(spinner)
|
||||
const spinner = log
|
||||
.spinner(log.withBrand("Installing those dependencies (this will take a few minutes)"))
|
||||
.start()
|
||||
spinners.push(spinner)
|
||||
}
|
||||
|
||||
cp.stdout?.setEncoding("utf8")
|
||||
@@ -153,8 +160,8 @@ export class AppGenerator extends Generator<AppGeneratorOptions> {
|
||||
let json = getJSON(data)
|
||||
if (json && json.type === "step") {
|
||||
spinners[spinners.length - 1]?.succeed()
|
||||
// const spinner = log.spinner(log.withBrand(json.data.message)).start()
|
||||
// spinners.push(spinner)
|
||||
const spinner = log.spinner(log.withBrand(json.data.message)).start()
|
||||
spinners.push(spinner)
|
||||
}
|
||||
if (json && json.type === "success") {
|
||||
spinners[spinners.length - 1]?.succeed()
|
||||
@@ -195,7 +202,7 @@ export class AppGenerator extends Generator<AppGeneratorOptions> {
|
||||
if (pkgManager === "yarn") {
|
||||
return spawn.sync("yarn", ["run", ...command.split(" ")])
|
||||
} else if (pkgManager === "pnpm") {
|
||||
return spawn.sync("pnpx", command.split(" "))
|
||||
return spawn.sync("pnpm", command.split(" "))
|
||||
} else {
|
||||
return spawn.sync("npx", command.split(" "))
|
||||
}
|
||||
@@ -203,31 +210,39 @@ export class AppGenerator extends Generator<AppGeneratorOptions> {
|
||||
|
||||
// Ensure the generated files are formatted with the installed prettier version
|
||||
if (this.packageInstallSuccess) {
|
||||
// const formattingSpinner = log.spinner(log.withBrand("Formatting your code")).start()
|
||||
const formattingSpinner = log.spinner(log.withBrand("Formatting your code")).start()
|
||||
const prettierResult = runLocalNodeCLI("prettier --loglevel silent --write .")
|
||||
if (prettierResult.status !== 0) {
|
||||
// formattingSpinner.fail(
|
||||
// chalk.yellow.bold(
|
||||
// "We had an error running Prettier, but don't worry your app will still run fine :)",
|
||||
// ),
|
||||
// )
|
||||
formattingSpinner.fail(
|
||||
chalk.yellow.bold(
|
||||
"We had an error running Prettier, but don't worry your app will still run fine :)",
|
||||
),
|
||||
)
|
||||
} else {
|
||||
// formattingSpinner.succeed()
|
||||
formattingSpinner.succeed()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.log("") // New line needed
|
||||
if (this.options.skipInstall) {
|
||||
// spinner.succeed()
|
||||
spinner.succeed()
|
||||
} else {
|
||||
// spinner.fail(
|
||||
// chalk.red.bold(
|
||||
// `We had some trouble connecting to the network, so we'll skip installing your dependencies right now. Make sure to run ${`${this.pkgManager} install`} once you're connected again.`,
|
||||
// ),
|
||||
// )
|
||||
spinner.fail(
|
||||
chalk.red.bold(
|
||||
`We had some trouble connecting to the network, so we'll skip installing your dependencies right now. Make sure to run ${`${this.pkgManager} install`} once you're connected again.`,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// 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()
|
||||
}
|
||||
@@ -242,7 +257,7 @@ export class AppGenerator extends Generator<AppGeneratorOptions> {
|
||||
}
|
||||
|
||||
commitChanges() {
|
||||
// const commitSpinner = log.spinner(log.withBrand("Committing your app")).start()
|
||||
const commitSpinner = log.spinner(log.withBrand("Committing your app")).start()
|
||||
const commands: Array<[string, string[], object]> = [
|
||||
["git", ["add", "."], {stdio: "ignore"}],
|
||||
[
|
||||
@@ -264,15 +279,15 @@ export class AppGenerator extends Generator<AppGeneratorOptions> {
|
||||
for (let command of commands) {
|
||||
const result = spawn.sync(...command)
|
||||
if (result.status !== 0) {
|
||||
// commitSpinner.fail(
|
||||
// chalk.red.bold(
|
||||
// `Failed to run command ${command[0]} with ${command[1].join(" ")} options.`,
|
||||
// ),
|
||||
// )
|
||||
commitSpinner.fail(
|
||||
chalk.red.bold(
|
||||
`Failed to run command ${command[0]} with ${command[1].join(" ")} options.`,
|
||||
),
|
||||
)
|
||||
return
|
||||
}
|
||||
}
|
||||
// commitSpinner.succeed()
|
||||
commitSpinner.succeed()
|
||||
}
|
||||
private updateForms() {
|
||||
const pkg = this.fs.readJSON(this.destinationPath("package.json")) as
|
||||
|
||||
@@ -19,7 +19,7 @@ export class PageGenerator extends Generator<PageGeneratorOptions> {
|
||||
super(options)
|
||||
this.sourceRoot = getTemplateRoot(options.templateDir, {type: "template", path: "page"})
|
||||
}
|
||||
static subdirectory = "pages"
|
||||
static subdirectory = "../../.."
|
||||
|
||||
private getId(input: string = "") {
|
||||
if (!input) return input
|
||||
@@ -63,7 +63,7 @@ export class PageGenerator extends Generator<PageGeneratorOptions> {
|
||||
const parent = this.options.parentModels
|
||||
? `${this.options.parentModels}/__parentModelParam__/`
|
||||
: ""
|
||||
return `app/pages/${parent}${kebabCaseModelName}`
|
||||
return `pages/${parent}${kebabCaseModelName}`
|
||||
}
|
||||
|
||||
async postWrite() {
|
||||
|
||||
101
packages/generator/src/utils/default-config.ts
Normal file
101
packages/generator/src/utils/default-config.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import os from "os"
|
||||
|
||||
export const VALID_LOADERS = ["default", "imgix", "cloudinary", "akamai", "custom"] as const
|
||||
|
||||
export type LoaderValue = typeof VALID_LOADERS[number]
|
||||
|
||||
export type ImageConfig = {
|
||||
deviceSizes: number[]
|
||||
imageSizes: number[]
|
||||
loader: LoaderValue
|
||||
path: string
|
||||
domains?: string[]
|
||||
disableStaticImages?: boolean
|
||||
minimumCacheTTL?: number
|
||||
}
|
||||
|
||||
export const imageConfigDefault: ImageConfig = {
|
||||
deviceSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 3840],
|
||||
imageSizes: [16, 32, 48, 64, 96, 128, 256, 384],
|
||||
path: "/_next/image",
|
||||
loader: "default",
|
||||
domains: [],
|
||||
disableStaticImages: false,
|
||||
minimumCacheTTL: 60,
|
||||
}
|
||||
|
||||
export const defaultConfig: {[key: string]: any} = {
|
||||
env: {},
|
||||
webpack: null,
|
||||
webpackDevMiddleware: null,
|
||||
distDir: ".next",
|
||||
cleanDistDir: true,
|
||||
assetPrefix: "",
|
||||
configOrigin: "default",
|
||||
useFileSystemPublicRoutes: true,
|
||||
generateBuildId: () => null,
|
||||
generateEtags: true,
|
||||
pageExtensions: ["tsx", "ts", "jsx", "js"],
|
||||
target: "server",
|
||||
poweredByHeader: true,
|
||||
compress: true,
|
||||
analyticsId: process.env.VERCEL_ANALYTICS_ID || "",
|
||||
images: imageConfigDefault,
|
||||
devIndicators: {
|
||||
buildActivity: true,
|
||||
},
|
||||
onDemandEntries: {
|
||||
maxInactiveAge: 60 * 1000,
|
||||
pagesBufferLength: 2,
|
||||
},
|
||||
amp: {
|
||||
canonicalBase: "",
|
||||
},
|
||||
basePath: "",
|
||||
sassOptions: {},
|
||||
trailingSlash: false,
|
||||
i18n: null,
|
||||
productionBrowserSourceMaps: false,
|
||||
optimizeFonts: true,
|
||||
log: {
|
||||
level: "info",
|
||||
},
|
||||
webpack5: Number(process.env.NEXT_PRIVATE_TEST_WEBPACK4_MODE) > 0 ? false : undefined,
|
||||
excludeDefaultMomentLocales: true,
|
||||
serverRuntimeConfig: {},
|
||||
publicRuntimeConfig: {},
|
||||
reactStrictMode: false,
|
||||
httpAgentOptions: {
|
||||
keepAlive: true,
|
||||
},
|
||||
experimental: {
|
||||
swcLoader: false,
|
||||
swcMinify: false,
|
||||
cpus: Math.max(
|
||||
1,
|
||||
(Number(process.env.CIRCLE_NODE_TOTAL) || (os.cpus() || {length: 1}).length) - 1,
|
||||
),
|
||||
plugins: false,
|
||||
profiling: false,
|
||||
isrFlushToDisk: true,
|
||||
workerThreads: false,
|
||||
pageEnv: false,
|
||||
optimizeImages: false,
|
||||
optimizeCss: false,
|
||||
scrollRestoration: false,
|
||||
stats: false,
|
||||
externalDir: false,
|
||||
disableOptimizedLoading: false,
|
||||
gzipSize: true,
|
||||
craCompat: false,
|
||||
esmExternals: false,
|
||||
staticPageGenerationTimeout: 60,
|
||||
pageDataCollectionTimeout: 60,
|
||||
// default to 50MB limit
|
||||
isrMemoryCacheSize: 50 * 1024 * 1024,
|
||||
concurrentFeatures: false,
|
||||
},
|
||||
future: {
|
||||
strictPostcssConfiguration: false,
|
||||
},
|
||||
}
|
||||
254
packages/generator/src/utils/log.ts
Normal file
254
packages/generator/src/utils/log.ts
Normal file
@@ -0,0 +1,254 @@
|
||||
import {ISettingsParam, Logger} from "tslog"
|
||||
import c from "chalk"
|
||||
import {Table} from "console-table-printer"
|
||||
import ora from "ora"
|
||||
import readline from "readline"
|
||||
import {join} from "path"
|
||||
import {defaultConfig} from "./default-config"
|
||||
|
||||
// eslint-disable-next-line
|
||||
declare module globalThis {
|
||||
let _blitz_baseLogger: Logger
|
||||
let _blitz_logLevel: LogLevel
|
||||
}
|
||||
|
||||
export type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal"
|
||||
|
||||
export function normalizeConfig(phase: string, config: any) {
|
||||
if (typeof config === "function") {
|
||||
config = config(phase, {defaultConfig})
|
||||
|
||||
if (typeof config.then === "function") {
|
||||
throw new Error(
|
||||
"> Promise returned in blitz config. https://nextjs.org/docs/messages/promise-in-next-config",
|
||||
)
|
||||
}
|
||||
}
|
||||
return config
|
||||
}
|
||||
|
||||
export function loadConfigAtRuntime() {
|
||||
if (!process.env.BLITZ_APP_DIR) {
|
||||
throw new Error("Internal Blitz Error: process.env.BLITZ_APP_DIR is not set")
|
||||
}
|
||||
return loadConfigProduction(process.env.BLITZ_APP_DIR)
|
||||
}
|
||||
|
||||
export function assignDefaultsBase(userConfig: {[key: string]: any}) {
|
||||
const config = Object.keys(userConfig).reduce<{[key: string]: any}>((currentConfig, key) => {
|
||||
const value = userConfig[key]
|
||||
|
||||
if (value === undefined || value === null) {
|
||||
return currentConfig
|
||||
}
|
||||
|
||||
// Copied from assignDefaults in server/config.ts
|
||||
if (!!value && value.constructor === Object) {
|
||||
currentConfig[key] = {
|
||||
...defaultConfig[key],
|
||||
...Object.keys(value).reduce<any>((c, k) => {
|
||||
const v = value[k]
|
||||
if (v !== undefined && v !== null) {
|
||||
c[k] = v
|
||||
}
|
||||
return c
|
||||
}, {}),
|
||||
}
|
||||
} else {
|
||||
currentConfig[key] = value
|
||||
}
|
||||
|
||||
return currentConfig
|
||||
}, {})
|
||||
const result = {...defaultConfig, ...config}
|
||||
return result
|
||||
}
|
||||
|
||||
export function loadConfigProduction(pagesDir: string) {
|
||||
let userConfigModule
|
||||
try {
|
||||
const path = join(pagesDir, "next.confi.js")
|
||||
debug("Loading config from ", path)
|
||||
// eslint-disable-next-line no-eval -- block webpack from following this module path
|
||||
userConfigModule = eval("require")(path)
|
||||
} catch {
|
||||
debug("Did not find custom config file")
|
||||
// In case user does not have custom config
|
||||
userConfigModule = {}
|
||||
}
|
||||
let userConfig = normalizeConfig(
|
||||
"phase-production-server",
|
||||
userConfigModule.default || userConfigModule,
|
||||
)
|
||||
return assignDefaultsBase(userConfig) as any
|
||||
}
|
||||
|
||||
export const newline = () => {
|
||||
globalThis._blitz_logLevel = globalThis._blitz_logLevel ?? loadConfigAtRuntime().log?.level
|
||||
|
||||
const logLevel = globalThis._blitz_logLevel
|
||||
|
||||
switch (logLevel) {
|
||||
case "trace":
|
||||
case "debug":
|
||||
case "info":
|
||||
console.log(" ")
|
||||
break
|
||||
case "warn":
|
||||
case "error":
|
||||
case "fatal":
|
||||
default:
|
||||
//nothing
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
export const baseLogger = (options?: ISettingsParam): Logger => {
|
||||
if (globalThis._blitz_baseLogger) return globalThis._blitz_baseLogger
|
||||
|
||||
let config
|
||||
try {
|
||||
config = loadConfigAtRuntime()
|
||||
} catch {
|
||||
config = {}
|
||||
}
|
||||
|
||||
globalThis._blitz_baseLogger = new Logger({
|
||||
minLevel: config.log?.level || "info",
|
||||
type: config.log?.type || "pretty",
|
||||
dateTimePattern:
|
||||
process.env.NODE_ENV === "production"
|
||||
? "year-month-day hour:minute:second.millisecond"
|
||||
: "hour:minute:second.millisecond",
|
||||
displayFunctionName: false,
|
||||
displayFilePath: "hidden",
|
||||
displayRequestId: false,
|
||||
dateTimeTimezone:
|
||||
process.env.NODE_ENV === "production"
|
||||
? "utc"
|
||||
: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
||||
prettyInspectHighlightStyles: {
|
||||
name: "yellow",
|
||||
number: "blue",
|
||||
bigint: "blue",
|
||||
boolean: "blue",
|
||||
},
|
||||
colorizePrettyLogs: process.env.FORCE_COLOR === "0" ? false : true,
|
||||
maskValuesOfKeys: ["password", "passwordConfirmation"],
|
||||
exposeErrorCodeFrame: process.env.NODE_ENV !== "production",
|
||||
...options,
|
||||
})
|
||||
|
||||
return globalThis._blitz_baseLogger
|
||||
}
|
||||
|
||||
export const table = Table
|
||||
export const chalk = c
|
||||
|
||||
// const blitzTrueBrandColor = '6700AB'
|
||||
const blitzBrightBrandColor = "8a3df0"
|
||||
|
||||
// Using bright brand color so it's better for dark terminals
|
||||
const brandColor = blitzBrightBrandColor
|
||||
|
||||
const withBrand = (str: string) => {
|
||||
return c.hex(brandColor).bold(str)
|
||||
}
|
||||
|
||||
const withCaret = (str: string) => {
|
||||
return `${c.gray(">")} ${str}`
|
||||
}
|
||||
|
||||
const withCheck = (str: string) => {
|
||||
return `${c.green("✔")} ${str}`
|
||||
}
|
||||
|
||||
const withProgress = (str: string) => {
|
||||
return withCaret(str)
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs a branded purple message to stdout.
|
||||
*
|
||||
* @param {string} msg
|
||||
*/
|
||||
const branded = (msg: string) => {
|
||||
console.log(c.hex(brandColor).bold(msg))
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the line and optionally log a message to stdout.
|
||||
*
|
||||
* @param {string} msg
|
||||
*/
|
||||
const clearLine = (msg?: string) => {
|
||||
readline.clearLine(process.stdout, 0)
|
||||
readline.cursorTo(process.stdout, 0)
|
||||
msg && process.stdout.write(msg)
|
||||
}
|
||||
|
||||
const clearConsole = () => {
|
||||
if (process.platform === "win32") {
|
||||
process.stdout.write("\x1B[2J\x1B[0f")
|
||||
} else {
|
||||
process.stdout.write("\x1B[2J\x1B[3J\x1B[H")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs a progress message to stdout.
|
||||
*
|
||||
* @param {string} msg
|
||||
*/
|
||||
const progress = (msg: string) => {
|
||||
console.log(withProgress(msg))
|
||||
}
|
||||
|
||||
const spinner = (str: string) => {
|
||||
return ora({
|
||||
text: str,
|
||||
color: "blue",
|
||||
spinner: {
|
||||
interval: 120,
|
||||
frames: ["◢", "◣", "◤", "◥"],
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs a green success message to stdout.
|
||||
*
|
||||
* @param {string} msg
|
||||
*/
|
||||
const success = (msg: string) => {
|
||||
console.log(withCheck(c.green(msg)))
|
||||
}
|
||||
|
||||
/**
|
||||
* Colorizes a variable for display.
|
||||
*
|
||||
* @param {string} val
|
||||
*/
|
||||
const variable = (val: any) => {
|
||||
return c.cyan.bold(`${val}`)
|
||||
}
|
||||
|
||||
/**
|
||||
* If the DEBUG env var is set this will write to the console
|
||||
* @param str msg
|
||||
*/
|
||||
const debug = require("debug")("blitz")
|
||||
|
||||
export const log = {
|
||||
withBrand,
|
||||
withCaret,
|
||||
branded,
|
||||
clearLine,
|
||||
clearConsole,
|
||||
progress,
|
||||
spinner,
|
||||
success,
|
||||
variable,
|
||||
debug,
|
||||
Table,
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user