diff --git a/lib/respond.ts b/lib/respond.ts index d706342..af3e971 100644 --- a/lib/respond.ts +++ b/lib/respond.ts @@ -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) { res.status(statusCode).send(responseJson || {}); } @@ -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) { sendHttpSuccessResponse(res, 200, responseJson); } -export function sendHttpBadRequest(res: NextApiResponse, responseJson?: object) { +export function sendHttpBadRequest(res: NextApiResponse, responseJson?: Record) { 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) { sendHttpErrorResponse(res, 401, { error: 'session is not authenticated', ...responseJson }); } -export function sendHttpUnauthorized(res: NextApiResponse, responseJson?: object) { +export function sendHttpUnauthorized(res: NextApiResponse, responseJson?: Record) { sendHttpErrorResponse(res, 403, { error: 'session is not authorized', ...responseJson }); } -export function sendHttpNotFound(res: NextApiResponse, responseJson?: object) { +export function sendHttpNotFound(res: NextApiResponse, responseJson?: Record) { sendHttpErrorResponse(res, 404, { error: 'resource was not found', ...responseJson }); } -export function sendHttpBadMethod(res: NextApiResponse, responseJson?: object) { +export function sendHttpBadMethod(res: NextApiResponse, responseJson?: Record) { sendHttpErrorResponse(res, 405, { error: 'bad method', ...responseJson }); } -export function sendHttpTooLarge(res: NextApiResponse, responseJson?: object) { +export function sendHttpTooLarge(res: NextApiResponse, responseJson?: Record) { sendHttpErrorResponse(res, 413, { error: 'request body is too large', ...responseJson }); } -export function sendHttpRateLimited(res: NextApiResponse, responseJson?: object) { +export function sendHttpRateLimited(res: NextApiResponse, responseJson?: Record) { sendHttpErrorResponse(res, 429, { error: 'session is rate limited', ...responseJson }); } -export function sendHttpError(res: NextApiResponse, responseJson?: object) { +export function sendHttpError(res: NextApiResponse, responseJson?: Record) { 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) { sendHttpErrorResponse(res, 501, { error: 'this endpoint has not yet been implemented', ...responseJson diff --git a/lib/test-api-endpoint.ts b/lib/test-api-endpoint.ts index 18172ae..838fe45 100644 --- a/lib/test-api-endpoint.ts +++ b/lib/test-api-endpoint.ts @@ -7,7 +7,7 @@ export type TestParams = { fetch: (init?: RequestInit) => ReturnType Promise; - params?: object; + params?: Record; requestPatcher?: (req: IncomingMessage) => void; responsePatcher?: (res: ServerResponse) => void; // eslint-disable-next-line @typescript-eslint/no-explicit-any diff --git a/src/__test__/backend.test.ts b/src/__test__/backend.test.ts index 31d1c23..e3565ff 100644 --- a/src/__test__/backend.test.ts +++ b/src/__test__/backend.test.ts @@ -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 () => { @@ -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 @@ -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 */ }); }); @@ -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(); }); @@ -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); @@ -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 @@ -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({ @@ -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 }, @@ -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 }, @@ -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, @@ -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); @@ -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 @@ -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(); @@ -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 @@ -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'] }] @@ -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 @@ -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 () => { diff --git a/src/__test__/election.test.ts b/src/__test__/election.test.ts index fecfc34..60fa1b2 100644 --- a/src/__test__/election.test.ts +++ b/src/__test__/election.test.ts @@ -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 } = 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) => { const { title, election_id, diff --git a/src/__test__/elections.test.ts b/src/__test__/elections.test.ts index 4168f56..4ba6e64 100644 --- a/src/__test__/elections.test.ts +++ b/src/__test__/elections.test.ts @@ -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 } = 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) => { const { title, election_id, @@ -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) => !containsOnlyPublicData(o))).toBeFalse(); } }); }); diff --git a/src/__test__/meta.test.ts b/src/__test__/meta.test.ts index f3c31f4..a09ac61 100644 --- a/src/__test__/meta.test.ts +++ b/src/__test__/meta.test.ts @@ -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 } = Meta.default; metaEndpoint.config = Meta.config; process.env.REQUESTS_PER_CONTRIVED_ERROR = '0'; diff --git a/src/__test__/voters.test.ts b/src/__test__/voters.test.ts index bda5086..699fe11 100644 --- a/src/__test__/voters.test.ts +++ b/src/__test__/voters.test.ts @@ -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 } = Voters.default; votersEndpoint.config = Voters.config; process.env.REQUESTS_PER_CONTRIVED_ERROR = '0'; diff --git a/src/backend/env.ts b/src/backend/env.ts index 767725d..88fd92f 100644 --- a/src/backend/env.ts +++ b/src/backend/env.ts @@ -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)) { diff --git a/src/backend/index.ts b/src/backend/index.ts index e3f75fb..dc79e1a 100644 --- a/src/backend/index.ts +++ b/src/backend/index.ts @@ -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'); diff --git a/src/backend/middleware.ts b/src/backend/middleware.ts index 9c33f15..08cdc39 100644 --- a/src/backend/middleware.ts +++ b/src/backend/middleware.ts @@ -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) { sendHttpErrorResponse(res, 555, { error: '(note: do not report this contrived error)', ...responseJson diff --git a/types/_shared.ts b/types/_shared.ts index 52252ab..ca683f1 100644 --- a/types/_shared.ts +++ b/types/_shared.ts @@ -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 = { +export type NextParamsRR> = { req: NextApiRequest; res: NextApiResponse; }; -export type NextParamsRRQ = NextParamsRR & { query: object }; +export type NextParamsRRQ = NextParamsRR & { query: Record };