Skip to content

Commit 2f75560

Browse files
authored
Search and replace policy (#57)
* Added ‘SearchAndReplace’ Apigee policy * Renamed to ‘SearchAndReplaceResponse’ * Fixed typo * Removed any ES6 syntax * Added SearchAndReplaceResponse Apigee policy to sandbox * Added JS file * Updated name * Moved flow in to the TargetEndpoint * Updated the search and replace JS * Debugging basePath and subdomain * Debug context * Debugging proxyRequest and targetRequest * WIP * Added timestamp * WIP * WIP * WIP * WIP * WIP * Using target.name in API host name * Removed switch statement * Using a request.url * Removed targetName * Updated JS * Added check for ‘api.nhs.uk’ too * Added api.nhs.uk * Added a check for ‘api’ * Added debugging for ‘requestUrl’ and ‘environmentSubdomain’ * Moved ‘item’ variable outside the ‘for’ loop * WIP: AssignMessage.CopyRequest * Commented out the ‘CopyRequest’ flow * Using ‘requestHostnameEnv’ in SearchAndReplaceResponse.js * Debugging ‘requestHostnameEnv’ variable * Removed debugging * Updated the search and replace Regex
1 parent a937fde commit 2f75560

2 files changed

Lines changed: 252 additions & 70 deletions

File tree

proxies/live/apiproxy/resources/jsc/SearchAndReplaceResponse.js

Lines changed: 126 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,75 +2,166 @@ var requestHostnameEnv = context.getVariable("request_hostname_env");
22

33
var searchAndReplaceStrings = [
44
{
5-
searchFor: "www.nhs.uk/conditions/",
6-
replaceWith: requestHostnameEnv + "/nhs-website-content/conditions/",
5+
comment1: "=============================================================",
6+
comment2: "Conditions ",
7+
comment3: "=============================================================",
8+
searchForRegex: /:"https:\/\/api.nhs.uk\/conditions\//gm,
9+
replaceWithStr:
10+
':"https://' + requestHostnameEnv + "/nhs-website-content/conditions/",
711
},
812
{
9-
searchFor: "www.nhs.uk/live-well/",
10-
replaceWith: requestHostnameEnv + "/nhs-website-content/live-well/",
13+
searchForRegex: /:"https:\/\/www.nhs.uk\/conditions\//gm,
14+
replaceWithStr:
15+
':"https://' + requestHostnameEnv + "/nhs-website-content/conditions/",
1116
},
1217
{
13-
searchFor: "www.nhs.uk/medicines/",
14-
replaceWith: requestHostnameEnv + "/nhs-website-content/medicines/",
18+
searchForRegex: /href=\\"https:\/\/api.nhs.uk\/conditions\//gm,
19+
replaceWithStr: 'href=\\"https://www.nhs.uk/conditions/',
1520
},
1621
{
17-
searchFor: "www.nhs.uk/nhs-services/",
18-
replaceWith: requestHostnameEnv + "/nhs-website-content/nhs-services/",
22+
searchForRegex: /href=\\"\/conditions\//gm,
23+
replaceWithStr: 'href=\\"https://www.nhs.uk/conditions/',
1924
},
2025
{
21-
searchFor: "www.nhs.uk/pregnancy/",
22-
replaceWith: requestHostnameEnv + "/nhs-website-content/pregnancy/",
26+
comment1: "=============================================================",
27+
comment2: "Live Well ",
28+
comment3: "=============================================================",
29+
searchForRegex: /:"https:\/\/api.nhs.uk\/live-well\//gm,
30+
replaceWithStr:
31+
':"https://' + requestHostnameEnv + "/nhs-website-content/live-well/",
2332
},
2433
{
25-
searchFor: "www.nhs.uk/common-health-questions/",
26-
replaceWith:
27-
requestHostnameEnv + "/nhs-website-content/common-health-questions/",
34+
searchForRegex: /:"https:\/\/www.nhs.uk\/live-well\//gm,
35+
replaceWithStr:
36+
':"https://' + requestHostnameEnv + "/nhs-website-content/live-well/",
2837
},
2938
{
30-
searchFor: "www.nhs.uk/mental-health/",
31-
replaceWith:
32-
requestHostnameEnv +
33-
"api.service.nhs.uk/nhs-website-content/mental-health/",
39+
searchForRegex: /href=\\"https:\/\/api.nhs.uk\/live-well\//gm,
40+
replaceWithStr: 'href=\\"https://www.nhs.uk/live-well/',
41+
},
42+
{
43+
searchForRegex: /href=\\"\/live-well\//gm,
44+
replaceWithStr: 'href=\\"https://www.nhs.uk/live-well/',
45+
},
46+
{
47+
comment1: "=============================================================",
48+
comment2: "Mental health ",
49+
comment3: "=============================================================",
50+
searchForRegex: /:"https:\/\/api.nhs.uk\/mental-health\//gm,
51+
replaceWithStr:
52+
':"https://' + requestHostnameEnv + "/nhs-website-content/mental-health/",
53+
},
54+
{
55+
searchForRegex: /:"https:\/\/www.nhs.uk\/mental-health\//gm,
56+
replaceWithStr:
57+
':"https://' + requestHostnameEnv + "/nhs-website-content/mental-health/",
58+
},
59+
{
60+
searchForRegex: /href=\\"https:\/\/api.nhs.uk\/mental-health\//gm,
61+
replaceWithStr: 'href=\\"https://www.nhs.uk/mental-health/',
62+
},
63+
{
64+
searchForRegex: /href=\\"\/mental-health\//gm,
65+
replaceWithStr: 'href=\\"https://www.nhs.uk/mental-health/',
66+
},
67+
{
68+
comment1: "=============================================================",
69+
comment2: "Medicines ",
70+
comment3: "=============================================================",
71+
searchForRegex: /:"https:\/\/api.nhs.uk\/medicines\//gm,
72+
replaceWithStr:
73+
':"https://' + requestHostnameEnv + "/nhs-website-content/medicines/",
74+
},
75+
{
76+
searchForRegex: /:"https:\/\/www.nhs.uk\/medicines\//gm,
77+
replaceWithStr:
78+
':"https://' + requestHostnameEnv + "/nhs-website-content/medicines/",
3479
},
3580
{
36-
searchFor: "api.nhs.uk/conditions/",
37-
replaceWith: requestHostnameEnv + "/nhs-website-content/conditions/",
81+
searchForRegex: /href=\\"https:\/\/api.nhs.uk\/medicines\//gm,
82+
replaceWithStr: 'href=\\"https://www.nhs.uk/medicines/',
3883
},
3984
{
40-
searchFor: "api.nhs.uk/live-well/",
41-
replaceWith: requestHostnameEnv + "/nhs-website-content/live-well/",
85+
searchForRegex: /href=\\"\/medicines\//gm,
86+
replaceWithStr: 'href=\\"https://www.nhs.uk/medicines/',
4287
},
4388
{
44-
searchFor: "api.nhs.uk/medicines/",
45-
replaceWith: requestHostnameEnv + "/nhs-website-content/medicines/",
89+
comment1: "=============================================================",
90+
comment2: "NHS services ",
91+
comment3: "=============================================================",
92+
searchForRegex: /:"https:\/\/api.nhs.uk\/nhs-services\//gm,
93+
replaceWithStr:
94+
':"https://' + requestHostnameEnv + "/nhs-website-content/nhs-services/",
4695
},
4796
{
48-
searchFor: "api.nhs.uk/nhs-services/",
49-
replaceWith: requestHostnameEnv + "/nhs-website-content/nhs-services/",
97+
searchForRegex: /:"https:\/\/www.nhs.uk\/nhs-services\//gm,
98+
replaceWithStr:
99+
':"https://' + requestHostnameEnv + "/nhs-website-content/nhs-services/",
50100
},
51101
{
52-
searchFor: "api.nhs.uk/pregnancy/",
53-
replaceWith: requestHostnameEnv + "/nhs-website-content/pregnancy/",
102+
searchForRegex: /href=\\"https:\/\/api.nhs.uk\/nhs-services\//gm,
103+
replaceWithStr: 'href=\\"https://www.nhs.uk/nhs-services/',
54104
},
55105
{
56-
searchFor: "api.nhs.uk/common-health-questions/",
57-
replaceWith:
58-
requestHostnameEnv + "/nhs-website-content/common-health-questions/",
106+
searchForRegex: /href=\\"\/nhs-services\//gm,
107+
replaceWithStr: 'href=\\"https://www.nhs.uk/nhs-services/',
59108
},
60109
{
61-
searchFor: "api.nhs.uk/mental-health/",
62-
replaceWith:
110+
comment1: "=============================================================",
111+
comment2: "Pregnancy ",
112+
comment3: "=============================================================",
113+
searchForRegex: /:"https:\/\/api.nhs.uk\/pregnancy\//gm,
114+
replaceWithStr:
115+
':"https://' + requestHostnameEnv + "/nhs-website-content/pregnancy/",
116+
},
117+
{
118+
searchForRegex: /:"https:\/\/www.nhs.uk\/pregnancy\//gm,
119+
replaceWithStr:
120+
':"https://' + requestHostnameEnv + "/nhs-website-content/pregnancy/",
121+
},
122+
{
123+
searchForRegex: /href=\\"https:\/\/api.nhs.uk\/pregnancy\//gm,
124+
replaceWithStr: 'href=\\"https://www.nhs.uk/pregnancy/',
125+
},
126+
{
127+
searchForRegex: /href=\\"\/pregnancy\//gm,
128+
replaceWithStr: 'href=\\"https://www.nhs.uk/pregnancy/',
129+
},
130+
{
131+
comment1: "=============================================================",
132+
comment2: "Common health questions ",
133+
comment3: "=============================================================",
134+
searchForRegex: /:"https:\/\/api.nhs.uk\/common-health-questions\//gm,
135+
replaceWithStr:
136+
':"https://' +
63137
requestHostnameEnv +
64-
"api.service.nhs.uk/nhs-website-content/mental-health/",
138+
"/nhs-website-content/common-health-questions/",
139+
},
140+
{
141+
searchForRegex: /:"https:\/\/www.nhs.uk\/common-health-questions\//gm,
142+
replaceWithStr:
143+
':"https://' +
144+
requestHostnameEnv +
145+
"/nhs-website-content/common-health-questions/",
146+
},
147+
{
148+
searchForRegex: /href=\\"https:\/\/api.nhs.uk\/common-health-questions\//gm,
149+
replaceWithStr: 'href=\\"https://www.nhs.uk/common-health-questions/',
150+
},
151+
{
152+
searchForRegex: /href=\\"\/common-health-questions\//gm,
153+
replaceWithStr: 'href=\\"https://www.nhs.uk/common-health-questions/',
65154
},
66155
];
67156

68157
var responseContent = context.getVariable("response.content");
69158
var regex, item;
70159
for (var i = 0; i < searchAndReplaceStrings.length; i++) {
71160
item = searchAndReplaceStrings[i];
72-
regex = new RegExp(item.searchFor, "g");
73-
responseContent = responseContent.replace(regex, item.replaceWith);
161+
responseContent = responseContent.replace(
162+
item.searchForRegex,
163+
item.replaceWithStr
164+
);
74165
}
75166

76167
context.setVariable("response.content", responseContent);

0 commit comments

Comments
 (0)