~romuloalves/get-instagram-feed

eb5415c74c9f270e9533910a113c434330b069ce — Rômulo Alves 4 years ago 2bf7c04
Including query param to avoid cache in the request
1 files changed, 16 insertions(+), 3 deletions(-)

M src/index.js
M src/index.js => src/index.js +16 -3
@@ 1,3 1,6 @@
// Native
const {parse} = require('url')

// Packages
const micro = require('micro')
const get = require('micro-get')


@@ 18,13 21,21 @@ const useCache = cache.isCacheActive()
// Server function
const server = micro(get(async function (req, res) {
  // Verify user
  const user = req.url.replace('/', '')
  const {pathname, query} = parse(req.url, true)

  const user = pathname.replace('/', '')
  if (user.length === 0) {
    return send(res, 404, 'User is required.')
  }
  res.setHeader('Content-Type', 'application/json; charset=utf-8')

  if (useCache) {
  let useCacheInRequest = useCache

  if (useCacheInRequest && query.cache === 'false') {
    useCacheInRequest = false
  }

  if (useCacheInRequest) {
    const cacheData = await cache.get(user)
    if (cacheData !== null) {
      return send(res, 200, cacheData)


@@ 33,7 44,9 @@ const server = micro(get(async function (req, res) {

  // Get pictures
  const data = await getPicture(user)
  await cache.set(user, data)
  if (useCacheInRequest) {
    await cache.set(user, data)
  }
  send(res, 200, data)
}))