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

@thatapicompany/thecatapi

v1.0.2

Published

Client library for TheCatAPI.com

Downloads

214

Readme

Client library for TheCatAPI

Contents

Public API service, all about cats (or dogs), free to use when making your fancy new app, website or service

Installation

This library is published on npm, you can add it as a dependency using the following command

npm install @thatapicompany/thecatapi
# or
yarn add @thatapicompany/thecatapi

Configuration

You'll need to configure the library with your API key, you can grab it from TheCatAPI.

Imports

CommonJS

const { TheCatAPI } = require("@thatapicompany/thecatapi");

ES Modules

import { TheCatAPI } from "@thatapicompany/thecatapi";

initialize the client using your access key:

const theCatAPI = new TheCatAPI("YOUR_API_KEY");

You can also provide a custom hook (useful if you want use the library with TheDogAPI):

const theCatAPI = new TheCatAPI("YOUR_API_KEY", {
  host: "https://api.thedogapi.com/v1",
});

Documentation

The library documentation can be found in docs

Examples

More code samples can be found in examples

Usage

After initiating the client, you can access endpoint methods using the following pattern: [object instance].[endpoint].[method]

For example, getting 5 random images would be: theCatAPI.images.searchImages({ limit: 5 });,

| endpoint | attribute | example | | ----------- | ---------- | -------------------------------------------------------- | | /images | images | client.images.searchImages({ limit: 12 }) | | /favourites | favourites | client.favourites.addFavourite('IMAGE_ID') | | /votes | votes | client.votes.addVote({ imageId: "IMAGE_ID", value: 1}) |

For details on each endpoint accepted values, please reference these docs: docs.thecatapi.com

All methods return a promise containing the returned JSON as a javascript object. Each method of an endpoint maps HTTP methods to

| HTTP Method | method name | example | | ----------- | ---------------- | ------------------------------------- | | POST | add* / upload* | client.images.uploadImage(image) | | GET | get* | client.images.getImages() | | DELETE | delete* | client.images.deleteImage("MY_KEY") |

Example: Fetching random images

We will fetch six random images, by default the API returns only 1 image, unless we specify the limit option, to fetch six images we have to pass { limit: 6 } as an option when calling searchImages

theCatAPI.images
  .searchImages({
    limit: 6,
  })
  .then((images) => {
    console.log(images);
  })
  .catch((error) => {
    // handle error
  });

Using async/await

try {
  const images = await theCatAPI.images.searchImages({
    limit: 6,
  });
  console.log(images);
} catch (error) {
  // handle error
}

Example: Fetching random images of a specific bread

try {
  const images = await theCatAPI.images.searchImages({
    limit: 10,
    breeds: [Breed.CHARTREUX],
  });
  console.log(images);
} catch (error) {
  // handle error
}

Handling Errors

ApiRequestError

Thrown when there's a network or a connectivity issue, for example, if the client didn't establish any network connection with the host

ApiRequestError: getaddrinfo EAI_AGAIN api.thecatapi.com
ApiResponseError

Thrown when the server responds with an HTTP status code not in the 2xx range. ApiRequestError provides two properties to distinguish the type of the error

  • statusCode HTTP status code
  • data the message the server responded with in the body

This is the most common thrown error, you should expect and handle it each time you use any of the library methods

Example: Fetching an image throws ApiResponseError if the image doesn't exist

If we try to fetch an image that doesn't exist

const image = await theCatAPI.images.getImage("BAD_IMAGE_ID");

ApiResponseError will be thrown

ApiResponseError: ApiResponseError (400)
    at ...
    at ...
    at ...
  statusCode: 400,
  data: "Couldn't find an image matching the passed 'id' of BAD_IMAGE_ID"

The server returns a 400 error with the string "Couldn't find an image matching the passed 'id' of BAD_IMAGE_ID" in its body. You can access these properties using error.statusCode and error.data respectively

try {
  const image = await theCatAPI.images.getImage("BAD_IMAGE_ID");
} catch (error) {
  if (error instanceof ApiResponseError) {
    console.log(error.statusCode); // 400
    console.log(error.data); // Couldn't find an image matching the passed 'id' of BAD_IMAGE_ID
  }
}
Error

Unknown error, just a normal javascript error

Handling Errors the Right Way

Since all the possible thrown errors are instances of classes, we can check the type of the thrown error and handle it accordingly

try {
  const image = await theCatAPI.images.getImage("BAD_IMAGE_ID");
} catch (error) {
  if (error instanceof ApiResponseError) {
    // handle response error
  }
  if (error instanceof ApiRequestError) {
    // handle network error
  }
  // handle unknown error
  throw error;
}

Typescript

This library is written in Typescript, types are provided out of the box and can be imported from @thatapicompany/thecatapi/dist/types

Example of usage with Typescript:

import { TheCatAPI } from "@thatapicompany/thecatapi";
import { Image } from "@thatapicompany/thecatapi/dist/types";

const theCatAPI = new TheCatAPI("YOUR_API_KEY");

async function getImageUrls(): Promise<string[]> {
  const images: Image[] = await theCatAPI.images.searchImages({ limit: 10 });
  return images.map((image) => image.url);
}

📙 Further Reading