Skip to content

Instantly share code, notes, and snippets.

@markgarrigan
Created August 25, 2020 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markgarrigan/81438f22d32ae14b575398fe40065326 to your computer and use it in GitHub Desktop.
Save markgarrigan/81438f22d32ae14b575398fe40065326 to your computer and use it in GitHub Desktop.
Netlify Function for getting category info from swell api.
const ENV = (variable) => {
const {env} = process
const {
PREPEND,
NETLIFY_DEV,
NETLIFY_DEV_VALUE
} = env
const pre = NETLIFY_DEV == NETLIFY_DEV_VALUE ? PREPEND : ''
return env[`${pre}${variable}`]
}
const swell = require('swell-node');
swell.init('luna-caffe-llc', ENV('SWELL_API_KEY'));
exports.handler = async (event, context, callback) => {
const {headers} = event
const {pathname} = new URL(headers.referer)
const pathArray = pathname.split('/')
pathArray.splice(0,2)
const categorySlug = pathArray[0]
const subCategorySlug = pathArray[1]
let {results: categoryResults} = await swell.get('/categories', {
where: {
slug: categorySlug
},
expand: [
'children:10'
],
})
const {0:category} = categoryResults
callback(null, {
statusCode: 200,
body: JSON.stringify({
category,
subCategorySlug
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment