webserver-framework/@types/Server.ts

226 lines
5.4 KiB
TypeScript

import { LoggerClientOptions } from '@navy.gif/logger';
import { BrokerOptions, MariaOptions, MongoOptions, ObjectId } from '@navy.gif/wrappers';
import { Request as ExpressRequest, Response as ExpressResponse, NextFunction } from 'express';
import http from 'http';
import { UploadedFile } from 'express-fileupload';
import { Session } from 'express-session';
import AbstractUser from '../src/server/interfaces/AbstractUser.js';
import { Role } from '../src/server/structures/index.js';
/**
* TERMINOLOGY
* Data: Any JSON structured data stored in the database
* Opt(ion)s: Variables passed to functions/constructors
* Json: Any JSON structured data that can be passed to the consumers of the endpoints (i.e. returned to the user)
*
*/
export type PwResetToken = {
token: string,
valid: number,
user: string
}
export type PwResetTokenQuery = {
token?: string,
user?: string
}
export type RateLimit = {
limit: number,
time: number,
disabled?: boolean
}
export type RateLimits = {
[key: string]: RateLimit
}
export type RegistryOptions = {
endpointsPath: string
}
type HttpOptions = {
port: number
} & http.ServerOptions
export type OAuthProvider = {
name: string,
iconURL: string,
authoriseURL: string,
tokenURL: string,
profileURL: string
}
export type ServerOptions = {
dir: string,
name: string,
http: HttpOptions,
databases: {
mariadb?: MariaOptions,
mongodb?: MongoOptions,
broker?: BrokerOptions
},
logger?: LoggerClientOptions,
domain: string,
serveFiles: string,
registrationEnabled: boolean,
requireCodeForRegister: boolean,
OAuthProviders: OAuthProvider[],
callbackPath: string,
discord: {
scope?: string[],
version?: number
},
sweeperInterval?: number
}
export type Permissions = {
[key: string]: number | Permissions
}
export type ExternalProfile = {
provider: string,
username: string,
id: string,
accessToken?: string
}
export type EntityData = {
[key:string]: unknown
id?: string | ObjectId,
name?: string,
disabled?: boolean,
permissions?: Permissions,
permissionsDisabled?: boolean,
createdTimestamp?: number,
note?: string | null
}
export type EntityJson = {
[key: string]: string | string[] | number | boolean | null | ObjectId | Permissions | unknown,
id: string,
name: string,
disabled: boolean,
permissions: Permissions,
createdTimestamp: number,
note: string | null
}
export type RoleData = {
position: number,
rateLimits?: RateLimits,
users?: string[]
} & EntityData
export type RoleJson = {
position: number,
rateLimits: RateLimits
} & EntityJson
export type AbstractUserData = {
icon?: string | null,
roles?: (Role|RoleData|string)[],
temporary?: boolean
} & EntityData
export type AbstractUserJson = {
icon: string,
roles: RoleJson[],
temporary: boolean
} & EntityJson
export type ApplicationData = {
token: string,
ownerId: string | ObjectId,
description: string | null
} & AbstractUserData
export type ApplicationJson = {
token: string,
ownerId: string,
description: string | null
} & AbstractUserJson
export type UserData = {
applications?: string[],
externalProfiles?: { [key: string]: ExternalProfile },
password?: string | null,
otpSecret?: string | null,
twoFactor?: boolean,
displayName?: string | null,
requestPasswordReset?: boolean,
requirePasswordReset?: boolean
} & AbstractUserData
export type UserJson = {
applications: string[]
externalProfiles: { [key: string]: ExternalProfile },
twoFactor: boolean,
displayName: string | null
} & AbstractUserJson
// export type EntityJson = { [key: string]: string | string[] | number | boolean | null | ObjectId | Permissions | unknown }
// export type UserJson = { externalProfiles: {[key: string]: ExternalProfile} } & EntityJson;
export type ParsedQuery = {
[key: string]: string | number | boolean | undefined,
page?: number,
pageSize?: number,
sort?: 'newest' | 'oldest',
userId?: string | number,
rangeStart?: number,
rangeEnd?: number,
timeInterval?: number
}
export type Request = {
user: AbstractUser,
session: {
verified: boolean,
loginMethod: string
} & Session,
files: { [key: string]: UploadedFile },
remoteAddress: string,
parsedQuery?: ParsedQuery,
skipBodyLog?: boolean
} & ExpressRequest
export type Response = {
sentContent: string
} & ExpressResponse
export type MiddlewareFunction = (req: Request, res: Response, next: NextFunction) => void
export type HandlerFunction = (req: Request, res: Response) => void
export type SessionLimit = {
budget: number,
reset: number
}
export type AuthFunction = (req: Request, res: Response, next: NextFunction) => void
export type ExtendedServerOptions = {
//
} & ServerOptions
export type EndpointOptions = {
path: string,
name: string,
auth?: MiddlewareFunction | true | null,
loadOrder?: number
}
export type RegistrationInvite = {
code: string,
validFor: number,
created: number
}
// Types for these don't matter for the server as they're only used by the client
export type GlobalClientSettings = object
export type UserClientSettings = object
export type ClientSettings = {
global: GlobalClientSettings,
user?: UserClientSettings
}