forked from obytes/react-native-template-obytes
-
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 #6 from rootstrap/feature/add-interceptors
Feature: Add Axios interceptors
- Loading branch information
Showing
7 changed files
with
119 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { Env } from '@env'; | ||
import axios from 'axios'; | ||
|
||
export const client = axios.create({ | ||
baseURL: Env.API_URL, | ||
}); |
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,21 @@ | ||
import type { AxiosError, InternalAxiosRequestConfig } from 'axios'; | ||
|
||
import { client } from './client'; | ||
import { toCamelCase, toSnakeCase } from './utils'; | ||
|
||
export default function interceptors() { | ||
client.interceptors.request.use((config: InternalAxiosRequestConfig) => { | ||
config.data = toSnakeCase(config.data); | ||
return config; | ||
}); | ||
|
||
client.interceptors.response.use( | ||
(response) => { | ||
response.data = toCamelCase(response.data); | ||
return response; | ||
}, | ||
(error: AxiosError) => { | ||
return Promise.reject(error); | ||
} | ||
); | ||
} |
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,59 @@ | ||
import { toCamelCase, toSnakeCase } from './utils'; | ||
|
||
describe('utils', () => { | ||
describe('toCamelCase', () => { | ||
it('should convert snake_case to camelCase', () => { | ||
const obj = { | ||
foo_bar: 'foo', | ||
bar_baz: 'bar', | ||
}; | ||
const expected = { | ||
fooBar: 'foo', | ||
barBaz: 'bar', | ||
}; | ||
expect(toCamelCase(obj)).toEqual(expected); | ||
}); | ||
|
||
it('should convert to camelCase only snake_case keys', () => { | ||
const obj = { | ||
foo_bar: 'foo', | ||
bar_baz: 'bar', | ||
fooBar: 'foo', | ||
barBaz: 'bar', | ||
}; | ||
const expected = { | ||
fooBar: 'foo', | ||
barBaz: 'bar', | ||
}; | ||
expect(toCamelCase(obj)).toEqual(expected); | ||
}); | ||
}); | ||
|
||
describe('toSnakeCase', () => { | ||
it('should convert camelCase to snake_case', () => { | ||
const obj = { | ||
fooBar: 'foo', | ||
barBaz: 'bar', | ||
}; | ||
const expected = { | ||
foo_bar: 'foo', | ||
bar_baz: 'bar', | ||
}; | ||
expect(toSnakeCase(obj)).toEqual(expected); | ||
}); | ||
|
||
it('should convert to snake_case only camelCase keys', () => { | ||
const obj = { | ||
fooBar: 'foo', | ||
barBaz: 'bar', | ||
foo_bar: 'foo', | ||
bar_baz: 'bar', | ||
}; | ||
const expected = { | ||
foo_bar: 'foo', | ||
bar_baz: 'bar', | ||
}; | ||
expect(toSnakeCase(obj)).toEqual(expected); | ||
}); | ||
}); | ||
}); |
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
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