@it_dhruv/delta-instagram-feed 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@it_dhruv/delta-instagram-feed",
3
+ "version": "1.0.2",
4
+ "description": "Plug-and-play Instagram Feed API with caching using Instagram Graph API",
5
+ "type": "module",
6
+ "main": "src/index.js",
7
+ "exports": {
8
+ ".": "./src/index.js"
9
+ },
10
+ "files": [
11
+ "src"
12
+ ],
13
+ "keywords": [
14
+ "instagram",
15
+ "instagram-feed",
16
+ "express",
17
+ "api",
18
+ "cache",
19
+ "delta"
20
+ ],
21
+ "author": "DHRUV ITWALA",
22
+ "license": "MIT",
23
+ "dependencies": {
24
+ "node-fetch": "^3.3.2"
25
+ },
26
+ "publishConfig": {
27
+ "access": "public"
28
+ }
29
+ }
package/src/index.js ADDED
@@ -0,0 +1,4 @@
1
+ import instagramRoutes from "./instagram.routes.js";
2
+ import { fetchInstagramFeed } from "./instagram.services.js";
3
+
4
+ export { instagramRoutes, fetchInstagramFeed };
@@ -0,0 +1,21 @@
1
+ import { getCache, isCacheValid, setCache } from "./instagram.util.js";
2
+ import { fetchInstagramFeed } from "./instagram.services.js";
3
+
4
+ export const getInstagramFeed = async (req, res) => {
5
+ try {
6
+ if (isCacheValid() && getCache()) {
7
+ return res.status(200).json(getCache());
8
+ }
9
+
10
+ const feed = await fetchInstagramFeed();
11
+ setCache(feed);
12
+
13
+ return res.status(200).json(feed);
14
+ } catch (error) {
15
+ console.error("Instagram Feed Error:", error.message);
16
+
17
+ return res.status(500).json({
18
+ message: "Unable to fetch Instagram feed",
19
+ });
20
+ }
21
+ };
@@ -0,0 +1,8 @@
1
+ import express from "express";
2
+ import { getInstagramFeed } from "./instagram.controller.js";
3
+
4
+ const instagramRoutes = express.Router();
5
+
6
+ instagramRoutes.get("/feed", getInstagramFeed);
7
+
8
+ export default instagramRoutes;
@@ -0,0 +1,23 @@
1
+ import fetch from "node-fetch";
2
+
3
+ export const fetchInstagramFeed = async () => {
4
+ const fields = ["id", "media_type", "media_url", "permalink"].join(",");
5
+
6
+ const url = `${process.env.INSTAGRAM_BASE_URL}/${process.env.INSTAGRAM_ACCOUNT_ID}/media?fields=${fields}&access_token=${process.env.INSTAGRAM_ACCESS_TOKEN}`;
7
+
8
+ const response = await fetch(url);
9
+
10
+ if (!response.ok) {
11
+ const err = await response.text();
12
+ throw new Error(`Instagram API failed: ${err}`);
13
+ }
14
+
15
+ const result = await response.json();
16
+
17
+ return result.data.filter(
18
+ (post) =>
19
+ post.media_type === "IMAGE" ||
20
+ post.media_type === "CAROUSEL_ALBUM" ||
21
+ post.media_type === "VIDEO"
22
+ );
23
+ };
@@ -0,0 +1,4 @@
1
+ export const INSTAGRAM_MEDIA_TYPES = {
2
+ IMAGE: "IMAGE",
3
+ CAROUSEL: "CAROUSEL_ALBUM",
4
+ };
@@ -0,0 +1,18 @@
1
+ let cache = {
2
+ data: null,
3
+ lastFetched: 0,
4
+ };
5
+
6
+ const CACHE_DURATION = 1000 * 60 * 60 * 3; // 3 hours
7
+
8
+ export const isCacheValid = () =>
9
+ Date.now() - cache.lastFetched < CACHE_DURATION;
10
+
11
+ export const getCache = () => cache.data;
12
+
13
+ export const setCache = (data) => {
14
+ cache = {
15
+ data,
16
+ lastFetched: Date.now(),
17
+ };
18
+ };