shared: schema: split enums from schemas

Otherwise zod will be included in client dependency graph and
increase the bundle size by 10kB unnecessarily.
This commit is contained in:
Jesse Chan
2020-10-28 12:06:14 +08:00
parent 20a1c2a75b
commit 9e03d2a385
14 changed files with 72 additions and 68 deletions
+8 -11
View File
@@ -1,19 +1,16 @@
import * as z from 'zod';
import {nativeEnum, object, string} from 'zod';
import type {infer as zodInfer} from 'zod';
import {AccessLevel} from './constants/Auth';
import {clientConnectionSettingsSchema} from './ClientConnectionSettings';
export enum AccessLevel {
USER = 5,
ADMINISTRATOR = 10,
}
export const credentialsSchema = z.object({
username: z.string(),
password: z.string(),
export const credentialsSchema = object({
username: string(),
password: string(),
client: clientConnectionSettingsSchema,
level: z.nativeEnum(AccessLevel),
level: nativeEnum(AccessLevel),
});
export type Credentials = z.infer<typeof credentialsSchema>;
export type Credentials = zodInfer<typeof credentialsSchema>;
export type UserInDatabase = Required<Credentials> & {_id: string};
+42 -43
View File
@@ -1,68 +1,67 @@
import * as z from 'zod';
import {literal, number, object, string, union} from 'zod';
import type {infer as zodInfer} from 'zod';
const delugeConnectionSettingsSchema = z.object({
client: z.literal('Deluge'),
type: z.literal('web'),
version: z.literal(1),
url: z.string().url(),
password: z.string(),
const delugeConnectionSettingsSchema = object({
client: literal('Deluge'),
type: literal('web'),
version: literal(1),
url: string().url(),
password: string(),
});
export type DelugeConnectionSettings = z.infer<typeof delugeConnectionSettingsSchema>;
export type DelugeConnectionSettings = zodInfer<typeof delugeConnectionSettingsSchema>;
const qBittorrentConnectionSettingsSchema = z.object({
client: z.literal('qBittorrent'),
type: z.literal('web'),
version: z.literal(1),
url: z.string().url(),
username: z.string(),
password: z.string(),
const qBittorrentConnectionSettingsSchema = object({
client: literal('qBittorrent'),
type: literal('web'),
version: literal(1),
url: string().url(),
username: string(),
password: string(),
});
export type QBittorrentConnectionSettings = z.infer<typeof qBittorrentConnectionSettingsSchema>;
export type QBittorrentConnectionSettings = zodInfer<typeof qBittorrentConnectionSettingsSchema>;
const rTorrentTCPConnectionSettingsSchema = z.object({
client: z.literal('rTorrent'),
type: z.literal('tcp'),
version: z.literal(1),
host: z.string(),
port: z.number(),
const rTorrentTCPConnectionSettingsSchema = object({
client: literal('rTorrent'),
type: literal('tcp'),
version: literal(1),
host: string(),
port: number(),
});
export type RTorrentTCPConnectionSettings = z.infer<typeof rTorrentTCPConnectionSettingsSchema>;
export type RTorrentTCPConnectionSettings = zodInfer<typeof rTorrentTCPConnectionSettingsSchema>;
const rTorrentSocketConnectionSettingsSchema = z.object({
client: z.literal('rTorrent'),
type: z.literal('socket'),
version: z.literal(1),
socket: z.string(),
const rTorrentSocketConnectionSettingsSchema = object({
client: literal('rTorrent'),
type: literal('socket'),
version: literal(1),
socket: string(),
});
export type RTorrentSocketConnectionSettings = z.infer<typeof rTorrentSocketConnectionSettingsSchema>;
export type RTorrentSocketConnectionSettings = zodInfer<typeof rTorrentSocketConnectionSettingsSchema>;
const rTorrentConnectionSettingsSchema = z.union([
const rTorrentConnectionSettingsSchema = union([
rTorrentTCPConnectionSettingsSchema,
rTorrentSocketConnectionSettingsSchema,
]);
export type RTorrentConnectionSettings = z.infer<typeof rTorrentConnectionSettingsSchema>;
export type RTorrentConnectionSettings = zodInfer<typeof rTorrentConnectionSettingsSchema>;
const transmissionConnectionSettingsSchema = z.object({
client: z.literal('Transmission'),
type: z.literal('web'),
version: z.literal(1),
url: z.string().url(),
username: z.string(),
password: z.string(),
const transmissionConnectionSettingsSchema = object({
client: literal('Transmission'),
type: literal('web'),
version: literal(1),
url: string().url(),
username: string(),
password: string(),
});
export type TransmissionConnectionSettings = z.infer<typeof transmissionConnectionSettingsSchema>;
export type TransmissionConnectionSettings = zodInfer<typeof transmissionConnectionSettingsSchema>;
export const clientConnectionSettingsSchema = z.union([
export const clientConnectionSettingsSchema = union([
qBittorrentConnectionSettingsSchema,
rTorrentConnectionSettingsSchema,
]);
export type ClientConnectionSettings = z.infer<typeof clientConnectionSettingsSchema>;
export const SUPPORTED_CLIENTS: Array<ClientConnectionSettings['client']> = ['qBittorrent', 'rTorrent'];
export type ClientConnectionSettings = zodInfer<typeof clientConnectionSettingsSchema>;
+6 -5
View File
@@ -1,12 +1,13 @@
import * as z from 'zod';
import type {infer as zodInfer} from 'zod';
import {AccessLevel, credentialsSchema} from '../Auth';
import {AccessLevel} from '../constants/Auth';
import {credentialsSchema} from '../Auth';
// All auth requests are schema validated to ensure security.
// POST /api/auth/authenticate
export const authAuthenticationSchema = credentialsSchema.pick({username: true, password: true});
export type AuthAuthenticationOptions = Required<z.infer<typeof authAuthenticationSchema>>;
export type AuthAuthenticationOptions = Required<zodInfer<typeof authAuthenticationSchema>>;
// POST /api/auth/authenticate - success response
export interface AuthAuthenticationResponse {
@@ -17,11 +18,11 @@ export interface AuthAuthenticationResponse {
// POST /api/auth/register
export const authRegistrationSchema = credentialsSchema;
export type AuthRegistrationOptions = Required<z.infer<typeof authRegistrationSchema>>;
export type AuthRegistrationOptions = Required<zodInfer<typeof authRegistrationSchema>>;
// PATCH /api/auth/users/{username}
export const authUpdateUserSchema = credentialsSchema.partial();
export type AuthUpdateUserOptions = z.infer<typeof authUpdateUserSchema>;
export type AuthUpdateUserOptions = zodInfer<typeof authUpdateUserSchema>;
// GET /api/auth/verify - preload configurations
export interface AuthVerificationPreloadConfigs {
+5
View File
@@ -0,0 +1,5 @@
// eslint-disable-next-line import/prefer-default-export
export enum AccessLevel {
USER = 5,
ADMINISTRATOR = 10,
}
@@ -0,0 +1,2 @@
// eslint-disable-next-line import/prefer-default-export
export const SUPPORTED_CLIENTS = ['qBittorrent', 'rTorrent'] as const;