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

React version upgrade #352

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20,006 changes: 9,972 additions & 10,034 deletions package-lock.json

Large diffs are not rendered by default.

54 changes: 27 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "next-routes",
"version": "1.4.2",
"name": "next-routes-latest",
"version": "2.0.1",
"description": "Easy to use universal dynamic routes for Next.js",
"repository": "fridays/next-routes",
"repository": "saurabhgupta050890/next-routes",
"main": "dist",
"typings": "typings/next-routes.d.ts",
"files": [
Expand All @@ -21,7 +21,7 @@
"dev": "concurrently -k 'npm run build -- -w' 'npm run testOnly -- --watch'"
},
"standard": {
"parser": "babel-eslint"
"parser": "@babel/eslint-parser"
},
"jest": {
"testEnvironment": "node",
Expand All @@ -31,34 +31,34 @@
]
},
"dependencies": {
"path-to-regexp": "^2.1.0"
"path-to-regexp": "^6.2.2"
},
"peerDependencies": {
"next": ">=2",
"react": "^15.4.2 || ^16"
"next": ">=9",
"react": ">=16"
},
"devDependencies": {
"@babel/cli": "^7.0.0-beta.47",
"@babel/core": "^7.0.0-beta.47",
"@babel/preset-env": "^7.0.0-beta.47",
"@types/next": "^2.4.8",
"@types/node": "^10.1.2",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^8.2.1",
"babel-jest": "^22.4.3",
"concurrently": "^3.5.0",
"coveralls": "^3.0.0",
"del-cli": "^1.1.0",
"jest": "^22.4.3",
"next": "^6.0.0",
"prop-types": "^15.6.0",
"react": "^16.0.0",
"react-dom": "^16.0.0",
"react-test-renderer": "^16.0.0",
"standard": "^11.0.1",
"typescript": "^2.7.2"
"@babel/cli": "^7.24.7",
"@babel/core": "^7.24.7",
"@babel/eslint-parser": "7.24.7",
"@babel/preset-env": "^7.24.7",
"@types/node": "^20.14.10",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"babel-jest": "^29.7.0",
"concurrently": "^8.2.2",
"coveralls": "^3.1.1",
"del-cli": "^5.1.0",
"jest": "^29.7.0",
"next": "^14.2.4",
"prop-types": "^15.8.1",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-test-renderer": "^18.3.1",
"standard": "^17.1.0",
"typescript": "5.5.3"
},
"author": "fridays",
"author": "Saurabh",
"license": "MIT",
"keywords": [
"next",
Expand Down
103 changes: 55 additions & 48 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import pathToRegexp from 'path-to-regexp'
import { pathToRegexp, compile } from 'path-to-regexp'
import React from 'react'
import {parse} from 'url'
/* eslint-disable n/no-deprecated-api */
import { parse } from 'url'
import NextLink from 'next/link'
import NextRouter from 'next/router'

module.exports = opts => new Routes(opts)
module.exports = (opts) => new Routes(opts)

class Routes {
constructor ({
Link = NextLink,
Router = NextRouter
} = {}) {
constructor ({ Link = NextLink, Router = NextRouter } = {}) {
this.routes = []
this.Link = this.getLink(Link)
this.Router = this.getRouter(Router)
Expand All @@ -27,7 +25,7 @@ class Routes {
pattern = name
name = null
}
options = {name, pattern, page}
options = { name, pattern, page }
}

if (this.findByName(name)) {
Expand All @@ -40,44 +38,47 @@ class Routes {

findByName (name) {
if (name) {
return this.routes.filter(route => route.name === name)[0]
return this.routes.filter((route) => route.name === name)[0]
}
}

match (url) {
const parsedUrl = parse(url, true)
const {pathname, query} = parsedUrl

return this.routes.reduce((result, route) => {
if (result.route) return result
const params = route.match(pathname)
if (!params) return result
return {...result, route, params, query: {...query, ...params}}
}, {query, parsedUrl})
const { pathname, query } = parsedUrl

return this.routes.reduce(
(result, route) => {
if (result.route) return result
const params = route.match(pathname)
if (!params) return result
return { ...result, route, params, query: { ...query, ...params } }
},
{ query, parsedUrl }
)
}

findAndGetUrls (nameOrUrl, params) {
const route = this.findByName(nameOrUrl)

if (route) {
return {route, urls: route.getUrls(params), byName: true}
return { route, urls: route.getUrls(params), byName: true }
} else {
const {route, query} = this.match(nameOrUrl)
const { route, query } = this.match(nameOrUrl)
const href = route ? route.getHref(query) : nameOrUrl
const urls = {href, as: nameOrUrl}
return {route, urls}
const urls = { href, as: nameOrUrl }
return { route, urls }
}
}

getRequestHandler (app, customHandler) {
const nextHandler = app.getRequestHandler()

return (req, res) => {
const {route, query, parsedUrl} = this.match(req.url)
const { route, query, parsedUrl } = this.match(req.url)

if (route) {
if (customHandler) {
customHandler({req, res, route, query})
customHandler({ req, res, route, query })
} else {
app.render(req, res, route.page, query)
}
Expand All @@ -88,8 +89,8 @@ class Routes {
}

getLink (Link) {
const LinkRoutes = props => {
const {route, params, to, ...newProps} = props
const LinkRoutes = (props) => {
const { route, params, to, ...newProps } = props
const nameOrUrl = route || to

if (nameOrUrl) {
Expand All @@ -102,8 +103,11 @@ class Routes {
}

getRouter (Router) {
const wrap = method => (route, params, options) => {
const {byName, urls: {as, href}} = this.findAndGetUrls(route, params)
const wrap = (method) => (route, params, options) => {
const {
byName,
urls: { as, href }
} = this.findAndGetUrls(route, params)
return Router[method](href, as, byName ? options : params)
}

Expand All @@ -115,17 +119,17 @@ class Routes {
}

class Route {
constructor ({name, pattern, page = name}) {
constructor ({ name, pattern, page = name }) {
if (!name && !page) {
throw new Error(`Missing page to render for route "${pattern}"`)
}

this.name = name
this.pattern = pattern || `/${name}`
this.page = page.replace(/(^|\/)index$/, '').replace(/^\/?/, '/')
this.regex = pathToRegexp(this.pattern, this.keys = [])
this.keyNames = this.keys.map(key => key.name)
this.toPath = pathToRegexp.compile(this.pattern)
this.regex = pathToRegexp(this.pattern, (this.keys = []))
this.keyNames = this.keys.map((key) => key.name)
this.toPath = compile(this.pattern)
}

match (path) {
Expand All @@ -151,34 +155,37 @@ class Route {
getAs (params = {}) {
const as = this.toPath(params) || '/'
const keys = Object.keys(params)
const qsKeys = keys.filter(key => this.keyNames.indexOf(key) === -1)
const qsKeys = keys.filter((key) => this.keyNames.indexOf(key) === -1)

if (!qsKeys.length) return as

const qsParams = qsKeys.reduce((qs, key) => Object.assign(qs, {
[key]: params[key]
}), {})
const qsParams = qsKeys.reduce(
(qs, key) =>
Object.assign(qs, {
[key]: params[key]
}),
{}
)

return `${as}?${toQuerystring(qsParams)}`
}

getUrls (params) {
const as = this.getAs(params)
const href = this.getHref(params)
return {as, href}
return { as, href }
}
}

const toQuerystring = obj => Object.keys(obj)
.filter(key => obj[key] !== null && obj[key] !== undefined)
.map(key => {
let value = obj[key]
const toQuerystring = (obj) =>
Object.keys(obj)
.filter((key) => obj[key] !== null && obj[key] !== undefined)
.map((key) => {
let value = obj[key]

if (Array.isArray(value)) {
value = value.join('/')
}
return [
encodeURIComponent(key),
encodeURIComponent(value)
].join('=')
}).join('&')
if (Array.isArray(value)) {
value = value.join('/')
}
return [encodeURIComponent(key), encodeURIComponent(value)].join('=')
})
.join('&')
Loading