File tree Expand file tree Collapse file tree 3 files changed +31
-29
lines changed Expand file tree Collapse file tree 3 files changed +31
-29
lines changed Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ import { Readable } from "node:stream";
5
5
import { BuildId , HtmlPages , NextConfig } from "config/index.js" ;
6
6
import type { IncomingMessage } from "http/index.js" ;
7
7
import { OpenNextNodeResponse } from "http/openNextResponse.js" ;
8
- import { parseHeaders } from "http/util.js" ;
8
+ import { getQueryFromIterator , parseHeaders } from "http/util.js" ;
9
9
import type {
10
10
FunctionsConfigManifest ,
11
11
MiddlewareManifest ,
@@ -38,21 +38,11 @@ export function isExternal(url?: string, host?: string) {
38
38
export function convertFromQueryString ( query : string ) {
39
39
if ( query === "" ) return { } ;
40
40
const queryParts = query . split ( "&" ) ;
41
- return queryParts . reduce (
42
- ( acc , part ) => {
43
- const [ key , value ] = part . split ( "=" ) ;
44
- if ( key in acc ) {
45
- if ( Array . isArray ( acc [ key ] ) ) {
46
- acc [ key ] . push ( value ) ;
47
- } else {
48
- acc [ key ] = [ acc [ key ] , value ] ;
49
- }
50
- } else {
51
- acc [ key ] = value ;
52
- }
53
- return acc ;
54
- } ,
55
- { } as Record < string , string | string [ ] > ,
41
+ return getQueryFromIterator (
42
+ queryParts . map ( ( p ) => {
43
+ const [ key , value ] = p . split ( "=" ) ;
44
+ return [ key , value ] as const ;
45
+ } ) ,
56
46
) ;
57
47
}
58
48
Original file line number Diff line number Diff line change @@ -39,3 +39,25 @@ export function parseCookies(
39
39
? cookies . split ( / (?< ! E x p i r e s = \w + ) , / i) . map ( ( c ) => c . trim ( ) )
40
40
: cookies ;
41
41
}
42
+
43
+ /**
44
+ *
45
+ * Get the query object from an iterable of [key, value] pairs
46
+ * @param it - The iterable of [key, value] pairs
47
+ * @returns The query object
48
+ */
49
+ export function getQueryFromIterator ( it : Iterable < [ string , string ] > ) {
50
+ const query : Record < string , string | string [ ] > = { } ;
51
+ for ( const [ key , value ] of it ) {
52
+ if ( key in query ) {
53
+ if ( Array . isArray ( query [ key ] ) ) {
54
+ query [ key ] . push ( value ) ;
55
+ } else {
56
+ query [ key ] = [ query [ key ] , value ] ;
57
+ }
58
+ } else {
59
+ query [ key ] = value ;
60
+ }
61
+ }
62
+ return query ;
63
+ }
Original file line number Diff line number Diff line change
1
+ import { getQueryFromIterator } from "http/util.js" ;
2
+
1
3
export function removeUndefinedFromQuery (
2
4
query : Record < string , string | string [ ] | undefined > ,
3
5
) {
@@ -29,17 +31,5 @@ export function extractHostFromHeaders(
29
31
* @returns
30
32
*/
31
33
export function getQueryFromSearchParams ( searchParams : URLSearchParams ) {
32
- const query : Record < string , string | string [ ] > = { } ;
33
- for ( const [ key , value ] of searchParams . entries ( ) ) {
34
- if ( key in query ) {
35
- if ( Array . isArray ( query [ key ] ) ) {
36
- query [ key ] . push ( value ) ;
37
- } else {
38
- query [ key ] = [ query [ key ] , value ] ;
39
- }
40
- } else {
41
- query [ key ] = value ;
42
- }
43
- }
44
- return query ;
34
+ return getQueryFromIterator ( searchParams . entries ( ) ) ;
45
35
}
You can’t perform that action at this time.
0 commit comments