-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from stscoundrel/feature/backend-search
Feature/backend search
- Loading branch information
Showing
8 changed files
with
128 additions
and
50 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,21 @@ | ||
import { getAllWords } from 'lib/services/dictionary' | ||
import { Criteria, searchDictionary } from 'lib/services/search'; | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
if (!req.query.search || !req.query.criteria) { | ||
return res.status(422).json({ message: 'Missing search term or criteria' }) | ||
} | ||
|
||
const { search, criteria } = req.query | ||
const formattedCriteria = Array.isArray(criteria) ? criteria as Criteria[] : criteria.split(',') as Criteria[] | ||
|
||
const dictionary = getAllWords(); | ||
const results = searchDictionary(String(search), dictionary, formattedCriteria) | ||
|
||
if (results.length > 100) { | ||
return res.status(200).json(results.slice(0, 100)) | ||
} | ||
|
||
return res.status(200).json(results) | ||
} |
This file contains 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
This file contains 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
This file contains 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,20 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Search API endpoint Should return search results 1`] = ` | ||
[ | ||
{ | ||
"foundIn": [ | ||
"In headword: afköra", | ||
], | ||
"headword": "afköra", | ||
"slug": "afkora", | ||
}, | ||
{ | ||
"foundIn": [ | ||
"In headword: utafköra", | ||
], | ||
"headword": "utafköra", | ||
"slug": "utafkora", | ||
}, | ||
] | ||
`; |
This file contains 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,49 @@ | ||
import { createMocks } from 'node-mocks-http' | ||
// eslint-disable-next-line import/no-unresolved | ||
import searchHandler from 'pages/api/search' | ||
|
||
describe('Search API endpoint', () => { | ||
test('Should error if missing query params', async () => { | ||
const { req, res } = createMocks() | ||
|
||
// Ensure response is blank. | ||
expect(res.finished).toBeFalsy() | ||
expect(res._headers).toEqual({}) // eslint-disable-line | ||
|
||
// Setup invalid query params | ||
req.query = { | ||
foo: 'bar', | ||
} | ||
|
||
await searchHandler(req, res) | ||
|
||
// Should've received 422 bad request | ||
expect(res.finished).toBeTruthy() | ||
expect(res.statusCode).toBe(422) | ||
}) | ||
|
||
test('Should return search results', async () => { | ||
const { req, res } = createMocks() | ||
|
||
// Ensure response is blank. | ||
expect(res.finished).toBeFalsy() | ||
expect(res._headers).toEqual({}) // eslint-disable-line | ||
|
||
// Setup valid query params | ||
req.query = { | ||
search: 'afkora', | ||
criteria: 'headword', | ||
} | ||
|
||
await searchHandler(req, res) | ||
|
||
// eslint-disable-next-line no-underscore-dangle | ||
const response = JSON.parse(res._getData()) | ||
|
||
// Should be valid response with results | ||
expect(res.finished).toBeTruthy() | ||
expect(res.statusCode).toBe(200) | ||
expect(response.length).toEqual(2) | ||
expect(response).toMatchSnapshot() | ||
}) | ||
}) |
This file contains 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
10844e6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
old-swedish-dictionary-next – ./
old-swedish-dictionary-next-git-main-stscoundrel.vercel.app
old-swedish-dictionary-next-stscoundrel.vercel.app
old-swedish-dictionary.vercel.app