Options
All
  • Public
  • Public/Protected
  • All
Menu

prismy

Index

Type aliases

AsyncSelector

AsyncSelector<T>: function

An asynchronous argument selector

Type parameters

  • T

Type declaration

    • Parameters

      Returns Promise<T>

ContextHandler

ContextHandler: function

Type declaration

Promisable

Promisable<T>: T | Promise<T>

Type parameters

  • T

Selector

Selector<T>: SyncSelector<T> | AsyncSelector<T>

An argument selector to extract arguments for the handler

Type parameters

  • T

Selectors

Selectors<T>: object

Type parameters

  • T

Type declaration

SyncSelector

SyncSelector<T>: function

A Synchronous argument selector

Type parameters

  • T

Type declaration

Unpromise

Unpromise<T>: Unpromise<T>

Type parameters

  • T

Variables

Const querySymbol

querySymbol: unique symbol = Symbol('prismy-query')

Const urlSymbol

urlSymbol: unique symbol = Symbol('prismy-url')

Functions

compileHandler

  • compileHandler<A, R>(selectors: Selectors<A>, handler: function): function
  • Compile a handler into a runnable function by resolving selectors and injecting the arguments into the handler.

    internal

    Type parameters

    • A: any[]

    • R

    Parameters

    • selectors: Selectors<A>

      Selectors to gather handler arguments from

    • handler: function

      Handler to be compiled

        • (...args: A): R
        • Parameters

          • Rest ...args: A

          Returns R

    Returns function

    compiled handler ready to be used

      • Parameters

        Returns Promise<R>

Const contextSelector

createBufferBodySelector

  • Factory function that creates a selector to extract the request body in a buffer

    example

    Simple example

    
    const bufferBodySelector = createBufferBodySelector({
     limit: "1mb"
    })
    
    const prismyHandler = prismy(
     [bufferBodySelector],
     bufferBody => {
       ...
     }
    )
    

    Parameters

    Returns AsyncSelector<string | Buffer>

    A selector for extracting request body in a buffer

createJsonBodySelector

  • Factory function to create a selector to extract the JSON encoded body of a request

    example

    Simple example

    
    const jsonBodySelector = createJsonBodySelector({
     limit: "1mb"
    })
    
    const prismyHandler = prismy(
     [jsonBodySelector],
     body => {
       ...
     }
    )
    
    throws

    Throws an Error with 400 if content type is not application/json

    Parameters

    Returns AsyncSelector<any>

    A selector for JSON body requests

createTextBodySelector

  • Factory function to create a selector to extract the text body from a request

    example

    Simple example

    
    const textBodySelector = createTextBodySelector({
     limit: "1mb"
    })
    
    const prismyHandler = prismy(
     [textBodySelector],
     body => {
       ...
     }
    )
    

    Parameters

    Returns AsyncSelector<string>

    a selector for text request bodies

createUrlEncodedBodySelector

  • Factory function to create a selector to extract the url encoded body from a request

    example

    Simple example

    
    const urlEncodedBodySelector = createUrlEncodedBodySelector({
     limit: "1mb"
    })
    
    const prismyHandler = prismy(
     [urlEncodedBodySelector],
     body => {
       ...
     }
    )
    
    throws

    Throws an Error with 400 code if parsing fails

    Parameters

    Returns AsyncSelector<ParsedUrlQuery>

    selector for url encoded request bodies

createWithErrorHandler

  • Factory function to create a simple error handler middleware

    remarks

    Catches errors thrown and returns either text or json response depending on configuration. Will display either the string error message or the full stack if dev = true

    Parameters

    • Default value __namedParameters: object = {}
      • dev: boolean
      • json: boolean

    Returns PrismyPureMiddleware

    A prismy compatible middleware error handler

Const headersSelector

  • headersSelector(context: Context): IncomingHttpHeaders
  • A selector to extract the headers of a request

    Parameters

    • context: Context

      The request context

    Returns IncomingHttpHeaders

    The request headers

isContentTypeIsApplicationJSON

  • isContentTypeIsApplicationJSON(contentType: string | undefined): boolean

Const methodSelector

  • methodSelector(__namedParameters: object): undefined | string
  • Selector to extract the HTTP method from the request

    Parameters

    • __namedParameters: object
      • req: IncomingMessage

    Returns undefined | string

    the http request method

middleware

middlewarex

  • Factory function to create a prismy compatible middleware. Accepts selectors to help with testing, DI etc.

    internal

    Type parameters

    • A: any[]

    Parameters

    • selectors: Selectors<A>

      Tuple of selectors

    • mhandler: function

      Middleware handler

    Returns PrismyMiddleware<A>

prismy

prismyx

  • Generates a handler to be used by http.Server

    remarks

    For most cases use prismy Use this function if you require more than 12 selectors or need to write a custom prismy function.

    Type parameters

    • A: any[]

    Parameters

    Returns PrismyRequestListener<A>

Const querySelector

  • querySelector(context: Context): ParsedUrlQuery
  • Selector to extract the parsed query from the request URL

    Parameters

    Returns ParsedUrlQuery

    a selector for the url query

redirect

  • redirect(location: string, statusCode?: number, extraHeaders?: OutgoingHttpHeaders): ResponseObject<null>
  • Factory function for easily generating a redirect response

    Parameters

    • location: string

      URL to redirect to

    • Default value statusCode: number = 302

      Status code for response. Defaults to 302

    • Default value extraHeaders: OutgoingHttpHeaders = {}

      Additional headers of the response

    Returns ResponseObject<null>

    A redirect {@link ResponseObject | response} to location

res

  • res<B>(body: B, statusCode?: number, headers?: OutgoingHttpHeaders): ResponseObject<B>
  • Factory function for creating http responses

    Type parameters

    • B

    Parameters

    • body: B

      Body of the response

    • Default value statusCode: number = 200

      HTTP status code of the response

    • Default value headers: OutgoingHttpHeaders = {}

      HTTP headers for the response

    Returns ResponseObject<B>

    A {@link ResponseObject | response object} containing necessary information

resolveSelectors

  • Executes the selectors and produces an array of args to be passed to a handler

    internal

    Type parameters

    • A: any[]

    Parameters

    • context: Context

      Context object to be passed to the selectors

    • selectors: Selectors<A>

      array of selectos

    Returns Promise<A>

    arguments for a handler

setBody

  • Creates a new response with a new body

    Type parameters

    • B1

    • B2

    Parameters

    • resObject: ResponseObject<B1>

      The response to set the body on

    • body: B2

      Body to be set

    Returns ResponseObject<B2>

    New {@link ResponseObject | response} with the new body

setHeaders

  • Creates a new response overriting all headers with new ones.

    Type parameters

    • B

    Parameters

    • resObject: ResponseObject<B>

      response to set new headers on

    • headers: OutgoingHttpHeaders

      HTTP response headers to set

    Returns ResponseObject<B>

    New {@link ResponseObject | response} with new headers set

setStatusCode

  • Creates a new response with a new status code

    Type parameters

    • B

    Parameters

    • resObject: ResponseObject<B>

      The response to set the code to

    • statusCode: number

      HTTP status code

    Returns ResponseObject<B>

    New {@link ResponseObject | response} with the new statusCode

updateHeaders

  • Creates a new response with the extra headers.

    Type parameters

    • B

    Parameters

    • resObject: ResponseObject<B>

      The response to add the new headers to

    • extraHeaders: OutgoingHttpHeaders

      HTTP response headers

    Returns ResponseObject<B>

    New {@link ResponseObject | response} with the extra headers

Const urlSelector

  • urlSelector(context: Context): UrlWithStringQuery
  • Selector for extracting the requested URL

    Parameters

    Returns UrlWithStringQuery

    The url of the request

Generated using TypeDoc