From ee572807daf0015bac82c738b63b2bc021f5aa44 Mon Sep 17 00:00:00 2001 From: Alvin Cheng <88267875+cheng-alvin@users.noreply.github.com> Date: Thu, 19 Jan 2023 13:18:09 +1100 Subject: [PATCH 1/3] Install node-fetch --- package.json | 1 + yarn.lock | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/package.json b/package.json index 9f038e5..3265da9 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "express-rate-limit": "^6.3.0", "file-type": "^16.5.3", "mongoose": "^6.7.11", + "node-fetch": "2.0.0", "nodemailer": "^6.7.3", "randomcolor": "^0.6.2", "sharp": "^0.31.2", diff --git a/yarn.lock b/yarn.lock index d9421f8..f10ef37 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1829,6 +1829,11 @@ node-addon-api@^5.0.0: resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-5.0.0.tgz#7d7e6f9ef89043befdb20c1989c905ebde18c501" integrity sha512-CvkDw2OEnme7ybCykJpVcKH+uAOLV2qLqiyla128dN9TkEWfrYmxG6C2boDe5KcNQqZF3orkqzGgOMvZ/JNekA== +node-fetch@2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.0.0.tgz#982bba43ecd4f2922a29cc186a6bbb0bb73fcba6" + integrity sha512-bici2HCWFnAghTYMcy12WPxrEwJ5qK7GQJOTwTfyEZjyL99ECWxbYQfabZ2U1zrHMKkOBE97Z9iHIuKQfCMdzQ== + node-fetch@^2.6.7: version "2.6.7" resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad" From 8bff7958429b71f111870fee281db1efbf568c8f Mon Sep 17 00:00:00 2001 From: Alvin Cheng <88267875+cheng-alvin@users.noreply.github.com> Date: Thu, 19 Jan 2023 13:18:15 +1100 Subject: [PATCH 2/3] Add endpoint --- src/endpoints/getGif.ts | 48 +++++++++++++++++++++++++++++++++++++++++ src/index.ts | 2 ++ 2 files changed, 50 insertions(+) create mode 100644 src/endpoints/getGif.ts diff --git a/src/endpoints/getGif.ts b/src/endpoints/getGif.ts new file mode 100644 index 0000000..8313b71 --- /dev/null +++ b/src/endpoints/getGif.ts @@ -0,0 +1,48 @@ +import { NextFunction, Request, Response } from "express"; +import fetch from "node-fetch"; + +import debug from "../utils/debug"; + +/** + * This is the get gif endpoint, this endpoint will return the gif images from a search term, + * or trending is search parameter is not provided. + * + * @param {string} search The search term. + * @returns {Array} Returns the gif images in an array format. + * @see https://developers.giphy.com/docs/api/schema#gif-object for schema def. + */ + +const getGif = async ( + req: Request, + res: Response, + _next: NextFunction +): Promise => { + if (req.query.key !== String(process.env.KEY)) { + res.status(401).send("Invalid api key."); + return; + } + + try { + fetch( + req.query.search === undefined + ? `https://api.giphy.com/v1/gifs/trending?api_key=${process.env.GIPHY_KEY}&limit=30&rating=g` + : `https://api.giphy.com/v1/gifs/search?api_key=${process.env.GIPHY_KEY}&q=${req.query.search}hi&limit=30&offset=0&rating=g&lang=en` + ).then((data: any): void => { + data.json().then((json: any): void => { + res.status(200).send(json.data); + }); + + debug.log( + `Found ${ + req.query.search === undefined ? "trending" : req.query.search + } GIFs` + ); + }); + } catch (err: unknown) { + res.status(500).send(err); + + debug.error(err); + } +}; + +export default getGif; diff --git a/src/index.ts b/src/index.ts index 982fa0c..558674f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -60,6 +60,7 @@ import getPublicRooms from "./endpoints/getPublicRooms"; import uploadContent from "./endpoints/uploadContent"; import content from "./schema/contentSchema"; import connectDatabase from "./utils/connectDatabase"; +import getGif from "./endpoints/getGif"; const app: express.Express = express(); const httpServer: any = createServer(app); @@ -92,6 +93,7 @@ app.get("/site-map", siteMap); app.get("/api/get-users", getUsers); // Deprecated. app.get("/api/get-user-info", getUserInfo); app.get("/api/get-rooms", getAllRooms); +app.get("/api/get-gif", getGif); app.get("/api/get-public-rooms", getPublicRooms); app.post("/api/search-message", searchMessage); app.post("/api/block_user", blockUser); From 8e6a360584283b84aa13c4dc9c1af0e150b0b522 Mon Sep 17 00:00:00 2001 From: Alvin Cheng <88267875+cheng-alvin@users.noreply.github.com> Date: Sat, 28 Jan 2023 07:13:00 +1100 Subject: [PATCH 3/3] Fixed review issue --- src/endpoints/getGif.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/endpoints/getGif.ts b/src/endpoints/getGif.ts index 8313b71..3627742 100644 --- a/src/endpoints/getGif.ts +++ b/src/endpoints/getGif.ts @@ -9,7 +9,7 @@ import debug from "../utils/debug"; * * @param {string} search The search term. * @returns {Array} Returns the gif images in an array format. - * @see https://developers.giphy.com/docs/api/schema#gif-object for schema def. + * @see https://developers.giphy.com/docs/api/schema#gif-object for schema definition. */ const getGif = async (