@flyo/nitro-next 1.12.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/server.d.ts CHANGED
@@ -1,55 +1,21 @@
1
1
  import * as react from 'react';
2
2
  import * as react_jsx_runtime from 'react/jsx-runtime';
3
- import { Metadata, MetadataRoute } from 'next';
4
- import { Entity, Block, ConfigResponse, Page, Configuration, EntitiesApi, PagesApi, SearchApi, SitemapApi } from '@flyo/nitro-typescript';
3
+ import { MetadataRoute, Metadata } from 'next';
4
+ import { Entity, ConfigResponse, PagesApi, EntitiesApi, SitemapApi, SearchApi, Page, Block } from '@flyo/nitro-typescript';
5
5
 
6
6
  /**
7
- * Interface for Nitro configuration state
7
+ * Read-only configuration state
8
8
  */
9
9
  interface NitroState {
10
- configuration: Configuration | null;
11
- accessToken: string | null;
12
- lang: string | null;
13
- baseUrl: string | null;
14
- components: Record<string, any>;
15
- showMissingComponentAlert: boolean;
16
- liveEdit: boolean;
17
- serverCacheTtl: number;
18
- clientCacheTtl: number;
10
+ readonly accessToken: string;
11
+ readonly lang: string | null;
12
+ readonly baseUrl: string | null;
13
+ readonly components: Record<string, any>;
14
+ readonly showMissingComponentAlert: boolean;
15
+ readonly liveEdit: boolean;
16
+ readonly serverCacheTtl: number;
17
+ readonly clientCacheTtl: number;
19
18
  }
20
- /**
21
- * Global Nitro state - shared across server and middleware
22
- */
23
- declare const globalNitroState: NitroState;
24
- /**
25
- * Access the Nitro configuration state
26
- * Can be used anywhere: server components, middlewares, API routes, etc.
27
- * Must be called after initNitro() has been initialized.
28
- *
29
- * @throws {Error} If Nitro has not been initialized with initNitro()
30
- *
31
- * @example
32
- * ```ts
33
- * const state = getNitro();
34
- * const { configuration, lang, components } = state;
35
- * ```
36
- */
37
- declare function getNitro(): NitroState;
38
- declare const initNitro: ({ accessToken, lang, baseUrl, components, showMissingComponentAlert, liveEdit, serverCacheTtl, clientCacheTtl, }: {
39
- accessToken: string;
40
- lang?: string;
41
- baseUrl?: string;
42
- components?: object;
43
- showMissingComponentAlert?: boolean;
44
- liveEdit?: boolean;
45
- serverCacheTtl?: number;
46
- clientCacheTtl?: number;
47
- }) => (() => NitroState);
48
- declare const getNitroConfig: () => Promise<ConfigResponse>;
49
- declare function getNitroPages(): PagesApi;
50
- declare function getNitroEntities(): EntitiesApi;
51
- declare function getNitroSitemap(): SitemapApi;
52
- declare function getNitroSearch(): SearchApi;
53
19
  /**
54
20
  * Route params type for Next.js catch-all routes
55
21
  */
@@ -60,229 +26,210 @@ type RouteParams = {
60
26
  };
61
27
  /**
62
28
  * Generic route params type for entity routes
63
- * Allows any param structure from Next.js app router
64
29
  */
65
30
  type EntityRouteParams<T = any> = {
66
31
  params: Promise<T>;
67
32
  };
68
- /**
69
- * Resolve a Nitro page from route params
70
- * Uses React cache to avoid duplicate fetching.
71
- * Use this when you need access to the page data for custom rendering logic.
72
- *
73
- * @example
74
- * ```tsx
75
- * // app/[[...slug]]/page.tsx
76
- * import { nitroPageResolveRoute, NitroPage } from '@flyo/nitro-next/server';
77
- *
78
- * export default async function Page(props: { params: Promise<{ slug?: string[] }> }) {
79
- * const { page, cfg } = await nitroPageResolveRoute(props);
80
- * return <NitroPage page={page} />;
81
- * }
82
- * ```
83
- */
84
- declare const nitroPageResolveRoute: ({ params }: RouteParams) => Promise<{
85
- page: Page;
86
- path: string;
87
- cfg: ConfigResponse;
88
- }>;
89
33
  /**
90
34
  * Entity resolver function type
91
35
  * Users provide this to resolve entities from their route params
92
36
  */
93
37
  type EntityResolver<T = any> = (params: Promise<T>) => Promise<Entity>;
94
38
  /**
95
- * NitroDebugInfo Component
39
+ * The Flyo instance returned by initNitro().
40
+ * Carries all API access methods and configuration state.
41
+ */
42
+ interface FlyoInstance {
43
+ /** Read-only configuration state */
44
+ readonly state: NitroState;
45
+ /** Fetch and cache the Nitro CMS configuration (React-cached per request) */
46
+ getNitroConfig(): Promise<ConfigResponse>;
47
+ /** Create a PagesApi client */
48
+ getNitroPages(): PagesApi;
49
+ /** Create an EntitiesApi client */
50
+ getNitroEntities(): EntitiesApi;
51
+ /** Create a SitemapApi client */
52
+ getNitroSitemap(): SitemapApi;
53
+ /** Create a SearchApi client */
54
+ getNitroSearch(): SearchApi;
55
+ /** Resolve a page from catch-all route params (React-cached per request) */
56
+ pageResolveRoute(props: RouteParams): Promise<{
57
+ page: Page;
58
+ path: string;
59
+ cfg: ConfigResponse;
60
+ }>;
61
+ /** Generate a Next.js sitemap from Flyo Nitro content */
62
+ sitemap(): Promise<MetadataRoute.Sitemap>;
63
+ }
64
+ /**
65
+ * Initialize and return a FlyoInstance.
66
+ *
67
+ * Call once in your flyo.config.tsx and export the result.
68
+ * The returned object carries all methods bound to the configuration,
69
+ * eliminating the need for global state.
96
70
  *
97
- * Outputs debug information about the current Nitro/Flyo setup as an HTML comment.
98
- * This includes environment info, API version, token type, deployment details, etc.
71
+ * @example
72
+ * ```tsx
73
+ * // flyo.config.tsx
74
+ * import { initNitro } from '@flyo/nitro-next/server';
99
75
  *
100
- * Usage: Add <NitroDebugInfo config={config} /> to your layout to include debug info in the HTML output.
76
+ * export const flyo = initNitro({
77
+ * accessToken: process.env.FLYO_ACCESS_TOKEN || '',
78
+ * lang: 'en',
79
+ * baseUrl: process.env.SITE_URL || 'http://localhost:3000',
80
+ * liveEdit: process.env.FLYO_LIVE_EDIT === 'true',
81
+ * components: { HeroBanner, Text },
82
+ * });
83
+ * ```
101
84
  */
102
- declare function NitroDebugInfo({ config }: {
103
- config: ConfigResponse;
104
- }): react_jsx_runtime.JSX.Element;
85
+ declare function initNitro({ accessToken, lang, baseUrl, components, showMissingComponentAlert, liveEdit, serverCacheTtl, clientCacheTtl, }: {
86
+ accessToken: string;
87
+ lang?: string;
88
+ baseUrl?: string;
89
+ components?: Record<string, any>;
90
+ showMissingComponentAlert?: boolean;
91
+ liveEdit?: boolean;
92
+ serverCacheTtl?: number;
93
+ clientCacheTtl?: number;
94
+ }): FlyoInstance;
105
95
  /**
106
- * Renders a JSON-LD structured data script tag from an Entity's jsonld field.
107
- * Safely escapes HTML entities to prevent XSS attacks.
108
- * Returns null if the entity has no jsonld data.
96
+ * NitroDebugInfo Component
97
+ *
98
+ * Async server component that outputs debug information as an HTML comment.
99
+ * Resolves config internally from the flyo instance.
109
100
  *
110
101
  * @example
111
102
  * ```tsx
112
- * import { NitroEntityJsonLd } from '@flyo/nitro-next/server';
113
- *
114
- * export default function BlogPost({ entity }: { entity: Entity }) {
115
- * return (
116
- * <>
117
- * <NitroEntityJsonLd entity={entity} />
118
- * <h1>{entity.entity?.entity_title}</h1>
119
- * </>
120
- * );
121
- * }
103
+ * <NitroDebugInfo flyo={flyo} />
122
104
  * ```
123
105
  */
106
+ declare function NitroDebugInfo({ flyo }: {
107
+ flyo: FlyoInstance;
108
+ }): Promise<react_jsx_runtime.JSX.Element>;
109
+ /**
110
+ * Renders a JSON-LD structured data script tag from an Entity's jsonld field.
111
+ * Safely escapes HTML to prevent XSS.
112
+ */
124
113
  declare function NitroEntityJsonLd({ entity }: {
125
114
  entity: Entity;
126
115
  }): react_jsx_runtime.JSX.Element | null;
127
116
  /**
128
- * NitroPage component renders all blocks from a Flyo page
117
+ * NitroPage renders all blocks from a Flyo page.
129
118
  */
130
- declare function NitroPage({ page, }: {
119
+ declare function NitroPage({ page, flyo, }: {
131
120
  page: Page;
121
+ flyo: FlyoInstance;
132
122
  }): react_jsx_runtime.JSX.Element | null;
133
- declare function NitroBlock({ block, }: {
123
+ /**
124
+ * NitroBlock renders a single block using the registered component.
125
+ */
126
+ declare function NitroBlock({ block, flyo, }: {
134
127
  block: Block;
128
+ flyo: FlyoInstance;
135
129
  }): react_jsx_runtime.JSX.Element | null;
136
130
  /**
137
- * NitroSlot component renders nested blocks from a slot
138
- * Used for recursive block rendering when blocks contain slots
131
+ * NitroSlot renders nested blocks from a slot.
132
+ * Used for recursive block rendering when blocks contain slots.
139
133
  *
140
134
  * @example
141
135
  * ```tsx
136
+ * import { flyo } from '@/flyo.config';
142
137
  * import { NitroSlot } from '@flyo/nitro-next/server';
143
138
  *
144
- * export default function MyComponent({ block }) {
139
+ * export function MyComponent({ block }) {
145
140
  * return (
146
141
  * <div>
147
- * <NitroSlot slot={block.slots.mysuperslotname} />
142
+ * <NitroSlot slot={block.slots?.content} flyo={flyo} />
148
143
  * </div>
149
144
  * );
150
145
  * }
151
146
  * ```
152
147
  */
153
- declare function NitroSlot({ slot, }: {
148
+ declare function NitroSlot({ slot, flyo, }: {
154
149
  slot?: {
155
150
  content?: Block[];
156
151
  };
152
+ flyo: FlyoInstance;
157
153
  }): react_jsx_runtime.JSX.Element | null;
158
154
  /**
159
- * Default page route handler for Nitro pages
160
- * Can be re-exported directly from Next.js app routes
155
+ * Create a page route handler for Nitro pages.
156
+ * Returns a Next.js page component function.
161
157
  *
162
158
  * @example
163
- * ```ts
164
- * // app/[[...slug]]/page.tsx
165
- * export { nitroPageRoute as default } from '@flyo/nitro-next/server';
159
+ * ```tsx
160
+ * import { flyo } from '@/flyo.config';
161
+ * import { nitroPageRoute } from '@flyo/nitro-next/server';
162
+ *
163
+ * export default nitroPageRoute(flyo);
166
164
  * ```
167
165
  */
168
- declare function nitroPageRoute(props: RouteParams): Promise<react_jsx_runtime.JSX.Element>;
166
+ declare function nitroPageRoute(flyo: FlyoInstance): (props: RouteParams) => Promise<react_jsx_runtime.JSX.Element>;
169
167
  /**
170
- * Generate metadata for Nitro pages
171
- * Provides basic meta tags based on Flyo page data
172
- * Can be re-exported directly from Next.js app routes
168
+ * Create a metadata generator for Nitro pages.
169
+ * Returns a Next.js generateMetadata function.
173
170
  *
174
171
  * @example
175
- * ```ts
176
- * // app/[[...slug]]/page.tsx
177
- * export { nitroPageGenerateMetadata as generateMetadata } from '@flyo/nitro-next/server';
172
+ * ```tsx
173
+ * import { flyo } from '@/flyo.config';
174
+ * import { nitroPageGenerateMetadata } from '@flyo/nitro-next/server';
175
+ *
176
+ * export const generateMetadata = nitroPageGenerateMetadata(flyo);
178
177
  * ```
179
178
  */
180
- declare function nitroPageGenerateMetadata(props: RouteParams): Promise<Metadata>;
179
+ declare function nitroPageGenerateMetadata(flyo: FlyoInstance): (props: RouteParams) => Promise<Metadata>;
181
180
  /**
182
- * Generate static params for all Nitro pages
183
- * Enables static site generation (SSG) for all pages
184
- * Can be re-exported directly from Next.js app routes
181
+ * Create a static params generator for Nitro pages.
182
+ * Returns a Next.js generateStaticParams function.
185
183
  *
186
184
  * @example
187
- * ```ts
188
- * // app/[[...slug]]/page.tsx
189
- * export { nitroPageGenerateStaticParams as generateStaticParams } from '@flyo/nitro-next/server';
185
+ * ```tsx
186
+ * import { flyo } from '@/flyo.config';
187
+ * import { nitroPageGenerateStaticParams } from '@flyo/nitro-next/server';
188
+ *
189
+ * export const generateStaticParams = nitroPageGenerateStaticParams(flyo);
190
190
  * ```
191
191
  */
192
- declare function nitroPageGenerateStaticParams(): Promise<{
192
+ declare function nitroPageGenerateStaticParams(flyo: FlyoInstance): () => Promise<{
193
193
  slug: string[] | undefined;
194
194
  }[]>;
195
195
  /**
196
- * Default entity route handler with custom resolver
197
- * Flexible solution that works with any route param structure
196
+ * Create an entity route handler with a custom resolver.
197
+ * Returns a Next.js page component function.
198
198
  *
199
199
  * @example
200
- * ```ts
201
- * // app/blog/[slug]/page.tsx
202
- * const resolver = async (params: Promise<{ slug: string }>) => {
200
+ * ```tsx
201
+ * import { flyo } from '@/flyo.config';
202
+ * import { nitroEntityRoute } from '@flyo/nitro-next/server';
203
+ *
204
+ * const resolver = async (params) => {
203
205
  * const { slug } = await params;
204
- * return getNitroEntities().entityBySlug({ slug, typeId: 123 });
206
+ * return flyo.getNitroEntities().entityBySlug({ slug, typeId: 123 });
205
207
  * };
206
208
  *
207
- * export default (props) => nitroEntityRoute(props, {
209
+ * export default nitroEntityRoute(flyo, {
208
210
  * resolver,
209
- * render: (entity) => <h1>{entity.entity?.entity_title}</h1>
211
+ * render: (entity) => <h1>{entity.entity?.entity_title}</h1>,
210
212
  * });
211
213
  * ```
212
- *
213
- * @example
214
- * ```ts
215
- * // app/items/[uniqueid]/page.tsx
216
- * const resolver = async (params: Promise<{ uniqueid: string }>) => {
217
- * const { uniqueid } = await params;
218
- * return getNitroEntities().entityByUniqueid({ uniqueid });
219
- * };
220
- *
221
- * export default (props) => nitroEntityRoute(props, { resolver });
222
- * ```
223
- *
224
- * @example
225
- * ```ts
226
- * // app/custom/[whatever]/page.tsx
227
- * const resolver = async (params: Promise<{ whatever: string }>) => {
228
- * const { whatever } = await params;
229
- * return getNitroEntities().entityBySlug({ slug: whatever });
230
- * };
231
- *
232
- * export default (props) => nitroEntityRoute(props, { resolver });
233
- * ```
234
214
  */
235
- declare function nitroEntityRoute<T = any>(props: EntityRouteParams<T>, options: {
215
+ declare function nitroEntityRoute<T = any>(flyo: FlyoInstance, options: {
236
216
  resolver: EntityResolver<T>;
237
217
  render?: (entity: Entity) => React.ReactNode;
238
- }): Promise<string | number | bigint | boolean | react_jsx_runtime.JSX.Element | Iterable<react.ReactNode> | null | undefined>;
218
+ }): (props: EntityRouteParams<T>) => Promise<string | number | bigint | boolean | react_jsx_runtime.JSX.Element | Iterable<react.ReactNode> | null | undefined>;
239
219
  /**
240
- * Generate metadata for Nitro entities with custom resolver
241
- * Works with any route param structure
220
+ * Create a metadata generator for entity detail pages.
221
+ * Returns a Next.js generateMetadata function.
242
222
  *
243
223
  * @example
244
- * ```ts
245
- * // app/blog/[slug]/page.tsx
246
- * const resolver = async (params: Promise<{ slug: string }>) => {
247
- * const { slug } = await params;
248
- * return getNitroEntities().entityBySlug({ slug, typeId: 123 });
249
- * };
224
+ * ```tsx
225
+ * import { flyo } from '@/flyo.config';
226
+ * import { nitroEntityGenerateMetadata } from '@flyo/nitro-next/server';
250
227
  *
251
- * export const generateMetadata = (props) => nitroEntityGenerateMetadata(props, { resolver });
228
+ * export const generateMetadata = nitroEntityGenerateMetadata(flyo, { resolver });
252
229
  * ```
253
230
  */
254
- declare function nitroEntityGenerateMetadata<T = any>(props: EntityRouteParams<T>, options: {
231
+ declare function nitroEntityGenerateMetadata<T = any>(flyo: FlyoInstance, options: {
255
232
  resolver: EntityResolver<T>;
256
- }): Promise<Metadata>;
257
- /**
258
- * Generate sitemap for Next.js from Flyo Nitro
259
- * Fetches all pages and entities from the sitemap endpoint
260
- * Uses the baseUrl from the Nitro configuration state
261
- *
262
- * @param state The Nitro state containing configuration and baseUrl
263
- * @returns Promise resolving to Next.js MetadataRoute.Sitemap format
264
- *
265
- * @example
266
- * ```ts
267
- * // app/sitemap.ts
268
- * import { nitroSitemap } from '@flyo/nitro-next/server';
269
- * import { flyoConfig } from '../flyo.config';
270
- *
271
- * export default async function sitemap() {
272
- * return nitroSitemap(flyoConfig());
273
- * }
274
- * ```
275
- *
276
- * @example
277
- * ```ts
278
- * // flyo.config.tsx
279
- * export const flyoConfig = initNitro({
280
- * accessToken: process.env.FLYO_ACCESS_TOKEN!,
281
- * baseUrl: process.env.SITE_URL || 'http://localhost:3000',
282
- * lang: 'en',
283
- * });
284
- * ```
285
- */
286
- declare function nitroSitemap(state: NitroState): Promise<MetadataRoute.Sitemap>;
233
+ }): (props: EntityRouteParams<T>) => Promise<Metadata>;
287
234
 
288
- export { type EntityResolver, NitroBlock, NitroDebugInfo, NitroEntityJsonLd, NitroPage, NitroSlot, type NitroState, getNitro, getNitroConfig, getNitroEntities, getNitroPages, getNitroSearch, getNitroSitemap, globalNitroState, initNitro, nitroEntityGenerateMetadata, nitroEntityRoute, nitroPageGenerateMetadata, nitroPageGenerateStaticParams, nitroPageResolveRoute, nitroPageRoute, nitroSitemap };
235
+ export { type EntityResolver, type FlyoInstance, NitroBlock, NitroDebugInfo, NitroEntityJsonLd, NitroPage, NitroSlot, type NitroState, initNitro, nitroEntityGenerateMetadata, nitroEntityRoute, nitroPageGenerateMetadata, nitroPageGenerateStaticParams, nitroPageRoute };