fastify / fastify-swagger

Swagger documentation generator for Fastify
MIT License
915 stars 201 forks source link

Please update example folder for typescript #694

Closed giabao1008 closed 9 months ago

giabao1008 commented 1 year ago

Prerequisites

🚀 Feature Proposal

currently im working with fastify swagger and typescript, follow as example folder. but when use exposeRoute option, it will prompt error

Motivation

No response

Example

.register(swagger, {
    swagger: {
      info: {
        title: 'Test swagger',
        description: 'testing the fastify swagger api',
        version: '0.1.0',
      },
      host: 'localhost:3000',
      schemes: ['http'],
      consumes: ['application/json'],
      produces: ['application/json'],
      swagger: '2.0',
    },
    exposeRoute: true,
    // routePrefix: '/swagger-docs',
  })
Uzlopak commented 1 year ago

Can provide a PR please?

giabao1008 commented 1 year ago

i cant, please help

climba03003 commented 1 year ago

Remove routePrefix: '/swagger-docs', as you show in your example.

giabao1008 commented 1 year ago

  .register(swagger, {
    swagger: {
      info: {
        title: 'Test swagger',
        description: 'testing the fastify swagger api',
        version: '0.1.0',
      },
      basePath: '/',
      consumes: ['application/json'],
      host: 'localhost',
      swagger: '2.0',
    },
    // exposeRoute: true,
    // routePrefix: '/swagger-docs',
  });`` 
yes, i removed but, seem the path /documentation not work
Uzlopak commented 1 year ago

Please read migration.md

giabao1008 commented 1 year ago

one more question, i ve connected swagger to fastify with swagger ui, define a router with schema, but in swagger doc, it not auto generated

this is my config

await fastify.register(fastifySwagger, {
    swagger: {
      info: {
        title: ' Swagger',
        description: 'Testing swagger API',
        version: '0.1.0',
      },
      host: `${HOST}:${PORT}`,
      schemes: ['http'],
      consumes: ['application/json'],
      produces: ['application/json'],
      swagger: '2.0',
    },
  });
  await fastify.register(fastifySwaggerUi, {
    routePrefix: '/v1/example/docs',
    initOAuth: {},
    uiConfig: {
      docExpansion: 'full',
      deepLinking: false,
    },
    uiHooks: {
      onRequest: function (request, reply, next) {
        next();
      },
      preHandler: function (request, reply, next) {
        next();
      },
    },
    staticCSP: true,
    transformStaticCSP: (header) => header,
  });

and router

fastify.get(
    '/',
    {
      schema: {
        description: 'This is an endpoint for fetching all users',
        tags: ['users'],
        response: {
          200: {
            description: 'Success Response',
            type: 'array',
            items: {
              type: 'object',
              properties: {
                id: { type: 'number' },
                name: { type: 'string' },
                age: { type: 'number' },
                createdAt: { type: 'string' },
                updatedAt: { type: 'string' },
              },
            },
          },
        },
      },
    },
    async function (request: FastifyRequest, reply: FastifyReply) {
      return fastify.exampleService.getAll();
    }
  );

please advise me

kibertoad commented 1 year ago

you need both fastify-swagger and fastify-swagger-ui

giabao1008 commented 1 year ago

you need both fastify-swagger and fastify-swagger-ui

yes, i have

kibertoad commented 1 year ago

Please share full config then

giabao1008 commented 1 year ago

Please share full config then

please see above, it's my all configs, or what else ?

giabao1008 commented 1 year ago

Please share full config then

please see above

borjatur commented 1 year ago

here you have a fully working example https://github.com/borjatur/clean-architecture-fastify-mongodb/blob/master/src/infrastructure/http/plugins/docs.ts

hope that can help you

kp198 commented 1 year ago

I tried the same and was unable to generate docs automatically for my APIs. Any update here?

mcollina commented 1 year ago

Everything is working fine for me, so it would be useful to have a reproduction.

alexey-sh commented 10 months ago

I copy-pasted the docs.ts file and even didn't get any page

image image
casantosmu commented 9 months ago

I copied and pasted examples from the documentation, and everything is functioning well: GitHub Link.

Similarly, I replicated the contents of the docs.ts file from @borjatur's example, and it is also working smoothly: GitHub Link.

Uzlopak commented 9 months ago

If there are problems then please open a new issue.