Last active
November 24, 2020 21:49
-
-
Save bravo-kernel/947281f59405fc63d997c73d1a8c7d3c to your computer and use it in GitHub Desktop.
Working example of feathers-swagger with the swagger definition in a service (not in app.js)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ----------------------------------------------------- | |
// projects.service.js | |
// ----------------------------------------------------- | |
// Initializes the `projects` service on path `/projects` | |
const createService = require('feathers-sequelize'); | |
const createModel = require('../../models/projects.model'); | |
const hooks = require('./projects.hooks'); | |
module.exports = function (app) { | |
const Model = createModel(app); | |
const paginate = app.get('paginate'); | |
const options = { | |
Model, | |
paginate | |
}; | |
// Initialize our service with any options it requires | |
app.use('/projects', createService(options)); | |
// Get our initialized service so that we can register hooks | |
const service = app.service('projects'); | |
// attempt at swagger definition | |
service.docs = { | |
description: 'Service to manage projects', | |
definitions: { | |
'projects list': { | |
$ref: '#/definitions/projects' | |
}, | |
projects: { | |
"type": "object", | |
"required": [ "name", "description" ], | |
"properties": { | |
"name": { | |
"type": "string", | |
"description": "Project name" | |
}, | |
"description": { | |
"type": "string", | |
"description": "Project description" | |
} | |
} | |
} | |
} | |
}; | |
// Activate hooks | |
service.hooks(hooks); | |
// Expose swagger definition | |
app.use('/projects', service); | |
}; | |
// ----------------------------------------------------- | |
// app.js | |
// ----------------------------------------------------- | |
const path = require('path'); | |
const favicon = require('serve-favicon'); | |
const compress = require('compression'); | |
const helmet = require('helmet'); | |
const cors = require('cors'); | |
const logger = require('./logger'); | |
const feathers = require('@feathersjs/feathers'); | |
const configuration = require('@feathersjs/configuration'); | |
const express = require('@feathersjs/express'); | |
const socketio = require('@feathersjs/socketio'); | |
const authentication = require('@feathersjs/authentication'); | |
const jwt = require('@feathersjs/authentication-jwt'); | |
const local = require('@feathersjs/authentication-local'); | |
const middleware = require('./middleware'); | |
const services = require('./services'); | |
const appHooks = require('./app.hooks'); | |
const channels = require('./channels'); | |
const sequelize = require('./sequelize'); | |
const app = express(feathers()); | |
const swagger = require('feathers-swagger'); | |
// Load app configuration | |
app.configure(configuration()); | |
// Setup Swagger. MUST come before configuring services | |
app.configure(swagger({ | |
docsPath: '/docs', | |
uiIndex: true, // no need for src/docs.html | |
info: { | |
title: 'A test', | |
description: 'A description' | |
}, | |
ignore: { | |
tags: ['authentication'] | |
} | |
})); | |
// Debug swagger object | |
//console.log (app.docs); | |
// Enable security, CORS, compression, favicon and body parsing | |
app.use(helmet()); | |
app.use(cors()); | |
app.use(compress()); | |
app.use(express.json()); | |
app.use(express.urlencoded({ extended: true })); | |
app.use(favicon(path.join(app.get('public'), 'favicon.ico'))); | |
// Host the public folder | |
app.use('/', express.static(app.get('public'))); | |
// Set up Plugins and providers | |
app.configure(express.rest()); | |
app.configure(socketio()); | |
app.configure(sequelize); | |
// Configure other middleware (see `middleware/index.js`) | |
app.configure(middleware); | |
// Setup authentication | |
app.configure(authentication(app.get('authentication'))); // load settings from global config | |
app.configure(local()); | |
app.configure(jwt()); | |
// Set up our services (see `services/index.js`) | |
app.configure(services); | |
// Set up event channels (see channels.js) | |
app.configure(channels); | |
// Setup a hook to only allow valid JWTs to authenticate | |
// and get new JWT access tokens | |
app.service('authentication').hooks({ | |
before: { | |
create: [ | |
authentication.hooks.authenticate(['jwt', 'local']) | |
], | |
remove: [ | |
authentication.hooks.authenticate('jwt') | |
] | |
} | |
}); | |
// Configure a middleware for 404s and the error handler | |
app.use(express.notFound()); | |
app.use(express.errorHandler({ logger })); | |
app.hooks(appHooks); | |
module.exports = app; |
@gowthaman-i2i you are absolutely right, this example was very sub-optimal.
Because I needed a clean solution I created https://github.com/alt3/sequelize-to-json-schemas.
Now all that is needed in my service is:
service.model = app.get('jsonSchemaManager').generate(Model, app.get('openApi3Strategy'));
Prerequisites
Add to app.js
:
const sequelizeToJsonSchemas = require('./sequelize-to-json-schemas');
app.configure(sequelizeToJsonSchemas);
Create new file src/sequelize-to-json-schemas.js
:
const {
JsonSchemaManager,
OpenApi3Strategy,
} = require('@alt3/sequelize-to-json-schemas');
module.exports = function init(app) {
const schemaManager = new JsonSchemaManager({
baseUri: 'https://api.example.com',
absolutePaths: true,
});
const openApi3Strategy = new OpenApi3Strategy();
app.set('jsonSchemaManager', schemaManager);
app.set('openApi3Strategy', openApi3Strategy);
};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I hope we can use like below, correct me if i am wrong