Skip to content

Commit

Permalink
fix: replace url-parse with native URL
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Jan 11, 2023
1 parent f71c67f commit f6317e3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 74 deletions.
9 changes: 8 additions & 1 deletion README.md
@@ -1,6 +1,9 @@
# get-it

[![npm version](http://img.shields.io/npm/v/get-it.svg)](https://www.npmjs.com/package/get-it) [![CI & Release](https://github.com/sanity-io/get-it/actions/workflows/ci.yml/badge.svg)](https://github.com/sanity-io/get-it/actions/workflows/ci.yml)
[![npm stat](https://img.shields.io/npm/dm/get-it.svg?style=flat-square)](https://npm-stat.com/charts.html?package=get-it)
[![npm version](https://img.shields.io/npm/v/get-it.svg?style=flat-square)](https://www.npmjs.com/package/get-it)
[![gzip size][gzip-badge]][bundlephobia]
[![size][size-badge]][bundlephobia]

Generic HTTP request library for node.js (>= 12) and browsers (IE9 and newer)

Expand Down Expand Up @@ -190,3 +193,7 @@ Run the ["CI & Release" workflow](https://github.com/sanity-io/get-it/actions).
Make sure to select the main branch and check "Release new version".

Semantic release will only release on configured branches, so it is safe to run release on any branch.

[gzip-badge]: https://img.shields.io/bundlephobia/minzip/get-it?label=gzip%20size&style=flat-square
[size-badge]: https://img.shields.io/bundlephobia/min/get-it?label=size&style=flat-square
[bundlephobia]: https://bundlephobia.com/package/get-it
29 changes: 1 addition & 28 deletions package-lock.json

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

4 changes: 1 addition & 3 deletions package.json
Expand Up @@ -107,8 +107,7 @@
"is-stream": "^2.0.1",
"parse-headers": "^2.0.5",
"progress-stream": "^2.0.0",
"tunnel-agent": "^0.6.0",
"url-parse": "^1.5.10"
"tunnel-agent": "^0.6.0"
},
"devDependencies": {
"@edge-runtime/vm": "^2.0.2",
Expand All @@ -118,7 +117,6 @@
"@types/follow-redirects": "^1.14.1",
"@types/node": "^18.11.18",
"@types/progress-stream": "^2.0.2",
"@types/url-parse": "^1.4.8",
"@typescript-eslint/eslint-plugin": "^5.48.1",
"@typescript-eslint/parser": "^5.48.1",
"@vitest/coverage-c8": "^0.27.0",
Expand Down
55 changes: 15 additions & 40 deletions src/middleware/defaultOptionsProcessor.ts
@@ -1,8 +1,5 @@
import urlParse from 'url-parse'

const isReactNative = typeof navigator === 'undefined' ? false : navigator.product === 'ReactNative'

const has = Object.prototype.hasOwnProperty
const defaultOptions = {timeout: isReactNative ? 60000 : 120000}

/** @public */
Expand All @@ -12,50 +9,38 @@ export function processOptions(opts: any): any {
? Object.assign({url: opts}, defaultOptions)
: Object.assign({}, defaultOptions, opts)

// Parse URL into parts
const url = urlParse(
options.url,
{}, // Don't use current browser location
true // Parse query strings
)
// Allow parsing relativ URLs by setting the origin
const url = new URL(options.url, 'http://localhost')

// Normalize timeouts
options.timeout = normalizeTimeout(options.timeout)

// Shallow-merge (override) existing query params
if (options.query) {
Object.assign(url.query, removeUndefined(options.query))
for (const [key, value] of Object.entries(options.query)) {
if (value !== undefined) {
if (Array.isArray(value)) {
for (const v of value) {
url.searchParams.append(key, v as string)
}
} else {
url.searchParams.append(key, value as string)
}
}
}
}

// Implicit POST if we have not specified a method but have a body
options.method =
options.body && !options.method ? 'POST' : (options.method || 'GET').toUpperCase()

// Stringify URL
options.url = url.toString(stringifyQueryString)
options.url =
url.origin === 'http://localhost' ? `${url.pathname}?${url.searchParams}` : url.toString()

return options
}

function stringifyQueryString(obj: any) {
const pairs: any[] = []
for (const key in obj) {
if (has.call(obj, key)) {
push(key, obj[key])
}
}

return pairs.length ? pairs.join('&') : ''

function push(key: any, val: any) {
if (Array.isArray(val)) {
val.forEach((item) => push(key, item))
} else {
pairs.push([key, val].map(encodeURIComponent).join('='))
}
}
}

function normalizeTimeout(time: any): any {
if (time === false || time === 0) {
return false
Expand All @@ -72,13 +57,3 @@ function normalizeTimeout(time: any): any {

return {connect: delay, socket: delay}
}

function removeUndefined(obj: any) {
const target: any = {}
for (const key in obj) {
if (obj[key] !== undefined) {
target[key] = obj[key]
}
}
return target
}
3 changes: 1 addition & 2 deletions test-deno/import_map.json
Expand Up @@ -4,7 +4,6 @@
"debug": "https://esm.sh/debug@^4.3.4",
"form-urlencoded": "https://esm.sh/form-urlencoded@^6.1.0",
"is-plain-object": "https://esm.sh/is-plain-object@^5.0.0",
"parse-headers": "https://esm.sh/parse-headers@^2.0.5",
"url-parse": "https://esm.sh/url-parse@^1.5.10"
"parse-headers": "https://esm.sh/parse-headers@^2.0.5"
}
}

0 comments on commit f6317e3

Please sign in to comment.