Coder Social home page Coder Social logo

cosmicjs / cosmicjs-node Goto Github PK

View Code? Open in Web Editor NEW
274.0 13.0 38.0 4.16 MB

*API v2 JavaScript client for Cosmic. See important message in README

Home Page: https://www.cosmicjs.com

License: MIT License

JavaScript 100.00%
nodejs cosmicjs api cms content-management headless-cms javascript

cosmicjs-node's Introduction

Cosmic NPM module (dashboard v1)

IMPORTANT

This package connects to projects on the old Cosmic dashboard and API v2 only. To connect to projects on the new Cosmic dashboard and API v3 use the Cosmic JavaScript SDK.

Content management made simple, fast, and secure.

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. Build nearly any type of content model using our admin dashboard then deliver your content powered by our reliable infrastructure and global CDN.

Save time and launch faster

Avoid the pain of building, configuring, and maintaining your own CMS infrastructure. Cosmic has all of the features you need out of the box optimized and ready. Plug into Cosmic, save time, and launch your content-powered apps faster.

Features include

🛠️  Powerful content modeling
🔍  Flexible queries
⚡  Customized API response
🌎  Localization
🎨  Image optimization
…and more! See more features →

Get started

Start by going to https://www.cosmicjs.com, create your free account and set up a project. You can get started from scratch, or start with a pre-built template. Then follow the steps below to use this NPM package.

Install

Install the Cosmic NPM module:

pnpm install cosmicjs
# OR
yarn add cosmicjs
# OR
npm install cosmicjs

Import

Import Cosmic into your app:

const Cosmic = require("cosmicjs")
const api = Cosmic()

Connect

In your Cosmic admin dashboard go to Your 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 bucket = api.bucket({
  slug: "YOUR_BUCKET_SLUG",
  read_key: "YOUR_BUCKET_READ_KEY",
})

Get content

Delivering content to your app is simple using the objects.find method.

Get multiple Objects

Use the objects.find method and set the type property to any Object Type slug. Additional options noted below.

const data = await bucket.objects.find({
  type: "products" // Object Type slug
})
.props("title,slug,metadata") // response properties
.limit(10) // number of Objects to be returned

Get single Object by slug

Use the objects.find method and set the slug property to any Object slug.

const data = await bucket.objects.find({
  type: "pages", // Object Type slug
  slug: "home", // Object slug
  locale: "en", // optional, if localization set on Objects
})
.props("title,slug,metadata") // response properties

More examples

For more in-depth guides on getting Cosmic data into your app, you can view these basic query examples. When you are ready to level up, check out advanced query examples.

Add content

The Cosmic API is fully CRUD (create, read, update, delete) capable, enabling user-generated content and file uploads. Check out the add Object and add media examples in the documentation.

Further documentation

See the full documentation for more requests and capabilities.

Community support

For general help, please refer to the official Cosmic documentation. For additional help, you can use one of these channels to ask a question:

  • Slack (For live discussions with the Cosmic community and team)
  • GitHub (Bug reports, contributions)
  • Twitter (Get the latest news about Cosmic features and notifications)
  • YouTube (Learn from video tutorials)

License

This project is published under the MIT license.

cosmicjs-node's People

Contributors

atinux avatar dependabot[bot] avatar f1yn avatar flippedcoder avatar jazibsawar avatar joe2son avatar kgdiem avatar manimalik avatar nileshpatil-dev avatar pcwa-ahendricks avatar robinjayaswal avatar tonyspiro avatar waqasarshad777 avatar weewey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cosmicjs-node's Issues

Add better querying support

Currently, there seems to be no option for selecting the fields that should be included in the response during a getObject or seachObject call. To get a single metafield value from an object, the whole object needs to be downloaded. For simple objects, this doesn't add much overhead. But that's not the case when an object contains large descriptions, articles etc.
Similarly, should be able to select fields on connected objects too.

Failed to minify code

I'm using cosmicjs in a big React app created with create-react-app and I cannot build the project via npm run build without receiving this error:

Creating an optimized production build...
Failed to compile.

Failed to minify the code from this file: 

 	./node_modules/cosmicjs/index.js:6 

Read more here: http://bit.ly/2tRViJ9

Now as the helpful link suggest this means the file is not pre-compiled and that seems to be true, so could you please publish the next version precompiled (if that is indeed the issue here)?

Also, is there anything I can do as a workaround, so that I can import coscmisjs and build the app like I can with any other npm module now? The build happens via CI, so precompiling it manually makes the whole thing more brittle than it should anyway.

I'm open for suggestions. Maybe I'm just doing something wrong myself.

Vulnerability in axios< 0.21.1

Source: https://www.npmjs.com/advisories/1594

Overview

The axios NPM package before 0.21.1 contains a Server-Side Request Forgery (SSRF) vulnerability where an attacker is able to bypass a proxy by providing a URL that responds with a redirect to a restricted host or IP address.

Remediation

Upgrade to 0.21.1 or later.

Getting media by folder not working

When I make the request to get a collection of media in a specific folder, it is returning all the most recent media for my current filter. Looking at the request, folder is not added to the query.

Is this functionality missing, or is there another way to get media folders returned?

Potential braking change with the 4.2 version

Hello,

I was using 4.1 for almost 2 years ago, and my site started to crash last week, debugging my code I realized we stopped getting some of the nested data as we were expecting, I suppose there was a change in the cosmicjs api, I have a structure of nested objects in cosmic, something like this pages -> sections -> videos, the reason why my site started to crash is because of the data of the last object in my nesting chain was different, I was expecting an object with the whole data, instead of that, I was getting a string with just the id.

Before

{
   // rest of the page data,
  metadata: {
    sections: [
      {
        // rest of the object's metadata,
        video: {
          // whole video's metadata
        }
      }
    ]
  }
}

Before

{
   // rest of the page data,
  metadata: {
    sections: [
      {
        // rest of the object's metadata,
        video: '60d10806dd65e129d4f2870b' // just a single string
      }
    ]
  }
}

I updated the package to the latest version and I kept having this issue, this article says there are no breaking changes https://www.cosmicjs.com/changelog/npm-module-updates-v4-2, however, I had to change the implementation from bucket.getObjects to bucket.objects.find with the new specification, only after that, it worked as before again

Unhandled Errors in Fetch Promises

Curious why there is no catch on the fetches or any way to handle a reject. I think this is in most of the code, but I came across it looking at getObject.

  fetch(endpoint)
    .then(function(response){
      if (response.status >= 400) {
        var err = {
          'message': 'There was an error with this request.'
        }
        return callback(err, false);
      }
      return response.json()
    })
    .then(function(response){
      // Constructor
      var cosmic = {};
      var object = response.object;
      var metafields = object.metafields;
      if(metafields){
        object.metafield = _.keyBy(metafields, "key");
      }

If the resources does not exist, Cosmic returns a 404, which passes an error message back to my callback, but then continues to the next then() statement and errors out at response.object. Is there a way that I am supposed to halt the fetch() in my callback to prevent it from executing the final then() when there is an error ?

Node API for search

Hi,

Are there plans for extending this API for interacting with the /search endpoint? It currently looks like the getObjectType interface does not support this case.

Not able to get data to come back.

Hi, I have been trying to run your examples and I am not retrieving any data back to console.log out. It seems to never hit the callback... any ideas?

Update: Looks like its somewhere in the fetch(endpoint) call.

deleteObject not working - returns 'object not found'

Seems like the api endpoint is getting the bucket_slug instead of the object slug and that's why it is returning 'Object not found'.

In line 272 bucket_config contains slug.

After changing that line to something like this return axios.delete(endpoint, { data: Object.assign(bucket_config, {slug: params.slug}) }) deleting was working. Probably it would also work if bucket slug is removed from config before adding it to data.

Published_at query

Hello all! I'd like to get my data filtering by published_at field but I guess that the field type is an String. Is that right? This is my query which is not working.

const data = await bucket.objects .find({ type: "publicacoes", published_at: { $gte: new Date("2021-01-10T10:03:46Z") } }).props("imagem,slug,content,metadata,published_at,title,excerpt") return data.objects;

sort not working for getObjects

In the docs there is a sort property for getObjects but it looks like that's missing in the source code.

After adding

if (params && params.sort) {
    endpoint += `&sort=${params.sort}`
}

it's working.

Is this missing or can I do sorting differently?
I can add these lines to be consistent with the docs.

Cannot read properties of undefined (reading 'COSMIC_API_URL')

Hello,
I'm using Nuxt 3 (SSR) with Cosmic.JS and getting the following error:

Bildschirmfoto 2022-09-06 um 14 46 21

Here is my plugin I created unter plugins/cosmic.ts:

import Cosmic from 'cosmicjs'

const api = Cosmic();
const bucket = api.bucket({
  slug: process.env.COSMIC_JS_BUCKET_SLUG,
  read_key: process.env.COSMIC_JS_READ_KEY,
});

export default defineNuxtPlugin(() => ({
  provide: {
    cosmic: bucket
  }
}));

Maybe it's necessary to check if "process.env" is existing?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.