Skip to content

Commit

Permalink
Merge pull request #10 from Chillandchat/get-gif-endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
cheng-alvin authored Jan 27, 2023
2 parents 8116833 + 8e6a360 commit 8583f5f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
48 changes: 48 additions & 0 deletions src/endpoints/getGif.ts
Original file line number Diff line number Diff line change
@@ -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<unknown>} Returns the gif images in an array format.
* @see https://developers.giphy.com/docs/api/schema#gif-object for schema definition.
*/

const getGif = async (
req: Request,
res: Response,
_next: NextFunction
): Promise<void> => {
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;
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
import deleteUser from "./endpoints/deleteUser";

const app: express.Express = express();
Expand Down Expand Up @@ -93,6 +94,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/delete-user", deleteUser);
app.post("/api/search-message", searchMessage);
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 8583f5f

Please sign in to comment.