Skip to main content

WebContext

@webda/core


Class: WebContext<T, P, U>

Defined in: packages/core/src/contexts/webcontext.ts:24

This represent in fact a WebContext In 3.0 an abstract version of Context will replace this (closer to OperationContext)

Extends​

Type Parameters​

T​

T = any

P​

P = any

U​

U = any

Properties​

_body​

protected _body: string

Defined in: packages/core/src/contexts/operationcontext.ts:44

Output

Inherited from​

OperationContext._body


_cookie: Map<string, Cookie>

Defined in: packages/core/src/contexts/webcontext.ts:37

Cookies to send back


_ended​

_ended: Promise<any> = undefined

Defined in: packages/core/src/contexts/webcontext.ts:44


_outputHeaders​

protected _outputHeaders: OutgoingHttpHeaders

Defined in: packages/core/src/contexts/webcontext.ts:28

Contains the response headers


_promises​

_promises: Promise<any>[]

Defined in: packages/core/src/contexts/icontext.ts:33

Contain all registered promises to this context

Inherited from​

OperationContext._promises


_route​

_route: any

Defined in: packages/core/src/contexts/webcontext.ts:42


_sanitized​

protected _sanitized: any

Defined in: packages/core/src/contexts/operationcontext.ts:39

Contain the sanitized request body if computed

Inherited from​

OperationContext._sanitized


_stream​

_stream: Writable

Defined in: packages/core/src/contexts/operationcontext.ts:80

Output stream

Inherited from​

OperationContext._stream


caches​

caches: any

Defined in: packages/core/src/contexts/icontext.ts:19

Get the caches linked to the context

Inherited from​

OperationContext.caches


extensions​

protected extensions: object

Defined in: packages/core/src/contexts/icontext.ts:23

Allow extensions

Index Signature​

[key: string]: any

Inherited from​

OperationContext.extensions


flushed​

protected flushed: boolean = false

Defined in: packages/core/src/contexts/icontext.ts:37

If the headers were flushed

Inherited from​

OperationContext.flushed


headers​

headers: Map<string, string>

Defined in: packages/core/src/contexts/webcontext.ts:41


headersFlushed​

protected headersFlushed: boolean

Defined in: packages/core/src/contexts/webcontext.ts:49

If headers were flushed


parameters​

parameters: P

Defined in: packages/core/src/contexts/operationcontext.ts:51

Inherited from​

OperationContext.parameters


session​

protected session: Session

Defined in: packages/core/src/contexts/operationcontext.ts:35

Session

Inherited from​

OperationContext.session


statusCode​

statusCode: number

Defined in: packages/core/src/contexts/webcontext.ts:33

Current status code

Methods​

addAsyncRequest()​

addAsyncRequest(promise): void

Defined in: packages/core/src/contexts/operationcontext.ts:131

Register a promise with the context

Parameters​

promise​

any

the promise to add

Returns​

void

Inherited from​

OperationContext.addAsyncRequest


clearInput()​

clearInput(): void

Defined in: packages/core/src/contexts/operationcontext.ts:156

Remove the input

Returns​

void

Inherited from​

OperationContext.clearInput


cookie(param, value, options?): void

Defined in: packages/core/src/contexts/webcontext.ts:175

For compatibility reason

Parameters​

param​

any

the parameter name

value​

any

the value to set

options?​

any = undefined

the options

Returns​

void


createStream()​

createStream(): void

Defined in: packages/core/src/contexts/operationcontext.ts:275

Create a buffer stream

Returns​

void

Inherited from​

OperationContext.createStream


end()​

end(): Promise<any>

Defined in: packages/core/src/contexts/webcontext.ts:241

Flush the request

Returns​

Promise<any>

the result

Emits​

'finish' event

Throws​

Error if the request was already ended

Overrides​

OperationContext.end


flushHeaders()​

flushHeaders(): Promise<void>

Defined in: packages/core/src/contexts/webcontext.ts:377

Returns​

Promise<void>

Overrides​

OperationContext.flushHeaders


getCurrentUser()​

getCurrentUser<K>(refresh?): Promise<K>

Defined in: packages/core/src/contexts/operationcontext.ts:331

Get the current user from session

Type Parameters​

K​

K extends IUser

Parameters​

refresh?​

boolean = false

whether to refresh

Returns​

Promise<K>

the result

Inherited from​

OperationContext.getCurrentUser


getCurrentUserId()​

getCurrentUserId(): string

Defined in: packages/core/src/contexts/operationcontext.ts:351

Get the current user id from session

A session waiting for its second factor (mfa: "pending") is not logged: it has no current user. Code that needs the pending user reads getSession().userId.

Returns​

string

the result

Inherited from​

OperationContext.getCurrentUserId


getExtension()​

getExtension<K>(name): K

Defined in: packages/core/src/contexts/operationcontext.ts:86

Get an extension of the context

Type Parameters​

K​

K = any

Parameters​

name​

string

of the extension

Returns​

K

extension object

Inherited from​

OperationContext.getExtension


getHttpContext()​

getHttpContext(): HttpContext

Defined in: packages/core/src/contexts/webcontext.ts:66

Get current http context

Returns​

HttpContext

the result


getInput()​

getInput(sanitizedOptions?): Promise<T>

Defined in: packages/core/src/contexts/webcontext.ts:87

A multipart body (file upload) is never JSON: return the default value instead of letting the parent try to JSON.parse it and log an error. The raw body stays available to getRawInput()/getRawStream().

Parameters​

sanitizedOptions?​

IOptions & object

Returns​

Promise<T>

Overrides​

OperationContext.getInput


getLocale()​

getLocale(): string

Defined in: packages/core/src/contexts/webcontext.ts:330

Get the request locale if found

Returns​

string

the result


getOutput()​

getOutput(): string

Defined in: packages/core/src/contexts/operationcontext.ts:139

Get output as string, if a OutputStream is provided it will returned null

Returns​

string

the result string

Inherited from​

OperationContext.getOutput


getOutputStream()​

getOutputStream(): Promise<Writable>

Defined in: packages/core/src/contexts/operationcontext.ts:240

Get the HTTP stream to output raw data

Returns​

Promise<Writable>

the result

Inherited from​

OperationContext.getOutputStream


getParameters()​

getParameters(): P

Defined in: packages/core/src/contexts/operationcontext.ts:104

Get the parameters

Returns​

P

the result

Inherited from​

OperationContext.getParameters


getRawInput()​

getRawInput(limit?, timeout?): Promise<Buffer<ArrayBufferLike>>

Defined in: packages/core/src/contexts/webcontext.ts:97

Parameters​

limit?​

number = ...

timeout?​

number = 60000

Returns​

Promise<Buffer<ArrayBufferLike>>

Overrides​

OperationContext.getRawInput


getRawInputAsString()​

getRawInputAsString(limit?, timeout?, encoding?): Promise<string>

Defined in: packages/core/src/contexts/webcontext.ts:73

Parameters​

limit?​

number = ...

timeout?​

number = 60000

encoding?​

string

Returns​

Promise<string>

Overrides​

OperationContext.getRawInputAsString


getRawStream()​

getRawStream(): Readable

Defined in: packages/core/src/contexts/webcontext.ts:104

Returns​

Readable

Overrides​

OperationContext.getRawStream


getRequest()​

getRequest(): IncomingMessage

Defined in: packages/core/src/contexts/webcontext.ts:304

Retrieve a http.IncomingMessage valid from Context

Need more testing

Returns​

IncomingMessage

the result


getRequestBody()​

getRequestBody(sanitizedOptions?): Promise<T>

Defined in: packages/core/src/contexts/webcontext.ts:278

Alias to keep compatibility with WebContext

Parameters​

sanitizedOptions?​

any = ...

the sanitized options

Returns​

Promise<T>

the result


getResponseBody()​

getResponseBody(): any

Defined in: packages/core/src/contexts/webcontext.ts:291

Get request body

Returns​

any

the result


getResponseCode()​

getResponseCode(): number

Defined in: packages/core/src/contexts/webcontext.ts:158

Returns​

number

the result


getResponseCookies()​

getResponseCookies(): Map<string, Cookie>

Defined in: packages/core/src/contexts/webcontext.ts:187

Get all cookies set on the response

Returns​

Map<string, Cookie>

the result map


getResponseHeaders()​

getResponseHeaders(): OutgoingHttpHeaders

Defined in: packages/core/src/contexts/webcontext.ts:370

Return the merged response headers — defaults set by the constructor (_outputHeaders) overlaid with any headers added via setHeader() / writeHead(). Same merge order as flushHeaders writes to the wire, so callers (logging, capture, transports) see exactly what the client will receive.

Returns​

OutgoingHttpHeaders

the merged response headers

Overrides​

OperationContext.getResponseHeaders


getResponseSize()​

getResponseSize(): number

Defined in: packages/core/src/contexts/webcontext.ts:230

Return the response size

Returns​

number

the result


getSession()​

getSession<K>(): K

Defined in: packages/core/src/contexts/operationcontext.ts:249

Get linked session

Type Parameters​

K​

K = Session

Returns​

K

the result

Inherited from​

OperationContext.getSession


hasFlushedHeaders()​

hasFlushedHeaders(): boolean

Defined in: packages/core/src/contexts/webcontext.ts:344

Return true if Headers got flushed already

Returns​

boolean

the result


init()​

init(force?): Promise<WebContext<T, P, U>>

Defined in: packages/core/src/contexts/webcontext.ts:415

Initialize the web context, loading session and setting up stream listeners

Parameters​

force?​

boolean = false

whether to force the operation

Returns​

Promise<WebContext<T, P, U>>

this for chaining

Overrides​

OperationContext.init


isEnded()​

isEnded(): Promise<any>

Defined in: packages/core/src/contexts/webcontext.ts:195

Whether the response has been ended

Returns​

Promise<any>

the result


isGlobalContext()​

isGlobalContext(): boolean

Defined in: packages/core/src/contexts/icontext.ts:43

Whether this is the global (non-request-scoped) context

Returns​

boolean

true if the condition is met

Inherited from​

OperationContext.isGlobalContext


json()​

json(obj): this

Defined in: packages/core/src/contexts/webcontext.ts:221

Express response allow answer to be sent this way

Parameters​

obj​

any

the target object

Returns​

this

this for chaining


newSession()​

newSession(): Session

Defined in: packages/core/src/contexts/operationcontext.ts:257

Create and assign a new empty session

Returns​

Session

the result

Inherited from​

OperationContext.newSession


parameter()​

parameter(name, defaultValue?): any

Defined in: packages/core/src/contexts/operationcontext.ts:96

Get one parameter

Parameters​

name​

string

the name to use

defaultValue?​

any = undefined

the default value

Returns​

any

the result

Inherited from​

OperationContext.parameter


pipeline()​

pipeline(stream1, ...streams): Promise<void>

Defined in: packages/core/src/contexts/icontext.ts:149

Pipeline streams into the output stream

Parameters​

stream1​

ReadableStream

the first stream

streams​

...(ReadWriteStream | PipelineOptions)[]

additional streams

Returns​

Promise<void>

See​

https://nodejs.org/api/stream.html#streampipelinestreams-options

Inherited from​

OperationContext.pipeline


redirect()​

redirect(url): void

Defined in: packages/core/src/contexts/webcontext.ts:166

Redirect to another url

Parameters​

url​

string

the URL

Returns​

void


registerPromise()​

registerPromise(promise): void

Defined in: packages/core/src/contexts/icontext.ts:56

Register a promise with the context

Parameters​

promise​

any

the promise to add

Returns​

void

Inherited from​

OperationContext.registerPromise


reinit()​

reinit(): void

Defined in: packages/core/src/contexts/operationcontext.ts:265

Remove sanitized body

Returns​

void

Inherited from​

OperationContext.reinit


resetResponse()​

resetResponse(): void

Defined in: packages/core/src/contexts/webcontext.ts:111

Remove everything that was about to be sent

Returns​

void

Overrides​

OperationContext.resetResponse


setExtension()​

setExtension(name, extension): this

Defined in: packages/core/src/contexts/operationcontext.ts:122

Parameters​

name​

string

to add

extension​

any

object to store

Returns​

this

this for chaining

Inherited from​

OperationContext.setExtension


setFlushedHeaders()​

setFlushedHeaders(status?): void

Defined in: packages/core/src/contexts/webcontext.ts:352

Set flushed header status

Parameters​

status?​

boolean = true

the HTTP status code

Returns​

void


setHeader()​

setHeader(name, value): void

Defined in: packages/core/src/contexts/icontext.ts:104

Set a header in the response

Parameters​

name​

string

the name to use

value​

string | number | string[]

the value to set

Returns​

void

Inherited from​

OperationContext.setHeader


setHttpContext()​

setHttpContext(httpContext): void

Defined in: packages/core/src/contexts/webcontext.ts:57

Set current http context

Parameters​

httpContext​

HttpContext

current http context

Returns​

void


setParameters()​

setParameters(params): void

Defined in: packages/core/src/contexts/operationcontext.ts:112

Set the parameters

Parameters​

params​

P

the service parameters

Returns​

void

Inherited from​

OperationContext.setParameters


setSession()​

setSession(session): void

Defined in: packages/core/src/contexts/operationcontext.ts:73

Set the session

Parameters​

session​

Session

the session object

Returns​

void

Inherited from​

OperationContext.setSession


status()​

status(code): this

Defined in: packages/core/src/contexts/webcontext.ts:210

Express response allow statusCode to be defined this way

Parameters​

code​

number

to return

Returns​

this

this for chaining


write()​

write(output, encoding?, cb?): boolean

Defined in: packages/core/src/contexts/webcontext.ts:126

Write data to the client

Parameters​

output​

U

If it is an object it will be serializeb with toPublicJSON, if it is a String it will be appended to the result, if it is a buffer it will replace the result

encoding?​

string

the encoding to use

cb?​

(error) => void

the callback function

Returns​

boolean

true if the condition is met

Overrides​

OperationContext.write


writeHead()​

writeHead(statusCode, headers?): this

Defined in: packages/core/src/contexts/webcontext.ts:144

Write the http return code and some headers Those headers are not flushed yet so can still be overwritten

Parameters​

statusCode​

number

to return to the client

headers?​

OutgoingHttpHeaders = undefined

to add to the response

Returns​

this

this for chaining

Overrides​

OperationContext.writeHead