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

Compare commits

...

5 Commits

Author SHA1 Message Date
Siddharth Suresh
b08c0ff49a use AST to parse the config and allow blitz server to be in src 2022-10-31 20:46:44 +05:30
Siddharth Suresh
ceb7db274f Add GET support to RPC specification (#3891) 2022-10-29 17:10:27 -04:00
github-actions[bot]
0ebdf3bc93 Version Packages (beta) (#3929)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-10-28 15:27:06 +08:00
Aleksandra
1b798d9a03 Export enhancePrisma from the server entry point (#3928)
* Export enhancePrisma from the server entry point

* Changeset

* Update pnpm lock
2022-10-27 10:04:31 -04:00
Blitz.js Bot
758ccbbbe5 (meta) added @joneskj55 as contributor 2022-10-25 06:38:44 -04:00
60 changed files with 688 additions and 499 deletions

View File

@@ -3733,6 +3733,15 @@
"doc",
"code"
]
},
{
"login": "joneskj55",
"name": "Kevin Jones",
"avatar_url": "https://avatars.githubusercontent.com/u/20748598?v=4",
"profile": "https://kevinjones.engineer",
"contributions": [
"doc"
]
}
],
"contributorsPerLine": 7,

View File

@@ -0,0 +1,7 @@
---
"blitz": patch
"@blitzjs/rpc": patch
---
Add an opt-in GET request support to RPC specification by exporting a `config` object that has the `httpMethod` property.
from `query` files.

View File

@@ -195,6 +195,7 @@
"shy-olives-hang",
"shy-pumpkins-try",
"silent-colts-reply",
"silent-lies-run",
"silly-apricots-share",
"silly-shoes-agree",
"six-apricots-kick",

View File

@@ -0,0 +1,5 @@
---
"blitz": patch
---
Fix `useSession` hook by exporting `enhancePrisma` from the server entry point instead of server

View File

@@ -6,7 +6,7 @@
<img alt="" src="https://img.shields.io/badge/Join%20our%20community-6700EB.svg?style=for-the-badge&labelColor=000000&logoWidth=20&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAQ9SURBVHgB7d3dVdtAEIbhcSpICUoH0IEogQqSVBBSAU4FSSpIOoAORAfQgSghHXzZ1U/YcMD4R9rZmf2ec3y448LyiNf27iLiGIAmPLrweC9Un3DhrzG6EarLNP09nlwJ1SOZ/lQr5N80/S/p2QMVCBf5N17XCfm1Y/rBHqjAG9PPHvBsz+mf9WAP+HLA9M/YA14cOP2payH7jpj+VCtk1wnTP+vj7xCy6cTpn7EHLMLp059iD1iD8eveJbVCNsSLheX1YA/YgOWnf8YeKB3Wmf7Ud6Fy4f/FHmtpxbl3YlC4MJ/Cj0bWdwPnPbARg+L0S54XQHS32WwuxClzd4CM0z9rPfeAuTtA5ulPXYQ7wZ04Y+oOoDD9KZc9YOoOoDj9s4dwFzgXR6w1wIPoOvPWA9buAHEJ173o3gWiy3AnuBUHLEbgmYwvAk1/wuM8vAgexThzbwPDkx7/DHwVXfFOxP2GmsKd4Ab6zPeAyU8CI7AHFmH2BRCBPXAyk18GzUrqAXCTiR4ssyj0VFw/oCU8+e+RZ33AWz6KMaYbIIWxB+JSLs1bsbkeMN0AqakHvoku9oA2sAfqBvbAQdw0QArsgb25aYBUQT3QgT2gB+yBuqGcHij2UCqXDZACe2Anlw2QYg/QAOyBuoE98CL3DZDCuK4/rh/Q7oGL6U+TOvcNkJoijN8X1C48+T+g75eQDrAH/qmqAVJgDwyqaoAUe4AGYA/UDZX3QLUNkEIZPRCd5+6BahsgVUgPROwBTSijB7jpVAvGHriHvmw9wAZ4BpX1ABvgmakHtPcbRuwBTWAPULgAV9D/jKDY9YRvwvgEaurD44uQHvAol7qBW7WKluVtIHiUS7GyvA0s6CiXDnxrpQfsgbqBS7GKk/2jYHCrVlGyfxTMrVo0ALdq1Q3sgSKofh0M9oA61a+D2QM0AHugbmAPqClmSRjK2apVVQ8UsySsoK1aHdgDesCtWnUDeyCrIpeFg1u3sylyWTi3btMA7IG6gT2wuuK3hoE9sKrit4YVslWLPaAN7IG6ocKt2zmY2h4O9sDiTG0PZw/QANy6XTewBxZj9ogYVHy025LMHhEz9cBn0We6B0yfERReBLfhx0/R1YQHPx/QBPbA0VwcEwf2wNFcHBPHHjiem3MC2QPHcXdSaJjA+KfgTPQ8hhfjBzHC40mhlzJ+Xq9lK4a4PCs43AVaGTed5mZq+iOXZwWHi3AnOj2wFWNcnxYe7gTxLtBKHuamP/J+Wnh8a5irB7ZC5Yk9gPX1QuXC+usHWqGyhYvUYR0a7zboUOFCNVhnk0krZAOW7wFOvzXhom2xnEbIHizTA1wEYhWW6YFGyC6c1gOcfg9wfA80Qj7g8B7g9HuCww+haIR8wf49wOn3Cvv9k8tGyC/s7gFOv3fY3QONkH+v9MBWqB7PeqDn9FcIT//kcitUn6kHOu/T/xfWzlQy3dEHhwAAAABJRU5ErkJggg==">
</a>
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
<a aria-label="All Contributors" href="#contributors-"><img alt="" src="https://img.shields.io/badge/all_contributors-395-17BB8A.svg?style=for-the-badge&labelColor=000000"></a>
<a aria-label="All Contributors" href="#contributors-"><img alt="" src="https://img.shields.io/badge/all_contributors-396-17BB8A.svg?style=for-the-badge&labelColor=000000"></a>
<!-- ALL-CONTRIBUTORS-BADGE:END -->
<a aria-label="License" href="https://github.com/blitz-js/blitz/blob/main/LICENSE">
<img alt="" src="https://img.shields.io/npm/l/blitz.svg?style=for-the-badge&labelColor=000000&color=blue">
@@ -730,6 +730,7 @@ Thanks to these wonderful people ([emoji key](https://allcontributors.org/docs/e
<td align="center"><a href="https://github.com/corydeppen"><img src="https://avatars.githubusercontent.com/u/313264?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Cory Deppen</b></sub></a><br /><a href="https://github.com/blitz-js/blitz/commits?author=corydeppen" title="Documentation">📖</a></td>
<td align="center"><a href="https://github.com/oloost"><img src="https://avatars.githubusercontent.com/u/72395941?v=4?s=100" width="100px;" alt=""/><br /><sub><b>oloost</b></sub></a><br /><a href="https://github.com/blitz-js/blitz/commits?author=oloost" title="Documentation">📖</a></td>
<td align="center"><a href="https://github.com/janvennemann"><img src="https://avatars.githubusercontent.com/u/1406024?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jan Vennemann</b></sub></a><br /><a href="https://github.com/blitz-js/blitz/commits?author=janvennemann" title="Documentation">📖</a> <a href="https://github.com/blitz-js/blitz/commits?author=janvennemann" title="Code">💻</a></td>
<td align="center"><a href="https://kevinjones.engineer"><img src="https://avatars.githubusercontent.com/u/20748598?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Kevin Jones</b></sub></a><br /><a href="https://github.com/blitz-js/blitz/commits?author=joneskj55" title="Documentation">📖</a></td>
</tr>
</table>

View File

@@ -29,7 +29,7 @@
"@blitzjs/rpc": "workspace:*",
"@hookform/resolvers": "2.8.8",
"@prisma/client": "4.0.0",
"blitz": "workspace:2.0.0-beta.14",
"blitz": "workspace:2.0.0-beta.15",
"next": "12.2.5",
"openid-client": "5.1.8",
"prisma": "4.0.0",

View File

@@ -11,3 +11,7 @@ export default async function getCurrentUser(_ = null, { session }: Ctx) {
return user
}
export const config = {
httpMethod: "GET",
}

View File

@@ -29,7 +29,7 @@
"@blitzjs/rpc": "workspace:*",
"@hookform/resolvers": "2.8.8",
"@prisma/client": "4.0.0",
"blitz": "workspace:2.0.0-beta.14",
"blitz": "workspace:2.0.0-beta.15",
"next": "12.2.5",
"prisma": "4.0.0",
"react": "18.2.0",

View File

@@ -21,7 +21,7 @@
"@blitzjs/config": "workspace:*",
"@blitzjs/next": "workspace:*",
"@prisma/client": "4.0.0",
"blitz": "workspace:2.0.0-beta.14",
"blitz": "workspace:2.0.0-beta.15",
"lowdb": "3.0.0",
"next": "12.2.5",
"prisma": "4.0.0",

View File

@@ -0,0 +1,16 @@
if (typeof window !== "undefined") {
throw new Error("This should not be loaded on the client")
}
export default async function getBasicWithGET() {
if (typeof window !== "undefined") {
throw new Error("This should not be loaded on the client")
}
global.basic ??= "basic-result"
return global.basic
}
export const config = {
httpMethod: "GET",
}

View File

@@ -9,13 +9,10 @@ import {
nextBuild,
nextStart,
nextExport,
getPageFileFromBuildManifest,
getPageFileFromPagesManifest,
} from "../../utils/next-test-utils"
// jest.setTimeout(1000 * 60 * 2)
const appDir = join(__dirname, "../")
const nextConfig = join(appDir, "next.config.js")
let appPort
let mode
let app
@@ -33,17 +30,6 @@ function runTests(dev = false) {
5000 * 60 * 2,
)
it(
"returns 404 for GET",
async () => {
const res = await fetchViaHTTP(appPort, "/api/rpc/getBasic", null, {
method: "GET",
})
expect(res.status).toEqual(404)
},
5000 * 60 * 2,
)
it(
"requires params",
async () => {
@@ -58,6 +44,71 @@ function runTests(dev = false) {
5000 * 60 * 2,
)
it(
"GET - returns 200 only when enabled",
async () => {
const res = await fetchViaHTTP(
appPort,
"/api/rpc/getBasicWithGET?params=%7B%7D&meta=%7B%7D",
null,
{
method: "GET",
},
)
expect(res.status).toEqual(200)
},
5000 * 60 * 2,
)
it(
"GET - returns 404 otherwise",
async () => {
const res = await fetchViaHTTP(
appPort,
"/api/rpc/getBasic?params=%7B%7D&meta=%7B%7D",
null,
{
method: "GET",
},
)
expect(res.status).toEqual(404)
},
5000 * 60 * 2,
)
it(
"query works - GET",
async () => {
const res = await fetchViaHTTP(
appPort,
"/api/rpc/getBasicWithGET?params=%7B%7D&meta=%7B%7D",
null,
{
method: "GET",
},
)
const json = await res.json()
expect(json).toEqual({result: "basic-result", error: null, meta: {}})
expect(res.status).toEqual(200)
},
5000 * 60 * 2,
)
it(
"requires params - GET",
async () => {
const res = await fetchViaHTTP(appPort, "/api/rpc/getBasicWithGET", null, {
method: "GET",
})
const json = await res.json()
expect(res.status).toEqual(400)
expect(json.error.message).toBe(
"Request query is missing the required `params` and `meta` keys",
)
},
5000 * 60 * 2,
)
it(
"query works",
async () => {

View File

@@ -1,5 +1,12 @@
# @blitzjs/auth
## 2.0.0-beta.15
### Patch Changes
- Updated dependencies [1b798d9a]
- blitz@2.0.0-beta.15
## 2.0.0-beta.14
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/auth",
"version": "2.0.0-beta.14",
"version": "2.0.0-beta.15",
"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-beta.14",
"blitz": "2.0.0-beta.15",
"cookie": "0.4.1",
"cookie-session": "2.0.0",
"debug": "4.3.3",
@@ -40,7 +40,7 @@
"url": "0.11.0"
},
"devDependencies": {
"@blitzjs/config": "workspace:2.0.0-beta.14",
"@blitzjs/config": "workspace:2.0.0-beta.15",
"@testing-library/react": "13.0.0",
"@testing-library/react-hooks": "7.0.2",
"@types/cookie": "0.4.1",

View File

@@ -1,5 +1,11 @@
# @blitzjs/next
## 2.0.0-beta.15
### Patch Changes
- @blitzjs/rpc@2.0.0-beta.15
## 2.0.0-beta.14
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/next",
"version": "2.0.0-beta.14",
"version": "2.0.0-beta.15",
"scripts": {
"build": "unbuild",
"dev": "pnpm predev && pnpm watch unbuild src --wait=0.2",
@@ -24,7 +24,7 @@
"eslint.js"
],
"dependencies": {
"@blitzjs/rpc": "2.0.0-beta.14",
"@blitzjs/rpc": "2.0.0-beta.15",
"@tanstack/react-query": "4.0.10",
"@types/hoist-non-react-statics": "3.3.1",
"debug": "4.3.3",
@@ -34,7 +34,7 @@
"supports-color": "8.1.1"
},
"devDependencies": {
"@blitzjs/config": "workspace:2.0.0-beta.14",
"@blitzjs/config": "workspace:2.0.0-beta.15",
"@testing-library/dom": "8.13.0",
"@testing-library/jest-dom": "5.16.3",
"@testing-library/react": "13.0.0",
@@ -44,7 +44,7 @@
"@types/react": "18.0.17",
"@types/react-dom": "17.0.14",
"@types/testing-library__react-hooks": "4.0.0",
"blitz": "2.0.0-beta.14",
"blitz": "2.0.0-beta.15",
"cross-spawn": "7.0.3",
"find-up": "4.1.0",
"next": "12.2.5",

View File

@@ -1,5 +1,11 @@
# @blitzjs/rpc
## 2.0.0-beta.15
### Patch Changes
- @blitzjs/auth@2.0.0-beta.15
## 2.0.0-beta.14
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/rpc",
"version": "2.0.0-beta.14",
"version": "2.0.0-beta.15",
"scripts": {
"build": "unbuild",
"predev": "wait-on -d 400 ../blitz/dist/index-server.d.ts && wait-on -d 400 ../blitz-auth/dist/index-browser.d.ts",
@@ -20,7 +20,8 @@
"dist/**"
],
"dependencies": {
"@blitzjs/auth": "2.0.0-beta.14",
"@blitzjs/auth": "2.0.0-beta.15",
"@swc/core": "1.3.7",
"@tanstack/react-query": "4.0.10",
"b64-lite": "1.4.0",
"bad-behavior": "1.0.1",
@@ -30,11 +31,11 @@
"supports-color": "8.1.1"
},
"devDependencies": {
"@blitzjs/config": "workspace:2.0.0-beta.14",
"@blitzjs/config": "workspace:2.0.0-beta.15",
"@types/debug": "4.1.7",
"@types/react": "18.0.17",
"@types/react-dom": "17.0.14",
"blitz": "2.0.0-beta.14",
"blitz": "2.0.0-beta.15",
"next": "12.2.5",
"react": "18.2.0",
"react-dom": "18.2.0",

View File

@@ -4,6 +4,7 @@ import {deserialize, serialize} from "superjson"
import {SuperJSONResult} from "superjson/dist/types"
import {CSRFTokenMismatchError, isServer} from "blitz"
import {getQueryKeyFromUrlAndParams, getQueryClient} from "./react-query-utils"
import {stringify} from "superjson"
import {
getAntiCSRFToken,
getPublicDataStore,
@@ -23,6 +24,7 @@ export interface BuildRpcClientParams {
resolverName: string
resolverType: ResolverType
routePath: string
httpMethod: string
}
export interface RpcOptions {
@@ -54,9 +56,10 @@ export function __internal_buildRpcClient({
resolverName,
resolverType,
routePath,
httpMethod,
}: BuildRpcClientParams): RpcClient {
const fullRoutePath = normalizeApiRoute("/api/rpc" + routePath)
const routePathURL = new URL(fullRoutePath, window.location.origin)
const httpClient: RpcClientBase = async (params, opts = {}, signal = undefined) => {
const debug = (await import("debug")).default("blitz:rpc")
if (!opts.fromQueryHook && !opts.fromInvoke) {
@@ -93,18 +96,26 @@ export function __internal_buildRpcClient({
serialized = serialize(params)
}
if (httpMethod === "GET") {
routePathURL.searchParams.set("params", stringify(serialized.json))
routePathURL.searchParams.set("meta", stringify(serialized.meta))
}
const promise = window
.fetch(fullRoutePath, {
method: "POST",
.fetch(routePathURL, {
method: httpMethod,
headers,
credentials: "include",
redirect: "follow",
body: JSON.stringify({
params: serialized.json,
meta: {
params: serialized.meta,
},
}),
body:
httpMethod === "POST"
? JSON.stringify({
params: serialized.json,
meta: {
params: serialized.meta,
},
})
: undefined,
signal,
})
.then(async (response) => {

View File

@@ -1,6 +1,6 @@
import {assert, baseLogger, Ctx, newLine, prettyMs} from "blitz"
import {assert, baseLogger, Ctx, newLine, prettyMs, ResolverConfig} from "blitz"
import {NextApiRequest, NextApiResponse} from "next"
import {deserialize, serialize as superjsonSerialize} from "superjson"
import {deserialize, serialize as superjsonSerialize, parse} from "superjson"
import {resolve} from "path"
import chalk from "chalk"
@@ -14,6 +14,10 @@ function isObject(value: unknown): value is Record<string | symbol, unknown> {
return typeof value === "object" && value !== null
}
const defaultConfig: ResolverConfig = {
httpMethod: "POST",
}
function getGlobalObject<T extends Record<string, unknown>>(key: string, defaultValue: T): T {
assert(key.startsWith("__internal_blitz"), "unsupported key")
if (typeof global === "undefined") {
@@ -25,7 +29,7 @@ function getGlobalObject<T extends Record<string, unknown>>(key: string, default
}
type Resolver = (...args: unknown[]) => Promise<unknown>
type ResolverFiles = Record<string, () => Promise<{default?: Resolver}>>
type ResolverFiles = Record<string, () => Promise<{default?: Resolver; config?: ResolverConfig}>>
export type ResolverPathOptions = "queries|mutations" | "root" | ((path: string) => string)
// We define `global.__internal_blitzRpcResolverFiles` to ensure we use the same global object.
@@ -43,7 +47,7 @@ export function loadBlitzRpcResolverFilesWithInternalMechanism() {
export function __internal_addBlitzRpcResolver(
routePath: string,
resolver: () => Promise<{default?: Resolver}>,
resolver: () => Promise<{default?: Resolver; config?: ResolverConfig}>,
) {
g.blitzRpcResolverFilesLoaded = g.blitzRpcResolverFilesLoaded || {}
g.blitzRpcResolverFilesLoaded[routePath] = resolver
@@ -169,19 +173,33 @@ export function rpcHandler(config: RpcConfig) {
throw new Error("No resolver for path: " + routePath)
}
const resolver = (await loadableResolver()).default
const {default: resolver, config: resolverConfig} = await loadableResolver()
if (!resolver) {
throw new Error("No default export for resolver path: " + routePath)
}
const resolverConfigWithDefaults = {...defaultConfig, ...resolverConfig}
if (req.method === "HEAD") {
// We used to initiate database connection here
res.status(200).end()
return
} else if (req.method === "POST") {
// Handle RPC call
if (typeof req.body.params === "undefined") {
} else if (
req.method === "POST" ||
(req.method === "GET" && resolverConfigWithDefaults.httpMethod === "GET")
) {
if (req.method === "GET") {
if (Object.keys(req.query).length === 1 && req.query.blitz) {
const error = {message: "Request query is missing the required `params` and `meta` keys"}
log.error(error.message)
res.status(400).json({
result: null,
error,
})
return
}
} else if (typeof req.body.params === "undefined") {
const error = {message: "Request body is missing the `params` key"}
log.error(error.message)
res.status(400).json({
@@ -193,10 +211,9 @@ export function rpcHandler(config: RpcConfig) {
try {
const data = deserialize({
json: req.body.params,
meta: req.body.meta?.params,
json: req.method === "POST" ? req.body.params : parse(`${req.query.params}`),
meta: req.method === "POST" ? req.body.meta?.params : parse(`${req.query.meta}`),
})
log.info(customChalk.dim("Starting with input:"), data ? data : JSON.stringify(data))
const startTime = Date.now()
const result = await resolver(data, (res as any).blitzCtx)

View File

@@ -8,6 +8,8 @@ import {
toPosixPath,
} from "./loader-utils"
import {posix} from "path"
import {log, ResolverConfig} from "blitz"
import {getResolverConfig} from "./parse-rpc-config"
// Subset of `import type { LoaderDefinitionFunction } from 'webpack'`
@@ -39,12 +41,24 @@ export async function transformBlitzRpcResolverClient(
) {
assertPosixPath(id)
assertPosixPath(root)
const resolverFilePath = "/" + posix.relative(root, id)
assertPosixPath(resolverFilePath)
const routePath = convertPageFilePathToRoutePath(resolverFilePath, options?.resolverPath)
const resolverName = convertFilePathToResolverName(resolverFilePath)
const resolverType = convertFilePathToResolverType(resolverFilePath)
const resolverConfig: ResolverConfig = {
httpMethod: "POST",
}
if (resolverType === "query") {
try {
const {httpMethod} = getResolverConfig(_src)
if (httpMethod) {
resolverConfig.httpMethod = httpMethod
}
} catch (e) {
log.error(e as string)
}
}
const code = `
// @ts-nocheck
@@ -53,6 +67,7 @@ export async function transformBlitzRpcResolverClient(
resolverName: "${resolverName}",
resolverType: "${resolverType}",
routePath: "${routePath}",
httpMethod: "${resolverConfig.httpMethod}",
});
`

View File

@@ -1,4 +1,4 @@
import {dirname, join, posix, relative} from "path"
import {dirname, join, relative} from "path"
import {promises} from "fs"
import {
assertPosixPath,
@@ -52,18 +52,15 @@ export async function transformBlitzRpcServer(
assertPosixPath(root)
const blitzImport = 'import { __internal_addBlitzRpcResolver } from "@blitzjs/rpc";'
// No break line between `blitzImport` and `src` in order to preserve the source map's line mapping
let code = blitzImport + src
code += "\n\n"
for (let resolverFilePath of resolvers) {
const relativeResolverPath = slash(relative(dirname(id), join(root, resolverFilePath)))
const routePath = convertPageFilePathToRoutePath(resolverFilePath, options?.resolverPath)
code += `__internal_addBlitzRpcResolver('${routePath}', () => import('${relativeResolverPath}'));`
code += `__internal_addBlitzRpcResolver('${routePath}',() => import('${relativeResolverPath}'));`
code += "\n"
}
// console.log("NEW CODE", code)
return code
}

View File

@@ -0,0 +1,57 @@
import {parseSync} from "@swc/core"
import {ResolverConfig} from "blitz"
type _ResolverType = "GET" | "POST"
const defaultResolverConfig: ResolverConfig = {
httpMethod: "POST",
}
export function getResolverConfig(content: string): ResolverConfig {
const resolverConfig = defaultResolverConfig
const resolver = parseSync(content, {
syntax: "typescript",
target: "es2020",
})
const exportDelaration = resolver.body.find((node) => {
if (node.type === "ExportDeclaration") {
if (node.declaration.type === "VariableDeclaration") {
if (node.declaration.declarations[0]?.id.type === "Identifier") {
if (node.declaration.declarations[0].id.value === "config") {
return true
}
}
}
}
return false
})
if (exportDelaration && exportDelaration.type == "ExportDeclaration") {
const declaration = exportDelaration.declaration
if (declaration && declaration.type == "VariableDeclaration") {
const declarator = declaration.declarations[0]
if (declarator && declarator.type == "VariableDeclarator") {
const variable = declarator.init
if (variable && variable.type == "ObjectExpression") {
const properties = variable.properties
if (properties) {
const httpMethodProperty = properties.find((property) => {
if (property.type == "KeyValueProperty") {
if (property.key.type == "Identifier") {
return property.key.value == "httpMethod"
}
}
return false
})
if (httpMethodProperty && httpMethodProperty.type == "KeyValueProperty") {
const value = httpMethodProperty.value
if (value && value.type == "StringLiteral") {
resolverConfig.httpMethod = value.value as _ResolverType
}
}
}
}
}
}
}
return resolverConfig
}

View File

@@ -1,5 +1,12 @@
# blitz
## 2.0.0-beta.15
### Patch Changes
- 1b798d9a: Fix `useSession` hook by exporting `enhancePrisma` from the server entry point instead of server
- @blitzjs/generator@2.0.0-beta.15
## 2.0.0-beta.14
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "blitz",
"version": "2.0.0-beta.14",
"version": "2.0.0-beta.15",
"scripts": {
"build": "unbuild",
"dev": "pnpm run predev && watch unbuild src --wait=0.2",
@@ -25,7 +25,7 @@
"blitz": "bin/blitz"
},
"dependencies": {
"@blitzjs/generator": "2.0.0-beta.14",
"@blitzjs/generator": "2.0.0-beta.15",
"@mrleebo/prisma-ast": "0.2.6",
"@types/global-agent": "2.1.1",
"arg": "5.0.1",
@@ -73,7 +73,7 @@
"watchpack": "2.1.1"
},
"devDependencies": {
"@blitzjs/config": "workspace:2.0.0-beta.14",
"@blitzjs/config": "workspace:2.0.0-beta.15",
"@types/cookie": "0.4.1",
"@types/cross-spawn": "6.0.2",
"@types/debug": "4.1.7",

View File

@@ -15,7 +15,7 @@ import {
MutationsGenerator,
ModelGenerator,
QueryGenerator,
addCustomTemplatesBlitzConfig,
customTemplatesBlitzConfig,
} from "@blitzjs/generator"
import {log} from "../../logging"
@@ -65,7 +65,7 @@ const createCustomTemplates = async () => {
})
const templatesPathValue: string = templatesPath.value
const isTypeScript = await getIsTypeScript()
addCustomTemplatesBlitzConfig(templatesPathValue, isTypeScript)
await customTemplatesBlitzConfig(isTypeScript, templatesPathValue, true) // to run the codemod
log.success(`🚀 Custom templates path added/updated in app/blitz-server file`)
const customTemplatesPath = require("path").join(process.cwd(), templatesPathValue)
const fsExtra = await import("fs-extra")
@@ -275,20 +275,12 @@ const generate: CliCommand = async () => {
const generators = generatorMap[selectedType as keyof typeof generatorMap]
const isTypeScript = await getIsTypeScript()
const blitzServerPath = isTypeScript ? "app/blitz-server.ts" : "app/blitz-server.js"
const blitzServer = require("path").join(process.cwd(), blitzServerPath)
const {register} = require("esbuild-register/dist/node")
const {unregister} = register({
target: "es6",
})
const blitzConfig = require(blitzServer)
const {cliConfig} = blitzConfig
unregister()
const cliConfig = await customTemplatesBlitzConfig(isTypeScript)
for (const GeneratorClass of generators) {
const generator = new GeneratorClass({
destinationRoot: require("path").resolve(),
templateDir: cliConfig?.customTemplates,
templateDir: cliConfig,
extraArgs: args["_"].slice(3) as string[],
modelName: singularRootContext,
modelNames: modelNames(singularRootContext),

View File

@@ -96,4 +96,3 @@ if (typeof window !== "undefined" && process.env.NODE_ENV === "development") {
export * from "./utils"
export * from "./types"
export * from "./utils/zod"
export * from "./utils/prisma"

View File

@@ -5,9 +5,11 @@ import {Ctx} from "./types"
export * from "./index-browser"
export * from "./types"
export * from "./utils/run-prisma"
export * from "./utils/enhance-prisma"
export * from "./middleware"
export * from "./paginate"
export * from "./logging"
export {startWatcher, stopWatcher} from "./cli/utils/routes-manifest"
export interface MiddlewareResponse<C extends Ctx = Ctx> extends ServerResponse {

View File

@@ -6,6 +6,10 @@ export interface RouteUrlObject extends Pick<UrlObject, "pathname" | "query"> {
pathname: string
}
export type ResolverConfig = {
httpMethod: "GET" | "POST"
}
export type BlitzCliConfig = {
customTemplates?: string
}

View File

@@ -1,5 +1,13 @@
# @blitzjs/codemod
## 2.0.0-beta.15
### Patch Changes
- Updated dependencies [1b798d9a]
- blitz@2.0.0-beta.15
- @blitzjs/generator@2.0.0-beta.15
## 2.0.0-beta.14
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/codemod",
"version": "2.0.0-beta.14",
"version": "2.0.0-beta.15",
"scripts": {
"build": "unbuild",
"dev": "watch unbuild src --wait=0.2",
@@ -25,9 +25,9 @@
"@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-beta.14",
"@blitzjs/generator": "2.0.0-beta.15",
"arg": "5.0.1",
"blitz": "2.0.0-beta.14",
"blitz": "2.0.0-beta.15",
"chalk": "^4.1.0",
"cross-spawn": "7.0.3",
"debug": "4.3.3",

View File

@@ -1,5 +1,7 @@
# @blitzjs/config
## 2.0.0-beta.15
## 2.0.0-beta.14
## 2.0.0-beta.13

View File

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

View File

@@ -1,5 +1,7 @@
# @blitzjs/generator
## 2.0.0-beta.15
## 2.0.0-beta.14
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/generator",
"version": "2.0.0-beta.14",
"version": "2.0.0-beta.15",
"scripts": {
"dev": "watch unbuild src --wait=0.2",
"build": "unbuild && pnpm build:templates",
@@ -31,6 +31,7 @@
"diff": "5.0.0",
"enquirer": "2.3.6",
"fs-extra": "10.0.1",
"globby": "13.1.2",
"got": "^11.8.1",
"jscodeshift": "0.13.0",
"mem-fs": "1.2.0",
@@ -46,7 +47,7 @@
"vinyl": "2.2.1"
},
"devDependencies": {
"@blitzjs/config": "2.0.0-beta.14",
"@blitzjs/config": "2.0.0-beta.15",
"@juanm04/cpx": "2.0.1",
"@types/babel__core": "7.1.19",
"@types/diff": "5.0.2",

View File

@@ -16,12 +16,31 @@ import {readdirRecursive} from "./utils/readdir-recursive"
import prettier from "prettier"
const debug = require("debug")("blitz:generator")
export const addCustomTemplatesBlitzConfig = (
customTemplatesPath: string,
export function getProjectRootSync() {
return path.dirname(getConfigSrcPath())
}
export function getConfigSrcPath() {
const jsPath = path.resolve(path.join(process.cwd(), "next.config.js"))
return jsPath
}
export const customTemplatesBlitzConfig = async (
isTypeScript: boolean,
customTemplatesPath = "",
codemod = false,
) => {
const blitzServer = isTypeScript ? "app/blitz-server.ts" : "app/blitz-server.js"
const blitzServerPath = require("path").join(process.cwd(), blitzServer)
const {globby} = await import("globby")
const blitzServer = await globby(["{app,src}/**/blitz-server.{ts,js}"], {
cwd: getProjectRootSync(),
})
if (blitzServer.length === 0) {
throw new Error("Could not find blitz-server.js or blitz-server.ts in app or src folder")
}
if (blitzServer.length > 1) {
throw new Error("Found more than one blitz-server.js or blitz-server.ts in app or src folder")
}
const blitzServerPath = require("path").join(process.cwd(), blitzServer.at(0))
const userConfigModuleSource = fs.readFileSync(blitzServerPath, {encoding: "utf-8"})
const userConfigModule = j(userConfigModuleSource, {parser: customTsParser})
const program = userConfigModule.get()
@@ -86,6 +105,9 @@ export const addCustomTemplatesBlitzConfig = (
if (customTemplatesProperty.type === "ObjectProperty") {
const customValue = customTemplatesProperty.value
if (customValue.type === "StringLiteral") {
if (!codemod) {
return customValue.value
}
customValue.value = customTemplatesPath
}
}
@@ -94,8 +116,10 @@ export const addCustomTemplatesBlitzConfig = (
}
}
}
const newSource = userConfigModule.toSource()
fs.writeFileSync(blitzServerPath, newSource)
if (codemod) {
const newSource = userConfigModule.toSource()
fs.writeFileSync(blitzServerPath, newSource)
}
}
export const customTsParser = {

View File

@@ -25,7 +25,7 @@
"@typescript-eslint/parser": "5.9.1"
},
"devDependencies": {
"@blitzjs/config": "2.0.0-beta.14",
"@blitzjs/config": "2.0.0-beta.15",
"@types/react": "18.0.17",
"@types/react-dom": "17.0.14",
"react": "18.2.0",

721
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -23,7 +23,7 @@
},
"homepage": "https://github.com/blitz-js/blitz#readme",
"dependencies": {
"blitz": "2.0.0-beta.14",
"blitz": "2.0.0-beta.15",
"jscodeshift": "0.13.0"
},
"devDependencies": {

View File

@@ -22,7 +22,7 @@
},
"homepage": "https://github.com/blitz-js/blitz#readme",
"dependencies": {
"blitz": "2.0.0-beta.14",
"blitz": "2.0.0-beta.15",
"jscodeshift": "0.13.0"
},
"devDependencies": {

View File

@@ -24,7 +24,7 @@
},
"homepage": "https://github.com/blitz-js/blitz#readme",
"dependencies": {
"blitz": "2.0.0-beta.14",
"blitz": "2.0.0-beta.15",
"jscodeshift": "0.13.0"
},
"devDependencies": {

View File

@@ -24,7 +24,7 @@
},
"homepage": "https://github.com/blitz-js/blitz#readme",
"dependencies": {
"blitz": "2.0.0-beta.14",
"blitz": "2.0.0-beta.15",
"jscodeshift": "0.13.0"
},
"devDependencies": {

View File

@@ -22,7 +22,7 @@
},
"homepage": "https://github.com/blitz-js/blitz#readme",
"dependencies": {
"blitz": "2.0.0-beta.14",
"blitz": "2.0.0-beta.15",
"jscodeshift": "0.13.0"
},
"devDependencies": {

View File

@@ -24,6 +24,6 @@
},
"homepage": "https://github.com/blitz-js/blitz#readme",
"dependencies": {
"blitz": "2.0.0-beta.14"
"blitz": "2.0.0-beta.15"
}
}

View File

@@ -24,6 +24,6 @@
},
"homepage": "https://github.com/blitz-js/blitz#readme",
"dependencies": {
"blitz": "2.0.0-beta.14"
"blitz": "2.0.0-beta.15"
}
}

View File

@@ -23,7 +23,7 @@
},
"homepage": "https://github.com/blitz-js/blitz#readme",
"dependencies": {
"blitz": "2.0.0-beta.14",
"blitz": "2.0.0-beta.15",
"jscodeshift": "0.13.0"
},
"devDependencies": {

View File

@@ -22,7 +22,7 @@
},
"homepage": "https://github.com/blitz-js/blitz#readme",
"dependencies": {
"blitz": "2.0.0-beta.14",
"blitz": "2.0.0-beta.15",
"jscodeshift": "0.13.0",
"uuid": "^8.3.1"
},

View File

@@ -23,7 +23,7 @@
},
"homepage": "https://github.com/blitz-js/blitz#readme",
"dependencies": {
"blitz": "2.0.0-beta.14",
"blitz": "2.0.0-beta.15",
"jscodeshift": "0.13.0"
},
"devDependencies": {

View File

@@ -23,7 +23,7 @@
},
"homepage": "https://github.com/blitz-js/blitz#readme",
"dependencies": {
"blitz": "2.0.0-beta.14",
"blitz": "2.0.0-beta.15",
"jscodeshift": "0.13.0"
},
"devDependencies": {

View File

@@ -23,7 +23,7 @@
},
"homepage": "https://github.com/blitz-js/blitz#readme",
"dependencies": {
"blitz": "2.0.0-beta.14",
"blitz": "2.0.0-beta.15",
"jscodeshift": "0.13.0"
},
"devDependencies": {

View File

@@ -23,6 +23,6 @@
},
"homepage": "https://github.com/blitz-js/blitz#readme",
"dependencies": {
"blitz": "2.0.0-beta.14"
"blitz": "2.0.0-beta.15"
}
}

View File

@@ -22,6 +22,6 @@
},
"homepage": "https://github.com/blitz-js/blitz#readme",
"dependencies": {
"blitz": "2.0.0-beta.14"
"blitz": "2.0.0-beta.15"
}
}

View File

@@ -23,7 +23,7 @@
},
"homepage": "https://github.com/blitz-js/blitz#readme",
"dependencies": {
"blitz": "2.0.0-beta.14",
"blitz": "2.0.0-beta.15",
"jscodeshift": "0.13.0"
},
"devDependencies": {

View File

@@ -22,6 +22,6 @@
},
"homepage": "https://github.com/blitz-js/blitz#readme",
"dependencies": {
"blitz": "2.0.0-beta.14"
"blitz": "2.0.0-beta.15"
}
}

View File

@@ -22,7 +22,7 @@
},
"homepage": "https://github.com/blitz-js/blitz#readme",
"dependencies": {
"blitz": "2.0.0-beta.14",
"blitz": "2.0.0-beta.15",
"jscodeshift": "0.13.0",
"uuid": "^8.3.1"
},

View File

@@ -24,7 +24,7 @@
},
"homepage": "https://github.com/blitz-js/blitz#readme",
"dependencies": {
"blitz": "2.0.0-beta.14",
"blitz": "2.0.0-beta.15",
"jscodeshift": "0.13.0"
},
"devDependencies": {

View File

@@ -24,7 +24,7 @@
},
"homepage": "https://github.com/blitz-js/blitz#readme",
"dependencies": {
"blitz": "2.0.0-beta.14",
"blitz": "2.0.0-beta.15",
"jscodeshift": "0.13.0"
},
"devDependencies": {

View File

@@ -22,7 +22,7 @@
},
"homepage": "https://github.com/blitz-js/blitz#readme",
"dependencies": {
"blitz": "2.0.0-beta.14",
"blitz": "2.0.0-beta.15",
"jscodeshift": "0.13.0"
},
"devDependencies": {

View File

@@ -23,7 +23,7 @@
},
"homepage": "https://github.com/blitz-js/blitz#readme",
"dependencies": {
"blitz": "2.0.0-beta.14",
"blitz": "2.0.0-beta.15",
"jscodeshift": "0.13.0"
},
"devDependencies": {

View File

@@ -25,7 +25,7 @@
},
"homepage": "https://github.com/blitz-js/blitz#readme",
"dependencies": {
"blitz": "2.0.0-beta.14",
"blitz": "2.0.0-beta.15",
"jscodeshift": "0.13.0"
},
"devDependencies": {