Skip to content

Commit

Permalink
remove query param from URL when its value is default
Browse files Browse the repository at this point in the history
  • Loading branch information
tsnobip committed Jan 6, 2025
1 parent 01a927c commit becb82c
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 20 deletions.
8 changes: 8 additions & 0 deletions .changeset/kind-rings-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@rescript-relay-router-example/client-rendering": minor
"@rescript-relay-router-example/express": minor
"rescript-relay-router": minor
"@rescript-relay-router/utils": minor
---

remove query param from URL when its value is default
5 changes: 0 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ jobs:
- name: Build all projects
run: |
yarn build
# Since we don't want to have generated code formatted in the router we regenerate
# the router code after formatting.
- name: Sync router code generation
run: |
yarn workspaces foreach --topological-dev run router:generate
- name: git status
id: git_status
shell: bash
Expand Down
2 changes: 1 addition & 1 deletion examples/client-rendering/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"type": "module",
"packageManager": "[email protected]",
"scripts": {
"build": "run-s 'build:*'",
"build": "yarn router:generate && run-s 'build:*'",
"build:relay": "rescript-relay-compiler",
"build:rescript": "rescript build -with-deps",
"build:vite": "vite build --outDir dist/client",
Expand Down

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

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

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

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

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

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

5 changes: 5 additions & 0 deletions examples/client-rendering/test/UrlEncodingDecoding.test.res
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ describe("makeLink", () => {
let link = Routes.Root.Todos.Route.makeLink(~byValue="/incorrect value, for url")
expect(link)->Expect.toBe("/todos?byValue=%2Fincorrect%20value%2C%20for%20url")
})

test("should omit query param when value is default value", _t => {
let link = Routes.Root.Todos.Route.makeLink(~statusWithDefault=NotCompleted)
expect(link)->Expect.toBe("/todos")
})
})

describe("parsing", () => {
Expand Down
2 changes: 1 addition & 1 deletion examples/express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"type": "module",
"packageManager": "[email protected]",
"scripts": {
"build": "run-s 'build:*'",
"build": "yarn router:generate && run-s 'build:*'",
"build:relay": "rescript-relay-compiler",
"build:rescript": "rescript build -with-deps",
"build:vite": "run-s 'build:vite:*'",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ let routePattern = "${route.path->RoutePath.toPattern}"
queryParamSerializers->Array.forEach(((key, serializer, paramType)) => {
let serializerStr = `queryParams->QueryParams.${switch paramType {
| Array(_) => "setParamArray"
| CustomModule({required: true}) => "setParamOpt"
| _ => "setParam"
}}(~key="${key->SafeParam.getOriginalKey}", ~value=${serializer})`

Expand Down Expand Up @@ -231,7 +232,7 @@ let applyQueryParams = (
~variableName=key,
)})))`
| CustomModule({required: true}) =>
`\n queryParams->QueryParams.setParam(~key="${key}", ~value=${queryParam->Utils.QueryParams.toSerializer(
`\n queryParams->QueryParams.setParamOpt(~key="${key}", ~value=${queryParam->Utils.QueryParams.toSerializer(
~variableName=`newParams.${key}`,
)})`
| queryParam =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ module QueryParams = {
| Boolean => `switch ${variableName} { | true => "true" | false => "false" }`
| Int => `Int.toString(${variableName})`
| Float => `Float.toString(${variableName})`
| CustomModule({moduleName, required: true}) =>
`${variableName} == ${moduleName}.defaultValue ? None : ${variableName}->${moduleName}.serialize->Some`
| CustomModule({moduleName}) => `${variableName}->${moduleName}.serialize`
| Array(inner) =>
switch inner {
Expand Down

0 comments on commit becb82c

Please sign in to comment.