Skip to content

Instantly share code, notes, and snippets.

@csakis
Forked from bravo-kernel/feathersjs-swagger.js
Created October 1, 2018 19:35
Show Gist options
  • Save csakis/52a2ba76940e95162f11fb9f450006e6 to your computer and use it in GitHub Desktop.
Save csakis/52a2ba76940e95162f11fb9f450006e6 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)
// -----------------------------------------------------
// 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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment