Skip to content

Commit

Permalink
Added router for autodoc
Browse files Browse the repository at this point in the history
  • Loading branch information
xdan committed Aug 22, 2023
1 parent cefcc7e commit 141f7a5
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 6 deletions.
164 changes: 164 additions & 0 deletions docs/v1/spec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
openapi: '3.0.1'
info:
version: v1
title: Mappable test server
license:
name: "Apache-2"
paths:
/ping:
get:
description: |
Handle for checking the readiness of the server to work
responses:
200:
description: Server is ready.
content:
application/json:
example: {"ok": true}
schema:
type: object
properties:
ok:
type: boolean
400:
description: Server is not ready.
content:
application/json:
example: { "ok": false }
schema:
type: object
properties:
ok:
type: boolean

/v1/bbox:
post:
description: |
Returns geo features whose coordinates are within the given rectangle bbox
requestBody:
content:
application/json:
schema:
type: object
required: ['leftBottom', 'rightTop']
properties:
leftBottom:
$ref: '#/components/schemas/LngLat'
rightTop:
$ref: '#/components/schemas/LngLat'

description: JSON object
responses:
200:
description: Features list.
content:
application/json:
schema:
type: object
properties:
features:
type: array
items:
$ref: '#/components/schemas/Feature'
400:
$ref: '#/components/responses/InvalidParameters'

/v1/tile:
post:
description: |
Returns geo features whose coordinates are within a rectangle that is uniquely defined by its x,y and z tile coordinates
requestBody:
content:
application/json:
schema:
type: object
required: ['x', 'y', 'z']
properties:
x:
type: integer
y:
type: integer
z:
type: integer

description: JSON object
responses:
200:
description: Features list.
content:
application/json:
schema:
type: object
properties:
bounds:
description: Computed rectangle of coordinates lower left and upper right corner for a given tile
items:
$ref: '#/components/schemas/LngLat'

features:
type: array
items:
$ref: '#/components/schemas/Feature'
400:
$ref: '#/components/responses/InvalidParameters'

components:
responses:
InvalidParameters:
description: Invalid parameters.
content:
application/json:
schema:
$ref: '#/components/schemas/Error'

schemas:
LngLat:
description: 'Array of coordinates [Longitude, Latitude].'
example: [24, 54]
type: array
items:
type: number
minItems: 2
maxItems: 2

Feature:
description: 'GeoJSON Feature'
type: object
example:
{
type: 'Feature',
properties: {name: 'Abu Dhabi'},
geometry: {'type': 'Point', 'coordinates': [24.189215755000077, 53.796540969000034]}
}
properties:
type:
type: string
properties:
type: object
properties:
name:
type: string
geometry:
type: object
properties:
type:
type: string
coordinates:
$ref: '#/components/schemas/LngLat'

Error:
type: object
properties:
statusCode:
type: integer
error:
type: string
message:
type: string
required:
- statusCode
- error
- message
additionalProperties: false
76 changes: 70 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,21 @@
"dotenv": "^16.3.1",
"express": "^4.18.2",
"jest": "^29.6.2",
"js-yaml": "^4.1.0",
"nock": "^13.3.3",
"pg": "^8.11.3",
"pm2": "^5.3.0",
"swagger-ui-express": "^5.0.0",
"winston": "^3.10.0",
"zod": "^3.22.1"
},
"devDependencies": {
"@types/express": "^4.17.17",
"@types/got": "^9.6.12",
"@types/jest": "^29.5.3",
"@types/js-yaml": "^4.0.5",
"@types/pg": "^8.10.2",
"@types/swagger-ui-express": "^4.1.3",
"@typescript-eslint/eslint-plugin": "^6.4.0",
"@typescript-eslint/parser": "^6.4.0",
"eslint": "^8.47.0",
Expand Down
2 changes: 2 additions & 0 deletions src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {asyncMiddleware} from './lib/async-middlware';
import {loadByBBox} from './middleware/load-by-bbox';
import {loadByTile} from './middleware/load-by-tile';
import {DataProvider} from "./data-provider/interface";
import {apiDocs} from "./middleware/api-docs";

export function createApp(dataProvider: DataProvider) {
return (
Expand All @@ -16,6 +17,7 @@ export function createApp(dataProvider: DataProvider) {
.get('/ping', asyncMiddleware(pingMiddleware.bind(null, dataProvider)))
.post('/v1/bbox', asyncMiddleware(loadByBBox.bind(null, dataProvider)))
.post('/v1/tile', asyncMiddleware(loadByTile.bind(null, dataProvider)))
.use('/v1/api_docs', apiDocs)
.use((req: express.Request, res: express.Response, next: express.NextFunction) =>
next(Boom.notFound('Endpoint not found'))
)
Expand Down
11 changes: 11 additions & 0 deletions src/app/middleware/api-docs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {Router} from 'express';
import * as fs from 'fs';
import * as yaml from 'js-yaml';
import * as path from 'path';
import * as swaggerUi from 'swagger-ui-express';

const apiSpec = yaml.load(fs.readFileSync(path.join(__dirname, '../../../docs/v1/spec.yaml'), 'utf8')) as object;

export const apiDocs = Router()
.use('/', swaggerUi.serve)
.get('/', swaggerUi.setup(apiSpec));

0 comments on commit 141f7a5

Please sign in to comment.