marvel-ts
v1.0.0
Published
TypeScript Wrapper for Marvel API
Downloads
4
Readme
marvel-ts
An unofficial TypeScript wrapper for Marvel Comics API. Suitable for client and server-side usage.
Install
npm i marvel-ts
Usage
Prerequisites
- Go to https://developer.marvel.com/ and generate your API keys.
- Add your domains as authorized referrers.
- For development, you can use
*.*
.
Import
import { MarvelAPI } from 'marvel-ts';
Instantiate
For client-side / browser you only need to provide your public key.
const PUBLIC_KEY = 'MY_PUBLIC_KEY';
const marvelAPI = new MarvelAPI(PUBLIC_KEY);
For server-side you will need to provide your public and private key.
const PUBLIC_KEY = 'MY_PUBLIC_KEY';
const PRIVATE_KEY = 'MY_PRIVATE_KEY';
const marvelAPI = new MarvelAPI(PUBLIC_KEY, PRIVATE_KEY);
You can also pass in rateLimit options. By default, the wrapper does not apply a rate limit.
const PUBLIC_KEY = 'MY_PUBLIC_KEY';
const PRIVATE_KEY = 'MY_PRIVATE_KEY';
const marvelAPI = new MarvelAPI(PUBLIC_KEY, PRIVATE_KEY, { maxRequests: 4, perMilliseconds: 1000, maxRPS: 4 });
Call Endpoint
All endpoints described in the API documentation are available. For example:
const response = await marvelAPI.getCharacters();
Getting Data
The response from endpoints are typed to mirror those from the documentation. To get the results of the call, use destructuring. For example:
const response = await marvelAPI.getCharacters();
const { data: { results } } = response;
const character = results[0];
Using parameters
All endpoints which do not call for a identifier e.g characterId
, can optionally be passed parameters. See documentation for more details.
const response = await marvelAPI.getCharacters({ nameStartsWith: "spider", limit: 5 });