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

Compare commits

...

72 Commits

Author SHA1 Message Date
Dillon Raphael
009664e0f5 Merge branch 'main' of github.com:blitz-js/blitz 2022-06-30 11:33:42 +02:00
Dillon Raphael
3110522dca release drafter template 2022-06-30 11:33:32 +02:00
github-actions[bot]
7269ac2bc5 Version Packages (alpha) (#3472)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-30 11:09:25 +02:00
Datner
7a7ce2229e Add middleware integration tests (#3411)
* Set up middleware integration test application

* Add middleware integration test

* Update pnpm lock

Co-authored-by: Aleksandra <alexsandra.sikora@gmail.com>
2022-06-30 11:05:30 +02:00
Dillon Raphael
10c23d3d20 Merge branch 'main' of github.com:blitz-js/blitz 2022-06-29 13:03:54 +02:00
Dillon Raphael
d93b05d0ac Add github release template 2022-06-29 13:03:41 +02:00
Dillon Raphael
aafdc5b4c4 Codemod: Move middlewares from blitz config to blitz server (#3471)
Co-authored-by: Aleksandra <alexsandra.sikora@gmail.com>
2022-06-29 12:52:56 +02:00
Dillon Raphael
37ef42811b Merge branch 'main' of github.com:blitz-js/blitz 2022-06-29 12:24:44 +02:00
Dillon Raphael
34cc29e40c Turn off auto github releases 2022-06-29 12:24:26 +02:00
Fran Zekan
bee19a259d Hook up onError function for rpc server and next (#3460)
Co-authored-by: beerose <alexsandra.sikora@gmail.com>
2022-06-29 12:18:22 +02:00
Fran Zekan
9ada0f6661 Allow customizing PreviewData in gSSP (#3461) 2022-06-29 11:42:41 +02:00
Dillon Raphael
425559d360 pnpm install without frozen lockfile for github release action 2022-06-29 10:40:12 +02:00
Dillon Raphael
0411689b1d pnpmlock 2022-06-26 06:28:07 -04:00
github-actions[bot]
0d3514cd27 Version Packages (alpha) (#3464)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-26 06:18:37 -04:00
Dillon Raphael
a9edde6d83 pnpmlock 2022-06-26 06:05:38 -04:00
Dillon Raphael
5c5decbce2 Remove suspense wrapper from withBlitz (#3463)
* remove suspense wrapper

* changeset
2022-06-26 06:02:45 -04:00
beerose
e476beba39 Update pnpm-lock.yaml 2022-06-23 17:00:27 +02:00
github-actions[bot]
1a1b23a7e8 Version Packages (alpha) (#3459)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-23 16:59:54 +02:00
Fran Zekan
ae0b714f69 Expanding types for GetServerSideProps (#3457)
Co-authored-by: beerose <alexsandra.sikora@gmail.com>
2022-06-23 16:56:41 +02:00
Aleksandra
7817fe3a85 Add missing RouteUrlObject on Page.authenticate.redirectTo (#3458) 2022-06-23 16:45:27 +02:00
beerose
30bb474abb Update pnpm-lock.yaml 2022-06-23 13:32:30 +02:00
github-actions[bot]
135b30efde Version Packages (alpha) (#3456)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-23 13:31:59 +02:00
Aleksandra
527e48ac3e Fix running bin commands with Blitz CLI (#3455) 2022-06-23 13:29:22 +02:00
beerose
b905270875 Update pnpm-lock.yaml 2022-06-23 12:56:21 +02:00
github-actions[bot]
96ea5291e4 Version Packages (alpha) (#3454)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-23 12:54:23 +02:00
Aleksandra
9c2e7d372c Import useRouter form next/router in useParams.tsx (#3453) 2022-06-23 12:47:01 +02:00
beerose
493d505b24 Update pnpm-lock 2022-06-22 12:12:57 +02:00
github-actions[bot]
09da992bef Version Packages (alpha) (#3450)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-22 12:11:54 +02:00
Dillon Raphael
bbac7906e8 fix codemod for wrapping \_app arrow function and nested pages directory (#3443)
Co-authored-by: Aleksandra <alexsandra.sikora@gmail.com>
2022-06-22 10:55:35 +02:00
Dillon Raphael
21ca3a9b02 pnpmlock 2022-06-21 10:27:09 -04:00
github-actions[bot]
32274803d9 Version Packages (alpha) (#3449)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-21 10:25:53 -04:00
Dillon Raphael
9ded8dacba Export useParam & useParams from @blitzjs/next (#3448) 2022-06-21 14:45:29 +02:00
beerose
80ffbeaa4c Update pnpm-lock.yaml 2022-06-19 18:20:49 +02:00
github-actions[bot]
6bde1b07da Version Packages (alpha) (#3445)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-19 18:18:57 +02:00
Datner
b918055bf3 Add aliases for Blitz CLI commands (#3410)
Co-authored-by: Aleksandra <alexsandra.sikora@gmail.com>
2022-06-19 18:13:43 +02:00
Aleksandra
f9a2971f05 Improve typings in blitz cli package (#3444) 2022-06-19 15:48:27 +02:00
Dillon Raphael
72b08f2269 pnpmlock 2022-06-13 15:45:47 -04:00
github-actions[bot]
2124a4d0c5 Version Packages (alpha) (#3440)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-13 15:41:12 -04:00
Dillon Raphael
8aee25c58a changeset 2022-06-13 15:37:27 -04:00
Dillon Raphael
f1003faf94 update generator template (#3439) 2022-06-13 15:35:38 -04:00
Andreas Asprou
50468a3bb0 Fix duplicate react-query clients (#3431)
* Fix duplicate react-query clients

* fix tests

* add codemod & remove queryClient export from generator templates

Co-authored-by: Dillon Raphael <dillon@creatorsneverdie.com>
2022-06-13 11:07:41 -04:00
Dillon Raphael
891d91bf4d pnpmlock 2022-06-10 16:19:50 -04:00
github-actions[bot]
f96c953457 Version Packages (alpha) (#3430)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-10 16:18:48 -04:00
Dillon Raphael
a80d2a8f77 Rename Blitz server plugin type from Middleware to RequestMiddleware (#3428)
* rename to requestMiddleware

* update plugins

* merge main

* changeset
2022-06-10 16:14:21 -04:00
beerose
b336ad05f4 Update pnpm-lock.yaml 2022-06-10 11:32:17 -07:00
github-actions[bot]
39ca0ef8bf Version Packages (alpha) (#3429)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-10 11:31:27 -07:00
Aleksandra
4cad9cca25 Add queryClient to RPC plugin exports (#3424) 2022-06-10 10:18:50 -07:00
Dillon Raphael
b6fc940bf2 pnpmlock 2022-06-10 11:21:33 -04:00
github-actions[bot]
a946dd5889 Version Packages (alpha) (#3427)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-10 11:20:25 -04:00
Dillon Raphael
e3750b049d Codemod fixes (#3420)
* change BlitzApiRequest to NextApiRequest & update import map & fail catch for custom server

* fix page consolidation when there are sub directories

* Update prisma db export/import

* Update db imports in templates

* update root types file

* update error messages + merge main + changeset

Co-authored-by: beerose <alexsandra.sikora@gmail.com>
2022-06-10 11:16:18 -04:00
Dillon Raphael
fb01cc7788 pnpmlock 2022-06-10 10:34:59 -04:00
github-actions[bot]
ac8c412da2 Version Packages (alpha) (#3419)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-10 10:31:19 -04:00
David
dfd2408e95 Added resolverBasePath option to rpc loaders (#3380)
Co-authored-by: beerose <alexsandra.sikora@gmail.com>
2022-06-08 14:50:00 -07:00
Dillon Raphael
9741287050 pnpmlock 2022-06-07 18:28:53 -04:00
github-actions[bot]
0e9c81abdc Version Packages (alpha) (#3409)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-07 18:28:29 -04:00
Dillon Raphael
9e05d6e155 Filter file extensions in codemod (#3408)
* add allowedExt to getAllFiles for codemod

* pnpmlock

* codemod
2022-06-07 18:24:10 -04:00
beerose
17f70e65ef Update pnpm-lock.yaml 2022-06-07 13:21:28 -07:00
github-actions[bot]
0ddc5a8169 Version Packages (alpha) (#3407)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-07 13:20:44 -07:00
Aleksandra
e6fb09d494 Use dist/templates for rpc template (#3406) 2022-06-07 13:02:13 -07:00
beerose
d846fc6be9 Update pnpm-lock.yaml 2022-06-07 11:47:05 -07:00
github-actions[bot]
f5e80e3835 Version Packages (alpha) (#3405)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-07 11:46:31 -07:00
Aleksandra
17ce29e5e4 Update rpc plugin setup in templates (#3404) 2022-06-07 11:33:06 -07:00
Aleksandra
46d9f81adf Update templates directory for codemod (#3402) 2022-06-07 11:04:34 -07:00
Dillon Raphael
994cfc6292 pnpmlock 2022-06-07 12:09:09 -04:00
github-actions[bot]
7811748526 Version Packages (alpha) (#3401)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-07 12:08:45 -04:00
Dillon Raphael
ce45368334 Add codemods to upgrade from framework to toolkit (#3353)
* init migrate cli command

* make sure migrate command can only be run in legacy blitz directory

* extract codemods into its own package/cli

* removed unused dependencies

* renamed codemods bin directory

* update next.config file to use withBlitz

* codemods update package.json step

* codemod for blitz server & client setup, as well as the blitz rpc route in pages dir

* fix ignore previous step if statement to check for less than instead of equal

* move files from app/pages to root pages directory

* remove blitz babel plugin & update imports inside app dir

* consolidate pages from app dir to root pages dir

* move all api directories to the root pages dir

* use generator templates for blitz rpc route & server/client setup files

* update custom server file

* make custom server step work with require statement & import statements

* useRouterQuery to useRouter().query

* pkg dependecy & import map updates

* Change import map

* Add BlitzLayout to import map

* import withBlitz and wrap App function in _app

* modify _document to use next.js imports

* fix default import for next modules & add useParam to source map

* gssp/gsp

* api routes

* dont run api wrap on rpc/[[...blitz]] & error on usage of local middleware

* DRY cleanup

* update codemod steps

* add ignore extension to getAllFiles

* add more imports to source map

* check for invokeWithMiddleware

* add error counter to middleware checker

* rename codemod & fix silly typo error

* update bin file & change all invokeWithMiddleware to invoke

* add logging from blitz

* manypkg fix

* add codemod test

* lockfile

* fix test

* show errors for invokeWithMiddleware

* update invokeWithMiddleware error message

* line break in new app generator before your new blitz app is ready

* Apply suggestions from code review

* Add changeset

* changeset

* pnpmlock

Co-authored-by: beerose <alexsandra.sikora@gmail.com>
2022-06-07 12:03:37 -04:00
Dillon Raphael
4e9c1f60b6 pnpm lock 2022-06-07 11:36:45 -04:00
github-actions[bot]
508682c8f8 Version Packages (alpha) (#3400)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-07 11:35:42 -04:00
Dillon Raphael
962eb58af6 Migrate printEnvInfo to CLI (#3394)
* init printEnvInfo function

* changeset
2022-06-07 11:31:56 -04:00
Dillon Raphael
17669b3af8 pnpmlock 2022-06-07 11:06:31 -04:00
github-actions[bot]
ec6299c36a Version Packages (alpha) (#3399)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-07 11:05:15 -04:00
Dillon Raphael
6ac2d3412a Update generator template npmrc & next versions (#3398)
* add strict-peer-dependencies=false
side-effects-cache=false to npmrc generator template

* add comment to generated npmrc file

* update versions of next for full and minimal templates

* pinned next version to 12.1.6 in generator templates

* add next back to root package

* fix lock file

* add changeset

Co-authored-by: Aleksandra <alexsandra.sikora@gmail.com>
2022-06-07 10:59:28 -04:00
122 changed files with 4194 additions and 855 deletions

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/rpc": patch
---
Add queryClient to RPC Plugin exports

View File

@@ -0,0 +1,6 @@
---
"@blitzjs/codemod": patch
"@blitzjs/generator": patch
---
codemod fixes

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/next": patch
---
Use `useRouter` from next/router in useParams function

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/generator": patch
---
updated nextjs version in generator & npmrc file

View File

@@ -0,0 +1,6 @@
---
"@blitzjs/auth": patch
"@blitzjs/next": patch
---
Add missing RouteUrlObject on Page.authenticate.redirectTo

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/codemod": patch
---
fix codemod for wrapping \_app arrow function & fix codemod for nested pages directory

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/codemod": patch
---
Update queryClient import in codemod

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/next": patch
---
Allow passing optional type argument for ParamsType in GSSP

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/generator": patch
---
Update codemod and template with a new queryClient import location

View File

@@ -0,0 +1,5 @@
---
"blitz": patch
---
detailed print env info

View File

@@ -0,0 +1,6 @@
---
"@blitzjs/next": patch
"@blitzjs/rpc": patch
---
Support RPC error middleware

View File

@@ -0,0 +1,7 @@
---
"blitz": patch
"@blitzjs/auth": patch
"@blitzjs/next": patch
---
rename middleware type for blitz server plugin

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/next": patch
---
useParam & useParams functions now accessible from @blitzjs/next

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/codemod": patch
---
Move middlewares from blitz config to blitz server with codemod

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/codemod": patch
---
Fix templates source in RPC codemod step

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/codemod": patch
---
Add codemod to upgrade from legacy framework to the Blitz Toolkit

View File

@@ -12,47 +12,75 @@
"@blitzjs/rpc": "2.0.0-alpha.0",
"@blitzjs/config": "0.0.0",
"@blitzjs/generator": "2.0.0-alpha.0",
"@blitzjs/codemod": "2.0.0-alpha.0",
"template": "0.0.0",
"toolkit-app": "1.0.0",
"test-qm": "0.0.0",
"test-no-suspense": "0.0.0",
"test-trailing-slash": "0.0.0"
"test-trailing-slash": "0.0.0",
"test-middleware": "0.0.0"
},
"changesets": [
"big-phones-bow",
"breezy-cameras-double",
"bright-mangos-run",
"cool-doors-invent",
"dirty-monkeys-greet",
"eleven-humans-sort",
"empty-berries-rule",
"empty-turkeys-wave",
"fair-wombats-sneeze",
"famous-kings-explain",
"fast-trainers-kneel",
"flat-bees-approve",
"four-brooms-juggle",
"four-meals-fry",
"fuzzy-jars-admire",
"gentle-dogs-reply",
"good-insects-wink",
"great-months-train",
"green-papayas-do",
"healthy-rice-shout",
"hot-drinks-approve",
"late-steaks-give",
"lovely-colts-share",
"lucky-cows-try",
"modern-cameras-pull",
"moody-squids-cheer",
"nervous-beds-travel",
"nervous-dolls-rule",
"nice-starfishes-live",
"nine-onions-admire",
"ninety-pets-heal",
"olive-bees-buy",
"olive-feet-rhyme",
"plenty-bottles-swim",
"poor-peas-lick",
"poor-penguins-look",
"poor-shrimps-think",
"purple-singers-greet",
"quiet-feet-travel",
"quiet-pans-hunt",
"rich-chairs-invent",
"sharp-falcons-begin",
"shy-olives-hang",
"silent-colts-reply",
"small-socks-confess",
"stupid-walls-sell",
"swift-drinks-dress",
"tame-keys-reply",
"tasty-news-collect",
"ten-hairs-listen",
"ten-rivers-burn",
"tender-pianos-check",
"thick-parrots-float",
"thirty-countries-build",
"twenty-beans-pump",
"two-kiwis-help",
"two-tigers-type",
"unlucky-papayas-sleep",
"violet-bags-leave",
"violet-lions-help",
"weak-suns-shave",
"wicked-ghosts-cough",
"wise-frogs-give"

View File

@@ -0,0 +1,7 @@
---
"@blitzjs/rpc": patch
"@blitzjs/codemod": patch
"@blitzjs/generator": patch
---
getQueryClient function & queryClient codemod updates & shared plugin config

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/next": patch
---
Removes the suspense wrapper from withBlitz since it's not needed

View File

@@ -0,0 +1,6 @@
---
"@blitzjs/rpc": patch
"@blitzjs/generator": patch
---
Update RPC plugin setup in templates

View File

@@ -0,0 +1,5 @@
---
"blitz": patch
---
Add aliases for Blitz CLI commands

View File

@@ -0,0 +1,6 @@
---
"blitz": patch
"@blitzjs/codemod": patch
---
init codemod generator

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/next": patch
---
Allow customizing PreviewData in gSSP

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/codemod": patch
---
allow extension catch in getAllFiles codemod util

View File

@@ -0,0 +1,5 @@
---
"blitz": patch
---
Fix running bin commands with Blitz CLI

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/codemod": patch
---
Update templates directory for codemod

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/rpc": patch
---
Add resolverBasePath to Blitz config to change the way rpc routes are generated

2
.github/CODEOWNERS vendored
View File

@@ -1,5 +1,5 @@
# https://help.github.com/en/github/creating-cloning-and-archiving-repositories/about-code-owners
* @flybayer @beerose
* @flybayer @beerose @dillonraphael
packages/generator/templates**/* @flybayer

43
.github/release-drafter.yml vendored Normal file
View File

@@ -0,0 +1,43 @@
name-template: "v$RESOLVED_VERSION"
tag-template: "v$RESOLVED_VERSION"
# categories:
# - title: 'Blitz'
# label: 'blitz'
# - title: '@blitzjs/next'
# label: 'blitz-next'
# - title: '@blitzjs/rpc'
# label: 'blitz-rpc'
# - title: '@blitzjs/auth'
# label: 'blitz-auth'
version-resolver:
major:
labels:
- "major"
minor:
labels:
- "minor"
patch:
labels:
- "patch"
default: patch
# autolabeler:
# - label: 'blitz'
# title:
# - '/blitz/i'
# - label: 'blitz-next'
# title:
# - '/@blitzjs\/next/i'
# - label: 'blitz-rpc'
# title:
# - '/@blitzjs\/rpc/i'
# - label: 'blitz-auth'
# title:
# - '/@blitzjs\/auth/i'
template: |
## Whats Changed
$CHANGES
## Contributors
$CONTRIBUTORS

View File

@@ -7,9 +7,15 @@ on:
concurrency: ${{ github.workflow }}-${{ github.ref }}
permissions:
contents: read
jobs:
release:
name: Release
permissions:
contents: write
pull-requests: write
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
@@ -32,13 +38,19 @@ jobs:
uses: pnpm/action-setup@646cdf48217256a3d0b80361c5a50727664284f2
with:
version: 6.32.6
- run: pnpm install --frozen-lockfile
- run: pnpm install --no-frozen-lockfile
- run: pnpm build
- name: Create Release Pull Request
uses: changesets/action@v1
with:
publish: pnpm release
createGithubReleases: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Github Release
uses: release-drafter/release-drafter@v5.19.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

1
.npmrc
View File

@@ -6,3 +6,4 @@ public-hoist-pattern[]=*types*
public-hoist-pattern[]=*eslint*
public-hoist-pattern[]=@prettier/plugin-*
public-hoist-pattern[]=*prettier-plugin-*
strict-peer-dependencies=false

View File

@@ -7,12 +7,6 @@ export const { withBlitz } = setupBlitzClient({
AuthClientPlugin({
cookiePrefix: "web-cookie-prefix",
}),
BlitzRpcPlugin({
reactQueryOptions: {
queries: {
staleTime: 7000,
},
},
}),
BlitzRpcPlugin({}),
],
})

View File

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

View File

@@ -10,6 +10,7 @@ module.exports = withBlitz(
customServer: {
hotReload: false,
},
resolverBasePath: "root",
},
}),
)

View File

@@ -0,0 +1,2 @@
SESSION_SECRET_KEY=hsdenhJfpLHrGjgdgg3jdF8g2bYD2PaQ
HEADLESS=true

View File

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

View File

@@ -0,0 +1,3 @@
node_modules
# Keep environment variables out of version control
*.sqlite

View File

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

View File

@@ -0,0 +1,7 @@
import {setupBlitzServer} from "@blitzjs/next"
const {gSSP, gSP, api} = setupBlitzServer({
plugins: [],
})
export {gSSP, gSP, api}

View File

@@ -0,0 +1,3 @@
export default async function getBasic() {
return {success: true}
}

View File

@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />
// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.

View File

@@ -0,0 +1,4 @@
const {withBlitz} = require("@blitzjs/next")
module.exports = withBlitz({
// update me
})

View File

@@ -0,0 +1,34 @@
{
"name": "test-middleware",
"version": "0.0.0",
"private": true,
"scripts": {
"start:dev": "next dev",
"test": "vitest run",
"test-watch": "vitest",
"start": "next start",
"lint": "next lint",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf .next"
},
"dependencies": {
"@blitzjs/config": "workspace:*",
"@blitzjs/next": "workspace:*",
"@blitzjs/rpc": "workspace:*",
"blitz": "workspace:*",
"next": "12.1.6-canary.17",
"react": "18.0.0",
"react-dom": "18.0.0"
},
"devDependencies": {
"@next/bundle-analyzer": "12.0.8",
"@types/express": "4.17.13",
"@types/fs-extra": "9.0.13",
"@types/node-fetch": "2.6.1",
"@types/react": "18.0.1",
"eslint": "7.32.0",
"fs-extra": "10.0.1",
"get-port": "6.1.2",
"node-fetch": "3.2.3",
"typescript": "^4.5.3"
}
}

View File

@@ -0,0 +1,34 @@
import {ErrorFallbackProps, ErrorComponent, ErrorBoundary, AppProps} from "@blitzjs/next"
import {AuthenticationError, AuthorizationError} from "blitz"
import React from "react"
import {withBlitz} from "../app/blitz-client"
function RootErrorFallback({error}: ErrorFallbackProps) {
if (error instanceof AuthenticationError) {
return <div id="error">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}>
<Component {...pageProps} />
</ErrorBoundary>
)
}
export default withBlitz(MyApp)

View File

@@ -0,0 +1,7 @@
import {NextResponse} from "next/server"
export function middleware() {
const response = NextResponse.next()
response.headers.set("global-middleware", "true")
return response
}

View File

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

View File

@@ -0,0 +1,5 @@
import {api} from "../../app/blitz-server"
export default api((_req, res) => {
res.status(200).json({success: true})
})

View File

@@ -0,0 +1,11 @@
export const getServerSideProps = () => {
return {props: {}}
}
export default function Web() {
return (
<div>
<h1>Web</h1>
</div>
)
}

View File

@@ -0,0 +1,72 @@
import {describe, it, expect, beforeAll, afterAll} from "vitest"
import {
killApp,
findPort,
launchApp,
nextBuild,
nextStart,
fetchViaHTTP,
} from "../../utils/next-test-utils"
import {join} from "path"
let app: any
let appPort: number
const appDir = join(__dirname, "../")
const runTests = () => {
describe("Middleware", () => {
describe("global middleware", () => {
it(
"should call global middleware on api calls",
async () => {
const res = await fetchViaHTTP(appPort, "/api/test", null, {
method: "GET",
})
expect(res.headers.get("global-middleware")).toBe("true")
},
5000 * 60 * 2,
)
it(
"should call global middleware on rpc calls",
async () => {
const res = await fetchViaHTTP(appPort, "/api/rpc/getBasic", null, {
method: "GET",
})
expect(res.headers.get("global-middleware")).toBe("true")
},
5000 * 60 * 2,
)
})
})
}
describe("Middleware Tests", () => {
describe("dev mode", () => {
beforeAll(async () => {
try {
appPort = await findPort()
app = await launchApp(appDir, appPort, {cwd: process.cwd()})
} catch (error) {
console.log(error)
}
}, 5000 * 60 * 2)
afterAll(async () => await killApp(app))
runTests()
})
describe("server mode", () => {
beforeAll(async () => {
try {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort, {cwd: process.cwd()})
} catch (err) {
console.log(err)
}
}, 5000 * 60 * 2)
afterAll(async () => await killApp(app))
runTests()
})
})

View File

@@ -0,0 +1,11 @@
{
"extends": "@blitzjs/config/tsconfig.nextjs.json",
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"compilerOptions": {
"paths": {
"react": ["./node_modules/@types/react"]
}
},
"exclude": ["node_modules"],
"baseUrl": "."
}

View File

@@ -7,13 +7,7 @@ const {withBlitz} = setupBlitzClient({
AuthClientPlugin({
cookiePrefix: "no-suspense-tests-cookie-prefix",
}),
BlitzRpcPlugin({
reactQueryOptions: {
queries: {
staleTime: 7000,
},
},
}),
BlitzRpcPlugin({}),
],
})

View File

@@ -7,13 +7,7 @@ const {withBlitz} = setupBlitzClient({
AuthClientPlugin({
cookiePrefix: "trailing-slash-tests-cookie-prefix",
}),
BlitzRpcPlugin({
reactQueryOptions: {
queries: {
staleTime: 7000,
},
},
}),
BlitzRpcPlugin({}),
],
})

View File

@@ -80,21 +80,7 @@ const BlitzWrapper = ({plugins, children}) => {
export function render(ui: RenderUI, {wrapper, router, ...options}: RenderOptions = {}) {
if (!wrapper) {
wrapper = ({children}) => {
return (
<BlitzWrapper
plugins={[
BlitzRpcPlugin({
reactQueryOptions: {
queries: {
staleTime: 7000,
},
},
}),
]}
>
{children}
</BlitzWrapper>
)
return <BlitzWrapper plugins={[BlitzRpcPlugin({})]}>{children}</BlitzWrapper>
}
}

View File

@@ -1,5 +1,126 @@
# @blitzjs/auth
## 2.0.0-alpha.46
### Patch Changes
- blitz@2.0.0-alpha.46
## 2.0.0-alpha.45
### Patch Changes
- blitz@2.0.0-alpha.45
## 2.0.0-alpha.44
### Patch Changes
- 7817fe3a: Add missing RouteUrlObject on Page.authenticate.redirectTo
- blitz@2.0.0-alpha.44
## 2.0.0-alpha.43
### Patch Changes
- Updated dependencies [527e48ac]
- blitz@2.0.0-alpha.43
## 2.0.0-alpha.42
### Patch Changes
- blitz@2.0.0-alpha.42
## 2.0.0-alpha.41
### Patch Changes
- blitz@2.0.0-alpha.41
## 2.0.0-alpha.40
### Patch Changes
- blitz@2.0.0-alpha.40
## 2.0.0-alpha.39
### Patch Changes
- Updated dependencies [b918055b]
- blitz@2.0.0-alpha.39
## 2.0.0-alpha.38
### Patch Changes
- blitz@2.0.0-alpha.38
## 2.0.0-alpha.37
### Patch Changes
- a80d2a8f: rename middleware type for blitz server plugin
- Updated dependencies [a80d2a8f]
- blitz@2.0.0-alpha.37
## 2.0.0-alpha.36
### Patch Changes
- blitz@2.0.0-alpha.36
## 2.0.0-alpha.35
### Patch Changes
- blitz@2.0.0-alpha.35
## 2.0.0-alpha.34
### Patch Changes
- blitz@2.0.0-alpha.34
## 2.0.0-alpha.33
### Patch Changes
- blitz@2.0.0-alpha.33
## 2.0.0-alpha.32
### Patch Changes
- blitz@2.0.0-alpha.32
## 2.0.0-alpha.31
### Patch Changes
- blitz@2.0.0-alpha.31
## 2.0.0-alpha.30
### Patch Changes
- Updated dependencies [ce453683]
- blitz@2.0.0-alpha.30
## 2.0.0-alpha.29
### Patch Changes
- Updated dependencies [962eb58a]
- blitz@2.0.0-alpha.29
## 2.0.0-alpha.28
### Patch Changes
- blitz@2.0.0-alpha.28
## 2.0.0-alpha.27
### Patch Changes

View File

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

View File

@@ -205,7 +205,7 @@ export type RedirectAuthenticatedToFn = (
export type BlitzPage<P = {}> = React.ComponentType<P> & {
getLayout?: (component: JSX.Element) => JSX.Element
authenticate?: boolean | {redirectTo?: string}
authenticate?: boolean | {redirectTo?: string | RouteUrlObject}
suppressFirstRenderFlicker?: boolean
redirectAuthenticatedTo?: RedirectAuthenticatedTo | RedirectAuthenticatedToFn
}

View File

@@ -1,4 +1,4 @@
import type {BlitzServerPlugin, Middleware, Ctx} from "blitz"
import type {BlitzServerPlugin, RequestMiddleware, Ctx} from "blitz"
import {assert} from "blitz"
import {IncomingMessage, ServerResponse} from "http"
import {PublicData, SessionModel, SessionConfigMethods} from "../shared/types"
@@ -101,7 +101,7 @@ export function AuthServerPlugin(options: AuthPluginOptions): BlitzServerPlugin<
`The cookie prefix used has invalid characters. Only alphanumeric characters, "-" and "_" character are supported`,
)
const blitzSessionMiddleware: Middleware<
const blitzSessionMiddleware: RequestMiddleware<
IncomingMessage,
ServerResponse & {blitzCtx: Ctx}
> = async (req, res, next) => {
@@ -119,6 +119,6 @@ export function AuthServerPlugin(options: AuthPluginOptions): BlitzServerPlugin<
return blitzSessionMiddleware
}
return {
middlewares: [authPluginSessionMiddleware()],
requestMiddlewares: [authPluginSessionMiddleware()],
}
}

View File

@@ -8,7 +8,7 @@ import {
connectMiddleware,
Ctx,
handleRequestWithMiddleware,
Middleware,
RequestMiddleware,
MiddlewareResponse,
secureProxyMiddleware,
} from "blitz"
@@ -79,9 +79,9 @@ export function passportAuth(config: BlitzPassportConfig): ApiHandler {
const passportMiddleware = passport.initialize()
const middleware: Middleware<ApiHandlerIncomingMessage, MiddlewareResponse<Ctx>>[] = [
connectMiddleware(cookieSessionMiddleware as Middleware),
connectMiddleware(passportMiddleware as Middleware),
const middleware: RequestMiddleware<ApiHandlerIncomingMessage, MiddlewareResponse<Ctx>>[] = [
connectMiddleware(cookieSessionMiddleware as RequestMiddleware),
connectMiddleware(passportMiddleware as RequestMiddleware),
connectMiddleware(passport.session()),
]

View File

@@ -1,5 +1,132 @@
# @blitzjs/next
## 2.0.0-alpha.46
### Patch Changes
- bee19a25: Support RPC error middleware
- 9ada0f66: Allow customizing PreviewData in gSSP
- Updated dependencies [bee19a25]
- @blitzjs/rpc@2.0.0-alpha.46
## 2.0.0-alpha.45
### Patch Changes
- 5c5decbc: Removes the suspense wrapper from withBlitz since it's not needed
- @blitzjs/rpc@2.0.0-alpha.45
## 2.0.0-alpha.44
### Patch Changes
- 7817fe3a: Add missing RouteUrlObject on Page.authenticate.redirectTo
- ae0b714f: Allow passing optional type argument for ParamsType in GSSP
- @blitzjs/rpc@2.0.0-alpha.44
## 2.0.0-alpha.43
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.43
## 2.0.0-alpha.42
### Patch Changes
- 9c2e7d37: Use `useRouter` from next/router in useParams function
- @blitzjs/rpc@2.0.0-alpha.42
## 2.0.0-alpha.41
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.41
## 2.0.0-alpha.40
### Patch Changes
- 9ded8dac: useParam & useParams functions now accessible from @blitzjs/next
- @blitzjs/rpc@2.0.0-alpha.40
## 2.0.0-alpha.39
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.39
## 2.0.0-alpha.38
### Patch Changes
- Updated dependencies [8aee25c5]
- @blitzjs/rpc@2.0.0-alpha.38
## 2.0.0-alpha.37
### Patch Changes
- a80d2a8f: rename middleware type for blitz server plugin
- @blitzjs/rpc@2.0.0-alpha.37
## 2.0.0-alpha.36
### Patch Changes
- Updated dependencies [4cad9cca]
- @blitzjs/rpc@2.0.0-alpha.36
## 2.0.0-alpha.35
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.35
## 2.0.0-alpha.34
### Patch Changes
- Updated dependencies [dfd2408e]
- @blitzjs/rpc@2.0.0-alpha.34
## 2.0.0-alpha.33
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.33
## 2.0.0-alpha.32
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.32
## 2.0.0-alpha.31
### Patch Changes
- Updated dependencies [17ce29e5]
- @blitzjs/rpc@2.0.0-alpha.31
## 2.0.0-alpha.30
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.30
## 2.0.0-alpha.29
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.29
## 2.0.0-alpha.28
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.28
## 2.0.0-alpha.27
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/next",
"version": "2.0.0-alpha.27",
"version": "2.0.0-alpha.46",
"scripts": {
"build": "unbuild",
"dev": "pnpm predev && pnpm watch unbuild src --wait=0.2",
@@ -23,7 +23,7 @@
"eslint.js"
],
"dependencies": {
"@blitzjs/rpc": "2.0.0-alpha.27",
"@blitzjs/rpc": "2.0.0-alpha.46",
"@types/hoist-non-react-statics": "3.3.1",
"debug": "4.3.3",
"fs-extra": "10.0.1",
@@ -32,21 +32,19 @@
"superjson": "1.8.0"
},
"devDependencies": {
"@blitzjs/config": "workspace:2.0.0-alpha.27",
"@blitzjs/config": "workspace:2.0.0-alpha.46",
"@testing-library/dom": "8.13.0",
"@testing-library/jest-dom": "5.16.3",
"@testing-library/react": "13.0.0",
"@testing-library/react-hooks": "7.0.2",
"@testing-library/user-event": "13.5.0",
"@types/lodash.frompairs": "4.0.6",
"@types/node": "17.0.16",
"@types/react": "18.0.1",
"@types/react-dom": "17.0.14",
"@types/testing-library__react-hooks": "4.0.0",
"blitz": "2.0.0-alpha.27",
"blitz": "2.0.0-alpha.46",
"cross-spawn": "7.0.3",
"find-up": "4.1.0",
"lodash.frompairs": "4.0.1",
"next": "12.1.6-canary.17",
"react": "18.0.0",
"react-dom": "18.0.0",

View File

@@ -17,6 +17,7 @@ import {Router} from "next/router"
export * from "./error-boundary"
export * from "./error-component"
export * from "./use-params"
export {Routes} from ".blitz"
const compose =
@@ -50,11 +51,7 @@ const buildWithBlitz = <TPlugins extends readonly ClientPlugin<object>[]>(plugin
<>
{/* @ts-ignore todo */}
{props.Component.suppressFirstRenderFlicker && <NoPageFlicker />}
{mounted && (
<React.Suspense fallback="Loading...">
<UserAppRoot {...props} Component={component} />
</React.Suspense>
)}
{mounted && <UserAppRoot {...props} Component={component} />}
</>
</BlitzProvider>
)
@@ -81,7 +78,7 @@ type RedirectAuthenticatedToFnCtx = {
type RedirectAuthenticatedToFn = (args: RedirectAuthenticatedToFnCtx) => RedirectAuthenticatedTo
export type BlitzPage<P = {}> = React.ComponentType<P> & {
getLayout?: (component: JSX.Element) => JSX.Element
authenticate?: boolean | {redirectTo?: string}
authenticate?: boolean | {redirectTo?: string | RouteUrlObject}
suppressFirstRenderFlicker?: boolean
redirectAuthenticatedTo?: RedirectAuthenticatedTo | RedirectAuthenticatedToFn
}

View File

@@ -1,3 +1,4 @@
import type {NextConfig} from "next"
import {
GetServerSideProps,
GetServerSidePropsResult,
@@ -7,21 +8,22 @@ import {
NextApiResponse,
} from "next"
import type {
Ctx as BlitzCtx,
BlitzServerPlugin,
Middleware,
MiddlewareResponse,
AsyncFunc,
FirstParam,
AddParameters,
AsyncFunc,
BlitzServerPlugin,
Ctx as BlitzCtx,
FirstParam,
RequestMiddleware,
MiddlewareResponse,
} from "blitz"
import {handleRequestWithMiddleware, startWatcher, stopWatcher} from "blitz"
import type {NextConfig} from "next"
import {getQueryKey, getInfiniteQueryKey, installWebpackConfig} from "@blitzjs/rpc"
import {dehydrate} from "@blitzjs/rpc"
import {dehydrate, getQueryKey, getInfiniteQueryKey, loaderClient, loaderServer} from "@blitzjs/rpc"
import {DefaultOptions, QueryClient} from "react-query"
import {IncomingMessage, ServerResponse} from "http"
import {withSuperJsonProps} from "./superjson"
import {ResolverBasePath} from "@blitzjs/rpc/src/index-server"
import {ParsedUrlQuery} from "querystring"
import {PreviewData} from "next/types"
export * from "./index-browser"
@@ -38,20 +40,33 @@ export type NextApiHandler = (
) => void | Promise<void>
type SetupBlitzOptions = {
plugins: BlitzServerPlugin<Middleware, Ctx>[]
plugins: BlitzServerPlugin<RequestMiddleware, Ctx>[]
onError?: (err: Error) => void
}
export type BlitzGSSPHandler<TProps> = ({
export type BlitzGSSPHandler<
TProps,
Query extends ParsedUrlQuery = ParsedUrlQuery,
PD extends PreviewData = PreviewData,
> = ({
ctx,
req,
res,
...args
}: Parameters<GetServerSideProps<TProps>>[0] & {ctx: Ctx}) => ReturnType<GetServerSideProps<TProps>>
}: Parameters<GetServerSideProps<TProps, Query, PD>>[0] & {ctx: Ctx}) => ReturnType<
GetServerSideProps<TProps, Query, PD>
>
export type BlitzGSPHandler<TProps> = ({
export type BlitzGSPHandler<
TProps,
Query extends ParsedUrlQuery = ParsedUrlQuery,
PD extends PreviewData = PreviewData,
> = ({
ctx,
...args
}: Parameters<GetStaticProps<TProps>>[0] & {ctx: Ctx}) => ReturnType<GetStaticProps<TProps>>
}: Parameters<GetStaticProps<TProps, Query, PD>>[0] & {ctx: Ctx}) => ReturnType<
GetStaticProps<TProps, Query, PD>
>
export type BlitzAPIHandler = (
req: Parameters<NextApiHandler>[0],
@@ -59,12 +74,14 @@ export type BlitzAPIHandler = (
ctx: Ctx,
) => ReturnType<NextApiHandler>
export const setupBlitzServer = ({plugins}: SetupBlitzOptions) => {
const middlewares = plugins.flatMap((p) => p.middlewares)
export const setupBlitzServer = ({plugins, onError}: SetupBlitzOptions) => {
const middlewares = plugins.flatMap((p) => p.requestMiddlewares)
const contextMiddleware = plugins.flatMap((p) => p.contextMiddleware).filter(Boolean)
const gSSP =
<TProps>(handler: BlitzGSSPHandler<TProps>): GetServerSideProps<TProps> =>
<TProps, Query extends ParsedUrlQuery = ParsedUrlQuery, PD extends PreviewData = PreviewData>(
handler: BlitzGSSPHandler<TProps, Query, PD>,
): GetServerSideProps<TProps, Query, PD> =>
async ({req, res, ...rest}) => {
await handleRequestWithMiddleware<IncomingMessage, ServerResponse>(req, res, middlewares)
const ctx = contextMiddleware.reduceRight(
@@ -88,12 +105,19 @@ export const setupBlitzServer = ({plugins}: SetupBlitzOptions) => {
ctx.prefetchQuery = prefetchQuery
ctx.prefetchInfiniteQuery = (...args) => prefetchQuery(...args, true)
const result = await handler({req, res, ctx, ...rest})
return withSuperJsonProps(withDehydratedState(result, queryClient))
try {
const result = await handler({req, res, ctx, ...rest})
return withSuperJsonProps(withDehydratedState(result, queryClient))
} catch (err: any) {
onError?.(err)
throw err
}
}
const gSP =
<TProps>(handler: BlitzGSPHandler<TProps>): GetStaticProps<TProps> =>
<TProps, Query extends ParsedUrlQuery = ParsedUrlQuery, PD extends PreviewData = PreviewData>(
handler: BlitzGSPHandler<TProps, Query, PD>,
): GetStaticProps<TProps, Query, PD> =>
async (context) => {
const ctx = contextMiddleware.reduceRight((y, f) => (f ? f(y) : y), {} as Ctx)
let queryClient: null | QueryClient = null
@@ -113,8 +137,13 @@ export const setupBlitzServer = ({plugins}: SetupBlitzOptions) => {
ctx.prefetchQuery = prefetchQuery
ctx.prefetchInfiniteQuery = (...args) => prefetchQuery(...args, true)
const result = await handler({...context, ctx: ctx})
return withSuperJsonProps(withDehydratedState(result, queryClient))
try {
const result = await handler({...context, ctx: ctx})
return withSuperJsonProps(withDehydratedState(result, queryClient))
} catch (err: any) {
onError?.(err)
throw err
}
}
const api =
@@ -123,7 +152,8 @@ export const setupBlitzServer = ({plugins}: SetupBlitzOptions) => {
try {
await handleRequestWithMiddleware(req, res, middlewares)
return handler(req, res, res.blitzCtx)
} catch (error) {
} catch (error: any) {
onError?.(error)
return res.status(400).send(error)
}
}
@@ -133,12 +163,59 @@ export const setupBlitzServer = ({plugins}: SetupBlitzOptions) => {
export interface BlitzConfig extends NextConfig {
blitz?: {
resolverBasePath?: ResolverBasePath
customServer?: {
hotReload?: boolean
}
}
}
interface WebpackRuleOptions {
resolverBasePath?: ResolverBasePath
}
interface WebpackRule {
test: RegExp
use: Array<{
loader: string
options: WebpackRuleOptions
}>
}
interface InstallWebpackConfigOptions {
webpackConfig: {
module: {
rules: WebpackRule[]
}
}
nextConfig: BlitzConfig
}
export function installWebpackConfig({webpackConfig, nextConfig}: InstallWebpackConfigOptions) {
const options: WebpackRuleOptions = {
resolverBasePath: nextConfig.blitz?.resolverBasePath,
}
webpackConfig.module.rules.push({
test: /\/\[\[\.\.\.blitz]]\.[jt]s$/,
use: [
{
loader: loaderServer,
options,
},
],
})
webpackConfig.module.rules.push({
test: /[\\/](queries|mutations)[\\/]/,
use: [
{
loader: loaderClient,
options,
},
],
})
}
export function withBlitz(nextConfig: BlitzConfig = {}) {
if (
process.env.NODE_ENV !== "production" &&
@@ -158,8 +235,8 @@ export function withBlitz(nextConfig: BlitzConfig = {}) {
}
const config = Object.assign({}, nextConfig, {
webpack: (config: any, options: any) => {
installWebpackConfig(config)
webpack: (config: InstallWebpackConfigOptions["webpackConfig"], options: any) => {
installWebpackConfig({webpackConfig: config, nextConfig})
if (typeof nextConfig.webpack === "function") {
return nextConfig.webpack(config, options)
}

View File

@@ -1,9 +1,8 @@
/**
* @vitest-environment jsdom
*/
import React from "react"
import {describe, it, expect, vi} from "vitest"
import {describe, it, expect, vi, afterEach} from "vitest"
import {extractRouterParams, useParam, useParams} from "./use-params"
import {renderHook as defaultRenderHook} from "@testing-library/react-hooks"
import {NextRouter} from "next/router"
@@ -16,6 +15,14 @@ type RenderHookOptions = DefaultHookParams[1] & {
dehydratedState?: unknown
}
// This is the router query object which includes route params
const query = {
id: "1",
cat: "category",
slug: ["example", "multiple", "slugs"],
empty: "",
}
const mockRouter: NextRouter = {
basePath: "",
pathname: "/",
@@ -54,6 +61,10 @@ export function renderHook(
}
describe("extractRouterParams", () => {
afterEach(() => {
vi.clearAllMocks()
})
it("returns proper params", () => {
const routerQuery = {
id: "1",
@@ -81,92 +92,99 @@ describe("extractRouterParams", () => {
})
describe("useParams", () => {
afterEach(() => {
vi.clearAllMocks()
})
it("works without parameter", () => {
// This is the router query object which includes route params
const query = {
id: "1",
cat: "category",
slug: ["example", "multiple", "slugs"],
empty: "",
}
const {result} = renderHook(() => useParams(), {router: {query}})
expect(result.current).toEqual({
id: "1",
cat: "category",
slug: ["example", "multiple", "slugs"],
empty: "",
vi.mock("next/router", () => {
return {
useRouter: vi.fn(() => ({...mockRouter, query})),
}
})
})
it("works with string", () => {
// This is the router query object which includes route params
const query = {
id: "1",
cat: "category",
slug: ["example", "multiple", "slugs"],
empty: "",
}
it("works with string", () => {
vi.mock("next/router", () => {
return {
useRouter: vi.fn(() => ({...mockRouter, query})),
}
})
const {result} = renderHook(() => useParams("string"), {
router: {query},
const {result} = renderHook(() => useParams("string"), {
router: {query},
})
expect(result.current).toEqual({
id: "1",
cat: "category",
empty: "",
})
})
expect(result.current).toEqual({
id: "1",
cat: "category",
empty: "",
})
})
it("works with number", () => {
// This is the router query object which includes route params
const query = {
id: "1",
cat: "category",
slug: ["example", "multiple", "slugs"],
empty: "",
}
it("works with string", () => {
vi.mock("next/router", () => {
return {
useRouter: vi.fn(() => ({...mockRouter, query})),
}
})
const {result} = renderHook(() => useParams("number"), {
router: {query},
const {result} = renderHook(() => useParams("string"), {
router: {query},
})
expect(result.current).toEqual({
id: "1",
cat: "category",
empty: "",
})
})
expect(result.current).toEqual({
id: 1,
cat: undefined,
slug: undefined,
})
})
it("works with array", () => {
// This is the router query object which includes route params
const query = {
id: "1",
cat: "category",
slug: ["example", "multiple", "slugs"],
empty: "",
}
it("works with number", () => {
vi.mock("next/router", () => {
return {
useRouter: vi.fn(() => ({...mockRouter, query})),
}
})
const {result} = renderHook(() => useParams("array"), {
router: {query},
const {result} = renderHook(() => useParams("number"), {
router: {query},
})
expect(result.current).toEqual({
id: 1,
cat: undefined,
slug: undefined,
})
})
expect(result.current).toEqual({
id: ["1"],
cat: ["category"],
slug: ["example", "multiple", "slugs"],
empty: [""],
it("works with array", () => {
vi.mock("next/router", () => {
return {
useRouter: vi.fn(() => ({...mockRouter, query})),
}
})
const {result} = renderHook(() => useParams("array"), {
router: {query},
})
expect(result.current).toEqual({
id: ["1"],
cat: ["category"],
slug: ["example", "multiple", "slugs"],
empty: [""],
})
})
})
})
describe("useParam", () => {
afterEach(() => {
vi.clearAllMocks()
})
it("works without parameter", () => {
// This is the router query object which includes route params
const query = {
id: "1",
cat: "category",
slug: ["example", "multiple", "slugs"],
empty: "",
}
vi.mock("next/router", () => {
return {
useRouter: vi.fn(() => ({...mockRouter, query})),
}
})
let {result} = renderHook(() => useParam("id"), {router: {query}})
expect(result.current).toEqual("1")
@@ -183,13 +201,11 @@ describe("useParam", () => {
})
it("works with string", () => {
// This is the router query object which includes route params
const query = {
id: "1",
cat: "category",
slug: ["example", "multiple", "slugs"],
empty: "",
}
vi.mock("next/router", () => {
return {
useRouter: vi.fn(() => ({...mockRouter, query})),
}
})
let {result} = renderHook(() => useParam("id", "string"), {
router: {query},

View File

@@ -1,22 +1,10 @@
import fromPairs from "lodash.frompairs"
import {NextRouter} from "next/router"
import {useRouter} from "next/router"
import {ParsedUrlQuery} from "querystring"
import React from "react"
import {RouterContext} from "./router-context"
type Dict<T> = Record<string, T | undefined>
type ReturnTypes = "string" | "number" | "array"
/**
* `useRouter` is a React hook used to access `router` object within components
*
* @returns `router` object
* @see Docs {@link https://blitzjs.com/docs/router#router-object | router}
*/
export function useRouter(): NextRouter {
return React.useContext(RouterContext)
}
/*
* Based on the code of https://github.com/lukeed/qss
*/
@@ -81,7 +69,7 @@ function areQueryValuesEqual(value1: ParsedUrlQueryValue, value2: ParsedUrlQuery
}
export function extractRouterParams(routerQuery: ParsedUrlQuery, asPathQuery: ParsedUrlQuery) {
return fromPairs(
return Object.fromEntries(
Object.entries(routerQuery).filter(
([key, value]) =>
typeof asPathQuery[key] === "undefined" || !areQueryValuesEqual(value, asPathQuery[key]),

View File

@@ -1,5 +1,149 @@
# @blitzjs/rpc
## 2.0.0-alpha.46
### Patch Changes
- bee19a25: Support RPC error middleware
- @blitzjs/auth@2.0.0-alpha.46
- blitz@2.0.0-alpha.46
## 2.0.0-alpha.45
### Patch Changes
- @blitzjs/auth@2.0.0-alpha.45
- blitz@2.0.0-alpha.45
## 2.0.0-alpha.44
### Patch Changes
- Updated dependencies [7817fe3a]
- @blitzjs/auth@2.0.0-alpha.44
- blitz@2.0.0-alpha.44
## 2.0.0-alpha.43
### Patch Changes
- Updated dependencies [527e48ac]
- blitz@2.0.0-alpha.43
- @blitzjs/auth@2.0.0-alpha.43
## 2.0.0-alpha.42
### Patch Changes
- @blitzjs/auth@2.0.0-alpha.42
- blitz@2.0.0-alpha.42
## 2.0.0-alpha.41
### Patch Changes
- @blitzjs/auth@2.0.0-alpha.41
- blitz@2.0.0-alpha.41
## 2.0.0-alpha.40
### Patch Changes
- @blitzjs/auth@2.0.0-alpha.40
- blitz@2.0.0-alpha.40
## 2.0.0-alpha.39
### Patch Changes
- Updated dependencies [b918055b]
- blitz@2.0.0-alpha.39
- @blitzjs/auth@2.0.0-alpha.39
## 2.0.0-alpha.38
### Patch Changes
- 8aee25c5: getQueryClient function & queryClient codemod updates & shared plugin config
- blitz@2.0.0-alpha.38
- @blitzjs/auth@2.0.0-alpha.38
## 2.0.0-alpha.37
### Patch Changes
- Updated dependencies [a80d2a8f]
- blitz@2.0.0-alpha.37
- @blitzjs/auth@2.0.0-alpha.37
## 2.0.0-alpha.36
### Patch Changes
- 4cad9cca: Add queryClient to RPC Plugin exports
- blitz@2.0.0-alpha.36
- @blitzjs/auth@2.0.0-alpha.36
## 2.0.0-alpha.35
### Patch Changes
- blitz@2.0.0-alpha.35
- @blitzjs/auth@2.0.0-alpha.35
## 2.0.0-alpha.34
### Patch Changes
- dfd2408e: Add resolverBasePath to Blitz config to change the way rpc routes are generated
- @blitzjs/auth@2.0.0-alpha.34
- blitz@2.0.0-alpha.34
## 2.0.0-alpha.33
### Patch Changes
- @blitzjs/auth@2.0.0-alpha.33
- blitz@2.0.0-alpha.33
## 2.0.0-alpha.32
### Patch Changes
- @blitzjs/auth@2.0.0-alpha.32
- blitz@2.0.0-alpha.32
## 2.0.0-alpha.31
### Patch Changes
- 17ce29e5: Update RPC plugin setup in templates
- blitz@2.0.0-alpha.31
- @blitzjs/auth@2.0.0-alpha.31
## 2.0.0-alpha.30
### Patch Changes
- Updated dependencies [ce453683]
- blitz@2.0.0-alpha.30
- @blitzjs/auth@2.0.0-alpha.30
## 2.0.0-alpha.29
### Patch Changes
- Updated dependencies [962eb58a]
- blitz@2.0.0-alpha.29
- @blitzjs/auth@2.0.0-alpha.29
## 2.0.0-alpha.28
### Patch Changes
- blitz@2.0.0-alpha.28
- @blitzjs/auth@2.0.0-alpha.28
## 2.0.0-alpha.27
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/rpc",
"version": "2.0.0-alpha.27",
"version": "2.0.0-alpha.46",
"scripts": {
"build": "unbuild",
"predev": "wait-on -d 250 ../blitz/dist/index-server.d.ts && wait-on -d 250 ../blitz-auth/dist/index-browser.d.ts",
@@ -20,7 +20,7 @@
"dist/**"
],
"dependencies": {
"@blitzjs/auth": "2.0.0-alpha.27",
"@blitzjs/auth": "2.0.0-alpha.46",
"b64-lite": "1.4.0",
"bad-behavior": "1.0.1",
"chalk": "^4.1.0",
@@ -30,11 +30,11 @@
"zod": "3.10.1"
},
"devDependencies": {
"@blitzjs/config": "workspace:2.0.0-alpha.27",
"@blitzjs/config": "workspace:2.0.0-alpha.46",
"@types/debug": "4.1.7",
"@types/react": "18.0.1",
"@types/react-dom": "17.0.14",
"blitz": "2.0.0-alpha.27",
"blitz": "2.0.0-alpha.46",
"next": "12.1.6-canary.17",
"react": "18.0.0",
"react-dom": "18.0.0",
@@ -43,7 +43,7 @@
"watch": "1.0.2"
},
"peerDependencies": {
"blitz": "2.0.0-alpha.27",
"blitz": "2.0.0-alpha.46",
"next": "*"
},
"publishConfig": {

View File

@@ -2,11 +2,11 @@ export * from "./rpc"
export {useQuery, usePaginatedQuery, useInfiniteQuery, useMutation} from "./react-query"
export type {MutateFunction} from "./react-query"
export {
queryClient,
getQueryKey,
getInfiniteQueryKey,
invalidateQuery,
setQueryData,
getQueryClient,
} from "./react-query-utils"
export {useQueryErrorResetBoundary, QueryClient} from "react-query"
export {dehydrate} from "react-query/hydration"

View File

@@ -54,8 +54,8 @@ export const initializeQueryClient = () => {
})
}
// Create internal QueryClient instance
export const queryClient = initializeQueryClient()
// Query client is initialised in `BlitzRpcPlugin`, and can only be used with BlitzRpcPlugin right now
export const getQueryClient = () => globalThis.queryClient
function isRpcClient(f: any): f is RpcClient<any, any> {
return !!f._isRpcClient
@@ -176,7 +176,7 @@ export function invalidateQuery<TInput, TResult, T extends AsyncFunc>(
// Params not provided, only use first query key item (url)
queryKey = fullQueryKey[0]
}
return queryClient.invalidateQueries(queryKey)
return getQueryClient().invalidateQueries(queryKey)
}
export function setQueryData<TInput, TResult, T extends AsyncFunc>(
@@ -184,15 +184,15 @@ export function setQueryData<TInput, TResult, T extends AsyncFunc>(
params: TInput,
newData: TResult | ((oldData: TResult | undefined) => TResult),
opts: MutateOptions = {refetch: true},
): Promise<void | ReturnType<typeof queryClient.invalidateQueries>> {
): Promise<void | ReturnType<ReturnType<typeof getQueryClient>["invalidateQueries"]>> {
if (typeof resolver === "undefined") {
throw new Error("setQueryData is missing the first argument - it must be a resolver function")
}
const queryKey = getQueryKey(resolver, params)
return new Promise((res) => {
queryClient.setQueryData(queryKey, newData)
let result: void | ReturnType<typeof queryClient.invalidateQueries>
getQueryClient().setQueryData(queryKey, newData)
let result: void | ReturnType<ReturnType<typeof getQueryClient>["invalidateQueries"]>
if (opts.refetch) {
result = invalidateQuery(resolver, params)
}

View File

@@ -2,8 +2,8 @@ import {normalizePathTrailingSlash} from "next/dist/client/normalize-trailing-sl
import {addBasePath} from "next/dist/shared/lib/router/router"
import {deserialize, serialize} from "superjson"
import {SuperJSONResult} from "superjson/dist/types"
import {isServer, CSRFTokenMismatchError} from "blitz"
import {getQueryKeyFromUrlAndParams, queryClient} from "./react-query-utils"
import {CSRFTokenMismatchError, isServer} from "blitz"
import {getQueryKeyFromUrlAndParams, getQueryClient} from "./react-query-utils"
import {
getAntiCSRFToken,
getPublicDataStore,
@@ -55,7 +55,7 @@ export function __internal_buildRpcClient({
resolverType,
routePath,
}: BuildRpcClientParams): RpcClient {
const fullRoutePath = normalizeApiRoute("/api/rpc/" + routePath)
const fullRoutePath = normalizeApiRoute("/api/rpc" + routePath)
const httpClient: RpcClientBase = async (params, opts = {}) => {
const debug = (await import("debug")).default("blitz:rpc")
@@ -123,9 +123,9 @@ export function __internal_buildRpcClient({
setTimeout(async () => {
// Do these in the next tick to prevent various bugs like https://github.com/blitz-js/blitz/issues/2207
debug("Invalidating react-query cache...")
await queryClient.cancelQueries()
await queryClient.resetQueries()
queryClient.getMutationCache().clear()
await getQueryClient().cancelQueries()
await getQueryClient().resetQueries()
getQueryClient().getMutationCache().clear()
// We have a 100ms delay here to prevent unnecessary stale queries from running
// This prevents the case where you logout on a page with
// Page.authenticate = {redirectTo: '/login'}
@@ -184,7 +184,7 @@ export function __internal_buildRpcClient({
if (!opts.fromQueryHook) {
const queryKey = getQueryKeyFromUrlAndParams(routePath, params)
queryClient.setQueryData(queryKey, data)
getQueryClient().setQueryData(queryKey, data)
}
return data
}

View File

@@ -4,14 +4,13 @@ import {DefaultOptions, QueryClient} from "react-query"
export * from "./data-client/index"
export const queryClient = globalThis.queryClient
interface BlitzRpcOptions {
reactQueryOptions?: DefaultOptions
}
export const BlitzRpcPlugin = createClientPlugin<BlitzRpcOptions, any>(
({reactQueryOptions}: BlitzRpcOptions) => {
export const BlitzRpcPlugin = createClientPlugin<BlitzRpcOptions, {queryClient: QueryClient}>(
(options?: BlitzRpcOptions) => {
const initializeQueryClient = () => {
const {reactQueryOptions} = options || {}
let suspenseEnabled = reactQueryOptions?.queries?.suspense ?? true
if (!process.env.CLI_COMMAND_CONSOLE && !process.env.CLI_COMMAND_DB) {
globalThis.__BLITZ_SUSPENSE_ENABLED = suspenseEnabled
@@ -36,12 +35,14 @@ export const BlitzRpcPlugin = createClientPlugin<BlitzRpcOptions, any>(
},
})
}
globalThis.queryClient = initializeQueryClient()
const queryClient = initializeQueryClient()
globalThis.queryClient = queryClient
return {
events: {},
middleware: {},
exports: () => {},
exports: () => ({
queryClient,
}),
}
},
)

View File

@@ -1,7 +1,8 @@
import {assert, Ctx, baseLogger, prettyMs, newLine} from "blitz"
import {assert, baseLogger, Ctx, newLine, prettyMs} from "blitz"
import {NextApiRequest, NextApiResponse} from "next"
import {deserialize, serialize as superjsonSerialize} from "superjson"
import chalk from "chalk"
import {resolve} from "path"
// TODO - optimize end user server bundles by not exporting all client stuff here
export * from "./index-browser"
@@ -12,6 +13,7 @@ export * from "./resolver"
function isObject(value: unknown): value is Record<string | symbol, unknown> {
return typeof value === "object" && value !== null
}
function getGlobalObject<T extends Record<string, unknown>>(key: string, defaultValue: T): T {
assert(key.startsWith("__internal_blitz"), "unsupported key")
if (typeof global === "undefined") {
@@ -24,6 +26,7 @@ function getGlobalObject<T extends Record<string, unknown>>(key: string, default
type Resolver = (...args: unknown[]) => Promise<unknown>
type ResolverFiles = Record<string, () => Promise<{default?: Resolver}>>
export type ResolverBasePath = "queries|mutations" | "root" | undefined
// We define `global.__internal_blitzRpcResolverFiles` to ensure we use the same global object.
// Needed for Next.js. I'm guessing that Next.js is including the `node_modules/` files in a seperate bundle than user files.
@@ -47,21 +50,9 @@ export function __internal_addBlitzRpcResolver(
return resolver
}
import {resolve} from "path"
const dir = __dirname + (() => "")() // trick to avoid `@vercel/ncc` to glob import
const loaderServer = resolve(dir, "./loader-server.cjs")
const loaderClient = resolve(dir, "./loader-client.cjs")
export function installWebpackConfig<T extends any[]>(config: {module?: {rules?: T}}) {
config.module!.rules!.push({
test: /\/\[\[\.\.\.blitz]]\.[jt]s$/,
use: [{loader: loaderServer}],
})
config.module!.rules!.push({
test: /[\\/](queries|mutations)[\\/]/,
use: [{loader: loaderClient}],
})
}
export const loaderServer = resolve(dir, "./loader-server.cjs")
export const loaderClient = resolve(dir, "./loader-client.cjs")
// ----------
// END LOADER
@@ -190,6 +181,9 @@ export function rpcHandler(config: RpcConfig) {
if (error._clearStack) {
delete error.stack
}
config.onError?.(error)
log.error(error)
newLine()

View File

@@ -3,20 +3,13 @@ import {
convertFilePathToResolverName,
convertFilePathToResolverType,
convertPageFilePathToRoutePath,
Loader,
LoaderOptions,
toPosixPath,
} from "./loader-utils"
import {assert} from "blitz"
import {posix} from "path"
// Subset of `import type { LoaderDefinitionFunction } from 'webpack'`
type Loader = {
_compiler?: {
name: string
context: string
}
resource: string
cacheable: (enabled: boolean) => void
}
export async function loader(this: Loader, input: string): Promise<string> {
const compiler = this._compiler!
@@ -25,8 +18,12 @@ export async function loader(this: Loader, input: string): Promise<string> {
const isSSR = compiler.name === "server"
if (!isSSR) {
const code = await transformBlitzRpcResolverClient(input, toPosixPath(id), toPosixPath(root))
return code
return await transformBlitzRpcResolverClient(
input,
toPosixPath(id),
toPosixPath(root),
this.query,
)
}
return input
@@ -34,13 +31,18 @@ export async function loader(this: Loader, input: string): Promise<string> {
module.exports = loader
export async function transformBlitzRpcResolverClient(_src: string, id: string, root: string) {
export async function transformBlitzRpcResolverClient(
_src: string,
id: string,
root: string,
options?: LoaderOptions,
) {
assertPosixPath(id)
assertPosixPath(root)
const resolverFilePath = "/" + posix.relative(root, id)
assertPosixPath(resolverFilePath)
const routePath = convertPageFilePathToRoutePath(resolverFilePath)
const routePath = convertPageFilePathToRoutePath(resolverFilePath, options?.resolverBasePath)
const resolverName = convertFilePathToResolverName(resolverFilePath)
const resolverType = convertFilePathToResolverType(resolverFilePath)

View File

@@ -1,23 +1,17 @@
import {posix, join, dirname} from "path"
import {dirname, join, posix} from "path"
import {promises} from "fs"
import {
assertPosixPath,
toPosixPath,
buildPageExtensionRegex,
getIsRpcFile,
topLevelFoldersThatMayContainResolvers,
convertPageFilePathToRoutePath,
getIsRpcFile,
Loader,
LoaderOptions,
topLevelFoldersThatMayContainResolvers,
toPosixPath,
} from "./loader-utils"
// Subset of `import type { LoaderDefinitionFunction } from 'webpack'`
type Loader = {
_compiler?: {
name: string
context: string
}
resource: string
cacheable: (enabled: boolean) => void
}
export async function loader(this: Loader, input: string): Promise<string> {
const compiler = this._compiler!
@@ -29,8 +23,13 @@ export async function loader(this: Loader, input: string): Promise<string> {
this.cacheable(false)
const resolvers = await collectResolvers(root, ["ts", "js"])
const code = await transformBlitzRpcServer(input, toPosixPath(id), toPosixPath(root), resolvers)
return code
return await transformBlitzRpcServer(
input,
toPosixPath(id),
toPosixPath(root),
resolvers,
this.query,
)
}
return input
@@ -43,6 +42,7 @@ export async function transformBlitzRpcServer(
id: string,
root: string,
resolvers: string[],
options?: LoaderOptions,
) {
assertPosixPath(id)
assertPosixPath(root)
@@ -55,7 +55,7 @@ export async function transformBlitzRpcServer(
for (let resolverFilePath of resolvers) {
const relativeResolverPath = posix.relative(dirname(id), join(root, resolverFilePath))
const routePath = convertPageFilePathToRoutePath(resolverFilePath)
const routePath = convertPageFilePathToRoutePath(resolverFilePath, options?.resolverBasePath)
code += `__internal_addBlitzRpcResolver('${routePath}', () => import('${relativeResolverPath}'));`
code += "\n"
}

View File

@@ -0,0 +1,24 @@
import {describe, expect, it} from "vitest"
import {convertPageFilePathToRoutePath} from "./loader-utils"
const FILE_PATH = "app/queries/getData.ts"
describe("convertPageFilePathToRoutePath", () => {
it("should return the full path when resolverBasePath is set to root", () => {
const res = convertPageFilePathToRoutePath(FILE_PATH, "root")
expect(res).toEqual("app/queries/getData")
})
it("should return the relative path when resolverBasePath is set to queries|mutations", () => {
const res = convertPageFilePathToRoutePath(FILE_PATH, "queries|mutations")
expect(res).toEqual("/getData")
})
it("should return the relative path when resolverBasePath is set to undefined", () => {
const res = convertPageFilePathToRoutePath(FILE_PATH, undefined)
expect(res).toEqual("/getData")
})
})

View File

@@ -1,5 +1,20 @@
import {assert} from "blitz"
import {win32, posix, sep} from "path"
import {posix, sep, win32} from "path"
import {ResolverBasePath} from "./index-server"
export interface LoaderOptions {
resolverBasePath?: ResolverBasePath
}
export interface Loader {
_compiler?: {
name: string
context: string
}
resource: string
cacheable: (enabled: boolean) => void
query: LoaderOptions
}
export function assertPosixPath(path: string) {
const errMsg = `Wrongly formatted path: ${path}`
@@ -34,7 +49,14 @@ export function buildPageExtensionRegex(pageExtensions: string[]) {
const fileExtensionRegex = /\.([a-z]+)$/
export function convertPageFilePathToRoutePath(filePath: string) {
export function convertPageFilePathToRoutePath(
filePath: string,
resolverBasePath: ResolverBasePath,
) {
if (resolverBasePath === "root") {
return filePath.replace(fileExtensionRegex, "")
}
return filePath
.replace(/^.*?[\\/]queries[\\/]/, "/")
.replace(/^.*?[\\/]mutations[\\/]/, "/")

View File

@@ -1,5 +1,5 @@
import {Ctx} from "blitz"
import {describe, it, expect} from "vitest"
import {describe, expect, it} from "vitest"
import {z} from "zod"
import {ParserType, resolver} from "./resolver"
@@ -44,10 +44,9 @@ const asyncResolver = resolver.pipe(
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},
)
const result1 = await resolver1({email: "test@example.com"}, {
session: {$authorize: () => undefined},
} as Ctx)
expect(result1).toBe("test@example.com")
const resolver2 = resolver.pipe(
@@ -55,9 +54,8 @@ const resolverTest = async ({type}: {type?: ParserType}) => {
return input.email
},
)
const result2 = await resolver2(
{email: "test@example.com"},
{session: {$authorize: () => undefined} as Ctx},
)
const result2 = await resolver2({email: "test@example.com"}, {
session: {$authorize: () => undefined},
} as Ctx)
expect(result2).toBe("test@example.com")
}

View File

@@ -3,10 +3,14 @@
*/
import {assert, expect, test, beforeEach, describe, spyOn, it} from "vitest"
import {queryClient, invalidateQuery, setQueryData} from "../../src/data-client"
import {getQueryClient, invalidateQuery, setQueryData} from "../../src/data-client"
import {getQueryCacheFunctions} from "../../src/data-client/react-query-utils"
import {
getQueryCacheFunctions,
initializeQueryClient,
} from "../../src/data-client/react-query-utils"
import {buildQueryRpc} from "../blitz-test-utils"
globalThis.queryClient = initializeQueryClient()
// eslint-disable-next-line require-await
const isEmpty = async (arg: string): Promise<boolean> => {
@@ -14,7 +18,7 @@ const isEmpty = async (arg: string): Promise<boolean> => {
}
describe("getQueryCacheFunctions", () => {
const spyRefetchQueries = spyOn(queryClient, "invalidateQueries")
const spyRefetchQueries = spyOn(getQueryClient(), "invalidateQueries")
beforeEach(() => {
spyRefetchQueries.mockReset()
@@ -47,7 +51,7 @@ describe("getQueryCacheFunctions", () => {
})
describe("invalidateQuery", () => {
const spyRefetchQueries = spyOn(queryClient, "invalidateQueries")
const spyRefetchQueries = spyOn(getQueryClient(), "invalidateQueries")
beforeEach(() => {
spyRefetchQueries.mockReset()
@@ -56,15 +60,15 @@ describe("invalidateQuery", () => {
it("invalidates a query given resolver and params", async () => {
await invalidateQuery(buildQueryRpc(isEmpty), "a")
expect(spyRefetchQueries).toBeCalledTimes(1)
const calledWith = spyRefetchQueries.mock.calls[0][0] as any
const calledWith = spyRefetchQueries.mock.calls[0]![0] as any
// json of the queryKey is "a"
expect(calledWith[1].json).toEqual("a")
})
})
describe("setQueryData", () => {
const spyRefetchQueries = spyOn(queryClient, "invalidateQueries")
const spySetQueryData = spyOn(queryClient, "setQueryData")
const spyRefetchQueries = spyOn(getQueryClient(), "invalidateQueries")
const spySetQueryData = spyOn(getQueryClient(), "setQueryData")
beforeEach(() => {
spyRefetchQueries.mockReset()
@@ -88,7 +92,7 @@ describe("setQueryData", () => {
expect(spyRefetchQueries).toBeCalledTimes(1)
expect(spySetQueryData).toBeCalledTimes(1)
const invalidateCalledWith = spyRefetchQueries.mock.calls[0][0] as any
const invalidateCalledWith = spyRefetchQueries.mock.calls[0]![0] as any
expect(invalidateCalledWith[1].json).toEqual("params")
const calledWith = spySetQueryData.mock.calls[0] as Array<any>

View File

@@ -1,5 +1,129 @@
# blitz
## 2.0.0-alpha.46
### Patch Changes
- @blitzjs/generator@2.0.0-alpha.46
## 2.0.0-alpha.45
### Patch Changes
- @blitzjs/generator@2.0.0-alpha.45
## 2.0.0-alpha.44
### Patch Changes
- @blitzjs/generator@2.0.0-alpha.44
## 2.0.0-alpha.43
### Patch Changes
- 527e48ac: Fix running bin commands with Blitz CLI
- @blitzjs/generator@2.0.0-alpha.43
## 2.0.0-alpha.42
### Patch Changes
- @blitzjs/generator@2.0.0-alpha.42
## 2.0.0-alpha.41
### Patch Changes
- @blitzjs/generator@2.0.0-alpha.41
## 2.0.0-alpha.40
### Patch Changes
- @blitzjs/generator@2.0.0-alpha.40
## 2.0.0-alpha.39
### Patch Changes
- b918055b: Add aliases for Blitz CLI commands
- @blitzjs/generator@2.0.0-alpha.39
## 2.0.0-alpha.38
### Patch Changes
- Updated dependencies [8aee25c5]
- @blitzjs/generator@2.0.0-alpha.38
## 2.0.0-alpha.37
### Patch Changes
- a80d2a8f: rename middleware type for blitz server plugin
- @blitzjs/generator@2.0.0-alpha.37
## 2.0.0-alpha.36
### Patch Changes
- Updated dependencies [4cad9cca]
- @blitzjs/generator@2.0.0-alpha.36
## 2.0.0-alpha.35
### Patch Changes
- Updated dependencies [e3750b04]
- @blitzjs/generator@2.0.0-alpha.35
## 2.0.0-alpha.34
### Patch Changes
- @blitzjs/generator@2.0.0-alpha.34
## 2.0.0-alpha.33
### Patch Changes
- @blitzjs/generator@2.0.0-alpha.33
## 2.0.0-alpha.32
### Patch Changes
- @blitzjs/generator@2.0.0-alpha.32
## 2.0.0-alpha.31
### Patch Changes
- Updated dependencies [17ce29e5]
- @blitzjs/generator@2.0.0-alpha.31
## 2.0.0-alpha.30
### Patch Changes
- ce453683: init codemod generator
- @blitzjs/generator@2.0.0-alpha.30
## 2.0.0-alpha.29
### Patch Changes
- 962eb58a: detailed print env info
- @blitzjs/generator@2.0.0-alpha.29
## 2.0.0-alpha.28
### Patch Changes
- Updated dependencies [6ac2d341]
- @blitzjs/generator@2.0.0-alpha.28
## 2.0.0-alpha.27
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "blitz",
"version": "2.0.0-alpha.27",
"version": "2.0.0-alpha.46",
"scripts": {
"build": "unbuild",
"dev": "watch unbuild src --wait=0.2",
@@ -23,7 +23,7 @@
"blitz": "bin/blitz"
},
"dependencies": {
"@blitzjs/generator": "2.0.0-alpha.27",
"@blitzjs/generator": "2.0.0-alpha.46",
"arg": "5.0.1",
"chalk": "^4.1.0",
"console-table-printer": "2.10.0",
@@ -32,12 +32,14 @@
"detect-port": "1.3.0",
"dotenv": "16.0.0",
"dotenv-expand": "8.0.3",
"envinfo": "7.8.1",
"esbuild": "0.14.34",
"find-up": "4.1.0",
"fs-extra": "10.0.1",
"hasbin": "1.2.3",
"npm-which": "3.0.1",
"ora": "5.3.0",
"os-name": "5.0.1",
"p-event": "4.2.0",
"pkg-dir": "5.0.0",
"prompts": "2.4.2",
@@ -50,11 +52,12 @@
"watchpack": "2.1.1"
},
"devDependencies": {
"@blitzjs/config": "workspace:2.0.0-alpha.27",
"@blitzjs/config": "workspace:2.0.0-alpha.46",
"@types/cookie": "0.4.1",
"@types/cross-spawn": "6.0.2",
"@types/debug": "4.1.7",
"@types/detect-port": "1.3.2",
"@types/envinfo": "7.8.1",
"@types/express": "4.17.13",
"@types/fs-extra": "9.0.13",
"@types/hasbin": "1.2.0",

View File

@@ -295,7 +295,7 @@ const newApp: CliCommand = async (argv) => {
postInstallSteps.push("blitz dev")
console.log("Your new Blitz app is ready! Next steps:")
console.log("\n Your new Blitz app is ready! Next steps:")
postInstallSteps.forEach((step, index) => {
console.log(chalk.yellow(` ${index + 1}. ${step}`))
})

View File

@@ -1,12 +1,15 @@
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"
import {readdirSync} from "fs-extra"
import resolveFrom from "resolve-from"
import pkgDir from "pkg-dir"
import {join} from "path"
const commonArgs = {
// Types
// Flags
"--version": Boolean,
"--help": Boolean,
"--inspect": Boolean,
@@ -17,10 +20,13 @@ const commonArgs = {
"-h": "--help",
"-e": "--env",
}
const args = arg(commonArgs, {
permissive: true,
})
const defaultCommand = "dev"
export type CliCommand = (argv?: string[]) => void
const commands: {[command: string]: () => Promise<CliCommand>} = {
const commands = {
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),
@@ -30,90 +36,38 @@ const commands: {[command: string]: () => Promise<CliCommand>} = {
db: () => import("./commands/db").then((i) => i.db),
}
const args = arg(commonArgs, {
permissive: true,
})
if (args["--env"]) {
process.env.APP_ENV = args["--env"]
const aliases: Record<string, keyof typeof commands> = {
d: "dev",
b: "build",
s: "start",
n: "new",
g: "generate",
}
loadEnvConfig(process.cwd(), undefined, {error: console.error, info: console.info})
type Command = keyof typeof commands
type Alias = keyof typeof aliases
// Version is inlined into the file using taskr build pipeline
if (args["--version"]) {
console.log(`Blitz.js v${packageJson.version}`)
process.exit(0)
let blitzCommand: Command | undefined
if (commands[args._[0] as Command]) {
blitzCommand = args._[0] as Command
}
if (aliases[args._[0] as Alias]) {
blitzCommand = aliases[args._[0] as Alias]
}
const foundCommand = Boolean(commands[args._[0] as string])
const forwardedArgs = blitzCommand ? args._.slice(1) : args._
const command = foundCommand ? (args._[0] as string) : defaultCommand
const forwardedArgs = foundCommand ? args._.slice(1) : args._
if (args["--env"]) {
process.env.APP_ENV = args["--env"]
}
if (args["--help"]) {
forwardedArgs.push("--help")
}
// env variable should default to dev unless the command is build or start
const defaultEnv = command === "build" || command === "start" ? "production" : "development"
const standardEnv = ["production", "development", "test"]
if (process.env.NODE_ENV && !standardEnv.includes(process.env.NODE_ENV)) {
console.warn(NON_STANDARD_NODE_ENV)
}
;(process.env as any).NODE_ENV = process.env.NODE_ENV || defaultEnv
// Make sure commands gracefully respect termination signals (e.g. from Docker)
process.on("SIGTERM", () => process.exit(0))
process.on("SIGINT", () => process.exit(0))
if (foundCommand) {
commands[command]?.()
.then((exec: any) => exec(forwardedArgs))
.then(() => {
if (command === "build") {
// ensure process exits after build completes so open handles/connections
// don't cause process to hang
process.exit(0)
}
})
.catch((err) => {
console.log(err)
})
} else {
if (args["--help"] && args._.length === 0) {
console.log(`
Usage
$ blitz <command>
Available commands
${Object.keys(commands).join(", ")}
Options
--env, -e App environment name
--version, -v Version number
--help, -h Displays this message
For more information run a command with the --help flag
$ blitz build --help
`)
process.exit(0)
} else {
// If the command is not found, we assume it is a command from the bin
void runCommandFromBin()
}
}
const globalBlitzPath = resolveFrom(__dirname, "blitz")
const localBlitzPath = resolveFrom.silent(process.cwd(), "blitz")
async function runCommandFromBin() {
const command = args._[0] as string
if (!args._[0]) {
console.log("No command specified")
process.exit(1)
}
let commandBin: string | null = null
try {
commandBin = await getCommandBin(command)
commandBin = await getCommandBin(args._[0])
} catch (e: any) {
console.error(`Error: ${e.message}`)
}
@@ -125,3 +79,133 @@ async function runCommandFromBin() {
const result = spawn.sync(commandBin, process.argv.slice(3), {stdio: "inherit"})
process.exit(result.status || 0)
}
async function printEnvInfo() {
const osName = await import("os-name")
const envinfo = await import("envinfo")
const pkgManager = readdirSync(process.cwd()).includes("pnpm-lock.yaml")
? "pnpm"
: readdirSync(process.cwd()).includes("yarn-lock.yaml")
? "yarn"
: "npm"
const env = await envinfo.default.run(
{
System: ["OS", "CPU", "Memory", "Shell"],
Binaries: ["Node", "Yarn", "npm", "pnpm"],
npmPackages: [
"blitz",
"typescript",
"react",
"react-dom",
"prisma",
"@prisma/client",
"next",
],
},
{showNotFound: true},
)
const globalBlitzPkgJsonPath = pkgDir.sync(globalBlitzPath)
const localBlitzPkgJsonPath = pkgDir.sync(localBlitzPath)
if (globalBlitzPkgJsonPath && globalBlitzPkgJsonPath !== localBlitzPkgJsonPath) {
// This branch won't run if user does `npx blitz` or `yarn blitz`
const globalVersion = require(join(globalBlitzPkgJsonPath, "package.json")).version
console.log(`Blitz version: ${globalVersion} (global)`)
}
if (localBlitzPkgJsonPath) {
const localVersion = require(join(localBlitzPkgJsonPath, "package.json")).version
console.log(`Blitz version: ${localVersion} (local)`)
}
console.log(
`${osName.default()} | ${process.platform}-${process.arch} | Node: ${process.version}\n`,
)
console.log(`\n Package manager: ${pkgManager}`)
console.log(env)
process.exit(0)
}
async function main() {
loadEnvConfig(process.cwd(), undefined, {error: console.error, info: console.info})
// Version is inlined into the file using taskr build pipeline
if (args["_"].length === 0 && args["--version"]) {
await printEnvInfo()
}
if (args["--env"]) {
process.env.APP_ENV = args["--env"]
}
if (args["--help"]) {
forwardedArgs.push("--help")
}
// env variable should default to dev unless the command is build or start
const defaultEnv =
blitzCommand === "build" || blitzCommand === "start" ? "production" : "development"
const standardEnv = ["production", "development", "test"]
if (process.env.NODE_ENV && !standardEnv.includes(process.env.NODE_ENV)) {
console.warn(NON_STANDARD_NODE_ENV)
}
process.env.NODE_ENV = process.env.NODE_ENV || defaultEnv
// Make sure commands gracefully respect termination signals (e.g. from Docker)
process.on("SIGTERM", () => process.exit(0))
process.on("SIGINT", () => process.exit(0))
if (blitzCommand) {
const commandFn = commands[blitzCommand]
commandFn?.()
.then((exec: any) => exec(forwardedArgs))
.then(() => {
if (blitzCommand === "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) {
// TODO: add back the generate command description once it's working
// generate, g Generate new files for your Blitz project 🤠
console.log(`
Usage
$ blitz <command>
Available commands
dev, d Start a development server 🪄
build, b Create a production build 🏗️
start, s Start the production server 🐎
new, n Create a new Blitz project ✨
codegen Run the blitz codegen 🤖
db Run database commands 🗄️
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()
}
}
}
main().catch((e) => {
console.error(e)
})

View File

@@ -9,7 +9,7 @@ export * from "./types"
export * from "./prisma-utils"
export * from "./middleware"
export * from "./paginate"
export {baseLogger, newLine} from "./logging"
export {baseLogger, newLine, log} from "./logging"
export {startWatcher, stopWatcher} from "./cli/utils/routes-manifest"
// const blitzConfig = findBlitzConfigDirectory() as string
// const file = readFileSync(blitzConfig)
@@ -22,7 +22,7 @@ export interface MiddlewareResponse<C extends Ctx = Ctx> extends ServerResponse
export type MiddlewareNext = (error?: Error) => Promise<void> | void
export type Middleware<
export type RequestMiddleware<
TRequest extends IncomingMessage = IncomingMessage,
TResponse = ServerResponse,
> = {
@@ -32,11 +32,11 @@ export type Middleware<
}
export type BlitzServerPlugin<
MiddlewareType = Middleware<any, any>,
RequestMiddlewareType = RequestMiddleware<any, any>,
TCtx extends Ctx = Ctx,
TExports extends object = {},
> = {
middlewares: MiddlewareType[]
requestMiddlewares: RequestMiddlewareType[]
contextMiddleware?: (ctx: TCtx) => TCtx
exports?: TExports
}
@@ -54,7 +54,7 @@ export function createServerPlugin<
return pluginConstructor
}
export function createSetupServer<TMiddleware extends Middleware, TExports extends object>(
export function createSetupServer<TMiddleware extends RequestMiddleware, TExports extends object>(
setupServerConstructor: (plugins: BlitzServerPlugin<TMiddleware>) => TExports,
) {
return setupServerConstructor

View File

@@ -149,6 +149,15 @@ const success = (msg: string) => {
console.log(withCheck(c.green(msg)))
}
/**
* Logs a red error message to stdout.
*
* @param {string} msg
*/
const error = (msg: string) => {
console.log(`${c.red(msg)}`)
}
/**
* Colorizes a variable for display.
*
@@ -173,6 +182,7 @@ export const log = {
progress,
spinner,
success,
error,
variable,
debug,
Table,

View File

@@ -1,5 +1,5 @@
import {IncomingMessage, ServerResponse} from "http"
import {compose, Ctx, Middleware, MiddlewareNext, MiddlewareResponse} from "./index-server"
import {compose, Ctx, RequestMiddleware, MiddlewareNext, MiddlewareResponse} from "./index-server"
export async function handleRequestWithMiddleware<
Req extends IncomingMessage = IncomingMessage,
@@ -7,7 +7,7 @@ export async function handleRequestWithMiddleware<
>(
req: Req,
res: Res,
middleware: Middleware<Req, Res>[],
middleware: RequestMiddleware<Req, Res>[],
{
throwOnError = true,
stackPrintOnError = true,
@@ -117,7 +117,7 @@ export async function handleRequestWithMiddleware<
export function noCallbackHandler<
Req extends IncomingMessage = IncomingMessage,
Res = MiddlewareResponse,
>(req: Req, res: Res, next: MiddlewareNext, middleware: Middleware<Req, Res>) {
>(req: Req, res: Res, next: MiddlewareNext, middleware: RequestMiddleware<Req, Res>) {
// Cast to any to call with two arguments for connect compatibility
;(middleware as any)(req, res)
return next()
@@ -131,7 +131,7 @@ export function noCallbackHandler<
export function withCallbackHandler<
Req extends IncomingMessage = IncomingMessage,
Res = MiddlewareResponse,
>(req: Req, res: Res, next: MiddlewareNext, middleware: Middleware<Req, Res>) {
>(req: Req, res: Res, next: MiddlewareNext, middleware: RequestMiddleware<Req, Res>) {
return new Promise((resolve, reject) => {
// Rule doesn't matter since we are inside new Promise()
//eslint-disable-next-line @typescript-eslint/no-floating-promises
@@ -150,14 +150,14 @@ export function withCallbackHandler<
export function connectMiddleware<
Req extends IncomingMessage = IncomingMessage,
Res extends MiddlewareResponse = MiddlewareResponse,
>(middleware: Middleware<Req, Res>): Middleware<Req, Res> {
>(middleware: RequestMiddleware<Req, Res>): RequestMiddleware<Req, Res> {
const handler = middleware.length < 3 ? noCallbackHandler : withCallbackHandler
return function connectHandler(req: Req, res, next) {
return handler(req, res, next, middleware)
} as Middleware<Req, Res>
} as RequestMiddleware<Req, Res>
}
export const secureProxyMiddleware: Middleware<
export const secureProxyMiddleware: RequestMiddleware<
IncomingMessage & {protocol?: string},
MiddlewareResponse
> = function (

View File

@@ -1,4 +1,4 @@
import {Middleware} from "./index-server"
import {RequestMiddleware} from "./index-server"
import * as path from "path"
import * as fs from "fs"
@@ -76,7 +76,7 @@ export const setCookie = (name: string, value: string, expires: string) => {
}
export const deleteCookie = (name: string) => setCookie(name, "", "Thu, 01 Jan 1970 00:00:01 GMT")
export function compose(middleware: Middleware<any, any>[]) {
export function compose(middleware: RequestMiddleware<any, any>[]) {
if (!Array.isArray(middleware)) {
throw new TypeError("Middleware stack must be an array!")
}
@@ -115,7 +115,7 @@ export function compose(middleware: Middleware<any, any>[]) {
// return next(result as any)
return dispatch(0).then(next as any)
} as Middleware
} as RequestMiddleware
}
function round(num: number, decimalPlaces: number) {

View File

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

View File

@@ -0,0 +1,138 @@
# @blitzjs/codemod
## 2.0.0-alpha.46
### Patch Changes
- aafdc5b4: Move middlewares from blitz config to blitz server with codemod
- @blitzjs/generator@2.0.0-alpha.46
- blitz@2.0.0-alpha.46
## 2.0.0-alpha.45
### Patch Changes
- @blitzjs/generator@2.0.0-alpha.45
- blitz@2.0.0-alpha.45
## 2.0.0-alpha.44
### Patch Changes
- @blitzjs/generator@2.0.0-alpha.44
- blitz@2.0.0-alpha.44
## 2.0.0-alpha.43
### Patch Changes
- Updated dependencies [527e48ac]
- blitz@2.0.0-alpha.43
- @blitzjs/generator@2.0.0-alpha.43
## 2.0.0-alpha.42
### Patch Changes
- @blitzjs/generator@2.0.0-alpha.42
- blitz@2.0.0-alpha.42
## 2.0.0-alpha.41
### Patch Changes
- bbac7906: fix codemod for wrapping \_app arrow function & fix codemod for nested pages directory
- @blitzjs/generator@2.0.0-alpha.41
- blitz@2.0.0-alpha.41
## 2.0.0-alpha.40
### Patch Changes
- @blitzjs/generator@2.0.0-alpha.40
- blitz@2.0.0-alpha.40
## 2.0.0-alpha.39
### Patch Changes
- Updated dependencies [b918055b]
- blitz@2.0.0-alpha.39
- @blitzjs/generator@2.0.0-alpha.39
## 2.0.0-alpha.38
### Patch Changes
- 8aee25c5: getQueryClient function & queryClient codemod updates & shared plugin config
- Updated dependencies [8aee25c5]
- @blitzjs/generator@2.0.0-alpha.38
- blitz@2.0.0-alpha.38
## 2.0.0-alpha.37
### Patch Changes
- Updated dependencies [a80d2a8f]
- blitz@2.0.0-alpha.37
- @blitzjs/generator@2.0.0-alpha.37
## 2.0.0-alpha.36
### Patch Changes
- 4cad9cca: Update queryClient import in codemod
- Updated dependencies [4cad9cca]
- @blitzjs/generator@2.0.0-alpha.36
- blitz@2.0.0-alpha.36
## 2.0.0-alpha.35
### Patch Changes
- e3750b04: codemod fixes
- Updated dependencies [e3750b04]
- @blitzjs/generator@2.0.0-alpha.35
- blitz@2.0.0-alpha.35
## 2.0.0-alpha.34
### Patch Changes
- @blitzjs/generator@2.0.0-alpha.34
- blitz@2.0.0-alpha.34
## 2.0.0-alpha.33
### Patch Changes
- 9e05d6e1: allow extension catch in getAllFiles codemod util
- @blitzjs/generator@2.0.0-alpha.33
- blitz@2.0.0-alpha.33
## 2.0.0-alpha.32
### Patch Changes
- e6fb09d4: Fix templates source in RPC codemod step
- @blitzjs/generator@2.0.0-alpha.32
- blitz@2.0.0-alpha.32
## 2.0.0-alpha.31
### Patch Changes
- 46d9f81a: Update templates directory for codemod
- Updated dependencies [17ce29e5]
- @blitzjs/generator@2.0.0-alpha.31
- blitz@2.0.0-alpha.31
## 2.0.0-alpha.30
### Patch Changes
- ce453683: Add codemod to upgrade from legacy framework to the Blitz Toolkit
- ce453683: init codemod generator
- Updated dependencies [ce453683]
- blitz@2.0.0-alpha.30
- @blitzjs/generator@2.0.0-alpha.30

View File

@@ -0,0 +1,2 @@
#!/usr/bin/env node
require("@blitzjs/codemod/dist/index.cjs")

View File

@@ -0,0 +1,11 @@
import {BuildConfig} from "unbuild"
const config: BuildConfig = {
entries: ["./src/index"],
externals: ["index.cjs", "blitz"],
declaration: true,
rollup: {
emitCJS: true,
},
}
export default config

View File

@@ -0,0 +1,51 @@
{
"name": "@blitzjs/codemod",
"version": "2.0.0-alpha.46",
"scripts": {
"build": "unbuild",
"dev": "watch unbuild src --wait=0.2",
"lint": "eslint . --fix",
"test": "vitest run",
"test-watch": "vitest",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
},
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"browser": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"sideEffects": false,
"license": "MIT",
"files": [
"dist/**",
"bin/**"
],
"bin": "bin/@blitzjs/codemod",
"dependencies": {
"@babel/core": "7.12.10",
"@babel/plugin-proposal-class-properties": "7.17.12",
"@babel/plugin-syntax-jsx": "7.17.12",
"@babel/plugin-syntax-typescript": "7.17.12",
"@blitzjs/generator": "2.0.0-alpha.46",
"arg": "5.0.1",
"blitz": "2.0.0-alpha.46",
"chalk": "^4.1.0",
"cross-spawn": "7.0.3",
"debug": "4.3.3",
"esbuild": "0.14.34",
"fs-extra": "10.0.1",
"jscodeshift": "0.13.0",
"prompts": "2.4.2"
},
"devDependencies": {
"@babel/preset-env": "7.12.10",
"@blitzjs/config": "workspace:*",
"@types/jscodeshift": "0.11.2",
"@types/node": "17.0.16",
"ast-types": "0.14.2",
"unbuild": "0.6.9",
"watch": "1.0.2"
},
"publishConfig": {
"access": "public"
}
}

View File

@@ -0,0 +1,9 @@
import {expect, describe, it} from "vitest"
import spawn from "cross-spawn"
describe("codemod cli", () => {
it("errors without codemod name", async () => {
const run = spawn.sync("node", ["dist/index.cjs"], {encoding: "utf8"})
expect(run.stdout).toContain("Codemod not found")
})
})

View File

@@ -0,0 +1,31 @@
import arg from "arg"
const commonArgs = {
// Types
"--help": Boolean,
// Aliases
"-h": "--help",
}
const args = arg(commonArgs, {
permissive: true,
})
const commands: {[command: string]: () => Promise<() => void>} = {
"upgrade-legacy": () => import("./upgrade-legacy").then((i) => i.upgradeLegacy),
}
const foundCommand = Boolean(commands[args._[0] as string])
const forwardedArgs = foundCommand ? args._.slice(1) : args._
if (foundCommand) {
commands[args["_"][0] as string]?.()
.then((exec: any) => exec(forwardedArgs))
.catch((err) => {
console.log(err)
})
} else {
console.log("Codemod not found. Try one of these:")
console.log(`${Object.keys(commands).map((c) => c)}`)
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,243 @@
import path from "path"
import * as fs from "fs-extra"
import j, {
ASTPath,
Collection,
FunctionDeclaration,
VariableDeclaration,
ExportDefaultDeclaration,
ImportDeclaration,
CallExpression,
ImportSpecifier,
Identifier,
} from "jscodeshift"
import {parseSync} from "@babel/core"
export function findIdentifier(program: Collection<any>, name: string): Collection<Identifier> {
return program.find(j.Identifier, (node) => node.name === name)
}
export function findFunction(
program: Collection<any>,
declarationName: string,
): Collection<FunctionDeclaration> | Collection<VariableDeclaration> | null {
const funcDeclaration = program.find(
j.FunctionDeclaration,
(node) => node.id.name === declarationName,
)
const constDeclaration = program.find(
j.VariableDeclaration,
(node) => node.declarations[0].id.name === declarationName,
)
const paths = funcDeclaration.length
? funcDeclaration
: constDeclaration.length
? constDeclaration
: null
return paths
}
export function findVariable(
program: Collection<any>,
declarationName: string,
): Collection<VariableDeclaration> | null {
const constDeclaration = program.find(
j.VariableDeclaration,
(node) => node.declarations[0].id.name === declarationName,
)
const paths = constDeclaration.length ? constDeclaration : null
return paths
}
export function findImport(
program: Collection<any>,
declarationName: string,
): Collection<ImportDeclaration> | null {
const importDeclaration = program.find(
j.ImportDeclaration,
(node) => node.source.value === declarationName,
)
const paths = importDeclaration.length ? importDeclaration : null
return paths
}
export function findImportSpecifier(
program: Collection<any>,
declarationName: string,
): Collection<ImportSpecifier> | null {
const importSpecifier = program.find(
j.ImportSpecifier,
(node) => node.imported.name === declarationName,
)
const paths = importSpecifier.length ? importSpecifier : null
return paths
}
export function findCallExpression(
program: Collection<any>,
declarationName: string,
): Collection<CallExpression> | null {
const callExpression = program.find(
j.CallExpression,
(node) => node.callee.name === declarationName,
)
const paths = callExpression.length ? callExpression : null
return paths
}
export function findDefaultExportPath(
program: Collection<any>,
): ASTPath<ExportDefaultDeclaration> | null | undefined {
const defaultDeclaration = program.find(j.ExportDefaultDeclaration)
return defaultDeclaration.length ? defaultDeclaration.paths()[0] : null
}
export function wrapDeclaration(
path: ASTPath<FunctionDeclaration> | ASTPath<VariableDeclaration>,
declaration: string,
wrapper: string,
) {
if (path.node.type === "FunctionDeclaration") {
// CASE 1
// function myFunction()
// TURNS INTO
// const myFunction = wrapper(function myFunction () {})
path.replace(
j.variableDeclaration("const", [
j.variableDeclarator(
j.identifier(declaration),
j.callExpression(j.identifier(wrapper), [
j.functionExpression.from({
id: path.node.id,
async: path.node.async,
body: path.node.body,
params: path.node.params,
typeParameters: path.node.typeParameters,
defaults: path.node.defaults,
expression: path.node.expression,
generator: path.node.generator,
returnType: path.node.returnType,
rest: path.node.rest,
comments: path.node.comments ?? null,
}),
]),
),
]),
)
} else if (path.node.type === "VariableDeclaration") {
// CASE 2
// const myFunction = () => {}
// TURNS INTO
// const myFunction = gSSP(() => {})
for (const declaration of path.node.declarations) {
if (declaration.type === "VariableDeclarator" && declaration.init) {
const init = declaration.init
// Running the codemod twice should not wrap in `gSSP` twice.
if (
init.type === "CallExpression" &&
init.callee.type === "Identifier" &&
init.callee.name === wrapper
) {
// For 100% confidence, we could probably check if gSSP is imported from Blitz here
// but this codemod adds the gSSP import.
continue
}
declaration.init = j.callExpression(j.identifier(wrapper), [init])
break
}
}
}
}
export function addNamedImport(
program: Collection<any>,
importStatement: string,
importFrom: string,
defaultSpecifier?: boolean,
) {
const existingImport = program.find(
j.ImportDeclaration,
(node) => node.source.value === importFrom,
)
if (existingImport.length) {
existingImport.get().value.specifiers.push(j.importSpecifier(j.identifier(importStatement)))
} else {
program
.get()
.value.program.body.unshift(
defaultSpecifier
? j.importDeclaration(
[j.importDefaultSpecifier(j.identifier(importStatement))],
j.stringLiteral(importFrom),
)
: j.importDeclaration(
[j.importSpecifier(j.identifier(importStatement))],
j.stringLiteral(importFrom),
),
)
}
}
export function getAllFiles(
dirPath: string,
accFiles: string[] = [],
skipDirs?: string[],
allowedExt?: string[],
) {
let currentFiles = fs.readdirSync(dirPath)
currentFiles.forEach((file) => {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
if (!skipDirs?.includes(file)) {
accFiles = getAllFiles(dirPath + "/" + file, accFiles, skipDirs, allowedExt)
}
} else {
if (allowedExt?.length) {
if (allowedExt?.includes(path.extname(file))) {
accFiles.push(path.join(dirPath, "/", file))
}
} else {
accFiles.push(path.join(dirPath, "/", file))
}
}
})
return accFiles
}
export function getCollectionFromSource(filename: string) {
const fileSource = fs.readFileSync(path.resolve(filename), {encoding: "utf-8"})
return j(fileSource, {
parser: {
parse: (source: string) =>
parseSync(source, {
configFile: false,
plugins: [require(`@babel/plugin-syntax-jsx`)],
overrides: [
{
test: [`**/*.ts`, `**/*.tsx`],
plugins: [[require(`@babel/plugin-syntax-typescript`), {isTSX: true}]],
},
],
filename,
parserOpts: {
tokens: true, // recast uses this
},
}),
},
})
}

View File

@@ -0,0 +1,10 @@
{
"extends": "@blitzjs/config/tsconfig.library.json",
"compilerOptions": {
"lib": ["DOM", "ES2015"],
"esModuleInterop": true,
"resolveJsonModule": true
},
"include": ["."],
"exclude": ["dist", "build", "node_modules"]
}

View File

@@ -1,5 +1,43 @@
# @blitzjs/config
## 2.0.0-alpha.46
## 2.0.0-alpha.45
## 2.0.0-alpha.44
## 2.0.0-alpha.43
## 2.0.0-alpha.42
## 2.0.0-alpha.41
## 2.0.0-alpha.40
## 2.0.0-alpha.39
## 2.0.0-alpha.38
## 2.0.0-alpha.37
## 2.0.0-alpha.36
## 2.0.0-alpha.35
## 2.0.0-alpha.34
## 2.0.0-alpha.33
## 2.0.0-alpha.32
## 2.0.0-alpha.31
## 2.0.0-alpha.30
## 2.0.0-alpha.29
## 2.0.0-alpha.28
## 2.0.0-alpha.27
## 2.0.0-alpha.26

View File

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

View File

@@ -1,5 +1,63 @@
# @blitzjs/generator
## 2.0.0-alpha.46
## 2.0.0-alpha.45
## 2.0.0-alpha.44
## 2.0.0-alpha.43
## 2.0.0-alpha.42
## 2.0.0-alpha.41
## 2.0.0-alpha.40
## 2.0.0-alpha.39
## 2.0.0-alpha.38
### Patch Changes
- 8aee25c5: getQueryClient function & queryClient codemod updates & shared plugin config
## 2.0.0-alpha.37
## 2.0.0-alpha.36
### Patch Changes
- 4cad9cca: Update codemod and template with a new queryClient import location
## 2.0.0-alpha.35
### Patch Changes
- e3750b04: codemod fixes
## 2.0.0-alpha.34
## 2.0.0-alpha.33
## 2.0.0-alpha.32
## 2.0.0-alpha.31
### Patch Changes
- 17ce29e5: Update RPC plugin setup in templates
## 2.0.0-alpha.30
## 2.0.0-alpha.29
## 2.0.0-alpha.28
### Patch Changes
- 6ac2d341: updated nextjs version in generator & npmrc file
## 2.0.0-alpha.27
## 2.0.0-alpha.26

View File

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

View File

@@ -1,5 +1,5 @@
import { NotFoundError, AuthenticationError } from "blitz"
import { db } from "db"
import db from "db"
import { authenticateUser } from "./login"
import { ChangePassword } from "../validations"
import { resolver } from "@blitzjs/rpc"

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