Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/logger / src / logging.ts
Size: Mime:
import { Request as ExpressRequest, Response as ExpressResponse } from 'express'
import { fromNodeHttpRequestToFetchRequest } from '@skava/request/dist/deps/fromNodeHttpRequestToFetchRequest'
import { fromHeadersToSerialized } from '@skava/request/dist/deps/fromHeadersToSerialized'
import { isEmpty, isObj } from 'exotic'

export interface SerializedRes {
  statusCode: string | number
  headers: {[key: string]: any}
  body?: {[key: string]: any}
}

export function fromNodeHttpResponseToFetchResponse(res: ExpressResponse) {
  const obj: SerializedRes = {
    statusCode: res.statusCode,
    headers: res.getHeaders(),
  }
  return obj
}

export type MethodType =  'get' | 'post' | 'put' | 'options' | 'patch' | 'head'
export interface SerializedReq {
  headers: {[key: string]: any}
  method: MethodType
  url: string
  body?: {[key: string]: any}
}

export function fromReqToObj(req: ExpressRequest) {
  const nodeReq = fromNodeHttpRequestToFetchRequest(req)
  const obj: SerializedReq = {
    headers: fromHeadersToSerialized(nodeReq.headers as any),
    method: nodeReq.method as MethodType,
    url: nodeReq.url,
  }
  if (isObj(req.body) && isEmpty(req.body) === false) {
    obj.body = req.body
  }
  return obj
}