Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Antony-Figueroa/my-evershop-app/llms.txt
Use this file to discover all available pages before exploring further.
Routing
EverShop uses a file-based routing system withroute.json configuration files. These files define how API endpoints are accessed and control the middleware execution order.
route.json Files
Every API endpoint in EverShop requires aroute.json file that specifies:
- HTTP methods allowed
- URL path
- Access control (public/private)
Basic Structure
route.json
Configuration Properties
methods
HTTP methods that the endpoint accepts:Only the methods listed in
methods will be accepted. Other methods will return a 405 Method Not Allowed error.path
The URL path where the endpoint is accessible:access
Controls authentication requirements:Middleware Execution Order
EverShop uses filename conventions to control middleware execution order. Files are executed alphabetically, and you can use brackets[name] to specify dependencies.
Naming Convention
Format:[prerequisite]handlerName.ts
The name in brackets runs before the main handler.
Example: Body Parser Middleware
From the sample extension:extensions/sample/src/api/createFoo/
import bodyParser from 'body-parser';
export default (request, response, next) => {
bodyParser.json({ inflate: false })(request, response, next);
};
import { EvershopRequest, EvershopResponse } from '@evershop/evershop';
export default (request: EvershopRequest, response: EvershopResponse, next) => {
const { name, description } = request.body; // Body is now parsed!
if (!name || !description) {
return response
.status(400)
.json({ error: 'Name and description are required' });
}
const newFoo = {
id: Date.now(),
name,
description
};
response.status(201).json({
success: true,
data: { foo: newFoo }
});
};
Complete API Endpoint Example
Here’s the full structure for thecreateFoo endpoint:
Middleware Chain Examples
Single Middleware
Two Middleware
validateInput.ts- Validates request data[validateInput]createOrder.ts- Creates the order
Three Middleware
authenticate.ts- Verify user is logged in[authenticate]validatePermissions.ts- Check user has permission[authenticate][validatePermissions]updateProduct.ts- Update the product
Middleware in brackets must exist as separate files. The files are executed in dependency order, not alphabetically.
Request and Response Types
EverShop provides TypeScript types for Express request and response objects:Common Patterns
Public API Endpoint
route.json
Private API Endpoint
route.json
RESTful Resource
Page Routing
While API endpoints useroute.json, page components use a different system based on directory structure and the layout export.
Page Areas
Pages are organized by “areas” - logical groupings based on where/when they render:Layout Export
Thelayout export determines where a component renders:
Common Area IDs
| Area ID | Description |
|---|---|
header | Top of all pages |
footer | Bottom of all pages |
content | Main content area |
sidebar | Sidebar area |
productPageBottom | Below product details |
checkoutPaymentMethod | Payment method selection |
checkoutShipping | Shipping method selection |
Best Practices
Use descriptive endpoint names
Use descriptive endpoint names
Name your API directories and files clearly:
- Good:
createOrder,updateProduct,deleteUser - Bad:
handler,endpoint1,api
Validate input in middleware
Validate input in middleware
Create separate validation middleware:
Handle errors consistently
Handle errors consistently
Return appropriate HTTP status codes:
Use TypeScript for type safety
Use TypeScript for type safety
Always import and use EverShop types:
Testing Routes
Using curl
Using Postman or Insomnia
- Set the HTTP method (GET, POST, etc.)
- Enter the URL:
http://localhost:3000/foos - Add headers (e.g.,
Content-Type: application/json) - Add body (for POST/PUT requests)
- Send the request
Next Steps
- Learn about Extensions for organizing your code
- Understand GraphQL API for alternative data fetching
- Explore Themes for page component rendering