Skip to content

Commit fb500eb

Browse files
chore: replace js-client with new api client
1 parent 4eb85d3 commit fb500eb

File tree

12 files changed

+61
-106
lines changed

12 files changed

+61
-106
lines changed

packages/angular/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
"@angular/router": "^21.1.0",
3535
"@angular/ssr": "^21.1.2",
3636
"@storyblok/live-preview": "workspace:*",
37-
"storyblok-js-client": "workspace:*",
3837
"@storyblok/richtext": "workspace:*",
3938
"@storyblok/js": "workspace:*",
39+
"@storyblok/api-client": "workspace:*",
4040
"express": "^5.1.0",
4141
"rxjs": "~7.8.0",
4242
"tslib": "^2.3.0"

packages/angular/projects/sdk/ng-package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
"entryFile": "src/public-api.ts"
66
},
77
"allowedNonPeerDependencies": [
8-
"storyblok-js-client",
98
"@storyblok/live-preview",
109
"@storyblok/richtext",
1110
"@storyblok/js",
12-
"@storyblok/preview-bridge"
11+
"@storyblok/api-client"
1312
]
1413
}

packages/angular/projects/sdk/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
"dependencies": {
2727
"tslib": "^2.8.1",
2828
"@storyblok/live-preview": "workspace:*",
29-
"storyblok-js-client": "workspace:*",
3029
"@storyblok/richtext": "workspace:*",
31-
"@storyblok/js": "workspace:*"
30+
"@storyblok/js": "workspace:*",
31+
"@storyblok/api-client": "workspace:*"
3232
},
3333
"sideEffects": false
3434
}

packages/angular/projects/sdk/project.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
},
1010
"implicitDependencies": [
1111
"live-preview",
12-
"storyblok-js-client",
1312
"richtext",
14-
"js"
13+
"js",
14+
"api-client"
1515
]
1616
}

packages/angular/projects/sdk/src/lib/storyblok.feature.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { ISbConfig } from 'storyblok-js-client';
21
import { StoryblokFeature } from './components.feature';
32
import {
43
EnvironmentProviders,
54
inject,
65
makeEnvironmentProviders,
76
provideAppInitializer,
87
} from '@angular/core';
9-
import { STORYBLOK_CONFIG, StoryblokService } from './storyblok.service';
8+
import { STORYBLOK_CONFIG, StoryblokClientConfig, StoryblokService } from './storyblok.service';
109

1110
/**
1211
* Provides Storyblok configuration at the application level.
@@ -34,7 +33,7 @@ import { STORYBLOK_CONFIG, StoryblokService } from './storyblok.service';
3433
* ```
3534
*/
3635
export function provideStoryblok(
37-
config: ISbConfig,
36+
config: StoryblokClientConfig,
3837
...features: StoryblokFeature[]
3938
): EnvironmentProviders {
4039
return makeEnvironmentProviders([
Lines changed: 17 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,35 @@
11
import { Injectable, InjectionToken } from '@angular/core';
2-
import StoryblokClient, {
3-
type ISbConfig,
4-
type ISbStoryData,
5-
type ISbStoriesParams,
6-
} from 'storyblok-js-client';
2+
import type { ContentApiClientConfig } from '@storyblok/api-client';
3+
import { createApiClient } from '@storyblok/api-client';
74

8-
/**
9-
* Injection token for Storyblok configuration
10-
*/
11-
export const STORYBLOK_CONFIG = new InjectionToken<ISbConfig>('STORYBLOK_CONFIG');
12-
13-
/**
14-
* Core service for interacting with the Storyblok API.
15-
* Provides methods for fetching stories and integrating with the Visual Editor.
16-
*
17-
* @example
18-
* ```typescript
19-
* export class MyComponent {
20-
* private storyblok = inject(StoryblokService);
21-
*
22-
* async loadContent() {
23-
* const story = await this.storyblok.getStory('home');
24-
* }
25-
* }
26-
* ```
27-
*/
5+
export type StoryblokClientConfig = ContentApiClientConfig<boolean, boolean>;
6+
export const STORYBLOK_CONFIG = new InjectionToken<StoryblokClientConfig>('STORYBLOK_CONFIG');
7+
type StoryblokClient = ReturnType<typeof createApiClient>;
288
@Injectable({
299
providedIn: 'root',
3010
})
3111
export class StoryblokService {
32-
private storyblok: StoryblokClient | null = null;
12+
private client: StoryblokClient | null = null;
3313
private initialized = false;
3414

3515
/**
36-
* @internal
37-
* Initialize Storyblok SDK with the provided configuration.
38-
* This is called automatically by `provideStoryblok()`.
16+
* Initialize Storyblok API client with config
3917
*/
40-
/** @internal */
41-
ɵinit(config: ISbConfig): void {
42-
if (this.initialized) {
43-
return;
44-
}
45-
const storyblok = new StoryblokClient(config);
46-
this.storyblok = storyblok;
18+
ɵinit(config: StoryblokClientConfig): void {
19+
if (this.initialized) return;
20+
this.client = createApiClient(config);
4721
this.initialized = true;
4822
}
4923

5024
/**
51-
* Fetch a single story by its slug.
52-
*
53-
* @param slug - The story slug (e.g., 'home', 'about', 'blog/my-post')
54-
* @param options - API options for resolving relations and links
55-
* @returns The story data or null if not found
25+
* Get the initialized API client instance
5626
*/
57-
async getStory(slug: string, options: ISbStoriesParams = {}): Promise<ISbStoryData | null> {
58-
if (!this.storyblok) {
59-
console.error('Storyblok API not initialized. Call init() first.');
60-
return null;
61-
}
62-
try {
63-
const response = await this.storyblok.get(`cdn/stories/${slug}`, {
64-
version: options.version ?? 'draft',
65-
resolve_relations: options.resolve_relations,
66-
resolve_links: options.resolve_links,
67-
});
68-
69-
return response.data?.story ?? null;
70-
} catch (error) {
71-
console.error(`Failed to fetch story: ${slug}`, error);
72-
return null;
27+
getClient(): StoryblokClient {
28+
if (!this.client) {
29+
throw new Error(
30+
'Storyblok API client not initialized. Did you forget to call provideStoryblok()?',
31+
);
7332
}
33+
return this.client;
7434
}
7535
}

packages/angular/projects/sdk/src/public-api.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
*/
44

55
// Core service and provider
6-
export { StoryblokService, STORYBLOK_CONFIG } from './lib/storyblok.service';
6+
export {
7+
StoryblokService,
8+
STORYBLOK_CONFIG,
9+
type StoryblokClientConfig,
10+
} from './lib/storyblok.service';
711
export { provideStoryblok } from './lib/storyblok.feature';
812
// Component registry
913
export {
@@ -34,14 +38,6 @@ export {
3438
type StoryblokRichTextOptions,
3539
} from '@storyblok/richtext';
3640

37-
// Re-export useful types from @storyblok/js
38-
export type {
39-
ISbStoriesParams,
40-
ISbStoryData,
41-
ISbResult,
42-
ISbStory,
43-
ISbConfig,
44-
ISbComponentType,
45-
} from 'storyblok-js-client';
46-
47-
export type { SbBlokData } from '@storyblok/js';
41+
export type { SbBlokData, ISbStoryData } from '@storyblok/js';
42+
43+
export type { Story } from '@storyblok/api-client';

packages/angular/projects/ssr-playground/src/app/app.config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import {
66
provideStoryblok,
77
withLivePreview,
88
withStoryblokComponents,
9-
type ISbConfig,
9+
type StoryblokClientConfig,
1010
} from '@storyblok/angular';
1111
import { storyblokComponents } from './storyblok.components';
1212

13-
const sbConfig: ISbConfig = {
13+
const sbConfig: StoryblokClientConfig = {
1414
accessToken: 'nVjy7VdMK6ObZxyDfTWNwgtt',
1515
region: 'eu',
16+
inlineRelations: true,
1617
};
1718
export const appConfig: ApplicationConfig = {
1819
providers: [

packages/angular/projects/ssr-playground/src/app/app.routes.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@ export const routes: Routes = [
1414
loadComponent: () =>
1515
import('./routes/catch-all/catch-all.component').then((m) => m.CatchAllComponent),
1616
resolve: {
17-
story: (route: ActivatedRouteSnapshot) => {
17+
story: async (route: ActivatedRouteSnapshot) => {
1818
const slug = route.url.map((s) => s.path).join('/') || 'home';
19-
return inject(StoryblokService).getStory(slug, {
20-
version: 'draft',
21-
resolve_relations: 'feature_posts.posts',
19+
const client = inject(StoryblokService).getClient();
20+
const { data } = await client.stories.get(slug, {
21+
query: {
22+
version: 'draft',
23+
resolve_relations: 'feature_posts.posts',
24+
},
2225
});
26+
return data?.story;
2327
},
2428
},
2529
},

packages/angular/projects/ssr-playground/src/app/components/teaser/teaser.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
type SbBlokData,
55
type StoryblokRichTextNode,
66
} from '@storyblok/angular';
7-
import type { StoryblokRichTextOptions } from '@storyblok/richtext';
87

98
export interface TeaserBlok extends SbBlokData {
109
headline?: string;
@@ -18,20 +17,10 @@ export interface TeaserBlok extends SbBlokData {
1817
template: `
1918
<div>
2019
<h2>{{ blok().headline }}</h2>
21-
<sb-rich-text [doc]="blok().text" [options]="richTextOptions" />
20+
<sb-rich-text [doc]="blok().text" />
2221
</div>
2322
`,
2423
})
2524
export class TeaserComponent {
2625
readonly blok = input.required<TeaserBlok>();
27-
28-
/** Custom rich text configuration */
29-
readonly richTextOptions: StoryblokRichTextOptions<string> = {
30-
tiptapExtensions: {
31-
link: (node: any) => {
32-
console.log(node);
33-
return `<p>This is a custom link</p>`;
34-
},
35-
},
36-
};
3726
}

0 commit comments

Comments
 (0)