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

[Closes #46] Integrate @fastify/swagger and Scalar #68

Merged
merged 3 commits into from
Apr 4, 2024
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
78 changes: 78 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
"@fastify/cookie": "^9.3.1",
"@fastify/secure-session": "^7.1.0",
"@fastify/sensible": "^5.5.0",
"@fastify/swagger": "^8.14.0",
"@getbigger-io/prisma-fixtures-cli": "^2.0.1",
"@prisma/client": "^5.12.0",
"@scalar/fastify-api-reference": "^1.20.20",
"bcrypt": "^5.1.1",
"dotenv": "^16.4.5",
"fastify": "^4.26.1",
Expand Down
37 changes: 37 additions & 0 deletions server/plugins/swagger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import fp from 'fastify-plugin';
import swagger from '@fastify/swagger';
import scalar from '@scalar/fastify-api-reference';

import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const pkg = await fs.readFile(path.resolve(__dirname, '../../package.json'), {
encoding: 'utf8',
});
const { version } = JSON.parse(pkg);

/**
* This adds Swagger support to our API endpoints for generating documentation.
*/
export default fp(async (fastify) => {
await fastify.register(swagger, {
openapi: {
info: {
title: 'SF Life Line API',
version,
},
servers: [
{
url: 'http://localhost:5000',
},
],
},
});
await fastify.register(scalar, {
routePrefix: '/api/reference',
});
});
File renamed without changes.
17 changes: 9 additions & 8 deletions server/routes/api/v1/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ export default async function (fastify, _opts) {
},
response: {
200: {
type: 'object',
properties: {
id: { type: 'string' },
firstName: { type: 'string' },
lastName: { type: 'string' },
email: { type: 'string', format: 'email' },
role: { type: 'string' },
createdAt: { type: 'string' },
type: 'null',
description:
'Successfully authenticated. The response sets a cookie named `session` that should be sent in subsequent requests for authentication. This cookie will NOT appear in the web-based API tester infterface because it is an HttpOnly cookie that cannot be accessed by JavaScript.',
headers: {
'Set-Cookie': {
schema: {
type: 'string',
},
},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion server/routes/api/v1/licenses/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import verifyLicense from '../../../../helpers/license/verifyLicense.js';

export default async function (fastify) {
fastify.get(
'/',
'',
{
schema: {
querystring: {
Expand Down
6 changes: 3 additions & 3 deletions server/routes/api/v1/users/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ export default async function (fastify, _opts) {
schema: {
body: {
type: 'object',
required: ['firstName', 'lastName', 'email'],
required: ['firstName', 'lastName', 'email', 'password'],
properties: {
firstName: { type: 'string' },
middleName: { type: 'string' },
lastName: { type: 'string' },
email: { type: 'string', format: 'email' },
hashedPassword: { type: 'string' },
password: { type: 'string' },
licenseNumber: { type: 'string' },
},
},
Expand Down Expand Up @@ -90,7 +90,7 @@ export default async function (fastify, _opts) {

// Read All Users
fastify.get(
'/',
'',
{
schema: {
response: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { describe, it } from 'node:test';
import * as assert from 'node:assert';
import { StatusCodes } from 'http-status-codes';

import { build } from '../helper.js';
import { build } from '../../helper.js';

describe('/', () => {
describe('/api', () => {
describe('GET /health', () => {
it('returns ok when the server is up and running', async (t) => {
const app = await build(t);

const res = await app.inject({
url: '/health',
url: '/api/health',
});
assert.deepStrictEqual(res.statusCode, StatusCodes.OK);
assert.deepStrictEqual(JSON.parse(res.payload), { status: 'OK' });
Expand Down