Repository URL to install this package:
|
Version:
0.0.3 ▾
|
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
}