Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Fastify is not implemented yet #9

Open
bardak-dev opened this issue Mar 1, 2020 · 9 comments
Open

Error: Fastify is not implemented yet #9

bardak-dev opened this issue Mar 1, 2020 · 9 comments
Labels
bug Something isn't working enhancement New feature or request Hacktoberfest

Comments

@bardak-dev
Copy link

No description provided.

@ojoanalogo ojoanalogo added bug Something isn't working enhancement New feature or request labels Mar 9, 2020
@ojoanalogo
Copy link
Owner

I'll try to add support for Fastify in the upcoming days, first I need to learn how Fastify serves a static file

@icereval
Copy link

@AGPDev
Copy link

AGPDev commented Mar 24, 2021

nice

@JoeyyT
Copy link

JoeyyT commented Mar 29, 2021

Any updates regading the implementation of Fastify?

@AGPDev
Copy link

AGPDev commented Mar 30, 2021

@JoeyyT you dont need this.

see example:

main.ts

import { join } from 'path';
import { NestFactory } from '@nestjs/core';
import { ValidationPipe } from '@nestjs/common';
import {
  FastifyAdapter,
  NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter(),
  );

  app.useGlobalPipes(
    new ValidationPipe({
      transform: true,
    }),
  );

  app.setGlobalPrefix('v1');

  app.useStaticAssets({
    root: join(__dirname, '..', 'public'),
    prefix: '/public/',
  });

  app.setViewEngine({
    engine: {
      handlebars: require('handlebars'),
    },
    templates: join(__dirname, '..', 'views'),
  });

  const config = new DocumentBuilder()
    .setTitle('Title')
    .setDescription('API description')
    .setVersion('1.0')
    .build();
  const document = SwaggerModule.createDocument(app, config);

  document.info['x-logo'] = {
    url: '/public/logo-light.svg',
    backgroundColor: '#F0F0F0',
    altText: 'Logo',
  };

  document.info['x-tagGroups'] = [
    {
      name: 'Catalog resources',
      tags: ['catalog'],
    },
  ];

  SwaggerModule.setup('api', app, document);

  await app.listen(3000, '0.0.0.0');
}
bootstrap();

app.controller.ts

import { Controller, Get, Header, Headers, Render } from '@nestjs/common';
import { ApiExcludeEndpoint, ApiTags } from '@nestjs/swagger';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  @ApiExcludeEndpoint()
  getHello(): string {
    return this.appService.getHello();
  }

  @Get('/docs')
  @Render('redoc.handlebars')
  @Header(
    'Content-Security-Policy',
    "default-src * 'unsafe-inline' 'unsafe-eval'; script-src * 'unsafe-inline' 'unsafe-eval'; child-src * 'unsafe-inline' 'unsafe-eval' blob:; worker-src * 'unsafe-inline' 'unsafe-eval' blob:; connect-src * 'unsafe-inline'; img-src * data: blob: 'unsafe-inline'; frame-src *; style-src * 'unsafe-inline';",
  )
  @ApiExcludeEndpoint()
  getDocs() {
    return {
      data: {
        docUrl: 'http://localhost:3000/api/json',
        favicon: '/public/icon.svg',
        options: JSON.stringify({
          theme: {
            logo: {
              gutter: '15px',
            },
          },
          sortPropsAlphabetically: true,
          hideDownloadButton: false,
          hideHostname: false,
          noAutoAuth: true,
          pathInMiddlePanel: true,
        }),
      },
    };
  }
}

views/redoc.handlebars

<!DOCTYPE html>
<html>

<head>
  <title>{{ data.title }}</title>
  <!-- needed for adaptive design -->
  <meta charset="utf-8" />
  {{#if data.favicon}}
  <link rel="shortcut icon" type="image/x-icon" href="{{ data.favicon }}" />
  {{/if}}
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">
  <!--
    ReDoc doesn't change outer page styles
    -->
  <style>
    body {
      margin: 0;
      padding: 0;
    }
  </style>
</head>

<body>
  <!-- we provide is specification here -->
  <div id="redoc_container"></div>
  <script src="https://cdn.jsdelivr.net/npm/redoc/bundles/redoc.standalone.js"> </script>
  <script>
    Redoc.init(
      '{{ data.docUrl }}',
      JSON.parse('{{{ data.options }}}'),
      document.getElementById("redoc_container")
    );
  </script>
</body>

</html>

Just access /v1/docs and be happy

@cerireyhan
Copy link

Is there any progress on this?

@red010182
Copy link

any progress on this? 2 years have passed... please

@elicobanksbridge
Copy link

@ojoanalogo is there any current progress or future road-map to support Fastify in nestjs-redoc ?

@julianpoemp
Copy link

Hi, I just created a library that supports Redoc using NestJS 10 and fastify: nestjs-redox

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request Hacktoberfest
Projects
None yet
Development

No branches or pull requests

9 participants