> ## Documentation Index
> Fetch the complete documentation index at: https://juo.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Services

> Domain APIs that blocks call to read and mutate data.

Services are the data layer for blocks. Each service abstracts a domain — subscriptions, customers, orders, products, schedules, auth, routing, workflows — behind a stable interface so blocks stay decoupled from any particular backend.

## Pattern

Every service follows the same shape:

```ts theme={null}
const service = create<Domain>Service(adapter);
```

The **adapter** is the I/O layer. Real adapters call the application's API; mock adapters return static data for development. The service wraps the adapter, manages reactive state, and exposes a stable interface to blocks.

Provide each service once on the `<juo-context-root>` element:

```ts theme={null}
import {
  provideContext,
  createSubscriptionService,
  createApiSubscriptionAdapter,
  SubscriptionServiceContext,
} from "@juo/blocks";

const root = document.querySelector("juo-context-root") as HTMLElement;

provideContext(
  root,
  SubscriptionServiceContext,
  createSubscriptionService(createApiSubscriptionAdapter({ baseUrl: "/api/v1" })),
);
```

Blocks then call `injectContext(element, SubscriptionServiceContext)` to retrieve it. See [Contexts](https://juo.dev/docs/blocks/concepts/contexts.md) for how that flows through the DOM.

## Result type

All async service methods return a discriminated union — check `_tag` before reading data:

```ts theme={null}
const result = await subscriptionService.getById(id);

if (result._tag === "Success") {
  console.log(result.data);
} else {
  console.error(result.error);
}
```

## Reactive state

Where it matters, services expose their state as signals — `subscriptionService.current`, `customerService.current`, `workflowService.isLoading`. Blocks read these signals and re-render automatically. See [Reactivity](https://juo.dev/docs/blocks/concepts/reactivity.md).

## Adapters: real vs mock

Every service ships with a mock adapter for development and Storybook:

```ts theme={null}
import {
  createSubscriptionService,
  createMockSubscriptionAdapter,
} from "@juo/blocks";

provideContext(
  root,
  SubscriptionServiceContext,
  createSubscriptionService(createMockSubscriptionAdapter()),
);
```

## Writing a custom adapter

If the backend doesn't match the default adapter, implement the adapter interface and pass it to the service factory:

```ts theme={null}
import type { SubscriptionAdapter } from "@juo/blocks";
import {
  createSubscriptionService,
  SubscriptionServiceContext,
  provideContext,
} from "@juo/blocks";

const myAdapter: SubscriptionAdapter = {
  async search() { /* ... */ },
  async getById(id) { /* ... */ },
  async pause(id) { /* ... */ },
  // ...
};

provideContext(root, SubscriptionServiceContext, createSubscriptionService(myAdapter));
```

## Available services

> [card] [Subscription](https://juo.dev/docs/blocks/services/subscription.md)
>
> Read, mutate, pause, resume, cancel subscriptions; manage items, discounts, upsells.

> [card] [Customer](https://juo.dev/docs/blocks/services/customer.md)
>
> Current customer profile and reactive customer state.

> [card] [Order](https://juo.dev/docs/blocks/services/order.md)
>
> Order history and details.

> [card] [Product](https://juo.dev/docs/blocks/services/product.md)
>
> Product catalogue lookup and search with variants.

> [card] [Schedules](https://juo.dev/docs/blocks/services/schedules.md)
>
> Upcoming orders, skip / reschedule.

> [card] [Login](https://juo.dev/docs/blocks/services/login.md)
>
> Passwordless and social login, token management.

> [card] [Router](https://juo.dev/docs/blocks/services/router.md)
>
> In-page navigation, decoupled from a specific router.

> [card] [Workflow](https://juo.dev/docs/blocks/services/workflow.md)
>
> Interactive flow execution — start, respond, observe.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.