Module Configuration
Sherpa modules are configured using a sherpa.module.ts
file, where you define
the structure and behavior of your module. This configuration file is located
at the root of your project and serves as the entry point for your Sherpa module.
Config File
The configuration file must be located at sherpa.module.ts
and have a
default export of the config using the SherpaJS.New.module
function as
follows. You can export a class using CreateModuleInterface
that acts as a
wrapper for validating the context when the module is loaded.
Basic Configuration
This basic configuration sets up a module with a simple interface
definition. The context schema is defined inline using the
CreateModuleInterface
function.
import { SherpaJS, CreateModuleInterface } from "sherpa-core";
export default SherpaJS.New.module({
name: "example-module",
interface: CreateModuleInterface<{ foo: boolean, bar: string }>()
});
Class-Based Configuration
Alternatively, you can export any interface class with a constructor that
takes the context as a parameter and sets this.context
within your class.
You can attach additional methods to interact with your module.
These interfaces can be called by other functions and endpoints inside your codebase, see the calling module interfaces example.
import { SherpaJS, ModuleInterface } from "sherpa-core";
export default SherpaJS.New.module({
name: "example-module",
interface: class Example implements ModuleInterface {
context: { foo: number, bar: string };
constructor(context: { foo: number, bar: string }) {
this.context = context;
}
}
});
You can also export any additional attributes from this file as needed. The
module config file, sherpa.module.ts
should be the main script defined
in your package.json
.
interface
A class that has a constructor with context:[TYPE]
parameter and a
property context: [TYPE]
. You can also use CreateModuleInterface
to
generate this class.
Module Config Type
The module configuration type is structured as:
{
name:string;
interface:T;
}
where T
is a class that implements implements ModuleInterface
. Thus, it
has a context
and a constructor
that takes context
as a parameter.
{
context:Schema;
constructor(context:Schema) { this.context = context; }
}
Basic Example
A basic configuration using the CreateModuleInterface
function.
import { SherpaJS, CreateModuleInterface } from "sherpa-core";
export default SherpaJS.New.module({
name: "example-module",
interface: CreateModuleInterface<{ foo: boolean, bar: string }>()
});
Loading the module from a server endpoints, learn more.
import ExampleModule from "../example-module/sherpa.module.ts";
export default ExampleModule.load({
foo: true,
bar: "hello"
})
Module Interface Call Example
A basic configuration using the CreateModuleInterface
function.
import { SherpaJS, CreateModuleInterface } from "sherpa-core";
export type Schema = { foo: number, bar: string };
export default SherpaJS.New.module({
name: "example-module",
interface: class Example implements ModuleInterface {
context:Schema;
constructor(context:Schema) {
this.context = context;
}
getFoo():number {
return this.context.foo;
}
}
});
Loading the a module from a server endpoints, learn more.
import ExampleModule from "../../example-module/sherpa.module.ts";
export default ExampleModule.load({
foo: 3,
bar: "hello"
})
Calling the loaded module instance from another endpoint...
import { Request, Response } from "sherpa-core";
import example from "./example/module.ts";
export function GET() {
return Response.text(example.getFoo()); // returns 3
}