Skip to content

Commit bfbab41

Browse files
authored
Search and replace in response policy (#48)
* 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
1 parent 430711d commit bfbab41

7 files changed

Lines changed: 148 additions & 2 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<Javascript async="false" continueOnError="false" enabled="true" timeLimit="200" name="javascript.SearchAndReplaceResponse">
3+
<DisplayName>javascript.SearchAndReplaceResponse</DisplayName>
4+
<Properties/>
5+
<ResourceURL>jsc://SearchAndReplaceResponse.js</ResourceURL>
6+
</Javascript>

proxies/live/apiproxy/proxies/default.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
</Step>
77
</Request>
88
</PreFlow>
9-
109
<Flows>
1110
<Flow name="OptionsPreFlight">
1211
<Request/>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// An example requestUrl would be https://internal-dev-sandbox.apis.ptl.api.platform.nhs.uk/nwca-48/conditions/
2+
var requestUrl = context.getVariable("request.url");
3+
var environmentSubdomain = requestUrl.split("/");
4+
environmentSubdomain = environmentSubdomain[2];
5+
environmentSubdomain = environmentSubdomain.split(".");
6+
environmentSubdomain = environmentSubdomain[0];
7+
8+
var apiVersion2Host;
9+
if (environmentSubdomain === "apis" || environmentSubdomain === "prod") {
10+
apiVersion2Host = "api.service.nhs.uk";
11+
} else {
12+
apiVersion2Host = environmentSubdomain + ".api.service.nhs.uk";
13+
}
14+
15+
var searchAndReplaceStrings = [
16+
{
17+
searchFor: "www.nhs.uk/conditions/",
18+
replaceWith: apiVersion2Host + "/nhs-website-content/conditions/",
19+
},
20+
{
21+
searchFor: "www.nhs.uk/live-well/",
22+
replaceWith: apiVersion2Host + "/nhs-website-content/live-well/",
23+
},
24+
{
25+
searchFor: "www.nhs.uk/medicines/",
26+
replaceWith: apiVersion2Host + "/nhs-website-content/medicines/",
27+
},
28+
{
29+
searchFor: "www.nhs.uk/nhs-services/",
30+
replaceWith: apiVersion2Host + "/nhs-website-content/nhs-services/",
31+
},
32+
{
33+
searchFor: "www.nhs.uk/pregnancy/",
34+
replaceWith: apiVersion2Host + "/nhs-website-content/pregnancy/",
35+
},
36+
{
37+
searchFor: "www.nhs.uk/common-health-questions/",
38+
replaceWith:
39+
apiVersion2Host + "/nhs-website-content/common-health-questions/",
40+
},
41+
{
42+
searchFor: "www.nhs.uk/mental-health/",
43+
replaceWith:
44+
apiVersion2Host + "api.service.nhs.uk/nhs-website-content/mental-health/",
45+
},
46+
];
47+
48+
var responseContent = context.getVariable("response.content");
49+
var regex;
50+
for (var i = 0; i < searchAndReplaceStrings.length; i++) {
51+
var item = searchAndReplaceStrings[i];
52+
regex = new RegExp(item.searchFor, "g");
53+
responseContent = responseContent.replace(regex, item.replaceWith);
54+
}
55+
56+
context.setVariable("response.content", responseContent);

proxies/live/apiproxy/targets/target.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@
99
</Step>
1010
</Request>
1111
</PreFlow>
12+
<Flows>
13+
<Flow name="SearchAndReplaceResponse">
14+
<Description/>
15+
<Request/>
16+
<Response>
17+
<Step>
18+
<Name>javascript.SearchAndReplaceResponse</Name>
19+
</Step>
20+
</Response>
21+
<Condition/>
22+
</Flow>
23+
</Flows>
1224
<FaultRules>
1325
<FaultRule name="api_key_invalid">
1426
<Condition>verifyapikey.VerifyAPIKey.FromHeader.failed</Condition>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<Javascript async="false" continueOnError="false" enabled="true" timeLimit="200" name="javascript.SearchAndReplaceResponse">
3+
<DisplayName>javascript.SearchAndReplaceResponse</DisplayName>
4+
<Properties/>
5+
<ResourceURL>jsc://SearchAndReplaceResponse.js</ResourceURL>
6+
</Javascript>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// An example requestUrl would be https://internal-dev-sandbox.apis.ptl.api.platform.nhs.uk/nwca-48/conditions/
2+
var requestUrl = context.getVariable("request.url");
3+
var environmentSubdomain = requestUrl.split("/");
4+
environmentSubdomain = environmentSubdomain[2];
5+
environmentSubdomain = environmentSubdomain.split(".");
6+
environmentSubdomain = environmentSubdomain[0];
7+
8+
var apiVersion2Host;
9+
if (environmentSubdomain === "apis" || environmentSubdomain === "prod") {
10+
apiVersion2Host = "api.service.nhs.uk";
11+
} else {
12+
apiVersion2Host = environmentSubdomain + ".api.service.nhs.uk";
13+
}
14+
15+
var searchAndReplaceStrings = [
16+
{
17+
searchFor: "www.nhs.uk/conditions/",
18+
replaceWith: apiVersion2Host + "/nhs-website-content/conditions/",
19+
},
20+
{
21+
searchFor: "www.nhs.uk/live-well/",
22+
replaceWith: apiVersion2Host + "/nhs-website-content/live-well/",
23+
},
24+
{
25+
searchFor: "www.nhs.uk/medicines/",
26+
replaceWith: apiVersion2Host + "/nhs-website-content/medicines/",
27+
},
28+
{
29+
searchFor: "www.nhs.uk/nhs-services/",
30+
replaceWith: apiVersion2Host + "/nhs-website-content/nhs-services/",
31+
},
32+
{
33+
searchFor: "www.nhs.uk/pregnancy/",
34+
replaceWith: apiVersion2Host + "/nhs-website-content/pregnancy/",
35+
},
36+
{
37+
searchFor: "www.nhs.uk/common-health-questions/",
38+
replaceWith:
39+
apiVersion2Host + "/nhs-website-content/common-health-questions/",
40+
},
41+
{
42+
searchFor: "www.nhs.uk/mental-health/",
43+
replaceWith:
44+
apiVersion2Host + "api.service.nhs.uk/nhs-website-content/mental-health/",
45+
},
46+
];
47+
48+
var responseContent = context.getVariable("response.content");
49+
var regex;
50+
for (var i = 0; i < searchAndReplaceStrings.length; i++) {
51+
var item = searchAndReplaceStrings[i];
52+
regex = new RegExp(item.searchFor, "g");
53+
responseContent = responseContent.replace(regex, item.replaceWith);
54+
}
55+
56+
context.setVariable("response.content", responseContent);

proxies/sandbox/apiproxy/targets/sandbox.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
<TargetEndpoint name="sandbox">
22
<Description/>
33
<FaultRules/>
4-
<Flows/>
4+
<Flows>
5+
<Flow name="SearchAndReplaceResponse">
6+
<Description/>
7+
<Request/>
8+
<Response>
9+
<Step>
10+
<Name>javascript.SearchAndReplaceResponse</Name>
11+
</Step>
12+
</Response>
13+
<Condition/>
14+
</Flow>
15+
</Flows>
516
<PostFlow name="PostFlow">
617
<Request/>
718
<Response/>

0 commit comments

Comments
 (0)