Building a Module
Building a SherpaJS module is straightforward and can be accomplished in just a few steps. Modules allow you to encapsulate and share functionality across multiple SherpaJS applications. Here's how to get started:
Quick Installation
We recommend starting with the SherpaJS Module Template, which provides a pre-configured structure and example endpoints. After downloading the template, install all dependencies by running:
npm install
You can start the development server with npm run dev
.
Now your project is all setup, explore around the project files and get acquainted with the SherpaJS Project Structure and SherpaJS CLI.
Manual Installation
Creating a new module is extremely easy and can be done within a couple of
minutes. To manually create a new module start by create a new NodeJS project
with npm init
. Then install the SherpaJS Core package:
npm install sherpa-core
Updating package.json
Open the package.json
file in the root directory of your project, and
update the following properties.
{
"type": "module",
"exports": "./sherpa.module.ts",
"scripts": {
"build": "sherpa build -b Vercel",
"build-local": "sherpa build -b local",
"start": "sherpa start",
"dev": "sherpa dev",
"test": "echo \"no test specified\""
}
}
Creating Module Config
Create a module config file named sherpa.module.ts
in
the root directory of your module. This file will default export a module
configuration.
import { SherpaJS, CreateModuleInterface } from "sherpa-core";
export type Schema = { foo: boolean, bar: string };
export default SherpaJS.New.module({
name: "example-module",
interface: CreateModuleInterface<Schema>()
});
Learn more about module configs.
Creating Server Config
Create a new Typescript file for your server configuration
in the root directory of your project named sherpa.server.ts
. Technically a
server config is not required to create a module, but it is to test the module.
import { SherpaJS } from "sherpa-core";
import { Schema } from "../sherpa.module.ts";
export default SherpaJS.New.server<Schema>({
context: {
example: "foo"
}
});
The context you provide the server should match the context schema for your module.
Creating Endpoints
SherpaJS uses a directory-based structure for routing, similar to NextJS. Start
by creating a /routes
directory and place a new enpoint index.ts
file in the
directory. This will be processed at the root /
of your application.
import { Request, Response } from "sherpa-core";
import { Schema } from "../sherpa.module.ts"
export function GET(request:Request, context:Schema) {
return Response.text("Hello World!");
}
Learn more about routing and endpoints.
Details
While the routes of your module are available on a server when the module is
loaded, other assets like the public
directory are not available. Keep this
in mind when designing your module.
Share Your Module
Share your module with the world by deploying it as an NPM package and submitting a new issue to get it listed as a SherpaJS Community module.
Important Notes
- Ensure your module is deployed as an NPM package.
- Include documentation on how to set it up, including required properties.
- Link to the SherpaJS documentation for users to understand how to set it up.
Thank you for supporting SherpaJS! 🎉