Skip to content

Commit 03ab5d3

Browse files
committed
setup a scaffolded OpenAPI specification.
- documents build process in README.md - endpoints in endpoints/* - JSON schemas in schemas/* - build tooling defined in package.json
1 parent dc05354 commit 03ab5d3

File tree

5 files changed

+179
-0
lines changed

5 files changed

+179
-0
lines changed

README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Example Library Specification
2+
This repository defines a simple API using [OpenAPI v3.0.1](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md).
3+
4+
# Setup and installation
5+
6+
The API spec is defined in `openapi.yml`. Each URL has its own endpoint in `endpoints/` and JSON request/response schemas are defined in `schemas/`. JSON Schema is used to define responses and can be used for validation.
7+
8+
## Setup and run the documentation
9+
Run these commands to install npm dependencies and serve the API documentation locally.
10+
11+
```sh
12+
npm install
13+
npm run test
14+
npm run serve
15+
```
16+
17+
The API documentation will be available at [https://localhost:5000](http://localhost:5000).
18+
19+
To resolve and publish the API spec, run:
20+
21+
```sh
22+
npm run resolve
23+
```
24+
25+
A simple HTML document including [ReDoc](https://github.com/Rebilly/ReDoc) is available in the `public/` directory. To copy across the published spec and schemas, run:
26+
27+
```sh
28+
npm run publish
29+
```
30+
31+
You can then serve `/public` from any static content hosting service.

endpoints/ping.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
paths:
2+
'/ping':
3+
get:
4+
operationId: ping
5+
description: "Check to see that the application is ready to respond to requests."
6+
tags:
7+
- Ping
8+
responses:
9+
'200':
10+
description: Successful ping; the application is ready to respond to requests
11+
content:
12+
application/json:
13+
schema:
14+
$ref: '../openapi.yml#/components/schemas/ping'
15+
'500':
16+
description: Unsuccessful ping; the application is NOT ready.
17+
content:
18+
application/json:
19+
schema:
20+
$ref: '../openapi.yml#/components/schemas/error'

openapi.yml

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
openapi: 3.0.0
2+
info:
3+
title: The Library
4+
description: A simple library API
5+
contact:
6+
name: developerjack
7+
url: https://www.twitter.com/developerjack
8+
license:
9+
name: Proprietary
10+
version: 0.0.1 #Spec version/revision - not the API or product version
11+
12+
servers:
13+
- url: 'http://localhost:8080'
14+
description: Local dev API.
15+
16+
tags:
17+
- name: User
18+
#- name: Book
19+
#- name: Loan
20+
21+
paths:
22+
/ping:
23+
$ref: "endpoints/ping.yml#/paths/~1ping"
24+
25+
# /user:
26+
# $ref: "endpoints/user.yml#/paths/~1user"
27+
# todo: The user endpoints
28+
# /user/{id}:
29+
# $ref: "endpoints/user.yml#/paths/~1user~1{id}"
30+
31+
# todo: The book endpoints
32+
# /book:
33+
# $ref: "endpoints/book.yml#/paths/~1book"
34+
# /book/{id}:
35+
# $ref: "endpoints/book.yml#/paths/~1book~1{id}"
36+
37+
# todo: The loan endpoints
38+
# /loan:
39+
# $ref: "endpoints/loan.yml#/paths/~1loan"
40+
# /loan/{id}:
41+
# $ref: "endpoints/loan.yml#/paths/~1loan~1{id}"
42+
43+
components:
44+
schemas:
45+
ping:
46+
$ref: 'schemas/ping.json'
47+
48+
# todo: user resources
49+
# user:
50+
# $ref: 'schemas/user.json'
51+
# user-collection:
52+
# $ref: 'schemas/user-collection.json'
53+
54+
# todo: book resources
55+
# book:
56+
# $ref: 'schemas/book.json'
57+
# book-collection:
58+
# $ref: 'schemas/book-collection.json'
59+
60+
# todo: loan resources
61+
# loan:
62+
# $ref: 'schemas/loan.json'
63+
# loan-collection:
64+
# $ref: 'schemas/loan-collection.json'
65+
66+
error:
67+
type: object
68+
required:
69+
- id
70+
- message
71+
properties:
72+
id:
73+
type: string
74+
description: "Unique ID of this error. For example sake, use the HTTP status code."
75+
nullable: false
76+
example: "500"
77+
message:
78+
type: string
79+
description: "A human readable message describing the nature of the error."
80+
nullable: false
81+
path:
82+
type: string
83+
description: "A JSONpath reference to the field where the error occurred."
84+
nullable: true

package.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "example-library-specification",
3+
"version": "0.0.1",
4+
"description": "An example library API definition",
5+
"scripts": {
6+
"test": "node_modules/.bin/speccy lint openapi.yml -j",
7+
"resolve": "mkdir -p dist && node_modules/.bin/speccy resolve openapi.yml -j > dist/openapi.yml",
8+
"serve": "node_modules/.bin/speccy serve openapi.yml -j",
9+
"publish": "cp dist/openapi.yml public/openapi.yml && cp -R schemas public/"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "[email protected]:devjack/example-library-specification.git"
14+
},
15+
"author": "Jack Skinner",
16+
"license": "ISC",
17+
"bugs": {
18+
"url": "https://github.com/devjack/example-library-specification/issues"
19+
},
20+
"homepage": "https://github.com/devjack/example-library-specification",
21+
"devDependencies": {
22+
"speccy": "^0.7"
23+
},
24+
"engines": {
25+
"node": ">=10",
26+
"npm": ">=5.6"
27+
}
28+
}

schemas/ping.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"type": "object",
4+
"title": "Ping/Pong resource for simple healthchecks.",
5+
"required": [
6+
"ping"
7+
],
8+
"properties": {
9+
"ping": {
10+
"type": "string",
11+
"description": "An acknowledgement tat the ping was received",
12+
"default": "",
13+
"example": "pong"
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)