├── .gitignore
├── .idea
├── .gitignore
├── api-layer-example-semaphore.iml
├── inspectionProfiles
│ └── Project_Default.xml
├── misc.xml
├── modules.xml
└── vcs.xml
├── README.md
├── package-lock.json
├── package.json
├── public
├── favicon.ico
├── index.html
├── logo192.png
├── logo512.png
├── manifest.json
└── robots.txt
└── src
├── App.css
├── App.js
├── App.test.js
├── apis
├── PokemonAPI.js
└── configs
│ ├── axiosConfigs.js
│ └── axiosUtils.js
├── index.css
├── index.js
├── logo.svg
├── reportWebVitals.js
└── setupTests.js
/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 | /.pnp
6 | .pnp.js
7 |
8 | # testing
9 | /coverage
10 |
11 | # production
12 | /build
13 |
14 | # misc
15 | .DS_Store
16 | .env.local
17 | .env.development.local
18 | .env.test.local
19 | .env.production.local
20 |
21 | npm-debug.log*
22 | yarn-debug.log*
23 | yarn-error.log*
24 |
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 | # Editor-based HTTP Client requests
5 | /httpRequests/
6 | # Datasource local storage ignored files
7 | /dataSources/
8 | /dataSources.local.xml
9 |
--------------------------------------------------------------------------------
/.idea/api-layer-example-semaphore.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/inspectionProfiles/Project_Default.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Getting Started with Create React App
2 |
3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4 |
5 | ## Available Scripts
6 |
7 | In the project directory, you can run:
8 |
9 | ### `npm start`
10 |
11 | Runs the app in the development mode.\
12 | Open [http://localhost:3000](http://localhost:3000) to view it in your browser.
13 |
14 | The page will reload when you make changes.\
15 | You may also see any lint errors in the console.
16 |
17 | ### `npm test`
18 |
19 | Launches the test runner in the interactive watch mode.\
20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21 |
22 | ### `npm run build`
23 |
24 | Builds the app for production to the `build` folder.\
25 | It correctly bundles React in production mode and optimizes the build for the best performance.
26 |
27 | The build is minified and the filenames include the hashes.\
28 | Your app is ready to be deployed!
29 |
30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31 |
32 | ### `npm run eject`
33 |
34 | **Note: this is a one-way operation. Once you `eject`, you can't go back!**
35 |
36 | If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
37 |
38 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.
39 |
40 | You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.
41 |
42 | ## Learn More
43 |
44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45 |
46 | To learn React, check out the [React documentation](https://reactjs.org/).
47 |
48 | ### Code Splitting
49 |
50 | This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
51 |
52 | ### Analyzing the Bundle Size
53 |
54 | This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
55 |
56 | ### Making a Progressive Web App
57 |
58 | This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
59 |
60 | ### Advanced Configuration
61 |
62 | This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
63 |
64 | ### Deployment
65 |
66 | This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
67 |
68 | ### `npm run build` fails to minify
69 |
70 | This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
71 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "api-layer-example-semaphore",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "@testing-library/jest-dom": "^5.16.4",
7 | "@testing-library/react": "^13.3.0",
8 | "@testing-library/user-event": "^13.5.0",
9 | "antd": "^4.21.7",
10 | "axios": "^0.27.2",
11 | "react": "^18.2.0",
12 | "react-dom": "^18.2.0",
13 | "react-scripts": "5.0.1",
14 | "web-vitals": "^2.1.4"
15 | },
16 | "scripts": {
17 | "start": "react-scripts start",
18 | "build": "react-scripts build",
19 | "test": "react-scripts test",
20 | "eject": "react-scripts eject"
21 | },
22 | "eslintConfig": {
23 | "extends": [
24 | "react-app",
25 | "react-app/jest"
26 | ]
27 | },
28 | "browserslist": {
29 | "production": [
30 | ">0.2%",
31 | "not dead",
32 | "not op_mini all"
33 | ],
34 | "development": [
35 | "last 1 chrome version",
36 | "last 1 firefox version",
37 | "last 1 safari version"
38 | ]
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Tonel/api-layer-example-semaphore/b6fe246493bb6eedee092be3265175f6975ea46e/public/favicon.ico
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
17 |
18 |
27 | React App
28 |
29 |
30 |
31 |
32 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/public/logo192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Tonel/api-layer-example-semaphore/b6fe246493bb6eedee092be3265175f6975ea46e/public/logo192.png
--------------------------------------------------------------------------------
/public/logo512.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Tonel/api-layer-example-semaphore/b6fe246493bb6eedee092be3265175f6975ea46e/public/logo512.png
--------------------------------------------------------------------------------
/public/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "short_name": "React App",
3 | "name": "Create React App Sample",
4 | "icons": [
5 | {
6 | "src": "favicon.ico",
7 | "sizes": "64x64 32x32 24x24 16x16",
8 | "type": "image/x-icon"
9 | },
10 | {
11 | "src": "logo192.png",
12 | "type": "image/png",
13 | "sizes": "192x192"
14 | },
15 | {
16 | "src": "logo512.png",
17 | "type": "image/png",
18 | "sizes": "512x512"
19 | }
20 | ],
21 | "start_url": ".",
22 | "display": "standalone",
23 | "theme_color": "#000000",
24 | "background_color": "#ffffff"
25 | }
26 |
--------------------------------------------------------------------------------
/public/robots.txt:
--------------------------------------------------------------------------------
1 | # https://www.robotstxt.org/robotstxt.html
2 | User-agent: *
3 | Disallow:
4 |
--------------------------------------------------------------------------------
/src/App.css:
--------------------------------------------------------------------------------
1 | .App {
2 | text-align: center;
3 | }
4 |
5 | .App-logo {
6 | height: 40vmin;
7 | pointer-events: none;
8 | }
9 |
10 | @media (prefers-reduced-motion: no-preference) {
11 | .App-logo {
12 | animation: App-logo-spin infinite 20s linear;
13 | }
14 | }
15 |
16 | .App-header {
17 | background-color: #282c34;
18 | min-height: 100vh;
19 | display: flex;
20 | flex-direction: column;
21 | align-items: center;
22 | justify-content: center;
23 | font-size: calc(10px + 2vmin);
24 | color: white;
25 | }
26 |
27 | .App-link {
28 | color: #61dafb;
29 | }
30 |
31 | @keyframes App-logo-spin {
32 | from {
33 | transform: rotate(0deg);
34 | }
35 | to {
36 | transform: rotate(360deg);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/App.js:
--------------------------------------------------------------------------------
1 | import { useEffect, useState } from "react"
2 | import { PokemonAPI } from "./apis/PokemonAPI"
3 | import { Card, Radio, Spin } from "antd"
4 |
5 | function App() {
6 | const [pokemonListLoading, setPokemonListLoading] = useState(false)
7 | const [pokemonList, setPokemonList] = useState([])
8 | const [selectedPokemonName, setSelectedPokemonName] = useState()
9 | const [selectedPokemon, setSelectedPokemon] = useState()
10 |
11 | useEffect(() => {
12 | setPokemonListLoading(true)
13 |
14 | PokemonAPI.getPaginated({
15 | limit: 20,
16 | offset: 0,
17 | })
18 | .then((pokemonList) => {
19 | setPokemonList(pokemonList)
20 | setSelectedPokemonName(pokemonList[0]?.name)
21 | })
22 | .finally(() => {
23 | setPokemonListLoading(false)
24 | })
25 | }, [])
26 |
27 | useEffect(() => {
28 | if (selectedPokemonName) {
29 | // enabling request cancellation
30 | PokemonAPI.get(selectedPokemonName, true).then((pokemon) => {
31 | setSelectedPokemon(pokemon)
32 | })
33 | }
34 | }, [selectedPokemonName])
35 |
36 | return (
37 |
38 |
39 |
40 |
{
44 | setSelectedPokemonName(e.target.value)
45 | }}
46 | >
47 | {pokemonList.map((pokemon) => {
48 | return (
49 |
50 | {pokemon.name}
51 |
52 | )
53 | })}
54 |
55 | {selectedPokemon && (
56 |
57 | {selectedPokemon.sprites.front_default && (
58 |
59 | )}
60 |
61 | )}
62 |
63 |
64 |
65 | )
66 | }
67 |
68 | export default App
69 |
--------------------------------------------------------------------------------
/src/App.test.js:
--------------------------------------------------------------------------------
1 | import { render, screen } from '@testing-library/react';
2 | import App from './App';
3 |
4 | test('renders learn react link', () => {
5 | render();
6 | const linkElement = screen.getByText(/learn react/i);
7 | expect(linkElement).toBeInTheDocument();
8 | });
9 |
--------------------------------------------------------------------------------
/src/apis/PokemonAPI.js:
--------------------------------------------------------------------------------
1 | import { api } from "./configs/axiosConfigs"
2 | import { defineCancelApiObject } from "./configs/axiosUtils"
3 |
4 | export const PokemonAPI = {
5 | get: async function (name, cancel = false) {
6 | const response = await api.request({
7 | url: `/pokemon/${name}`,
8 | method: "GET",
9 | signal: cancel ? cancelApiObject[this.get.name].handleRequestCancellation().signal : undefined,
10 | })
11 |
12 | return response.data
13 | },
14 | getPaginated: async function ({ limit, offset }, cancel = false) {
15 | const response = await api.request({
16 | url: "/pokemon/",
17 | method: "GET",
18 | params: {
19 | limit: limit,
20 | offset: offset,
21 | },
22 | signal: cancel ? cancelApiObject[this.getPaginated.name].handleRequestCancellation().signal : undefined,
23 | })
24 |
25 | return response.data.results
26 | },
27 | }
28 |
29 | // defining the cancel API object for ProductAPI
30 | const cancelApiObject = defineCancelApiObject(PokemonAPI)
31 |
--------------------------------------------------------------------------------
/src/apis/configs/axiosConfigs.js:
--------------------------------------------------------------------------------
1 | import axios from "axios"
2 | import { notification } from "antd"
3 |
4 | export const api = axios.create({
5 | baseURL: "https://pokeapi.co/api/v2",
6 | })
7 |
8 | // defining a custom error handler for all APIs
9 | const errorHandler = (error) => {
10 | const statusCode = error.response?.status
11 |
12 | if (error.code === "ERR_CANCELED") {
13 | notification.error({
14 | placement: "bottomRight",
15 | description: "API canceled!",
16 | })
17 | return Promise.resolve()
18 | }
19 |
20 | // logging only errors that are not 401
21 | if (statusCode && statusCode !== 401) {
22 | console.error(error)
23 | }
24 |
25 | return Promise.reject(error)
26 | }
27 |
28 | // registering the custom error handler to the
29 | // "api" axios instance
30 | api.interceptors.response.use(undefined, (error) => {
31 | return errorHandler(error)
32 | })
33 |
--------------------------------------------------------------------------------
/src/apis/configs/axiosUtils.js:
--------------------------------------------------------------------------------
1 | export function defineCancelApiObject(apiObject) {
2 | // an object that will contain a cancellation handler
3 | // associated to each API property name in the apiObject API object
4 | const cancelApiObject = {}
5 |
6 | // each property in the apiObject API layer object
7 | // is associated with a function that defines an API call
8 |
9 | // this loop iterates over each API property name
10 | Object.getOwnPropertyNames(apiObject).forEach((apiPropertyName) => {
11 | const cancellationControllerObject = {
12 | controller: undefined,
13 | }
14 |
15 | // associating the request cancellation handler with the API property name
16 | cancelApiObject[apiPropertyName] = {
17 | handleRequestCancellation: () => {
18 | // if the controller already exists,
19 | // canceling the request
20 | if (cancellationControllerObject.controller) {
21 | // canceling the request and returning this custom message
22 | cancellationControllerObject.controller.abort()
23 | }
24 |
25 | // generating a new controller
26 | // with the AbortController factory
27 | cancellationControllerObject.controller = new AbortController()
28 |
29 | return cancellationControllerObject.controller
30 | },
31 | }
32 | })
33 |
34 | return cancelApiObject
35 | }
36 |
--------------------------------------------------------------------------------
/src/index.css:
--------------------------------------------------------------------------------
1 | body {
2 | margin: 0;
3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
5 | sans-serif;
6 | -webkit-font-smoothing: antialiased;
7 | -moz-osx-font-smoothing: grayscale;
8 | }
9 |
10 | code {
11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
12 | monospace;
13 | }
14 |
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import ReactDOM from 'react-dom/client';
3 | import './index.css';
4 | import "antd/dist/antd.css"
5 | import App from './App';
6 | import reportWebVitals from './reportWebVitals';
7 |
8 | const root = ReactDOM.createRoot(document.getElementById('root'));
9 | root.render(
10 |
11 |
12 |
13 | );
14 |
15 | // If you want to start measuring performance in your app, pass a function
16 | // to log results (for example: reportWebVitals(console.log))
17 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
18 | reportWebVitals();
19 |
--------------------------------------------------------------------------------
/src/logo.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/src/reportWebVitals.js:
--------------------------------------------------------------------------------
1 | const reportWebVitals = onPerfEntry => {
2 | if (onPerfEntry && onPerfEntry instanceof Function) {
3 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
4 | getCLS(onPerfEntry);
5 | getFID(onPerfEntry);
6 | getFCP(onPerfEntry);
7 | getLCP(onPerfEntry);
8 | getTTFB(onPerfEntry);
9 | });
10 | }
11 | };
12 |
13 | export default reportWebVitals;
14 |
--------------------------------------------------------------------------------
/src/setupTests.js:
--------------------------------------------------------------------------------
1 | // jest-dom adds custom jest matchers for asserting on DOM nodes.
2 | // allows you to do things like:
3 | // expect(element).toHaveTextContent(/react/i)
4 | // learn more: https://github.com/testing-library/jest-dom
5 | import '@testing-library/jest-dom';
6 |
--------------------------------------------------------------------------------