Skip to content

Commit

Permalink
fix: remove request id when calling the sync api
Browse files Browse the repository at this point in the history
  • Loading branch information
beaussan committed Apr 15, 2024
1 parent 6c9c0e7 commit fe6749c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/consts/endpoints.ts
Original file line number Diff line number Diff line change
@@ -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/'

Expand Down
11 changes: 11 additions & 0 deletions src/restClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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)

Expand Down
4 changes: 3 additions & 1 deletion src/restClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>) {
const qs = new URLSearchParams()
Expand Down Expand Up @@ -96,7 +97,8 @@ export async function request<T>(
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()
}

Expand Down

0 comments on commit fe6749c

Please sign in to comment.