PokeAPI / pokeapi-js-wrapper

PokeAPI browser wrapper, fully async with built-in cache
Mozilla Public License 2.0
278 stars 44 forks source link

TypeScript support and ES6 #26

Open blai30 opened 3 years ago

blai30 commented 3 years ago

Are there any plans to add typings for use with TypeScript and/or ES6 modules (import/export)?

Naramsim commented 3 years ago

Not right now, To be honest, I don't know how to add the types.

About ES6 modules, the package.json should be modified? Right?

blai30 commented 3 years ago

I think package.json would have "type": "module" but I'm sure there is more to it than just that.

mmethot commented 1 year ago

For what it's worth, I got it working right out of the box with Typescript + Angular.

  1. I created a service file called pokedex.service.ts and added the PokeAPI wrapper in it:
import { Injectable } from '@angular/core';
import * as Pokedex from 'pokeapi-js-wrapper';

@Injectable({
  providedIn: 'root'
})

export class PokedexService {
  pokedex: Pokedex;

  constructor() {
    this.pokedex = new Pokedex.Pokedex();
  }

  async getPokemon() {
    const golduck = await this.pokedex.getPokemonByName("golduck");
    console.log(golduck);
  }
}
  1. I added my service file in my app.component.ts constructor:

    constructor(private pokedexService: PokedexService) {
    ...
    }
  2. I called the getPokemon method in the ngOnInit:

    ngOnInit(): void {
    this.pokedexService.getPokemon();
    }