Skip to content

Function: fastifyJSONEnvelope() ​

fastifyJSONEnvelope(options?): FastifyResponder

Create a responder for fastifyHTTPEntrypoint that wraps every response in the envelope {errno, errMsg, data}: a result as {errno: null, errMsg: null, data} (data is null when the action returned nothing, and its status is not turned into 204), an error as {errno, errMsg, data: null} with the error's status, the errorStatus option, or for the errors of the actions the actionErrorStatus option; the extra option adds fields to both.

Parameters ​

options? ​

FastifyJSONEnvelopeOptions

The envelope options.

Returns ​

FastifyResponder

The responder, to give to the respond option of fastifyHTTPEntrypoint.

Example ​

typescript
import {Application} from 'lakutata'
import {buildEntrypoints, Controller} from 'lakutata/com/entrypoint'
import {GET} from 'lakutata/decorator/ctrl'
import {fastifyHTTPEntrypoint, fastifyJSONEnvelope} from 'lakutata/adapter/fastify'

class PingController extends Controller {
    //GET /ping answers {"errno": null, "errMsg": null, "data": "pong"}
    @GET('/ping')
    public async ping(): Promise<string> {
        return 'pong'
    }
}

Application.run({
    id: 'web.app',
    name: 'Web',
    components: {
        entrypoint: buildEntrypoints({
            controllers: [PingController],
            //The errors of the actions answered with 200 too (as the applications of lakutata 2.x), the routes not
            //found and the errors of the hooks with their status (404, 401…); every envelope with
            //its timestamp ({"errno": null, "errMsg": null, "data": "pong", "timestamp": 1767225600000})
            http: fastifyHTTPEntrypoint({
                port: 8080,
                respond: fastifyJSONEnvelope({
                    actionErrorStatus: 200,
                    extra: (): Record<string, unknown> => ({timestamp: Date.now()})
                })
            })
        })
    },
    bootstrap: ['entrypoint']
})

Released under the MIT License.