Skip to content

Instantly share code, notes, and snippets.

@evasconcelos
Last active March 30, 2021 09:43
Show Gist options
  • Save evasconcelos/de0d6e884f62fd2989771798dc4a4d91 to your computer and use it in GitHub Desktop.
Save evasconcelos/de0d6e884f62fd2989771798dc4a4d91 to your computer and use it in GitHub Desktop.
SOLID example
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>
);
}
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;
}>;
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