@alepha/react 0.7.4 → 0.7.6

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.
@@ -0,0 +1,578 @@
1
+ import * as _alepha_core7 from "@alepha/core";
2
+ import * as _alepha_core0 from "@alepha/core";
3
+ import * as _alepha_core2 from "@alepha/core";
4
+ import { Alepha, Async, KIND, Module, OPTIONS, Service, Static, TObject, TSchema } from "@alepha/core";
5
+ import { Route, RouterProvider } from "@alepha/router";
6
+ import * as react14 from "react";
7
+ import * as react15 from "react";
8
+ import React, { AnchorHTMLAttributes, ErrorInfo, FC, PropsWithChildren, ReactNode } from "react";
9
+ import * as react_jsx_runtime12 from "react/jsx-runtime";
10
+ import * as react_jsx_runtime13 from "react/jsx-runtime";
11
+ import { ApiLinksResponse, ClientScope, HttpClient, HttpVirtualClient, ServerRequest } from "@alepha/server";
12
+ import { Root } from "react-dom/client";
13
+ import { ServerRouteCache } from "@alepha/server-cache";
14
+ import * as _sinclair_typebox6 from "@sinclair/typebox";
15
+
16
+ //#region src/components/ClientOnly.d.ts
17
+ interface ClientOnlyProps {
18
+ fallback?: ReactNode;
19
+ disabled?: boolean;
20
+ }
21
+ /**
22
+ * A small utility component that renders its children only on the client side.
23
+ *
24
+ * Optionally, you can provide a fallback React node that will be rendered.
25
+ *
26
+ * You should use this component when
27
+ * - you have code that relies on browser-specific APIs
28
+ * - you want to avoid server-side rendering for a specific part of your application
29
+ * - you want to prevent pre-rendering of a component
30
+ */
31
+ declare const ClientOnly: (props: PropsWithChildren<ClientOnlyProps>) => ReactNode;
32
+ //#endregion
33
+ //#region src/components/ErrorBoundary.d.ts
34
+ /**
35
+ * Props for the ErrorBoundary component.
36
+ */
37
+ interface ErrorBoundaryProps {
38
+ /**
39
+ * Fallback React node to render when an error is caught.
40
+ * If not provided, a default error message will be shown.
41
+ */
42
+ fallback: (error: Error) => ReactNode;
43
+ /**
44
+ * Optional callback that receives the error and error info.
45
+ * Use this to log errors to a monitoring service.
46
+ */
47
+ onError?: (error: Error, info: ErrorInfo) => void;
48
+ }
49
+ /**
50
+ * State of the ErrorBoundary component.
51
+ */
52
+ interface ErrorBoundaryState {
53
+ error?: Error;
54
+ }
55
+ /**
56
+ * A reusable error boundary for catching rendering errors
57
+ * in any part of the React component tree.
58
+ */
59
+ declare class ErrorBoundary extends React.Component<PropsWithChildren<ErrorBoundaryProps>, ErrorBoundaryState> {
60
+ constructor(props: ErrorBoundaryProps);
61
+ /**
62
+ * Update state so the next render shows the fallback UI.
63
+ */
64
+ static getDerivedStateFromError(error: Error): ErrorBoundaryState;
65
+ /**
66
+ * Lifecycle method called when an error is caught.
67
+ * You can log the error or perform side effects here.
68
+ */
69
+ componentDidCatch(error: Error, info: ErrorInfo): void;
70
+ render(): ReactNode;
71
+ }
72
+ //#endregion
73
+ //#region src/providers/PageDescriptorProvider.d.ts
74
+ declare const envSchema: _alepha_core7.TObject<{
75
+ REACT_STRICT_MODE: _sinclair_typebox6.TBoolean;
76
+ }>;
77
+ declare module "@alepha/core" {
78
+ interface Env extends Partial<Static<typeof envSchema>> {}
79
+ }
80
+ declare class PageDescriptorProvider {
81
+ protected readonly log: _alepha_core7.Logger;
82
+ protected readonly env: {
83
+ REACT_STRICT_MODE: boolean;
84
+ };
85
+ protected readonly alepha: Alepha;
86
+ protected readonly pages: PageRoute[];
87
+ getPages(): PageRoute[];
88
+ page(name: string): PageRoute;
89
+ url(name: string, options?: {
90
+ params?: Record<string, string>;
91
+ base?: string;
92
+ }): URL;
93
+ root(state: RouterState, context: PageReactContext): ReactNode;
94
+ createLayers(route: PageRoute, request: PageRequest): Promise<CreateLayersResult>;
95
+ protected getErrorHandler(route: PageRoute): ((error: Error) => ReactNode) | undefined;
96
+ protected createElement(page: PageRoute, props: Record<string, any>): Promise<ReactNode>;
97
+ protected fillHead(page: PageRoute, ctx: PageRequest, props: Record<string, any>): void;
98
+ renderError(error: Error): ReactNode;
99
+ renderEmptyView(): ReactNode;
100
+ href(page: {
101
+ options: {
102
+ name?: string;
103
+ };
104
+ }, params?: Record<string, any>): string;
105
+ compile(path: string, params?: Record<string, string>): string;
106
+ protected renderView(index: number, path: string, view: ReactNode | undefined, page: PageRoute): ReactNode;
107
+ protected readonly configure: _alepha_core7.HookDescriptor<"configure">;
108
+ protected map(pages: Array<{
109
+ value: {
110
+ [OPTIONS]: PageDescriptorOptions;
111
+ };
112
+ }>, target: {
113
+ [OPTIONS]: PageDescriptorOptions;
114
+ }): PageRouteEntry;
115
+ add(entry: PageRouteEntry): void;
116
+ protected createMatch(page: PageRoute): string;
117
+ protected _next: number;
118
+ protected nextId(): string;
119
+ }
120
+ declare const isPageRoute: (it: any) => it is PageRoute;
121
+ interface PageRouteEntry extends Omit<PageDescriptorOptions, "children" | "parent"> {
122
+ children?: PageRouteEntry[];
123
+ }
124
+ interface PageRoute extends PageRouteEntry {
125
+ type: "page";
126
+ name: string;
127
+ parent?: PageRoute;
128
+ match: string;
129
+ }
130
+ interface Layer {
131
+ config?: {
132
+ query?: Record<string, any>;
133
+ params?: Record<string, any>;
134
+ context?: Record<string, any>;
135
+ };
136
+ name: string;
137
+ props?: Record<string, any>;
138
+ error?: Error;
139
+ part?: string;
140
+ element: ReactNode;
141
+ index: number;
142
+ path: string;
143
+ }
144
+ type PreviousLayerData = Omit<Layer, "element" | "index" | "path">;
145
+ interface AnchorProps {
146
+ href: string;
147
+ onClick: (ev: any) => any;
148
+ }
149
+ interface RouterState {
150
+ pathname: string;
151
+ search: string;
152
+ layers: Array<Layer>;
153
+ }
154
+ interface TransitionOptions {
155
+ state?: RouterState;
156
+ previous?: PreviousLayerData[];
157
+ context?: PageReactContext;
158
+ }
159
+ interface RouterStackItem {
160
+ route: PageRoute;
161
+ config?: Record<string, any>;
162
+ props?: Record<string, any>;
163
+ error?: Error;
164
+ }
165
+ interface RouterRenderResult {
166
+ state: RouterState;
167
+ context: PageReactContext;
168
+ redirect?: string;
169
+ }
170
+ interface PageRequest extends PageReactContext {
171
+ params: Record<string, any>;
172
+ query: Record<string, string>;
173
+ previous?: PreviousLayerData[];
174
+ }
175
+ interface CreateLayersResult extends RouterState {
176
+ redirect?: string;
177
+ }
178
+ /**
179
+ * It's like RouterState, but publicly available in React context.
180
+ * This is where we store all plugin data!
181
+ */
182
+ interface PageReactContext {
183
+ url: URL;
184
+ head: Head;
185
+ onError: (error: Error) => ReactNode;
186
+ links?: ApiLinksResponse;
187
+ }
188
+ //#endregion
189
+ //#region src/descriptors/$page.d.ts
190
+ declare const KEY = "PAGE";
191
+ interface PageConfigSchema {
192
+ query?: TSchema;
193
+ params?: TSchema;
194
+ }
195
+ type TPropsDefault = any;
196
+ type TPropsParentDefault = {};
197
+ interface PageDescriptorOptions<TConfig extends PageConfigSchema = PageConfigSchema, TProps extends object = TPropsDefault, TPropsParent extends object = TPropsParentDefault> {
198
+ /**
199
+ * Name your page.
200
+ *
201
+ * @default Descriptor key
202
+ */
203
+ name?: string;
204
+ /**
205
+ * Optional description of the page.
206
+ */
207
+ description?: string;
208
+ /**
209
+ * Add a pathname to the page.
210
+ *
211
+ * Pathname can contain parameters, like `/post/:slug`.
212
+ *
213
+ * @default ""
214
+ */
215
+ path?: string;
216
+ /**
217
+ * Add an input schema to define:
218
+ * - `params`: parameters from the pathname.
219
+ * - `query`: query parameters from the URL.
220
+ */
221
+ schema?: TConfig;
222
+ /**
223
+ * Load data before rendering the page.
224
+ *
225
+ * This function receives
226
+ * - the request context and
227
+ * - the parent props (if page has a parent)
228
+ *
229
+ * In SSR, the returned data will be serialized and sent to the client, then reused during the client-side hydration.
230
+ *
231
+ * Resolve can be stopped by throwing an error, which will be handled by the `errorHandler` function.
232
+ * It's common to throw a `NotFoundError` to display a 404 page.
233
+ *
234
+ * RedirectError can be thrown to redirect the user to another page.
235
+ */
236
+ resolve?: (context: PageResolve<TConfig, TPropsParent>) => Async<TProps>;
237
+ /**
238
+ * The component to render when the page is loaded.
239
+ *
240
+ * If `lazy` is defined, this will be ignored.
241
+ * Prefer using `lazy` to improve the initial loading time.
242
+ */
243
+ component?: FC<TProps & TPropsParent>;
244
+ /**
245
+ * Lazy load the component when the page is loaded.
246
+ *
247
+ * It's recommended to use this for components to improve the initial loading time
248
+ * and enable code-splitting.
249
+ */
250
+ lazy?: () => Promise<{
251
+ default: FC<TProps & TPropsParent>;
252
+ }>;
253
+ /**
254
+ * Set some children pages and make the page a parent page.
255
+ *
256
+ * /!\ Parent page can't be rendered directly. /!\
257
+ *
258
+ * If you still want to render at this pathname, add a child page with an empty path.
259
+ */
260
+ children?: Array<{
261
+ [OPTIONS]: PageDescriptorOptions;
262
+ }>;
263
+ parent?: {
264
+ [OPTIONS]: PageDescriptorOptions<PageConfigSchema, TPropsParent>;
265
+ };
266
+ can?: () => boolean;
267
+ head?: Head | ((props: TProps, previous?: Head) => Head);
268
+ errorHandler?: (error: Error) => ReactNode;
269
+ prerender?: boolean | {
270
+ entries?: Array<Partial<PageRequestConfig<TConfig>>>;
271
+ };
272
+ /**
273
+ * If true, the page will be rendered on the client-side.
274
+ */
275
+ client?: boolean | ClientOnlyProps;
276
+ afterHandler?: (request: ServerRequest) => any;
277
+ cache?: ServerRouteCache;
278
+ }
279
+ interface PageDescriptor<TConfig extends PageConfigSchema = PageConfigSchema, TProps extends object = TPropsDefault, TPropsParent extends object = TPropsParentDefault> {
280
+ [KIND]: typeof KEY;
281
+ [OPTIONS]: PageDescriptorOptions<TConfig, TProps, TPropsParent>;
282
+ /**
283
+ * For testing or build purposes, this will render the page (with or without the HTML layout) and return the HTML and context.
284
+ * Only valid for server-side rendering, it will throw an error if called on the client-side.
285
+ */
286
+ render: (options?: PageDescriptorRenderOptions) => Promise<PageDescriptorRenderResult>;
287
+ }
288
+ /**
289
+ * Main descriptor for defining a React route in the application.
290
+ */
291
+ declare const $page: {
292
+ <TConfig extends PageConfigSchema = PageConfigSchema, TProps extends object = any, TPropsParent extends object = TPropsParentDefault>(options: PageDescriptorOptions<TConfig, TProps, TPropsParent>): PageDescriptor<TConfig, TProps, TPropsParent>;
293
+ [KIND]: string;
294
+ };
295
+ interface PageDescriptorRenderOptions {
296
+ params?: Record<string, string>;
297
+ query?: Record<string, string>;
298
+ withLayout?: boolean;
299
+ }
300
+ interface PageDescriptorRenderResult {
301
+ html: string;
302
+ context: PageReactContext;
303
+ }
304
+ interface Head {
305
+ title?: string;
306
+ description?: string;
307
+ titleSeparator?: string;
308
+ htmlAttributes?: Record<string, string>;
309
+ bodyAttributes?: Record<string, string>;
310
+ meta?: Array<{
311
+ name: string;
312
+ content: string;
313
+ }>;
314
+ keywords?: string[];
315
+ author?: string;
316
+ robots?: string;
317
+ themeColor?: string;
318
+ viewport?: string | {
319
+ width?: string;
320
+ height?: string;
321
+ initialScale?: string;
322
+ maximumScale?: string;
323
+ userScalable?: "no" | "yes" | "0" | "1";
324
+ interactiveWidget?: "resizes-visual" | "resizes-content" | "overlays-content";
325
+ };
326
+ og?: {
327
+ title?: string;
328
+ description?: string;
329
+ image?: string;
330
+ url?: string;
331
+ type?: string;
332
+ };
333
+ twitter?: {
334
+ card?: string;
335
+ title?: string;
336
+ description?: string;
337
+ image?: string;
338
+ site?: string;
339
+ };
340
+ }
341
+ interface PageRequestConfig<TConfig extends PageConfigSchema = PageConfigSchema> {
342
+ params: TConfig["params"] extends TSchema ? Static<TConfig["params"]> : Record<string, string>;
343
+ query: TConfig["query"] extends TSchema ? Static<TConfig["query"]> : Record<string, string>;
344
+ }
345
+ type PageResolve<TConfig extends PageConfigSchema = PageConfigSchema, TPropsParent extends object = TPropsParentDefault> = PageRequestConfig<TConfig> & TPropsParent & PageReactContext;
346
+ //#endregion
347
+ //#region src/components/Link.d.ts
348
+ interface LinkProps extends AnchorHTMLAttributes<HTMLAnchorElement> {
349
+ to: string | PageDescriptor;
350
+ children?: React.ReactNode;
351
+ }
352
+ declare const Link: (props: LinkProps) => react_jsx_runtime12.JSX.Element | null;
353
+ //#endregion
354
+ //#region src/components/NestedView.d.ts
355
+ interface NestedViewProps {
356
+ children?: ReactNode;
357
+ }
358
+ /**
359
+ * A component that renders the current view of the nested router layer.
360
+ *
361
+ * To be simple, it renders the `element` of the current child page of a parent page.
362
+ *
363
+ * @example
364
+ * ```tsx
365
+ * import { NestedView } from "@alepha/react";
366
+ *
367
+ * class App {
368
+ * parent = $page({
369
+ * component: () => <NestedView />,
370
+ * });
371
+ *
372
+ * child = $page({
373
+ * parent: this.root,
374
+ * component: () => <div>Child Page</div>,
375
+ * });
376
+ * }
377
+ * ```
378
+ */
379
+ declare const NestedView: (props: NestedViewProps) => react_jsx_runtime13.JSX.Element;
380
+ //#endregion
381
+ //#region src/contexts/RouterContext.d.ts
382
+ interface RouterContextValue {
383
+ alepha: Alepha;
384
+ state: RouterState;
385
+ context: PageReactContext;
386
+ }
387
+ declare const RouterContext: react14.Context<RouterContextValue | undefined>;
388
+ //#endregion
389
+ //#region src/contexts/RouterLayerContext.d.ts
390
+ interface RouterLayerContextValue {
391
+ index: number;
392
+ path: string;
393
+ }
394
+ declare const RouterLayerContext: react15.Context<RouterLayerContextValue | undefined>;
395
+ //#endregion
396
+ //#region src/providers/ServerHeadProvider.d.ts
397
+ interface Head$1 {
398
+ title?: string;
399
+ htmlAttributes?: Record<string, string>;
400
+ bodyAttributes?: Record<string, string>;
401
+ meta?: Array<{
402
+ name: string;
403
+ content: string;
404
+ }>;
405
+ }
406
+ //#endregion
407
+ //#region src/providers/BrowserHeadProvider.d.ts
408
+ declare class BrowserHeadProvider {
409
+ renderHead(document: Document, head: Head$1): void;
410
+ }
411
+ //#endregion
412
+ //#region src/providers/BrowserRouterProvider.d.ts
413
+ interface BrowserRoute extends Route {
414
+ page: PageRoute;
415
+ }
416
+ declare class BrowserRouterProvider extends RouterProvider<BrowserRoute> {
417
+ protected readonly log: _alepha_core0.Logger;
418
+ protected readonly alepha: Alepha;
419
+ protected readonly pageDescriptorProvider: PageDescriptorProvider;
420
+ add(entry: PageRouteEntry): void;
421
+ protected readonly configure: _alepha_core0.HookDescriptor<"configure">;
422
+ transition(url: URL, options?: TransitionOptions): Promise<RouterRenderResult>;
423
+ root(state: RouterState, context: PageReactContext): ReactNode;
424
+ }
425
+ //#endregion
426
+ //#region src/providers/ReactBrowserProvider.d.ts
427
+ declare class ReactBrowserProvider {
428
+ protected readonly log: _alepha_core2.Logger;
429
+ protected readonly client: HttpClient;
430
+ protected readonly alepha: Alepha;
431
+ protected readonly router: BrowserRouterProvider;
432
+ protected readonly headProvider: BrowserHeadProvider;
433
+ protected root: Root;
434
+ transitioning?: {
435
+ to: string;
436
+ };
437
+ state: RouterState;
438
+ get document(): Document;
439
+ get history(): History;
440
+ get url(): string;
441
+ invalidate(props?: Record<string, any>): Promise<void>;
442
+ go(url: string, options?: RouterGoOptions): Promise<void>;
443
+ protected render(options?: {
444
+ url?: string;
445
+ previous?: PreviousLayerData[];
446
+ }): Promise<RouterRenderResult>;
447
+ /**
448
+ * Get embedded layers from the server.
449
+ */
450
+ protected getHydrationState(): ReactHydrationState | undefined;
451
+ readonly ready: _alepha_core2.HookDescriptor<"ready">;
452
+ readonly onTransitionEnd: _alepha_core2.HookDescriptor<keyof _alepha_core2.Hooks>;
453
+ }
454
+ interface RouterGoOptions {
455
+ replace?: boolean;
456
+ match?: TransitionOptions;
457
+ params?: Record<string, string>;
458
+ }
459
+ interface ReactHydrationState {
460
+ layers?: Array<PreviousLayerData>;
461
+ links?: ApiLinksResponse;
462
+ }
463
+ //#endregion
464
+ //#region src/hooks/RouterHookApi.d.ts
465
+ declare class RouterHookApi {
466
+ private readonly pages;
467
+ private readonly state;
468
+ private readonly layer;
469
+ private readonly browser?;
470
+ constructor(pages: PageRoute[], state: RouterState, layer: {
471
+ path: string;
472
+ }, browser?: ReactBrowserProvider | undefined);
473
+ get current(): RouterState;
474
+ get pathname(): string;
475
+ get query(): Record<string, string>;
476
+ back(): Promise<void>;
477
+ forward(): Promise<void>;
478
+ invalidate(props?: Record<string, any>): Promise<void>;
479
+ /**
480
+ * Create a valid href for the given pathname.
481
+ *
482
+ * @param pathname
483
+ * @param layer
484
+ */
485
+ createHref(pathname: HrefLike, layer?: {
486
+ path: string;
487
+ }, options?: {
488
+ params?: Record<string, any>;
489
+ }): string;
490
+ go(path: string, options?: RouterGoOptions): Promise<void>;
491
+ go<T extends object>(path: keyof VirtualRouter<T>, options?: RouterGoOptions): Promise<void>;
492
+ anchor(path: string, options?: {
493
+ params?: Record<string, any>;
494
+ }): AnchorProps;
495
+ anchor<T extends object>(path: keyof VirtualRouter<T>, options?: {
496
+ params?: Record<string, any>;
497
+ }): AnchorProps;
498
+ /**
499
+ * Set query params.
500
+ *
501
+ * @param record
502
+ * @param options
503
+ */
504
+ setQueryParams(record: Record<string, any> | ((queryParams: Record<string, any>) => Record<string, any>), options?: {
505
+ /**
506
+ * If true, this will add a new entry to the history stack.
507
+ */
508
+ push?: boolean;
509
+ }): void;
510
+ }
511
+ type HrefLike = string | {
512
+ options: {
513
+ path?: string;
514
+ name?: string;
515
+ };
516
+ };
517
+ type VirtualRouter<T> = { [K in keyof T as T[K] extends PageDescriptor ? K : never]: T[K] };
518
+ //#endregion
519
+ //#region src/errors/RedirectionError.d.ts
520
+ declare class RedirectionError extends Error {
521
+ readonly page: HrefLike;
522
+ constructor(page: HrefLike);
523
+ }
524
+ //#endregion
525
+ //#region src/hooks/useActive.d.ts
526
+ declare const useActive: (path: HrefLike) => UseActiveHook;
527
+ interface UseActiveHook {
528
+ isActive: boolean;
529
+ anchorProps: AnchorProps;
530
+ isPending: boolean;
531
+ name?: string;
532
+ }
533
+ //#endregion
534
+ //#region src/hooks/useAlepha.d.ts
535
+ declare const useAlepha: () => Alepha;
536
+ //#endregion
537
+ //#region src/hooks/useClient.d.ts
538
+ declare const useClient: <T extends object>(_scope?: ClientScope) => HttpVirtualClient<T>;
539
+ //#endregion
540
+ //#region src/hooks/useInject.d.ts
541
+ declare const useInject: <T extends object>(clazz: Service<T>) => T;
542
+ //#endregion
543
+ //#region src/hooks/useQueryParams.d.ts
544
+ interface UseQueryParamsHookOptions {
545
+ format?: "base64" | "querystring";
546
+ key?: string;
547
+ push?: boolean;
548
+ }
549
+ declare const useQueryParams: <T extends TObject>(schema: T, options?: UseQueryParamsHookOptions) => [Static<T>, (data: Static<T>) => void];
550
+ //#endregion
551
+ //#region src/hooks/useRouter.d.ts
552
+ declare const useRouter: () => RouterHookApi;
553
+ //#endregion
554
+ //#region src/hooks/useRouterEvents.d.ts
555
+ declare const useRouterEvents: (opts?: {
556
+ onBegin?: (ev: {
557
+ state: RouterState;
558
+ }) => void;
559
+ onEnd?: (ev: {
560
+ state: RouterState;
561
+ }) => void;
562
+ onError?: (ev: {
563
+ state: RouterState;
564
+ error: Error;
565
+ }) => void;
566
+ }, deps?: any[]) => void;
567
+ //#endregion
568
+ //#region src/hooks/useRouterState.d.ts
569
+ declare const useRouterState: () => RouterState;
570
+ //#endregion
571
+ //#region src/index.browser.d.ts
572
+ declare class AlephaReact implements Module {
573
+ readonly name = "alepha.react";
574
+ readonly $services: (alepha: Alepha) => Alepha;
575
+ }
576
+ //#endregion
577
+ export { $page, AlephaReact, AnchorProps, BrowserRoute, BrowserRouterProvider, ClientOnly, CreateLayersResult, ErrorBoundary, Head, HrefLike, Layer, Link, NestedView, PageConfigSchema, PageDescriptor, PageDescriptorOptions, PageDescriptorProvider, PageDescriptorRenderOptions, PageDescriptorRenderResult, PageReactContext, PageRequest, PageRequestConfig, PageResolve, PageRoute, PageRouteEntry, PreviousLayerData, ReactBrowserProvider, ReactHydrationState, RedirectionError, RouterContext, RouterContextValue, RouterGoOptions, RouterHookApi, RouterLayerContext, RouterLayerContextValue, RouterRenderResult, RouterStackItem, RouterState, TPropsDefault, TPropsParentDefault, TransitionOptions, UseActiveHook, UseQueryParamsHookOptions, VirtualRouter, isPageRoute, useActive, useAlepha, useClient, useInject, useQueryParams, useRouter, useRouterEvents, useRouterState };
578
+ //# sourceMappingURL=index.browser.d.ts.map