<commit_before>import * as express from "express";

import { Option, option } from "ts-option";
import { left, right } from "../either";

import { IRequestMiddleware } from "../request_middleware";

import { IResponseErrorInternal, ResponseErrorInternal } from "../response";

import { IContext } from "azure-function-express";

const CONTEXT_IDENTIFIER = "context";

export function setAppContext(
  app: express.Express,
  context: IContext<{}>
): void {
  app.set(CONTEXT_IDENTIFIER, context);
}

export function getAppContext<T>(
  request: express.Request
): Option<IContext<T>> {
  return option(request.app.get(CONTEXT_IDENTIFIER));
}

/**
 * Returns a request middleware that extracts the Azure request context
 * from the request.
 *
 * @param T The type of the bindings found in the context.
 */
export function ContextMiddleware<T>(): IRequestMiddleware<
  IResponseErrorInternal,
  IContext<T> | void
> {
  return request =>
    new Promise(resolve =>
      getAppContext<T>(request).match({
        none: () =>
          resolve(
            left(ResponseErrorInternal("Cannot get context from request"))
          ),
        some: context => resolve(right(context))
      })
    );
}
<commit_msg>[#151970170] ContextMiddleware never returns a void<commit_after>import * as express from "express";

import { Option, option } from "ts-option";
import { left, right } from "../either";

import { IRequestMiddleware } from "../request_middleware";

import { IResponseErrorInternal, ResponseErrorInternal } from "../response";

import { IContext } from "azure-function-express";

const CONTEXT_IDENTIFIER = "context";

export function setAppContext(
  app: express.Express,
  context: IContext<{}>
): void {
  app.set(CONTEXT_IDENTIFIER, context);
}

export function getAppContext<T>(
  request: express.Request
): Option<IContext<T>> {
  return option(request.app.get(CONTEXT_IDENTIFIER));
}

/**
 * Returns a request middleware that extracts the Azure request context
 * from the request.
 *
 * @param T The type of the bindings found in the context.
 */
export function ContextMiddleware<T>(): IRequestMiddleware<
  IResponseErrorInternal,
  IContext<T>
> {
  return request =>
    new Promise(resolve =>
      getAppContext<T>(request).match({
        none: () =>
          resolve(
            left(ResponseErrorInternal("Cannot get context from request"))
          ),
        some: context => resolve(right(context))
      })
    );
}

{'fraction_non_alphanumeric': 0.12403100775193798, 'fraction_numerical': 0.0036719706242350062, 'mean_word_length': 3.865079365079365, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '663688', 'n_tokens_mistral': 696, 'n_tokens_neox': 694, 'n_words': 194}