-
-
Save markgarrigan/81438f22d32ae14b575398fe40065326 to your computer and use it in GitHub Desktop.
Netlify Function for getting category info from swell api.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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