1.0.2 • Published 7 months ago

fauxify v1.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
7 months ago

Fauxify

Fauxify is a lightweight JavaScript library for creating and testing mock API endpoints with schema-based input and output validation. It simplifies the process of creating and testing APIs by allowing developers to define endpoint behavior in a few lines of code.


Features

  • Schema-based Validation: Define and validate input/output schemas effortlessly.
  • Type Handling: Predefined and extensible types for fields, including custom validators and generators.
  • Mock API Endpoints: Simulate endpoints with configurable behavior.
  • Asynchronous Responses: Simulate network delays for realistic API behavior.
  • Nested Objects Support: Easily define and validate nested object structures.

Installation

Install the library via npm:

npm install fauxify

Getting Started

Importing Fauxify

import fauxify from 'fauxify';
import { types } from 'fauxify/types.js';

Usage

1. Define a Mock API Endpoint

Define a mock endpoint using fauxify.create. Specify the input schema, output schema, and the endpoint path.

fauxify.create('/signup', {
    email: types.email,
    password: types.string,
    profile: types.object({
        name: types.string,
        age: types.number,
        email: types.email
    })
}, {
    userId: types.uuid,
    message: types.success
});

2. Call the Mock API

Call the endpoint using fauxify.call. Provide payload data and handle success or error responses in a callback.

fauxify.call('/signup', {
    email: 'user@example.com',
    password: 'password123',
    profile: {
        name: 'John Doe',
        age: 30,
        email: 'johndoe@example.com'
    }
}, (success, error) => {
    if (success) {
        console.log('Signup Success:', success);
    } else {
        console.error('Signup Error:', error.message);
    }
});

How It Works

Step 1: Define Input and Output Schemas

Fauxify uses predefined types to validate inputs and generate outputs. Each type has:

  • Type Validation: Ensures the payload adheres to the schema.
  • Value Generation: Generates default values for mocked responses.
const userProfileSchema = types.object({
    name: types.string,
    age: types.number,
    email: types.email
});

Step 2: Create Endpoints

Use fauxify.create(endpoint, inputSchema, outputSchema) to define a mock endpoint.

  • endpoint: Path of the mock API.
  • inputSchema: Expected structure for the request payload.
  • outputSchema: Defines the mock response structure.

Predefined Types

Fauxify provides built-in types for common use cases.

TypeValidationAuto-Generated Value
stringEnsures value is a string'mock_string'
numberEnsures value is a numberRandom integer
emailValidates email format'user@example.com'
uuidValidates string (UUID)Random UUID
arrayEnsures value is an array[]
objectEnsures value is a non-array object{}
successAlways valid (success message placeholder)'Operation successful'

Example: Nested API Endpoints

Mock an endpoint to fetch a user profile:

const userProfileSchema = types.object({
    name: types.string,
    age: types.number,
    email: types.email
});

fauxify.create('/getProfile', {
    userId: types.uuid
}, {
    profile: userProfileSchema,
    message: types.success
});

fauxify.call('/getProfile', {
    userId: '550e8400-e29b-41d4-a716-446655440000'
}, (success, error) => {
    if (success) {
        console.log('Get Profile Success:', success);
    } else {
        console.error('Get Profile Error:', error.message);
    }
});

A complete example

import fauxify from 'fauxify';
import { types } from 'fauxify/types.js';

const userProfileSchema = types.object({
    name: types.string,
    age: types.number,
    email: types.email
});

fauxify.create('/signup', {
    email: types.email,
    password: types.string,
    profile: userProfileSchema
}, {
    userId: types.uuid,
    message: types.success
});

fauxify.create('/getProfile', {
    userId: types.uuid
}, {
    profile: userProfileSchema,
    message: types.success
});

fauxify.call('/signup', {
    email: 'user@example.com',
    password: 'password123',
    profile: {
        name: 'John Doe',
        age: 30,
        email: 'johndoe@example.com'
    }
}, (success, error) => {
    if (success) {
        console.log('Signup Success:', success);
    } else {
        console.error('Signup Error:', error.message);
    }
});

fauxify.call('/getProfile', {
    userId: '550e8400-e29b-41d4-a716-446655440000'
}, (success, error) => {
    if (success) {
        console.log('Get Profile Success:', success);
    } else {
        console.error('Get Profile Error:', error.message);
    }
});

License

Fauxify is licensed under the MIT License.

Contact

For questions, feedback, or issues, contact atulit23@gmail.com.


Happy mocking! 🚀

1.0.2

7 months ago

1.0.1

7 months ago