Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages
The information in this browser tab may be outdated, please refresh this page for the latest updates.

Repository URL to install this package:

Details    
@nestjs/platform-fastify / adapters / fastify-adapter.js
Size: Mime:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const common_1 = require("@nestjs/common");
const load_package_util_1 = require("@nestjs/common/utils/load-package.util");
const http_adapter_1 = require("@nestjs/core/adapters/http-adapter");
const fastify = require("fastify");
const cors = require("fastify-cors");
const formBody = require("fastify-formbody");
const Reply = require("fastify/lib/reply");
const pathToRegexp = require("path-to-regexp");
class FastifyAdapter extends http_adapter_1.AbstractHttpAdapter {
    constructor(instanceOrOptions = fastify()) {
        const instance = instanceOrOptions &&
            instanceOrOptions.server
            ? instanceOrOptions
            : fastify(instanceOrOptions);
        super(instance);
    }
    listen(port, ...args) {
        return this.instance.listen(port, ...args);
    }
    reply(response, body, statusCode) {
        const isNativeResponse = typeof response.status !== 'function';
        if (isNativeResponse) {
            const fastifyContext = {
                preSerialization: null,
                preValidation: [],
                preHandler: [],
                onSend: [],
                onError: [],
            };
            response = new Reply(response, fastifyContext, {});
        }
        if (statusCode) {
            response.status(statusCode);
        }
        return response.send(body);
    }
    status(response, statusCode) {
        return response.code(statusCode);
    }
    render(response, view, options) {
        return response.view(view, options);
    }
    redirect(response, statusCode, url) {
        const code = statusCode ? statusCode : common_1.HttpStatus.FOUND;
        return response.status(code).redirect(url);
    }
    setErrorHandler(handler, prefix) {
        return this.instance.setErrorHandler(handler);
    }
    setNotFoundHandler(handler, prefix) {
        return this.instance.setNotFoundHandler(handler);
    }
    getHttpServer() {
        return this.instance.server;
    }
    getInstance() {
        return this.instance;
    }
    register(...args) {
        return this.instance.register(...args);
    }
    inject(...args) {
        return this.instance.inject(...args);
    }
    close() {
        return this.instance.close();
    }
    initHttpServer(options) {
        this.httpServer = this.instance.server;
    }
    useStaticAssets(options) {
        return this.register(load_package_util_1.loadPackage('fastify-static', 'FastifyAdapter.useStaticAssets()', () => require('fastify-static')), options);
    }
    setViewEngine(options) {
        return this.register(load_package_util_1.loadPackage('point-of-view', 'FastifyAdapter.setViewEngine()'), options, () => require('point-of-view'));
    }
    setHeader(response, name, value) {
        return response.header(name, value);
    }
    getRequestHostname(request) {
        return request.hostname;
    }
    getRequestMethod(request) {
        return request.raw ? request.raw.method : request.method;
    }
    getRequestUrl(request) {
        return request.raw ? request.raw.url : request.url;
    }
    enableCors(options) {
        this.register(cors, options);
    }
    registerParserMiddleware() {
        this.register(formBody);
    }
    createMiddlewareFactory(requestMethod) {
        return (path, callback) => {
            const re = pathToRegexp(path);
            const normalizedPath = path === '/*' ? '' : path;
            this.instance.use(normalizedPath, (req, res, next) => {
                const queryParamsIndex = req.originalUrl.indexOf('?');
                const pathname = queryParamsIndex >= 0
                    ? req.originalUrl.slice(0, queryParamsIndex)
                    : req.originalUrl;
                if (!re.exec(pathname + '/') && normalizedPath) {
                    return next();
                }
                if (requestMethod === common_1.RequestMethod.ALL ||
                    req.method === common_1.RequestMethod[requestMethod]) {
                    return callback(req, res, next);
                }
                next();
            });
        };
    }
    getType() {
        return 'fastify';
    }
    registerWithPrefix(factory, prefix = '/') {
        return this.instance.register(factory, { prefix });
    }
}
exports.FastifyAdapter = FastifyAdapter;