Skip to content

Commit 99eb16c

Browse files
committed
Process unavailable properties as well.
1 parent 23b82e8 commit 99eb16c

3 files changed

Lines changed: 29 additions & 16 deletions

File tree

src/app/Listeners/AppInitedListener.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class AppInitedListener extends AbstractListener {
3434
this.onSearchProperties.bind(this)
3535
)
3636
.interceptSearchAll(
37-
this.updateCustomUrlToSearchAll.bind(this)
37+
this.onAllSearchProperties.bind(this)
3838
)
3939
}
4040

@@ -50,16 +50,16 @@ export class AppInitedListener extends AbstractListener {
5050
private onSearchProperties (search: HostelworldSearch): HostelworldSearch {
5151
const adapted: HostelworldSearch = SearchDataAdapter.stripPromotions(search)
5252

53-
this.emit('hostelworld:search:intercepted', adapted)
53+
this.emit('hostelworld:search:intercepted', adapted.properties)
5454

5555
return adapted
5656
}
5757

58-
private updateCustomUrlToSearchAll (url: URL): URL {
59-
url.searchParams.delete('guests')
60-
url.searchParams.delete('num-nights')
61-
url.searchParams.delete('date-start')
58+
private onAllSearchProperties (search: HostelworldSearch): HostelworldSearch {
59+
const adapted: HostelworldSearch = SearchDataAdapter.stripPromotions(search)
6260

63-
return url
61+
this.emit('hostelworld:search:intercepted', adapted.properties)
62+
63+
return adapted
6464
}
6565
}

src/app/Listeners/PropertiesInterceptedListener.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import type { HostelworldSearch } from 'Types/HostelworldSearch'
1+
import type { Property } from 'Types/HostelworldSearch'
22
import { Search } from 'DTOs/Search'
33
import { Subscribe } from 'Core/EventBus'
44
import { AbstractListener } from './AbstractListener'
55

66
@Subscribe('hostelworld:search:intercepted')
77
export class PropertiesInterceptedListener extends AbstractListener {
8-
public async handle (searchResponse: HostelworldSearch): Promise<void> {
8+
public async handle (properties: Property[]): Promise<void> {
99
const search: Search | undefined = this.latestSearchInSession()
1010
if (!search) throw new Error('There is no search in session to properly compose the properties.')
1111

12-
for (const property of searchResponse.properties) {
12+
for (const property of properties) {
1313
this.emit('worker:task:dispatch', 'compose:property', property, search)
1414
}
1515
}

src/app/Services/Hostelworld/SearchApiRequestsInterceptor.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ import { XHRRequestInterceptor } from 'Utils/XHRRequestInterceptor'
44
export class SearchApiRequestsInterceptor {
55
private static searchPropertiesRegex: RegExp = /cities\/\d+\/properties\/\?.*(?=date-start=(?!1943-04-19))/
66
private static searchAllPropertiesRegex: RegExp = /cities\/\d+\/properties\/\?.*date-start=1943-04-19/
7+
private static searchAllPropertiesWithoutDateStartRegex: RegExp = /cities\/\d+\/properties\/(?!.*[?&]date-start=)/
78

89
public static interceptSearch (
910
UrlCallback: Callback<URL>,
1011
responseCallback: Callback<HostelworldSearch>
1112
): typeof this {
1213
const parseUrlWithCallback: Callback<string> = (url: string): string => {
1314
const parsed: URL = new URL(url)
14-
UrlCallback(parsed)
1515

16-
return parsed.toString()
16+
return UrlCallback(parsed).toString()
1717
}
1818

1919
const parseResponseWithCallback: Callback<string> = (response: string): string => {
@@ -33,16 +33,29 @@ export class SearchApiRequestsInterceptor {
3333
return this
3434
}
3535

36-
public static interceptSearchAll (UrlCallback: Callback<URL>): typeof this {
37-
const parseUrlWithCallback: Callback<string> = (url: string): string => {
36+
public static interceptSearchAll (responseCallback: Callback<HostelworldSearch>): typeof this {
37+
const updateCustomUrlToSearchAll: Callback<string> = (url: string): string => {
3838
const parsed: URL = new URL(url)
39+
parsed.searchParams.delete('guests')
40+
parsed.searchParams.delete('num-nights')
41+
parsed.searchParams.delete('date-start')
3942

40-
return UrlCallback(parsed).toString()
43+
return parsed.toString()
44+
}
45+
46+
const parseResponseWithCallback: Callback<string> = (response: string): string => {
47+
const parsed: HostelworldSearch = JSON.parse(response)
48+
49+
return JSON.stringify(responseCallback(parsed))
4150
}
4251

4352
XHRRequestInterceptor
4453
.intercept({ url: this.searchAllPropertiesRegex })
45-
.withUrl(parseUrlWithCallback)
54+
.withUrl(updateCustomUrlToSearchAll)
55+
56+
XHRRequestInterceptor
57+
.intercept({ url: this.searchAllPropertiesWithoutDateStartRegex, status: 200 })
58+
.withResponse(parseResponseWithCallback)
4659

4760
return this
4861
}

0 commit comments

Comments
 (0)