Skip to content

Commit

Permalink
Add axios and configure api requests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdlli committed Mar 25, 2022
1 parent e0e860e commit 1e649f0
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^12.0.0",
"@testing-library/user-event": "^13.2.1",
"axios": "^0.26.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "^6.2.2",
Expand Down
23 changes: 23 additions & 0 deletions src/axios/factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import axios from 'axios';
import {
requestInterceptor,
responseSuccessInterceptor,
responseErrorInterceptor,
} from './interceptors';

function Factory(baseURL) {
const instance = axios.create({
baseURL,
});

instance.interceptors.request.use(requestInterceptor);

instance.interceptors.response.use(
responseSuccessInterceptor,
responseErrorInterceptor,
);

return instance;
}

export default Factory;
6 changes: 6 additions & 0 deletions src/axios/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Factory from './factory';

const coreApiUrl = 'https://api.themoviedb.org/3';
const commonInstance = Factory(coreApiUrl);

export default commonInstance;
14 changes: 14 additions & 0 deletions src/axios/interceptos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const requestInterceptor = config => config;

const responseSuccessInterceptor = response => response.data;

const responseErrorInterceptor = (error) => {
const response = error.response || error;
return Promise.reject(response);
};

export {
requestInterceptor,
responseSuccessInterceptor,
responseErrorInterceptor,
};
8 changes: 8 additions & 0 deletions src/services/apifilmes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import axios from '@/axios';

export const getMovies = ({ page = 1 }) => axios.get(`/movie/popular?api_key=${process.env.REACT_APP_API_MOVIE_DB}&language=pt-BR&page=${page}`);

export const getMovieSimilarly = ({ page = 1, id }) => axios.get(`/movie/${id}/similar?api_key=${process.env.REACT_APP_API_MOVIE_DB}&language=pt-BR&page=${page}`);

export const getMovieDetails = id => axios.get(`/movie/${id})movie/${id}?api_key=${process.env.REACT_APP_API_MOVIE_DB}&language=pt-BR&page=1`);

9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2472,6 +2472,13 @@ axe-core@^4.3.5:
resolved "https://registry.yarnpkg.com/axe-core/-/axe-core-4.4.1.tgz#7dbdc25989298f9ad006645cd396782443757413"
integrity sha512-gd1kmb21kwNuWr6BQz8fv6GNECPBnUasepcoLbekws23NVBLODdsClRZ+bQ8+9Uomf3Sm3+Vwn0oYG9NvwnJCw==

axios@^0.26.1:
version "0.26.1"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.26.1.tgz#1ede41c51fcf51bbbd6fd43669caaa4f0495aaa9"
integrity sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==
dependencies:
follow-redirects "^1.14.8"

axobject-query@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/axobject-query/-/axobject-query-2.2.0.tgz#943d47e10c0b704aa42275e20edf3722648989be"
Expand Down Expand Up @@ -4291,7 +4298,7 @@ flatted@^3.1.0:
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.5.tgz#76c8584f4fc843db64702a6bd04ab7a8bd666da3"
integrity sha512-WIWGi2L3DyTUvUrwRKgGi9TwxQMUEqPOPQBVi71R96jZXJdFskXEmf54BoZaS1kknGODoIGASGEzBUYdyMCBJg==

follow-redirects@^1.0.0:
follow-redirects@^1.0.0, follow-redirects@^1.14.8:
version "1.14.9"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.9.tgz#dd4ea157de7bfaf9ea9b3fbd85aa16951f78d8d7"
integrity sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==
Expand Down

1 comment on commit 1e649f0

@vercel
Copy link

@vercel vercel bot commented on 1e649f0 Mar 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.