From e206106b8fe024d813486ecfb98ed9cb693ca275 Mon Sep 17 00:00:00 2001 From: Alid Castano Date: Fri, 4 Aug 2017 19:30:09 -0400 Subject: [PATCH] added query and findOnly to static build CLOSES #38 --- lib/content/api.js | 14 +++++----- lib/content/build.js | 58 +++++++++++++++++++++++++++-------------- lib/content/database.js | 22 ++++++++-------- lib/module.js | 4 +-- lib/plugin.js | 4 +-- 5 files changed, 60 insertions(+), 42 deletions(-) diff --git a/lib/content/api.js b/lib/content/api.js index c81397c..2b94e73 100644 --- a/lib/content/api.js +++ b/lib/content/api.js @@ -13,7 +13,7 @@ export default function createRouter (options) { if (!options.content['/']) { router.use("/", new Router().get('/', (req, res) => { response(res).json({ - 'content-endpoints': Object.keys(options.content) + 'content-endpoints': Object.keys(options.content) }) })) } @@ -36,17 +36,17 @@ function curryResponseHandler (endpoint, options) { return function sendContent (req, res) { const send = response(res) const permalink = req.params['0'] - const [_, query] = req.url.match(/\?(.*)/) || [] - const { only, between, ...params } = parse(query) + const [_, queryStr] = req.url.match(/\?(.*)/) || [] + const { only, between, ...query } = parse(queryStr) logRequest(permalink, api.serverPrefix, api.baseURL) if (permalink === '/') { // request multiple pages from directory - if (between) send.json(db.findBetween(between, params)) - else if (only) send.json(db.findOnly(only, params)) - else send.json(db.findAll(params)) + if (between) send.json(db.findBetween(between, query)) + else if (only) send.json(db.findOnly(only, query)) + else send.json(db.findAll(query)) } else { // request single page - if (db.exists(permalink)) send.json(db.find(permalink, params)) + if (db.exists(permalink)) send.json(db.find(permalink, query)) else send.notFound() } } diff --git a/lib/content/build.js b/lib/content/build.js index e626f37..984fef5 100644 --- a/lib/content/build.js +++ b/lib/content/build.js @@ -28,42 +28,60 @@ const asset = (object) => { // webpack asset export default function buildContent ({ nuxt, options }) { const { sitePath, srcDir, content } = options - let routeData = [] - let dynamicRoutePaths = new Map() - let assetMap = new Map() + let routePages = [] // dynamic pages to create + let routePaths = new Map() // paths to reconfigure + let assetMap = new Map() // browser assets to generate Object.keys(content).forEach(dirName => { - const dirOpts = content[dirName] + const { page, generate, permalink } = content[dirName] const contentPath = join(sitePath, srcDir) - if (!dirOpts.routes) return - const { findAll, query } = createDatabase(contentPath, dirName, options) + if (!generate) return - dirOpts.routes.forEach(route => { - if (!route.path || !route.method) throw new Error('Routes must have a path and a method') - const name = routeName(route.path) - switch (route.method) { + const db = createDatabase(contentPath, dirName, options) + + generate.forEach(reqType => { + const req = {} + if (typeof reqType === 'string') { + req['method'] = reqType + } else if (Array.isArray(reqType)) { + const [reqMethod, reqOptions] = reqType + req['method'] = reqMethod + req['query'] = reqOptions.query ? reqOptions.query : {} + req['args'] = reqOptions.args ? reqOptions.args : [] + } + + switch (req['method']) { case 'get': + if (!page) throw new Error('You must specify a page path') + const name = routeName(page) const pathPrefix = getPrefix(name) - dynamicRoutePaths.set(name, dirOpts.permalink.replace(/^\//, '')) - findAll().forEach((page) => { - routeData.push({ route: join(pathPrefix, page.permalink)/* , payload: page */ }) + routePaths.set(name, permalink.replace(/^\//, '')) + db.findAll(req['query']).forEach((page) => { + routePages.push(join(pathPrefix, page.permalink)) assetMap.set(buildPath(page.permalink, dirName, options), page) }) break case 'getAll': - // routeData.push({ route: name, payload: findAll() }) - assetMap.set(buildPath('_all', dirName, options), findAll()) + assetMap.set( + buildPath('_all', dirName, options), + db.findAll(req['query']) + ) + break + case 'getOnly': + assetMap.set( + buildPath('_only', dirName, options), + db.findOnly(req['args'], req['query']) + ) break - // case 'query': - // break default: - throw new Error(`Generate route method "${route.method}" does not exist.`) + throw new Error(`The ${req['method']} is not supported for static builds.`) } }) }) - interceptRoutes(nuxt, dynamicRoutePaths) - addRoutes(nuxt.options, routeData) + + interceptRoutes(nuxt, routePaths) + addRoutes(nuxt.options, routePages) addAssets(nuxt.options, assetMap) } diff --git a/lib/content/database.js b/lib/content/database.js index f72395b..cd99fbb 100644 --- a/lib/content/database.js +++ b/lib/content/database.js @@ -24,11 +24,11 @@ export default function createDatabase (contentPath, dirName, options) { return pagesMap.has(permalink) }, - find (permalink, params) { - return pagesMap.get(permalink).create(params) + find (permalink, query) { + return pagesMap.get(permalink).create(query) }, - findOnly(onlyArg, params) { + findOnly(onlyArg, query) { if (typeof onlyArg === 'string') onlyArg = onlyArg.split(',') const [startIndex, endIndex] = onlyArg @@ -41,16 +41,16 @@ export default function createDatabase (contentPath, dirName, options) { currIndex++ } - return pages.map(page => page.create(params)) + return pages.map(page => page.create(query)) }, - findBetween(betweenStr, params) { + findBetween(betweenStr, query) { const { findOnly } = this const [currPermalink, numStr1, numStr2] = betweenStr.split(',') if (!pagesMap.has(currPermalink)) return [] - const currPage = pagesMap.get(currPermalink).create(params) + const currPage = pagesMap.get(currPermalink).create(query) const { index } = currPage.meta const total = pagesArr.length -1 @@ -67,14 +67,14 @@ export default function createDatabase (contentPath, dirName, options) { if (num2 === 0 || (!num2 && num1 === 0)) afterRange = [] else afterRange = [min(index + 1, total), min(index + (num2 || num1), total)] - const beforePages = findOnly(beforeRange, params) - const afterPages = findOnly(afterRange, params) - + const beforePages = findOnly(beforeRange, query) + const afterPages = findOnly(afterRange, query) + return [...beforePages, currPage, ...afterPages] }, - findAll (params) { - return pagesArr.map(page => page.create(params)) + findAll (query) { + return pagesArr.map(page => page.create(query)) } } } diff --git a/lib/module.js b/lib/module.js index 4ba9e41..2f5bff6 100644 --- a/lib/module.js +++ b/lib/module.js @@ -47,11 +47,11 @@ export default function ContentModule(moduleOpts) { buildDir: `/content`, content: contentOptions(userOptions.content, { + page: null, permalink: ':slug', anchorsLevel: 1, isPost: true, - data: {}, - routes: null + generate: [] }), parsers: { diff --git a/lib/plugin.js b/lib/plugin.js index 7dc3bc5..3e3d3ae 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -67,13 +67,13 @@ export default ({ app, isClient, isServer }) => { return await fetchContent(contentDir, permalink, '?' + query) }, async getBetween (permalink, num1or2, num2 = '') { - const endpoint = isAPI ? '/' : '_between' // TODO + const endpoint = isAPI ? '/' : '_between' const betweenQuery = 'between=' + [permalink, num1or2, num2].join(',') const fullQuery = '?' + betweenQuery + '&' + query return await fetchContent(contentDir, endpoint, fullQuery) }, async getOnly (startIndex, endIndex) { - const endpoint = isAPI ? '/' : '_only' // TODO + const endpoint = isAPI ? '/' : '_only' const onlyQuery = 'only=' + [startIndex, endIndex].join(',') const fullQuery = '?' + onlyQuery + '&' + query return await fetchContent(contentDir, endpoint, fullQuery)