Options
All
  • Public
  • Public/Protected
  • All
Menu

gobits

Index

Type aliases

GlobalOptions

GlobalOptions: { baseUrl: string; timeout: number; type?: string; useDefaultMiddlewares: boolean }

Define the global options for the requests. Some options can be overridden by the request options.

param timeout

The timeout for the request.

param type

The type of the request (json or form). This will be handled by the built-in middleware

param useDefaultMiddlewares

Use the built-in middlewares provided by gobits (cannot be overriden)

param baseUrl

The base url for the request. (cannot be overriden)

Type declaration

  • baseUrl: string
  • timeout: number
  • Optional type?: string
  • useDefaultMiddlewares: boolean

Middleware

Middleware: (req: Request, res: Response, next: NextFn, responded?: boolean) => Promise<any> | void

Type declaration

    • (req: Request, res: Response, next: NextFn, responded?: boolean): Promise<any> | void
    • A middleware placed in the chain to handle the request and response. There are three stages with three states of the response:

      • On the first pass, the request object will be passed through all middlewares. (state = Pending)
      • After the first pass, an actual request will be sent to the server. (state = Responded)
      • After receiving the response, the response will be passed through all middlewares. (state = Completed)

      Of course, you can also skip parts of the chain by marking the response as completed or responded. The response can be marked as follows:

      res.markAsResponded(); 
      

      or

      res.markAsCompleted(); 
      

      The middleware must return void or a promise. Therefore, the middleware can also be asynchronous

      example
      const errorLogger = async (req, res, next, responded) => {
      if (!res.ok) {
      await writeToLog(`${req.url} failed with ${res.status}`);
      }
      next();
      }

      Parameters

      • req: Request

        The Gobits request object.

      • res: Response

        The Gobits response object.

      • next: NextFn

        The next function to call. This function should be called when the middleware is done.

      • Optional responded: boolean

        Whether the middleware is in the first (false) or second pass (true).

      Returns Promise<any> | void

RequestOptions

RequestOptions: { body?: any; bodyParser?: keyof typeof BodyParsers | ((response: globalThis.Response) => Promise<any>); headers?: Headers; query?: QueryParams; timeout?: number; type?: string }

An object that contains the options for a single request. Those options will override the global options. You can also add any custom property to this object, so that your middlewares can use it.

param headers

The headers to send with the request.

param query

The query parameters to send with the request.

param body

The body to send with the request.

param bodyParser

The parser to use to parse the body, default is text. Available (text, blob)

param timeout

The timeout for the request (in miliseconds).

param type

The type of the request to be handled by the middleware. If type is json, the middleware will automatically parse the request and response body as json.

Type declaration

  • [p: string]: any
  • Optional body?: any
  • Optional bodyParser?: keyof typeof BodyParsers | ((response: globalThis.Response) => Promise<any>)
  • Optional headers?: Headers
  • Optional query?: QueryParams
  • Optional timeout?: number
  • Optional type?: string

Generated using TypeDoc