API Reference
This page documents all public functions, types, and options in the Contract v2 package.Main Package
Import:github.com/go-mizu/mizu/contract/v2
Functions
Register
Registers a Go interface implementation as a Contract service.| Parameter | Type | Description |
|---|---|---|
T | type parameter | The interface type that defines your API |
impl | T | Your implementation that satisfies the interface |
opts | ...Option | Optional configuration |
*RegisteredService for mounting on transports.
Example:
Types
RegisteredService
A registered service ready to be mounted on transports.| Method | Returns | Description |
|---|---|---|
Descriptor() | *ServiceDescriptor | Get service metadata |
Call(ctx, resource, method, input) | (any, error) | Invoke a method programmatically |
NewInput(resource, method) | (any, error) | Create new input instance |
ServiceDescriptor
Metadata about a registered service.Resource
A group of related methods (e.g., “todos”).Method
Metadata for a single method.TypeInfo
Information about a type used in the API.HTTPBinding
HTTP method and path for a method.Options
WithName
Sets the service name:WithDescription
Sets the service description:WithDefaultResource
Groups all methods under a resource name:WithResource
Groups specific methods under a resource:WithMethodHTTP
Override HTTP binding for a method:WithHTTP
Set HTTP bindings for multiple methods:WithDefaults
Set global defaults:WithStreaming
Mark a method as streaming:REST Package
Import:github.com/go-mizu/mizu/contract/v2/transport/rest
Functions
Mount
Mounts REST endpoints on a router.| Parameter | Type | Description |
|---|---|---|
router | *mizu.Router | Mizu router |
svc | *RegisteredService | Registered service |
opts | ...Option | Optional configuration |
Create->POST /{resource}List->GET /{resource}Get->GET /{resource}/{id}Update->PUT /{resource}/{id}Delete->DELETE /{resource}/{id}
NewHandler
Creates an HTTP handler for the service.Options
JSON-RPC Package
Import:github.com/go-mizu/mizu/contract/v2/transport/jsonrpc
Functions
Mount
Mounts a JSON-RPC 2.0 endpoint.| Parameter | Type | Description |
|---|---|---|
router | *mizu.Router | Mizu router |
path | string | URL path for the endpoint |
svc | *RegisteredService | Registered service |
opts | ...Option | Optional configuration |
NewHandler
Creates a JSON-RPC HTTP handler.Options
Types
Request
Response
Error
MCP Package
Import:github.com/go-mizu/mizu/contract/v2/transport/mcp
Functions
Mount
Mounts MCP endpoint.NewHandler
Creates an MCP HTTP handler.Options
Types
ServerInfo
OpenAPI Package
Import:github.com/go-mizu/mizu/contract/v2/transport/openapi
Functions
Mount
Mounts OpenAPI spec endpoint.Generate
Generates OpenAPI document from service.Error Types
Error
Contract error with code and message.| Method | Returns | Description |
|---|---|---|
Error() | string | Error message (implements error) |
HTTPStatus() | int | HTTP status code |
JSONRPCCode() | int | JSON-RPC error code |
Unwrap() | error | Wrapped cause |
WithDetail(key, value) | *Error | Add detail |
WithDetails(map) | *Error | Add multiple details |
WithCause(err) | *Error | Wrap underlying error |
Error Constructors
Convenience functions for common errors:ErrorCode
All available error codes:Helper Functions
Method Signatures
Contract recognizes these method patterns:Pattern 1: Input and Output
Pattern 2: Output Only
Pattern 3: Input Only
Pattern 4: Neither
- First parameter must be
context.Context - Input (if any) must be a pointer to a struct
- Last return value must be
error - Output (if any) must be a pointer to a struct
Complete Example
This example shows a complete application using the recommended package-based organization:See Also
- Quick Start - Get started quickly
- Services - Method signatures in detail
- Errors - Error patterns
- Overview - All transports