1.0.5 • Published 3 years ago
dummy-apis-jsonplaceholder v1.0.5
dummy-apis-jsonplaceholder
dummy-apis-jsonplaceholder
is a collection of dummy api urls based on jsonplaceholder like https://jsonplaceholder.typicode.com/posts.
Quick Start
yarn add dummy-apis-jsonplaceholder
npm i dummy-apis-jsonplaceholder
Examples
import { dummyApiUrl, DummyPost } from "dummy-apis-jsonplaceholder";
const response = await axios.get<DummyPost[]>(dummyApiUrl().posts.index);
- response.data : https://jsonplaceholder.typicode.com/posts
const response = await axios.get<DummyPost>(dummyApiUrl(2).posts.index);
- response.data : https://jsonplaceholder.typicode.com/posts/2
// with react
import { MouseEventHandler, useCallback, useState } from "react";
import { dummyApiUrl, DummyPost } from "dummy-apis-jsonplaceholder";
import axios from "axios";
export default function Home(): JSX.Element {
const [posts, setPosts] = useState<DummyPost[] | null>(null);
const handleButtonClick: MouseEventHandler<HTMLButtonElement> =
useCallback(async (): Promise<void> => {
const response = await axios.get<DummyPost[]>(dummyApiUrl().posts.index);
setPosts(response.data);
}, []);
return (
<div>
<button onClick={handleButtonClick}>get api</button>
{posts?.map((post) => (
<div key={post.id}> {post.title}</div>
))}
</div>
);
}