Build your first Service endpoint

Run a route through the real gateway and inspect successful and invalid requests.

This example returns JSON through the real Service router and HTTP gateway. With name, it returns 200 and Hello, Sitenera. Without it, Service returns 400 invalid_input for the query. The example runs in-process.

Run the complete example

Install repository dependencies first. Save the following as a temporary first-route.mjs and run it using the repository alias loader.

import assert from "node:assert/strict";
import { createServiceGateway } from "@sitenera/service/gateway.ts";
import { defineServiceApplication } from "@service/common/app/application.ts";
import { createAppContext } from "@service/common/app/context.ts";
import { openServiceRuntime } from "@service/common/app/entry.ts";
import { defineRoute } from "@service/common/route/definition.ts";
import { defineRouteModule } from "@service/common/route/module.ts";
import { defineValidator } from "@service/common/route/validator.ts";

const input = defineValidator((s) => ({
  query: s.object({
    name: s.single(s.string().trim().min(1).max(80)),
  }),
}));

const greeting = defineRouteModule("greeting", {
  routes: [
    defineRoute("GET public.greeting.read", {
      async handler(ctx) {
        const { name } = ctx.parse(input.query, ctx.query, "query");
        return ctx.json({ message: `Hello, ${name}` });
      },
    }),
  ],
});

const application = defineServiceApplication({ modules: [greeting] });
const app = createAppContext({
  platform: { deployment: "standalone", mode: "test" },
  configuration: { allowedOrigins: [] },
  secrets: {},
  capabilities: [],
});
const send = createServiceGateway(await openServiceRuntime(application, app));

const url = "https://service.example/_/public/greeting/read";
const ok = await send(new Request(url + "?name=Sitenera"));
const success = await ok.json();
assert.equal(ok.status, 200);
assert.deepEqual(success.data, { message: "Hello, Sitenera" });

const invalid = await send(new Request(url));
const failure = await invalid.json();
assert.equal(invalid.status, 400);
assert.equal(failure.error.code, "invalid_input");
assert.equal(failure.error.details.part, "query");
assert.equal(failure.meta.requestId, invalid.headers.get("x-request-id"));
console.log("200 Hello, Sitenera; 400 invalid_input (query)");
node --import ./apps/sitenera/tests/register-alias.mjs /path/to/first-route.mjs

Move the route into your application

  • defineValidator declares the query field. s.single reads one value from the query string.
  • defineRoute maps the method and full route name to the handler. defineRouteModule groups related routes.
  • This example assembles a standalone application with defineServiceApplication. In your project, export the route from the owning feature’s route.ts and register its loader in packages/service/app/route-modules.ts alongside the existing modules.
  • Once the route needs writes or queries, put them in the owning use case. For protected operations, choose the audience and check permission for the specific operation.

Continue with controllers, the HTTP contract, and authentication and authorization.