Skip to content

Commit

Permalink
chore: rename buildUrl to serializePathWithQuery + jsdoc (#3545)
Browse files Browse the repository at this point in the history
* chore: rename buildUrl to serializePathWithQuery

* fix
  • Loading branch information
Uzlopak authored Sep 4, 2024
1 parent 030ce23 commit 89a46dd
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const {
isFormDataLike,
isIterable,
isBlobLike,
buildURL,
serializePathWithQuery,
validateHandler,
getServerName,
normalizedMethodRecords
Expand Down Expand Up @@ -135,7 +135,7 @@ class Request {

this.upgrade = upgrade || null

this.path = query ? buildURL(path, query) : path
this.path = query ? serializePathWithQuery(path, query) : path

this.origin = origin

Expand Down
9 changes: 7 additions & 2 deletions lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ function isBlobLike (object) {
}
}

function buildURL (url, queryParams) {
/**
* @param {string} url The URL to add the query params to
* @param {import('node:querystring').ParsedUrlQueryInput} queryParams The object to serialize into a URL query string
* @returns {string} The URL with the query params added
*/
function serializePathWithQuery (url, queryParams) {
if (url.includes('?') || url.includes('#')) {
throw new Error('Query params cannot be passed when url already contains "?" or "#".')
}
Expand Down Expand Up @@ -689,7 +694,7 @@ module.exports = {
validateHandler,
getSocketInfo,
isFormDataLike,
buildURL,
serializePathWithQuery,
addAbortListener,
isValidHTTPToken,
isValidHeaderValue,
Expand Down
4 changes: 2 additions & 2 deletions lib/mock/mock-interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const {
kMockDispatch
} = require('./mock-symbols')
const { InvalidArgumentError } = require('../core/errors')
const { buildURL } = require('../core/util')
const { serializePathWithQuery } = require('../core/util')

/**
* Defines the scope API for an interceptor reply
Expand Down Expand Up @@ -72,7 +72,7 @@ class MockInterceptor {
// fragments to servers when they retrieve a document,
if (typeof opts.path === 'string') {
if (opts.query) {
opts.path = buildURL(opts.path, opts.query)
opts.path = serializePathWithQuery(opts.path, opts.query)
} else {
// Matches https://github.com/nodejs/undici/blob/main/lib/web/fetch/index.js#L1811
const parsedURL = new URL(opts.path, 'data://')
Expand Down
4 changes: 2 additions & 2 deletions lib/mock/mock-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {
kOrigin,
kGetNetConnect
} = require('./mock-symbols')
const { buildURL } = require('../core/util')
const { serializePathWithQuery } = require('../core/util')
const { STATUS_CODES } = require('node:http')
const {
types: {
Expand Down Expand Up @@ -126,7 +126,7 @@ function getResponseData (data) {
}

function getMockDispatch (mockDispatches, key) {
const basePath = key.query ? buildURL(key.path, key.query) : key.path
const basePath = key.query ? serializePathWithQuery(key.path, key.query) : key.path
const resolvedPath = typeof basePath === 'string' ? safeUrl(basePath) : basePath

// Match path
Expand Down
4 changes: 2 additions & 2 deletions test/node-test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ test('parseRawHeaders', () => {
assert.deepEqual(util.parseRawHeaders(['content-length', 'value', 'content-disposition', 'form-data; name="fieldName"']), ['content-length', 'value', 'content-disposition', 'form-data; name="fieldName"'])
})

test('buildURL', () => {
test('serializePathWithQuery', () => {
const tests = [
[{ id: BigInt(123456) }, 'id=123456'],
[{ date: new Date() }, 'date='],
Expand All @@ -111,7 +111,7 @@ test('buildURL', () => {

for (const [input, output] of tests) {
const expected = `${base}${output ? `?${output}` : output}`
assert.deepEqual(util.buildURL(base, input), expected)
assert.deepEqual(util.serializePathWithQuery(base, input), expected)
}
})

Expand Down

0 comments on commit 89a46dd

Please sign in to comment.