Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import * as functions from 'firebase-functions';
- import Fuse from 'fuse.js';
- import * as admin from 'firebase-admin';
- admin.initializeApp();
- export const search = functions.https.onRequest(async (req, res) => {
- res = res.set('Access-Control-Allow-Origin', '*').status(200);
- const ingredients = await admin.database().ref('ff/').once('value').then(snapshot => snapshot.val());
- if (!ingredients) {
- res.send([]);
- return;
- }
- const fullData = Object.values(ingredients);
- const searchText = req.query.query as string;
- if (typeof searchText !== 'string'
- || !searchText
- || ['null', 'undefined'].includes(searchText)) {
- res.status(400).send([ 'searchText' ]);
- return;
- }
- if (!fullData.length) {
- res.send([]);
- return;
- }
- const fuse = new Fuse(fullData, {
- keys: [ 'synonyms' ],
- shouldSort: false,
- threshold: 0.2,
- location: 0,
- distance: 100,
- minMatchCharLength: 1,
- });
- const resultIngredients = fuse.search(searchText);
- res.send(resultIngredients);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement