Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[legacy-framework] Move internal @blitzjs/core package into nextjs fork core (meta) #2857

Merged
merged 16 commits into from
Oct 19, 2021
Merged
2 changes: 1 addition & 1 deletion .node-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12.20.0
14.18.1
6 changes: 3 additions & 3 deletions examples/auth/app/pages/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {render} from "test/utils"
import Home from "./index"

jest.mock("@blitzjs/core", () => ({
...jest.requireActual<object>("@blitzjs/core")!,
jest.mock("next/data-client", () => ({
...jest.requireActual<object>("next/data-client")!,
useQuery: () => [
{
id: 1,
Expand All @@ -24,4 +24,4 @@ test("renders blitz documentation link", () => {
const element = getByText(/powered by blitz/i)
// @ts-ignore
expect(element).toBeInTheDocument()
})
})
4 changes: 2 additions & 2 deletions examples/cypress/app/pages/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { render } from "test/utils"
import Home from "./index"

jest.mock("@blitzjs/core", () => ({
...jest.requireActual<object>("@blitzjs/core")!,
jest.mock("next/data-client", () => ({
...jest.requireActual<object>("next/data-client")!,
useQuery: () => [
{
id: 1,
Expand Down
2 changes: 1 addition & 1 deletion examples/cypress/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"react": "0.0.0-experimental-6a589ad71",
"react-dom": "0.0.0-experimental-6a589ad71",
"react-final-form": "6.5.2",
"zod": "3.8.1"
"zod": "3.10.1"
},
"devDependencies": {
"@testing-library/cypress": "8.0.1",
Expand Down
1 change: 1 addition & 0 deletions nextjs/.node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
14.18.1
10 changes: 5 additions & 5 deletions nextjs/packages/next/build/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ export async function saveRouteManifest(
async function findNodeModulesRoot(src: string) {
/*
* Because of our package structure, and because of how things like pnpm link modules,
* we must first find blitz package, and then find @blitzjs/core and then
* the root of @blitzjs/core
* we must first find blitz package, and then find `next` and then
* the root of `next`
*
* This is because we import from `.blitz` inside @blitzjs/core.
* This is because we import from `.blitz` inside `next/stdlib`.
* If that changes, then this logic here will need to change
*/
manifestDebug('src ' + src)
Expand Down Expand Up @@ -189,13 +189,13 @@ async function findNodeModulesRoot(src: string) {
}
const blitzCorePkgLocation = dirname(
(await findUp('package.json', {
cwd: resolveFrom(blitzPkgLocation, '@blitzjs/core'),
cwd: resolveFrom(blitzPkgLocation, 'next'),
})) ?? ''
)
manifestDebug('blitzCorePkgLocation ' + blitzCorePkgLocation)
if (!blitzCorePkgLocation) {
throw new Error(
"Internal Blitz Error: unable to find '@blitzjs/core' package location"
"Internal Blitz Error: unable to find 'next' package location"
)
}
root = join(blitzCorePkgLocation, '../../')
Expand Down
89 changes: 88 additions & 1 deletion nextjs/packages/next/client/router.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* global window */
import React from 'react'
import Router from '../shared/lib/router/router'
import Router, {
extractQueryFromAsPath,
extractRouterParams,
} from '../shared/lib/router/router'
import type { NextRouter } from '../shared/lib/router/router'
import { RouterContext } from '../shared/lib/router-context'

Expand All @@ -17,6 +20,7 @@ type SingletonRouterBase = {
export { Router }

export type { NextRouter }
export type BlitzRouter = NextRouter

export type SingletonRouter = SingletonRouterBase & NextRouter

Expand Down Expand Up @@ -177,3 +181,86 @@ export function makePublicRouterInstance(router: Router): NextRouter {

return instance
}

export function useRouterQuery() {
const router = useRouter()

const query = React.useMemo(() => {
const query = extractQueryFromAsPath(router.asPath)
return query
}, [router.asPath])

return query
}

type Dict<T> = Record<string, T | undefined>
type ReturnTypes = 'string' | 'number' | 'array'

export function useParams(): Dict<string | string[]>
export function useParams(returnType?: ReturnTypes): Dict<string | string[]>
export function useParams(returnType: 'string'): Dict<string>
export function useParams(returnType: 'number'): Dict<number>
export function useParams(returnType: 'array'): Dict<string[]>

export function useParams(
returnType?: 'string' | 'number' | 'array' | undefined
) {
const router = useRouter()
const query = useRouterQuery()

const params = React.useMemo(() => {
const rawParams = extractRouterParams(router.query, query)

if (returnType === 'string') {
const params: Dict<string> = {}
for (const key in rawParams) {
if (typeof rawParams[key] === 'string') {
params[key] = rawParams[key] as string
}
}
return params
}

if (returnType === 'number') {
const params: Dict<number> = {}
for (const key in rawParams) {
if (rawParams[key]) {
const num = Number(rawParams[key])
params[key] = isNaN(num) ? undefined : num
}
}
return params
}

if (returnType === 'array') {
const params: Dict<string[]> = {}
for (const key in rawParams) {
const rawValue = rawParams[key]
if (Array.isArray(rawParams[key])) {
params[key] = rawValue as string[]
} else if (typeof rawValue === 'string') {
params[key] = [rawValue]
}
}
return params
}

return rawParams
}, [router.query, query, returnType])

return params
}

export function useParam(key: string): undefined | string | string[]
export function useParam(key: string, returnType: 'string'): string | undefined
export function useParam(key: string, returnType: 'number'): number | undefined
export function useParam(key: string, returnType: 'array'): string[] | undefined
export function useParam(
key: string,
returnType?: ReturnTypes
): undefined | number | string | string[] {
const params = useParams(returnType)
const value = params[key]

return value
}
22 changes: 22 additions & 0 deletions nextjs/packages/next/compiled/lodash.frompairs/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright 2012-2016 The Dojo Foundation <http://dojofoundation.org/>
Based on Underscore.js, copyright 2009-2016 Jeremy Ashkenas,
DocumentCloud and Investigative Reporters & Editors <http://underscorejs.org/>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1 change: 1 addition & 0 deletions nextjs/packages/next/compiled/lodash.frompairs/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"lodash.frompairs","main":"index.js","author":"John-David Dalton <[email protected]> (http://allyoucanleet.com/)","license":"MIT"}
6 changes: 6 additions & 0 deletions nextjs/packages/next/lib-types/lodash.frompairs.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare module 'lodash.frompairs' {
// eslint-disable-next-line
export default function fromPairs<T>(
pairs: List<[string, T]> | null | undefined
): Dictionary<T>
}
3 changes: 3 additions & 0 deletions nextjs/packages/next/lib-types/micromatch.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'micromatch' {
export function isMatch(source: string, patterns: string[]): boolean
}
176 changes: 176 additions & 0 deletions nextjs/packages/next/lib-types/npm-which.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
// Type definitions for npm-which 3.0
// Project: https://github.com/timoxley/npm-which
// Definitions by: Manuel Thalmann <https://github.com/manuth>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

declare module 'npm-which' {
/**
* Provides options for the `npmwhich`-module.
*/
interface NpmWhichOptions {
/**
* The environment to use for resolving the binary.
*/
env?: NodeJS.ProcessEnv

/**
* The directory to find the binary for.
*/
cwd?: string
}

/**
* Provides options for the `npmwhich`-module.
*/
interface StaticWhichOptions {
/**
* The environment to use for resolving the binary.
*/
env?: NodeJS.ProcessEnv

/**
* The directory to find the binary for.
*/
cwd: string
}

/**
* Represents a callback for handling the result of `NpmWhich`.
*/
interface NpmWhichCallback {
/**
* Handles the result of `NpmWhich`.
*
* @param error
* The error-message.
*
* @param result
* The result.
*/
(error: string, result: string): void
}

/**
* Represents a basic interface for `npm-which`.
*/
interface WhichBase<TOptions> {
/**
* Creates a searcher for the specified command.
*
* @param cmd
* The command to look for.
*
* @param options
* The default options.
*
* @return
* A searcher for the specified command.
*/
(cmd: string, options?: TOptions): InnerWhich

/**
* Searches for the specified command.
*
* @param cmd
* The command to look for.
*
* @param callback
* A callback for handling the result.
*/
(cmd: string, callback: NpmWhichCallback): void

/**
* Searches for the specified command.
*
* @param cmd
* The command to look for.
*
* @param options
* The options for searching the command.
*
* @param callback
* A callback for handling the result.
*/
(cmd: string, options: TOptions, callback: NpmWhichCallback): void
}

/**
* Represents the static instance of `npm-which`.
*/
interface StaticWhich extends WhichBase<StaticWhichOptions> {
/**
* Initializes an `NpmWhich`-instance for the specified working-directory.
*
* @param cwd
* The working-directory to browse.
*/
(cwd?: string): NpmWhich

/**
* Searches for the specified command.
*
* @param cmd
* The command to look for.
*
* @param options
* The options for searching the command.
*/
sync(cmd: string, options: StaticWhichOptions): string
}

/**
* Provides the functionality to search for a command.
*/
interface NpmWhich extends WhichBase<NpmWhichOptions> {
/**
* Searches for the specified command.
*
* @param cmd
* The command to look for.
*
* @param options
* The options for searching the command.
*/
sync(cmd: string, options?: StaticWhichOptions): string
}

interface InnerWhich {
/**
* Creates a searcher for the specified command.
*
* @param options
* The options for searching the command.
*/
(options?: NpmWhichOptions): InnerWhich

/**
* Searches for the command.
*
* @param callback
* A callback for handling the result.
*/
(callback: NpmWhichCallback): void

/**
* Searches for the command.
*
* @param options
* The options for searching the command.
*
* @param callback
* A callback for handling the result.
*/
(options: NpmWhichOptions, callback: NpmWhichCallback): void

/**
* Searches for the command.
*
* @param options
* The options for searching the command.
*/
sync(options?: NpmWhichOptions): string
}

let npmWhich: StaticWhich
export = npmWhich
}
Loading