diff --git a/src/consts/endpoints.ts b/src/consts/endpoints.ts index b20e93b..57d9d77 100644 --- a/src/consts/endpoints.ts +++ b/src/consts/endpoints.ts @@ -1,6 +1,6 @@ const BASE_URI = 'https://api.todoist.com' const API_REST_BASE_URI = '/rest/v2/' -const API_SYNC_BASE_URI = '/sync/v9/' +export const API_SYNC_BASE_URI = '/sync/v9/' const TODOIST_URI = 'https://todoist.com' const API_AUTHORIZATION_BASE_URI = '/oauth/' diff --git a/src/restClient.test.ts b/src/restClient.test.ts index fb63deb..a4dd1be 100644 --- a/src/restClient.test.ts +++ b/src/restClient.test.ts @@ -5,6 +5,7 @@ import { TodoistRequestError } from './types/errors' import * as caseConverter from 'axios-case-converter' import { assertInstance } from './testUtils/asserts' import { DEFAULT_REQUEST_ID } from './testUtils/testDefaults' +import { API_SYNC_BASE_URI } from './consts/endpoints' const RANDOM_ID = 'SomethingRandom' @@ -193,6 +194,16 @@ describe('restClient', () => { }) }) + test('random request ID is not created if none provided for POST request on the sync endpoint', async () => { + const syncUrl = new URL(API_SYNC_BASE_URI, DEFAULT_BASE_URI).toString() + await request('POST', syncUrl, DEFAULT_ENDPOINT, DEFAULT_AUTH_TOKEN, DEFAULT_PAYLOAD) + + expect(axiosMock.create).toBeCalledWith({ + baseURL: syncUrl, + headers: { ...AUTHORIZATION_HEADERS }, + }) + }) + test('delete calls axios with expected endpoint', async () => { await request('DELETE', DEFAULT_BASE_URI, DEFAULT_ENDPOINT, DEFAULT_AUTH_TOKEN) diff --git a/src/restClient.ts b/src/restClient.ts index 9312508..e8928f1 100644 --- a/src/restClient.ts +++ b/src/restClient.ts @@ -5,6 +5,7 @@ import { TodoistRequestError } from './types/errors' import { HttpMethod } from './types/http' import { v4 as uuidv4 } from 'uuid' import axiosRetry from 'axios-retry' +import { API_SYNC_BASE_URI } from './consts/endpoints' export function paramsSerializer(params: Record) { const qs = new URLSearchParams() @@ -96,7 +97,8 @@ export async function request( const originalStack = new Error() try { - if (httpMethod === 'POST' && !requestId) { + // Sync api don't allow a request id in the CORS + if (httpMethod === 'POST' && !requestId && !baseUri.includes(API_SYNC_BASE_URI)) { requestId = uuidv4() }