Improving type safety
This commit is contained in:
parent
37e4f35e33
commit
8cf563ed92
12 changed files with 26 additions and 23 deletions
|
@ -3,8 +3,8 @@ import sendError, { API } from "./sendError"
|
|||
import type { Player } from "@prisma/client"
|
||||
import bcrypt from "bcrypt"
|
||||
|
||||
export default async function checkPasswordIsValid(
|
||||
context: API,
|
||||
export default async function checkPasswordIsValid<T>(
|
||||
context: API<T>,
|
||||
player: Player,
|
||||
password: string,
|
||||
next: () => void
|
||||
|
|
|
@ -4,8 +4,8 @@ import type { IdToken, RawToken } from "./createTokenDB"
|
|||
import sendError, { API } from "./sendError"
|
||||
import jwt from "jsonwebtoken"
|
||||
|
||||
async function checkTokenIsValid(
|
||||
context: API,
|
||||
async function checkTokenIsValid<T>(
|
||||
context: API<T>,
|
||||
rawToken: RawToken,
|
||||
next: (token: IdToken) => void
|
||||
) {
|
||||
|
|
|
@ -3,8 +3,8 @@ import { rejectionErrors } from "../errors"
|
|||
import sendError, { API } from "./sendError"
|
||||
import type { Player, Token } from "@prisma/client"
|
||||
|
||||
export default async function getPlayerByIdDB(
|
||||
context: API,
|
||||
export default async function getPlayerByIdDB<T>(
|
||||
context: API<T>,
|
||||
tokenDB: Token,
|
||||
next: (player: Player) => void
|
||||
) {
|
||||
|
|
|
@ -3,8 +3,8 @@ import { rejectionErrors } from "../errors"
|
|||
import sendError, { API } from "./sendError"
|
||||
import type { Player } from "@prisma/client"
|
||||
|
||||
export default async function getPlayerByNameDB(
|
||||
context: API,
|
||||
export default async function getPlayerByNameDB<T>(
|
||||
context: API<T>,
|
||||
username: string,
|
||||
next: (player: Player) => void
|
||||
) {
|
||||
|
|
|
@ -4,8 +4,8 @@ import type { IdToken } from "./createTokenDB"
|
|||
import sendError, { API } from "./sendError"
|
||||
import type { Token } from "@prisma/client"
|
||||
|
||||
async function getTokenDB(
|
||||
context: API,
|
||||
async function getTokenDB<T>(
|
||||
context: API<T>,
|
||||
token: IdToken,
|
||||
next: (tokenDB: Token) => void
|
||||
) {
|
||||
|
|
|
@ -2,8 +2,8 @@ import { rejectionErrorFns } from "../errors"
|
|||
import type { RawToken } from "./createTokenDB"
|
||||
import sendError, { API } from "./sendError"
|
||||
|
||||
async function getTokenFromBody(
|
||||
context: API,
|
||||
async function getTokenFromBody<T>(
|
||||
context: API<T>,
|
||||
next: (accessToken: RawToken) => void
|
||||
) {
|
||||
const type = "ACCESS"
|
||||
|
|
|
@ -2,8 +2,8 @@ import createPlayerDB from "./createPlayerDB"
|
|||
import createTokenDB, { RawToken } from "./createTokenDB"
|
||||
import type { API } from "./sendError"
|
||||
|
||||
async function getTokenFromCookie(
|
||||
context: API,
|
||||
async function getTokenFromCookie<T>(
|
||||
context: API<T>,
|
||||
next: (refreshToken: RawToken) => void
|
||||
) {
|
||||
const type = "REFRESH"
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import { rejectionErrors } from "../errors"
|
||||
import sendError, { API } from "./sendError"
|
||||
|
||||
async function getUserFromBody(
|
||||
context: API,
|
||||
async function getUserFromBody<T>(
|
||||
context: API<T>,
|
||||
next: (username: string, password: string) => void
|
||||
) {
|
||||
const body = JSON.parse(context.req.body)
|
||||
|
|
|
@ -2,12 +2,15 @@ import type { rejectionError } from "../errors"
|
|||
import logging from "../logging"
|
||||
import type { NextApiRequest, NextApiResponse } from "next"
|
||||
|
||||
export interface API {
|
||||
export interface API<T> {
|
||||
req: NextApiRequest
|
||||
res: NextApiResponse
|
||||
res: NextApiResponse<T>
|
||||
}
|
||||
|
||||
export default function sendError(context: API, err: rejectionError | Error) {
|
||||
export default function sendError<T>(
|
||||
context: API<T>,
|
||||
err: rejectionError | Error
|
||||
) {
|
||||
const { res, req } = context
|
||||
// If something went wrong, let the client know with status 500
|
||||
res.status("statusCode" in err ? err.statusCode : 500).end()
|
||||
|
|
|
@ -8,7 +8,7 @@ export interface Result<T> {
|
|||
type?: Logging[]
|
||||
}
|
||||
|
||||
export default function sendResponse<T>(context: API, result: Result<T>) {
|
||||
export default function sendResponse<T>(context: API<T>, result: Result<T>) {
|
||||
const { req, res } = context
|
||||
res.status(result.statusCode ?? 200)
|
||||
result.body ? res.json(result.body) : res.end()
|
||||
|
|
|
@ -3,8 +3,8 @@ import { rejectionErrors } from "../errors"
|
|||
import sendError, { API } from "./sendError"
|
||||
import type { Player, Prisma } from "@prisma/client"
|
||||
|
||||
async function updatePlayerDB(
|
||||
context: API,
|
||||
async function updatePlayerDB<T>(
|
||||
context: API<T>,
|
||||
player: Player,
|
||||
data: Prisma.PlayerUpdateInput,
|
||||
next: (updatedPlayer: Player) => void
|
||||
|
|
|
@ -53,7 +53,7 @@ export default async function login(
|
|||
).catch((err) => sendError(context, err))
|
||||
}
|
||||
|
||||
async function preCheck(context: API, next: () => void) {
|
||||
async function preCheck<T>(context: API<T>, next: () => void) {
|
||||
const { req } = context
|
||||
const oldRefreshToken = req.cookies.token
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue