# `@lazarv/react-server/resources`

Typed server resources — `createResource` / `createResources` — for binding validated data loaders to routes and reading them inside server components.

## Functions

### `createResource`

```ts
function createResource(): ResourceDescriptor<void>;
function createResource<TKey>(options: {
    key: ValidateSchema<TKey>;
}): ResourceDescriptor<TKey>;
function createResource<TParseMap extends Record<string, (value: string) => any>>(options: {
    key: TParseMap;
}): ResourceDescriptor<InferParseMap<TParseMap>>;
```

Create a singleton resource descriptor (no key).

```ts
export const currentUser = createResource();
```

### `createResources`

```ts
function createResources<T extends Record<string, ResourceDescriptor<any, any>>>(resources: T): ResourceCollection<T>;
```

Collect resources into a typed registry.

```ts
export const resources = createResources({ userById, posts, currentUser });
resources.invalidateAll(); // bust all caches
```

## Constants

### `resources`

```ts
const resources: Record<string, ResourceDescriptor<any, any>>;
```

Collection of file-router generated resource descriptors.
Each entry is created from `.resource.ts/js` files via the file-router.

Empty when the file-router is not active.

```tsx
import { resources } from "@lazarv/react-server/resources";

const data = resources.todos.use({ filter: "all" });
resources.todos.invalidate({ filter: "all" });
```

## Interfaces

### `ResourceBinding`

```ts
interface ResourceBinding<TKey = void, TData = unknown> {
    resource: ResourceDescriptor<TKey, TData>;
    mapFn: (routeParams: Record<string, any>, searchParams: Record<string, any>) => TKey extends void ? void : TKey;
}
```

A route-resource binding returned by `.from()`.
Used in createRoute's `resources` option to load data when a route matches.

### `ResourceDescriptor`

```ts
interface ResourceDescriptor<TKey = void, TData = unknown> {
    readonly _loader: ((key: TKey) => TData | Promise<TData>) | null;
    readonly key: ValidateSchema<TKey> | Record<string, (value: string) => any> | null;
    use: TKey extends void ? () => TData : (key: TKey) => TData;
    query: TKey extends void ? () => Promise<TData> : (key: TKey) => Promise<TData>;
    prefetch: TKey extends void ? () => void : (key: TKey) => void;
    invalidate: TKey extends void ? () => void | Promise<void> : (key?: TKey) => void | Promise<void>;
    from: TKey extends void ? never : (mapFn: (routeParams: Record<string, any>, searchParams: Record<string, any>) => TKey) => ResourceBinding<TKey, TData>;
    bind: <TBoundData>(loader: TKey extends void ? () => TBoundData | Promise<TBoundData> : (key: TKey) => TBoundData | Promise<TBoundData>) => ResourceDescriptor<TKey, TBoundData>;
}
```

A typed resource descriptor with suspense-integrated data fetching,
imperative queries, cache prefetching, and invalidation.

Created by `createResource(options?)` (descriptor only, no loader).
Call `.bind(loaderFn)` to attach a loader.

## Types

### `ResourceCollection`

```ts
type ResourceCollection<T extends Record<string, ResourceDescriptor<any, any>>> = T & {
    invalidateAll(): Promise<void[]>;
};
```

A resource collection with individual resources and `invalidateAll()`.
