Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
frontend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
frontend
Commits
d9ea46f6
Commit
d9ea46f6
authored
Feb 08, 2024
by
tom
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[skip ci] support array-like query params in API resource calls
parent
0a8a82e6
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
1 deletion
+50
-1
buildUrl.test.ts
lib/api/buildUrl.test.ts
+43
-0
buildUrl.ts
lib/api/buildUrl.ts
+7
-1
No files found.
lib/api/buildUrl.test.ts
0 → 100644
View file @
d9ea46f6
import
buildUrl
from
'
./buildUrl
'
;
test
(
'
builds URL for resource without path params
'
,
()
=>
{
const
url
=
buildUrl
(
'
config_backend_version
'
);
expect
(
url
).
toBe
(
'
https://localhost:3003/api/v2/config/backend-version
'
);
});
test
(
'
builds URL for resource with path params
'
,
()
=>
{
const
url
=
buildUrl
(
'
block
'
,
{
height_or_hash
:
'
42
'
});
expect
(
url
).
toBe
(
'
https://localhost:3003/api/v2/blocks/42
'
);
});
describe
(
'
falsy query parameters
'
,
()
=>
{
test
(
'
leaves "false" as query parameter
'
,
()
=>
{
const
url
=
buildUrl
(
'
block
'
,
{
height_or_hash
:
'
42
'
},
{
includeTx
:
false
});
expect
(
url
).
toBe
(
'
https://localhost:3003/api/v2/blocks/42?includeTx=false
'
);
});
test
(
'
leaves "null" as query parameter
'
,
()
=>
{
const
url
=
buildUrl
(
'
block
'
,
{
height_or_hash
:
'
42
'
},
{
includeTx
:
null
});
expect
(
url
).
toBe
(
'
https://localhost:3003/api/v2/blocks/42?includeTx=null
'
);
});
test
(
'
strips out empty string as query parameter
'
,
()
=>
{
const
url
=
buildUrl
(
'
block
'
,
{
height_or_hash
:
'
42
'
},
{
includeTx
:
null
,
sort
:
''
});
expect
(
url
).
toBe
(
'
https://localhost:3003/api/v2/blocks/42?includeTx=null
'
);
});
test
(
'
strips out "undefined" as query parameter
'
,
()
=>
{
const
url
=
buildUrl
(
'
block
'
,
{
height_or_hash
:
'
42
'
},
{
includeTx
:
null
,
sort
:
undefined
});
expect
(
url
).
toBe
(
'
https://localhost:3003/api/v2/blocks/42?includeTx=null
'
);
});
});
test
(
'
builds URL with array-like query parameters
'
,
()
=>
{
const
url
=
buildUrl
(
'
block
'
,
{
height_or_hash
:
'
42
'
},
{
includeTx
:
[
'
0x11
'
,
'
0x22
'
],
sort
:
'
asc
'
});
expect
(
url
).
toBe
(
'
https://localhost:3003/api/v2/blocks/42?includeTx%5B0%5D=0x11&includeTx%5B1%5D=0x22&sort=asc
'
);
});
test
(
'
builds URL for resource with custom API endpoint
'
,
()
=>
{
const
url
=
buildUrl
(
'
token_verified_info
'
,
{
chainId
:
'
42
'
,
hash
:
'
0x11
'
});
expect
(
url
).
toBe
(
'
https://localhost:3005/api/v1/chains/42/token-infos/0x11
'
);
});
lib/api/buildUrl.ts
View file @
d9ea46f6
...
...
@@ -19,7 +19,13 @@ export default function buildUrl<R extends ResourceName>(
queryParams
&&
Object
.
entries
(
queryParams
).
forEach
(([
key
,
value
])
=>
{
// there are some pagination params that can be null or false for the next page
value
!==
undefined
&&
value
!==
''
&&
url
.
searchParams
.
append
(
key
,
String
(
value
));
if
(
value
!==
undefined
&&
value
!==
''
)
{
if
(
Array
.
isArray
(
value
))
{
value
.
forEach
((
v
,
i
)
=>
url
.
searchParams
.
append
(
`
${
key
}
[
${
i
}
]`
,
String
(
v
)));
}
else
{
url
.
searchParams
.
append
(
key
,
String
(
value
));
}
}
});
return
url
.
toString
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment