Skip to content

marzukr/cbp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CBP (Customs & Border Patrol)

A lightweight typescript package to check the types of arbitrary objects.

Example Usage

import {
  objectAgent, arrayAgent, Stamped, stringAgent
} from 'cbp';

const articleAgent = objectAgent({
  title: stringAgent,
  articleSections: arrayAgent(
    objectAgent({
      heading: stringAgent,
      paragraphs: arrayAgent(stringAgent),
    }),
  ),
  imageDescription: stringAgent,
});
type Article = Stamped<typeof articleAgent>;

const getArticle = async () => {
  const response = await fetch("https://myapi.com/get-article");
  const jsonArticle = response.json();

  if (!articleAgent.canStamp(articleRaw)) {
    console.error("The API response was not a valid article");
    return;
  }
  const article: Article = articleAgent.stamp(articleRaw);
  return article;
};

About

Lightweight typescript type checking

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors