diff --git a/package.json b/package.json index 3693d3b..5544597 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pokemon-tcg-sdk-typescript", - "version": "0.1.4", + "version": "1.0.0", "description": "Typescript SDK for the PokemonTCG API (https://pokemontcg.io)", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/client.ts b/src/client.ts index e2c6eb5..a116a07 100644 --- a/src/client.ts +++ b/src/client.ts @@ -11,7 +11,7 @@ export class Client { static apiUrl: string = `${PokemonTCG.API_URL}/v${PokemonTCG.version}`; static get(resource: string, params?: IQuery[]): Promise { - let url: string = `${Client.apiUrl}/${resource}`; + let url: string = `${this.apiUrl}/${resource}`; let config: axios.AxiosRequestConfig = { headers: { 'Content-Type': 'application/json' @@ -20,20 +20,20 @@ export class Client { // This is needed because the /sets endpoint doesn't take // an id as a parameter so we need to append it to the url - url += this.checkForId(params); + url += this.checkForId(resource, params); - return axios.default.get(`${this.apiUrl}/${resource}?${this.paramsToQuery(params)}`, config) + return axios.default.get(`${url}?${this.paramsToQuery(params)}`, config) .then(response => { return response.data[Object.keys(response.data)[0]]; }) } - private static checkForId(params?: IQuery[]): string { + private static checkForId(resource: string, params?: IQuery[]): string { let url: string = ''; if (params) { params.map(param => { - if (param.name === 'id') { + if (resource === 'sets' && param.name === 'id') { url = `/${param.value}`; } }); diff --git a/src/coverage/client.test.ts b/src/coverage/client.test.ts new file mode 100644 index 0000000..7e2c98c --- /dev/null +++ b/src/coverage/client.test.ts @@ -0,0 +1,95 @@ +import * as chai from 'chai'; +import * as mocha from 'mocha'; +import { Client } from '../client'; +import { IQuery } from '../interfaces/query'; + +const expect = chai.expect; +describe('Client', () => { + it('should get a single using the cards resource and query params' , () => { + let params: IQuery[] = [{ + name: 'id', + value: 'xy7-54' + }]; + + Client.get('cards', params) + .then(response => { + expect(response).to.be.a('array'); + expect(response[0].name).to.equal('Gardevoir'); + }) + .catch(error => console.error(error)); + }); + + it('should get a default list of cards using the cards resource with no query params', () => { + Client.get('cards') + .then(response => { + expect(response).to.be.a('array'); + expect(response.length).to.equal(100); + }) + .catch(error => console.error(error)); + }); + + it('should get sets using the sets resource and query params', () => { + let params: IQuery[] = [{ + name: 'name', + value: 'Base' + }]; + + Client.get('sets', params) + .then(response => { + expect(response).to.be.a('array'); + expect(response[0]).to.be.a('object'); + }) + .catch(error => console.error(error)); + }); + + it('should get a single set using the sets resource and query params', () => { + let params: IQuery[] = [{ + name: 'id', + value: 'base1' + }]; + + Client.get('sets', params) + .then(response => { + expect(response).to.be.a('object'); + expect(response.name).to.equal('Base'); + }) + .catch(error => console.error(error)); + }); + + it('should get a default list of sets using the sets resource with no query params', () => { + Client.get('sets') + .then(response => { + expect(response).to.be.a('array'); + expect(response[0]).to.be.a('object'); + expect(response[0].code).to.equal('base1'); + }) + .catch(error => console.error(error)); + }); + + it('should get a list of types using the types resource', () => { + Client.get('types') + .then(response => { + expect(response).to.be.a('array'); + expect(response[0]).to.be.a('string'); + }) + .catch(error => console.error(error)); + }); + + it('should get a list of supertypes using the supertypes resource', () => { + Client.get('supertypes') + .then(response => { + expect(response).to.be.a('array'); + expect(response[0]).to.be.a('string'); + }) + .catch(error => console.error(error)); + }); + + it('should get a list of subtypes using the subtypes resource', () => { + Client.get('subtypes') + .then(response => { + expect(response).to.be.a('array'); + expect(response[0]).to.be.a('string'); + }) + .catch(error => console.error(error)); + }); +}); \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 5fa85e5..10e32f7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,52 @@ -export { Card } from './classes/card'; -export { Set } from './classes/set'; -export { Type } from './classes/type'; -export { SuperType } from './classes/superType'; -export { SubType } from './classes/subType'; -export { IQuery } from './interfaces/query'; \ No newline at end of file +import { Card as BaseCard } from './classes/card'; +import { Ability as BaseAbility } from './classes/ability'; +import { AncientAbility as BaseAncientAbility } from './classes/ancientAbility'; +import { Attack as BaseAttack } from './classes/attack'; +import { Set as BaseSet } from './classes/set'; +import { SubType as BaseSubType } from './classes/subType'; +import { SuperType as BaseSuperType } from './classes/superType'; +import { Type as BaseType } from './classes/type'; + +import { IAttack as BaseIAttack } from './interfaces/attack'; +import { IAncientAbility as BaseIAncientAbility } from './interfaces/ancientAbility'; +import { IAbility as BaseIAbility } from './interfaces/ability'; +import { IQuery as BaseIQuery } from './interfaces/query'; +import { ISet as BaseISet } from './interfaces/set'; +import { ISuperType as BaseISuperType } from './interfaces/superType'; +import { ISubType as BaseISubType } from './interfaces/subType'; +import { IType as BaseIType } from './interfaces/type'; + +export namespace PokemonTCG { + export type IAttack = BaseIAttack; + export type IAncientAbility = BaseIAncientAbility; + export type IAbility = BaseIAbility; + export type IQuery = BaseIQuery; + export type ISet = BaseISet; + export type ISuperType = BaseISuperType; + export type ISubType = BaseISubType; + export type IType = BaseIType; + + export const Card = BaseCard; + export type Card = BaseCard; + + export const Ability = BaseAbility; + export type Ability = BaseAbility; + + export const AncientAbility = BaseAncientAbility; + export type AncientAbility = BaseAncientAbility; + + export const Attack = BaseAttack; + export type Attack = BaseAttack; + + export const Set = BaseSet; + export type Set = BaseSet; + + export const SubType = BaseSubType; + export type SubType = BaseSubType; + + export const SuperType = BaseSuperType; + export type SuperType = BaseSuperType; + + export const Type = BaseType; + export type Type = BaseType; +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index e1b03ff..8815407 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,9 +3,11 @@ "module": "commonjs", "target": "es5", "declaration": true, - "outDir": "./dist" + "outDir": "./dist", + "sourceMap": true, + "experimentalDecorators": true }, "include": [ - "src/**/*" + "src/**/*.ts" ] } \ No newline at end of file