Skip to content

Commit

Permalink
Restructure files
Browse files Browse the repository at this point in the history
  • Loading branch information
benjick committed Feb 20, 2020
1 parent 7887e0d commit 56dcf0a
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 103 deletions.
26 changes: 26 additions & 0 deletions packages/vsf-storyblok-extension/helpers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import request from 'request'
import { promisify } from 'util'

const rp = promisify(request)

export function getHits (result) {
if (result.body) { // differences between ES5 andd ES7
return result.body.hits
Expand All @@ -17,6 +22,17 @@ export function getHitsAsStory (hits) {
return story
}

export const transformStory = ({ id, ...story } = {}) => {
story.content = JSON.stringify(story.content)
story.full_slug = story.full_slug.replace(/^\/|\/$/g, '')
return {
index: 'storyblok_stories',
type: 'story', // XXX: Change to _doc once VSF supports Elasticsearch 6
id: id,
body: story
}
}

function mapStoryToBulkAction ({ story: { id } }) {
return {
index: {
Expand Down Expand Up @@ -74,3 +90,13 @@ export function queryByPath (path) {
export const log = (string) => {
console.log('📖 : ' + string) // eslint-disable-line no-console
}

export const cacheInvalidate = async (config) => {
if (config.invalidate) {
log(`Invalidating cache... (${config.invalidate})`)
await rp({
uri: config.invalidate
})
log('Invalidated cache ✅')
}
}
95 changes: 0 additions & 95 deletions packages/vsf-storyblok-extension/hook.js

This file was deleted.

27 changes: 21 additions & 6 deletions packages/vsf-storyblok-extension/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Router } from 'express'
import crypto from 'crypto'
import { apiStatus } from '../../../lib/util'
import { getClient } from '../../../lib/elastic'
import { hook } from './hook'
import { fullSync } from './sync'
import { getHits, getHitsAsStory, queryByPath, log } from './helpers'
import { fullSync, handleHook } from './sync'
import { getHits, getHitsAsStory, queryByPath, log, cacheInvalidate } from './helpers'
import { initStoryblokClient } from './storyblok'
import protectRoute from './middleware/protectRoute'

module.exports = ({ config }) => {
if (!config.storyblok || !config.storyblok.previewToken) {
Expand All @@ -15,8 +15,6 @@ module.exports = ({ config }) => {
const db = getClient(config)
const api = Router()

api.use(hook({ config, db }))

const getStory = async (res, path) => {
try {
const response = await db.search(queryByPath(path))
Expand All @@ -28,13 +26,13 @@ module.exports = ({ config }) => {
}
}

// Seed the database on boot
(async () => {
try {
await db.ping()
await fullSync(db, config)
log('Stories synced!')
} catch (error) {
console.log(error)
log('Stories not synced!')
}
})()
Expand Down Expand Up @@ -70,5 +68,22 @@ module.exports = ({ config }) => {
}, 403)
})

api.post('/hook', protectRoute(config), async (req, res) => {
try {
await handleHook(db, config, req.body)
return apiStatus(res)
} catch (error) {
return apiStatus(res, {
error: 'Webhook failed'
})
}
})
api.get('/full', protectRoute(config), async (req, res) => {
await fullSync(db, config)
await cacheInvalidate(config.storyblok)
log('Stories synced!')
return apiStatus(res)
})

return api
}
35 changes: 33 additions & 2 deletions packages/vsf-storyblok-extension/sync.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { storyblokClient } from './storyblok'
import { log, createIndex, createBulkOperations } from './helpers'
import { log, createIndex, createBulkOperations, transformStory, cacheInvalidate } from './helpers'

function indexStories ({ db, stories = [] }) {
const bulkOps = createBulkOperations(stories)
Expand Down Expand Up @@ -39,4 +39,35 @@ const fullSync = async (db, config) => {
await syncStories({ db, perPage: config.storyblok.perPage })
}

export { syncStories, fullSync }
const handleHook = async (db, config, params) => {
const cv = Date.now() // bust cache
const { story_id: id, action } = params

switch (action) {
case 'published':
const { data: { story } } = await storyblokClient.get(`cdn/stories/${id}`, {
cv,
resolve_links: 'url'
})
const publishedStory = transformStory(story)

await db.index(publishedStory)
log(`Published ${story.full_slug}`)
break

case 'unpublished':
const unpublishedStory = transformStory({ id })
await db.delete(unpublishedStory)
log(`Unpublished ${id}`)
break

case 'branch_deployed':
await fullSync(db, config)
break
default:
break
}
await cacheInvalidate(config.storyblok)
}

export { syncStories, fullSync, handleHook }

0 comments on commit 56dcf0a

Please sign in to comment.