use typescript public/private constructor assignment

main
Yorick van Pelt 2023-04-19 11:01:23 +02:00
parent 929199338d
commit 4ba48ae97d
Signed by: yorick
GPG Key ID: A36E70F9DC014A15
3 changed files with 19 additions and 46 deletions

View File

@ -120,10 +120,8 @@ export namespace API {
}
}
export class Beeminder {
token: string
constructor(token: string) {
this.token = token
}
constructor(private readonly token: string) {}
async post_request(url: string, post_data: any, method: "POST" | "PUT" = "POST") {
const response = await fetch("https://www.beeminder.com/api/v1/" + url + ".json", {
method,
@ -154,12 +152,7 @@ export class Beeminder {
}
export class User {
b: Beeminder
user: string
constructor(b: Beeminder, user: string) {
this.b = b
this.user = user
}
constructor(public readonly b: Beeminder, public readonly user: string) {}
info() {
return this.b.get_request(`users/${this.user}`)
}
@ -174,15 +167,8 @@ export class User {
}
}
export class Goal {
b: Beeminder
user: string
goal: string
prefix: string
constructor(b: Beeminder, user: string, goal: string) {
Object.assign(this, { b, user, goal })
this.b = b
this.user = user
this.goal = goal
readonly prefix: string
constructor(public readonly b: Beeminder, public readonly user: string, public readonly goal: string) {
this.prefix = `users/${this.user}/goals/${this.goal}`
}
info(): Promise<Goal> {

View File

@ -21,12 +21,11 @@ interface Credentials {
}
export class Marvin {
credentials: Credentials
remote: PouchDB.Database
db: PouchDB.Database
synced: Promise<PouchDB.Find.CreateIndexResponse<{}>>
root: Taskset
constructor(credentials: Credentials=Marvin.parseCredentials(), dbName = "marvin") {
constructor(public credentials: Credentials=Marvin.parseCredentials(), dbName = "marvin") {
const c = credentials
let u = new URL(c.syncServer)
Object.assign(u, {
@ -34,7 +33,6 @@ export class Marvin {
password: c.syncPassword,
pathname: "/" + c.syncDatabase
})
this.credentials = credentials
this.remote = new PouchDB(u.href)
this.db = new PouchDB(dbName)
this.synced = new Promise((resolve, reject) => {
@ -128,7 +126,6 @@ function toInterval([start, end]: [number, number]): Interval {
}
class Task {
_task: Marvin_types.Task
title: string
day: string
dueDate: string | null
@ -136,15 +133,14 @@ class Task {
doneAt: DateTime | null
createdAt: DateTime
times: Interval[]
constructor(task: Marvin_types.Task) {
this._task = task
this.title = task.title
this.day = task.day
this.dueDate = task.dueDate
this.done = !!task.done
this.doneAt = task.doneAt ? DateTime.fromMillis(task.doneAt) : null
this.createdAt = DateTime.fromMillis(task.createdAt)
this.times = task.times ? chunk<number, 2>(task.times, 2).map(toInterval) : []
constructor(public _task: Marvin_types.Task) {
this.title = _task.title
this.day = _task.day
this.dueDate = _task.dueDate
this.done = !!_task.done
this.doneAt = _task.doneAt ? DateTime.fromMillis(_task.doneAt) : null
this.createdAt = DateTime.fromMillis(_task.createdAt)
this.times = _task.times ? chunk<number, 2>(_task.times, 2).map(toInterval) : []
}
timesInInterval(interval: Interval): Interval[] {
return this.times.map(time => time.intersection(interval)).filter(x => x !== null) as Interval[]
@ -166,12 +162,8 @@ class Task {
}
class Taskset {
marv: Marvin
_id: string
constructor(marv: Marvin, _id: string, rest: Marvin_types.DBEntry | null) {
this.marv = marv
this._id = _id
Object.assign(this, {marv, _id}, rest)
constructor(public readonly marv: Marvin, public readonly _id: string, rest: Marvin_types.DBEntry | null) {
Object.assign(this, rest)
}
async category(n: string): Promise<Taskset> {
await this.marv.synced

View File

@ -37,8 +37,7 @@ import Req = API.Request
import Res = API.Response
export class Toggl {
api_token: string
constructor(api_token=readFileSync("toggl-api", "utf8").trim()) {
constructor(private readonly api_token: string=readFileSync("toggl-api", "utf8").trim()) {
this.api_token = api_token
}
async parseResponse(response: Awaited<ReturnType<typeof fetch>>) {
@ -86,12 +85,8 @@ export class Toggl {
}
}
export class Workspace {
toggl: Toggl
workspace: Toggl_types.Workspace
constructor(t: Toggl, w: Toggl_types.Workspace) {
this.toggl = t
this.workspace = w
}
constructor(public readonly toggl: Toggl, public readonly workspace: Toggl_types.Workspace) {}
async post_time_entry(query: Omit<Req.POST.TimeEntry, "workspace_id">): Promise<Toggl_types.TimeEntry> {
const workspace_id = this.workspace.id
const q2: Req.POST.TimeEntry = Object.assign({ workspace_id }, query)