Class RestfulApi

The Context class is used to describe a route. Context can be nested using ContextChildCondition as children (generated using the when function). TODO: More documentation

Hierarchy

Constructors

Properties

The api that the context belongs to

Memberof

Context

children: ContextChild[] = []

The children of the context

Memberof

Context

Example

Context.current.children; // The children of the current context

See

condition?: ContextCondition

The condition for the context to be executed

Memberof

Context

parent?: Context

The parent context

Memberof

Context

Accessors

  • get map(): unknown
  • Get a map of all the data of the context

    Returns unknown

    A map with all the data of the context

    Example

    Context.current.map; // A map with all the data of the context
    

Methods

  • Search for paths starting with the given path. Is the same as Condition.describe

    Parameters

    • what: string

      the path that the condition should check for

    • init: ContextInitializer

      the action happening when the condition is met

    Returns RestfulApi

    Example

    Context.current.any("/hello", function() {
    // ctx is available here via [this] keyword
    // Do something
    });
  • Search for paths matching the given regex. Is the same as Condition.describe

    Parameters

    • what: RegExp

      the regex that the condition should check for

    • init: ContextInitializer

      the action happening when the condition is met

    Returns RestfulApi

    Example

    Context.current.any("/hello", function() {
    // ctx is available here via [this] keyword
    // Do something
    });
  • Search for paths starting with the given path. Is the same as Condition.describe This is a shorthand for Context.any and then Context.action inside.

    Parameters

    • what: string

      the path that the condition should check for

    • init: ActionFunction

      the action happening when the condition is met

    Returns RestfulApi

    Example

    Context.current.any("/hello", async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });

    // Is the same as this:
    Context.current.any("/hello", function() {
    this.action(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Search for paths matching the given regex. Is the same as Condition.describe This is a shorthand for Context.any and then Context.action inside.

    Parameters

    • what: RegExp

      the regex that the condition should check for

    • init: ActionFunction

      the action happening when the condition is met

    Returns RestfulApi

    Example

    Context.current.any(//hello/, async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });

    // Is the same as this:
    Context.current.any(//hello/, function() {
    this.action(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Get the context stack for the given request

    Parameters

    • req: Request

      The request to get the context stack for

    • res: Response

      The response to get the context stack for

    Returns Promise<ContextChild[][]>

    The context stack

    Memberof

    Context

    Example

    Context.current.contextStack(req, res); // The context stack for the given request
    
  • Get all the data of the context

    Returns {
        [key: string]: unknown;
    }

    A map with all the data of the context

    • [key: string]: unknown

    Example

    Context.current.data(); // A map with all the data of the context
    
  • Get the data with the given key

    Parameters

    • key: string

      The key of the data to get

    Returns unknown

    The data with the given key

    Example

    Context.current.data("key"); // The data with the given key
    
  • Match requests with the method DELETE.

    Parameters

    Returns Context

    Example

    Context.current.delete(function() {
    // ctx is available here via [this] keyword
    // Do something
    });
  • Match requests starting with the given path and requests with the method DELETE.

    Parameters

    • what: string

      the path that the condition should check for

    • init: ContextInitializer

      the action happening when the condition is met

    Returns Context

    Example

    Context.current.delete("/hello", function() {
    // ctx is available here via [this] keyword
    // Do something
    });
  • Match requests matching the given regex and requests with the method DELETE.

    Parameters

    • what: RegExp

      the regex that the condition should check for

    • init: ContextInitializer

      the action happening when the condition is met

    Returns Context

    Example

    Context.current.delete(//hello/, function() {
    // ctx is available here via [this] keyword
    // Do something
    });
  • Match requests with the method DELETE. This is a shorthand for Context.delete and then Context.action inside.

    Parameters

    Returns Context

    Example

    Context.current.delete(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });

    // Is the same as this:
    Context.current.delete(function() {
    this.action(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Match requests starting with the given path and requests with the method DELETE. This is a shorthand for Context.delete and then Context.action inside.

    Parameters

    • what: string

      the path that the condition should check for

    • init: ActionFunction

      the action happening when the condition is met

    Returns Context

    Example

    Context.current.delete("/hello", async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });

    // Is the same as this:
    Context.current.delete("/hello", function() {
    this.action(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Match requests matching the given regex and requests with the method DELETE. This is a shorthand for Context.delete and then Context.action inside.

    Parameters

    • what: RegExp

      the regex that the condition should check for

    • init: ActionFunction

      the action happening when the condition is met

    Returns Context

    Example

    Context.current.delete(//hello/, async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });

    // Is the same as this:
    Context.current.delete(//hello/, function() {
    this.action(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Search for paths starting with the given path. Is the same as Condition.any

    Parameters

    • what: string

      the path that the condition should check for

    • init: ContextInitializer

      the action happening when the condition is met

    Returns RestfulApi

    Example

    Context.current.describe("/hello", function() {
    // ctx is available here via [this] keyword
    // Do something
    this.action(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Search for paths matching the given regex. Is the same as Condition.any

    Parameters

    • what: RegExp

      the regex that the condition should check for

    • init: ContextInitializer

      the action happening when the condition is met

    Returns RestfulApi

    Example

    Context.current.describe("/hello", function() {
    // ctx is available here via [this] keyword
    // Do something
    this.action(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Search for paths starting with the given path. Is the same as Condition.any This is a shorthand for Context.describe and then Context.action inside.

    Parameters

    • what: string

      the path that the condition should check for

    • init: ActionFunction

      the action happening when the condition is met

    Returns RestfulApi

    Example


    Context.current.describe("/hello", async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });

    // Is the same as this:
    Context.current.describe("/hello", function() {
    this.action(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Search for paths matching the given regex. Is the same as Condition.any This is a shorthand for Context.describe and then Context.action inside.

    Parameters

    • what: RegExp

      the regex that the condition should check for

    • init: ActionFunction

      the action happening when the condition is met

    Returns RestfulApi

    Example

    Context.current.describe(//hello/, async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });

    // Is the same as this:
    Context.current.describe(//hello/, function() {
    this.action(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Match requests with the method GET.

    Parameters

    Returns Context

    Example

    Context.current.get(function() {
    // ctx is available here via [this] keyword
    // Do something
    });
  • Match requests starting with the given path and requests with the method GET.

    Parameters

    • what: string

      the path that the condition should check for

    • init: ContextInitializer

      the action happening when the condition is met

    Returns Context

    Example

    Context.current.get("/hello", function() {
    // ctx is available here via [this] keyword
    // Do something
    });
  • Match requests matching the given regex and requests with the method GET.

    Parameters

    • what: RegExp

      the regex that the condition should check for

    • init: ContextInitializer

      the action happening when the condition is met

    Returns Context

    Example

    Context.current.get(//hello/, function() {
    // ctx is available here via [this] keyword
    // Do something
    });
  • Match requests with the method GET. This is a shorthand for Context.get and then Context.action inside.

    Parameters

    Returns Context

    Example

    Context.current.get(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });

    // Is the same as this:
    Context.current.get(function() {
    this.action(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Match requests starting with the given path and requests with the method GET. This is a shorthand for Context.get and then Context.action inside.

    Parameters

    • what: string

      the path that the condition should check for

    • init: ActionFunction

      the action happening when the condition is met

    Returns Context

    Example

    Context.current.get("/hello", async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });

    // Is the same as this:
    Context.current.get("/hello", function() {
    this.action(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Match requests matching the given regex and requests with the method GET. This is a shorthand for Context.get and then Context.action inside.

    Parameters

    • what: RegExp

      the regex that the condition should check for

    • init: ActionFunction

      the action happening when the condition is met

    Returns Context

    Example

    Context.current.get(//hello/, async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });

    // Is the same as this:
    Context.current.get(//hello/, function() {
    this.action(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Parameters

    • req: Request
    • res: Response
    • __namedParameters: {
          send404?: boolean;
      } = {}
      • Optional send404?: boolean

    Returns Promise<void>

  • Match requests with the method HEAD.

    Parameters

    Returns Context

    Example

    Context.current.head(function() {
    // ctx is available here via [this] keyword
    // Do something
    });
  • Match requests starting with the given path and requests with the method HEAD.

    Parameters

    • what: string

      the path that the condition should check for

    • init: ContextInitializer

      the action happening when the condition is met

    Returns Context

    Example

    Context.current.head("/hello", function() {
    // ctx is available here via [this] keyword
    // Do something
    });
  • Match requests matching the given regex and requests with the method HEAD.

    Parameters

    • what: RegExp

      the regex that the condition should check for

    • init: ContextInitializer

      the action happening when the condition is met

    Returns Context

    Example

    Context.current.head(//hello/, function() {
    // ctx is available here via [this] keyword
    // Do something
    });
  • Match requests with the method HEAD. This is a shorthand for Context.head and then Context.action inside.

    Parameters

    Returns Context

    Example

    Context.current.head(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });

    // Is the same as this:
    Context.current.head(function() {
    this.action(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Match requests starting with the given path and requests with the method HEAD. This is a shorthand for Context.head and then Context.action inside.

    Parameters

    • what: string

      the path that the condition should check for

    • init: ActionFunction

      the action happening when the condition is met

    Returns Context

    Example

    Context.current.head("/hello", async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });

    // Is the same as this:
    Context.current.head("/hello", function() {
    this.action(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Match requests matching the given regex and requests with the method HEAD. This is a shorthand for Context.head and then Context.action inside.

    Parameters

    • what: RegExp

      the regex that the condition should check for

    • init: ActionFunction

      the action happening when the condition is met

    Returns Context

    Example

    Context.current.head(//hello/, async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });

    // Is the same as this:
    Context.current.head(//hello/, function() {
    this.action(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Match requests with the method OPTIONS.

    Parameters

    Returns Context

    Example

    Context.current.options(function() {
    // ctx is available here via [this] keyword
    // Do something
    });
  • Match requests starting with the given path and requests with the method OPTIONS.

    Parameters

    • what: string

      the path that the condition should check for

    • init: ContextInitializer

      the action happening when the condition is met

    Returns Context

    Example

    Context.current.options("/hello", function() {
    // ctx is available here via [this] keyword
    // Do something
    });
  • Match requests matching the given regex and requests with the method OPTIONS.

    Parameters

    • what: RegExp

      the regex that the condition should check for

    • init: ContextInitializer

      the action happening when the condition is met

    Returns Context

    Example

    Context.current.options(//hello/, function() {
    // ctx is available here via [this] keyword
    // Do something
    });
  • Match requests with the method OPTIONS. This is a shorthand for Context.options and then Context.action inside.

    Parameters

    Returns Context

    Example

    Context.current.options(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });

    // Is the same as this:
    Context.current.options(function() {
    this.action(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Match requests starting with the given path and requests with the method OPTIONS. This is a shorthand for Context.options and then Context.action inside.

    Parameters

    • what: string

      the path that the condition should check for

    • init: ActionFunction

      the action happening when the condition is met

    Returns Context

    Example

    Context.current.options("/hello", async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Match requests matching the given regex and requests with the method OPTIONS. This is a shorthand for Context.options and then Context.action inside.

    Parameters

    • what: RegExp

      the regex that the condition should check for

    • init: ActionFunction

      the action happening when the condition is met

    Returns Context

    Example

    Context.current.options(//hello/, async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Match requests with the method PATCH.

    Parameters

    Returns Context

    Example

    Context.current.patch(function() {
    // ctx is available here via [this] keyword
    // Do something
    });
  • Match requests starting with the given path and requests with the method PATCH.

    Parameters

    • what: string

      the path that the condition should check for

    • init: ContextInitializer

      the action happening when the condition is met

    Returns Context

    Example

    Context.current.patch("/hello", function() {
    // ctx is available here via [this] keyword
    // Do something
    });
  • Match requests matching the given regex and requests with the method PATCH.

    Parameters

    • what: RegExp

      the regex that the condition should check for

    • init: ContextInitializer

      the action happening when the condition is met

    Returns Context

    Example

    Context.current.patch(//hello/, function() {
    // ctx is available here via [this] keyword
    // Do something
    });
  • Match requests with the method PATCH. This is a shorthand for Context.patch and then Context.action inside.

    Parameters

    Returns Context

    Example

    Context.current.patch(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });

    // Is the same as this:
    Context.current.patch(function() {
    this.action(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Match requests starting with the given path and requests with the method PATCH. This is a shorthand for Context.patch and then Context.action inside.

    Parameters

    • what: string

      the path that the condition should check for

    • init: ActionFunction

      the action happening when the condition is met

    Returns Context

    Example

    Context.current.patch("/hello", async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });

    // Is the same as this:
    Context.current.patch("/hello", function() {
    this.action(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Match requests matching the given regex and requests with the method PATCH. This is a shorthand for Context.patch and then Context.action inside.

    Parameters

    • what: RegExp

      the regex that the condition should check for

    • init: ActionFunction

      the action happening when the condition is met

    Returns Context

    Example

    Context.current.patch(//hello/, async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });

    // Is the same as this:
    Context.current.patch(//hello/, function() {
    this.action(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Match requests with the method POST.

    Parameters

    Returns RestfulApi

    Example

    Context.current.post(function() {
    // ctx is available here via [this] keyword
    // Do something
    });
  • Match requests starting with the given path and requests with the method POST.

    Parameters

    • what: string

      the path that the condition should check for

    • init: ContextInitializer

      the action happening when the condition is met

    Returns RestfulApi

    Example

    Context.current.post("/hello", function() {
    // ctx is available here via [this] keyword
    // Do something
    });
  • Match requests matching the given regex and requests with the method POST.

    Parameters

    • what: RegExp

      the regex that the condition should check for

    • init: ContextInitializer

      the action happening when the condition is met

    Returns RestfulApi

    Example

    Context.current.post(//hello/, function() {
    // ctx is available here via [this] keyword
    // Do something
    });
  • Match requests with the method POST. This is a shorthand for Context.post and then Context.action inside.

    Parameters

    Returns RestfulApi

    Example

    Context.current.post(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });

    // Is the same as this:
    Context.current.post(function() {
    this.action(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Match requests starting with the given path and requests with the method POST. This is a shorthand for Context.post and then Context.action inside.

    Parameters

    • what: string

      the path that the condition should check for

    • init: ActionFunction

      the action happening when the condition is met

    Returns RestfulApi

    Example

    Context.current.post("/hello", async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });

    // Is the same as this:
    Context.current.post("/hello", function() {
    this.action(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Match requests matching the given regex and requests with the method POST. This is a shorthand for Context.post and then Context.action inside.

    Parameters

    • what: RegExp

      the regex that the condition should check for

    • init: ActionFunction

      the action happening when the condition is met

    Returns RestfulApi

    Example

    Context.current.post(//hello/, async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });

    // Is the same as this:
    Context.current.post(//hello/, function() {
    this.action(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Match requests with the method PUT.

    Parameters

    Returns Context

    Example

    Context.current.put(function() {
    // ctx is available here via [this] keyword
    // Do something
    });
  • Match requests starting with the given path and requests with the method PUT.

    Parameters

    • what: string

      the path that the condition should check for

    • init: ContextInitializer

      the action happening when the condition is met

    Returns Context

    Example

    Context.current.put("/hello", function() {
    // ctx is available here via [this] keyword
    // Do something
    });
  • Match requests matching the given regex and requests with the method PUT.

    Parameters

    • what: RegExp

      the regex that the condition should check for

    • init: ContextInitializer

      the action happening when the condition is met

    Returns Context

    Example

    Context.current.put(//hello/, function() {
    // ctx is available here via [this] keyword
    // Do something
    });
  • Match requests with the method PUT. This is a shorthand for Context.put and then Context.action inside.

    Parameters

    Returns Context

    Example

    Context.current.put(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });

    // Is the same as this:
    Context.current.put(function() {
    this.action(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Match requests starting with the given path and requests with the method PUT. This is a shorthand for Context.put and then Context.action inside.

    Parameters

    • what: string

      the path that the condition should check for

    • init: ActionFunction

      the action happening when the condition is met

    Returns Context

    Example

    Context.current.put("/hello", async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });

    // Is the same as this:
    Context.current.put("/hello", function() {
    this.action(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Match requests matching the given regex and requests with the method PUT. This is a shorthand for Context.put and then Context.action inside.

    Parameters

    • what: RegExp

      the regex that the condition should check for

    • init: ActionFunction

      the action happening when the condition is met

    Returns Context

    Example

    Context.current.put(//hello/, async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });

    // Is the same as this:
    Context.current.put(//hello/, function() {
    this.action(async (req, res) => {
    res.status(200).json({ message: "Hello World!" }).end();
    });
    });
  • Set the data with the given key to the given value

    Parameters

    • key: string

      The key of the data to set

    • value: unknown

      The value to set the data to

    Returns RestfulApi

    The context itself

    Example

    Context.current.setData("key", "value"); // The context itself
    
  • Define the use function for this context. Use functions are called when the context is executed. They can be used to add middleware to the context.

    Parameters

    Returns RestfulApi

    The context itself

    Example

    Context.current.use(function(req, res, next) {
    // ctx is available here via [this] keyword
    // Do something
    });
  • Create a new context and add it to the children of the current context with a condition and a handler

    Parameters

    Returns RestfulApi

    The context itself

    Example

    Context.current.when(new ContextConditionPath("/hello"), function() {
    // ctx is available here via [this] keyword
    // Do something
    });

Generated using TypeDoc