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