-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
174 lines (153 loc) · 4.21 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import fetch from 'node-fetch'
import qs from 'qs'
import uuid from 'uuid'
import moment from 'moment'
const API_BASE_URL = 'https://thingscloud.appspot.com/version/1/history'
type CreateTodoDestination = 'inbox' | 'today' | 'scheduled'
type CreateTodoOptions = {
title?: string,
dueDate?: string,
scheduled?: string,
note?: string,
aorId?: string,
destination?: CreateTodoDestination
checklist?: Array<string>
}
class ThingsAPIClient {
accountId: string
constructor(accountId: string) {
this.accountId = accountId
}
constructURL(path: string, query: object = {}) {
return `${API_BASE_URL}/${this.accountId}${path}?${qs.stringify(query)}`
}
async performGetRequest(path: string, query: object = {}) {
const url = this.constructURL(path, query)
const req = await fetch(url)
return req.json()
}
async performPostRequest(path: string, body: object) {
const url = this.constructURL(path, {})
const req = await fetch(url, {
method: 'POST',
headers: {
'User-Agent': 'ThingsMac/20808500mas (x86_64; OS X 10.12.2; en_DK)',
'Content-Type': 'application/json; charset=UTF-8',
'Content-Encoding': 'UTF-8'
},
body: JSON.stringify(body)
})
return req.json()
}
async getCurrentIndex() {
const res = await this.performGetRequest('')
return res['current-item-index']
}
async createTodoItem(options: CreateTodoOptions) {
const currentIndex = await this.getCurrentIndex()
const todoItem = this.constructTodoItem(options)
const body = {
'current-item-index': currentIndex,
'items': [todoItem],
'schema': 301
}
const res = this.performPostRequest('/items', body)
return res
}
constructTodoItem(options: CreateTodoOptions) {
const title = options.title || 'New todo'
const scheduled = options.scheduled ? moment(moment(options.scheduled).format('YYYY-MM-DD')).unix() : null
const dueDate = options.dueDate || null
let note = options.note || null
const destination = options.destination || 'inbox'
const uid = uuid.v4().toUpperCase()
const now = Date.now() / 1000
const ar = options.aorId ? [options.aorId] : []
let st
let sr
if (destination === 'inbox') {
st = 0
} else if (destination === 'today') {
st = 1
} else if (destination === 'scheduled') {
st = 0
} else {
throw new Error('unsupported destination')
}
if (destination === 'today') {
sr = moment(moment(new Date()).format('YYYY-MM-DD')).unix()
} else if (destination === 'scheduled') {
sr = scheduled
} else {
sr = null
}
if (note != null) {
note = '<note xml:space=\"preserve\">' + note + '</note>'
}
const items: any = {
[uid]: {
"t": 0,
"e": "Task3",
"p": {
"agr": [],
"ar": ar,
"ato": null,
"dds": null,
"lai": null,
"sb": 0,
"acrd": null,
"cd": now,
"dd": dueDate,
"dl": [],
"do": 0,
"icc": 0,
"icp": false,
"icsd": null,
"ix": 0,
"md": now,
"nt": note,
"pr": [],
"rr": null,
"rt": null,
"sp": null,
"sr": sr,
"ss": 0,
"st": st,
"tg": [],
"ti": 0,
"tir": sr,
"tp": 0,
"tr": false,
"tt": title
}
}
}
if (options.checklist) {
for (const c of options.checklist) {
const cuid = uuid.v4().toUpperCase()
items[cuid] = {
t: 0,
e: "ChecklistItem",
p: {
cd: now,
ts: [uid],
sp: null,
ss: 0,
md: now,
tt: c,
ix: 0
}
}
}
}
return items
}
}
// TODO(@kern): Create a stateful client that loads all items into a postgres db using jsonb
const client = new ThingsAPIClient(process.env.ACCOUNT_ID || '')
client.createTodoItem({
title: 'Pick up the package at the Austin front desk',
destination: 'today',
aorId: process.env.AOR || undefined,
checklist: ['foo', 'bar', 'baz']
}).then(console.log).catch(console.error)