Refactoring to Typescript...
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
import { ProblemId, SolutionId, UserId } from './model'
|
||||
|
||||
// export function refToUserId(ref: string): UserId {
|
||||
// if (!ref.startsWith('user/')) {
|
||||
// throw new Error(`"${ref}" is not a reference to a User`)
|
||||
// }
|
||||
|
||||
// return ref.slice('user/'.length)
|
||||
// }
|
||||
|
||||
// export function refToProblemId(ref: string): ProblemId {
|
||||
// if (!ref.startsWith('problem/')) {
|
||||
// throw new Error(`"${ref}" is not a reference to a Problem`)
|
||||
// }
|
||||
|
||||
// return ref.slice('problem/'.length)
|
||||
// }
|
||||
|
||||
// export function refToSolutionId(ref: string): SolutionId {
|
||||
// if (!ref.startsWith('solution/')) {
|
||||
// throw new Error(`"${ref}" is not a reference to a Solution`)
|
||||
// }
|
||||
|
||||
// return ref.slice('solution/'.length)
|
||||
// }
|
||||
@@ -0,0 +1,62 @@
|
||||
//
|
||||
// Common
|
||||
//
|
||||
|
||||
export type CommonProps = 'id' | 'createdAt'
|
||||
|
||||
type Opaque<T, K, L extends string = 'opaque'> = T & { _: K; __: L }
|
||||
|
||||
type Id<T> = Opaque<string, T, 'id'>
|
||||
|
||||
//
|
||||
// Users
|
||||
//
|
||||
|
||||
export type AdministratorRole = 'admin' | 'moderator'
|
||||
export type StudentRole = 'student'
|
||||
|
||||
export type UserRole = AdministratorRole | StudentRole
|
||||
|
||||
export type UserId = Id<User>
|
||||
|
||||
export type User = {
|
||||
id: UserId
|
||||
role: UserRole
|
||||
}
|
||||
|
||||
export function isAdministrator(role: UserRole): role is AdministratorRole {
|
||||
return role !== 'student'
|
||||
}
|
||||
|
||||
export function isStudent(role: UserRole): role is StudentRole {
|
||||
return role === 'student'
|
||||
}
|
||||
|
||||
//
|
||||
// Problems
|
||||
//
|
||||
|
||||
export type ProblemId = Id<Problem>
|
||||
|
||||
export type Problem = {
|
||||
id: ProblemId
|
||||
content: string
|
||||
createdBy: UserId
|
||||
createdAt: string
|
||||
}
|
||||
|
||||
//
|
||||
// Solutions
|
||||
//
|
||||
|
||||
export type SolutionStatus = 'pending' | 'rejected' | 'accepted'
|
||||
|
||||
export type SolutionId = string
|
||||
|
||||
export type Solution = {
|
||||
id: SolutionId
|
||||
sentBy: UserId
|
||||
forProblem: ProblemId
|
||||
content: string
|
||||
status: SolutionStatus
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
export type RenderedPage = {
|
||||
html: string
|
||||
metadata: any
|
||||
}
|
||||
|
||||
export type RenderFunction = (url: string) => RenderedPage
|
||||
Reference in New Issue
Block a user