Skip to content

Latest commit

 

History

History
25 lines (20 loc) · 929 Bytes

embedding.md

File metadata and controls

25 lines (20 loc) · 929 Bytes

Embedding as a module

You can embed an EdgeSpec service inside other applications.

For example, say you want to embed an API for ships in Star Wars inside an API for all things Star Wars:

// api/ships/[...modulePath].ts
import { withEdgeSpec } from "../with-edge-spec"
import { z } from "zod"

export default withEdgeSpec({
  auth: "none",
  methods: ["GET"],
})((req) => {
  // This is a thin wrapper over `await import()` to enable types, you can use `import * as shipsApi from "@acme/star-wars-ships` directly if you want.
  const shipsApi = await loadBundle("@acme/star-wars-ships")

  // The /ships base path is automatically removed from the request path that the ships API sees.
  return shipsApi.makeRequest(req, {
    // This is optional, you can add middleware if you want that runs before any other middleware in the ships API. This is often useful for dependency injection.
    middleware: [],
  })
})