Skip to main content

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 the extensions/ directory with the following structure:

Registering Extensions

Extensions must be registered in config/default.json to be loaded:
config/default.json
Set "enabled": false to disable an extension without removing it from your project.

Creating an Extension

Extension Components

API Endpoints

Create REST API endpoints in src/api/[endpoint]/:
extensions/sample/src/api/createFoo/[bodyParser]createFoo.ts
With a corresponding 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 in src/pages/[area]/[page]/:
extensions/sample/src/pages/frontStore/homepage/FooList.tsx

Event Subscribers

React to system events in src/subscribers/[event]/:
extensions/sample/src/subscribers/product_created/consoleLog.js
Subscriber functions are automatically called when the corresponding event is emitted. The directory name (product_created) determines which event to subscribe to.

Cron Jobs

Schedule background tasks in src/crons/:
extensions/sample/src/crons/everyMinute.ts
Register the job in bootstrap.ts:
extensions/sample/src/bootstrap.ts

GraphQL Extensions

Extend the GraphQL schema in src/graphql/types/[TypeName]/:
extensions/sample/src/graphql/types/Foo/Foo.graphql
With corresponding resolvers:
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

Always use .ts and .tsx files for new code. Define proper interfaces for your props and data structures.
  • Extension names: camelCase (e.g., offlinePayments, productCatalog)
  • API endpoints: descriptive names (e.g., createOrder, updateProduct)
  • Components: PascalCase (e.g., ProductReviews, CashOnDelivery)
Each extension should have a single, well-defined purpose. If you’re adding multiple unrelated features, consider splitting them into separate extensions.
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