fauxify v1.0.2
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.
Type | Validation | Auto-Generated Value |
---|---|---|
string | Ensures value is a string | 'mock_string' |
number | Ensures value is a number | Random integer |
email | Validates email format | 'user@example.com' |
uuid | Validates string (UUID) | Random UUID |
array | Ensures value is an array | [] |
object | Ensures value is a non-array object | {} |
success | Always 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! 🚀