-
Notifications
You must be signed in to change notification settings - Fork 2
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 #2 from magebitcom/2.0.0
2.0.0
- Loading branch information
Showing
9 changed files
with
101 additions
and
173 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,73 @@ | ||
import { Router } from 'express' | ||
import { apiStatus } from '../../../lib/util' | ||
import cache from '../../../lib/cache-instance' | ||
import { Magento2Client } from 'magento2-rest-client' | ||
import { sha3_224 } from 'js-sha3' | ||
|
||
export function extendClient (client: Magento2Client) { | ||
client.addMethods('instagram', restClient => ({ | ||
getPosts () { | ||
return restClient.get('/magebit-instagramfeed/media') | ||
} | ||
})) | ||
} | ||
|
||
async function saveToCache (req: any, result: any) { | ||
if (!cache) return | ||
|
||
try { | ||
await (cache as any).set( | ||
'api:' + sha3_224(`${JSON.stringify(req.body)}${req.url}`), | ||
result, | ||
['instagram'], | ||
1800 | ||
) | ||
} catch (e) { | ||
console.error('Failed to cache instagram:', e) | ||
} | ||
} | ||
|
||
async function loadFromCache (req: any) { | ||
if (!cache) return false | ||
|
||
const data = await (cache as any).get( | ||
'api:' + sha3_224(`${JSON.stringify(req.body)}${req.url}`) | ||
) | ||
|
||
return data | ||
} | ||
|
||
module.exports = ({ config }) => { | ||
const api = Router() | ||
const client = Magento2Client(config.magento2.api) | ||
extendClient(client) | ||
|
||
api.get('/feed', async (req, res) => { | ||
const { width, height } = req.query; | ||
const cached = await loadFromCache(req) | ||
|
||
if (cached) { | ||
res.setHeader('X-VS-Cache', 'Hit') | ||
return apiStatus(res, cached) | ||
} | ||
|
||
try { | ||
const posts = await client.instagram.getPosts() | ||
|
||
const items = posts.map(post => { | ||
post.media_url_thumb = `${config.server.url || ''}/img/?url=${encodeURIComponent(post.thumbnail_url || post.media_url)}&width=${width || 200}&height=${height || 200}&action=fit`; | ||
return post | ||
}) | ||
|
||
if (config.server.useOutputCache) { | ||
saveToCache(req, items) | ||
} | ||
|
||
return apiStatus(res, items) | ||
} catch (e) { | ||
apiStatus(res, 'Failed to fetch posts: ' + e.message, 500) | ||
} | ||
}) | ||
|
||
return api | ||
} |
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 |
---|---|---|
@@ -1,17 +1,10 @@ | ||
{ | ||
"name": "vue-storefront-instagram-api", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "Standalone offline ready instagram feed extension for Vue Storefront", | ||
"main": "index.js", | ||
"keywords": [], | ||
"author": "Lorens Milovanovs- Lācis <lorens.milovanovs.lacis@magebit.com>", | ||
"author": "Magebit <info@magebit.com>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"express": "^4.16.3", | ||
"request": "^2.88.0" | ||
}, | ||
"devDependencies": { | ||
"babel-preset-env": "^1.7.0", | ||
"babel-preset-stage-0": "^6.24.1" | ||
} | ||
"dependencies": {} | ||
} |
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
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
{ | ||
"name": "vue-storefront-instagram-feed", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "Standalone offline ready instagram feed extension for Vue Storefront", | ||
"main": "index.ts", | ||
"keywords": [], | ||
"author": "Lorens Milovanovs- Lācis <lorens.milovanovs.lacis@magebit.com>", | ||
"author": "Magebit <info@magebit.com>", | ||
"license": "MIT" | ||
} |
Oops, something went wrong.