Bazaar.Phoenix.Router (Bazaar v0.3.0)

Copy Markdown View Source

Phoenix router macros for mounting UCP and ACP routes.

Usage

defmodule MyAppWeb.Router do
  use Phoenix.Router
  use Bazaar.Phoenix.Router

  pipeline :api do
    plug :accepts, ["json"]
    plug Bazaar.Plugs.UCP
  end

  # Optional: Add schema validation
  pipeline :bazaar_validated do
    plug Bazaar.Plugs.ValidateRequest
    plug Bazaar.Plugs.ValidateResponse
  end

  scope "/" do
    pipe_through [:api, :bazaar_validated]

    # UCP protocol (default)
    bazaar_routes "/", MyApp.Commerce.Handler

    # ACP protocol
    bazaar_routes "/acp", MyApp.Commerce.Handler, protocol: :acp
  end
end

Schema Validation

Bazaar includes plugs for validating requests and responses against Smelter-generated Ecto schemas:

Both plugs are optional but recommended for catching schema violations early in development.

Generated Routes (UCP)

MethodPathDescription
GET/.well-known/ucpDiscovery endpoint
POST/checkout-sessionsCreate checkout
GET/checkout-sessions/:idGet checkout
PUT/checkout-sessions/:idUpdate checkout
POST/checkout-sessions/:id/completeComplete checkout
POST/checkout-sessions/:id/cancelCancel checkout
GET/orders/:idGet order
PUT/orders/:idUpdate order (with order_updates: true)
POST/orders/:id/actions/cancelCancel order
GET/productsList products
GET/products/searchSearch products
GET/products/:idGet product
POST/webhooks/ucpWebhook endpoint

Generated Routes (ACP)

ACP uses slightly different URL patterns and HTTP methods:

MethodPathDescription
POST/checkout_sessionsCreate checkout
GET/checkout_sessions/:idGet checkout
POST/checkout_sessions/:idUpdate checkout
POST/checkout_sessions/:id/completeComplete checkout
POST/checkout_sessions/:id/cancelCancel checkout

Note: ACP does not have a discovery endpoint.

Options

bazaar_routes "/api/v1", MyApp.Handler,
  protocol: :ucp,               # Protocol: :ucp (default) or :acp
  only: [:checkout, :orders],   # Limit capabilities
  discovery: true,              # Include discovery endpoint (UCP only)
  webhooks: true,               # Include webhook endpoint
  order_updates: false,         # Mount PUT /orders/:id (beyond the spec's REST binding)
  validate_requests: true,      # Enable request validation (requires plug in pipeline)
  validate_responses: true      # Enable response validation (requires plug in pipeline)

Summary

Functions

Mounts protocol routes at the given path using the specified handler.

Functions

bazaar_routes(path, handler, opts \\ [])

(macro)

Mounts protocol routes at the given path using the specified handler.

Examples

# UCP protocol (default)
bazaar_routes "/", MyApp.Handler

# ACP protocol
bazaar_routes "/acp", MyApp.Handler, protocol: :acp

# Multiple protocols at different paths
bazaar_routes "/ucp", MyApp.Handler, protocol: :ucp
bazaar_routes "/acp", MyApp.Handler, protocol: :acp