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

Swagger #19

Merged
merged 5 commits into from
Jul 7, 2022
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
Rest API of images of fumos (japanese plushies) using typescript and fastify.

![logo](https://repository-images.githubusercontent.com/395606928/753b9fdd-b978-4b74-841e-f3973daf9129)

## Documentation
Documentation available at [fumo-api.nosesisaid.com/docs](https://fumo-api.nosesisaid.com/docs)
## Contributing
if you would like something to be different or have any suggestion, please [open an issue](https://github.com/nosesisaid/fumo-api/issues/new).

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"scripts": {
"start": "node dist/server.js",
"lint": "eslint src",
"lint:fix": "eslint src --fix",
"build": "tsc",
"dev": "yarn build && yarn start",
"test": "tap --no-coverage"
Expand All @@ -16,6 +17,7 @@
"dependencies": {
"@fastify/cors": "7.0.0",
"@fastify/mongodb": "5.0.0",
"@fastify/swagger": "6.x",
"dotenv": "16.0.1",
"fastify": "3.29.0"
},
Expand Down
160 changes: 146 additions & 14 deletions src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,172 @@ import fastify, { FastifyServerOptions } from 'fastify';
import fastifyMongodb from '@fastify/mongodb';
import './config/env';
import cors from '@fastify/cors';
import fastifySwagger from '@fastify/swagger';

function build(opts:FastifyServerOptions={}) {
async function build(opts: FastifyServerOptions = {}, isTest = false) {
const App = fastify(opts);

App.register(fastifyMongodb, {
url: process.env.MONGO_URL
url: process.env.MONGO_URL,
});
App.register(cors, {
origin: '*'
origin: '*',
});

App.get('/', async (req, res) => {
const fumos = await App.mongo.db?.collection('fumos').find({}).toArray();

if (!isTest) {
await App.register(fastifySwagger, {
routePrefix: '/docs',
swagger: {
info: {
title: 'Fumo-API',
description: 'Rest API of images of fumos (japanese plushies).',
version: '2.2.0',
contact: {
email: 'vic@nosesisaid.me',
name: 'Nosesisaid',
url: 'https://github.com/nosesisaid',
},
license: {
name: 'MIT',
url: 'https://opensource.org/licenses/MIT',
},
},
externalDocs: {
url: 'https://github.com/nosesisaid/fumo-api',
description: 'Find more info here',
},
host: 'fumo-api.nosesisaid.me',
schemes: ['https'],
consumes: ['application/json'],
produces: ['application/json'],
tags: [
{ name: 'images', description: 'image related end-points' },
],
definitions: {
Fumo: {
type: 'object',
required: ['id', 'URL'],
properties: {
_id: { type: 'string'},
URL: { type: 'string' },
caption: { type: 'string' },
fumos: { type: 'array', } },
},
},

},
uiConfig: {
deepLinking: false,
},
uiHooks: {
onRequest: function (request, reply, next) {
next();
},
preHandler: function (request, reply, next) {
next();
},
},
staticCSP: true,
transformStaticCSP: (header) => header,
exposeRoute: true,
});
}
App.get('/', {
schema: {
description: 'Get the full list of fumos',
tags: ['images'],
response: {
200: {
type: 'array',
items: {
type: 'object',
properties: {
_id: { type: 'string' },
URL: { type: 'string' },
caption: { type: 'string' },
fumos: { type: 'array', },
},
},
},
},
},
},async (req, res) => {
const fumos = await App.mongo.db?.collection('fumos').find({}).toArray();
res.status(200).send(fumos);
});

App.get('/fumo/:id', async (req, res) => {

const id = (req as any).params.id;
const fumo = await (await App.mongo.db?.collection('fumos'))?.findOne({_id: id});
App.get('/fumo/:id',{
schema: {
description: 'Get a fumo by it\'s id',
tags: ['images'],
params: {
type: 'object',
properties: {
_id: { type: 'string', description: 'The id of the fumo' },
},
},
response: {
200: {
type: 'object',
properties: {
_id: { type: 'string' },
URL: { type: 'string' },
caption: { type: 'string' },
fumos: { type: 'array', },
},
},
},
},
}, async (req, res) => {
const params = req.params;
const fumo = await (await App.mongo.db?.collection('fumos'))?.findOne({_id: (params as any).id});

res.status(200).send(fumo);
});

App.get('/random', async (req,res)=> {
App.get('/random', {
schema: {
description: 'Get a random fumo',
tags: ['images'],
response: {
200: {
type: 'object',
properties: {
_id: { type: 'string' },
URL: { type: 'string' },
caption: { type: 'string' },
fumos: { type: 'array', },
},
},
},
},
}, async (req, res) => {
const fumos = await App.mongo.db?.collection('fumos').find({}).toArray();
if (!fumos) return res.status(400).send('No fumo :( (server error)');
const fumo = fumos[Math.floor(Math.random() * fumos?.length)];

res.status(200).send(fumo);
});

App.get('/fumos', async(req, res) => {
App.get('/fumos', {
schema: {
description: 'Get a list of fumos',
tags: ['images'],
response: {
200: {
type: 'array',
items: {
type: 'object',
properties: {
_id: { type: 'string' },
URL: { type: 'string' },
caption: { type: 'string' },
fumos: { type: 'array', },
},
},
},
},
},
}, async (req, res) => {
const fumos = await App.mongo.db?.collection('fumos').find({}).toArray();

res.status(200).send(fumos);
Expand Down
22 changes: 13 additions & 9 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import {PORT} from './config/env';
import build from './App';

const server = build({
logger: true
});

server.listen(PORT,'0.0.0.0', err => {
if (err) throw err;
console.log('Server listening on port ' + PORT);
});
async function main() {

const server = await build({
logger: true
});
await server.ready();
await server.swagger();
server.listen(PORT,'0.0.0.0', err => {
if (err) throw err;
console.log('Server listening on port ' + PORT);
});
}
main();
2 changes: 1 addition & 1 deletion tests/random.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const build = require("../dist/App.js").default;
const {test} = require("tap");
test('call `/random` route', async (t) => {
t.plan(2)
const app = build();
const app = await build();
const response = await app.inject({
method: 'GET',
url:'/random'
Expand Down
2 changes: 1 addition & 1 deletion tests/root.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { test } = require("tap");
test('call `/` route', async (t) => {
t.plan(2);

const app = build();
const app = await build();
const response = await app.inject({
method: 'GET',
url:'/'
Expand Down
2 changes: 1 addition & 1 deletion tests/specific.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const {test} = require("tap");

test('call `/fumo/id` route', async (t) => {
t.plan(4)
const app = build();
const app = await build();
const id = '6128c5578caf0cf9a83f73e8';
const response = await app.inject({
method: 'GET',
Expand Down
Loading