Skip to content

Commit

Permalink
chore: move complex response types to own exported types (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroalves0 authored Jan 22, 2025
1 parent 1211ded commit fc59a9c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 43 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@doist/todoist-api-typescript",
"version": "4.0.0-alpha.1",
"version": "4.0.0-alpha.2",
"description": "A typescript wrapper for the Todoist REST API.",
"author": "Doist developers",
"repository": "[email protected]:doist/todoist-api-typescript.git",
Expand Down
67 changes: 29 additions & 38 deletions src/TodoistApi.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import { String } from 'runtypes'
import {
Task,
QuickAddTaskResponse,
Project,
Label,
User,
Section,
Comment,
} from './types/entities'
import { Task, QuickAddTaskResponse, Project, Label, Section, Comment } from './types/entities'
import {
AddCommentArgs,
AddLabelArgs,
Expand All @@ -28,8 +20,15 @@ import {
RemoveSharedLabelArgs,
GetProjectsArgs,
GetProjectCollaboratorsArgs,
GetSections,
GetLabelsArgs,
GetLabelsResponse,
GetTasksResponse,
GetProjectsResponse,
GetProjectCollaboratorsResponse,
GetSectionsArgs,
GetSectionsResponse,
GetSharedLabelsResponse,
GetCommentsResponse,
} from './types/requests'
import { request, isSuccess } from './restClient'
import { getTaskFromQuickAddResponse } from './utils/taskConverters'
Expand Down Expand Up @@ -93,16 +92,16 @@ export class TodoistApi {
return validateTask(response.data)
}

async getTasks(args: GetTasksArgs = {}): Promise<{
results: Task[]
nextCursor: string | null
}> {
async getTasks(args: GetTasksArgs = {}): Promise<GetTasksResponse> {
const {
data: { results, nextCursor },
} = await request<{
results: Task[]
nextCursor: string | null
}>('GET', this.syncApiBase, ENDPOINT_REST_TASKS, this.authToken, args)
} = await request<GetTasksResponse>(
'GET',
this.syncApiBase,
ENDPOINT_REST_TASKS,
this.authToken,
args,
)

return {
results: validateTaskArray(results),
Expand Down Expand Up @@ -201,12 +200,10 @@ export class TodoistApi {
return validateProject(response.data)
}

async getProjects(
args: GetProjectsArgs = {},
): Promise<{ results: Project[]; nextCursor: string | null }> {
async getProjects(args: GetProjectsArgs = {}): Promise<GetProjectsResponse> {
const {
data: { results, nextCursor },
} = await request<{ results: Project[]; nextCursor: string | null }>(
} = await request<GetProjectsResponse>(
'GET',
this.syncApiBase,
ENDPOINT_REST_PROJECTS,
Expand Down Expand Up @@ -262,11 +259,11 @@ export class TodoistApi {
async getProjectCollaborators(
projectId: string,
args: GetProjectCollaboratorsArgs = {},
): Promise<{ results: User[]; nextCursor: string | null }> {
): Promise<GetProjectCollaboratorsResponse> {
String.check(projectId)
const {
data: { results, nextCursor },
} = await request<{ results: User[]; nextCursor: string | null }>(
} = await request<GetProjectCollaboratorsResponse>(
'GET',
this.syncApiBase,
generatePath(ENDPOINT_REST_PROJECTS, projectId, ENDPOINT_REST_PROJECT_COLLABORATORS),
Expand All @@ -280,12 +277,10 @@ export class TodoistApi {
}
}

async getSections(
args: GetSections,
): Promise<{ results: Section[]; nextCursor: string | null }> {
async getSections(args: GetSectionsArgs): Promise<GetSectionsResponse> {
const {
data: { results, nextCursor },
} = await request<{ results: Section[]; nextCursor: string | null }>(
} = await request<GetSectionsResponse>(
'GET',
this.syncApiBase,
ENDPOINT_REST_SECTIONS,
Expand Down Expand Up @@ -368,12 +363,10 @@ export class TodoistApi {
/**
* Fetches the personal labels
*/
async getLabels(
args: GetLabelsArgs = {},
): Promise<{ results: Label[]; nextCursor: string | null }> {
async getLabels(args: GetLabelsArgs = {}): Promise<GetLabelsResponse> {
const {
data: { results, nextCursor: nextCursor },
} = await request<{ results: Label[]; nextCursor: string | null }>(
} = await request<GetLabelsResponse>(
'GET',
this.syncApiBase,
ENDPOINT_REST_LABELS,
Expand Down Expand Up @@ -435,12 +428,10 @@ export class TodoistApi {
return isSuccess(response)
}

async getSharedLabels(
args?: GetSharedLabelsArgs,
): Promise<{ results: string[]; nextCursor: string | null }> {
async getSharedLabels(args?: GetSharedLabelsArgs): Promise<GetSharedLabelsResponse> {
const {
data: { results, nextCursor: nextCursor },
} = await request<{ results: string[]; nextCursor: string | null }>(
} = await request<GetSharedLabelsResponse>(
'GET',
this.syncApiBase,
ENDPOINT_REST_LABELS_SHARED,
Expand Down Expand Up @@ -477,10 +468,10 @@ export class TodoistApi {

async getComments(
args: GetTaskCommentsArgs | GetProjectCommentsArgs,
): Promise<{ results: Comment[]; nextCursor: string | null }> {
): Promise<GetCommentsResponse> {
const {
data: { results, nextCursor },
} = await request<{ results: Comment[]; nextCursor: string | null }>(
} = await request<GetCommentsResponse>(
'GET',
this.syncApiBase,
ENDPOINT_REST_COMMENTS,
Expand Down
41 changes: 39 additions & 2 deletions src/types/requests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import type { RequireAllOrNone, RequireOneOrNone, RequireExactlyOne } from 'type-fest'
import type { Duration, ProjectViewStyle } from './entities'
import type {
Comment,
Duration,
Label,
Project,
ProjectViewStyle,
Section,
Task,
User,
} from './entities'

export type AddTaskArgs = {
content: string
Expand Down Expand Up @@ -42,6 +51,10 @@ export type GetTasksArgs = {
cursor?: string | null
limit?: number
}
export type GetTasksResponse = {
results: Task[]
nextCursor: string | null
}

export type UpdateTaskArgs = {
content?: string
Expand All @@ -66,6 +79,10 @@ export type GetProjectsArgs = {
cursor?: string | null
limit?: number
}
export type GetProjectsResponse = {
results: Project[]
nextCursor: string | null
}

export type AddProjectArgs = {
name: string
Expand All @@ -86,12 +103,20 @@ export type GetProjectCollaboratorsArgs = {
cursor?: string | null
limit?: number
}
export type GetProjectCollaboratorsResponse = {
results: User[]
nextCursor: string | null
}

export type GetSections = {
export type GetSectionsArgs = {
projectId: string | null
cursor?: string | null
limit?: number
}
export type GetSectionsResponse = {
results: Section[]
nextCursor: string | null
}

export type AddSectionArgs = {
name: string
Expand All @@ -107,6 +132,10 @@ export type GetLabelsArgs = {
cursor?: string | null
limit?: number
}
export type GetLabelsResponse = {
results: Label[]
nextCursor: string | null
}

export type AddLabelArgs = {
name: string
Expand All @@ -126,6 +155,10 @@ export type GetCommentsBaseArgs = {
cursor?: string | null
limit?: number
}
export type GetCommentsResponse = {
results: Comment[]
nextCursor: string | null
}

export type GetTaskCommentsArgs = GetCommentsBaseArgs & {
taskId: string
Expand Down Expand Up @@ -159,6 +192,10 @@ export type GetSharedLabelsArgs = {
cursor?: string | null
limit?: number
}
export type GetSharedLabelsResponse = {
results: string[]
nextCursor: string | null
}

export type RenameSharedLabelArgs = {
name: string
Expand Down

0 comments on commit fc59a9c

Please sign in to comment.