Skip to content

Commit e46de5c

Browse files
committed
chore: use better naming and comments for utils
1 parent ebac7d7 commit e46de5c

File tree

4 files changed

+1101
-38
lines changed

4 files changed

+1101
-38
lines changed

plugin/src/templates/edge/prepare-destination.ts renamed to plugin/src/templates/edge/next-utils.ts

+38-16
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
1-
// Ported to Deno from Next.js source
2-
// https://github.com/vercel/next.js/blob/7280c3ced186bb9a7ae3d7012613ef93f20b0fa9/packages/next/shared/lib/router/utils/prepare-destination.ts
3-
import { Key, match } from 'https://deno.land/x/[email protected]/index.ts'
4-
import { getCookies } from 'https://deno.land/[email protected]/http/cookie.ts'
1+
/**
2+
* Various router utils ported to Deno from Next.js source
3+
* https://github.com/vercel/next.js/blob/7280c3ced186bb9a7ae3d7012613ef93f20b0fa9/packages/next/shared/lib/router/utils/
4+
* Licence: https://github.com/vercel/next.js/blob/7280c3ced186bb9a7ae3d7012613ef93f20b0fa9/license.md
5+
*
6+
* Some types have been re-implemented to be more compatible with Deno or avoid chains of dependent files
7+
*/
8+
9+
// Deno imports
10+
import type { Key } from 'https://deno.land/x/[email protected]/index.ts'
511

612
import { compile, pathToRegexp } from 'https://deno.land/x/[email protected]/index.ts'
13+
import { getCookies } from 'https://deno.land/[email protected]/http/cookie.ts'
714

8-
// regexp is based on https://github.com/sindresorhus/escape-string-regexp
9-
const reHasRegExp = /[|\\{}()[\]^$+*?.-]/
10-
const reReplaceRegExp = /[|\\{}()[\]^$+*?.-]/g
15+
// Inlined/re-implemented types
1116

1217
export interface ParsedUrlQuery {
1318
[key: string]: string | string[]
1419
}
1520

1621
export interface Params {
22+
// Yeah, best we get
23+
// deno-lint-ignore no-explicit-any
1724
[param: string]: any
1825
}
1926

@@ -35,6 +42,7 @@ export type Rewrite = {
3542
basePath?: false
3643
locale?: false
3744
has?: RouteHas[]
45+
regex: string
3846
}
3947

4048
export type Header = {
@@ -43,6 +51,7 @@ export type Header = {
4351
locale?: false
4452
headers: Array<{ key: string; value: string }>
4553
has?: RouteHas[]
54+
regex: string
4655
}
4756
export type Redirect = {
4857
source: string
@@ -52,8 +61,14 @@ export type Redirect = {
5261
has?: RouteHas[]
5362
statusCode?: number
5463
permanent?: boolean
64+
regex: string
5565
}
5666

67+
// escape-regexp.ts
68+
// regexp is based on https://github.com/sindresorhus/escape-string-regexp
69+
const reHasRegExp = /[|\\{}()[\]^$+*?.-]/
70+
const reReplaceRegExp = /[|\\{}()[\]^$+*?.-]/g
71+
5772
export function escapeStringRegexp(str: string) {
5873
// see also: https://github.com/lodash/lodash/blob/2da024c3b4f9947a48517639de7560457cd4ec6c/escapeRegExp.js#L23
5974
if (reHasRegExp.test(str)) {
@@ -62,6 +77,7 @@ export function escapeStringRegexp(str: string) {
6277
return str
6378
}
6479

80+
// querystring.ts
6581
export function searchParamsToUrlQuery(searchParams: URLSearchParams): ParsedUrlQuery {
6682
const query: ParsedUrlQuery = {}
6783
searchParams.forEach((value, key) => {
@@ -76,6 +92,7 @@ export function searchParamsToUrlQuery(searchParams: URLSearchParams): ParsedUrl
7692
return query
7793
}
7894

95+
// parse-url.ts
7996
interface ParsedUrl {
8097
hash: string
8198
hostname?: string | null
@@ -101,6 +118,7 @@ export function parseUrl(url: string): ParsedUrl {
101118
}
102119
}
103120

121+
// prepare-destination.ts
104122
// Changed to use WHATWG Fetch Request instead of IncomingMessage
105123
export function matchHas(req: Pick<Request, 'headers' | 'url'>, has: RouteHas[], query: Params): false | Params {
106124
const params: Params = {}
@@ -109,22 +127,19 @@ export function matchHas(req: Pick<Request, 'headers' | 'url'>, has: RouteHas[],
109127
const allMatch = has.every((hasItem) => {
110128
let value: undefined | string | null
111129
let key = hasItem.key
112-
if (!key) {
113-
return true
114-
}
115130

116131
switch (hasItem.type) {
117132
case 'header': {
118-
key = key.toLowerCase()
133+
key = hasItem.key.toLowerCase()
119134
value = req.headers.get(key)
120135
break
121136
}
122137
case 'cookie': {
123-
value = cookies[key]
138+
value = cookies[hasItem.key]
124139
break
125140
}
126141
case 'query': {
127-
value = query[key]
142+
value = query[hasItem.key]
128143
break
129144
}
130145
case 'host': {
@@ -135,12 +150,13 @@ export function matchHas(req: Pick<Request, 'headers' | 'url'>, has: RouteHas[],
135150
break
136151
}
137152
}
138-
if (!hasItem.value && value) {
153+
if (!hasItem.value && value && key) {
139154
params[getSafeParamName(key)] = value
140155
return true
141156
} else if (value) {
142157
const matcher = new RegExp(`^${hasItem.value}$`)
143158
const matches = Array.isArray(value) ? value.slice(-1)[0].match(matcher) : value.match(matcher)
159+
144160
if (matches) {
145161
if (Array.isArray(matches)) {
146162
if (matches.groups) {
@@ -205,8 +221,6 @@ export function prepareDestination(args: {
205221
escapedDestination = escapeSegment(escapedDestination, param)
206222
}
207223

208-
console.log({ escapedDestination, params: args.params })
209-
210224
const parsedDestination: ParsedUrl = parseUrl(escapedDestination)
211225
const destQuery = parsedDestination.query
212226
const destPath = unescapeSegments(`${parsedDestination.pathname!}${parsedDestination.hash || ''}`)
@@ -319,3 +333,11 @@ function escapeSegment(str: string, segmentName: string) {
319333
function unescapeSegments(str: string) {
320334
return str.replace(/__ESC_COLON_/gi, ':')
321335
}
336+
337+
// is-dynamic.ts
338+
// Identify /[param]/ in route string
339+
const TEST_ROUTE = /\/\[[^/]+?\](?=\/|$)/
340+
341+
export function isDynamicRoute(route: string): boolean {
342+
return TEST_ROUTE.test(route)
343+
}

0 commit comments

Comments
 (0)