From efb7526a9e29dc2c7130b7833ee94cbae50b2f97 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 12 Nov 2026 03:27:22 +0000 Subject: [PATCH 1/3] fix(Group): keep a flat group's include surface in its success schemas (#595) `Group.flat` / `flatReadOnly` inferred the documents' `Included` from `options.include?.included` while `Surface` was still generic, which widened it to `Schema.Top`: every endpoint's decoding services became `unknown`, and so did a typed client's. Pass the surface's `included` as an explicit type argument. `test/include-services.types.ts` pins it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Y5G6PMempKsgjiV1NDcCJY --- .changeset/flat-included-services.md | 5 ++ src/Group.ts | 18 +++++- test/include-services.types.ts | 94 ++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 .changeset/flat-included-services.md create mode 100644 test/include-services.types.ts diff --git a/.changeset/flat-included-services.md b/.changeset/flat-included-services.md new file mode 100644 index 00000000..874ac852 --- /dev/null +++ b/.changeset/flat-included-services.md @@ -0,0 +1,5 @@ +--- +"@thomasfosterau/effect-jsonapi": patch +--- + +`Group.flat` / `Group.flatReadOnly` keep their include surface's `included` union in the endpoints' success schemas (#595). It was inferred while the surface type was still generic, which widened it to `Schema.Top`: every endpoint's decoding services became `unknown`, so a typed `Client.make` method required `unknown` and `effect/cli` commands built on one failed to type-check. Applications that worked around it with a `Schema.Codec<…, never, never>` cast can drop the cast. diff --git a/src/Group.ts b/src/Group.ts index 9768b06c..627900df 100644 --- a/src/Group.ts +++ b/src/Group.ts @@ -344,6 +344,14 @@ const SHARED_FLAT_OPTIONS = { sort: false, } as const; +// The `included` union of a flat group's surface, or `never` without one. Named +// so the document constructors below receive it as an explicit type argument: +// inferred from `options.include?.included` while `Surface` is still generic, +// it widened to the constraint's `Schema.Top`, whose `DecodingServices` are +// `unknown`, so every endpoint's success schema, and a typed client's methods, +// asked for `unknown` services (#595). +type SurfaceIncluded = NonNullable["included"]; + const flatWire = < R extends Schema.Top, List extends Query.FlatListStruct, @@ -356,8 +364,14 @@ const flatWire = < const included = options.include?.included; const toManyMeta = options.toManyMeta; return { - document: Endpoint.ResourceDocument(options.resource, { included, toManyMeta }), - collection: Endpoint.FlatCollectionDocument(options.resource, { included, toManyMeta }), + document: Endpoint.ResourceDocument, TM>(options.resource, { + included, + toManyMeta, + }), + collection: Endpoint.FlatCollectionDocument, TM>(options.resource, { + included, + toManyMeta, + }), listQuery: Query.flatList(options.list, { filters: options.filters }), }; }; diff --git a/test/include-services.types.ts b/test/include-services.types.ts new file mode 100644 index 00000000..d4f6c59e --- /dev/null +++ b/test/include-services.types.ts @@ -0,0 +1,94 @@ +/** + * Type-level tests for #595: a flat group's include surface must not widen + * its endpoints' decoding services. `Group.flat` / `flatReadOnly` used to infer + * the documents' `included` from `options.include?.included` while the + * surface type was still generic, which widened it to `Schema.Top`, whose + * services are `unknown`; a typed client's methods then required `unknown`. + * + * This file is type-checked by the `typecheck` task, never run; the + * `@ts-expect-error` annotations are the assertions, each paired with its + * legal form. + */ +import * as Effect from "effect/Effect"; +import * as Schema from "effect/Schema"; +import type { HttpClient } from "effect/http"; +import { HttpApi } from "effect/http-api"; +import { + Client, + FlatQuery, + Group, + Include, + Relationship, + Resource, +} from "@thomasfosterau/effect-jsonapi"; + +const WidgetId = Schema.String.pipe(Schema.brand("WidgetId")); +const GadgetId = Schema.String.pipe(Schema.brand("GadgetId")); + +// A back-reference typed through a ref interface, as an application with a +// relationship-target registry spells it (preferenceflow's `lazyTarget`). +interface WidgetRef extends Resource.Any { + readonly type: "widgets"; + readonly identifier: Resource.Identifier<"widgets", typeof WidgetId>; +} +declare const widgetTarget: () => WidgetRef; + +const Gadget = Resource.make("gadgets", { + id: GadgetId, + attributes: { name: Schema.String }, + relationships: { widgets: Relationship.many(widgetTarget) }, +}); +const Widget = Resource.make("widgets", { + id: WidgetId, + attributes: { name: Schema.String }, + relationships: { maker: Relationship.one(() => Gadget) }, +}); +const WIDGET_INCLUDE = Include.make(Widget, { maker: Gadget }); +const ListWidgets = FlatQuery.listInput({ maxLimit: 100 }, {}); + +const { group: widgets } = Group.flat({ + resource: Widget, + create: Schema.Struct({ name: Schema.String }), + update: Schema.Struct({ id: WidgetId }), + list: ListWidgets, + include: WIDGET_INCLUDE, +}); +const { group: gadgets } = Group.flatReadOnly({ + resource: Gadget, + list: ListWidgets, + include: Include.make(Gadget, { widgets: Widget }), +}); +const { group: hidden } = Group.flat({ + resource: Widget, + create: Schema.Struct({ name: Schema.String }), + update: Schema.Struct({ id: WidgetId }), + list: ListWidgets, + include: WIDGET_INCLUDE, + hiddenWrites: true, +}); + +declare const needsService: Schema.Codec; + +// The surface itself needs no services. +const noServices = (_: Schema.Top & { readonly DecodingServices: never }): void => {}; +noServices(WIDGET_INCLUDE.included); +// @ts-expect-error -- control: a schema that does need a service is refused +noServices(needsService); + +// Every endpoint's success schema decodes without services. +noServices(widgets.endpoints.get["~Success"]); +noServices(widgets.endpoints.list["~Success"]); +noServices(widgets.endpoints.create["~Success"]); +noServices(gadgets.endpoints.get["~Success"]); +noServices(gadgets.endpoints.list["~Success"]); +noServices(hidden.endpoints.update["~Success"]); + +// A typed client over the groups requires only `HttpClient`, at `make` and in +// every method. +const Api = HttpApi.make("shop").add(widgets).add(gadgets); +const program = Effect.fn("program")(function* () { + const client = yield* Client.make(Api, { baseUrl: "http://localhost" }); + yield* client.widgets.get({ params: { id: WidgetId.make("1") }, query: {} }); + yield* client.gadgets.list({ query: { limit: 10, offset: 0 } }); +}); +export const requirements: Effect.Effect = program(); From fae240c8fc66a41c6400a1d340b80dec5c6c2712 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 12 Nov 2026 03:35:09 +0000 Subject: [PATCH 2/3] fix(Group): a flat group without a surface keeps its unconstrained included (#595) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Y5G6PMempKsgjiV1NDcCJY --- src/Endpoint.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Endpoint.ts b/src/Endpoint.ts index 1731dd42..8acad0a8 100644 --- a/src/Endpoint.ts +++ b/src/Endpoint.ts @@ -4480,6 +4480,15 @@ export interface FlatDocumentOptions = [Included] extends [never] + ? Schema.Top + : Included; + /** * The `success:` document of a flat `get` / `create` / `update`: a * single-resource data document over the **wire** form of `resource` @@ -4513,7 +4522,7 @@ export const ResourceDocument = < >( resource: S, options?: FlatDocumentOptions, -): DataDocument, WireSchema> => +): DataDocument, WireSchema, TM>> => asJsonApi( DataDocument( wire(resource, { toManyMeta: options?.toManyMeta }), @@ -4672,7 +4681,7 @@ export const FlatCollectionDocument = < >( resource: S, options?: FlatDocumentOptions, -): CollectionDocument, WireSchema> => +): CollectionDocument, WireSchema, TM>> => asJsonApi( CollectionDocument( wire(resource, { toManyMeta: options?.toManyMeta }), From 10bc7009292a1557745523380d4636301f41e34c Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 12 Nov 2026 03:53:05 +0000 Subject: [PATCH 3/3] feat(Assemble, ResourceHandlers): type `included` by the include registry (#595) Assemble's documents gain a trailing `I` (the `included` members' type, default `ResourceLike`); `resource` / `collection` / `unpaginatedCollection` set it from `targets` through the new `Assemble.TargetsIncluded` (what the loaders return; `never` with no registry), and `ResourceHandlers` uses it for `list` / `get` (`never` for the writes). A family's handlers then satisfy a flat group whose `included` is its surface's precise union, without a cast. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Y5G6PMempKsgjiV1NDcCJY --- .changeset/flat-included-services.md | 8 ++- src/Assemble.ts | 92 +++++++++++++++++++++------- src/ResourceHandlers.ts | 8 +-- test/assemble-collection.types.ts | 48 +++++++++++---- test/include-services.types.ts | 75 +++++++++++++++++++++++ 5 files changed, 191 insertions(+), 40 deletions(-) diff --git a/.changeset/flat-included-services.md b/.changeset/flat-included-services.md index 874ac852..862c9af8 100644 --- a/.changeset/flat-included-services.md +++ b/.changeset/flat-included-services.md @@ -1,5 +1,9 @@ --- -"@thomasfosterau/effect-jsonapi": patch +"@thomasfosterau/effect-jsonapi": minor --- -`Group.flat` / `Group.flatReadOnly` keep their include surface's `included` union in the endpoints' success schemas (#595). It was inferred while the surface type was still generic, which widened it to `Schema.Top`: every endpoint's decoding services became `unknown`, so a typed `Client.make` method required `unknown` and `effect/cli` commands built on one failed to type-check. Applications that worked around it with a `Schema.Codec<…, never, never>` cast can drop the cast. +A flat group's include surface now types its documents end to end (#595). + +- **`Group.flat` / `Group.flatReadOnly` keep the surface's `included` union in their success schemas.** It used to be inferred while the surface type was still generic, which widened it to `Schema.Top`: every endpoint's decoding services became `unknown`, so a typed `Client.make` method required `unknown`, and `effect/cli` commands built on one failed to type-check. A flat group without a surface is unchanged. +- **`Assemble` and `ResourceHandlers` type `included` by the include registry.** `Assemble.ResourceDocument`, `CollectionDocument` and `UnpaginatedCollectionDocument` gain a trailing `I` parameter, the `included` members' type, defaulting to `ResourceLike`. `Assemble.resource` / `collection` / `unpaginatedCollection` set it from their `targets` through the new `Assemble.TargetsIncluded`: what the loaders return, or `never` with no registry. `ResourceHandlers.readOnly` / `make` handlers get the same type, with writes typed `never`. A family's handlers then satisfy a group whose `included` is the surface's precise union, with no cast. Declare the registry with `satisfies Handlers.IncludeTargets` so the loaders' types stay visible; an annotated registry keeps the loose `ResourceLike`. +- **Migration:** drop any `Schema.Codec<…, never, never>` cast around a flat group's documents, and any cast on handler output. Code that names a document's exact type from a call with no registry sees `included?: ReadonlyArray` instead of `ReadonlyArray>`, which is still assignable to the old type. diff --git a/src/Assemble.ts b/src/Assemble.ts index db1020b8..18108cb4 100644 --- a/src/Assemble.ts +++ b/src/Assemble.ts @@ -125,6 +125,25 @@ export type WireResource = Document.Wire & { readonly links?: { readonly self?: string }; }; +/** + * The type of the `included` members a walk through `Targets` resolves to: + * what the registry's loaders return (`Handlers.IncludeTargetResource`). + * With no registry nothing is walked, so nothing can be included: `never`, + * which is also what lets such a document (a write's, say) satisfy a success + * schema whose `included` is an include surface's precise union (#595). A + * registry whose loaders' types aren't visible (annotated as + * `Handlers.IncludeTargets` rather than checked with `satisfies`) gives the + * loose {@link ResourceLike}. + * + * @since 0.22.0 + * @category type-level + */ +export type TargetsIncluded = [Targets] extends [never] + ? never + : [Extract, ResourceLike>] extends [never] + ? ResourceLike + : Extract, ResourceLike>; + /** * The `jsonapi` member every assembled document carries. * @@ -135,15 +154,18 @@ export const JSONAPI_MEMBER = { version: "1.1" } as const; /** * A single-resource document as {@link resource} assembles it, with the - * `jsonapi` member always present. `included` is the loose {@link ResourceLike} - * because the walk resolves through an untyped-by-type registry. + * `jsonapi` member always present. `I` is the `included` members' type: + * {@link resource} sets it to what the include registry's loaders return + * ({@link TargetsIncluded}), so a document is assignable to a success schema + * whose `included` is the include surface's precise union (#595). Written by + * hand, it defaults to the loose {@link ResourceLike}. * * @since 0.16.0 * @category models */ -export interface ResourceDocument { +export interface ResourceDocument { readonly data: WireResource & { readonly meta?: { readonly [key: string]: unknown } }; - readonly included?: ReadonlyArray>; + readonly included?: ReadonlyArray>; readonly links?: { readonly self?: string }; readonly meta?: { readonly [key: string]: unknown }; readonly jsonapi: typeof JSONAPI_MEMBER; @@ -156,11 +178,11 @@ export interface ResourceDocument { * @since 0.16.0 * @category models */ -export interface CollectionDocument { +export interface CollectionDocument { readonly data: ReadonlyArray< WireResource & { readonly meta?: { readonly [key: string]: unknown } } >; - readonly included?: ReadonlyArray>; + readonly included?: ReadonlyArray>; readonly links: Handlers.LinksValue; readonly meta: { readonly totalCount: number; readonly [key: string]: unknown }; readonly jsonapi: typeof JSONAPI_MEMBER; @@ -193,9 +215,10 @@ export type UnpaginatedCollectionDocument< Self extends string | undefined = undefined, // oxlint-disable-next-line effect/prefer-option-over-null -- a type-level switch on the `meta` option's type, not a runtime value: `undefined` selects the document with no `meta` M extends Handlers.MetaValue | undefined = undefined, + I extends ResourceLike = ResourceLike, > = { readonly data: ReadonlyArray & { readonly meta?: Handlers.MetaValue }>; - readonly included?: ReadonlyArray>; + readonly included?: ReadonlyArray>; readonly jsonapi: typeof JSONAPI_MEMBER; } & ([Self] extends [undefined] ? unknown @@ -409,8 +432,10 @@ export interface Options extends Options { - readonly included?: ReadonlyArray | undefined; + /** Members the caller already resolved; `X` is their type, part of the document's `included` type. */ + readonly included?: ReadonlyArray | undefined; } /** @@ -437,7 +462,8 @@ export interface UnpaginatedCollectionOptions< Self extends string | undefined = undefined, // oxlint-disable-next-line effect/prefer-option-over-null -- inferred from the `meta` option as passed; an `Option` there would stop a caller forwarding its own optional `meta` M extends Handlers.MetaValue | undefined = undefined, -> extends CollectionOptions { + X extends ResourceLike = never, +> extends CollectionOptions { /** * The collection's own absolute URL, emitted as the document's * `links.self`. Supplied by the handler, as {@link Pagination.baseUrl} is, @@ -841,7 +867,7 @@ export const resource = < resource: R, options: Options = {}, ): Effect.Effect< - ResourceDocument, + ResourceDocument>, IncludeError | Handlers.IncludeTargetError, Handlers.IncludeTargetServices > => { @@ -857,7 +883,10 @@ export const resource = < ), onResource: onResourceFor(options), }); - return { ...built, jsonapi: JSONAPI_MEMBER } as unknown as ResourceDocument; + return { ...built, jsonapi: JSONAPI_MEMBER } as unknown as ResourceDocument< + R, + TargetsIncluded + >; }), ); }; @@ -883,9 +912,9 @@ const withoutEmptyIncluded = ( +const assembleCollection = ( resources: ReadonlyArray, - options: CollectionOptions, + options: CollectionOptions, top: { readonly meta?: Handlers.MetaValue; readonly links?: Handlers.LinksValue; @@ -993,19 +1022,24 @@ export const collection = < R extends ResourceLike, V = unknown, Targets extends Handlers.IncludeTargets = never, + X extends ResourceLike = never, >( resources: ReadonlyArray, pagination: Pagination, - options: CollectionOptions = {}, + options: CollectionOptions = {}, ): Effect.Effect< - CollectionDocument, + CollectionDocument | X>, IncludeError | Handlers.IncludeTargetError, Handlers.IncludeTargetServices > => - assembleCollection, V, Targets>(resources, options, { - meta: { totalCount: pagination.totalCount }, - links: paginationLinks(pagination), - }); + assembleCollection | X>, V, Targets, X>( + resources, + options, + { + meta: { totalCount: pagination.totalCount }, + links: paginationLinks(pagination), + }, + ); /** * Assembles a JSON:API document around a whole, **unpaginated** collection — @@ -1087,13 +1121,19 @@ export const unpaginatedCollection = < Self extends string | undefined = undefined, // oxlint-disable-next-line effect/prefer-option-over-null -- see `UnpaginatedCollectionOptions`: inferred from the `meta` option as passed const M extends Handlers.MetaValue | undefined = undefined, + X extends ResourceLike = never, >( resources: ReadonlyArray, - options: UnpaginatedCollectionOptions = {}, + options: UnpaginatedCollectionOptions = {}, ): Effect.Effect< // `Self` is widened to `string`, so a literal `self` doesn't leak into the // type; `M` is not, since the document emits that very value. - UnpaginatedCollectionDocument, + UnpaginatedCollectionDocument< + R, + Self extends string ? string : undefined, + M, + TargetsIncluded | X + >, IncludeError | Handlers.IncludeTargetError, Handlers.IncludeTargetServices > => { @@ -1104,9 +1144,15 @@ export const unpaginatedCollection = < ? { self: options.self } : { self: options.self, query: options.query }; return assembleCollection< - UnpaginatedCollectionDocument, + UnpaginatedCollectionDocument< + R, + Self extends string ? string : undefined, + M, + TargetsIncluded | X + >, V, - Targets + Targets, + X >(resources, options, options.meta === undefined ? links : { ...links, meta: options.meta }); }; diff --git a/src/ResourceHandlers.ts b/src/ResourceHandlers.ts index d4c40b37..6f841b0e 100644 --- a/src/ResourceHandlers.ts +++ b/src/ResourceHandlers.ts @@ -376,14 +376,14 @@ export type ReadOnlyHandlers< readonly list: (request: { readonly query: TQuery; }) => Effect.Effect< - Assemble.CollectionDocument, + Assemble.CollectionDocument>, ReadError, R | RV | RL | Handlers.IncludeTargetServices >; readonly get: ( request: GetRequest, ) => Effect.Effect< - Assemble.ResourceDocument, + Assemble.ResourceDocument>, ReadError, R | RV | RC | Handlers.IncludeTargetServices >; @@ -418,7 +418,7 @@ export type CrudHandlers< readonly create: (request: { readonly payload: TCreate; }) => Effect.Effect< - Assemble.ResourceDocument | Committed, + Assemble.ResourceDocument | Committed, E | ET, R | RV | RC | RT | Exclude >; @@ -426,7 +426,7 @@ export type CrudHandlers< readonly params: { readonly id: TId }; readonly payload: TUpdate; }) => Effect.Effect< - Assemble.ResourceDocument | Committed, + Assemble.ResourceDocument | Committed, E | ET | ApiError.Conflict, R | RV | RC | RT | Exclude >; diff --git a/test/assemble-collection.types.ts b/test/assemble-collection.types.ts index bd504eaf..3ca7f8fa 100644 --- a/test/assemble-collection.types.ts +++ b/test/assemble-collection.types.ts @@ -39,7 +39,10 @@ const pagination = { totalCount: 3, offset: 0, limit: 10, baseUrl: "https://exam const paged = Assemble.collection(people, pagination); assertType< - Equals, Assemble.IncludeError>> + Equals< + typeof paged, + Effect.Effect, Assemble.IncludeError> + > >(true); declare const pagedDocument: Assemble.CollectionDocument; assertType(pagedDocument.links); @@ -54,7 +57,10 @@ const whole = Assemble.unpaginatedCollection(people, { include: [], includable: assertType< Equals< typeof whole, - Effect.Effect, Assemble.IncludeError> + Effect.Effect< + Assemble.UnpaginatedCollectionDocument, + Assemble.IncludeError + > > >(true); declare const wholeDocument: Assemble.UnpaginatedCollectionDocument; @@ -84,7 +90,10 @@ const linked = Assemble.unpaginatedCollection(people, { assertType< Equals< typeof linked, - Effect.Effect, Assemble.IncludeError> + Effect.Effect< + Assemble.UnpaginatedCollectionDocument, + Assemble.IncludeError + > > >(true); declare const linkedDocument: Assemble.UnpaginatedCollectionDocument; @@ -106,7 +115,10 @@ const optionallyLinked = Assemble.unpaginatedCollection(people, { assertType< Equals< typeof optionallyLinked, - Effect.Effect, Assemble.IncludeError> + Effect.Effect< + Assemble.UnpaginatedCollectionDocument, + Assemble.IncludeError + > > >(true); declare const optionallyLinkedDocument: Assemble.UnpaginatedCollectionDocument; @@ -129,7 +141,10 @@ const forwarded = Assemble.unpaginatedCollection(people, { assertType< Equals< typeof forwarded, - Effect.Effect, Assemble.IncludeError> + Effect.Effect< + Assemble.UnpaginatedCollectionDocument, + Assemble.IncludeError + > > >(true); @@ -138,7 +153,10 @@ const unlinked = Assemble.unpaginatedCollection(people, { self: undefined }); assertType< Equals< typeof unlinked, - Effect.Effect, Assemble.IncludeError> + Effect.Effect< + Assemble.UnpaginatedCollectionDocument, + Assemble.IncludeError + > > >(true); @@ -181,7 +199,7 @@ assertType< Equals< typeof stamped, Effect.Effect< - Assemble.UnpaginatedCollectionDocument, + Assemble.UnpaginatedCollectionDocument, Assemble.IncludeError > > @@ -204,7 +222,12 @@ assertType< Equals< typeof literal, Effect.Effect< - Assemble.UnpaginatedCollectionDocument, + Assemble.UnpaginatedCollectionDocument< + Person, + undefined, + { readonly generatedAt: "now" }, + never + >, Assemble.IncludeError > > @@ -229,7 +252,7 @@ assertType< Equals< typeof both, Effect.Effect< - Assemble.UnpaginatedCollectionDocument, + Assemble.UnpaginatedCollectionDocument, Assemble.IncludeError > > @@ -252,7 +275,7 @@ assertType< Equals< typeof forwardedMeta, Effect.Effect< - Assemble.UnpaginatedCollectionDocument, + Assemble.UnpaginatedCollectionDocument, Assemble.IncludeError > > @@ -275,7 +298,10 @@ const unstamped = Assemble.unpaginatedCollection(people, { meta: undefined }); assertType< Equals< typeof unstamped, - Effect.Effect, Assemble.IncludeError> + Effect.Effect< + Assemble.UnpaginatedCollectionDocument, + Assemble.IncludeError + > > >(true); diff --git a/test/include-services.types.ts b/test/include-services.types.ts index d4f6c59e..e7ba24fc 100644 --- a/test/include-services.types.ts +++ b/test/include-services.types.ts @@ -17,9 +17,12 @@ import { Client, FlatQuery, Group, + Handlers, Include, + Registry, Relationship, Resource, + ResourceHandlers, } from "@thomasfosterau/effect-jsonapi"; const WidgetId = Schema.String.pipe(Schema.brand("WidgetId")); @@ -92,3 +95,75 @@ const program = Effect.fn("program")(function* () { yield* client.gadgets.list({ query: { limit: 10, offset: 0 } }); }); export const requirements: Effect.Effect = program(); + +// --------------------------------------------------------------------------- +// The handlers satisfy a group whose `included` is the surface's precise union +// --------------------------------------------------------------------------- + +// The include registry's loaders return the members' own resource values +// (`satisfies`, so their types stay visible), and the handlers' documents type +// `included` by them: a `Group.flatReadOnly` success with the surface's union +// is then satisfiable without a cast. +const gadgetTargets = { + widgets: (ids: ReadonlyArray) => + Effect.succeed( + ids.map((id) => Widget.make({ id: WidgetId.make(id), attributes: { name: "w" } })), + ), +} satisfies Handlers.IncludeTargets; + +const GADGET_INCLUDE = Include.make(Gadget, { widgets: Widget }); +const gadgetGroup = Group.flatReadOnly({ + resource: Gadget, + list: ListWidgets, + include: GADGET_INCLUDE, +}); + +export const gadgetFamily = { + group: "gadgets", + handlers: ResourceHandlers.readOnly({ + segment: "gadgets", + listQuery: gadgetGroup.listQuery, + linkBase: Effect.succeed("https://example.test"), + include: { includable: GADGET_INCLUDE.paths, targets: gadgetTargets }, + ops: { + list: () => Effect.succeed({ items: [], totalCount: 0 }), + get: (id: string) => + Effect.succeed(Gadget.make({ id: GadgetId.make(id), attributes: { name: "g" } })), + }, + }), +} satisfies Registry.FamilyFor; + +// …and so do the CRUD handlers, writes included, over a `Group.flat` with a +// surface. +const widgetTargets = { + gadgets: (ids: ReadonlyArray) => + Effect.succeed( + ids.map((id) => Gadget.make({ id: GadgetId.make(id), attributes: { name: "g" } })), + ), +} satisfies Handlers.IncludeTargets; + +const widgetGroup = Group.flat({ + resource: Widget, + create: Schema.Struct({ name: Schema.String }), + update: Schema.Struct({ id: WidgetId }), + list: ListWidgets, + include: WIDGET_INCLUDE, +}); + +export const widgetFamily = { + group: "widgets", + handlers: ResourceHandlers.make({ + segment: "widgets", + listQuery: widgetGroup.listQuery, + linkBase: Effect.succeed("https://example.test"), + include: { includable: WIDGET_INCLUDE.paths, targets: widgetTargets }, + ops: { + list: () => Effect.succeed({ items: [], totalCount: 0 }), + get: (id: string) => + Effect.succeed(Widget.make({ id: WidgetId.make(id), attributes: { name: "w" } })), + create: (_input: { readonly name: string }) => Effect.succeed({ id: WidgetId.make("1") }), + update: (input: { readonly id: string }) => Effect.succeed({ id: input.id }), + delete: () => Effect.void, + }, + }), +} satisfies Registry.FamilyFor;