Skip to content
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
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": "2.0.2",
"version": "2.0.3",
"description": "A typescript wrapper for the Todoist REST API.",
"author": "Doist developers",
"repository": "[email protected]:doist/todoist-api-typescript.git",
Expand Down
1 change: 1 addition & 0 deletions src/TodoistApi.projects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ describe('TodoistApi project endpoints', () => {
getRestBaseUri(),
`${ENDPOINT_REST_PROJECTS}/${projectId}`,
DEFAULT_AUTH_TOKEN,
undefined,
DEFAULT_REQUEST_ID,
)
})
Expand Down
3 changes: 2 additions & 1 deletion src/TodoistApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ export class TodoistApi {
this.restApiBase,
generatePath(ENDPOINT_REST_PROJECTS, id),
this.authToken,
undefined,
requestId,
)
return isSuccess(response)
Expand All @@ -263,7 +264,7 @@ export class TodoistApi {
this.restApiBase,
ENDPOINT_REST_SECTIONS,
this.authToken,
projectId && { projectId },
projectId ? { projectId } : undefined,
)

return validateSectionArray(response.data)
Expand Down
18 changes: 18 additions & 0 deletions src/restClient.axios.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import axios from 'axios'
import { paramsSerializer } from './restClient'

const DEFAULT_BASE_URI = 'https://api.todoist.com/rest/v2/tasks'

describe('axios tests without mocking', () => {
test('GET calls serialise arrays correctly', () => {
const requestUri = axios.create().getUri({
method: 'GET',
baseURL: DEFAULT_BASE_URI,
params: {
ids: ['12345', '56789'],
},
paramsSerializer,
})
expect(requestUri).toEqual('https://api.todoist.com/rest/v2/tasks?ids=12345%2C56789')
})
})
4 changes: 3 additions & 1 deletion src/restClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Axios, { AxiosStatic, AxiosResponse, AxiosError } from 'axios'
import { request, isSuccess } from './restClient'
import { request, isSuccess, paramsSerializer } from './restClient'
import { TodoistRequestError } from './types/errors'
import * as caseConverter from 'axios-case-converter'
import { assertInstance } from './testUtils/asserts'
Expand Down Expand Up @@ -120,6 +120,7 @@ describe('restClient', () => {
expect(axiosMock.get).toBeCalledTimes(1)
expect(axiosMock.get).toBeCalledWith(DEFAULT_ENDPOINT, {
params: undefined,
paramsSerializer,
})
})

Expand All @@ -135,6 +136,7 @@ describe('restClient', () => {
expect(axiosMock.get).toBeCalledTimes(1)
expect(axiosMock.get).toBeCalledWith(DEFAULT_ENDPOINT, {
params: DEFAULT_PAYLOAD,
paramsSerializer,
})
})

Expand Down
22 changes: 20 additions & 2 deletions src/restClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ import { HttpMethod } from './types/http'
import { v4 as uuidv4 } from 'uuid'
import axiosRetry from 'axios-retry'

export function paramsSerializer(params: Record<string, unknown>) {
const qs = new URLSearchParams()

Object.keys(params).forEach((key) => {
const value = params[key]
if (Array.isArray(value)) {
qs.append(key, value.join(','))
} else {
qs.append(key, String(value))
}
})

return qs.toString()
}

const defaultHeaders = {
'Content-Type': 'application/json',
}
Expand Down Expand Up @@ -71,7 +86,7 @@ export async function request<T>(
baseUri: string,
relativePath: string,
apiToken?: string,
payload?: unknown,
payload?: Record<string, unknown>,
requestId?: string,
): Promise<AxiosResponse<T>> {
// axios loses the original stack when returning errors, for the sake of better reporting
Expand All @@ -88,7 +103,10 @@ export async function request<T>(

switch (httpMethod) {
case 'GET':
return await axiosClient.get<T>(relativePath, { params: payload })
return await axiosClient.get<T>(relativePath, {
params: payload,
paramsSerializer,
})
case 'POST':
return await axiosClient.post<T>(relativePath, payload)
case 'DELETE':
Expand Down