Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
// src/cosmicFunctions.js
import Cosmic from 'cosmicjs';
export async function getCosmicJsData() {
const api = Cosmic()
const bucket = api.bucket({
slug: process.env.REACT_APP_COSMIC_BUCKET
const arrayOfAllObjectsInBucket = (await bucket.getObjects()).objects;
return organizeCosmicJsDataByObjectType(arrayOfAllObjectsInBucket);
function organizeCosmicJsDataByObjectType(arrayOfCosmicJsData) {
//maps through array of objects, returns object with property for
//each slug-type, with the value being an array of the objects
let organizedData = {}; => {
const typeSlug = object.type_slug;
organizedData.hasOwnProperty(typeSlug) ? '': organizedData[typeSlug] = [];
return organizedData;
return organizedData;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.