Skip to content
Permalink
Browse files

refactor: use of async/await everywhere

  • Loading branch information...
NicoPennec committed Sep 29, 2019
1 parent a647762 commit 803487f15d998c828e62f99de1d4dfc00811c96f
Showing with 42 additions and 38 deletions.
  1. +3 −3 README.md
  2. +9 −10 lib/cache.js
  3. +30 −25 lib/middleware.js
@@ -417,9 +417,9 @@ const axios = require('axios')
{
sitemap: {
routes () {
return axios.get('https://jsonplaceholder.typicode.com/users')
.then(res => res.data.map(user => '/users/' + user.username))
routes: async () => {
const { data } = await axios.get('https://jsonplaceholder.typicode.com/users')
return data.map(user => `/users/${user.username}`)
}
}
}
@@ -13,16 +13,15 @@ const unionBy = require('lodash.unionby')
function createRoutesCache(globalCache, options) {
const cache = new AsyncCache({
maxAge: options.cacheTime,
load(_, callback) {
promisifyRoute(options.routes)
.then(routes => joinRoutes(globalCache.staticRoutes ? globalCache.staticRoutes() : [], routes))
.then(routes => {
callback(null, routes)
})
.catch(err => {
/* istanbul ignore next */
callback(err)
})
async load(_, callback) {
try {
let routes = await promisifyRoute(options.routes)
routes = joinRoutes(globalCache.staticRoutes ? globalCache.staticRoutes() : [], routes)
callback(null, routes)
} catch (err) {
/* istanbul ignore next */
callback(err)
}
}
})
cache.get = promisify(cache.get)
@@ -60,38 +60,43 @@ function registerSitemap(options, globalCache, nuxtInstance) {
// Add server middleware for sitemap.xml.gz
nuxtInstance.addServerMiddleware({
path: options.pathGzip,
handler(req, res, next) {
cache.routes
.get('routes')
.then(routes => createSitemap(options, routes, req).toGzip())
.then(gzip => {
res.setHeader('Content-Type', 'application/x-gzip')
res.setHeader('Content-Encoding', 'gzip')
res.end(gzip)
})
.catch(err => {
/* istanbul ignore next */
next(err)
})
async handler(req, res, next) {
try {
// Init sitemap
const routes = await cache.routes.get('routes')
const gzip = await createSitemap(options, routes, req).toGzip()
// Send http response
res.setHeader('Content-Type', 'application/x-gzip')
res.setHeader('Content-Encoding', 'gzip')
res.end(gzip)
} catch (err) {
/* istanbul ignore next */
next(err)
}
}
})
}

// Add server middleware for sitemap.xml
nuxtInstance.addServerMiddleware({
path: options.path,
handler(req, res, next) {
cache.routes
.get('routes')
.then(routes => createSitemap(options, routes, req).toXML())
.then(xml => {
res.setHeader('Content-Type', 'application/xml')
res.end(xml)
})
.catch(err => {
/* istanbul ignore next */
next(err)
})
/**
* @param {Request} req
* @param {Response} res
* @param {*} next
*/
async handler(req, res, next) {
try {
// Init sitemap
const routes = await cache.routes.get('routes')
const xml = await createSitemap(options, routes, req).toXML()
// Send http response
res.setHeader('Content-Type', 'application/xml')
res.end(xml)
} catch (err) {
/* istanbul ignore next */
next(err)
}
}
})
}

0 comments on commit 803487f

Please sign in to comment.
You can’t perform that action at this time.