Skip to content

Commit

Permalink
fixed problems revealed by check-types
Browse files Browse the repository at this point in the history
  • Loading branch information
Xunnamius committed Jun 13, 2020
1 parent 06be527 commit 015027b
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 45 deletions.
22 changes: 11 additions & 11 deletions lib/respond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
ErrorJsonResponse
} from 'types/global'

export function sendGenericHttpResponse(res: NextApiResponse, statusCode: HttpStatusCode, responseJson?: object) {
export function sendGenericHttpResponse(res: NextApiResponse, statusCode: HttpStatusCode, responseJson?: Record<string, unknown>) {
res.status(statusCode).send(responseJson || {});
}

Expand All @@ -22,67 +22,67 @@ export function sendHttpErrorResponse(res: NextApiResponse, statusCode: HttpStat
return responseJson;
}

export function sendHttpOk(res: NextApiResponse, responseJson?: object) {
export function sendHttpOk(res: NextApiResponse, responseJson?: Record<string, unknown>) {
sendHttpSuccessResponse(res, 200, responseJson);
}

export function sendHttpBadRequest(res: NextApiResponse, responseJson?: object) {
export function sendHttpBadRequest(res: NextApiResponse, responseJson?: Record<string, unknown>) {
sendHttpErrorResponse(res, 400, {
error: 'request was malformed or otherwise bad',
...responseJson
});
}

export function sendHttpUnauthenticated(res: NextApiResponse, responseJson?: object) {
export function sendHttpUnauthenticated(res: NextApiResponse, responseJson?: Record<string, unknown>) {
sendHttpErrorResponse(res, 401, {
error: 'session is not authenticated',
...responseJson
});
}

export function sendHttpUnauthorized(res: NextApiResponse, responseJson?: object) {
export function sendHttpUnauthorized(res: NextApiResponse, responseJson?: Record<string, unknown>) {
sendHttpErrorResponse(res, 403, {
error: 'session is not authorized',
...responseJson
});
}

export function sendHttpNotFound(res: NextApiResponse, responseJson?: object) {
export function sendHttpNotFound(res: NextApiResponse, responseJson?: Record<string, unknown>) {
sendHttpErrorResponse(res, 404, {
error: 'resource was not found',
...responseJson
});
}

export function sendHttpBadMethod(res: NextApiResponse, responseJson?: object) {
export function sendHttpBadMethod(res: NextApiResponse, responseJson?: Record<string, unknown>) {
sendHttpErrorResponse(res, 405, {
error: 'bad method',
...responseJson
});
}

export function sendHttpTooLarge(res: NextApiResponse, responseJson?: object) {
export function sendHttpTooLarge(res: NextApiResponse, responseJson?: Record<string, unknown>) {
sendHttpErrorResponse(res, 413, {
error: 'request body is too large',
...responseJson
});
}

export function sendHttpRateLimited(res: NextApiResponse, responseJson?: object) {
export function sendHttpRateLimited(res: NextApiResponse, responseJson?: Record<string, unknown>) {
sendHttpErrorResponse(res, 429, {
error: 'session is rate limited',
...responseJson
});
}

export function sendHttpError(res: NextApiResponse, responseJson?: object) {
export function sendHttpError(res: NextApiResponse, responseJson?: Record<string, unknown>) {
sendHttpErrorResponse(res, 500, {
error: '🤯 something unexpected happened on our end 🤯',
...responseJson
});
}

export function sendNotImplementedError(res: NextApiResponse, responseJson?: object) {
export function sendNotImplementedError(res: NextApiResponse, responseJson?: Record<string, unknown>) {
sendHttpErrorResponse(res, 501, {
error: 'this endpoint has not yet been implemented',
...responseJson
Expand Down
2 changes: 1 addition & 1 deletion lib/test-api-endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type TestParams = { fetch: (init?: RequestInit) => ReturnType<typeof fetc

export type TesApiEndParams = {
test: (obj: TestParams) => Promise<void>;
params?: object;
params?: Record<string, unknown>;
requestPatcher?: (req: IncomingMessage) => void;
responsePatcher?: (res: ServerResponse) => void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
42 changes: 21 additions & 21 deletions src/__test__/backend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ describe('universe/backend', () => {
});

it('rejects if no key is provided', async () => {
/* eslint-disable @typescript-eslint/ban-ts-ignore */
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore
expect(Backend.getPublicElections()).toReject();
// @ts-ignore
expect(Backend.getPublicElections({ key: 5 })).toReject();
// @ts-ignore
expect(Backend.getPublicElections({ key: null })).toReject();
/* eslint-enable @typescript-eslint/ban-ts-ignore */
/* eslint-enable @typescript-eslint/ban-ts-comment */
});

it('returns paginated data respecting limit and after', async () => {
Expand Down Expand Up @@ -174,14 +174,14 @@ describe('universe/backend', () => {

expect(Backend.getPublicElections({
key: Backend.NULL_KEY,
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
after: 'notAnObjectId!'
})).toReject();
});

it('rejects on strange/bad limit and/or after', async () => {
/* eslint-disable @typescript-eslint/ban-ts-ignore */
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore
expect(Backend.getPublicElections({ key: Backend.NULL_KEY, limit: 'lol' })).toReject();
// @ts-ignore
Expand All @@ -194,7 +194,7 @@ describe('universe/backend', () => {
expect(Backend.getPublicElections({ key: Backend.NULL_KEY, after: 100 })).toReject();
// @ts-ignore
expect(Backend.getPublicElections({ key: Backend.NULL_KEY, after: false })).toReject();
/* eslint-enable @typescript-eslint/ban-ts-ignore */
/* eslint-enable @typescript-eslint/ban-ts-comment */
});
});

Expand All @@ -220,7 +220,7 @@ describe('universe/backend', () => {

it('rejects if election_id does not exist', async () => {
expect(Backend.getPublicElection({ electionId: new ObjectId(), key: Backend.NULL_KEY })).toReject();
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
expect(Backend.getPublicElection({ electionId: 'not a real id', key: Backend.NULL_KEY })).toReject();
});
Expand All @@ -247,12 +247,12 @@ describe('universe/backend', () => {
const elections = getHydratedData().elections;

expect(await Backend.doesElectionExist(new ObjectId())).toEqual(false);
/* eslint-disable @typescript-eslint/ban-ts-ignore */
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore
expect(Backend.doesElectionExist(null)).toReject();
// @ts-ignore
expect(Backend.doesElectionExist(undefined)).toReject();
/* eslint-enable @typescript-eslint/ban-ts-ignore */
/* eslint-enable @typescript-eslint/ban-ts-comment */
expect(await Backend.doesElectionExist(elections[0].election_id)).toEqual(true);
expect(await Backend.doesElectionExist(elections[1].election_id)).toEqual(true);
expect(await Backend.doesElectionExist(elections[5].election_id)).toEqual(true);
Expand All @@ -273,7 +273,7 @@ describe('universe/backend', () => {
// ? Bad props should be ignored
const badProps = {
election_id: new ObjectId(),
/* eslint-disable @typescript-eslint/ban-ts-ignore */
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore
_id: new ObjectId(),
// @ts-ignore
Expand All @@ -286,7 +286,7 @@ describe('universe/backend', () => {
fakeprop: 'bad',
//@ ts-ignore
options: ['repeated', 'repeated']
/* eslint-enable @typescript-eslint/ban-ts-ignore */
/* eslint-enable @typescript-eslint/ban-ts-comment */
};

const results = await Promise.all(Object.entries(badProps).map(([k, v]) => Backend.upsertElection({
Expand Down Expand Up @@ -328,7 +328,7 @@ describe('universe/backend', () => {
key: Backend.NULL_KEY
})).toReject();

/* eslint-disable @typescript-eslint/ban-ts-ignore */
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore
expect(Backend.upsertElection({
election: { ...badElection, opens },
Expand All @@ -340,7 +340,7 @@ describe('universe/backend', () => {
election: { ...badElection, closes },
key: Backend.NULL_KEY
})).toReject();
/* eslint-enable @typescript-eslint/ban-ts-ignore */
/* eslint-enable @typescript-eslint/ban-ts-comment */

expect(Backend.upsertElection({
election: { ...badElection, closes: 100, opens },
Expand Down Expand Up @@ -383,7 +383,7 @@ describe('universe/backend', () => {

// ? Bad props should be ignored
const badProps = {
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
created: 100,
opens: newElection.closes,
Expand All @@ -409,12 +409,12 @@ describe('universe/backend', () => {
const returnedElection = await Backend.getInternalElection(election1.election_id || new ObjectId('bad'));

expect(returnedElection.election_id).toEqual(election1.election_id);
/* eslint-disable @typescript-eslint/ban-ts-ignore */
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore
expect(returnedElection._id).toBeUndefined();
// @ts-ignore
expect(returnedElection.extra).toBeUndefined();
/* eslint-enable @typescript-eslint/ban-ts-ignore */
/* eslint-enable @typescript-eslint/ban-ts-comment */
expect(returnedElection.created).toEqual(election1.created);
// ? Bad props should be ignored!
expect(returnedElection.created).not.toEqual(100);
Expand All @@ -431,7 +431,7 @@ describe('universe/backend', () => {
});

it('rejects when missing necessary params', async () => {
/* eslint-disable @typescript-eslint/ban-ts-ignore */
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore
const newElection00: NewElection = { election_id: 'fake', bad: 'nope' };
// @ts-ignore
Expand All @@ -454,7 +454,7 @@ describe('universe/backend', () => {
title: 'New election',
opens: Date.now() + 10**6,
};
/* eslint-enable @typescript-eslint/ban-ts-ignore */
/* eslint-enable @typescript-eslint/ban-ts-comment */

expect(Backend.upsertElection({ election: newElection1, key: Backend.NULL_KEY })).toReject();
expect(Backend.upsertElection({ election: newElection2, key: Backend.NULL_KEY })).toReject();
Expand Down Expand Up @@ -769,7 +769,7 @@ describe('universe/backend', () => {

it('::replaceRankings rejects on illegal options', async () => {
const election = getHydratedData().elections[0];
/* eslint-disable @typescript-eslint/ban-ts-ignore */
/* eslint-disable @typescript-eslint/ban-ts-comment */
expect(Backend.replaceRankings({
electionId: election.election_id,
// @ts-ignore
Expand Down Expand Up @@ -818,7 +818,7 @@ describe('universe/backend', () => {
rankings: [{ voter_id: '1', ranking: [1] }]
})).toReject();

/* eslint-enable @typescript-eslint/ban-ts-ignore */
/* eslint-enable @typescript-eslint/ban-ts-comment */
expect(Backend.replaceRankings({
electionId: election.election_id,
rankings: [{ voter_id: '1', ranking: ['1'] }]
Expand Down Expand Up @@ -931,7 +931,7 @@ describe('universe/backend', () => {
];
const newRankings24 = [{ voter_id: '', ranking: election.options }];

/* eslint-disable @typescript-eslint/ban-ts-ignore */
/* eslint-disable @typescript-eslint/ban-ts-comment */
expect(Backend.replaceRankings({
electionId: election.election_id,
// @ts-ignore
Expand Down Expand Up @@ -1075,7 +1075,7 @@ describe('universe/backend', () => {
// @ts-ignore
rankings: newRankings24
})).toReject();
/* eslint-enable @typescript-eslint/ban-ts-ignore */
/* eslint-enable @typescript-eslint/ban-ts-comment */
});

it('can fetch the empty array inserted when a brand new election is created', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/__test__/election.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import type { PublicElection } from 'types/global'

const { getHydratedData } = setupJest();

const electionEndpoint: typeof Election.default & { config?: object } = Election.default;
const electionEndpoint: typeof Election.default & { config?: Record<string, unknown> } = Election.default;
electionEndpoint.config = Election.config;

process.env.REQUESTS_PER_CONTRIVED_ERROR = '0';

const KEY = '5db4c4d3-294a-4086-9751-f3fce82d11e4';

const containsOnlyPublicData = (o: object) => {
const containsOnlyPublicData = (o: Record<string, unknown>) => {
const {
title,
election_id,
Expand Down
6 changes: 3 additions & 3 deletions src/__test__/elections.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import type { PublicElection, InternalElection } from 'types/global'

const { getHydratedData, getDb } = setupJest();

const electionsEndpoint: typeof Elections.default & { config?: object } = Elections.default;
const electionsEndpoint: typeof Elections.default & { config?: Record<string, unknown> } = Elections.default;
electionsEndpoint.config = Elections.config;

process.env.REQUESTS_PER_CONTRIVED_ERROR = '0';

const KEY = '5db4c4d3-294a-4086-9751-f3fce82d11e4';

const containsOnlyPublicData = (o: object) => {
const containsOnlyPublicData = (o: Record<string, unknown>) => {
const {
title,
election_id,
Expand Down Expand Up @@ -190,7 +190,7 @@ describe('api/v1/elections', () => {

expect(response.status).toBe(200);
expect(json.success).toBe(true);
expect(json.elections.some((o: object) => !containsOnlyPublicData(o))).toBeFalse();
expect(json.elections.some((o: Record<string, unknown>) => !containsOnlyPublicData(o))).toBeFalse();
}
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/__test__/meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getEnv } from 'universe/backend/env'

const { getHydratedData } = setupJest();

const metaEndpoint: typeof Meta.default & { config?: object } = Meta.default;
const metaEndpoint: typeof Meta.default & { config?: Record<string, unknown> } = Meta.default;
metaEndpoint.config = Meta.config;

process.env.REQUESTS_PER_CONTRIVED_ERROR = '0';
Expand Down
2 changes: 1 addition & 1 deletion src/__test__/voters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ObjectId } from 'mongodb'

const { getHydratedData } = setupJest();

const votersEndpoint: typeof Voters.default & { config?: object } = Voters.default;
const votersEndpoint: typeof Voters.default & { config?: Record<string, unknown> } = Voters.default;
votersEndpoint.config = Voters.config;

process.env.REQUESTS_PER_CONTRIVED_ERROR = '0';
Expand Down
2 changes: 1 addition & 1 deletion src/backend/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function getEnv(loud=false) {
console.info(env);
}

// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if(env.NODE_ENV == 'unknown' || (isServer() && env.MONGODB_URI === '') ||
_mustBeGtZero.some(v => !isNumber(v) || v < 0)) {
Expand Down
2 changes: 1 addition & 1 deletion src/backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ export async function upsertElection(opts: UpsNewEleParams | UpsPatEleParams): P

const { title, description, options, opens, closes, ...rest } = electionData as NewElection;

// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if(rest.deleted !== undefined)
throw new ValidationError('property "deleted" not allowed here');
Expand Down
2 changes: 1 addition & 1 deletion src/backend/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const runCorsMiddleware = (req: any, res: any) => {
}
/* eslint-enable @typescript-eslint/no-explicit-any */

export function sendHttpContrivedError(res: NextApiResponse, responseJson?: object) {
export function sendHttpContrivedError(res: NextApiResponse, responseJson?: Record<string, unknown>) {
sendHttpErrorResponse(res, 555, {
error: '(note: do not report this contrived error)',
...responseJson
Expand Down
4 changes: 2 additions & 2 deletions types/_shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export type HttpStatusCode =

| 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 555 | 598 | 599;

export type NextParamsRR<T=object> = {
export type NextParamsRR<T=Record<string, unknown>> = {
req: NextApiRequest;
res: NextApiResponse<T>;
};

export type NextParamsRRQ = NextParamsRR & { query: object };
export type NextParamsRRQ = NextParamsRR & { query: Record<string, unknown> };

0 comments on commit 015027b

Please sign in to comment.