Skip to content

Commit

Permalink
fix(deserializer): append attributes and relationships to ResourceRes…
Browse files Browse the repository at this point in the history
…ult in a cleaner way
  • Loading branch information
DASPRiD committed Mar 30, 2024
1 parent 47f46e0 commit 4ecbce1
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/deserializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,31 @@ type RelationshipsResult<T extends Relationships> = {
[K in keyof T]: RelationshipResult<T[K]>;
};

type AppendAttributes<
TBase,
TAttributesSchema extends AttributesSchema | undefined,
> = TAttributesSchema extends AttributesSchema ? TBase & z.output<TAttributesSchema> : TBase;

type AppendRelationships<
TBase,
TRelationships extends Relationships | undefined,
> = TRelationships extends Relationships ? TBase & RelationshipsResult<TRelationships> : TBase;

export type ResourceResult<
TDeserializer extends AnyResourceDeserializer,
TAttributesSchema extends AttributesSchema | undefined = InferAttributesSchema<TDeserializer>,
TRelationships extends Relationships | undefined = InferRelationships<TDeserializer>,
> = {
id: string;
_links?: DefaultLinks;
_meta?: DefaultMeta;
} & (TAttributesSchema extends AttributesSchema
? z.output<TAttributesSchema>
: Record<string, never>) &
(TRelationships extends Relationships
? RelationshipsResult<TRelationships>
: Record<string, never>);
> = AppendRelationships<
AppendAttributes<
{
id: string;
_links?: DefaultLinks;
_meta?: DefaultMeta;
},
TAttributesSchema
>,
TRelationships
>;

export type DocumentResult<TData> = {
data: TData;
Expand Down

0 comments on commit 4ecbce1

Please sign in to comment.