Skip to content

Instantly share code, notes, and snippets.

Avatar
🚀
Building and shipping

Tony Spiro tonyspiro

🚀
Building and shipping
View GitHub Profile
@tonyspiro
tonyspiro / advanced-query-ne.js
Last active Jun 7, 2022
Get Objects from Cosmic by metadata value (not equal to)
View advanced-query-ne.js
// index.js
const api = require('cosmicjs')() // empty init
const bucket = api.bucket({
slug: 'docs-ecommerce-app-production', // YOUR_BUCKET_SLUG
read_key: '49TG2OEZMVML9fNvVObbFOEsjkFaCYpbVIP1kb8kYCY6RMWqBf' // YOUR_BUCKET_READ_KEY
})
const app = async () => {
const data = await bucket.getObjects({
query: {
type: 'products',
@tonyspiro
tonyspiro / advanced-query-lte.js
Last active Jun 7, 2022
Get Objects from Cosmic by number metadata (less than or equal to)
View advanced-query-lte.js
// index.js
const api = require('cosmicjs')() // empty init
const bucket = api.bucket({
slug: 'docs-ecommerce-app-production', // YOUR_BUCKET_SLUG
read_key: '49TG2OEZMVML9fNvVObbFOEsjkFaCYpbVIP1kb8kYCY6RMWqBf' // YOUR_BUCKET_READ_KEY
})
const app = async () => {
const data = await bucket.getObjects({
query: {
type: 'products',
@tonyspiro
tonyspiro / advanced-query-metadata.js
Last active Jun 7, 2022
Get Objects from Cosmic by metadata value
View advanced-query-metadata.js
// index.js
const api = require('cosmicjs')() // empty init
const bucket = api.bucket({
slug: 'docs-ecommerce-app-production', // YOUR_BUCKET_SLUG
read_key: '49TG2OEZMVML9fNvVObbFOEsjkFaCYpbVIP1kb8kYCY6RMWqBf' // YOUR_BUCKET_READ_KEY
})
const app = async () => {
const data = await bucket.getObjects({
query: {
type: 'products',
@tonyspiro
tonyspiro / basic-query-single.js
Last active Jun 7, 2022
Get Objects from Cosmic by slug
View basic-query-single.js
// index.js
const api = require('cosmicjs')() // empty init
const bucket = api.bucket({
slug: 'simple-react-blog', // YOUR_BUCKET_SLUG
read_key: 'bucket-read-key' // YOUR_READ_KEY
})
const app = async () => {
const data = await bucket.getObjects({
query: {
slug: 'a-wonderful-blog-post-about-earth'
@tonyspiro
tonyspiro / basic-query.js
Last active Jun 7, 2022
Get Objects from Cosmic by type
View basic-query.js
// index.js
const api = require('cosmicjs')() // empty init
const bucket = api.bucket({
slug: 'simple-react-blog', // YOUR_BUCKET_SLUG
read_key: 'bucket-read-key' // YOUR_BUCKET_READ_KEY
})
const app = async () => {
const data = await bucket.getObjects({
query: {
type: 'posts'
View webhook-without-props.json
{
"created_at": 1632927978821,
"data": {
"_id": "61547f5e16bbc1ecc42e2bf6",
"order": 0,
"slug": "new-blog-post",
"title": "New Blog Post UPDATE",
"content": "<p>This is my blog post content...</p>",
"metafields": [
{
View webhook-with-props.json
{
"created_at": 1632927654815,
"data": {
"slug": "new-blog-post",
"title": "New Blog Post UPDATE",
"metadata": {
"headline": "Amazing Beach!",
"image": {
"url": "https://cdn.cosmicjs.com/cc633600-2135-11ec-81a5-2b4858c629b3-quino-al-mBQIfKlvowM-unsplash.jpg",
"imgix_url": "https://imgix.cosmicjs.com/cc633600-2135-11ec-81a5-2b4858c629b3-quino-al-mBQIfKlvowM-unsplash.jpg"
View logout.js
// logout.js
module.exports = (app, config, partials) => {
app.get('/logout', (req, res) => {
req.session.destroy()
return res.redirect('/')
})
}
View add-user.js
app.post('/users', (req, res) => {
const data = req.body
async.series([
callback => {
let user_found = false
Cosmic.getObjectType({ bucket: { slug: config.COSMIC_BUCKET } }, { type_slug: 'users' }, (err, response) => {
_.forEach(response.objects.all, user => {
if (_.find(user.metafields, { key: 'email', value: data.email.trim() }))
user_found = true
})
View users.js
app.get('/users', (req, res) => {
if(!req.session.user)
return res.redirect('/?message=unauthorized')
res.locals.user = req.session.user
async.series([
callback => {
Cosmic.getObjectType({ bucket: { slug: config.COSMIC_BUCKET } }, { type_slug: 'users' }, (err, response) => {
res.locals.users = response.objects.all
callback()
})