Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: TS Redux without non-scalar resolvers #341

Merged
merged 9 commits into from
Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27,848 changes: 15,638 additions & 12,210 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"fs-extra": "^10.0.1",
"jest": "^27.5.1",
"mockingoose": "^2.15.2",
"node-mocks-http": "^1.12.1",
"nodemon": "^2.0.19",
"npm-run-all": "^4.1.5",
"prettier": "^1.19.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Request, Response, NextFunction } from 'express';

import { ResourceList, escapeRegExp } from '../../util/index.js';

import Class from '../../models/class/index.js';
Expand All @@ -9,20 +11,26 @@ import Spell from '../../models/spell/index.js';
import Subclass from '../../models/subclass/index.js';

const simpleController = new SimpleController(Class);
interface ShowLevelsForClassQuery {
'class.url': string;
$or: Record<string, null | Record<string, RegExp>>[];
}

export const index = async (req, res, next) => simpleController.index(req, res, next);
export const show = async (req, res, next) => simpleController.show(req, res, next);
export const index = async (req: Request, res: Response, next: NextFunction) =>
simpleController.index(req, res, next);
export const show = async (req: Request, res: Response, next: NextFunction) =>
simpleController.show(req, res, next);

export const showLevelsForClass = async (req, res, next) => {
export const showLevelsForClass = async (req: Request, res: Response, next: NextFunction) => {
try {
const searchQueries = {
const searchQueries: ShowLevelsForClassQuery = {
'class.url': '/api/classes/' + req.params.index,
$or: [{ subclass: null }],
};

if (req.query.subclass !== undefined) {
searchQueries.$or.push({
'subclass.url': { $regex: new RegExp(escapeRegExp(req.query.subclass), 'i') },
'subclass.url': { $regex: new RegExp(escapeRegExp(req.query.subclass as string), 'i') },
});
}

Expand All @@ -37,7 +45,7 @@ export const showLevelsForClass = async (req, res, next) => {
}
};

export const showLevelForClass = async (req, res, next) => {
export const showLevelForClass = async (req: Request, res: Response, next: NextFunction) => {
try {
if (!Number.isInteger(parseInt(req.params.level))) {
return res.status(404).json({ error: 'Not found' });
Expand All @@ -53,18 +61,22 @@ export const showLevelForClass = async (req, res, next) => {
}
};

export const showMulticlassingForClass = async (req, res, next) => {
export const showMulticlassingForClass = async (
req: Request,
res: Response,
next: NextFunction
) => {
try {
const urlString = '/api/classes/' + req.params.index;

const data = await Class.findOne({ url: urlString });
return res.status(200).json(data.multi_classing);
return res.status(200).json(data?.multi_classing);
} catch (err) {
next(err);
}
};

export const showSubclassesForClass = async (req, res, next) => {
export const showSubclassesForClass = async (req: Request, res: Response, next: NextFunction) => {
try {
const urlString = '/api/classes/' + req.params.index;

Expand All @@ -81,19 +93,23 @@ export const showSubclassesForClass = async (req, res, next) => {
}
};

export const showStartingEquipmentForClass = async (req, res, next) => {
export const showStartingEquipmentForClass = async (
req: Request,
res: Response,
next: NextFunction
) => {
try {
const data = await Class.findOne({ index: req.params.index });
return res.status(200).json({
starting_equipment: data.starting_equipment,
starting_equipment_options: data.starting_equipment_options,
starting_equipment: data?.starting_equipment,
starting_equipment_options: data?.starting_equipment_options,
});
} catch (err) {
next(err);
}
};

export const showSpellcastingForClass = async (req, res, next) => {
export const showSpellcastingForClass = async (req: Request, res: Response, next: NextFunction) => {
try {
const data = await Class.findOne({ index: req.params.index });
if (data && data.spellcasting) {
Expand All @@ -106,7 +122,7 @@ export const showSpellcastingForClass = async (req, res, next) => {
}
};

export const showSpellsForClass = async (req, res, next) => {
export const showSpellsForClass = async (req: Request, res: Response, next: NextFunction) => {
try {
const urlString = '/api/classes/' + req.params.index;

Expand All @@ -119,7 +135,11 @@ export const showSpellsForClass = async (req, res, next) => {
}
};

export const showSpellsForClassAndLevel = async (req, res, next) => {
export const showSpellsForClassAndLevel = async (
req: Request,
res: Response,
next: NextFunction
) => {
try {
if (!Number.isInteger(parseInt(req.params.level))) {
return res.status(404).json({ error: 'Not found' });
Expand All @@ -139,7 +159,7 @@ export const showSpellsForClassAndLevel = async (req, res, next) => {
}
};

export const showFeaturesForClass = async (req, res, next) => {
export const showFeaturesForClass = async (req: Request, res: Response, next: NextFunction) => {
try {
const urlString = '/api/classes/' + req.params.index;

Expand All @@ -154,7 +174,11 @@ export const showFeaturesForClass = async (req, res, next) => {
}
};

export const showFeaturesForClassAndLevel = async (req, res, next) => {
export const showFeaturesForClassAndLevel = async (
req: Request,
res: Response,
next: NextFunction
) => {
try {
if (!Number.isInteger(parseInt(req.params.level))) {
return res.status(404).json({ error: 'Not found' });
Expand All @@ -174,7 +198,11 @@ export const showFeaturesForClassAndLevel = async (req, res, next) => {
}
};

export const showProficienciesForClass = async (req, res, next) => {
export const showProficienciesForClass = async (
req: Request,
res: Response,
next: NextFunction
) => {
try {
const urlString = '/api/classes/' + req.params.index;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Request, Response, NextFunction } from 'express';
import aws from 'aws-sdk';

const s3 = new aws.S3({ params: { Bucket: 'dnd-5e-api-images' } });

const show = async (req, res, next) => {
const show = async (req: Request, res: Response, next: NextFunction) => {
try {
const params = {
Bucket: 'dnd-5e-api-images',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { ResourceList, escapeRegExp, redisClient } from '../../util/index.js';
import { Request, Response, NextFunction } from 'express';

import { ResourceList, escapeRegExp, redisClient } from '../../util/index.js';
import MagicItem from '../../models/magicItem/index.js';

export const index = async (req, res, next) => {
interface IndexQuery {
name?: { $regex: RegExp };
}

export const index = async (req: Request, res: Response, next: NextFunction) => {
try {
const searchQueries = {};
const searchQueries: IndexQuery = {};
if (req.query.name !== undefined) {
searchQueries.name = { $regex: new RegExp(escapeRegExp(req.query.name), 'i') };
searchQueries.name = { $regex: new RegExp(escapeRegExp(req.query.name as string), 'i') };
}

const redisKey = req.originalUrl;
let data;
data = await redisClient.get(redisKey);
const data = await redisClient.get(redisKey);

if (data) {
res.status(200).json(JSON.parse(data));
Expand All @@ -28,7 +32,7 @@ export const index = async (req, res, next) => {
}
};

export const show = async (req, res, next) => {
export const show = async (req: Request, res: Response, next: NextFunction) => {
try {
const data = await MagicItem.findOne({ index: req.params.index });
if (!data) return next();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
import { Request, Response, NextFunction } from 'express';

import { ResourceList, escapeRegExp, redisClient } from '../../util/index.js';

import Monster from '../../models/monster/index.js';

export const index = async (req, res, next) => {
interface IndexQuery {
name?: { $regex: RegExp };
challenge_rating?: { $in: string[] | number[] };
}

export const index = async (req: Request, res: Response, next: NextFunction) => {
try {
const searchQueries = {};
const searchQueries: IndexQuery = {};
if (req.query.name !== undefined) {
searchQueries.name = { $regex: new RegExp(escapeRegExp(req.query.name), 'i') };
searchQueries.name = { $regex: new RegExp(escapeRegExp(req.query.name as string), 'i') };
}
if (req.query.challenge_rating !== undefined) {
if (typeof req.query.challenge_rating === 'string') {
req.query.challenge_rating = req.query.challenge_rating
.split(',')
.map(Number)
.filter(item => !isNaN(item));
req.query.challenge_rating = req.query.challenge_rating.split(',');
}
searchQueries.challenge_rating = { $in: req.query.challenge_rating };

const challengeRating = req.query.challenge_rating as string[];
searchQueries.challenge_rating = {
$in: challengeRating.map(Number).filter(item => !isNaN(item)),
};
}

const redisKey = req.originalUrl;
let data;
data = await redisClient.get(redisKey);
const data = await redisClient.get(redisKey);

if (data) {
res.status(200).json(JSON.parse(data));
Expand All @@ -37,7 +44,7 @@ export const index = async (req, res, next) => {
}
};

export const show = async (req, res, next) => {
export const show = async (req: Request, res: Response, next: NextFunction) => {
try {
const data = await Monster.findOne({ index: req.params.index });
if (!data) return next();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Request, Response, NextFunction } from 'express';

import Proficiency from '../../models/proficiency/index.js';
import Race from '../../models/race/index.js';
import { ResourceList } from '../../util/data.js';
Expand All @@ -7,10 +9,12 @@ import Trait from '../../models/trait/index.js';

const simpleController = new SimpleController(Race);

export const index = async (req, res, next) => simpleController.index(req, res, next);
export const show = async (req, res, next) => simpleController.show(req, res, next);
export const index = async (req: Request, res: Response, next: NextFunction) =>
simpleController.index(req, res, next);
export const show = async (req: Request, res: Response, next: NextFunction) =>
simpleController.show(req, res, next);

export const showSubracesForRace = async (req, res, next) => {
export const showSubracesForRace = async (req: Request, res: Response, next: NextFunction) => {
try {
const urlString = '/api/races/' + req.params.index;

Expand All @@ -26,7 +30,7 @@ export const showSubracesForRace = async (req, res, next) => {
}
};

export const showTraitsForRace = async (req, res, next) => {
export const showTraitsForRace = async (req: Request, res: Response, next: NextFunction) => {
try {
const urlString = '/api/races/' + req.params.index;

Expand All @@ -42,7 +46,7 @@ export const showTraitsForRace = async (req, res, next) => {
}
};

export const showProficienciesForRace = async (req, res, next) => {
export const showProficienciesForRace = async (req: Request, res: Response, next: NextFunction) => {
try {
const urlString = '/api/races/' + req.params.index;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import { Request, Response, NextFunction } from 'express';
import { ResourceList, escapeRegExp, redisClient } from '../../util/index.js';

import Rule from '../../models/rule/index.js';

export const index = async (req, res, next) => {
interface IndexQuery {
name?: { $regex: RegExp };
desc?: { $regex: RegExp };
}

export const index = async (req: Request, res: Response, next: NextFunction) => {
try {
const searchQueries = {};
const searchQueries: IndexQuery = {};
if (req.query.name !== undefined) {
searchQueries.name = { $regex: new RegExp(escapeRegExp(req.query.name), 'i') };
searchQueries.name = { $regex: new RegExp(escapeRegExp(req.query.name as string), 'i') };
}
if (req.query.desc !== undefined) {
searchQueries.desc = { $regex: new RegExp(escapeRegExp(req.query.desc), 'i') };
searchQueries.desc = { $regex: new RegExp(escapeRegExp(req.query.desc as string), 'i') };
}

const redisKey = req.originalUrl;
let data;
data = await redisClient.get(redisKey);
const data = await redisClient.get(redisKey);

if (data) {
res.status(200).json(JSON.parse(data));
} else {
Expand All @@ -30,7 +36,7 @@ export const index = async (req, res, next) => {
}
};

export const show = async (req, res, next) => {
export const show = async (req: Request, res: Response, next: NextFunction) => {
try {
const data = await Rule.findOne({ index: req.params.index });
if (!data) return next();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import { Request, Response, NextFunction } from 'express';
import { ResourceList, escapeRegExp, redisClient } from '../../util/index.js';

import RuleSection from '../../models/ruleSection/index.js';
interface IndexQuery {
name?: { $regex: RegExp };
desc?: { $regex: RegExp };
}

export const index = async (req, res, next) => {
export const index = async (req: Request, res: Response, next: NextFunction) => {
try {
const searchQueries = {};
const searchQueries: IndexQuery = {};
if (req.query.name !== undefined) {
searchQueries.name = { $regex: new RegExp(escapeRegExp(req.query.name), 'i') };
searchQueries.name = { $regex: new RegExp(escapeRegExp(req.query.name as string), 'i') };
}
if (req.query.desc !== undefined) {
searchQueries.desc = { $regex: new RegExp(escapeRegExp(req.query.desc), 'i') };
searchQueries.desc = { $regex: new RegExp(escapeRegExp(req.query.desc as string), 'i') };
}

const redisKey = req.originalUrl;
let data;
data = await redisClient.get(redisKey);
const data = await redisClient.get(redisKey);

if (data) {
res.status(200).json(JSON.parse(data));
Expand All @@ -31,7 +35,7 @@ export const index = async (req, res, next) => {
}
};

export const show = async (req, res, next) => {
export const show = async (req: Request, res: Response, next: NextFunction) => {
try {
const data = await RuleSection.findOne({ index: req.params.index });
if (!data) return next();
Expand Down
Loading