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

@contentstack/types-generator

v2.0.2

Published

Contentstack type definition generation library

Downloads

120

Readme

Contentstack - TypeScript generation library

This library helps to generate TypeScript type definition for the content types available in a Stack.

Installation

$ npm install @contentstack/types-generator

Usage

In NodeJs

require("@contentstack/types-generator") for Common JS (CJS)

OR

import {<< required method >>} from "@contentstack/types-generator" for ECMAScript Modules (ESM)

In Web application

import {<< required method >>} from "@contentstack/types-generator/dist/web"

Usage Guide

1. generateTS() (Available for both NodeJS and Web application)

This is an asynchronous method which generates Typescript type definition of the content types available in a Stack using given inputs. Use this method for REST API.

Input:

| Property Name | Description | Data type | Accepted values | Mandatory | Default value | | -------------------- | ---------------------------------------------------------------------------------- | --------- | ------------------------------------------------------------ | --------- | ------------- | | token | Unique identifier used for authorization | String | | Yes | | | tokenType | Type of token being provided (Currently we are supporting only delivery token) | String | delivery | Yes | | | apiKey | Stack API key | String | | Yes | | | environment | Name of the environment (example: development, staging, production) | String | | Yes | | | region | Contentstack API region | String | US (for AWS NA), EU (for AWS EU), AZURE_NA, AZURE_EU, GCP_NA | Yes | | | branch | Stack branch name | String | | No | | | prefix | Optional prefix to add for each interface | String | | No | | | includeDocumentation | To add content type documentation in the generated file | boolean | true, false | No | true | | systemFields | Boolean flag indicating whether to include system-generated fields in the response | boolean | true, false | No | false |

Output:

Returns a Promise that resolves with data or rejects with an error.

If resolved:

Type: String

Data: Generated Typescript type definition

If rejected:

Type: Error Object

Data: An object with error_message

Example usage: generateTS()

import { generateTS } from "@contentstack/types-generator"; // Import statement for NodeJS
import { generateTS } from "@contentstack/types-generator/dist/web"; // Import statement for Web application

async function getTypeDef() {
  try {
    const typeDef = await generateTS({
      token: "<< your_delivery_token >>",
      tokenType: "delivery", // Currently we are supporting only delivery token
      apiKey: "<< your_stack_api_key >>",
      environment: "development",
      region: "US",
      branch: "main",
      prefix: "CS",
      includeDocumentation: true,
      systemFields: false,
    });

    // Handle the resolved promise, e.g., process the typeDef
  } catch (error) {
    // Handle the rejected promise
    // error: { error_message: "Unauthorized! Please check the given token and api key" }
  }
}

getTypeDef();

Example output: generateTS()

/** This is a description. */
interface BuiltinExample {
  /** Title */
  title: string;
  /** URL */
  url: string;
  /** Group1 */
  group1?: {
    /** Group2 */
    group2?: {
      /** Group3 */
      group3?: {
        /** Number */
        number?: number;
      };
    };
  };
  /** SEO */
  seo?: Seo;
  /** Single line textbox */
  single_line?: string;
  /** Multi line textbox */
  multi_line?: string;
  /** Rich text editor */
  rich_text_editor?: string;
  /** Multiple Single Line Textbox */
  multiple_single_line_textbox?: string[];
  /** Markdown */
  markdown?: string;
  /** Multiple Choice */
  multiple_choice?: ("Choice 1" | "Choice 2" | "Choice 3")[];
  /** Single Choice */
  single_choice: "Choice 1" | "Choice 2" | "Choice 3";
  /** Modular Blocks */
  modular_blocks?: ModularBlocks[];
  /** Number */
  number?: number;
  /** Link */
  link?: Link;
  /** File */
  file?: File;
  /** Boolean */
  boolean?: boolean;
  /** Date */
  date?: string;
}

interface ModularBlocks {
  block_1: {
    /** Number */
    number?: number;
    /** Single line textbox */
    single_line?: string;
  };
  block_2: {
    /** Boolean */
    boolean?: boolean;
    /** Date */
    date?: string;
  };
  seo_gf: {
    /** Keywords */
    keywords?: string;
    /** Description */
    description?: string;
  };
}

2. graphqlTS() (Available only for NodeJS)

This is an asynchronous method which generates Typescript type definition of the content types available in a Stack using given inputs for GraphQL. Use this method for GraphQL.

Input:

| Property Name | Description | Data type | Accepted values | Mandatory | | ------------- | ----------------------------------------------------------------------------------------- | --------- | ------------------------------------------------------------ | --------- | | token | Unique identifier used for authorization. This should be the delivery token of the stack. | String | | Yes | | apiKey | Stack API key | String | | Yes | | environment | Name of the environment (example: development, staging, production) | String | | Yes | | region | Contentstack API region | String | US (for AWS NA), EU (for AWS EU), AZURE_NA, AZURE_EU, GCP_NA | Yes | | branch | Stack branch name | String | | No | | namespace | Identifies the specific namespace within schema | String | | No |

Output:

Returns a Promise that resolves with data or rejects with an error.

If resolved:

Type: String

Data: Generated Typescript type definition

If rejected:

Type: Error Object

Data: An object with error_message

Example usage: graphqlTS()

import { graphqlTS } from "@contentstack/types-generator"; // Import statement for NodeJS

async function getTypeDef() {
  try {
    const typeDef = await graphqlTS({
      token: "<< your_delivery_token >>", // Currently we are supporting only delivery token
      apiKey: "<< your_stack_api_key >>",
      environment: "development",
      region: "US",
      branch: "main",
      namespace: "<< your_name_space >>",
    });

    // Handle the resolved promise, e.g., process the typeDef
  } catch (error) {
    // Handle the rejected promise
    // error: { error_message: "Unauthorized! Please check the given token and api key" }
  }
}

getTypeDef();