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

feat(api): API documentation with Swagger UI #49791

Merged
merged 5 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion api/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import fastifySession from '@fastify/session';
import fastifyCookie from '@fastify/cookie';
import MongoStore from 'connect-mongo';
import type { TypeBoxTypeProvider } from '@fastify/type-provider-typebox';
import fastifySwagger from '@fastify/swagger';
import fastifySwaggerUI from '@fastify/swagger-ui';

import jwtAuthz from './plugins/fastify-jwt-authz';
import sessionAuth from './plugins/session-auth';
Expand All @@ -26,7 +28,9 @@ import {
AUTH0_DOMAIN,
NODE_ENV,
MONGOHQ_URL,
SESSION_SECRET
SESSION_SECRET,
FCC_ENABLE_SWAGGER_UI,
API_LOCATION
} from './utils/env';

export type FastifyInstanceWithTypeProvider = FastifyInstance<
Expand Down Expand Up @@ -63,6 +67,30 @@ export const build = async (
})
});

// Swagger plugin
if (FCC_ENABLE_SWAGGER_UI) {
void fastify.register(fastifySwagger, {
openapi: {
info: {
title: 'freeCodeCamp API',
version: '1.0.0' // API version
},
components: {
securitySchemes: {
session: {
type: 'apiKey',
name: 'sessionId',
in: 'cookie'
}
}
},
security: [{ session: [] }]
}
});
void fastify.register(fastifySwaggerUI);
fastify.log.info(`Swagger UI available at ${API_LOCATION}/documentation`);
}

// Auth0 plugin
void fastify.register(fastifyAuth0, {
domain: AUTH0_DOMAIN,
Expand Down
2 changes: 2 additions & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"@fastify/cookie": "^8.3.0",
"@fastify/middie": "8.1",
"@fastify/session": "^10.1.1",
"@fastify/swagger": "^8.3.1",
"@fastify/swagger-ui": "^1.5.0",
"@prisma/client": "4.11.0",
"@sinclair/typebox": "0.25.24",
"connect-mongo": "4.6.0",
Expand Down
3 changes: 3 additions & 0 deletions api/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ assert.ok(process.env.AUTH0_DOMAIN);
assert.ok(process.env.AUTH0_AUDIENCE);
assert.ok(process.env.API_LOCATION);
assert.ok(process.env.SESSION_SECRET);
assert.ok(process.env.FCC_ENABLE_SWAGGER_UI);

if (process.env.NODE_ENV !== 'development' && process.env.NODE_ENV !== 'test') {
assert.ok(process.env.PORT);
Expand All @@ -50,3 +51,5 @@ export const AUTH0_AUDIENCE = process.env.AUTH0_AUDIENCE;
export const PORT = process.env.PORT || '3000';
export const API_LOCATION = process.env.API_LOCATION;
export const SESSION_SECRET = process.env.SESSION_SECRET;
export const FCC_ENABLE_SWAGGER_UI =
process.env.FCC_ENABLE_SWAGGER_UI === 'true';
76 changes: 74 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,4 @@ CODESEE=false
# ---------------------
NODE_ENV=development
PORT=3000
FCC_ENABLE_SWAGGER_UI=true