diff --git a/package-lock.json b/package-lock.json index e0c8632..d0ef608 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@doist/todoist-api-typescript", - "version": "1.6.0", + "version": "1.7.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@doist/todoist-api-typescript", - "version": "1.6.0", + "version": "1.7.0", "license": "MIT", "dependencies": { "axios": "^0.27.0", diff --git a/package.json b/package.json index 79f9f5d..a490018 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@doist/todoist-api-typescript", - "version": "1.6.0", + "version": "1.7.0", "description": "A typescript wrapper for the Todoist REST API.", "author": "Doist developers", "repository": "git@github.com:doist/todoist-api-typescript.git", diff --git a/src/TodoistApi.comments.test.ts b/src/TodoistApi.comments.test.ts index a6ac8f9..457af60 100644 --- a/src/TodoistApi.comments.test.ts +++ b/src/TodoistApi.comments.test.ts @@ -5,7 +5,7 @@ import { DEFAULT_REQUEST_ID, INVALID_ENTITY_ID, } from './testUtils/testDefaults' -import { API_REST_BASE_URI, ENDPOINT_REST_COMMENTS } from './consts/endpoints' +import { getRestBaseUri, ENDPOINT_REST_COMMENTS } from './consts/endpoints' import { setupRestClientMock } from './testUtils/mocks' import { assertInputValidationError } from './testUtils/asserts' @@ -25,7 +25,7 @@ describe('TodoistApi comment endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'GET', - API_REST_BASE_URI, + getRestBaseUri(), ENDPOINT_REST_COMMENTS, DEFAULT_AUTH_TOKEN, getCommentsArgs, @@ -54,7 +54,7 @@ describe('TodoistApi comment endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'GET', - API_REST_BASE_URI, + getRestBaseUri(), `${ENDPOINT_REST_COMMENTS}/${commentId}`, DEFAULT_AUTH_TOKEN, ) @@ -92,7 +92,7 @@ describe('TodoistApi comment endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'POST', - API_REST_BASE_URI, + getRestBaseUri(), ENDPOINT_REST_COMMENTS, DEFAULT_AUTH_TOKEN, addCommentArgs, @@ -126,7 +126,7 @@ describe('TodoistApi comment endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'POST', - API_REST_BASE_URI, + getRestBaseUri(), `${ENDPOINT_REST_COMMENTS}/${taskId}`, DEFAULT_AUTH_TOKEN, updateCommentArgs, @@ -161,7 +161,7 @@ describe('TodoistApi comment endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'DELETE', - API_REST_BASE_URI, + getRestBaseUri(), `${ENDPOINT_REST_COMMENTS}/${taskId}`, DEFAULT_AUTH_TOKEN, undefined, diff --git a/src/TodoistApi.labels.test.ts b/src/TodoistApi.labels.test.ts index 3a18f39..186abaa 100644 --- a/src/TodoistApi.labels.test.ts +++ b/src/TodoistApi.labels.test.ts @@ -5,7 +5,7 @@ import { DEFAULT_REQUEST_ID, INVALID_ENTITY_ID, } from './testUtils/testDefaults' -import { API_REST_BASE_URI, ENDPOINT_REST_LABELS } from './consts/endpoints' +import { getRestBaseUri, ENDPOINT_REST_LABELS } from './consts/endpoints' import { setupRestClientMock } from './testUtils/mocks' import { assertInputValidationError } from './testUtils/asserts' @@ -25,7 +25,7 @@ describe('TodoistApi label endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'GET', - API_REST_BASE_URI, + getRestBaseUri(), `${ENDPOINT_REST_LABELS}/${labelId}`, DEFAULT_AUTH_TOKEN, ) @@ -57,7 +57,7 @@ describe('TodoistApi label endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'GET', - API_REST_BASE_URI, + getRestBaseUri(), ENDPOINT_REST_LABELS, DEFAULT_AUTH_TOKEN, ) @@ -88,7 +88,7 @@ describe('TodoistApi label endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'POST', - API_REST_BASE_URI, + getRestBaseUri(), ENDPOINT_REST_LABELS, DEFAULT_AUTH_TOKEN, DEFAULT_ADD_LABEL_ARGS, @@ -121,7 +121,7 @@ describe('TodoistApi label endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'POST', - API_REST_BASE_URI, + getRestBaseUri(), `${ENDPOINT_REST_LABELS}/${labelId}`, DEFAULT_AUTH_TOKEN, DEFAULT_UPDATE_LABEL_ARGS, @@ -157,7 +157,7 @@ describe('TodoistApi label endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'DELETE', - API_REST_BASE_URI, + getRestBaseUri(), `${ENDPOINT_REST_LABELS}/${labelId}`, DEFAULT_AUTH_TOKEN, undefined, diff --git a/src/TodoistApi.projects.test.ts b/src/TodoistApi.projects.test.ts index 2e9d61b..82fd5b2 100644 --- a/src/TodoistApi.projects.test.ts +++ b/src/TodoistApi.projects.test.ts @@ -7,7 +7,7 @@ import { INVALID_ENTITY_ID, } from './testUtils/testDefaults' import { - API_REST_BASE_URI, + getRestBaseUri, ENDPOINT_REST_PROJECTS, ENDPOINT_REST_PROJECT_COLLABORATORS, } from './consts/endpoints' @@ -30,7 +30,7 @@ describe('TodoistApi project endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'GET', - API_REST_BASE_URI, + getRestBaseUri(), `${ENDPOINT_REST_PROJECTS}/${projectId}`, DEFAULT_AUTH_TOKEN, ) @@ -62,7 +62,7 @@ describe('TodoistApi project endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'GET', - API_REST_BASE_URI, + getRestBaseUri(), ENDPOINT_REST_PROJECTS, DEFAULT_AUTH_TOKEN, ) @@ -93,7 +93,7 @@ describe('TodoistApi project endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'POST', - API_REST_BASE_URI, + getRestBaseUri(), ENDPOINT_REST_PROJECTS, DEFAULT_AUTH_TOKEN, DEFAULT_ADD_PROJECT_ARGS, @@ -123,7 +123,7 @@ describe('TodoistApi project endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'POST', - API_REST_BASE_URI, + getRestBaseUri(), `${ENDPOINT_REST_PROJECTS}/${projectId}`, DEFAULT_AUTH_TOKEN, updateArgs, @@ -158,7 +158,7 @@ describe('TodoistApi project endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'DELETE', - API_REST_BASE_URI, + getRestBaseUri(), `${ENDPOINT_REST_PROJECTS}/${projectId}`, DEFAULT_AUTH_TOKEN, DEFAULT_REQUEST_ID, @@ -194,7 +194,7 @@ describe('TodoistApi project endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'GET', - API_REST_BASE_URI, + getRestBaseUri(), `${ENDPOINT_REST_PROJECTS}/${projectId}/${ENDPOINT_REST_PROJECT_COLLABORATORS}`, DEFAULT_AUTH_TOKEN, ) diff --git a/src/TodoistApi.sections.test.ts b/src/TodoistApi.sections.test.ts index 95d4a42..771c607 100644 --- a/src/TodoistApi.sections.test.ts +++ b/src/TodoistApi.sections.test.ts @@ -5,7 +5,7 @@ import { DEFAULT_SECTION, INVALID_ENTITY_ID, } from './testUtils/testDefaults' -import { API_REST_BASE_URI, ENDPOINT_REST_SECTIONS } from './consts/endpoints' +import { getRestBaseUri, ENDPOINT_REST_SECTIONS } from './consts/endpoints' import { setupRestClientMock } from './testUtils/mocks' import { assertInputValidationError } from './testUtils/asserts' @@ -25,7 +25,7 @@ describe('TodoistApi section endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'GET', - API_REST_BASE_URI, + getRestBaseUri(), `${ENDPOINT_REST_SECTIONS}/${sectionId}`, DEFAULT_AUTH_TOKEN, ) @@ -58,7 +58,7 @@ describe('TodoistApi section endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'GET', - API_REST_BASE_URI, + getRestBaseUri(), ENDPOINT_REST_SECTIONS, DEFAULT_AUTH_TOKEN, { projectId }, @@ -91,7 +91,7 @@ describe('TodoistApi section endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'POST', - API_REST_BASE_URI, + getRestBaseUri(), ENDPOINT_REST_SECTIONS, DEFAULT_AUTH_TOKEN, DEFAULT_ADD_SECTION_ARGS, @@ -121,7 +121,7 @@ describe('TodoistApi section endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'POST', - API_REST_BASE_URI, + getRestBaseUri(), `${ENDPOINT_REST_SECTIONS}/${sectionId}`, DEFAULT_AUTH_TOKEN, updateArgs, @@ -157,7 +157,7 @@ describe('TodoistApi section endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'DELETE', - API_REST_BASE_URI, + getRestBaseUri(), `${ENDPOINT_REST_SECTIONS}/${sectionId}`, DEFAULT_AUTH_TOKEN, undefined, diff --git a/src/TodoistApi.tasks.test.ts b/src/TodoistApi.tasks.test.ts index e1ccd87..cd0e23c 100644 --- a/src/TodoistApi.tasks.test.ts +++ b/src/TodoistApi.tasks.test.ts @@ -9,8 +9,8 @@ import { INVALID_ENTITY_ID, } from './testUtils/testDefaults' import { - API_REST_BASE_URI, - API_SYNC_BASE_URI, + getRestBaseUri, + getSyncBaseUri, ENDPOINT_REST_TASK_CLOSE, ENDPOINT_REST_TASK_REOPEN, ENDPOINT_REST_TASKS, @@ -23,8 +23,8 @@ function setupSyncTaskConverter(returnedTask: Task) { return jest.spyOn(taskConverters, 'getTaskFromQuickAddResponse').mockReturnValue(returnedTask) } -function getTarget() { - return new TodoistApi(DEFAULT_AUTH_TOKEN) +function getTarget(baseUrl = 'https://api.todoist.com') { + return new TodoistApi(DEFAULT_AUTH_TOKEN, baseUrl) } describe('TodoistApi task endpoints', () => { @@ -42,7 +42,24 @@ describe('TodoistApi task endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'POST', - API_REST_BASE_URI, + getRestBaseUri(), + ENDPOINT_REST_TASKS, + DEFAULT_AUTH_TOKEN, + DEFAULT_ADD_TASK_ARGS, + DEFAULT_REQUEST_ID, + ) + }) + + test('calls post on restClient with expected parameters against staging', async () => { + const requestMock = setupRestClientMock(DEFAULT_TASK) + const api = getTarget('https://staging.todoist.com') + + await api.addTask(DEFAULT_ADD_TASK_ARGS, DEFAULT_REQUEST_ID) + + expect(requestMock).toBeCalledTimes(1) + expect(requestMock).toBeCalledWith( + 'POST', + getRestBaseUri('https://staging.todoist.com'), ENDPOINT_REST_TASKS, DEFAULT_AUTH_TOKEN, DEFAULT_ADD_TASK_ARGS, @@ -72,7 +89,7 @@ describe('TodoistApi task endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'POST', - API_REST_BASE_URI, + getRestBaseUri(), `${ENDPOINT_REST_TASKS}/${taskId}`, DEFAULT_AUTH_TOKEN, updateArgs, @@ -108,7 +125,7 @@ describe('TodoistApi task endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'POST', - API_REST_BASE_URI, + getRestBaseUri(), `${ENDPOINT_REST_TASKS}/${taskId}/${ENDPOINT_REST_TASK_CLOSE}`, DEFAULT_AUTH_TOKEN, undefined, @@ -143,7 +160,7 @@ describe('TodoistApi task endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'POST', - API_REST_BASE_URI, + getRestBaseUri(), `${ENDPOINT_REST_TASKS}/${taskId}/${ENDPOINT_REST_TASK_REOPEN}`, DEFAULT_AUTH_TOKEN, undefined, @@ -178,7 +195,7 @@ describe('TodoistApi task endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'DELETE', - API_REST_BASE_URI, + getRestBaseUri(), `${ENDPOINT_REST_TASKS}/${taskId}`, DEFAULT_AUTH_TOKEN, undefined, @@ -219,7 +236,7 @@ describe('TodoistApi task endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'POST', - API_SYNC_BASE_URI, + getSyncBaseUri(), ENDPOINT_SYNC_QUICK_ADD, DEFAULT_AUTH_TOKEN, DEFAULT_QUICK_ADD_ARGS, @@ -250,7 +267,7 @@ describe('TodoistApi task endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'GET', - API_REST_BASE_URI, + getRestBaseUri(), `${ENDPOINT_REST_TASKS}/${taskId}`, DEFAULT_AUTH_TOKEN, ) @@ -277,7 +294,7 @@ describe('TodoistApi task endpoints', () => { expect(requestMock).toBeCalledTimes(1) expect(requestMock).toBeCalledWith( 'GET', - API_REST_BASE_URI, + getRestBaseUri(), ENDPOINT_REST_TASKS, DEFAULT_AUTH_TOKEN, DEFAULT_GET_TASKS_ARGS, diff --git a/src/TodoistApi.ts b/src/TodoistApi.ts index 944af95..8b2d1a7 100644 --- a/src/TodoistApi.ts +++ b/src/TodoistApi.ts @@ -28,8 +28,8 @@ import { import { request, isSuccess } from './restClient' import { getTaskFromQuickAddResponse } from './utils/taskConverters' import { - API_REST_BASE_URI, - API_SYNC_BASE_URI, + getRestBaseUri, + getSyncBaseUri, ENDPOINT_REST_TASKS, ENDPOINT_REST_PROJECTS, ENDPOINT_SYNC_QUICK_ADD, @@ -66,15 +66,21 @@ function generatePath(...segments: string[]): string { export class TodoistApi { authToken: string - constructor(authToken: string) { + constructor(authToken: string, baseUrl?: string) { this.authToken = authToken + + this.restApiBase = getRestBaseUri(baseUrl) + this.syncApiBase = getSyncBaseUri(baseUrl) } + private restApiBase: string + private syncApiBase: string + async getTask(id: number): Promise { Int.check(id) const response = await request( 'GET', - API_REST_BASE_URI, + this.restApiBase, generatePath(ENDPOINT_REST_TASKS, String(id)), this.authToken, ) @@ -85,7 +91,7 @@ export class TodoistApi { async getTasks(args?: GetTasksArgs): Promise { const response = await request( 'GET', - API_REST_BASE_URI, + this.restApiBase, ENDPOINT_REST_TASKS, this.authToken, args, @@ -97,7 +103,7 @@ export class TodoistApi { async addTask(args: AddTaskArgs, requestId?: string): Promise { const response = await request( 'POST', - API_REST_BASE_URI, + this.restApiBase, ENDPOINT_REST_TASKS, this.authToken, args, @@ -110,7 +116,7 @@ export class TodoistApi { async quickAddTask(args: QuickAddTaskArgs): Promise { const response = await request( 'POST', - API_SYNC_BASE_URI, + this.syncApiBase, ENDPOINT_SYNC_QUICK_ADD, this.authToken, args, @@ -125,7 +131,7 @@ export class TodoistApi { Int.check(id) const response = await request( 'POST', - API_REST_BASE_URI, + this.restApiBase, generatePath(ENDPOINT_REST_TASKS, String(id)), this.authToken, args, @@ -138,7 +144,7 @@ export class TodoistApi { Int.check(id) const response = await request( 'POST', - API_REST_BASE_URI, + this.restApiBase, generatePath(ENDPOINT_REST_TASKS, String(id), ENDPOINT_REST_TASK_CLOSE), this.authToken, undefined, @@ -151,7 +157,7 @@ export class TodoistApi { Int.check(id) const response = await request( 'POST', - API_REST_BASE_URI, + this.restApiBase, generatePath(ENDPOINT_REST_TASKS, String(id), ENDPOINT_REST_TASK_REOPEN), this.authToken, undefined, @@ -164,7 +170,7 @@ export class TodoistApi { Int.check(id) const response = await request( 'DELETE', - API_REST_BASE_URI, + this.restApiBase, generatePath(ENDPOINT_REST_TASKS, String(id)), this.authToken, undefined, @@ -177,7 +183,7 @@ export class TodoistApi { Int.check(id) const response = await request( 'GET', - API_REST_BASE_URI, + this.restApiBase, generatePath(ENDPOINT_REST_PROJECTS, String(id)), this.authToken, ) @@ -188,7 +194,7 @@ export class TodoistApi { async getProjects(): Promise { const response = await request( 'GET', - API_REST_BASE_URI, + this.restApiBase, ENDPOINT_REST_PROJECTS, this.authToken, ) @@ -199,7 +205,7 @@ export class TodoistApi { async addProject(args: AddProjectArgs, requestId?: string): Promise { const response = await request( 'POST', - API_REST_BASE_URI, + this.restApiBase, ENDPOINT_REST_PROJECTS, this.authToken, args, @@ -213,7 +219,7 @@ export class TodoistApi { Int.check(id) const response = await request( 'POST', - API_REST_BASE_URI, + this.restApiBase, generatePath(ENDPOINT_REST_PROJECTS, String(id)), this.authToken, args, @@ -226,7 +232,7 @@ export class TodoistApi { Int.check(id) const response = await request( 'DELETE', - API_REST_BASE_URI, + this.restApiBase, generatePath(ENDPOINT_REST_PROJECTS, String(id)), this.authToken, requestId, @@ -238,7 +244,7 @@ export class TodoistApi { Int.check(projectId) const response = await request( 'GET', - API_REST_BASE_URI, + this.restApiBase, generatePath( ENDPOINT_REST_PROJECTS, String(projectId), @@ -253,7 +259,7 @@ export class TodoistApi { async getSections(projectId?: number): Promise { const response = await request( 'GET', - API_REST_BASE_URI, + this.restApiBase, ENDPOINT_REST_SECTIONS, this.authToken, projectId && { projectId }, @@ -266,7 +272,7 @@ export class TodoistApi { Int.check(id) const response = await request
( 'GET', - API_REST_BASE_URI, + this.restApiBase, generatePath(ENDPOINT_REST_SECTIONS, String(id)), this.authToken, ) @@ -277,7 +283,7 @@ export class TodoistApi { async addSection(args: AddSectionArgs, requestId?: string): Promise
{ const response = await request
( 'POST', - API_REST_BASE_URI, + this.restApiBase, ENDPOINT_REST_SECTIONS, this.authToken, args, @@ -291,7 +297,7 @@ export class TodoistApi { Int.check(id) const response = await request( 'POST', - API_REST_BASE_URI, + this.restApiBase, generatePath(ENDPOINT_REST_SECTIONS, String(id)), this.authToken, args, @@ -304,7 +310,7 @@ export class TodoistApi { Int.check(id) const response = await request( 'DELETE', - API_REST_BASE_URI, + this.restApiBase, generatePath(ENDPOINT_REST_SECTIONS, String(id)), this.authToken, undefined, @@ -317,7 +323,7 @@ export class TodoistApi { Int.check(id) const response = await request