Last active
October 12, 2021 06:49
-
-
Save crrobinson14/8290174 to your computer and use it in GitHub Desktop.
How to make a REST API definition in YAML, compile it into a Swagger-compatible JSON file, expose it via Swagger-UI, then write a Mocha unit test against it to verify that a sample record validates against the JSON-Schema produced by the process. This is a great mix of tools and scripts for building RESTful APIs in Node.JS. You can build backwar…
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
# api-docs/yaml/api-docs.yml | |
apiVersion: 1.0.0 | |
swaggerVersion: "1.2" | |
apis: | |
- path: /books | |
description: Books API | |
authorizations: | |
apiKey: | |
type: apiKey | |
passAs: header | |
info: | |
title: My custom API | |
description: "<strong>Great</strong> APIs by Great People.\n" | |
termsOfServiceUrl: "http://example.com/terms" | |
contact: "[email protected]" | |
license: Apache 2.0 | |
licenseUrl: "http://www.apache.org/licenses/LICENSE-2.0.html" |
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
# api-docs/yaml/books.yml | |
apiVersion: 1.0.0 | |
swaggerVersion: "1.2" | |
basePath: "/v1" | |
resourcePath: /books | |
produces: | |
- application/json | |
apis: | |
- path: "/books/{id}" | |
operations: | |
- method: GET | |
summary: Retrieve a book, along with its metadata and options | |
notes: Returns all available data if secretKey is supplied, otherwise returns only the public record. | |
type: Book | |
nickname: getBookById | |
produces: | |
- application/json | |
parameters: | |
- name: id | |
description: ID of the book to fetch | |
required: true | |
allowMultiple: false | |
type: string | |
paramType: path | |
- name: secretKey | |
description: Secret key for the request | |
required: false | |
allowMultiple: false | |
type: string | |
paramType: query | |
responseMessages: | |
- code: 404 | |
message: Book not found | |
models: | |
BookChapter: | |
id: BookChapter | |
title: Chapter | |
type: object | |
description: "A chapter within the book, to support displaying a table-of-contents." | |
required: | |
- title | |
properties: | |
title: | |
type: string | |
description: "The title of the chapter" | |
Book: | |
id: Book | |
title: Book | |
type: object | |
description: "A Book record with optional chapters." | |
required: | |
- title | |
properties: | |
id: | |
type: string | |
description: "Unique ID for this book" | |
title: | |
type: string | |
description: "Book title" | |
author: | |
type: string | |
description: "Author name" | |
chapters: | |
type: array | |
description: "One or more Chapters to list in a TOC" | |
items: | |
$ref: BookChapter |
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
// grunt/exec.js | |
module.exports = { | |
compile_apidocs: { | |
command: 'node yml2swagger.js api-docs/yaml api-docs/json', | |
}, | |
mocha_test: { | |
command: 'mocha -R spec --recursive test', | |
}, | |
}; |
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
// tests/books/01-samples.js | |
var request = require('supertest') | |
, app = require('../../app') | |
, chai = require('chai') | |
, expect = chai.expect | |
, chaiHttp = require('chai-http') | |
, fs = require('fs'); | |
chai.use(require('chai-json-schema')); | |
var api = require(process.cwd() + '/api-docs/json/books.yml.json'); | |
chai.tv4.addSchema('Book', api.models.Book); | |
chai.tv4.addSchema('BookChapter', api.models.BookChapter); | |
var getJSON = function(filename) { | |
var data = fs.readFileSync(process.cwd() + filename); | |
return JSON.parse(data); | |
}; | |
var samples = [ | |
{ id: 'SAMPLE-BOOK', file: 'sample-book.json', }, | |
]; | |
describe('Books API: 01. Samples', function() { | |
samples.map(function(sample) { | |
/* TEST: Sample File */ | |
describe(sample.id, function() { | |
it('should have a valid Schema', function(done) { | |
var data = getJSON('/data/books/' + sample.file); | |
expect(data).to.be.jsonSchema('Book'); | |
done(); | |
}); | |
}); | |
}); | |
}); |
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
'use strict'; | |
var request = require('request'); | |
module.exports = function (grunt) { | |
require('time-grunt')(grunt); | |
require('load-grunt-tasks')(grunt); | |
grunt.initConfig({ | |
pkg: grunt.file.readJSON('package.json'), | |
exec: require('./grunt/exec.js'), | |
mocha: require('./grunt/mocha.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
{ | |
"id": "12-456-3788-0", | |
"title": "My First Book", | |
"author": "Me", | |
"chapters": [ | |
{ | |
"title": "First Chapter" | |
}, | |
{ | |
"title": "Second Chapter" | |
}, | |
{ | |
"title": "It's a Short Book" | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wish to know that exactly
How to make a REST API definition in YAML, compile it into a Swagger-compatible JSON file, expose it via Swagger-UI, then write a Mocha unit test against it to verify that a sample record validates against the JSON-Schema produced by the process. This is a great mix of tools and scripts for building RESTful APIs in Node.JS. You can build backwar…