github.com/fcjr/shiftapi
Package shiftapi provides end-to-end type safety from Go to TypeScript. Define your API with typed Go handler functions and shiftapi automatically generates an OpenAPI 3.1 spec, validates requests, and produces a fully-typed TypeScript client — all from a single source of truth. where greet is a typed handler: ShiftAPI discriminates input struct fields by their struct tags: A single input struct can mix path, query, and body fields: Use WithEnum to register the allowed values for a named type. The values are reflected as an enum constraint in the OpenAPI schema for every field of that type — no validate:"oneof=..." tag required: Enum values apply to body fields, query parameters, path parameters, and header parameters. If a field also carries a validate:"oneof=..." tag, the tag takes precedence over the registered enum values. The type parameter must satisfy the Scalar constraint (~string, ~int*, ~uint*, ~float*). Use *multipart.FileHeader fields with the form tag for file uploads: Use the header tag on the Resp struct to set HTTP response headers. Header-tagged fields are written as response headers and automatically excluded from the JSON response body. They are also documented as response headers in the OpenAPI spec. Non-pointer fields are always sent, even with a zero value. Use a pointer field for optional headers that should only be sent when set. Supported types are the same scalars as request headers (string, bool, int*, uint*, float*). For static response headers, use WithResponseHeader. Headers are applied in the following order — later sources override earlier ones for the same header name: For status codes that forbid a response body (204 No Content, 304 Not Modified), use WithStatus with struct{} or a header-only response type. No JSON body or Content-Type header will be written. Response headers (both static and dynamic) are still sent. Registering a route with status 204 or 304 and a response type that has JSON body fields panics at startup — this catches misconfigurations early. Use HandleSSE for Server-Sent Events with a typed event writer: SSEWriter automatically sets Content-Type, Cache-Control, and Connection headers on the first write. SSEWriter.Send automatically determines the event name from the concrete Go type registered via SSESends. SSESends is required for HandleSSE. It registers event types for auto-wrap and OpenAPI schema generation. For multiple event types, pass multiple SSEEventType descriptors: SSESends generates a oneOf schema with a discriminator in the OpenAPI spec, which produces TypeScript discriminated unions in the generated client. The generated TypeScript client includes a typed subscribe function constrained to SSE paths. It handles path/query/header parameter substitution, SSE stream parsing, and yields typed events as an async iterable. For custom SSE framing or non-standard behavior, use HandleRaw with WithContentType("text/event-stream") instead. Use API.Group to create a sub-router with a shared path prefix and options. Groups can be nested, and error types and middleware are inherited by child groups: Use WithMiddleware to apply standard HTTP middleware at any level: Middleware is applied from outermost to innermost in the order: API → parent Group → child Group → Route → handler. Within a single WithMiddleware call, the first argument wraps outermost. Use NewContextKey, SetContext, and FromContext to pass typed data from middleware to handlers without untyped context keys or type assertions: Each ContextKey has pointer identity, so two keys for the same type T will never collide. The type parameter ensures that SetContext and FromContext agree on the value type at compile time. Use WithError to declare that a specific error type may be returned at a given HTTP status code. The error type must implement the [error] interface and its struct fields are reflected into the OpenAPI schema. WithError works at all three levels: New, API.Group/Group.Group, and route functions. At runtime, if the handler returns an error matching a registered type (via errors.As), it is serialized as JSON with the declared status code. Multiple error types can be declared per route. Wrapped errors are matched automatically. Validation failures automatically return 422 with structured ValidationError responses. Unrecognized errors return 500 Internal Server Error to prevent leaking implementation details. Use WithBadRequestError and WithInternalServerError to customize the default 400 and 500 response bodies. Option is the primary option type. It works at all three levels: New, API.Group/Group.Group, and Handle. WithError, WithMiddleware, and WithResponseHeader all return Option. Some options are level-specific: WithInfo and WithBadRequestError only work with New (APIOption), while WithStatus and WithRouteInfo only work with Handle (RouteOption). Use ComposeOptions to bundle multiple Option values into a reusable option: ComposeAPIOptions, ComposeGroupOptions, and ComposeRouteOptions can mix shared and level-specific options at their respective levels. Every API automatically serves: API implements http.Handler, so it works with any standard middleware, router, or test framework:
proxy.golang.org
v0.0.42
22 days ago
42
Links
| Registry | proxy.golang.org |
| Source | Repository |
| Docs | Documentation |
| JSON API | View JSON |
| CodeMeta | codemeta.json |
Package Details
Repository
| Stars | 7 on GitHub |
| Forks | 1 on GitHub |