UNPKG

servido

Version:

Versatile services for React ⚗️

109 lines (108 loc) 5.47 kB
/// <reference types="node" /> import React from "react"; import { Service, ServiceClass, ServiceQuery } from "./service"; import { ServiceDataStore } from "./service-data-store"; import { ServiceDependent } from "./service-dependent"; import { ServiceStore } from "./service-store"; import { ServiceData, ServiceIdentifier } from "./service-types"; import { IncomingMessage } from "http"; /** Contains the currently constructed services, dependents and requirements. */ export declare class ServiceContext { private readonly dependents; private readonly requirements; private defaultIds?; private proxy?; private _store?; private _params?; private _staticData?; private _nested?; readonly key: string; readonly static: boolean; readonly parent?: ServiceContext; readonly children: Set<ServiceContext>; constructor(props?: ServiceContextProps); getId<A extends any[]>(constructor: ServiceClass<Service, A>, args: A): ServiceIdentifier; parseQuery<S extends Service>(query: ServiceQuery<S>): ServiceQuery<S>; setDefaultId(constructor: ServiceClass, id: ServiceIdentifier): this; setProxy(proxy: ServiceQuery<Service>, query: ServiceQuery<Service>): this; deleteProxy(proxy: ServiceQuery<Service>): this; protected getDefaultId<A extends any[]>(constructor: ServiceClass<Service, A>): ServiceIdentifier | undefined; protected getProxy(query: ServiceQuery<Service>): ServiceQuery<Service>; addRequirement(dependent: ServiceDependent, service: Service): void; deleteRequirement(dependent: ServiceDependent, service: Service): boolean; /** Returns all of the required services locally and upwards. */ get requiredUp(): Set<Service>; /** Ensures that all required data has been loaded. */ get promiseDataRequirements(): Promise<void>; /** Returns all of the required services locally and upwards. */ get requiredDown(): Set<Service>; /** Returns all of the required services locally and upwards. */ get childrenRequired(): Set<Service>; /** Returns all of the required services in line with the context (from parents and children) */ get required(): Set<Service>; /** Sets whether data should not be cleared after deconstructing a service. */ setStaticData(staticData?: boolean): void; get staticData(): boolean; nest(id?: ServiceIdentifier, props?: Omit<ServiceContextProps, "parent">): ServiceContext; findNearest(where: (context: ServiceContext) => unknown): ServiceContext | undefined; /** Returns Data that can be transferred between contexts to ensure initial data and params. */ promiseData(): Promise<ServiceData>; /** Returns Data that can be transferred between contexts to ensure initial data and params. */ getData(): ServiceData; /** Used to set Data that has previously been retrieved using the `getData` method. */ setData(data: ServiceData): void; get store(): ServiceStore; get params(): servido.Params; get cacheId(): string; get childrenCacheId(): string; get data(): ServiceDataStore; get globalData(): ServiceDataStore; get instance(): import("./service-instance-store").ServiceInstanceStore; get root(): any; /** Use the `ServiceContext` provided, or default to the global context that is shared by all other components not being contained by a provider. */ static use(): ServiceContext; /** Get the `ServiceContext` using a possibly defined argument. */ static get(source?: ServiceContext | Service | ServiceDependent): ServiceContext; /** Get the `servido.Params` using a possibly defined argument. */ static getParams(source?: ServiceContext | Service): servido.Params; /** The default context used when no `ServiceContextProvider` is providing the context. */ static default: ServiceContext; /** The `React.Context` used for providing and consuming `ServiceContext`:s */ static reactContext: React.Context<ServiceContext>; static get Provider(): React.Provider<ServiceContext>; static get Consumer(): React.Consumer<ServiceContext>; } export interface ServiceContextProps { /** The parent context. */ parent?: ServiceContext; /** If the `store` of the `parent` should be nested. */ nestStore?: boolean; /** If the context is static, no service will ever be re-constructed. */ static?: boolean; /** If `true`, data will never be deleted when deconstructing a service. It can be * unset at a later time using the `ServiceContext` instance. */ staticData?: boolean; /** If defined, global data can be */ globalDataStore?: ServiceDataStore; /** Params to assign to the current parameters. */ params?: Partial<servido.Params> | ((current: servido.Params) => Partial<servido.Params>); data?: ServiceData; } declare global { namespace servido { /** The context provided to services. */ interface Params { request?: Request; } /** The assumed type of the request. It's declared here to allow for declaring additional properties. * @example * declare global { * namespace servido { * interface Request extends Express.Request {} * } * } */ interface Request extends IncomingMessage { } } }