Last active
March 30, 2021 09:43
-
-
Save evasconcelos/de0d6e884f62fd2989771798dc4a4d91 to your computer and use it in GitHub Desktop.
SOLID example
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
import { useEffect, useState } from "react"; | |
import RequestCatFactService from "./RequestCatFactService"; | |
export default function App() { | |
const [fact, setFact] = useState(""); | |
useEffect(() => { | |
RequestCatFactService().then((res) => { | |
setFact(res); | |
}); | |
}, []); | |
return ( | |
<div className="App"> | |
<h1>Cat Facts</h1> | |
<p>{fact}</p> | |
</div> | |
); | |
} |
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
import RequestFactory from "./RequestFactory"; | |
export default (): Promise<string> => { | |
return new Promise((resolve, reject) => { | |
RequestFactory("https://cat-fact.herokuapp.com/facts") | |
.then((res) => resolve((res as CatFactsResponse)[0].text)) | |
.catch((error) => reject(error)); | |
}); | |
}; | |
type CatFactsResponse = Array<{ | |
text: string; | |
}>; |
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
import RequestAxios from "./RequestAxios"; | |
import RequestFake from "./RequestFake"; | |
export default (url: string) => { | |
const enableFakeData = | |
new URL(window.location.href).searchParams.get("enableFakeData") === "1"; | |
return enableFakeData ? RequestFake(url) : RequestAxios(url); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment