Coder Social home page Coder Social logo

comic-vine's Introduction

Comic Vine SDK

The Comic Vine SDK provides convenient access to the Comic Vine API from applications written in JavaScript/TypeScript. The API provides full access to the structured-wiki content.

Table of Contents

Installation

Install the package with:

npm install comic-vine-sdk
# or
yarn add comic-vine-sdk

TypeScript Typings

There's a good change you may find an issue with the typings in the API response objects. They were generated using sample data from the API, if you find a problem open an issue detailing the problem along with the request details so I can add that request to the sample dataset. While you wait for it to be fixed add // @ts-expect-error above the line causing the problem. This will allow you to compile in the meantime but will flag when the problem has been fixed.

Roadmap

  • Expandable responses

  • Cached responses

  • Rate limiting

Comic Vine Resources

Comic Vine resources list

The library exposes an object for each Comic Vine resource, the object names are singular and expose a retrieve method that maps to the singular resource and a list method that maps to the plural resource.

The following table lists the resources that have been implemented and how the retrieve and list methods map to the API. Most resources are a direct mapping but object has been mapped to thing, this is due to object being a reserved word in JS and thing matches the Comic Vine wiki.

Library resource object Retrieve Method API Resource List Method API Resource
character character characters
concept concept concepts
episode episode episodes
issue issue issues
location location locations
movie movie movies
origin origin origins
person person people
power power powers
promo promo promos
publisher publisher publishers
series series series_list
storyArc story_arc story_arcs
team team teams
thing object objects
video video videos
videoCategory video_category video_categories
videoType video_type video_types
volume volume volumes

Usage/Examples

Initialization

The package needs to be configured with your API key, Grab an API key. Require it with the key's value:

const ComicVine = require('comic-vine-sdk');
const comicVine = new ComicVine('your-api-key-here');

comicVine.publisher
  .retrieve(1859)
  .then((customer) => console.log(customer.id))
  .catch((error) => console.error(error));

Or using ES modules and async/await:

import ComicVine from 'comic-vine-sdk';
const comicVine = new ComicVine('your-api-key-here');

(async () => {
  try {
    const publisher = await comicVine.publisher.retrieve(1859);
    console.log(publisher.name);
  } catch (error) {
    console.error(error);
  }
})();

Options

The second parameter of the constructor accepts options to configure the library

new ComicVine('your-api-key-here', options);

baseUrl

Type: string | undefined

Default: https://comicvine.gamespot.com/api/

If using this package in node this should not need set, the default value will work.

If using the package in a web browser then The Comic Vine API does not allow cross-origin requests. This option could be used to proxy the request assuming you have some safe way for the web client to fetch your api key, you don't want to send the api key to the browser in your JS bundle.

import ComicVine from 'comic-vine-sdk';

// This is just an example, to try it out you would
// have to visit (https://cors-anywhere.herokuapp.com)
// to request temporary access.
const comicVine = new ComicVine('your-api-key-here', {
  baseUrl: 'https://cors-anywhere.herokuapp.com/https://www.comicvine.com/api/',
});

(async () => {
  try {
    const publisher = await comicVine.publisher.retrieve(1859);
    console.log(publisher.name);
  } catch (error) {
    console.error(error);
  }
})();

Fetch a single resource

All resources have a retrieve method, the following example retrieves a publisher

import ComicVine from 'comic-vine-sdk';
const comicVine = new ComicVine('your-api-key-here');

(async () => {
  try {
    const publisher = await comicVine.publisher.retrieve(1859);
    console.log(publisher.name);
  } catch (error) {
    console.error(error);
  }
})();

Fetch a resource list

All resources have a retrieve method, the following example retrieves a list of publishers

import ComicVine from 'comic-vine-sdk';
const comicVine = new ComicVine('your-api-key-here');

(async () => {
  try {
    const publishers = await comicVine.publisher.list();
    console.log(publishers.data);
  } catch (error) {
    console.error(error);
  }
})();

Limit the fields in the response payload

When making a request it's likely that only certain properties are required. Both the retrieve and list methods accept options as the second parameter. This can be used to specify the field list.

When using TypeScript this is type safe, the return type is narrowed to the field list so that intellisense only displays the properties available in the response.

import ComicVine from 'comic-vine-sdk';
const comicVine = new ComicVine('your-api-key-here');

(async () => {
  try {
    const issue = await comicVine.issue.retrieve(id, {
      fieldList: ['id', 'name', 'description'],
    });

    // The id property is in the fieldList and will be available
    console.log(issue.id);

    // In JS dateAdded will be undefined at runtime
    // in TS the compiler will produce an error because it wasn't in the fieldList
    console.log(issue.dateAdded);

    // An object containing the id, name and description properties
    console.log(issue);
  } catch (error) {
    console.error(error);
  }
})();

Pagination

The Comic Vine API provides offset based pagination, this is done by providing a limit and offset in the request. The limit is the number of items to be returned in one page and the offset is the number of items to skip.

To fetch a page with 50 results and then move to the next page:

import ComicVine from 'comic-vine-sdk';
const comicVine = new ComicVine('your-api-key-here');

(async () => {
  try {
    const limit: 50;
    const filter: { name: 'The Boys' },

    // Retrieve the first 50 issues of The Boys (Page 1)
    const issuesPage1 = await comicVine.issue.list({ limit, filter });
    console.log(`Total issues: ${issuesPage1.data.length}`);
    console.log(issuesPage1.data.map(issue => issue.name).join(', '));

    // Retrieve the next 50 issues of The Boys (Page 2)
    const issuesPage2 = await comicVine.issue.list({ limit, filter, offset: 50 });
    console.log(`Total issues: ${issuesPage2.data.length}`);
    console.log(issuesPage2.data.map(issue => issue.name).join(', '));
  } catch (error) {
    console.error(error);
  }
})();

Auto Pagination

This feature allows calling any list method on a resource with for await...of rather than having to track the offset for making subsequent requests.

It will make the first request and return an item from that response on each iteration, when there are no more items to return it will automatically fetch the next page from the API. This will continue until all pages have been retrieved.

import ComicVine from 'comic-vine-sdk';
const comicVine = new ComicVine('your-api-key-here');

(async () => {
  try {
    const listOptions = {
      filter: { name: 'The Boys' },
      limit: 50,
    };

    let issueNames = [];
    for await (const issue of comicVine.issue.list(listOptions)) {
      issueName.push(issue.name);
    }

    console.log(`Total issues: ${issueNames.length}`);
    console.log(issueNames);
  } catch (error) {
    console.error(error);
  }
})();

Run Locally

Clone the project

  git clone https://github.com/AllyMurray/comic-vine.git

Go to the project directory

  cd comic-vine

Install dependencies

  npm install

Run the tests

  npm run test

Authors

comic-vine's People

Contributors

allymurray avatar semantic-release-bot avatar snyk-bot avatar dependabot[bot] avatar

Stargazers

Stanley Smith avatar  avatar Roman avatar

Watchers

 avatar Sarah Page avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.