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

View File

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

View File

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