Security News
Opengrep Emerges as Open Source Alternative Amid Semgrep Licensing Controversy
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
@cosmicjs/sdk
Advanced tools
The official client module for Cosmic. This module helps you easily add dynamic content to your website or application using the Cosmic headless CMS.
Cosmic is a headless CMS (content management system) that provides a web dashboard to create content and an API toolkit to deliver content to any website or application. Nearly any type of content can be built using the dashboard and delivered using this SDK.
Install the Cosmic JavaScript SDK. We recommend using the bun package manager.
bun add @cosmicjs/sdk
# OR
yarn add @cosmicjs/sdk
# OR
npm install @cosmicjs/sdk
Import Cosmic into your app using the createBucketClient
method.
import { createBucketClient } from '@cosmicjs/sdk';
In the Cosmic admin dashboard go to Bucket > Settings > API Access and get your Bucket slug and read key then set the variables in your app to connect to your Bucket.
const cosmic = createBucketClient({
bucketSlug: 'BUCKET_SLUG',
readKey: 'BUCKET_READ_KEY',
});
Objects are the basic building blocks of content in Cosmic.
Use the objects.find()
method to fetch Objects.
const posts = await cosmic.objects
.find({
type: 'posts',
})
.props(['title', 'slug', 'metadata'])
.limit(10);
The above example fetches Objects in the posts
Object type returning the title
, slug
, and metadata
properties, limiting the response to 10
Objects.
Use the objects.findOne()
method with type
and slug
to fetch a single Object.
const post = await cosmic.objects
.findOne({
type: 'pages',
slug: 'home',
})
.props(['title', 'slug', 'metadata']);
To write to the Cosmic API, you will need to set the Bucket write key found in Bucket > Settings > API Access. (NOTE: never expose your write key in any client-side code)
const cosmic = createBucketClient({
bucketSlug: 'BUCKET_SLUG',
readKey: 'BUCKET_READ_KEY',
writeKey: 'BUCKET_WRITE_KEY',
});
Use the objects.insertOne()
method to create an Object.
await cosmic.objects.insertOne({
title: 'Blog Post Title',
type: 'posts',
metadata: {
content: 'Here is the blog post content... still learning',
seo_description: 'This is the blog post SEO description.',
featured_post: true,
tags: ['javascript', 'cms'],
},
});
Use the objects.updateOne()
method to update an Object by specifying the Object id
and include properties that you want to update.
await cosmic.objects.updateOne('5ff75368c2dfa81a91695cec', {
metadata: {
content: 'This is the updated blog post content... I got it now!',
featured_post: false,
},
});
Use the objects.deleteOne()
method to delete an Object by specifying the Object id
.
await cosmic.objects.deleteOne('5ff75368c2dfa81a91695cec');
Go to the Cosmic docs to learn more capabilities.
For additional help, you can use one of these channels to ask a question:
This project uses changeset to manage releases. Follow the following steps to add a changeset:
npm run changeset
command and select type of release with description of changes.main
branch, Github will create a new PR with correct version change and changelog edits.codeowner
merges the generated PR, it will publish the package and create a Github release.This project is published under the MIT license.
1.3.1
FAQs
The official client module for Cosmic. This module helps you easily add dynamic content to your website or application using the Cosmic headless CMS.
The npm package @cosmicjs/sdk receives a total of 481 weekly downloads. As such, @cosmicjs/sdk popularity was classified as not popular.
We found that @cosmicjs/sdk demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.