UNPKG

@schnaq/strapi4-utils

Version:

Utility functions for working with Strapi v4

137 lines (134 loc) 3.86 kB
type StrapiID = number | `${number}`; type StrapiBaseProps = { id?: StrapiID; createdBy?: User; updatedBy?: User; createdAt?: string; updatedAt?: string; publishedAt?: string | Date; }; type StrapiSearchParams = { populate?: string[] | Record<string, any>; pagination?: { page?: number; pageSize?: number; withCount?: boolean; start?: number; limit?: number; }; sort?: string[]; filters?: Record<string, any>; publicationState?: "live" | "preview"; fields?: string[]; }; type StrapiResponse<T> = { data: T; error?: { status: number; name: string; message: string; details: { name: string; message: string; }; }; meta?: StrapiMeta; }; type StrapiMeta = { pagination: { page: number; pageCount: number; pageSize: number; total: number; }; }; type ImageWithDimensions = { url: string; width: number; height: number; }; type ImageFormats = "thumbnail" | "small" | "medium" | "large"; /** * Image file formats as returned by the Strapi API. */ type ImageFormat = { name: string; hash: string; ext: string; mime: string; path: string | null; width: number; height: number; size: number; url: string; }; type MediaAttributes = { name: string; alternativeText: string | null; caption: string | null; width: number; height: number; formats?: { thumbnail?: ImageFormat; small?: ImageFormat; medium?: ImageFormat; large?: ImageFormat; }; hash: string; ext: string; mime: string; size: number; url: string; previewUrl: string | null; provider: string; provider_metadata?: string; createdAt: string; updatedAt?: string; }; type Image = StrapiBaseProps & MediaAttributes & { id: StrapiID; }; type User = StrapiBaseProps & { username: string; email: string; confirmed: boolean; blocked: boolean; provider: string; role?: number; password?: string; }; type Headers = { Authorization?: string; "Content-Type"?: "application/json" | "text/plain"; body?: string; }; /** * Query data from the API. Coerces the response to the given type. * Expects as a default a Strapi API Token, but also accepts a user token from * the users-permissions plugin. * @param path The path to query. * @param body The body to send. * @param method The HTTP method to use. * @param token The token to use for authentication. Defaults to a Strapi API token. * @param apiUrl The API URL to the Strapi Backend. * @param cache The cache mode to use. */ declare function queryAPI<T>([path, body, method, token, apiUrl, cache,]: [string, any?, string?, string?, string?, RequestCache?]): Promise<StrapiResponse<T> | undefined>; /** * Flattens the data of the API response. */ declare function flattenData(json: any): void; /** * Query data from the API from the user-permissions plugin, which formats the return type differently. */ declare function queryAPIUser([url, token]: [string, string]): Promise<User | undefined>; /** * Flattens the attributes of an object. */ declare function flattenAttributes(obj: any): any; /** * Return the URL for the image in the given size. Falls back to the next smaller size, if the * requested size is not available. */ declare function getImageUrlForSize(image: Image | undefined, size?: ImageFormats): ImageWithDimensions | undefined; export { type Headers, type Image, type ImageFormat, type ImageFormats, type ImageWithDimensions, type MediaAttributes, type StrapiBaseProps, type StrapiID, type StrapiMeta, type StrapiResponse, type StrapiSearchParams, type User, flattenAttributes, flattenData, getImageUrlForSize, queryAPI, queryAPIUser };