Skip to content

Commit 4a1d59c

Browse files
committed
replaced proxies with backend functions (since proxies do not work with Static Web Apps anymore)
1 parent 6bdf604 commit 4a1d59c

File tree

11 files changed

+459
-70
lines changed

11 files changed

+459
-70
lines changed

api/autocomplete/function.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"bindings": [
3+
{
4+
"authLevel": "anonymous",
5+
"type": "httpTrigger",
6+
"direction": "in",
7+
"name": "req",
8+
"methods": [
9+
"get"
10+
]
11+
},
12+
{
13+
"type": "http",
14+
"direction": "out",
15+
"name": "res"
16+
}
17+
],
18+
"scriptFile": "../dist/autocomplete/index.js"
19+
}

api/autocomplete/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { AzureFunction, Context, HttpRequest } from "@azure/functions"
2+
import axios from 'axios';
3+
4+
const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
5+
6+
const searchApiUrl = `https://${process.env.SearchServiceName}.search.windows.net/indexes/${process.env.SearchIndexName}/docs/autocomplete?api-version=2019-05-06&`;
7+
const url = req.url.replace(/^http(s)?:\/\/[^/]+\/api\/autocomplete(\?)?/i, searchApiUrl);
8+
9+
const searchApiResponse = await axios.get(url, {
10+
headers: {
11+
"api-key": process.env.SearchApiKey,
12+
} });
13+
14+
context.res = {
15+
status: searchApiResponse.status,
16+
body: searchApiResponse.data,
17+
};
18+
};
19+
20+
export default httpTrigger;

api/config-script/function.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"bindings": [
3+
{
4+
"authLevel": "anonymous",
5+
"type": "httpTrigger",
6+
"direction": "in",
7+
"name": "req",
8+
"methods": [
9+
"get",
10+
"post"
11+
]
12+
},
13+
{
14+
"type": "http",
15+
"direction": "out",
16+
"name": "res"
17+
}
18+
],
19+
"scriptFile": "../dist/config-script/index.js"
20+
}

api/config-script/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { AzureFunction, Context } from "@azure/functions"
2+
3+
const httpTrigger: AzureFunction = async function (context: Context): Promise<void> {
4+
5+
const serverSideConfig = {
6+
SearchServiceName: process.env.SearchServiceName,
7+
SearchIndexName: process.env.SearchIndexName,
8+
AzureMapSubscriptionKey: process.env.AzureMapSubscriptionKey,
9+
CognitiveSearchKeyField: process.env.CognitiveSearchKeyField,
10+
CognitiveSearchNameField: process.env.CognitiveSearchNameField,
11+
CognitiveSearchGeoLocationField: process.env.CognitiveSearchGeoLocationField,
12+
CognitiveSearchOtherFields: process.env.CognitiveSearchOtherFields,
13+
CognitiveSearchTranscriptFields: process.env.CognitiveSearchTranscriptFields,
14+
CognitiveSearchFacetFields: process.env.CognitiveSearchFacetFields,
15+
CognitiveSearchSuggesterName: process.env.CognitiveSearchSuggesterName
16+
};
17+
18+
context.res = {
19+
body: `const ServerSideConfig = ${JSON.stringify(serverSideConfig)}`,
20+
21+
headers: {
22+
"Content-Type": "application/javascript; charset=UTF-8"
23+
}
24+
};
25+
};
26+
27+
export default httpTrigger;

api/lookup/function.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"bindings": [
3+
{
4+
"authLevel": "anonymous",
5+
"type": "httpTrigger",
6+
"direction": "in",
7+
"name": "req",
8+
"methods": [
9+
"get"
10+
],
11+
"route": "lookup/{key}"
12+
},
13+
{
14+
"type": "http",
15+
"direction": "out",
16+
"name": "res"
17+
}
18+
],
19+
"scriptFile": "../dist/lookup/index.js"
20+
}

api/lookup/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { AzureFunction, Context, HttpRequest } from "@azure/functions"
2+
import axios from 'axios';
3+
4+
const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
5+
6+
const searchApiUrl = `https://${process.env.SearchServiceName}.search.windows.net/indexes/${process.env.SearchIndexName}/docs`;
7+
const url = req.url.replace(/^http(s)?:\/\/[^/]+\/api\/lookup/i, searchApiUrl);
8+
9+
const searchApiResponse = await axios.get(`${url}?api-version=2019-05-06`, {
10+
headers: {
11+
"api-key": process.env.SearchApiKey,
12+
} });
13+
14+
context.res = {
15+
status: searchApiResponse.status,
16+
body: searchApiResponse.data,
17+
};
18+
};
19+
20+
export default httpTrigger;

0 commit comments

Comments
 (0)