Extensions
Extensions are the core building blocks of EverShop’s modular architecture. They encapsulate features, business logic, and functionality in isolated, reusable packages that can be enabled or disabled independently.What are Extensions?
Extensions in EverShop allow you to:- Add new API endpoints
- Create custom page components
- React to system events with subscribers
- Schedule background jobs with cron tasks
- Extend GraphQL schema with new types and resolvers
- Organize business logic separately from the core application
Extensions focus on business logic and functionality, while themes focus on visual presentation. This separation allows you to switch themes without losing functionality.
Directory Structure
Each extension lives in theextensions/ directory with the following structure:
Registering Extensions
Extensions must be registered inconfig/default.json to be loaded:
config/default.json
Creating an Extension
{
"name": "myExtension",
"version": "1.0.0",
"main": "index.js",
"type": "module",
"scripts": {
"tsc": "tsc"
}
}
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src/**/*"]
}
Extension Components
API Endpoints
Create REST API endpoints insrc/api/[endpoint]/:
extensions/sample/src/api/createFoo/[bodyParser]createFoo.ts
route.json:
extensions/sample/src/api/createFoo/route.json
The filename
[bodyParser]createFoo.ts indicates that bodyParser middleware runs before createFoo. See Routing for details.Page Components
Add React components insrc/pages/[area]/[page]/:
extensions/sample/src/pages/frontStore/homepage/FooList.tsx
Event Subscribers
React to system events insrc/subscribers/[event]/:
extensions/sample/src/subscribers/product_created/consoleLog.js
Cron Jobs
Schedule background tasks insrc/crons/:
extensions/sample/src/crons/everyMinute.ts
bootstrap.ts:
extensions/sample/src/bootstrap.ts
GraphQL Extensions
Extend the GraphQL schema insrc/graphql/types/[TypeName]/:
extensions/sample/src/graphql/types/Foo/Foo.graphql
extensions/sample/src/graphql/types/Foo/Foo.resolvers.js
Real-World Examples
offlinePayments Extension
Provides payment methods like Cash on Delivery and Bank Transfer:extensions/offlinePayments/src/pages/frontStore/checkout/cod/CashOnDelivery.tsx
productCatalog Extension
Extends the Product type with supplement-specific fields:extensions/productCatalog/src/graphql/types/ProductExtension/ProductExtension.graphql
Best Practices
Use TypeScript for type safety
Use TypeScript for type safety
Always use
.ts and .tsx files for new code. Define proper interfaces for your props and data structures.Follow naming conventions
Follow naming conventions
- Extension names: camelCase (e.g.,
offlinePayments,productCatalog) - API endpoints: descriptive names (e.g.,
createOrder,updateProduct) - Components: PascalCase (e.g.,
ProductReviews,CashOnDelivery)
Keep extensions focused
Keep extensions focused
Each extension should have a single, well-defined purpose. If you’re adding multiple unrelated features, consider splitting them into separate extensions.
Never modify .evershop/ or node_modules/
Never modify .evershop/ or node_modules/
All custom code should live in extensions. Never edit files in
.evershop/ or node_modules/ as they will be overwritten.Next Steps
- Learn about Themes for customizing the visual presentation
- Understand Routing to configure API endpoints and pages
- Explore the GraphQL API for data fetching