import { NextRouter, GetServerSideProps, GetStaticProps } from 'next/types'; import { ComponentType, ReactElement, ComponentProps } from 'react'; /** * © 2022 WavePlay */ interface BaseConfig { host?: string; i18n?: { defaultLocale: string; locales: string[]; }; logLevel?: 'debug' | 'info' | 'warn' | 'error'; webProps?: { [key: string]: 'always' | 'auto' | 'never'; }; } declare type DataMap = { [key: string]: string; }; interface PageModule { default: ComponentType; } interface PageRoute { getPropsType?: 'getStaticProps' | 'getServerSideProps'; importPath?: string; path: string; } declare type PilotHookCallback = (path: string, event: PilotEvent) => string | void; interface Redirect { destination: string; permanent?: boolean; source: string; } interface Rewrite { destination: string; permanent?: boolean; source: string; } declare type Url = string | { pathname: string; query?: DataMap; }; declare class Pilot { private _config; private _currentLocale?; private _currentPage?; private readonly _hooks; private readonly _routerRedirects; private readonly _routerRewrites; private readonly _stack; private readonly _localUrl?; private readonly _localTunnel?; constructor(config?: PilotConfig); addHook(event: PilotEventType, callback: PilotHookCallback): number; addRoute(route: PilotRouteOptions): void; back(): Promise; config(config?: PilotConfig): PilotConfig; getDefaultLocale(): string | undefined; getHost(): string; getLocale(): string | undefined; getLocales(): string[] | undefined; getParams(): DataMap; getPath(options?: { includeLocale?: boolean; }): string; getProps(): any; getQuery(): DataMap; /** * Equivalent to Next's router.push(). * Updates content with registered path component, also loading their props. * If a placeholder has been specified, it'll render that while the props are fetching. * * @param path */ fly(url: Url, as?: string, options?: PilotFlyOptions): Promise; log(level: 'trace' | 'debug' | 'info' | 'warn' | 'error', message?: string, ...args: any[]): void; /** * Alias for fly(). * * See {@link fly} for more details. */ push(url: Url, as?: string, options?: PilotFlyOptions): Promise; reload(): Promise; removeHook(id: number): void; removeRoute(path: string): void; render(): ReactElement | null; stats(): { dev: { localUrl: string; tunnelUrl: string; }; host: string; id: string; i18: { defaultLocale: string; locales: string[]; }; logger: Logger; nextRouter: boolean; path: string; params: DataMap; routerStats: { key: string; numRoutes: number; routes: PilotRouteOptions[]; }; query: DataMap; stackLength: number; }; /** * Boilerplate function mainly used to de-duplicate a lot of logic. * To be called by certain other functions like back(), fly(), and refresh(). * * @param path Path to navigate to. Mainly used for notifying listeners. * @param options Should contain action to execute + other options. */ private _fly; /** * Loads a page and its props by calling it's `getServerSideProps()` or `getStaticProps()` function. * The result is then stored into _currentPage to be rendered via the `render()` function. * * This will not create an instance of the page component, but rather just call the props function. * The component instance will be created when the page is rendered. * * @param path Path to load. * @returns Component and props for the specified path. If no component is found, returns null. */ private _load; /** * Calls route's getProps() function to load props for the specified path. * This basically emulates NextJS' getServerSideProps() or getStaticProps() function. * If a "revalidate" value is returned, it will be stored in our LRU memory cache. * * On native, these props are called on the server-side if "host" is set, otherwise they are called on the app. * * @param path Path to load. * @param route Route for the specified path. * @returns Props for the specified path. */ private _loadProps; /** * Notifies all added hooks of a certain event. * This allows hooks to modify the path, or do other things in response to events. * * @param path Path to notify listeners about. * @param event Event to notify listeners about. * @returns The modified path. If no hooks modify the path, this will be the same as the original. */ private _notify; } /** * © 2022 WavePlay */ interface Logger { debug: (...args: any[]) => void; error: (...args: any[]) => void; info: (...args: any[]) => void; warn: (...args: any[]) => void; trace: (...args: any[]) => void; } interface NativeCache { clear(): boolean; delete(key: string): boolean; evict(): boolean; get(key: string): any | undefined; keys(): string[]; set(key: string, value: any): boolean; } /** * Runtime Pilot.js config */ interface PilotConfig extends BaseConfig { id?: string; logger?: Logger; nativeCache?: NativeCache; nextRouter?: NextRouter | null; redirects?: Redirect[]; rewrites?: Rewrite[]; router?: PilotRouter; } interface PilotFlyOptions { locale?: string | false; scroll?: boolean; shallow?: boolean; /** * Decides when to load props using the web version of this app. * * `'always'` - Always load props using the web version of this app. Will fail if not available or set up. * * `'auto'` - Will load props using the web version of this app only as long as it's set up. (`host` in config) * * `'never'` - Will instead load props using the native app's runtime. Be careful when using Node APIs or environment secrets with this option. */ webProps?: 'always' | 'auto' | 'never'; } interface PilotEvent { error?: any; page?: PilotPage; type: PilotEventType; } declare type PilotEventType = '*' | 'error' | 'load-complete' | 'load-start' | 'redirect'; interface PilotPage { Component: ComponentType; pageProps: ComponentProps; params: DataMap; query: DataMap; } /** * PilotPath is an empty component whose sole purpose is to make it easy to define paths. * The PilotProvider extracts these props and stores them for navigation. */ interface PilotRouteOptions { Component: ComponentType; getProps?: GetServerSideProps | GetStaticProps; getPropsType?: 'getStaticProps' | 'getServerSideProps'; path: string; } interface PilotRouter { addRoute: (route: PilotRouteOptions) => void; find: (path: string, options: PilotRouterOptions) => PilotRouteResult; removeRoute: (path: string) => void; stats: () => { key: string; numRoutes: number; routes: PilotRouteOptions[]; }; } interface PilotRouterOptions { pilot: Pilot; } interface PilotRouteResult extends PilotRouteOptions { params?: DataMap; query?: DataMap; } export { DataMap as D, PilotConfig as P, PageRoute as a, PageModule as b };