You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

microcms-lib

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

microcms-lib

Export the MicroCMS schema file and save it in a folder. Execute the command as follows to generate the TypeScript types.

1.0.4
latest
Source
npmnpm
Version published
Weekly downloads
208
-26.5%
Maintainers
1
Weekly downloads
 
Created
Source

microcms-lib

usage

Export the MicroCMS schema file and save it in a folder.
If you use microcms-typescript and execute the command as follows, TypeScript types will be generated.

yarn add -D microcms-typescript
microcms-typescript schema types/cms-types.ts

Import the MicroCMS class and set the EndPoints.

import type { EndPoints } from './types/cms-types';
const cms = new MicroCMS<EndPoints>({service,...keys})

Functions of the MicroCMS class

interface GetOptions<T> {
  draftKey?: string;
  fields?: ReadonlyArray<T>;
  depth?: number;
  globalKey?: boolean;
}
interface GetsOptions<T> {
  draftKey?: string;
  limit?: number;
  offset?: number;
  orders?: string;
  q?: string;
  fields?: T[];
  ids?: string;
  filters?: string;
  depth?: number;
  globalKey?: boolean;
}
class MicroCMS {
  // Constructor
  constructor(options: {
      service: string;
      apiKey?: string;
      apiWriteKey?: string;
      apiGlobalKey?: string;
    });
  // Get a single content
  get(endpoint:string ,id: string,options: GetOptions) : Promise<Contents>;
  // Acquiring multiple contents
  gets(endpoint:string ,id: string,options: GetsOptions) : Promise<{
    contents: Contents[];
    totalCount: number;
    offset: number;
    limit: number;
  }>;
  // Create new content
  post(endpoint: string,params: Contents) : Promise<ID>;
  // Create a new content with ID
  put(endpoint: string, id: string, params: Contents) : Promise<ID>;
  // Rewriting content
  patch(endpoint: string, id: string, params: Contents) : Promise<ID>;
  // Deleting content
  del(id: string) : Promise<boolean>;
}

sample

import type { EndPoints } from './types/cms-types';
import { MicroCMS } from '../src/index';
import { config } from 'dotenv';

config(); // .env

const cms = new MicroCMS<EndPoints>({
  service: process.env.SERVICE!,
  apiKey: process.env.APIKEY,
  apiWriteKey: process.env.APIKEY_WRITE,
  apiGlobalKey: process.env.APIKEY_GLOBAL,
});

const main = async () => {
  // 10 write tests
  for (let i = 0; i < 10; i++) {
    await cms.post('contents', { title: 'Write' + i }).then((id) => console.log(`id:${id}`));
  }

  // loading content
  const result = await cms.gets('contents', {
    limit: 10000,
    fields: ['id', 'title'],
    orders: 'createdAt',
  });
  if (result) {
    const { totalCount, contents } = result;
    console.log(`Acquisition data: ${contents.length}/${totalCount}`);
    contents.forEach(({ id, title }) => {
      console.log(id, title);
    });
  }

  // batch deletion
  if (result)
    for (let i = 0; i < result.contents.length; i++) {
      const ps = new Set();
      const p = cms
        .del('contents', result.contents[i]['id'])
        .then((v) => (v ? console.log(i) : false));
      ps.add(p);
      if (ps.size > 20) {
        await Promise.all(ps);
        ps.clear();
      }
    }
};
main();

Keywords

microcms

FAQs

Package last updated on 21 Apr 2022

Did you know?

Socket

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.

Install

Related posts