1.0.2 • Published 8 months ago

json-placeholder-node-sdk v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
8 months ago

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

OptionTypeRequiredDefaultDescription
baseUrlstringNohttps://jsonplaceholder.typicode.comThe base URL for API requests
apiKeystringNo-API key for authentication

License

ISC

Author

Vrushab Kudchi

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago