Skip to content

Instantly share code, notes, and snippets.

@catalint
Created April 27, 2016 15:58
Show Gist options
  • Save catalint/68943916badc2cbd62c6030434159005 to your computer and use it in GitHub Desktop.
Save catalint/68943916badc2cbd62c6030434159005 to your computer and use it in GitHub Desktop.
hapi-swagger performance
{
"tags": [],
"host": "localhost:3000",
"schemes": [
"http"
],
"info": {
"title": "Test API Documentation",
"version": "1.0.0"
},
"basePath": "/",
"definitions": {
"User": {
"properties": {
"name": {
"type": "string"
}
},
"type": "object"
},
"Comment": {
"properties": {
"message": {
"type": "string"
},
"user": {
"type": "object",
"$ref": "#/definitions/User"
},
"timestamp": {
"type": "string",
"default": 1461772476386,
"format": "date"
}
},
"type": "object"
},
"Article": {
"properties": {
"title": {
"type": "string"
},
"createdBy": {
"type": "object",
"$ref": "#/definitions/User"
},
"editedBy": {
"type": "object",
"$ref": "#/definitions/User"
},
"comments": {
"items": {
"$ref": "#/definitions/Comment"
},
"type": "array"
}
},
"type": "object"
},
"List of articles": {
"items": {
"$ref": "#/definitions/Article"
},
"type": "array"
},
"Model 1": {
"type": "object",
"$ref": "#/definitions/Article"
}
},
"paths": {
"/article/list/": {
"get": {
"tags": [
"article"
],
"summary": "Get article list",
"operationId": "articlelist",
"responses": {
"200": {
"schema": {
"$ref": "#/definitions/List of articles",
"title": "List of articles"
},
"description": "Successful"
}
},
"description": "Returns a list of articles"
}
},
"/article/{id}/": {
"get": {
"tags": [
"article"
],
"summary": "Get article",
"operationId": "articleid",
"parameters": [
{
"required": true,
"description": "the id for the article item",
"type": "number",
"name": "id",
"in": "path"
}
],
"responses": {
"200": {
"schema": {
"$ref": "#/definitions/Model 1",
"title": "Article"
},
"description": "Successful"
}
},
"description": "Returns an article item by the id passed in the path"
}
}
},
"swagger": "2.0"
}
const Hapi = require('hapi');
const Inert = require('inert');
const Vision = require('vision');
const Joi = require('joi');
const HapiSwagger = require('hapi-swagger');
const Pack = require('../package');
const server = new Hapi.Server();
server.connection({
host: 'localhost',
port: 3000
});
const options = {
info: {
'title': 'Test API Documentation',
'version': Pack.version,
}
};
server.register([
Inert,
Vision,
{
'register': HapiSwagger,
'options': options
}], (err) => {
server.start((err) => {
if (err) {
console.log(err);
} else {
console.log('Server running at:', server.info.uri);
}
});
});
const handler = () => {
}
const User = Joi.object({
name: Joi.string()
}).label('User');
const Comment = Joi.object({
message: Joi.string(),
user: User,
timestamp: Joi.date().timestamp().default(()=>Date.now(), 'Current Timestamp')
}).label('Comment')
const Article = Joi.object({
title: Joi.string(),
createdBy: User,
editedBy: User,
comments: Joi.array().items(Comment)
}).label('Article')
const Routes = [];
Routes.push({
method: 'GET',
path: '/article/{id}/',
config: {
handler: handler,
description: 'Get article',
notes: 'Returns an article item by the id passed in the path',
tags: ['api'],
validate: {
params: {
id: Joi.number()
.required()
.description('the id for the article item'),
}
},
response: {
status: {
200: Article
}
}
}
});
Routes.push({
method: 'GET',
path: '/article/list/',
config: {
handler: handler,
description: 'Get article list',
notes: 'Returns a list of articles',
tags: ['api'],
validate: {},
response: {
status: {
200: Joi.array().items(Article).label('List of articles')
}
}
}
});
server.route(Routes);
{
"host": "localhost:3000",
"info": {
"title": "Test API Documentation",
"version": "1.0.0"
},
"schemes": [
"http"
],
"swagger": "2.0",
"paths": {
"/article/list/": {
"get": {
"summary": "Get article list",
"description": "Returns a list of articles",
"responses": {
"200": {
"description": "Successful",
"schema": {
"$ref": "#/definitions/List of articles",
"title": "List of articles"
}
}
},
"tags": [
"article"
],
"operationId": "articlelist"
}
},
"/article/{id}/": {
"get": {
"summary": "Get article",
"description": "Returns an article item by the id passed in the path",
"responses": {
"200": {
"description": "Successful",
"schema": {
"$ref": "#/definitions/Model 3",
"title": "Article"
}
}
},
"tags": [
"article"
],
"operationId": "articleid",
"parameters": [
{
"type": "number",
"description": "the id for the article item",
"required": true,
"name": "id",
"in": "path"
}
]
}
}
},
"definitions": {
"User": {
"properties": {
"name": {
"type": "string"
}
},
"type": "object"
},
"Comment": {
"properties": {
"message": {
"type": "string"
},
"user": {
"type": "object",
"$ref": "#/definitions/User"
},
"timestamp": {
"default": 1461772447803,
"type": "string",
"format": "date"
}
},
"type": "object"
},
"Article": {
"properties": {
"title": {
"type": "string"
},
"createdBy": {
"type": "object",
"$ref": "#/definitions/User"
},
"editedBy": {
"type": "object",
"$ref": "#/definitions/User"
},
"comments": {
"type": "array",
"items": {
"$ref": "#/definitions/Comment"
}
}
},
"type": "object"
},
"List of articles": {
"type": "array",
"items": {
"$ref": "#/definitions/Article"
}
},
"Model 1": {
"properties": {
"message": {
"type": "string"
},
"user": {
"type": "object",
"$ref": "#/definitions/User"
},
"timestamp": {
"default": 1461772447805,
"type": "string",
"format": "date"
}
},
"type": "object"
},
"Model 2": {
"properties": {
"title": {
"type": "string"
},
"createdBy": {
"type": "object",
"$ref": "#/definitions/User"
},
"editedBy": {
"type": "object",
"$ref": "#/definitions/User"
},
"comments": {
"type": "array",
"items": {
"$ref": "#/definitions/Model 1"
}
}
},
"type": "object"
},
"Model 3": {
"type": "object",
"$ref": "#/definitions/Model 2"
}
},
"tags": [],
"basePath": "/"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment