1.0.2 • Published 8 months ago
json-placeholder-node-sdk v1.0.2
JSON Placeholder Node SDK
A lightweight Node.js SDK for interacting with the JSONPlaceholder API.
Installation
npm install json-placeholder-node-sdk
Usage
ES6/TypeScript
import JsonPlaceholder from "json-placeholder-node-sdk";
const client = new JsonPlaceholder({
baseUrl: "https://jsonplaceholder.typicode.com", // optional
apiKey: "your-api-key" // optional
});
CommonJS
const JsonPlaceholder = require("json-placeholder-node-sdk").default;
const client = new JsonPlaceholder({
baseUrl: "https://jsonplaceholder.typicode.com", // optional
apiKey: "your-api-key" // optional
});
Examples
Get All Posts
// ES6/TypeScript
import JsonPlaceholder from "json-placeholder-node-sdk";
const client = new JsonPlaceholder({});
client.getPosts().then((posts) => {
console.log(posts);
});
// CommonJS
const JsonPlaceholder = require("json-placeholder-node-sdk").default;
const client = new JsonPlaceholder({});
client.getPosts().then((posts) => {
console.log(posts);
});
Get Post by ID
// ES6/TypeScript & CommonJS
client.getPostById(1).then((post) => {
console.log(post);
});
Create a New Post
// ES6/TypeScript & CommonJS
const newPost = {
id: 1,
title: "foo",
body: "bar",
userId: 1
};
client.createPost(newPost).then((createdPost) => {
console.log(createdPost);
});
API Reference
Posts
getPosts()
Returns a Promise that resolves to an array of all posts.
Response Type:
type Post = {
userId: number;
id: number;
title: string;
body: string;
}
getPostById(id: number)
Returns a Promise that resolves to a single post.
Parameters:
id
(number): The ID of the post to retrieve
createPost(newPost: NewPost)
Creates a new post and returns a Promise that resolves to the created post.
Parameters:
newPost
: Object with the following structure:
type NewPost = {
id: number;
title: string;
body: string;
}
Configuration Options
Option | Type | Required | Default | Description |
---|---|---|---|---|
baseUrl | string | No | https://jsonplaceholder.typicode.com | The base URL for API requests |
apiKey | string | No | - | API key for authentication |
License
ISC
Author
Vrushab Kudchi