storyblok-js-client
Version:
Universal JavaScript SDK for Storyblok's API
499 lines • 14.1 kB
text/typescript
//#region src/constants.d.ts
declare const _METHOD: {
readonly GET: "get";
readonly DELETE: "delete";
readonly POST: "post";
readonly PUT: "put";
};
type ObjectValues<T> = T[keyof T];
type Method = ObjectValues<typeof _METHOD>;
declare const StoryblokContentVersion: {
readonly DRAFT: "draft";
readonly PUBLISHED: "published";
};
type StoryblokContentVersionKeys = typeof StoryblokContentVersion[keyof typeof StoryblokContentVersion];
//#endregion
//#region src/sbFetch.d.ts
interface ResponseFn {
(arg?: ISbResponse | any): any;
}
//#endregion
//#region src/interfaces.d.ts
interface ISbStoriesParams extends Partial<ISbStoryData>, ISbMultipleStoriesData, ISbAssetsParams {
resolve_level?: number;
_stopResolving?: boolean;
by_slugs?: string;
by_uuids?: string;
by_uuids_ordered?: string;
component?: string;
content_type?: string;
cv?: number;
datasource?: string;
dimension?: string;
excluding_fields?: string;
excluding_ids?: string;
excluding_slugs?: string;
fallback_lang?: string;
filename?: string;
filter_query?: any;
first_published_at_gt?: string;
first_published_at_lt?: string;
from_release?: string;
is_startpage?: boolean;
language?: string;
level?: number;
page?: number;
per_page?: number;
published_at_gt?: string;
published_at_lt?: string;
resolve_assets?: number;
resolve_links?: 'link' | 'url' | 'story' | '0' | '1' | 'link';
resolve_links_level?: 1 | 2;
resolve_relations?: string | string[];
search_term?: string;
size?: string;
sort_by?: string;
starts_with?: string;
token?: string;
version?: StoryblokContentVersionKeys;
with_tag?: string;
}
interface ISbStoryParams {
resolve_level?: number;
token?: string;
find_by?: 'uuid';
version?: StoryblokContentVersionKeys;
resolve_assets?: number;
resolve_links?: 'link' | 'url' | 'story' | '0' | '1';
resolve_links_level?: 1 | 2;
resolve_relations?: string | string[];
cv?: number;
from_release?: string;
language?: string;
fallback_lang?: string;
}
interface Dimension {
id: number;
name: string;
entry_value: string;
datasource_id: number;
created_at: string;
updated_at: string;
}
/**
* @interface ISbDimensions
* @description Storyblok Dimensions Interface auxiliary interface
* @description One use it to handle the API response
*/
interface ISbDimensions {
dimensions: Dimension[];
}
interface ISbComponentType<T extends string> {
_uid?: string;
component?: T;
_editable?: string;
}
interface PreviewToken {
token: string;
timestamp: string;
}
interface LocalizedPath {
path: string;
name: string | null;
lang: string;
published: boolean;
}
interface ISbStoryData<Content = ISbComponentType<string> & {
[index: string]: any;
}> extends ISbMultipleStoriesData {
alternates: ISbAlternateObject[];
breadcrumbs?: ISbLinkURLObject[];
content: Content;
created_at: string;
deleted_at?: string;
default_full_slug?: string | null;
default_root?: string;
disable_fe_editor?: boolean;
favourite_for_user_ids?: number[] | null;
first_published_at?: string | null;
full_slug: string;
group_id: string;
id: number;
imported_at?: string;
is_folder?: boolean;
is_startpage?: boolean;
lang: string;
last_author?: {
id: number;
userid: string;
};
last_author_id?: number;
localized_paths?: LocalizedPath[] | null;
meta_data: any;
name: string;
parent?: ISbStoryData;
parent_id: number | null;
path?: string;
pinned?: '1' | boolean;
position: number;
preview_token?: PreviewToken;
published?: boolean;
published_at: string | null;
release_id?: number | null;
scheduled_date?: string | null;
slug: string;
sort_by_date: string | null;
tag_list: string[];
translated_slugs?: {
path: string;
name: string | null;
lang: ISbStoryData['lang'];
published: boolean;
}[] | null;
unpublished_changes?: boolean;
updated_at?: string;
uuid: string;
}
interface ISbMultipleStoriesData {
by_ids?: string;
by_uuids?: string;
contain_component?: string;
excluding_ids?: string;
filter_query?: any;
folder_only?: boolean;
full_slug?: string;
in_release?: string;
in_trash?: boolean;
is_published?: boolean;
in_workflow_stages?: string;
page?: number;
pinned?: '1' | boolean;
search?: string;
sort_by?: string;
starts_with?: string;
story_only?: boolean;
text_search?: string;
with_parent?: number;
with_slug?: string;
with_tag?: string;
}
interface ISbAlternateObject {
id: number;
name: string;
slug: string;
published: boolean;
full_slug: string;
is_folder: boolean;
parent_id: number;
}
interface ISbLinkURLObject {
id: number;
name: string;
slug: string;
full_slug: string;
url: string;
uuid: string;
}
interface ISbStories<Content = ISbComponentType<string> & {
[index: string]: any;
}> {
data: {
cv: number;
links: (ISbStoryData | ISbLinkURLObject)[];
rels: ISbStoryData[];
stories: ISbStoryData<Content>[];
};
perPage: number;
total: number;
headers: any;
}
interface ISbStory<Content = ISbComponentType<string> & {
[index: string]: any;
}> {
data: {
cv: number;
links: (ISbStoryData | ISbLinkURLObject)[];
rels: ISbStoryData[];
story: ISbStoryData<Content>;
};
headers: any;
}
interface IMemoryType extends ISbResult {
[key: string]: any;
}
interface ICacheProvider {
get: (key: string) => Promise<IMemoryType | void>;
set: (key: string, content: ISbResult) => Promise<void>;
getAll: () => Promise<IMemoryType | void>;
flush: () => Promise<void>;
}
interface ISbCache {
type?: 'none' | 'memory' | 'custom';
clear?: 'auto' | 'manual' | 'onpreview';
custom?: ICacheProvider;
}
interface ISbConfig {
accessToken?: string;
oauthToken?: string;
resolveNestedRelations?: boolean;
cache?: ISbCache;
responseInterceptor?: ResponseFn;
fetch?: typeof fetch;
timeout?: number;
headers?: any;
region?: string;
maxRetries?: number;
https?: boolean;
rateLimit?: number;
endpoint?: string;
version?: StoryblokContentVersionKeys | undefined;
inlineAssets?: boolean;
}
interface ISbResult {
data: any;
perPage: number;
total: number;
headers: Headers;
}
interface ISbLinksResult extends ISbResult {
data: ISbLinks;
}
interface ISbResponse {
data: any;
status: number;
statusText: string;
headers: any;
}
interface ISbError {
message?: string;
status?: number;
response?: ISbResponse;
}
interface ISbContentMangmntAPI<Content = ISbComponentType<string> & {
[index: string]: any;
}> {
story: {
name: string;
slug: string;
content?: Content;
default_root?: string;
is_folder?: boolean;
parent_id?: number;
disable_fe_editor?: boolean;
path?: string;
is_startpage?: boolean;
position?: number;
first_published_at?: string;
sort_by_date?: string;
translated_slugs_attributes?: {
path: string;
name: string | null;
lang: ISbContentMangmntAPI['lang'];
published: boolean;
}[];
};
force_update?: '1' | unknown;
release_id?: number;
publish?: '1' | unknown;
lang?: string;
}
interface ISbManagmentApiResult {
data: any;
headers: any;
}
interface ISbSchema {
nodes: any;
marks: any;
}
interface LinkCustomAttributes {
rel?: string;
title?: string;
[key: string]: any;
}
interface ISbLink {
id?: number;
slug?: string;
name?: string;
is_folder?: boolean;
parent_id?: number;
published?: boolean;
position?: number;
uuid?: string;
is_startpage?: boolean;
path?: string;
real_path?: string;
published_at?: string;
created_at?: string;
updated_at?: string;
}
interface ISbLinksParams {
starts_with?: string;
version?: StoryblokContentVersionKeys;
paginated?: number;
per_page?: number;
page?: number;
sort_by?: string;
include_dates?: 0 | 1;
with_parent?: number;
}
interface ISbLinks {
links?: {
[key: string]: ISbLink;
};
}
interface Queue<T> {
resolve: (value: unknown) => void;
reject: (reason?: unknown) => void;
args: T;
}
interface ISbResponseData {
link_uuids: string[];
links: string[];
rel_uuids?: string[];
rels: any;
story: ISbStoryData;
stories: Array<ISbStoryData>;
}
interface ISbThrottle<T extends (...args: Parameters<T>) => ReturnType<T>> {
abort?: () => void;
(...args: Parameters<T>): Promise<unknown>;
}
type ISbThrottledRequest = (type: Method, url: string, params: ISbStoriesParams, fetchOptions?: ISbCustomFetch) => Promise<unknown>;
type AsyncFn = (...args: any) => [] | Promise<ISbResult>;
type ArrayFn = (...args: any) => void;
interface HtmlEscapes {
[key: string]: string;
}
interface ISbCustomFetch extends Omit<RequestInit, 'method'> {}
interface ISbAssetsParams {
in_folder?: string;
is_private?: boolean;
by_alt?: string;
by_copyright?: string;
by_title?: string;
}
interface ISbField {
fieldtype: string;
id: string;
[key: string]: unknown;
}
//#endregion
//#region src/index.d.ts
interface CachedVersions {
[key: string]: number;
}
interface LinksType {
[key: string]: any;
}
interface RelationsType {
[key: string]: any;
}
declare class Storyblok {
private client;
private maxRetries;
private retriesDelay;
private throttle;
private accessToken;
private cache;
private resolveCounter;
relations: RelationsType;
links: LinksType;
version: StoryblokContentVersionKeys | undefined;
/**
* @deprecated This property is deprecated. Use the standalone `richTextResolver` from `@storyblok/richtext` instead.
* @see https://github.com/storyblok/richtext
*/
richTextResolver: unknown;
resolveNestedRelations: boolean;
private stringifiedStoriesCache;
private inlineAssets;
/**
*
* @param config ISbConfig interface
* @param pEndpoint string, optional
*/
constructor(config: ISbConfig, pEndpoint?: string);
private parseParams;
private factoryParamOptions;
private makeRequest;
get(slug: 'cdn/links', params?: ISbLinksParams, fetchOptions?: ISbCustomFetch): Promise<ISbLinksResult>;
get(slug: string, params?: ISbStoriesParams, fetchOptions?: ISbCustomFetch): Promise<ISbResult>;
getAll(slug: string, params?: ISbStoriesParams, entity?: string, fetchOptions?: ISbCustomFetch): Promise<any[]>;
post(slug: string, params?: ISbStoriesParams | ISbContentMangmntAPI, fetchOptions?: ISbCustomFetch): Promise<ISbResponseData>;
put(slug: string, params?: ISbStoriesParams | ISbContentMangmntAPI, fetchOptions?: ISbCustomFetch): Promise<ISbResponseData>;
delete(slug: string, params?: ISbStoriesParams | ISbContentMangmntAPI, fetchOptions?: ISbCustomFetch): Promise<ISbResponseData>;
getStories(params?: ISbStoriesParams, fetchOptions?: ISbCustomFetch): Promise<ISbStories>;
getStory(slug: string, params?: ISbStoryParams, fetchOptions?: ISbCustomFetch): Promise<ISbStory>;
private getToken;
ejectInterceptor(): void;
private _addResolveLevel;
private _cleanCopy;
private _insertLinks;
/**
*
* @param resolveId A counter number as a string
* @param uuid The uuid of the story
* @returns string | object
*/
private getStoryReference;
/**
* Resolves a field's value by replacing UUIDs with their corresponding story references
* @param jtree - The JSON tree object containing the field to resolve
* @param treeItem - The key of the field to resolve
* @param resolveId - The unique identifier for the current resolution context
*
* This method handles both single string UUIDs and arrays of UUIDs:
* - For single strings: directly replaces the UUID with the story reference
* - For arrays: maps through each UUID and replaces with corresponding story references
*/
private _resolveField;
/**
* Inserts relations into the JSON tree by resolving references
* @param jtree - The JSON tree object to process
* @param treeItem - The current field being processed
* @param fields - The relation patterns to resolve (string or array of strings)
* @param resolveId - The unique identifier for the current resolution context
*
* This method handles two types of relation patterns:
* 1. Nested relations: matches fields that end with the current field name
* Example: If treeItem is "event_type", it matches patterns like "*.event_type"
*
* 2. Direct component relations: matches exact component.field patterns
* Example: "event.event_type" for component "event" and field "event_type"
*
* The method supports both string and array formats for the fields parameter,
* allowing flexible specification of relation patterns.
*/
private _insertRelations;
/**
* Recursively traverses and resolves relations in the story content tree
* @param story - The story object containing the content to process
* @param fields - The relation patterns to resolve
* @param resolveId - The unique identifier for the current resolution context
*/
private iterateTree;
private resolveLinks;
private resolveRelations;
/**
*
* @param responseData
* @param params
* @param resolveId
* @description Resolves the relations and links of the stories
* @returns Promise<void>
*
*/
private resolveStories;
private cacheResponse;
private throttledRequest;
cacheVersions(): CachedVersions;
cacheVersion(): number;
setCacheVersion(cv: number): void;
clearCacheVersion(): void;
cacheProvider(): ICacheProvider;
flushCache(): Promise<this>;
private processInlineAssets;
}
//#endregion
export { ArrayFn, AsyncFn, HtmlEscapes, ICacheProvider, IMemoryType, ISbAlternateObject, ISbAssetsParams, ISbCache, ISbComponentType, ISbConfig, ISbContentMangmntAPI, ISbCustomFetch, ISbDimensions, ISbError, ISbField, ISbLink, ISbLinkURLObject, ISbLinks, ISbLinksParams, ISbLinksResult, ISbManagmentApiResult, ISbMultipleStoriesData, ISbResponse, ISbResponseData, ISbResult, ISbSchema, ISbStories, ISbStoriesParams, ISbStory, ISbStoryData, ISbStoryParams, ISbThrottle, ISbThrottledRequest, LinkCustomAttributes, LocalizedPath, PreviewToken, Queue, Storyblok, Storyblok as default };
//# sourceMappingURL=index.d.cts.map