mcu-sdk
Version:
MCU-SDK provides a set of methods to access the data provided by the Marvel Comics API.
92 lines (91 loc) • 5.38 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MarvelAPI = void 0;
const types_1 = require("./types");
const utilities_1 = require("./utilities");
class MarvelAPI {
baseUrl = "https://gateway.marvel.com/v1/public/";
endpoints = types_1.Endpoint;
privateKey = "";
publicKey = "";
static instance;
constructor(privateKey, publicKey) {
this.privateKey = privateKey;
this.publicKey = publicKey;
}
static init(privateKey, publicKey) {
if (!MarvelAPI.instance)
MarvelAPI.instance = new MarvelAPI(privateKey, publicKey);
return MarvelAPI.instance;
}
buildUrl(endpoint, parameters) {
const params = (parameters && new URLSearchParams(parameters).toString()) || "";
const ts = new Date().getTime();
const rawHash = ts + this.privateKey + this.publicKey;
const hash = (0, utilities_1.hashGenerator)(rawHash);
return `${this.baseUrl}${endpoint}?ts=${ts}&apikey=${this.publicKey}&hash=${hash}&${params}`;
}
async fetchData(endpoint, parameters) {
const url = this.buildUrl(endpoint, parameters);
return await fetch(url, {
method: "GET",
})
.then((response) => response.json())
.then((data) => {
return data;
})
.catch((error) => {
throw new Error(error);
});
}
createEndpointMethod(endpoint, subEndpoint) {
return (id, parameters) => {
return this.fetchData(`${endpoint}/${id}${subEndpoint ? `/${subEndpoint}` : ""}`, parameters);
};
}
createGlobalEndpointMethod(endpoint) {
return (parameters) => {
return this.fetchData(endpoint, parameters);
};
}
getAllCharacters = this.createGlobalEndpointMethod(this.endpoints.CHARACTERS);
getCharacterById = this.createEndpointMethod(this.endpoints.CHARACTERS);
getCharacterComics = this.createEndpointMethod(this.endpoints.CHARACTERS, this.endpoints.COMICS);
getCharacterEvents = this.createEndpointMethod(this.endpoints.CHARACTERS, this.endpoints.EVENTS);
getCharacterSeries = this.createEndpointMethod(this.endpoints.CHARACTERS, this.endpoints.SERIES);
getCharacterStories = this.createEndpointMethod(this.endpoints.CHARACTERS, this.endpoints.STORIES);
getAllComics = this.createGlobalEndpointMethod(this.endpoints.COMICS);
getComicById = this.createEndpointMethod(this.endpoints.COMICS);
getComicCharacters = this.createEndpointMethod(this.endpoints.COMICS, this.endpoints.CHARACTERS);
getComicCreators = this.createEndpointMethod(this.endpoints.COMICS, this.endpoints.CREATORS);
getComicEvents = this.createEndpointMethod(this.endpoints.COMICS, this.endpoints.EVENTS);
getComicStories = this.createEndpointMethod(this.endpoints.COMICS, this.endpoints.STORIES);
getAllCreators = this.createGlobalEndpointMethod(this.endpoints.CREATORS);
getCreatorById = this.createEndpointMethod(this.endpoints.CREATORS);
getCreatorComics = this.createEndpointMethod(this.endpoints.CREATORS, this.endpoints.COMICS);
getCreatorEvents = this.createEndpointMethod(this.endpoints.CREATORS, this.endpoints.EVENTS);
getCreatorSeries = this.createEndpointMethod(this.endpoints.CREATORS, this.endpoints.SERIES);
getCreatorStories = this.createEndpointMethod(this.endpoints.CREATORS, this.endpoints.STORIES);
getAllEvents = this.createGlobalEndpointMethod(this.endpoints.EVENTS);
getEventById = this.createEndpointMethod(this.endpoints.EVENTS);
getEventCharacters = this.createEndpointMethod(this.endpoints.EVENTS, this.endpoints.CHARACTERS);
getEventComics = this.createEndpointMethod(this.endpoints.EVENTS, this.endpoints.COMICS);
getEventCreators = this.createEndpointMethod(this.endpoints.EVENTS, this.endpoints.CREATORS);
getEventSeries = this.createEndpointMethod(this.endpoints.EVENTS, this.endpoints.SERIES);
getEventStories = this.createEndpointMethod(this.endpoints.EVENTS, this.endpoints.STORIES);
getAllSeries = this.createGlobalEndpointMethod(this.endpoints.SERIES);
getSeriesById = this.createEndpointMethod(this.endpoints.SERIES);
getSeriesCharacters = this.createEndpointMethod(this.endpoints.SERIES, this.endpoints.CHARACTERS);
getSeriesComics = this.createEndpointMethod(this.endpoints.SERIES, this.endpoints.COMICS);
getSeriesCreators = this.createEndpointMethod(this.endpoints.SERIES, this.endpoints.CREATORS);
getSeriesEvents = this.createEndpointMethod(this.endpoints.SERIES, this.endpoints.EVENTS);
getSeriesStories = this.createEndpointMethod(this.endpoints.SERIES, this.endpoints.STORIES);
getAllStories = this.createGlobalEndpointMethod(this.endpoints.STORIES);
getStoryById = this.createEndpointMethod(this.endpoints.STORIES);
getStoryCharacters = this.createEndpointMethod(this.endpoints.STORIES, this.endpoints.CHARACTERS);
getStoryComics = this.createEndpointMethod(this.endpoints.STORIES, this.endpoints.COMICS);
getStoryCreators = this.createEndpointMethod(this.endpoints.STORIES, this.endpoints.CREATORS);
getStoryEvents = this.createEndpointMethod(this.endpoints.STORIES, this.endpoints.EVENTS);
getStorySeries = this.createEndpointMethod(this.endpoints.STORIES, this.endpoints.SERIES);
}
exports.MarvelAPI = MarvelAPI;