Skip to content

Commit

Permalink
feat(transport): add request factory
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepolischuk committed Sep 27, 2024
1 parent c051bf0 commit a2f6e59
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/transport/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {request} from '.'
import {request, createRequest} from '.'

const fetchMock = jest.fn(() =>
Promise.resolve({
Expand Down Expand Up @@ -68,6 +68,24 @@ test('request api with timeout', async () => {
})
})

test('create request api', async () => {
const request = createRequest('/api', {
headers: {
'Request-Id': '1234'
}
})

await request('/endpoint', {})

expect(fetchMock).toHaveBeenCalledWith('/api/endpoint', {
method: 'get',
headers: {
'Content-Type': 'application/json',
'Request-Id': '1234'
}
})
})

afterEach(() => {
jest.clearAllMocks()
})
12 changes: 12 additions & 0 deletions packages/transport/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,15 @@ export async function request<T>(

return response.json()
}

/** Request factory */
export function createRequest(
url = '',
options: RequestOptions = {}
): typeof request {
return <T>(endpoint = '', requestOptions: RequestOptions = {}) =>
request<T>(url + endpoint, {
...options,
...requestOptions
})
}

0 comments on commit a2f6e59

Please sign in to comment.