Server Configuration
Sherpa servers are configured using a sherpa.server.ts
file, where you define
the structure and behavior of your server. This configuration file is located
at the root of your project and serves as the entry point for your Sherpa server.
Config File
The configuration file must be located at sherpa.server.ts
and have a
default export of the config using the SherpaJS.New.server
function as follows:
import SherpaJS from "sherpa-core";
export default SherpaJS.New.server({
content: {
serverSecret: process.env.SERVER_SECRET,
allowThingy: true
}
});
When you load modules they have there own context, that is provided by the module loader, and do not have access to your server context.
Context
A property that allows you to define a context object. Contexts are provided to endpoints and can contain any additional data or settings needed for request processing.
Server Config Type
The server configuration type is structured as:
{
context: T;
}
where T
can be any type, allowing for flexible and customizable
server configurations.
Basic Example
A basic configuration where the context contains simple settings:
import { SherpaJS } from "sherpa-core";
export default SherpaJS.New.server({
context: {
serverSecret: "foo",
allowThingy: true
}
});
The context object is then available within endpoints.
import { Request, Response } from "sherpa-core";
export function GET(request:Request, context:unknown) {
consoe.log(context.serverSecret); // returns "foo"
consoe.log(context.allowThingy); // returns true
return Response.new();
}
Typed Example
A more detailed configuration using a typed context for additional type safety and clarity:
import { SherpaJS } from "sherpa-core";
type ConfigExample = { serverSecret: string; allowThingy: boolean };
export default SherpaJS.New.server<ConfigExample>({
context: {
serverSecret: "foo",
allowThingy: true
}
});
The context object is then available within endpoints.
import { Request, Response } from "sherpa-core";
export function GET(request:Request, context:ConfigExample) {
consoe.log(context.serverSecret); // returns "foo"
consoe.log(context.allowThingy); // returns true
return Response.new();
}