wireadmin/src/lib/typings.ts
2023-09-18 21:01:07 +03:30

82 lines
2.3 KiB
TypeScript

import { z } from "zod";
import type React from "react";
import { IPV4_REGEX } from "@lib/constants";
import { NextApiRequest as TNextApiRequest } from "next/dist/shared/lib/utils";
export const WgKeySchema = z.object({
privateKey: z.string(),
publicKey: z.string(),
})
export type WgKey = z.infer<typeof WgKeySchema>
export interface WgPeerConfig {
publicKey: string
preSharedKey: string
endpoint: string
allowedIps: string[]
persistentKeepalive: number | null
}
const WgPeerSchema = z.object({
id: z.string().uuid(),
name: z.string().regex(/^[A-Za-z\d\s]{3,32}$/),
preSharedKey: z.string(),
endpoint: z.string(),
address: z.string().regex(IPV4_REGEX),
latestHandshakeAt: z.string().nullable(),
transferRx: z.number().nullable(),
transferTx: z.number().nullable(),
persistentKeepalive: z.number().nullable(),
createdAt: z.string().datetime(),
updatedAt: z.string().datetime(),
enabled: z.boolean(),
})
.merge(WgKeySchema)
export type WgPeer = z.infer<typeof WgPeerSchema>
export const WgServerSchema = z.object({
id: z.string().uuid(),
confId: z.number(),
type: z.enum([ 'default', 'bridge', 'tor' ]),
name: z.string().regex(/^[A-Za-z\d\s]{3,32}$/),
address: z.string().regex(IPV4_REGEX),
listen: z.number(),
preUp: z.string().nullable(),
postUp: z.string().nullable(),
preDown: z.string().nullable(),
postDown: z.string().nullable(),
dns: z.string().regex(IPV4_REGEX).nullable(),
peers: z.array(WgPeerSchema),
createdAt: z.string().datetime(),
updatedAt: z.string().datetime(),
status: z.enum([ 'up', 'down' ]),
})
.merge(WgKeySchema)
export type WgServer = z.infer<typeof WgServerSchema>
export type ReactHTMLProps<T extends HTMLElement> = React.DetailedHTMLProps<React.HTMLAttributes<T>, T>
export type APIErrorResponse = {
ok: false
details: string
}
export type APISuccessResponse<D> = {
ok: true
result: D
}
export type APIResponse<D, N extends boolean = false> = N extends true ? D : APIErrorResponse | APISuccessResponse<D>
export type NextApiRequest<
P = Record<string, string | string[] | undefined>,
Q = Record<string, string | string[] | undefined>
> = Omit<TNextApiRequest, 'query'> & {
query: Partial<Q> & P
}
export type LeastOne<T, U = { [K in keyof T]: Pick<T, K> }> = Partial<T> & U[keyof U];