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

@neuroomnet/cms-client

v1.7.0

Published

This is a client library for the Neuroomnet CMS. It is used to interact with the CMS API.

Downloads

35

Readme

@neuroomnet/cms-client

This is a client library for the Neuroomnet CMS. It is used to interact with the CMS API.

Features

  • Create, read, update, and remove documents
  • Check if a document exists
  • Count and find one or more documents
  • Create, read, update, and remove the schema
  • Create and download files
  • Use a cache to work offline
  • Use a custom storage to store and retrieve data

Installation

npm install @neuroomnet/cms-client

Usage

Create a client

Create a client instance using the CmsClient class. The client instance is used to interact with the CMS.

import { CmsClient } from '@neuroomnet/cms-client'

const client = new CmsClient({
    baseURL: 'https://cms.neuroom.net',
    auth: {
        username: 'username',
        password: 'password',
    },
})

Create a collection

To interact with documents, you need to create a collection instance using the collection method on the client. Supply the collection function with a type that represents the schema of the documents in the collection to get type safety.

import { CmsString, CmsNumber } from '@neuroomnet/cms-client'

type Cat = {
    name: CmsString
    age: CmsNumber
    breed?: CmsString
}

const Cat = client.collection<Cat>('neuroomnet', 'cats')

Infering the type from the schema

You can use the InferType utility type to infer the object type from the schema. This is useful when you need to define the schema anyway.

import { CmsSchemaObject, InferType } from '@neuroomnet/cms-client'

const CatSchema = {
    name: { type: 'string' },
    age: { type: 'number' },
    breed: { type: 'string', optional: true },
} satisfies CmsSchemaObject

type Cat = InferType<typeof CatSchema>

const Cat = client.collection<Cat>('neuroomnet', 'cats')

Interact with documents

You can create, read, update, and remove documents using the create, read, update, and remove methods. You can also check if a document exists using the exists method.

const { _id } = await Cat.create({
    name: 'Whiskers',
    age: 3,
    breed: 'Siamese',
})

const whiskers = await Cat.read(_id) // { _id: '...', _cms: { ... }, name: 'Whiskers', age: 3, breed: 'Siamese' }

await Cat.update(_id, { breed: 'Tabby' }) // { ..., breed: 'Tabby' }

await Cat.remove(_id) // { ... }

await Cat.exists(_id) // false

Query documents

You can count and find one or more documents using the count, find, and findOne methods. You can also filter, sort, and paginate the results.

const catsTotal = await Cat.count()
const tabbiesTotal = await Cat.count({ filter: { breed: 'Tabby' } })

const allCats = await Cat.find()
const tabbies = await Cat.find({ filter: { breed: 'Tabby' } })
const adultCats = await Cat.find({ filter: { age: { $gte: 2 } } })

const whiskers = await Cat.findOne({ filter: { name: 'Whiskers' } })
const oldestCat = await Cat.findOne({ sort: { age: 'descending' } })

const top10Cats = await Cat.find({
    filter: { age: { $lte: 2 } },
    sort: { name: 'ascending' },
    pageSize: 10,
})

Interact with the schema

You can create, read, and update the schema using the create, read, and update methods. The schema is used to define the structure of the documents in the collection.

const schema = await Cat.schema.read() // { ..., fields: { name: { type: 'string' }, age: { type: 'number' }, breed: { type: 'string', optional: true } } }

await Cat.schema.update({
    ...schema,
    fields: {
        ...schema.fields,
        image: { type: 'file', mimeType: ['image/jpeg', 'image/png'], optional: true },
    },
})

Interact with files

When you have a file field in your schema, you can upload and download files using the create and download methods.

const whiskers3rdBirthday = fs.readFileSync('whiskers-3rd-birthday.jpg')

const whiskers = await Cat.create(
    {
        name: 'Whiskers',
        age: 3,
        breed: 'Siamese',
        image: {
            name: 'whiskers 3rd birthday',
            path: 'whiskers-3rd-birthday.jpg',
            mime: 'image/jpeg',
            size: whiskers3rdBirthday.length,
        },
    },
    {
        'whiskers-3rd-birthday.jpg': whiskers3rdBirthday,
    }
)

const whiskersImage = await Cat.download(whiskers.image)

console.log(whiskersImage) // <Buffer ...>
console.log(whiskers3rdBirthday.equals(whiskersImage)) // true

const token = await client.getToken()

const whiskersImageUrl = Cat.getFileUrl(whiskers.image, token)

console.log(whiskersImageUrl) // https://cms.neuroom.net/media/neuroomnet/cats/whiskers-3rd-birthday.jpg?token=...

Use a cache

This library uses axios-cache-interceptor to optionally cache requests. The second argument of the CmsClient constructor is an options object for the cache. You can use the buildMemoryStorage, buildWebStorage, or buildFsStorage function to create a cache instance.

Note: The buildFsStorage function is only available in the Node.js environment. Import it from @neuroomnet/cms-client/node.

import { CmsClient, buildFsStorage } from '@neuroomnet/cms-client/node'

const client = new CmsClient(
    {
        baseURL: 'https://cms.neuroom.net',
        auth: {
            username: 'username',
            password: 'password',
        },
    },
    {
        storage: buildFsStorage({ directory: 'cache' }),
    }
)

Use a custom storage

Use the buildStorage function to create a storage instance that stores and retrieves data from a custom source.

import { CmsClient, buildStorage } from '@neuroomnet/cms-client'

const myCustomStorage = buildStorage({
    async find(key) {
        // ...
    },
    async remove(key) {
        // ...
    },
    async set(key, value) {
        // ...
    },
})

const client = new CmsClient(
    {
        baseURL: 'https://cms.neuroom.net',
        auth: {
            username: 'username',
            password: 'password',
        },
    },
    {
        storage: myCustomStorage,
    }
)