-
Notifications
You must be signed in to change notification settings - Fork 0
[LSP-1319]: add wrapResponse utility and update handlers to use it and fix CORS issue #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,41 +1,35 @@ | ||
| const { getOpenSearchClient } = require("../lib/opensearch-client"); | ||
| const { wrapResponse } = require("../lib/wrap-response"); | ||
|
|
||
| const client = getOpenSearchClient(); | ||
| const { buildAutoSuggestQuery } = require("../utils/esHelpers"); | ||
|
|
||
| module.exports.index = async (event) => { | ||
| try { | ||
| const body = event.queryStringParameters; | ||
| const queryText = body.query || body.q || ""; | ||
| const index = body.index; | ||
| const size = 10; | ||
| const langId = body.langId ? parseInt(body.langId, 10) : 1; | ||
| if (!queryText) { | ||
| return { | ||
| statusCode: 400, | ||
| body: JSON.stringify({ error: "Missing query parameter" }), | ||
| }; | ||
| } | ||
| return wrapResponse(async () => { | ||
| try { | ||
| const body = event.queryStringParameters; | ||
| const queryText = body.query || body.q || ""; | ||
| const index = body.index; | ||
| const size = 10; | ||
| const langId = body.langId ? parseInt(body.langId, 10) : 1; | ||
| if (!queryText) { | ||
| throw new Error("Missing query parameter"); | ||
| } | ||
|
|
||
| // Use the new buildAutoSuggestQuery for powerful autosuggestion | ||
| const suggestQuery = buildAutoSuggestQuery(queryText, langId, size); | ||
| console.log("suggestQuery", suggestQuery); | ||
| // Use the new buildAutoSuggestQuery for powerful autosuggestion | ||
| const suggestQuery = buildAutoSuggestQuery(queryText, langId, size); | ||
| console.log("suggestQuery", suggestQuery); | ||
|
|
||
| const results = await client.search(index, suggestQuery, 0, size); | ||
| const suggestions = results.results.map((hit) => hit._source); | ||
| const results = await client.search(index, suggestQuery, 0, size); | ||
| const suggestions = results.results.map((hit) => hit._source); | ||
|
|
||
| return { | ||
| statusCode: 200, | ||
| body: JSON.stringify({ | ||
| return { | ||
| suggestions, | ||
| total: results.total, | ||
| }), | ||
| }; | ||
| } catch (error) { | ||
| console.error("AutoSuggest error:", error); | ||
| return { | ||
| statusCode: 500, | ||
| body: JSON.stringify({ error: error.message || "Internal Server Error" }), | ||
| }; | ||
| } | ||
| }; | ||
| } catch (error) { | ||
| console.error("AutoSuggest error:", error); | ||
| throw new Error("Error processing auto-suggest request"); | ||
| } | ||
michael-puzon-resultscx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,79 +1,71 @@ | ||
| const OpenSearchClient = require('../engines/OpenSearch/openSearchClient'); | ||
| const { buildQuery } = require('../utils/esHelpers'); | ||
| const { getOpenSearchClient } = require('../lib/opensearch-client'); | ||
| const OpenSearchClient = require("../engines/OpenSearch/openSearchClient"); | ||
michael-puzon-resultscx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const { buildQuery } = require("../utils/esHelpers"); | ||
| const { getOpenSearchClient } = require("../lib/opensearch-client"); | ||
| const { wrapResponse } = require("../lib/wrap-response"); | ||
michael-puzon-resultscx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const osClient = getOpenSearchClient(); | ||
|
|
||
| module.exports.index = async (event) => { | ||
| let body; | ||
| if (event.body) { | ||
| try { | ||
| body = JSON.parse(event.body); | ||
| } catch (parseError) { | ||
| console.log('Invalid JSON in event.body:', parseError); | ||
| return { | ||
| statusCode: 400, | ||
| body: JSON.stringify({ error: 'Invalid JSON in request body' }), | ||
| }; | ||
| } | ||
| } else { | ||
| body = event; | ||
| } | ||
| const { index, query, project, filters, aggs, from, size, langId } = body; | ||
| console.log('Request body:', body); | ||
|
|
||
| try { | ||
| if (!index) { | ||
| throw new Error('Index should be present'); | ||
| return wrapResponse(async () => { | ||
| let body; | ||
| if (event.body) { | ||
| try { | ||
| body = JSON.parse(event.body); | ||
| } catch (parseError) { | ||
| console.log("Invalid JSON in event.body:", parseError); | ||
| throw new Error("Invalid JSON in request body"); | ||
michael-puzon-resultscx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } else { | ||
| body = event; | ||
| } | ||
| const {query: builtQuery, ...rest} = buildQuery(query, project, langId); | ||
| const { index, query, project, filters, aggs, from, size, langId } = body; | ||
| console.log("Request body:", body); | ||
|
|
||
| const mustClause = [ | ||
| builtQuery | ||
| ] | ||
| try { | ||
| if (!index) { | ||
| throw new Error("Index should be present"); | ||
| } | ||
| const { query: builtQuery, ...rest } = buildQuery(query, project, langId); | ||
|
|
||
| if (filters && Object.keys(filters).length) { | ||
| const terms = Object.entries(filters).map(([field, value]) => ({ | ||
| term: { [field]: value } | ||
| })); | ||
| mustClause.push(...terms); | ||
| } | ||
|
|
||
| const finalQuery = { | ||
| query: { | ||
| bool: { | ||
| must: [ | ||
| { | ||
| bool: { | ||
| must: mustClause | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| }, | ||
| ...rest | ||
| } | ||
| const mustClause = [builtQuery]; | ||
|
|
||
| if (aggs && Object.keys(aggs).length) { | ||
| finalQuery.aggs = aggs; | ||
| } | ||
| if (filters && Object.keys(filters).length) { | ||
| const terms = Object.entries(filters).map(([field, value]) => ({ | ||
| term: { [field]: value }, | ||
| })); | ||
| mustClause.push(...terms); | ||
| } | ||
|
|
||
| const finalQuery = { | ||
| query: { | ||
| bool: { | ||
| must: [ | ||
| { | ||
| bool: { | ||
| must: mustClause, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| ...rest, | ||
| }; | ||
|
|
||
| const results = await osClient.search( | ||
| index, | ||
| finalQuery, | ||
| parseInt(from, 10), | ||
| parseInt(size, 10) | ||
| ); | ||
| if (aggs && Object.keys(aggs).length) { | ||
| finalQuery.aggs = aggs; | ||
| } | ||
|
|
||
| return { | ||
| statusCode: 200, | ||
| body: JSON.stringify(results), | ||
| }; | ||
| } catch (error) { | ||
| console.log('OpenSearch index error:', error) | ||
| return { | ||
| statusCode: 500, | ||
| body: JSON.stringify({ error: error.message }), | ||
| }; | ||
| } | ||
| const results = await osClient.search( | ||
| index, | ||
| finalQuery, | ||
| parseInt(from, 10), | ||
| parseInt(size, 10), | ||
| ); | ||
|
|
||
| return results; | ||
| } catch (error) { | ||
| console.log("OpenSearch index error:", error); | ||
| throw error; | ||
| } | ||
michael-puzon-resultscx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| const wrapResponse = async (handler) => { | ||
| try { | ||
| const result = await handler(); | ||
| return { | ||
| statusCode: result.statusCode || 200, | ||
| body: JSON.stringify(result), | ||
| headers: { | ||
| "Access-Control-Allow-Origin": "*", | ||
| "Access-Control-Allow-Credentials": true, | ||
| }, | ||
michael-puzon-resultscx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
michael-puzon-resultscx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch (error) { | ||
| console.error("Error in wrapResponse:", error); | ||
| return { | ||
| statusCode: 500, | ||
| body: JSON.stringify({ | ||
| message: "Internal Server Error", | ||
| }), | ||
michael-puzon-resultscx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| headers: { | ||
| "Access-Control-Allow-Origin": "*", | ||
| "Access-Control-Allow-Credentials": true, | ||
| }, | ||
| }; | ||
| } | ||
| }; | ||
| module.exports = { wrapResponse }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.