UNPKG

ngx-rickandmorty

Version:

203 lines (187 loc) 6.36 kB
import { __rest } from 'tslib'; import * as i0 from '@angular/core'; import { InjectionToken, inject, NgModule, Injectable } from '@angular/core'; import { CommonModule, Location as Location$1 } from '@angular/common'; import { HttpClient, HttpParams } from '@angular/common/http'; import { of } from 'rxjs'; import { map, catchError } from 'rxjs/operators'; function extractIdsFromUrl(url) { if (!url) { return []; } const { pathname } = new URL(url); const maybeIds = pathname.split('/').pop().split(','); if (maybeIds.every((maybeId) => /\d+/.test(maybeId))) { return maybeIds.map((maybeId) => parseInt(maybeId, 10)); } return []; } class Character { static fromJson(_a) { var { origin, location, episode, created } = _a, common = __rest(_a, ["origin", "location", "episode", "created"]); const character = new Character(); Object.assign(character, common); character.origin = { name: origin.name, id: extractIdsFromUrl(origin.url)[0], }; character.location = { name: location.name, id: extractIdsFromUrl(location.url)[0], }; character.episode = episode.map(extractIdsFromUrl).map(([id]) => id); character.created = new Date(created); return character; } } class Episode { static fromJson(_a) { var { characters, created } = _a, common = __rest(_a, ["characters", "created"]); const episode = new Episode(); Object.assign(episode, common); episode.characters = characters.map(extractIdsFromUrl).map(([id]) => id); episode.created = new Date(created); return episode; } } class Location { static fromJson(_a) { var { created, residents } = _a, common = __rest(_a, ["created", "residents"]); const location = new Location(); Object.assign(location, common); location.residents = residents.map(extractIdsFromUrl).map(([id]) => id); location.created = new Date(created); return location; } } function extractSearchParamsFromUrl(url) { if (!url) { return null; } const { searchParams } = new URL(url); const params = {}; searchParams.forEach((value, key) => { params[key] = value; }); return params; } class Pagination { static fromJson(_a) { var { next, prev } = _a, common = __rest(_a, ["next", "prev"]); const pagination = new Pagination(); Object.assign(pagination, common); pagination.next = extractSearchParamsFromUrl(next); pagination.prev = extractSearchParamsFromUrl(prev); return pagination; } } class Response { static fromJson({ info, results }, modelFactory) { const response = new Response(); response.info = Pagination.fromJson(info); response.results = results.map(modelFactory); return response; } } const API_OPTIONS = new InjectionToken('RaM API Options', { providedIn: 'root', factory() { return { baseUrl: 'https://rickandmortyapi.com/api/', }; }, }); const BASE_URL = new InjectionToken('Base RaM API url', { providedIn: 'root', factory() { return inject(API_OPTIONS).baseUrl; }, }); class ApiModule { static configure(options) { return { ngModule: ApiModule, providers: [ { provide: API_OPTIONS, useValue: options, }, ], }; } } ApiModule.decorators = [ { type: NgModule, args: [{ imports: [CommonModule], },] } ]; function isRawResponse(value) { return !!(value === null || value === void 0 ? void 0 : value.info) && !!(value === null || value === void 0 ? void 0 : value.results); } function coerceArray(value) { return Array.isArray(value) ? value : [value]; } function responseFactory(rawResponse, modelFactory) { if (!rawResponse) { return null; } if (isRawResponse(rawResponse)) { return Response.fromJson(rawResponse, modelFactory); } if (Array.isArray(rawResponse)) { return rawResponse.map(modelFactory); } return coerceArray(modelFactory(rawResponse)); } class ApiService { constructor() { this.baseUrl = inject(BASE_URL); this.http = inject(HttpClient); } ping() { return this.http.get(this.baseUrl); } getEpisode(id) { return this.getEntity('episode', id).pipe(map((rawResponse) => responseFactory(rawResponse, Episode.fromJson))); } getLocation(id) { return this.getEntity('location', id).pipe(map((rawResponse) => responseFactory(rawResponse, Location.fromJson))); } getCharacter(id) { return this.getEntity('character', id).pipe(map((rawResponse) => responseFactory(rawResponse, Character.fromJson))); } getEntity(url, id) { var _a; const [ids, params] = [ this.isId(id) ? coerceArray(id).filter((i) => (i !== null && i !== void 0 ? i : false) !== false) : null, this.isId(id) ? null : id, ]; return this.http .get(Location$1.joinWithSlash(Location$1.joinWithSlash(this.baseUrl, url), (_a = ids === null || ids === void 0 ? void 0 : ids.join(',')) !== null && _a !== void 0 ? _a : ''), { params: new HttpParams({ fromObject: params }) }) .pipe(catchError(() => { if (typeof id === 'number' || Array.isArray(id)) { return of([]); } return of({ info: { pages: 0, count: 0, prev: null, next: null }, results: [], }); })); } isId(id) { return typeof id === 'number' || Array.isArray(id); } } ApiService.ɵprov = i0.ɵɵdefineInjectable({ factory: function ApiService_Factory() { return new ApiService(); }, token: ApiService, providedIn: "root" }); ApiService.decorators = [ { type: Injectable, args: [{ providedIn: 'root', },] } ]; /** * Generated bundle index. Do not edit. */ export { API_OPTIONS, ApiModule, ApiService, BASE_URL, Character, Episode, Location, Pagination, Response }; //# sourceMappingURL=ngx-rickandmorty.js.map