-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from rambler-digital-solutions/transport
feat(transport): add transport
- Loading branch information
Showing
8 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# API transport | ||
|
||
Enchanced API transport with timeout and query params | ||
|
||
## Install | ||
|
||
``` | ||
npm install -D @rambler-tech/transport | ||
``` | ||
|
||
or | ||
|
||
``` | ||
yarn add -D @rambler-tech/transport | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import {request} from '.' | ||
|
||
const fetchMock = jest.fn(() => | ||
Promise.resolve({ | ||
status: 200, | ||
json: () => Promise.resolve({data: 'baz'}) | ||
}) | ||
) as jest.Mock | ||
|
||
beforeEach(() => { | ||
jest.spyOn(global, 'fetch').mockImplementation(fetchMock) | ||
}) | ||
|
||
test('base request api', async () => { | ||
await request('/api', {}) | ||
|
||
expect(fetchMock).toHaveBeenCalledWith('/api', { | ||
method: 'get', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
} | ||
}) | ||
}) | ||
|
||
test('request api with query', async () => { | ||
await request('/api', { | ||
query: { | ||
foo: 'bar' | ||
} | ||
}) | ||
|
||
expect(fetchMock).toHaveBeenCalledWith('/api?foo=bar', { | ||
method: 'get', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
} | ||
}) | ||
}) | ||
|
||
test('request api with query', async () => { | ||
await request('/api', { | ||
method: 'post', | ||
body: { | ||
foo: 'bar' | ||
} | ||
}) | ||
|
||
expect(fetchMock).toHaveBeenCalledWith('/api', { | ||
method: 'post', | ||
body: '{"foo":"bar"}', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
} | ||
}) | ||
}) | ||
|
||
test('request api with timeout', async () => { | ||
await request('/api', { | ||
timeout: 1000 | ||
}) | ||
|
||
expect(fetchMock).toHaveBeenCalledWith('/api', { | ||
method: 'get', | ||
signal: expect.any(AbortSignal), | ||
headers: { | ||
'Content-Type': 'application/json' | ||
} | ||
}) | ||
}) | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* eslint-disable import/no-unused-modules */ | ||
|
||
const OK = 200 | ||
|
||
/** Request options */ | ||
export interface RequestOptions extends Omit<RequestInit, 'body'> { | ||
/** Request query params */ | ||
query?: Record<string, string> | ||
/** Request body */ | ||
body?: Record<string, any> | BodyInit | null | ||
/** Request timeout (ms) */ | ||
timeout?: number | ||
} | ||
|
||
/** Request */ | ||
export async function request<T>( | ||
url = '', | ||
requestOptions: RequestOptions = {} | ||
): Promise<T> { | ||
const { | ||
method = 'get', | ||
query = {}, | ||
body, | ||
timeout, | ||
headers, | ||
...options | ||
} = requestOptions | ||
|
||
const requestInit: RequestInit = { | ||
method, | ||
...options, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
...headers | ||
} | ||
} | ||
|
||
let requestUrl = url | ||
|
||
if (query) { | ||
const queryString = new URLSearchParams(query).toString() | ||
|
||
if (queryString) { | ||
requestUrl += `?${queryString}` | ||
} | ||
} | ||
|
||
if (body) { | ||
requestInit.body = | ||
body instanceof FormData || typeof body === 'string' | ||
? body | ||
: JSON.stringify(body) | ||
} | ||
|
||
if (timeout != null) { | ||
const controller = new AbortController() | ||
|
||
requestInit.signal = controller.signal | ||
window.setTimeout(() => controller.abort(), timeout) | ||
} | ||
|
||
const response = await fetch(requestUrl, requestInit) | ||
|
||
if (response.status !== OK) { | ||
throw new Error( | ||
`Failed to fetch: ${response.status} ${response.statusText}` | ||
) | ||
} | ||
|
||
return response.json() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "@rambler-tech/transport", | ||
"version": "0.0.0", | ||
"main": "dist", | ||
"module": "dist", | ||
"types": "dist/index.d.ts", | ||
"license": "MIT", | ||
"sideEffects": false, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../tsconfig.package.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../typedoc.package.json |