Skip to content

Commit

Permalink
fix: adjust typings for relationship deserializers
Browse files Browse the repository at this point in the history
  • Loading branch information
DASPRiD committed Mar 19, 2024
1 parent b4cd1ce commit fa85eda
Showing 1 changed file with 41 additions and 30 deletions.
71 changes: 41 additions & 30 deletions src/deserializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,37 @@ import type { AttributesSchema, DefaultLinks, DefaultMeta, RootLinks } from "./s

export type RelationshipType = "one" | "one_nullable" | "many";

export type RelationshipDeserializer<
export type ReferenceRelationshipDeserializer<
TRelationshipType extends RelationshipType,
TResourceType extends string,
> = {
relationshipType: TRelationshipType;
resourceType: TResourceType;
};

export type AnyReferenceRelationshipDeserializer = ReferenceRelationshipDeserializer<
RelationshipType,
string
>;

export type IncludedRelationshipDeserializer<
TRelationshipType extends RelationshipType,
TInclude extends AnyResourceDeserializer | undefined,
> = TInclude extends undefined
? {
relationshipType: TRelationshipType;
resourceType: TResourceType;
}
: {
relationshipType: TRelationshipType;
include: TInclude;
};
TInclude extends AnyResourceDeserializer,
> = {
relationshipType: TRelationshipType;
include: TInclude;
};

export type AnyRelationshipDeserializer = RelationshipDeserializer<
string,
export type AnyIncludedRelationshipDeserializer = IncludedRelationshipDeserializer<
RelationshipType,
// biome-ignore lint/suspicious/noExplicitAny: required to avoid circular dependency error
ResourceDeserializer<string, AttributesSchema | undefined, any> | undefined
// biome-ignore lint/suspicious/noExplicitAny: required to avoid circular dependency
any
>;

export type AnyRelationshipDeserializer =
| AnyReferenceRelationshipDeserializer
| AnyIncludedRelationshipDeserializer;

export type Relationships = Record<string, AnyRelationshipDeserializer>;

export type ResourceDeserializer<
Expand All @@ -43,21 +53,22 @@ export type AnyResourceDeserializer = ResourceDeserializer<
Relationships | undefined
>;

export type InferResourceType<T> = T extends RelationshipDeserializer<
infer U,
export type InferResourceType<T> = T extends ReferenceRelationshipDeserializer<
RelationshipType,
AnyResourceDeserializer | undefined
infer U
>
? U
: never;
export type InferRelationshipType<T> = T extends RelationshipDeserializer<
string,
infer U,
AnyResourceDeserializer | undefined
>
: T extends IncludedRelationshipDeserializer<infer U, AnyResourceDeserializer>
? U extends AnyResourceDeserializer
? U["type"]
: never
: never;
export type InferRelationshipType<T> = T extends ReferenceRelationshipDeserializer<infer U, string>
? U
: never;
export type InferInclude<T> = T extends RelationshipDeserializer<string, RelationshipType, infer U>
: T extends IncludedRelationshipDeserializer<infer U, AnyResourceDeserializer>
? U
: never;
export type InferInclude<T> = T extends IncludedRelationshipDeserializer<RelationshipType, infer U>
? U
: never;
export type InferType<T> = T extends ResourceDeserializer<
Expand All @@ -82,10 +93,10 @@ export type InferRelationships<T> = T extends ResourceDeserializer<
? U
: never;

type IncludeResult<
TDeserializer extends AnyRelationshipDeserializer,
TInclude extends AnyResourceDeserializer | undefined = InferInclude<TDeserializer>,
> = TInclude extends AnyResourceDeserializer ? ResourceResult<TInclude> : { id: string };
type IncludeResult<TDeserializer extends AnyRelationshipDeserializer> =
TDeserializer extends AnyReferenceRelationshipDeserializer
? { id: string }
: InferInclude<TDeserializer>;

export type RelationshipResult<
TDeserializer extends AnyRelationshipDeserializer,
Expand Down

0 comments on commit fa85eda

Please sign in to comment.