Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add generic types to get story functions #854

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,24 +224,24 @@ class Storyblok {
return this.cacheResponse(url, query, undefined, fetchOptions)
}

public get(
public get<T = any>(
slug: string,
params?: ISbStoriesParams,
fetchOptions?: ISbCustomFetch
): Promise<ISbResult> {
): Promise<ISbResult<T>> {
if (!params) params = {} as ISbStoriesParams
const url = `/${slug}`
const query = this.factoryParamOptions(url, params)

return this.cacheResponse(url, query, undefined, fetchOptions)
}

public async getAll(
public async getAll<T = any>(
slug: string,
params: ISbStoriesParams,
entity?: string,
fetchOptions?: ISbCustomFetch
): Promise<any[]> {
): Promise<T[]> {
const perPage = params?.per_page || 25
const url = `/${slug}`
const urlParts = url.split('/')
Expand Down Expand Up @@ -299,20 +299,20 @@ class Storyblok {
return Promise.resolve(this.throttle('delete', url, params, fetchOptions))
}

public getStories(
public getStories<T = any>(
params: ISbStoriesParams,
fetchOptions?: ISbCustomFetch
): Promise<ISbStories> {
): Promise<ISbStories<T>> {
this._addResolveLevel(params)

return this.get('cdn/stories', params, fetchOptions)
}

public getStory(
public getStory<T = any>(
slug: string,
params: ISbStoryParams,
fetchOptions?: ISbCustomFetch
): Promise<ISbStory> {
): Promise<ISbStory<T>> {
this._addResolveLevel(params)

return this.get(`cdn/stories/${slug}`, params, fetchOptions)
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ export interface ISbConfig {
endpoint?: string
}

export interface ISbResult {
data: any
export interface ISbResult<T = any> {
data: T
perPage: number
total: number
headers: Headers
Expand Down