Marked Promise methods as async; const correctness; return reject Promise, if failed.

This commit is contained in:
bradyn
2020-11-15 20:29:28 -06:00
parent bf2f351fa2
commit ef53f9f99c
10 changed files with 2348 additions and 146 deletions

2389
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -32,7 +32,7 @@
"@types/chai": "^4.1.1",
"@types/mocha": "^2.2.46",
"chai": "^4.1.2",
"mocha": "^5.0.0",
"mocha": "^8.2.1",
"ts-node": "^4.1.0"
}
}

View File

@ -35,24 +35,27 @@ export class Card implements ICard {
return 'cards';
}
static find(id: string): Promise<Card> {
static async find(id: string): Promise<Card> {
return QueryBuilder.find(this, id)
.then(response => {
return response;
});
})
.catch(error => Promise.reject(error));
}
static all(): Promise<Card[]> {
static async all(): Promise<Card[]> {
return QueryBuilder.all(this)
.then(response => {
return response;
});
})
.catch(error => Promise.reject(error));
}
static where(params: IQuery[]): Promise<Card[]> {
static async where(params: IQuery[]): Promise<Card[]> {
return QueryBuilder.where(this, params)
.then(response => {
return response;
});
})
.catch(error => Promise.reject(error));
}
}

View File

@ -1,15 +1,15 @@
import { Client } from '../client';
export class Meta {
static allTypes(): Promise<string[]> {
static async allTypes(): Promise<string[]> {
return Client.get('types');
}
static allSubtypes(): Promise<string[]> {
static async allSubtypes(): Promise<string[]> {
return Client.get('subtypes');
}
static allSupertypes(): Promise<string[]> {
static async allSupertypes(): Promise<string[]> {
return Client.get('supertypes');
}
}

View File

@ -19,24 +19,27 @@ export class Set implements ISet {
return 'sets';
}
static find(id: string): Promise<Set> {
static async find(id: string): Promise<Set> {
return QueryBuilder.find(this, id)
.then(response => {
return response;
});
})
.catch(error => Promise.reject(error));
}
static all(): Promise<Set[]> {
static async all(): Promise<Set[]> {
return QueryBuilder.all(this)
.then(response => {
return response;
});
})
.catch(error => Promise.reject(error));
}
static where(params: IQuery[]): Promise<Set[]> {
static async where(params: IQuery[]): Promise<Set[]> {
return QueryBuilder.where(this, params)
.then(response => {
return response;
});
})
.catch(error => Promise.reject(error));
}
}

View File

@ -5,7 +5,7 @@ import { IQuery } from './interfaces/query';
export class Client {
static apiUrl: string = `${API_URL}/v${API_VERSION}`;
static get(resource: string, params?: IQuery[] | string): Promise<any> {
static async get(resource: string, params?: IQuery[] | string): Promise<any> {
let url: string = `${this.apiUrl}/${resource}`;
let config: axios.AxiosRequestConfig = {
headers: {
@ -20,6 +20,7 @@ export class Client {
.then(response => {
return response.data[Object.keys(response.data)[0]];
})
.catch(error => Promise.reject(error));
}
private static paramsToQuery(params?: IQuery[]): string {

View File

@ -1,12 +1,11 @@
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[] = [{
const params: IQuery[] = [{
name: 'id',
value: 'xy7-54'
}];
@ -15,8 +14,7 @@ describe('Client', () => {
.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', () => {
@ -24,12 +22,11 @@ describe('Client', () => {
.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[] = [{
const params: IQuery[] = [{
name: 'name',
value: 'Base'
}];
@ -38,12 +35,11 @@ describe('Client', () => {
.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[] = [{
const params: IQuery[] = [{
name: 'id',
value: 'base1'
}];
@ -52,8 +48,7 @@ describe('Client', () => {
.then(response => {
expect(response).to.be.a('array');
expect(response[0].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', () => {
@ -62,8 +57,7 @@ describe('Client', () => {
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', () => {
@ -71,8 +65,7 @@ describe('Client', () => {
.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', () => {
@ -80,8 +73,7 @@ describe('Client', () => {
.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', () => {
@ -89,7 +81,6 @@ describe('Client', () => {
.then(response => {
expect(response).to.be.a('array');
expect(response[0]).to.be.a('string');
})
.catch(error => console.error(error));
});
});
});

View File

@ -1,7 +1,6 @@
import * as chai from 'chai';
import { Card } from '../classes/card';
import { QueryBuilder } from '../queryBuilder';
import * as chai from 'chai';
import * as mocha from 'mocha';
import { IQuery } from '../interfaces/query';
const expect = chai.expect;
@ -11,8 +10,7 @@ describe('QueryBuilder', () => {
.then(card => {
expect(card).to.be.a('object');
expect(card.name).to.equal('Gardevoir');
})
.catch(error => console.error(error));
});
});
it('should use where to filter data', () => {
@ -33,15 +31,13 @@ describe('QueryBuilder', () => {
expect(cards.length).to.equal(1);
expect(cards[0].id).to.equal('base1-4');
expect(cards[0].set).to.equal('Base');
})
.catch(error => console.error(error));
});
});
it('should use all to get all cards', () => {
QueryBuilder.all<Card>(Card)
.then(cards => {
expect(cards.length).to.equal(1000);
})
.catch(error => console.error(error));
});
});
});

View File

@ -1,12 +1,12 @@
import { Client } from './client';
import { Card } from "./classes/card";
import { Set } from "./classes/set";
import { Card } from './classes/card';
import { Set } from './classes/set';
import { IQuery } from './interfaces/query';
export class QueryBuilder {
static all<T extends Card | Set>(type: (new () => T)): Promise<T[]> {
let t = new type();
let params: IQuery[] = [{
static all<T extends Card | Set>(type: {new (): T}): Promise<T[]> {
const t = new type();
const params: IQuery[] = [{
name: 'pageSize',
value: 1000
}];
@ -14,14 +14,14 @@ export class QueryBuilder {
return Client.get(t.resource(), params);
}
static find<T extends Card | Set>(type: (new () => T), id: string): Promise<T> {
let t = new type();
static find<T extends Card | Set>(type: {new (): T}, id: string): Promise<T> {
const t = new type();
return Client.get(t.resource(), id);
}
static where<T extends Card | Set>(type: (new () => T), params: IQuery[]): Promise<T[]> {
let t = new type();
static where<T extends Card | Set>(type: {new (): T}, params: IQuery[]): Promise<T[]> {
const t = new type();
return Client.get(t.resource(), params);
}

View File

@ -5,7 +5,8 @@
"declaration": true,
"outDir": "./dist",
"sourceMap": true,
"experimentalDecorators": true
"experimentalDecorators": true,
"lib": ["ES2015"]
},
"include": [
"src/**/*.ts"