Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove request id when calling the sync api #258

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading