Options
All
  • Public
  • Public/Protected
  • All
Menu

Freesia - v2.0.1

Index

Type aliases

Status: StatusCode | [StatusCode, string]

Describe http status. Follow patterns are valid:

  • A number as status code like 200
  • A tuple with status code and status message like [200, "Ok"]
HttpHeader: {}

Describe http headers in an object, like:

{
"Content-Type": "application/json",
"Content-Length": "16"
}

Type declaration

  • [headerName: string]: string | string[]
Respond<T>: [T | undefined, Status, ...HttpHeader[]]

A tuple that can describe a http response. [body, status, ...httpHeaders]. If body is undefined, server will response nothing.

Type parameters

  • T

Trasnformer<T, N>: (body: T | undefined, status: Status, headers: HttpHeader[]) => MaybePromise<Respond<N>>

Type parameters

  • T

  • N

Type declaration

HttpReq: IncomingMessage | Http2ServerRequest

HttpReq is the request parameter of http or http2 request handler

HttpRes: ServerResponse | Http2ServerResponse

HttpRes is the response parameter of http or http2 request handler

ReqHandler: (req: HttpReq, res: HttpRes) => void

Type declaration

BasicRespond: Respond<string | Uint8Array | Readable>

BasicRespond is a Respond of binary-like (string, Uint8Array and Readable stream) body

EntryPoint: (req: HttpReq) => MaybePromise<BasicRespond>

Type declaration

RouteHandler<P, R>: {} | ((params: RouteParam<P>) => R | undefined)

Type parameters

  • P: string

  • R

Route<R>: (url: string) => R | undefined

Type parameters

  • R

Type declaration

    • (url: string): R | undefined
    • Parameters

      • url: string

      Returns R | undefined

RouteHandlerX<P, X, R>: {} | ((params: RouteParam<P>, extra: X) => R | undefined)

Type parameters

  • P: string

  • X

  • R

RouteX<R, X>: (url: string, extra: X) => R | undefined

Type parameters

  • R

  • X

Type declaration

    • (url: string, extra: X): R | undefined
    • Parameters

      • url: string
      • extra: X

      Returns R | undefined

RouteChainInit<R>: { route: any }

Type parameters

  • R

Type declaration

  • route:function
    • Type parameters

      • P: string

      Parameters

      Returns RouteChain<R>

RouteChainAdder<R>: <P>(pattern: P, handler: RouteHandler<P, R>, flags?: string) => RouteChain<R>

Type parameters

  • R

Type declaration

RouteChainFallback<R>: (handler: (url: string) => R) => (url: string) => R

Type parameters

  • R

Type declaration

    • (handler: (url: string) => R): (url: string) => R
    • Parameters

      • handler: (url: string) => R
          • (url: string): R
          • Parameters

            • url: string

            Returns R

      Returns (url: string) => R

        • (url: string): R
        • Parameters

          • url: string

          Returns R

RouteChain<R>: { route: RouteChainAdder<R>; fallback: RouteChainFallback<NonNullable<R>>; build: any }

Type parameters

  • R

Type declaration

RouteChainInitX<R, X>: { route: any }

Type parameters

  • R

  • X

Type declaration

  • route:function
    • Type parameters

      • P: string

      Parameters

      Returns RouteChainX<R, X>

RouteChainAdderX<R, X>: <P>(pattern: P, handler: RouteHandlerX<P, X, R>, flags?: string) => RouteChainX<R, X>

Type parameters

  • R

  • X

Type declaration

RouteChainFallbackX<R, X>: (handler: (url: string, extra: X) => R) => (url: string, extra: X) => R

Type parameters

  • R

  • X

Type declaration

    • (handler: (url: string, extra: X) => R): (url: string, extra: X) => R
    • Parameters

      • handler: (url: string, extra: X) => R
          • (url: string, extra: X): R
          • Parameters

            • url: string
            • extra: X

            Returns R

      Returns (url: string, extra: X) => R

        • (url: string, extra: X): R
        • Parameters

          • url: string
          • extra: X

          Returns R

RouteChainX<R, X>: { route: RouteChainAdderX<R, X>; fallback: RouteChainFallbackX<NonNullable<R>, X>; build: any }

Type parameters

  • R

  • X

Type declaration

Empty: Record<never, never>
NonGreedy<T>: `:<${T}>`

Type parameters

  • T: string

GreedyAtLeastOne<T>: `:{${T}}`

Type parameters

  • T: string

GreedyAtLeastZero<T>: `:[${T}]`

Type parameters

  • T: string

GreedyAtLeastZeroRemoveSlash<T>: `:(${T})`

Type parameters

  • T: string

Type parameters

  • T: string

RoutePattern<L, R>: `${L}/${R}`

Type parameters

  • L: string

  • R: string

RouteParam<U>: U extends RoutePattern<infer L, infer R> ? L extends ParamFlag<infer T> ? { [ key in T]: L extends GreedyAtLeastZeroRemoveSlash<infer _N> ? string | undefined : string } & RouteParam<R> : RouteParam<R> : U extends ParamFlag<infer T> ? { [ key in T]: U extends GreedyAtLeastZeroRemoveSlash<infer _N> ? string | undefined : string } : Empty

Type parameters

  • U: string

MaybePromise<T>: T | Promise<T>

MaybePromise is something that can may be wrapped in a Promise.

Type parameters

  • T

ComputeStream<T>: { map: any; mapNN: any; get value(): T }

Type parameters

  • T

Type declaration

  • map:function
    • map<R>(callbackFn: (value: Awaited<T>) => R): ComputeStream<T extends Promise<_M> ? R extends Promise<_AwaitedR> ? R : Promise<R> : R>
    • Type parameters

      • R

      Parameters

      • callbackFn: (value: Awaited<T>) => R
          • (value: Awaited<T>): R
          • Parameters

            • value: Awaited<T>

            Returns R

      Returns ComputeStream<T extends Promise<_M> ? R extends Promise<_AwaitedR> ? R : Promise<R> : R>

  • mapNN:function
    • mapNN<R>(callbackFn: (value: NonNullable<Awaited<T>>) => R): ComputeStream<T extends Promise<_N> ? R extends Promise<AwaitedR> ? Promise<AwaitedR | Extract<Awaited<T>, undefined | null>> : Promise<R | Extract<Awaited<T>, undefined | null>> : R | Extract<Awaited<T>, undefined | null>>
    • Type parameters

      • R

      Parameters

      • callbackFn: (value: NonNullable<Awaited<T>>) => R
          • (value: NonNullable<Awaited<T>>): R
          • Parameters

            • value: NonNullable<Awaited<T>>

            Returns R

      Returns ComputeStream<T extends Promise<_N> ? R extends Promise<AwaitedR> ? Promise<AwaitedR | Extract<Awaited<T>, undefined | null>> : Promise<R | Extract<Awaited<T>, undefined | null>> : R | Extract<Awaited<T>, undefined | null>>

  • get value(): T
Fn: (...args: any[]) => any

Type declaration

    • (...args: any[]): any
    • Parameters

      • Rest ...args: any[]

      Returns any

AsyncFn: (...args: any[]) => Promise<any>

Type declaration

    • (...args: any[]): Promise<any>
    • Parameters

      • Rest ...args: any[]

      Returns Promise<any>

BeforeHookTuple<Origin, Target>: [Parameters<Origin>, (result: ReturnType<Origin>) => ReturnType<Target>] | [null, () => ReturnType<Target>]

Type parameters

  • Origin: Fn

  • Target: Fn

Void: undefined | null
Voids<T>: T[]

Type parameters

BasicJSONTypes: string | number | boolean | null
JsonObject: {}

Type declaration

JsonArray: JsonType[]

JSON type is something that can be serialize with JSON.stringify without dropping anything.

Functions

  • Create a Respond with a body.

    Type parameters

    • T

    Parameters

    • body: undefined | T

    Returns Respond<T>

  • Create a Respond with a body and status definition

    Type parameters

    • T

    Parameters

    • body: undefined | T
    • status: Status

    Returns Respond<T>

  • Create a Respond with a body, status definitions and specify some headers.

    Type parameters

    • T

    Parameters

    Returns Respond<T>

  • Attach headers to a Respond

    Type parameters

    • T

    Parameters

    Returns Respond<T>

  • Rebuild a response with a synchronous function

    Type parameters

    • T

    • N

    Parameters

    Returns Respond<N>

  • Rebuild a response with a asynchronous function

    Type parameters

    • T

    • N

    Parameters

    Returns Promise<Respond<N>>

  • Get HttpReq from anywhere called by callback of shimHTTP

    Returns HttpReq

    HttpReq object

  • useURL(): URL
  • useURL(prop: "method"): string
  • useURL(prop: "path"): string
  • useURL(prop: "host"): string
  • useURL(prop: "query"): URLSearchParams
  • useURL<T>(prop: (url: string) => T): T
  • Get host, path and query object from the request.

    Returns URL

  • Get request method.

    Parameters

    • prop: "method"

      method

    Returns string

  • Get request path from the request.

    Parameters

    • prop: "path"

      path

    Returns string

  • Get hostname from the request.

    Parameters

    • prop: "host"

      host

    Returns string

  • Get the search params from the ruquest.

    Parameters

    • prop: "query"

      query

    Returns URLSearchParams

  • Get the route result from the router

    Type parameters

    • T

    Parameters

    • prop: (url: string) => T

      the router you want to use

        • (url: string): T
        • Parameters

          • url: string

          Returns T

    Returns T

  • createContext<T>(): [(value: T) => void, () => Readonly<T>, () => void]
  • createContext<T>(options: { mutable: true; reassign: boolean }): [(value: T) => void, () => T, () => void]
  • createContext<T>(options: { mutable: false; reassign: boolean }): [(value: T) => void, () => Readonly<T>, () => void]
  • Create a flare and return handle functions.

    Type parameters

    • T

    Returns [(value: T) => void, () => Readonly<T>, () => void]

    [light, observe, extinguish]

    • assign assign value to the flare
    • observe get the value from the flare
    • drop remove the value from the flare
  • Create a flare and return handle functions.

    Type parameters

    • T

    Parameters

    • options: { mutable: true; reassign: boolean }

      { mutable: boolean, reassign: boolean } define it this flare mutable and reassignable

      • mutable: true
      • reassign: boolean

    Returns [(value: T) => void, () => T, () => void]

    [light, observe, extinguish]

    • assign assign value to the flare
    • observe get the value from the flare
    • drop remove the value from the flare
  • Create a flare and return handle functions.

    Type parameters

    • T

    Parameters

    • options: { mutable: false; reassign: boolean }

      { mutable: boolean, reassign: boolean } define it this flare mutable and reassignable

      • mutable: false
      • reassign: boolean

    Returns [(value: T) => void, () => Readonly<T>, () => void]

    [light, observe, extinguish]

    • assign assign value to the flare
    • observe get the value from the flare
    • drop remove the value from the flare
  • shimHTTP(entry: EntryPoint, extraOptions?: { errHandler?: any; longestConnection?: number }): ReqHandler
  • Transform an entry function to Node HTTP request handler

    Parameters

    • entry: EntryPoint

      a function receives request object and return a reponse object.

    • Optional extraOptions: { errHandler?: any; longestConnection?: number }

      {errHandler, longestConnection}

      • errHandler is a function that can handler errors on request,
      • longestConnection is the longest timeout for a response handling and transporting, unit is ms.
      • errHandler?:function
        • errHandler(err: any): void
        • Parameters

          • err: any

          Returns void

      • Optional longestConnection?: number

    Returns ReqHandler

    a handler function for Node httphttpshttp2 modules

  • Type parameters

    • R: string

    Parameters

    • reality: R

    Returns ConditionInit<R>

  • createRoute<P, R>(pattern: P, handlers: RouteHandler<P, R>, flags?: string): Route<R>
  • Create a route that request can get in.

    Type parameters

    • P: string

    • R

    Parameters

    • pattern: P

      specify the url matching pattern, like /user/:<username>/:<age>/:{extra}/ can match url /user/miku/10/other/many/arguments/?timestamp=1641891955803.

    • handlers: RouteHandler<P, R>

      is a function that can receive route matched params and searchParams generated by URL class and return some thing.

    • Optional flags: string

    Returns Route<R>

    a function that receive a string as argument, if the string matched the pattern, call the handler and return its result, otherwise return undefined.

  • createRouteX<P, X, R>(pattern: P, handlers: RouteHandlerX<P, X, R>, flags?: string): RouteX<R, X>
  • Create a route that request can get in.

    Type parameters

    • P: string

    • X

    • R

    Parameters

    • pattern: P

      specify the url matching pattern, like /user/:<username>/:<age>/:{extra}/ can match url /user/miku/10/other/many/arguments/?timestamp=1641891955803.

    • handlers: RouteHandlerX<P, X, R>

      is a function that can receive route matched params and searchParams generated by URL class and return some thing.

    • Optional flags: string

    Returns RouteX<R, X>

    a function that receive a string argument and a extra arugment, if the string matched the pattern, call the handler and return its result, otherwise return undefined.

  • createRegExp<P>(pattern: P, flags?: string): RegExp
  • Type parameters

    • P: string

    Parameters

    • pattern: P
    • flags: string = "i"

    Returns RegExp

  • Create a switcher and registry some routes to it.

    Type parameters

    • R

    Returns RouteChainInit<R>

  • Create a extended switcher and registry some routes to it.

    Type parameters

    • R

    • X

    Returns RouteChainInitX<R, X>

  • createSwitcher<R>(...routes: Route<R>[]): Route<R>
  • Create a switcher connected to many routes. Routes will be matched with the order of the arguments.

    Type parameters

    • R

    Parameters

    • Rest ...routes: Route<R>[]

      routes created by createRoute() and has same return type, for example some routes with handlers all return string.

    Returns Route<R>

    a function that receive a string as argument, if the string matched the pattern, call the handler and return its result, otherwise return undefined.

  • createSwitcherX<R, X>(...routes: RouteX<R, X>[]): RouteX<R, X>
  • Create a switcher connected to many routes with same extra argument. Routes will be matched with the order of the arguments.

    Type parameters

    • R

    • X

    Parameters

    • Rest ...routes: RouteX<R, X>[]

      routes created by createExtRoute() and has same return type, for example some routes with handlers all return string.

    Returns RouteX<R, X>

    a function that receive a string argument and a extra arugment, if the string matched the pattern, call the handler and return its result, otherwise return undefined.

  • baseCompose<P, T, R>(a: (param: P) => T, b: (temp: T) => R): (param: P) => R
  • Type parameters

    • P

    • T

    • R

    Parameters

    • a: (param: P) => T
        • (param: P): T
        • Parameters

          • param: P

          Returns T

    • b: (temp: T) => R
        • (temp: T): R
        • Parameters

          • temp: T

          Returns R

    Returns (param: P) => R

      • (param: P): R
      • Parameters

        • param: P

        Returns R

  • composeFn<T, R>(currentFn: (t: T) => R): Composer<T, R>
  • Create a function composition, and add first function.

    Type parameters

    • T

    • R

    Parameters

    • currentFn: (t: T) => R

      a function that receive one value of type T and return a value of type R.

        • (t: T): R
        • Parameters

          • t: T

          Returns R

    Returns Composer<T, R>

  • Create a computation stream.

    Type parameters

    • T

    Parameters

    • initValue: T

      Give a initial value to start the computation stream.

    Returns ComputeStream<T>

    a container that contains the value.

  • Create a computation stream with lazy-computation features

    Type parameters

    • T

    Parameters

    • initFn: () => T

      Give a function that can return the value to start a lazy computation stream.

        • (): T
        • Returns T

    Returns ComputeStream<T>

    a container that contains the value.

  • fillNullable<T>(defaultValue: NonNullable<T>): (value: T | undefined | null) => NonNullable<T>
  • Type parameters

    • T

    Parameters

    • defaultValue: NonNullable<T>

    Returns (value: T | undefined | null) => NonNullable<T>

      • (value: T | undefined | null): NonNullable<T>
      • Parameters

        • value: T | undefined | null

        Returns NonNullable<T>

  • Create a proxy for an asynchronous function.

    Type parameters

    Parameters

    Returns (fn: F) => F

      • (fn: F): F
      • Create a proxy for an asynchronous function.

        Parameters

        • fn: F

        Returns F

  • Create a proxy for an synchronous function.

    Type parameters

    Parameters

    • hook: (...args: Parameters<F>) => BeforeHookTuple<F, F>

      is a function that executed before original function which returns arguments of original function, and a function executed after the original function, and returns a handled return value.

    Returns (fn: F) => F

      • (fn: F): F
      • Create a proxy for an synchronous function.

        Parameters

        • fn: F

        Returns F

  • Create a proxy that will change parameters type or returns type from the origin one.

    Type parameters

    • Origin: Fn

    • Target: Fn

    Parameters

    Returns (fn: Origin) => Target

      • (fn: Origin): Target
      • Create a proxy that will change parameters type or returns type from the origin one.

        Parameters

        • fn: Origin

        Returns Target

  • echo<T>(value: T): T
  • A function that just return the given value

    Type parameters

    • T

    Parameters

    • value: T

      the value pass to the function

    Returns T

    the value passed to the function

  • asyncEcho<T>(value: T): Promise<T>
  • Wrap a value into a Promise

    Type parameters

    • T

    Parameters

    • value: T

      the value to be wrapped

    Returns Promise<T>

    a Promisified value

  • createEffect<F>(hook: (...args: Readonly<Parameters<F>>) => (r: Readonly<ReturnType<F>>) => void): (fn: (...args: Parameters<F>) => ReturnType<F>) => F
  • createEffect can add hooks execute before and after original function, and hooks wouldn't change arguments and return value of original function, but can perform some side effect, like logging.

    Type parameters

    Parameters

    • hook: (...args: Readonly<Parameters<F>>) => (r: Readonly<ReturnType<F>>) => void

      a function execute before original function, and return a function execute after original function.

        • (...args: Readonly<Parameters<F>>): (r: Readonly<ReturnType<F>>) => void
        • Parameters

          • Rest ...args: Readonly<Parameters<F>>

          Returns (r: Readonly<ReturnType<F>>) => void

            • (r: Readonly<ReturnType<F>>): void
            • Parameters

              • r: Readonly<ReturnType<F>>

              Returns void

    Returns (fn: (...args: Parameters<F>) => ReturnType<F>) => F

    a wrapper that can wrap the orignal function to another function with side effect.

      • (fn: (...args: Parameters<F>) => ReturnType<F>): F
      • createEffect can add hooks execute before and after original function, and hooks wouldn't change arguments and return value of original function, but can perform some side effect, like logging.

        Parameters

        • fn: (...args: Parameters<F>) => ReturnType<F>
            • (...args: Parameters<F>): ReturnType<F>
            • Parameters

              • Rest ...args: Parameters<F>

              Returns ReturnType<F>

        Returns F

        a wrapper that can wrap the orignal function to another function with side effect.

  • createEffect4Any(hook: () => () => void): <F>(fn: F) => (...args: Parameters<F>) => ReturnType<F>
  • Create a effect for any function, the hook is not related to the parameters and return value of the origin function.

    Parameters

    • hook: () => () => void

      a function include code executed before the original function and returns the code executed after the original function

        • (): () => void
        • Returns () => void

            • (): void
            • Returns void

    Returns <F>(fn: F) => (...args: Parameters<F>) => ReturnType<F>

    a function that can wrap another function.

      • <F>(fn: F): (...args: Parameters<F>) => ReturnType<F>
      • Type parameters

        Parameters

        • fn: F

        Returns (...args: Parameters<F>) => ReturnType<F>

          • (...args: Parameters<F>): ReturnType<F>
          • Parameters

            • Rest ...args: Parameters<F>

            Returns ReturnType<F>

  • isEnum<E>(value: string | number, enums: E[]): value is E
  • Check if a string/number is a enumed value

    Type parameters

    • E: string | number

    Parameters

    • value: string | number

      The value to be checked

    • enums: E[]

      The enums

    Returns value is E

  • isVoid<T, V>(initValue: T, voidValues?: Voids<V>): initValue is Extract<T, V>
  • Find out if the value is null or undefined.

    Type parameters

    • T

    • V: Void = undefined | null

    Parameters

    • initValue: T

      the value to be cheched.

    • Optional voidValues: Voids<V>

      specify if the void value refers to undefined or null, or undefined and null

    Returns initValue is Extract<T, V>

    boolean

  • Return something can be defined as json.

    Type parameters

    Parameters

    • json: T

      the json content you'd like to send.

    Returns Respond<T>

    Respond with json content

  • CacheMap<K, V>(): { has: any; set: any; get: any; delete: any; entries: any }
  • Type parameters

    • K: any[]

    • V

    Returns { has: any; set: any; get: any; delete: any; entries: any }

    • has:function
      • has(k: K): boolean
      • Parameters

        • k: K

        Returns boolean

    • set:function
      • set(k: K, v: V): void
      • Parameters

        • k: K
        • v: V

        Returns void

    • get:function
      • get(k: K): undefined | V
      • Parameters

        • k: K

        Returns undefined | V

    • delete:function
      • delete(k: K): void
      • Parameters

        • k: K

        Returns void

    • entries:function
      • entries(): readonly K[]
      • Returns readonly K[]

  • memoryCache<F>(fn: F, expire?: number): F
  • Create a cache wrapper for a function. The wrapped function will return the cached value if found the same arguments. Comparation rule is same-value-zero.

    Be carefull! This may lead to OOM! You should only employ this wrapper to a simple caculation function, and do not use Inifity as the expire time.

    Type parameters

    • F: (...args: any[]) => any

    Parameters

    • fn: F

      the original function.

    • expire: number = Infinity

      the time that a cached expired. Default to Inifinity

    Returns F

    a wrapped function.

  • limitRate<F>(fn: F, options: { max: number; windowSize: number; fallback: ReturnType<F> }): F
  • Wrap a function to make it do not be called too many times.

    If it called too many times, wrapped function will return a fallback value without too many computation.

    Type parameters

    Parameters

    • fn: F

      function to be wrapped.

    • options: { max: number; windowSize: number; fallback: ReturnType<F> }

      specify maximum times to be called in a windowSize, and set the fallback value.

      • max: number
      • windowSize: number
      • fallback: ReturnType<F>

    Returns F

Generated using TypeDoc