Skip to content

Commit

Permalink
replaced proxies with backend functions (since proxies do not work wi…
Browse files Browse the repository at this point in the history
…th Static Web Apps anymore)
  • Loading branch information
scale-tone committed Jan 8, 2025
1 parent 6bdf604 commit 4a1d59c
Show file tree
Hide file tree
Showing 11 changed files with 459 additions and 70 deletions.
19 changes: 19 additions & 0 deletions api/autocomplete/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
],
"scriptFile": "../dist/autocomplete/index.js"
}
20 changes: 20 additions & 0 deletions api/autocomplete/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { AzureFunction, Context, HttpRequest } from "@azure/functions"
import axios from 'axios';

const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {

const searchApiUrl = `https://${process.env.SearchServiceName}.search.windows.net/indexes/${process.env.SearchIndexName}/docs/autocomplete?api-version=2019-05-06&`;
const url = req.url.replace(/^http(s)?:\/\/[^/]+\/api\/autocomplete(\?)?/i, searchApiUrl);

const searchApiResponse = await axios.get(url, {
headers: {
"api-key": process.env.SearchApiKey,
} });

context.res = {
status: searchApiResponse.status,
body: searchApiResponse.data,
};
};

export default httpTrigger;
20 changes: 20 additions & 0 deletions api/config-script/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
],
"scriptFile": "../dist/config-script/index.js"
}
27 changes: 27 additions & 0 deletions api/config-script/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { AzureFunction, Context } from "@azure/functions"

const httpTrigger: AzureFunction = async function (context: Context): Promise<void> {

const serverSideConfig = {
SearchServiceName: process.env.SearchServiceName,
SearchIndexName: process.env.SearchIndexName,
AzureMapSubscriptionKey: process.env.AzureMapSubscriptionKey,
CognitiveSearchKeyField: process.env.CognitiveSearchKeyField,
CognitiveSearchNameField: process.env.CognitiveSearchNameField,
CognitiveSearchGeoLocationField: process.env.CognitiveSearchGeoLocationField,
CognitiveSearchOtherFields: process.env.CognitiveSearchOtherFields,
CognitiveSearchTranscriptFields: process.env.CognitiveSearchTranscriptFields,
CognitiveSearchFacetFields: process.env.CognitiveSearchFacetFields,
CognitiveSearchSuggesterName: process.env.CognitiveSearchSuggesterName
};

context.res = {
body: `const ServerSideConfig = ${JSON.stringify(serverSideConfig)}`,

headers: {
"Content-Type": "application/javascript; charset=UTF-8"
}
};
};

export default httpTrigger;
20 changes: 20 additions & 0 deletions api/lookup/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get"
],
"route": "lookup/{key}"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
],
"scriptFile": "../dist/lookup/index.js"
}
20 changes: 20 additions & 0 deletions api/lookup/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { AzureFunction, Context, HttpRequest } from "@azure/functions"
import axios from 'axios';

const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {

const searchApiUrl = `https://${process.env.SearchServiceName}.search.windows.net/indexes/${process.env.SearchIndexName}/docs`;
const url = req.url.replace(/^http(s)?:\/\/[^/]+\/api\/lookup/i, searchApiUrl);

const searchApiResponse = await axios.get(`${url}?api-version=2019-05-06`, {
headers: {
"api-key": process.env.SearchApiKey,
} });

context.res = {
status: searchApiResponse.status,
body: searchApiResponse.data,
};
};

export default httpTrigger;
Loading

0 comments on commit 4a1d59c

Please sign in to comment.