Skip to content

Commit

Permalink
chore: cleanup typescript usage
Browse files Browse the repository at this point in the history
  • Loading branch information
favna committed May 16, 2020
1 parent 56fdb97 commit a921de3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "http://json.schemastore.org/prettierrc",
"endOfLine": "lf",
"printWidth": 120,
"printWidth": 150,
"quoteProps": "as-needed",
"semi": true,
"singleQuote": true,
Expand Down
28 changes: 5 additions & 23 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DexDetails, GenderEntry, MoveEntry, Query } from '@favware/graphql-pokemon';
import { DexDetails, GenderEntry, MoveEntry } from '@favware/graphql-pokemon';
import { toTitleCase } from '@klasa/utils';
import gql from 'graphql-tag';

Expand Down Expand Up @@ -120,11 +120,7 @@ export const parsePrevos = (data: DexDetails) => {
prevos.push(
[
`${toTitleCase(pr.species)}`,
`${
hasEvoByLevel(data.evolutionLevel)
? `(Level: ${data.evolutionLevel})`
: `(Special Condition: ${data.evolutionLevel})`
}`
`${hasEvoByLevel(data.evolutionLevel) ? `(Level: ${data.evolutionLevel})` : `(Special Condition: ${data.evolutionLevel})`}`
].join(' ')
);

Expand All @@ -133,11 +129,7 @@ export const parsePrevos = (data: DexDetails) => {
prevos.push(
[
`${toTitleCase(prr.species)}`,
`${
hasEvoByLevel(pr.evolutionLevel)
? `(Level: ${pr.evolutionLevel})`
: `(Special Condition: ${pr.evolutionLevel})`
}`
`${hasEvoByLevel(pr.evolutionLevel) ? `(Level: ${pr.evolutionLevel})` : `(Special Condition: ${pr.evolutionLevel})`}`
].join(' ')
);
});
Expand All @@ -157,11 +149,7 @@ export const parseEvos = (data: DexDetails) => {
evos.push(
[
`${toTitleCase(evo.species)}`,
`${
hasEvoByLevel(evo.evolutionLevel)
? `(Level: ${evo.evolutionLevel})`
: `(Special Condition: ${evo.evolutionLevel})`
}`
`${hasEvoByLevel(evo.evolutionLevel) ? `(Level: ${evo.evolutionLevel})` : `(Special Condition: ${evo.evolutionLevel})`}`
].join(' ')
);

Expand All @@ -170,11 +158,7 @@ export const parseEvos = (data: DexDetails) => {
evos.push(
[
`${toTitleCase(evvo.species)}`,
`${
hasEvoByLevel(evvo.evolutionLevel)
? `(Level: ${evvo.evolutionLevel})`
: `(Special Condition: ${evvo.evolutionLevel})`
}`
`${hasEvoByLevel(evvo.evolutionLevel) ? `(Level: ${evvo.evolutionLevel})` : `(Special Condition: ${evvo.evolutionLevel})`}`
].join(' ')
);
});
Expand Down Expand Up @@ -233,5 +217,3 @@ export const parseMoveTarget = (target: MoveEntry['target']) => {
return 'any Pokémon';
}
};

export type GraphQLPokemonResponse<K extends keyof Omit<Query, '__typename'>> = Record<K, Omit<Query[K], '__typename'>>;
67 changes: 27 additions & 40 deletions src/dexa.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import {
Query,
QueryGetAbilityDetailsByFuzzyArgs,
QueryGetItemDetailsByFuzzyArgs,
QueryGetMoveDetailsByFuzzyArgs,
QueryGetPokemonDetailsByFuzzyArgs
} from '@favware/graphql-pokemon';
import { toTitleCase } from '@klasa/utils';
import { app as AlexaApp, request as Request, response as Response } from 'alexa-app';
import ApolloClient, { ApolloQueryResult, InMemoryCache } from 'apollo-boost';
import ApolloClient, { InMemoryCache } from 'apollo-boost';
import removeDiacritics from 'confusables';
import 'cross-fetch/polyfill';
import * as c from './constants';

type GraphQLPokemonResponse<K extends keyof Omit<Query, '__typename'>> = Record<K, Omit<Query[K], '__typename'>>;

const enum SLOTS {
POKEMON = 'POKEMON',
ITEM = 'ITEM',
Expand Down Expand Up @@ -45,11 +54,7 @@ export default class extends AlexaApp {
INTENT_NAMES.DEX_INTENT,
{
slots: { [SLOTS.POKEMON]: SLOTS.POKEMON },
utterances: [
`data on {-|${SLOTS.POKEMON}}`,
`pokemon data for {-|${SLOTS.POKEMON}}`,
`pokemon data {-|${SLOTS.POKEMON}}`
]
utterances: [`data on {-|${SLOTS.POKEMON}}`, `pokemon data for {-|${SLOTS.POKEMON}}`, `pokemon data {-|${SLOTS.POKEMON}}`]
},
(req, res) => {
const pokemon = removeDiacritics(req.slot(SLOTS.POKEMON));
Expand Down Expand Up @@ -100,12 +105,10 @@ export default class extends AlexaApp {

public async DexIntent(res: Response, req: Request, pokemon: string) {
try {
const { data }: ApolloQueryResult<c.GraphQLPokemonResponse<'getPokemonDetailsByFuzzy'>> = await this.apollo.query(
{
query: c.getPokemonDetailsByFuzzy,
variables: { pokemon }
}
);
const { data } = await this.apollo.query<GraphQLPokemonResponse<'getPokemonDetailsByFuzzy'>, QueryGetPokemonDetailsByFuzzyArgs>({
query: c.getPokemonDetailsByFuzzy,
variables: { pokemon }
});
const { getPokemonDetailsByFuzzy: pokeData } = data;
const titleCaseName = toTitleCase(pokeData.species);

Expand Down Expand Up @@ -138,7 +141,7 @@ export default class extends AlexaApp {

public async MoveIntent(res: Response, req: Request, move: string) {
try {
const { data }: ApolloQueryResult<c.GraphQLPokemonResponse<'getMoveDetailsByFuzzy'>> = await this.apollo.query({
const { data } = await this.apollo.query<GraphQLPokemonResponse<'getMoveDetailsByFuzzy'>, QueryGetMoveDetailsByFuzzyArgs>({
query: c.getMoveDetailsByFuzzy,
variables: { move }
});
Expand All @@ -150,9 +153,7 @@ export default class extends AlexaApp {
`${titleCaseName} is a${this.dElectricOrIce.test(moveData.type) ? 'n' : ''} ${moveData.type} type move.`,
`${titleCaseName} ${c.parseMoveBasePower(moveData.basePower, moveData.category)} and it has ${moveData.pp} pp.`,
`Under normal conditions this move will have a priority of ${moveData.priority} and an accuracy of ${moveData.accuracy}%.`,
`In battles with multiple Pokémon on each side it will have an effect on ${c.parseMoveTarget(
moveData.target
)}.`,
`In battles with multiple Pokémon on each side it will have an effect on ${c.parseMoveTarget(moveData.target)}.`,
moveData.isZ ? `This move is a Z Move and requires the Z-Crystal ${moveData.isZ}.` : null,
moveData.isGMax ? `This move is a G MAX move and can only be used by G Max ${moveData.isGMax}.` : null,
moveData.isNonstandard !== 'Past' ? `${titleCaseName} is available in the generation 8 games.` : null
Expand All @@ -175,7 +176,7 @@ export default class extends AlexaApp {

private async ItemIntent(res: Response, req: Request, item: string) {
try {
const { data }: ApolloQueryResult<c.GraphQLPokemonResponse<'getItemDetailsByFuzzy'>> = await this.apollo.query({
const { data } = await this.apollo.query<GraphQLPokemonResponse<'getItemDetailsByFuzzy'>, QueryGetItemDetailsByFuzzyArgs>({
query: c.getItemDetailsByFuzzy,
variables: { item }
});
Expand Down Expand Up @@ -205,12 +206,10 @@ export default class extends AlexaApp {

private async AbilityIntent(res: Response, req: Request, ability: string) {
try {
const { data }: ApolloQueryResult<c.GraphQLPokemonResponse<'getAbilityDetailsByFuzzy'>> = await this.apollo.query(
{
query: c.getAbilityDetailsByFuzzy,
variables: { ability }
}
);
const { data } = await this.apollo.query<GraphQLPokemonResponse<'getAbilityDetailsByFuzzy'>, QueryGetAbilityDetailsByFuzzyArgs>({
query: c.getAbilityDetailsByFuzzy,
variables: { ability }
});
const { getAbilityDetailsByFuzzy: abilityData } = data;
const titleCaseName = toTitleCase(abilityData.name);

Expand All @@ -234,23 +233,13 @@ export default class extends AlexaApp {
try {
switch (req.data.request.intent.name) {
case INTENT_NAMES.DEX_INTENT:
return res.say(
`I couldn't find a Pokémon for ${req.slot(SLOTS.POKEMON)}. Are you sure you spelled that correctly?`
);
return res.say(`I couldn't find a Pokémon for ${req.slot(SLOTS.POKEMON)}. Are you sure you spelled that correctly?`);
case INTENT_NAMES.ABILITY_INTENT:
return res.say(
`I couldn't find an Ability for ${req.slot(SLOTS.ABILITY)}. Are you sure you spelled that correctly?`
);
return res.say(`I couldn't find an Ability for ${req.slot(SLOTS.ABILITY)}. Are you sure you spelled that correctly?`);
case INTENT_NAMES.MOVE_INTENT:
return res.say(
`I couldn't find an Move for ${req.slot(
SLOTS.MOVE
)}. I only support moves that have are used inside battles`
);
return res.say(`I couldn't find an Move for ${req.slot(SLOTS.MOVE)}. I only support moves that have are used inside battles`);
case INTENT_NAMES.ITEM_INTENT:
return res.say(
`I couldn't find an Item for ${req.slot(SLOTS.ITEM)}. Is that really an item that can be used in battle?`
);
return res.say(`I couldn't find an Item for ${req.slot(SLOTS.ITEM)}. Is that really an item that can be used in battle?`);
default:
return this.throwSystemErr(res);
}
Expand Down Expand Up @@ -334,9 +323,7 @@ export default class extends AlexaApp {

private throwSystemErr(res: Response) {
return res
.say(
'Something went awfully wrong browsing my dataset. Please use "Alexa ask Dexa Browser for help" if you are unsure how to use Dexa'
)
.say('Something went awfully wrong browsing my dataset. Please use "Alexa ask Dexa Browser for help" if you are unsure how to use Dexa')
.shouldEndSession(false);
}

Expand Down

0 comments on commit a921de3

Please sign in to comment.