npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@cosmicjs/sdk

v1.0.11

Published

The official client module for Cosmic. This module helps you easily add dynamic content to your website or application using the Cosmic headless CMS.

Downloads

5,449

Readme

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.

Get started free →

Install

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

Import Cosmic into your app using the createBucketClient method.

import { createBucketClient } from '@cosmicjs/sdk';

Authentication

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',
});

Get Objects

Objects are the basic building blocks of content in Cosmic.

Get multiple Objects [see docs]

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.

Get single Object by slug [see docs]

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']);

Create, update, and delete Objects

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',
});

Create Object [see docs]

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'],
  },
});

Update Object [see docs]

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,
  },
});

Delete Object [see docs]

Use the objects.deleteOne() method to delete an Object by specifying the Object id.

await cosmic.objects.deleteOne('5ff75368c2dfa81a91695cec');

Learn more

Go to the Cosmic docs to learn more capabilities.

Community support

For additional help, you can use one of these channels to ask a question:

  • GitHub (Bug reports, contributions)
  • Twitter (Get the latest news about Cosmic features and notifications)
  • YouTube (Learn from video tutorials)

Cosmic support

  • Contact us for help with any service questions and custom plan inquiries.

Contributing

This project uses changeset to manage releases. Follow the following steps to add a changeset:

  • Run npm run changeset command and select type of release with description of changes.
  • When PR with changeset is merged into main branch, Github will create a new PR with correct version change and changelog edits.
  • When codeowner merges the generated PR, it will publish the package and create a Github release.

License

This project is published under the MIT license.