Henrik-3 / unofficial-valorant-api

Unofficial VALORANT API using the VALORANT Ingame API
424 stars 18 forks source link

fix: Change declare enum to type #49

Closed sya-ri closed 1 year ago

sya-ri commented 1 year ago

If #48 is not merged, it will not compile. I thought about putting it in one PR but splitting it into two PRs in case I could discuss this change with you.


Using "declare enum" gives an undefined error. It can also define an enum on the js side, but using "type" is better. d.ts can only define types/interfaces (in other words, inputs and outputs), not new enums. Have you tried this API by creating a Typescript project? Also, this change is just a fix for broken code, not a breaking change.

Before

Source code (src/index.ts)

import HenrikDevValorantAPI, { Regions } from 'unofficial-valorant-api';

const run = async () => {
    const api = new HenrikDevValorantAPI();
    console.info(await api.getMatches({ region: Regions.Asia, name: '...', tag: '...' }));
}

run().catch(err => console.error(err))

Error

> ts-node src/index.ts

TypeError: Cannot read properties of undefined (reading 'Asia')
    at /Users/epq/project/Private/valorant-stats/src/index.ts:5:57
    at Generator.next (<anonymous>)
    at /Users/epq/project/Private/valorant-stats/src/index.ts:31:71
    at new Promise (<anonymous>)
    at __awaiter (/Users/epq/project/Private/valorant-stats/src/index.ts:27:12)
    at run (/Users/epq/project/Private/valorant-stats/src/index.ts:3:24)
    at Object.<anonymous> (/Users/epq/project/Private/valorant-stats/src/index.ts:8:1)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Module.m._compile (/Users/epq/project/Private/valorant-stats/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1155:10)

After

Source code (src/index.ts)

import HenrikDevValorantAPI from 'unofficial-valorant-api';

const run = async () => {
    const api = new HenrikDevValorantAPI();
    console.info(await api.getMatches({ region: 'ap', name: '...', tag: '...' }));
}

run().catch(err => console.error(err))
Henrik-3 commented 1 year ago

Have you tried this API by creating a Typescript project? ==> Actually no, i code in raw JS since two years as my first language ever and because of school that i vistited until a month ago i never really had time to take a look at TS, so i thought i am gonna do a small implementation based on what i know from other packages and if something is not working someone will create a PR for that, that's what happened now.

Thanks for the enum/type explanation, that is good to know ^^