Skip to content

ambientiaoy/reqres-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

reqres-api

A strongly-typed TypeScript client for the ReqRes.in API service. This package provides a clean and intuitive interface for performing user management operations with full TypeScript support.

npm package

Package URL: https://www.npmjs.com/package/reqres-api

Installation

npm install reqres-api

Features

  • Full TypeScript support with comprehensive type definitions
  • Simple and intuitive API for user management
  • Built-in error handling and response type safety
  • Support for pagination
  • Flexible update operations (PUT/PATCH)
  • Promise-based API

Usage

Initializing the Client

import { ReqResAPI } from 'reqres-api';

// Initialize with your preferred HTTP client
const client = new ReqResAPI(yourHttpClient);

Available Methods

Get Users (Paginated)

const { data, error } = await client.getUsers(1);
if (data) {
    console.log(`Total users: ${data.total}`);
    console.log(`Current page: ${data.page}`);
    console.log('Users:', data.data);
}

Create User

const newUser = {
    name: "John Doe",
    job: "Software Engineer"
};

const { data, error } = await client.createUser(newUser);
if (data) {
    console.log(`Created user with ID: ${data.id}`);
    console.log(`Created at: ${data.createdAt}`);
}

Update User

const updateData = {
    name: "John Doe",
    job: "Senior Software Engineer"
};

// Full update (PUT)
const { data: putData } = await client.updateUser(1, updateData, 'PUT');

// Partial update (PATCH)
const { data: patchData } = await client.updateUser(1, updateData, 'PATCH');

Delete User

const { data, error } = await client.deleteUser(1);
if (data === true) {
    console.log('User successfully deleted');
}

Error Handling

All methods return an object with either a data or an error property:

const { data, error } = await client.getUsers(1);
if (error) {
    console.error('Failed to fetch users:', error);
} else {
    console.log('Users:', data);
}

Requirements

  • Node.js ES2019 or later
  • TypeScript 5.0 or later (for development)

License

MIT

Author

Rafael Pinto Sperafico

Development

Building the Package

  1. Clone the repository:
git clone <your-repo-url>
cd reqres-api
  1. Install dependencies:
npm install
  1. Build the package:
npm run build

This will compile the TypeScript files into JavaScript in the dist directory.

Publishing to npm

  1. First, make sure you have an npm account and are logged in:
npm login
  1. Update the version number in package.json:
npm version patch  # for bug fixes
npm version minor  # for new features
npm version major  # for breaking changes
  1. Build the package:
npm run build
  1. Publish to npm:
npm publish

Note: The package is configured with "publishConfig": { "access": "public" } in package.json, which means it will be published as a public package.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Dependencies

  • @managed-api/generic-core: ^1.2.0

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors