Skip to content

Commit 48f90c2

Browse files
authored
Merge pull request #22 from riexn/duedate-fix
Fix due date type when creating a new item
2 parents 35b52ee + 8332c89 commit 48f90c2

File tree

7 files changed

+161
-51
lines changed

7 files changed

+161
-51
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
dist
22
node_modules
3+
.env

package-lock.json

Lines changed: 52 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
"@types/jest": "^25.1.4",
5151
"@types/node": "^13.9.8",
5252
"@types/uuid": "^8.0.0",
53+
"cross-env": "^7.0.3",
54+
"dotenv": "^10.0.0",
5355
"faker": "^4.1.0",
5456
"jest": "^25.2.4",
5557
"nodemon": "^2.0.4",

src/v8-types.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ export interface DueDate {
2020
is_recurring: boolean
2121
}
2222

23+
export interface DueDateInputString {
24+
string: string
25+
timezone?: string
26+
}
27+
28+
export interface DueDateInputDate {
29+
date: string
30+
}
31+
32+
export type DueDateInput = DueDateInputString | DueDateInputDate
33+
2334
export type Boolean = 0 | 1
2435

2536
export type Priority = 1 | 2 | 3 | 4
@@ -110,7 +121,7 @@ export interface ItemAdd {
110121
/** The id of the project to add the task to (a number or a temp id). By default the task is added to the user’s Inbox project. */
111122
project_id?: Id
112123
/** The due date of the task. See the Due dates section for more details. */
113-
due?: DueDate
124+
due?: DueDateInput
114125
/** The priority of the task (a number between 1 and 4, 4 for very urgent and 1 for natural). */
115126
/** Note: Keep in mind that very urgent is the priority 1 on clients. So, p1 will return 4 in the API. */
116127
priority?: Priority

src/v8.test.js

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/v8.test.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { v8 as Todoist } from './index'
2+
import { config } from 'dotenv'
3+
import { NodeType } from './v8-types'
4+
import path from 'path'
5+
config({ path: path.basename(__dirname + '/.env') })
6+
7+
type ApiType = ReturnType<typeof Todoist>
8+
let api: ApiType
9+
10+
describe('initialization', () => {
11+
it('fails with invalid api key', () => {
12+
expect(() => {
13+
api = Todoist('invalid')
14+
}).toThrow()
15+
})
16+
17+
it('works', () => {
18+
api = Todoist(process.env.TODOIST_API_KEY as string)
19+
})
20+
21+
test('.sync() properly', async () => {
22+
await api.sync()
23+
})
24+
})
25+
26+
describe('.items CRUD', () => {
27+
let newItem: NodeType | undefined
28+
29+
test('.get() returns data', () => {
30+
const items = api.items.get()
31+
expect(items).toBeInstanceOf(Array)
32+
})
33+
34+
test('.add() works', async () => {
35+
newItem = await api.items.add({ content: 'testing-task' })
36+
expect(newItem).toMatchObject({ content: 'testing-task' })
37+
})
38+
39+
test('.delete() works', async () => {
40+
if (!newItem) return
41+
await api.items.delete({ id: newItem.id })
42+
const deletedItem = api.items.get().find((item) => item.id === newItem?.id)
43+
expect(deletedItem).toBeUndefined()
44+
})
45+
})
46+
47+
describe('.items due dates', () => {
48+
let item: NodeType | undefined
49+
50+
afterEach(async () => {
51+
if (item) {
52+
await api.items.delete({ id: item.id })
53+
}
54+
})
55+
56+
test('.add() with string due date', async () => {
57+
item = await api.items.add({ content: 'testing-task', due: { string: '17th of july 2021' } })
58+
expect(item).toMatchObject({
59+
content: 'testing-task',
60+
due: {
61+
date: '2021-07-17',
62+
is_recurring: false,
63+
lang: 'en',
64+
string: '17th of july 2021',
65+
timezone: null,
66+
},
67+
})
68+
})
69+
70+
test('.add() with due date', async () => {
71+
item = await api.items.add({
72+
content: 'testing-task',
73+
due: { date: '2018-10-14' },
74+
})
75+
console.log(item)
76+
expect(item).toMatchObject({
77+
content: 'testing-task',
78+
due: {
79+
date: '2018-10-14',
80+
is_recurring: false,
81+
lang: 'en',
82+
string: '2018-10-14',
83+
timezone: null,
84+
},
85+
})
86+
})
87+
})

src/v8.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,10 @@ export const Todoist = (token: string, userOptions = defaultOptions) => {
232232
>
233233
return state[stateKey].find((item) => item.id === newId)
234234
}
235-
const createCommand = <Args>(type: keyof TodoistResources, action: string) => async (args: Args) =>
236-
executeCommand(type, action, args)
235+
const createCommand =
236+
<Args>(type: keyof TodoistResources, action: string) =>
237+
async (args: Args) =>
238+
executeCommand(type, action, args)
237239

238240
const sync = async (resourceTypes = options.resourceTypes) => {
239241
const res = await request(
@@ -373,7 +375,9 @@ export const Todoist = (token: string, userOptions = defaultOptions) => {
373375

374376
const syncToken_ = {
375377
get: () => syncToken,
376-
set: (newToken: string) => { syncToken = newToken },
378+
set: (newToken: string) => {
379+
syncToken = newToken
380+
},
377381
}
378382

379383
const api = {

0 commit comments

Comments
 (0)