Skip to content

Commit

Permalink
Organize url helpers in a single file and rename "mergeQueryStringsWi…
Browse files Browse the repository at this point in the history
…thData" to "mergeQueryStringWithData"
  • Loading branch information
reinink committed Oct 19, 2020
1 parent 7b8b88c commit 9ec4a36
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 41 deletions.
4 changes: 2 additions & 2 deletions packages/inertia-react/src/Link.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hrefToUrl, Inertia, mergeQueryStringsWithData, shouldIntercept } from '@inertiajs/inertia'
import { hrefToUrl, Inertia, mergeQueryStringWithData, shouldIntercept } from '@inertiajs/inertia'
import { createElement, useCallback, forwardRef } from 'react'

const noop = () => undefined
Expand Down Expand Up @@ -65,7 +65,7 @@ export default forwardRef(function InertiaLink({
],
)

const [url, _data] = mergeQueryStringsWithData(method, hrefToUrl(href), data)
const [url, _data] = mergeQueryStringWithData(method, hrefToUrl(href), data)
href = url.href
data = _data

Expand Down
4 changes: 2 additions & 2 deletions packages/inertia-vue/src/link.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hrefToUrl, Inertia, mergeQueryStringsWithData, shouldIntercept } from '@inertiajs/inertia'
import { hrefToUrl, Inertia, mergeQueryStringWithData, shouldIntercept } from '@inertiajs/inertia'

export default {
functional: true,
Expand Down Expand Up @@ -48,7 +48,7 @@ export default {
...(data.on || {}),
}

let [url, propsData] = mergeQueryStringsWithData(
let [url, propsData] = mergeQueryStringWithData(
props.method,
hrefToUrl(props.href),
props.data,
Expand Down
4 changes: 2 additions & 2 deletions packages/inertia-vue3/src/link.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { h } from 'vue'
import { hrefToUrl, Inertia, mergeQueryStringsWithData, shouldIntercept } from '@inertiajs/inertia'
import { hrefToUrl, Inertia, mergeQueryStringWithData, shouldIntercept } from '@inertiajs/inertia'

export default {
props: {
Expand Down Expand Up @@ -38,7 +38,7 @@ export default {
},
setup(props, { slots, attrs }) {
return props => {
let [url, data] = mergeQueryStringsWithData(
let [url, data] = mergeQueryStringWithData(
props.method,
hrefToUrl(props.href),
props.data,
Expand Down
13 changes: 0 additions & 13 deletions packages/inertia/src/hrefToUrl.js

This file was deleted.

4 changes: 1 addition & 3 deletions packages/inertia/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export { default as hrefToUrl } from './hrefToUrl'
export { default as Inertia } from './inertia'
export { default as mergeQueryStringsWithData } from './mergeQueryStringsWithData'
export { default as shouldIntercept } from './shouldIntercept'
export { default as urlWithoutHash } from './urlWithoutHash'
export { hrefToUrl, mergeQueryStringWithData, urlWithoutHash } from './url'
6 changes: 2 additions & 4 deletions packages/inertia/src/inertia.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import Axios from 'axios'
import debounce from './debounce'
import hrefToUrl from './hrefToUrl'
import mergeQueryStringsWithData from './mergeQueryStringsWithData'
import modal from './modal'
import urlWithoutHash from './urlWithoutHash'
import { hrefToUrl, mergeQueryStringWithData, urlWithoutHash } from './url'

export default {
resolveComponent: null,
Expand Down Expand Up @@ -165,7 +163,7 @@ export default {
onCancel = () => ({}),
onSuccess = () => ({}),
} = {}) {
[url, data] = mergeQueryStringsWithData(method, hrefToUrl(url), data)
[url, data] = mergeQueryStringWithData(method, hrefToUrl(url), data)

let visit = { url, ...arguments[1] }
if (onStart(visit) === false || !this.fireEvent('start', { cancelable: true, detail: { visit } } )) {
Expand Down
10 changes: 0 additions & 10 deletions packages/inertia/src/mergeQueryStringsWithData.js

This file was deleted.

28 changes: 28 additions & 0 deletions packages/inertia/src/url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import qs from 'qs'
import deepmerge from 'deepmerge'

export function hrefToUrl(href) {
try {
if (href.startsWith('#')) {
return new URL(`${urlWithoutHash(window.location)}${href}`)
} else {
return new URL(href)
}
} catch (error) {
return new URL(`${window.location.origin}${href}`)
}
}

export function mergeQueryStringWithData(method, url, data) {
if (method === 'get' && Object.keys(data).length) {
url.search = qs.stringify(deepmerge(qs.parse(url.search, { ignoreQueryPrefix: true }), data))
data = {}
}
return [url, data]
}

export function urlWithoutHash(url) {
url = new URL(url.href)
url.hash = ''
return url
}
5 changes: 0 additions & 5 deletions packages/inertia/src/urlWithoutHash.js

This file was deleted.

0 comments on commit 9ec4a36

Please sign in to comment.