Skip to content

Commit fd254a6

Browse files
authored
feat: allow using window.location on HTTP (#11186)
* feat: allow using .location on HTTP * refactor: also pick the Location href/pathname/hostname * fix cfg.body[k] getting overridden * don't use Window to not need DOM * use window().location.get('href') helper * amend changelog
1 parent f89f02a commit fd254a6

File tree

5 files changed

+67
-5
lines changed

5 files changed

+67
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88

99
HTTP input now accepts `field('..')` references in the HTTP body definition.
1010

11+
## 1.9.2
12+
13+
### New features
14+
15+
- Toolkit now exports `window().location.get` to country config that can be used as a template variable e.g. in HttpField request body.
16+
1117
## 1.9.1
1218

1319
### Breaking changes

packages/client/src/v2-events/features/events/registered-fields/Http.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,11 @@ async function fetchHttpFieldValue(
7171
for (const [k, v] of Object.entries(cfg.body)) {
7272
if (isTemplateVariable(v)) {
7373
cfg.body[k] = getMixedPath(systemVariables, v)
74-
}
75-
76-
if (isFieldReference(v)) {
74+
} else if (isFieldReference(v)) {
7775
cfg.body[k] = parseFieldReferenceToValue(v, form)
76+
} else {
77+
cfg.body[k] = v
7878
}
79-
cfg.body[k] = v
8079
}
8180
}
8281

packages/client/src/v2-events/hooks/useSystemVariables.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
1010
*/
1111

12+
import { pick } from 'lodash'
1213
import { SystemVariables } from '@opencrvs/commons/client'
1314
import { useUserDetails } from './useUserDetails'
1415

@@ -19,7 +20,10 @@ export function useSystemVariables() {
1920
const user = useUserDetails()
2021

2122
const variables = {
22-
$user: user
23+
$user: user,
24+
$window: {
25+
location: pick(window.location, ['href', 'pathname', 'hostname'])
26+
}
2327
} satisfies SystemVariables
2428

2529
return variables

packages/client/src/v2-events/utils.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,13 @@ const testCases = [
198198
$user: {
199199
district: '',
200200
province: ''
201+
},
202+
$window: {
203+
location: {
204+
href: 'http://example.com',
205+
pathname: '/path',
206+
hostname: 'example.com'
207+
}
201208
}
202209
},
203210
expected: undefined,
@@ -210,6 +217,13 @@ const testCases = [
210217
$user: {
211218
district: '',
212219
province: ''
220+
},
221+
$window: {
222+
location: {
223+
href: 'http://example.com',
224+
pathname: '/path',
225+
hostname: 'example.com'
226+
}
213227
}
214228
},
215229
expected: 'Hello',
@@ -222,6 +236,13 @@ const testCases = [
222236
$user: {
223237
district: 'Ibombo',
224238
province: ''
239+
},
240+
$window: {
241+
location: {
242+
href: 'http://example.com',
243+
pathname: '/path',
244+
hostname: 'example.com'
245+
}
225246
}
226247
},
227248
expected: 'Ibombo',
@@ -234,6 +255,13 @@ const testCases = [
234255
$user: {
235256
district: 'Ibombo',
236257
province: ''
258+
},
259+
$window: {
260+
location: {
261+
href: 'http://example.com',
262+
pathname: '/path',
263+
hostname: 'example.com'
264+
}
237265
}
238266
},
239267
expected: 'Hello world',
@@ -249,6 +277,13 @@ const testCases = [
249277
$user: {
250278
district: 'Ibombo',
251279
province: 'Central'
280+
},
281+
$window: {
282+
location: {
283+
href: 'http://example.com',
284+
pathname: '/path',
285+
hostname: 'example.com'
286+
}
252287
}
253288
},
254289
expected: {

packages/commons/src/events/TemplateConfig.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,26 @@ export type SystemVariables = {
2424
province: string
2525
district: string
2626
}
27+
$window: {
28+
location: {
29+
href: string
30+
pathname: string
31+
hostname: string
32+
}
33+
}
2734
}
2835

36+
/**
37+
* Resolves `window().location.get('href')` to `window.location.href` to allow us to 1) type check system variables 2) change the implementation later if needed
38+
*/
39+
export const window = () => ({
40+
location: {
41+
get: (key: 'href' | 'pathname' | 'hostname') => {
42+
return `$window.location.${key}`
43+
}
44+
}
45+
})
46+
2947
/**
3048
* Recursively flatten the keys of an object. Used to limit types when configuring default values in country config.
3149
* @example

0 commit comments

Comments
 (0)