strapi-flatten-graphql
Version:
helper to flatten nested strapi v4 graphql structure including typings
24 lines (23 loc) • 1.58 kB
TypeScript
export type StrapiEntity = {
id?: string | null;
attributes?: object | null;
};
export type StrapiEntityResponse<N extends object = object> = {
data?: StrapiEntity | null;
};
export type StrapiEntityResponseCollection<N extends object = object> = {
data?: Array<StrapiEntity> | null;
};
export type FlattenStrapi<T> = T extends StrapiEntityResponse ? Flatten<T> : T extends StrapiEntityResponseCollection ? FlattenArray<T> : T;
export type FlattenEntity<E extends StrapiEntity> = FlattenAttributes<NonNullable<E['attributes']>> & {
id: string;
};
export type Flatten<T extends StrapiEntityResponse> = FlattenEntity<NonNullable<T['data']>>;
export type FlattenArray<T extends StrapiEntityResponseCollection> = Array<FlattenEntity<NonNullable<NonNullable<T['data']>[number]>>>;
export type FlattenAttributes<T> = {
[K in keyof T]?: NonNullable<T[K]> extends StrapiEntityResponse ? FlattenEntity<NonNullable<NonNullable<T[K]>['data']>> : NonNullable<T[K]> extends StrapiEntityResponseCollection ? Array<FlattenEntity<NonNullable<NonNullable<T[K]>['data']>[number]>> : T[K];
};
export declare function flattenEntity<T extends StrapiEntity>({ id, attributes }: T): FlattenEntity<T>;
export declare function flattenEntityResponse<T extends StrapiEntityResponse>(response?: T | null | undefined): Flatten<T>;
export declare function flattenEntityResponseCollection<T extends StrapiEntityResponseCollection>({ data }: T): FlattenArray<T>;
export declare function flattenDeep<T extends StrapiEntityResponse | StrapiEntityResponseCollection>(obj: T): FlattenStrapi<T>;