Skip to content

Commit 754204d

Browse files
authored
Merge pull request #5 from yuriifabirovskyi/master
Fix query param value undefined, fix empty hash, add some tests
2 parents 7d0b547 + c17af34 commit 754204d

File tree

2 files changed

+123
-41
lines changed

2 files changed

+123
-41
lines changed

src/index.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,14 @@ export class UrlBuilder {
6161
this.baseUrl = baseUrl ?? ""
6262
}
6363
public buildHash(url: string, hash: string | number): string {
64-
if (url.match(/#.+/gi)) return url.replace(/#.+/gi, `#${hash}`)
65-
return `${url}#${hash}`
64+
hash = hash ? `#${hash}` : ''
65+
if (url.match(/#.+/gi)) return url.replace(/#.+/gi, `${hash}`)
66+
return `${url}${hash}`
6667
}
6768
public buildPathVariables(url: string, pathVariables: IPathVariables): string {
6869
Object.keys(pathVariables).forEach((_, index) => {
6970
const value = Object.values(pathVariables)[index]
70-
url = url.replace(/:([^\/]+)/gi, isReactive(value) ? (value as Ref).value : value.toString())
71+
url = url.replace(/:([^\/]+)/gi, isReactive(value) ? (value as Ref).value : value?.toString())
7172
})
7273
return url
7374
}
@@ -76,12 +77,9 @@ export class UrlBuilder {
7677
const params = Object.keys(queryParams)
7778
.map((key, index) => {
7879
const param = Object.values(queryParams)[index]
79-
switch (typeof key) {
80-
default:
81-
return `${key}=${param}`
82-
case "object":
83-
return this.buildCSV(key, param, disableCSV)
84-
}
80+
if (param === null || param === undefined) return undefined
81+
if (param instanceof Array) return this.buildCSV(key, param, disableCSV)
82+
return `${key}=${param}`
8583
})
8684
.flat()
8785
.filter(x => !!x)

test/test.ts

Lines changed: 116 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,123 @@ import { useDebounce } from "@vueuse/core"
22
import { useUrl, createUseUrlInstance } from "../src/index"
33
import { ref, reactive, set } from "vue-demi"
44

5-
const search = ref("query")
6-
const filters = ref(["filter1", "filter2", "filter3"])
5+
const urlBuilder = createUseUrlInstance()
76

8-
const params = reactive({
9-
search: "query",
10-
param: "something",
11-
})
7+
test('Path variables test', () => {
8+
const { url } = urlBuilder(
9+
{
10+
path: "/api/v1/entity/:id",
11+
pathVariables: {
12+
id: ref(1),
13+
},
14+
},
15+
"https://somedomain.com/",
16+
)
17+
expect(url.value).toBe('https://somedomain.com/api/v1/entity/1');
18+
});
19+
20+
21+
test('Query params test', () => {
22+
const { url } = urlBuilder(
23+
{
24+
path: "/api/v1/entity/list",
25+
queryParams: {
26+
string: ref('query'),
27+
boolean: false,
28+
someUndefined: undefined,
29+
someNull: null,
30+
},
31+
},
32+
"https://somedomain.com/",
33+
)
34+
expect(url.value).toBe('https://somedomain.com/api/v1/entity/list?string=query&boolean=false');
35+
});
36+
37+
38+
test('Query params CSV false test', () => {
39+
const { url } = urlBuilder(
40+
{
41+
path: "/api/v1/entity/list",
42+
queryParams: {
43+
string: ['query1', 'query2', 'query3'],
44+
},
45+
// disableCSV: false
46+
},
47+
"https://somedomain.com/",
48+
)
49+
expect(url.value).toBe('https://somedomain.com/api/v1/entity/list?string=query1,query2,query3');
50+
});
51+
52+
53+
test('Query params CSV true test', () => {
54+
const { url } = urlBuilder(
55+
{
56+
path: "/api/v1/entity/list",
57+
queryParams: {
58+
string: ['query1', 'query2', 'query3'],
59+
},
60+
disableCSV: true
61+
},
62+
"https://somedomain.com/",
63+
)
64+
expect(url.value).toBe('https://somedomain.com/api/v1/entity/list?string=query1&string=query2&string=query3');
65+
});
66+
67+
68+
test('Hash test', () => {
69+
const { url } = urlBuilder(
70+
{
71+
path: "/api/v1/entity/list",
72+
hash: "hash",
73+
},
74+
"https://somedomain.com/",
75+
)
76+
expect(url.value).toBe('https://somedomain.com/api/v1/entity/list#hash');
77+
});
78+
79+
80+
test('All together test', () => {
81+
const { url } = urlBuilder(
82+
{
83+
path: "/api/v1/entity/:id",
84+
pathVariables: {
85+
id: ref(1),
86+
},
87+
queryParams: {
88+
string: ref('query'),
89+
boolean: false,
90+
someUndefined: undefined,
91+
someNull: null,
92+
},
93+
hash: "hash",
94+
},
95+
"https://somedomain.com/",
96+
)
97+
expect(url.value).toBe('https://somedomain.com/api/v1/entity/1?string=query&boolean=false#hash');
98+
});
1299

13-
const urlBuilder = createUseUrlInstance()
14100

15-
const { url, queryParams, pathVariables, hash, path, disableCSV } = urlBuilder(
16-
{
17-
path: "/api/v1/entity/:id/subentity",
18-
pathVariables: {
19-
id: 1,
101+
test('Reactivity test', () => {
102+
const id = ref(1)
103+
const queryParam = ref('query')
104+
const { url } = urlBuilder(
105+
{
106+
path: "/api/v1/entity/:id",
107+
pathVariables: {
108+
id: id,
109+
},
110+
queryParams: {
111+
string: queryParam,
112+
boolean: false,
113+
someUndefined: undefined,
114+
someNull: null,
115+
},
116+
hash: "hash",
20117
},
21-
queryParams: params,
22-
hash: "someHash",
23-
},
24-
"https://somedomain.com/",
25-
)
26-
27-
console.log(url.value)
28-
29-
set(params, "s", ref("extra"))
30-
setInterval(() => {
31-
pathVariables.id = (pathVariables.id as number) + 1
32-
console.log(url.value)
33-
}, 1000)
34-
35-
/*search.value = "newQuery"
36-
filters.value = ["newFilter1" , "newFilter"]
37-
38-
setTimeout(() => {
39-
console.log(url.value)
40-
},500)*/
118+
"https://somedomain.com/",
119+
)
120+
expect(url.value).toBe('https://somedomain.com/api/v1/entity/1?string=query&boolean=false#hash');
121+
id.value = 2
122+
queryParam.value = 'query2'
123+
expect(url.value).toBe('https://somedomain.com/api/v1/entity/2?string=query2&boolean=false#hash');
124+
});

0 commit comments

Comments
 (0)