import { is as typeis } from 'type-is' ; import * as accepts from 'accepts' ; import { FaaSOriginContext } from '@midwayjs/faas-typings' ; import * as qs from 'querystring' ; import * as only from 'only' ; import { isPlainObject } from './util' ; const BODY = Symbol . for ( 'ctx#body' ) ; export const request = { _accept : null , req : null , get host ( ) : string { return this . get ( 'host' ) ; } , return { String } api get hostname ( ) { const host = this . host ; if ( ! host ) return '' ; if ( '[' === host [ 0 ] ) return '' ; return host . split ( ':' , 1 ) [ 0 ] ; } , accepts ( ... args ) { return this . accept . types ( ... args ) ; } , get accept ( ) { return this . _accept || ( this . _accept = accepts ( this as any ) ) ; } , param { String | Array } encoding return { String | Array } api acceptsEncodings ( ... args ) { return this . accept . encodings ( ... args ) ; } , param { String | Array } charset return { String | Array } api acceptsCharsets ( ... args ) { return this . accept . charsets ( ... args ) ; } , param { String | Array } lang return { Array | String } api acceptsLanguages ( ... args ) { return this . accept . languages ( ... args ) ; } , /**
     * faas origin context object
     */ get originEvent ( ) { return this . req . getOriginEvent ?. ( ) || { } ; } , get originContext ( ) : FaaSOriginContext { return this . req . getOriginContext ?. ( ) || { } ; } , get ip ( ) { return this . req ?. clientIP || this . req . ip ; } , get url ( ) { return this . req . url ; } , get path ( ) { return this . req . path ; } , set path ( value ) { this . req . path = value ; } , get method ( ) { return this . req . method ; } , get headers ( ) { return this . req . headers ; } , get header ( ) { return this . req . headers ; } , get query ( ) { return this . req ?. queries || this . req . query ; } , get body ( ) { const method = this . method . toLowerCase ( ) ; if ( [ 'get' , 'head' , 'options' ] . includes ( method ) ) { return undefined ; } if ( this [ BODY ] ) { return this [ BODY ] ; } let body = this . req . body ; if ( isPlainObject ( body ) ) { this . req . bodyParsed = true ; } if ( this . req . bodyParsed ) { this [ BODY ] = this . req . body ; return this [ BODY ] ; } if ( Buffer . isBuffer ( body ) ) { body = Buffer . from ( body ) . toString ( ) ; } switch ( typeis ( this . get ( 'content-type' ) , [ 'urlencoded' , 'json' ] ) ) { case 'json' : try { this [ BODY ] = JSON . parse ( body ) ; } catch { throw new Error ( 'invalid json received' ) ; } break ; case 'urlencoded' : try { this [ BODY ] = qs . parse ( body ) ; } catch { throw new Error ( 'invalid urlencoded received' ) ; } break ; default : this [ BODY ] = body ; } return this [ BODY ] ; } , get params ( ) { return this . req . pathParameters || { } ; } , is ( type , ... types ) { return typeis ( this . get ( 'content-type' ) , type , ... types ) ; } , param { String } field return { String } api get ( field ) { switch ( ( field = field . toLowerCase ( ) ) ) { case 'referer' : case 'referrer' : return this . headers . referrer || this . headers . referer || '' ; default : return this . headers [ field ] || '' ; } } , return { Object } api inspect ( ) { if ( ! this . req ) return ; return this . toJSON ( ) ; } , return { Object } api toJSON ( ) { return only ( this , [ 'method' , 'url' , 'header' ] ) ; } , get protocol ( ) { const proto = this . get ( 'X-Forwarded-Proto' ) ; return proto ? proto . split ( /\s*,\s*/ , 1 ) [ 0 ] : 'http' ; } , return { Boolean } api get secure ( ) { return this . protocol === 'https' ; } , } ;