@ministryofjustice/hmpps-connect-dps-shared-items 3.4.0 → 3.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,18 +10,26 @@ export const abortError = 'AbortError'
10
10
  export const abortTimeout = 'AbortTimeout'
11
11
 
12
12
  class AbortableFetch {
13
- constructor(url) {
13
+ constructor(url, options) {
14
14
  this.url = url
15
15
  this.controller = new window.AbortController()
16
16
  this.status = FetchStatus.unsent
17
+ this.fetchDelay = options?.fetchDelay || 500
17
18
  }
18
19
 
19
20
  async send() {
20
21
  this.status = FetchStatus.loading
21
22
  try {
23
+ // Abortable delay before querying the API to limit the rate of requests when someone is typing:
24
+ await new Promise((resolve) => {
25
+ setTimeout(resolve, this.fetchDelay)
26
+ this.controller.signal.addEventListener('abort', () => { resolve() }, { once: true })
27
+ })
28
+
22
29
  setTimeout(() => {
23
30
  this.controller.abort(abortTimeout)
24
31
  }, 5000)
32
+
25
33
  const response = await window.fetch(this.url, { signal: this.controller.signal })
26
34
 
27
35
  if (!(+response.status === 200)) {
@@ -13,6 +13,7 @@ export class AutosuggestAddress {
13
13
  this.container = context.querySelector(`.${classInputContainer}`)
14
14
  this.uprn = context.querySelector(`.${classUPRN}`)
15
15
  this.findUrl = this.container.getAttribute('data-find-url')
16
+ this.fetchDelay = this.container.getAttribute('data-fetch-delay')
16
17
 
17
18
  // State:
18
19
  this.fetch = null
@@ -29,7 +30,7 @@ export class AutosuggestAddress {
29
30
  }
30
31
 
31
32
  async checkAPIStatus() {
32
- this.fetch = abortableFetch(`${this.findUrl}/SW1H9AJ`)
33
+ this.fetch = abortableFetch(`${this.findUrl}/SW1H9AJ`, { fetchDelay: this.fetchDelay })
33
34
 
34
35
  try {
35
36
  const response = await this.fetch.send()
@@ -56,7 +57,7 @@ export class AutosuggestAddress {
56
57
  }
57
58
 
58
59
  async findAddress(query) {
59
- this.fetch = abortableFetch(`${this.findUrl}/${query}`)
60
+ this.fetch = abortableFetch(`${this.findUrl}/${query}`, { fetchDelay: this.fetchDelay })
60
61
  const data = await this.fetch.send()
61
62
  const response = await data.json()
62
63
  const status = response.status
@@ -16,6 +16,7 @@
16
16
  data-error-api="The address search is not working right now. Enter the address manually to continue."
17
17
  data-styling-base-class="hmpps-address-autosuggest"
18
18
  data-find-url="{{ options.findUrl or "/api/addresses/find" }}"
19
+ data-fetch-delay="{{ options.fetchDelay or 500 }}"
19
20
  >
20
21
  {{ govukInput({
21
22
  id: 'address-autosuggest-input',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ministryofjustice/hmpps-connect-dps-shared-items",
3
- "version": "3.4.0",
3
+ "version": "3.5.0",
4
4
  "description": "A package to share Connect DPS functionality across applications",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.esm.js",
@@ -64,7 +64,7 @@
64
64
  "jest-junit": "^16.0.0",
65
65
  "lint-staged": "^16.2.7",
66
66
  "nock": "^14.0.10",
67
- "rollup": "4.55.3",
67
+ "rollup": "4.56.0",
68
68
  "rollup-plugin-dts": "^6.3.0",
69
69
  "ts-jest": "^29.4.6",
70
70
  "tslib": "^2.8.1",