Skip to content

Commit f727644

Browse files
committed
Start met Node ExpressJS implementatie
1 parent 9e18986 commit f727644

2 files changed

Lines changed: 151 additions & 2 deletions

File tree

examples/express/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "express-example",
3+
"type": "module",
34
"dependencies": {
45
"@wesleytodd/openapi": "^1.1.0",
56
"express": "^5.2.0"

examples/express/src/server.js

Lines changed: 150 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
const express = require('express');
2-
const openapi = require('@wesleytodd/openapi');
1+
// @ts-check
2+
3+
import express from 'express';
4+
import openapi from '@wesleytodd/openapi';
5+
36
const app = express();
47
const port = 8080;
58

@@ -12,6 +15,79 @@ const API_VERSION_HEADER_SCHEMA = {
1215
type: 'string'
1316
}
1417
};
18+
const BAD_REQUEST_RESPONSE = {
19+
'application/problem+json': {
20+
schema: {
21+
type: 'object',
22+
properties: {
23+
status: {
24+
type: 'integer'
25+
},
26+
title: {
27+
type: 'string'
28+
},
29+
detail: {
30+
type: 'string'
31+
},
32+
errors: {
33+
type: 'array',
34+
items: {
35+
type: 'object',
36+
properties: {
37+
in: {
38+
type: 'string'
39+
},
40+
location: {
41+
type: 'string'
42+
},
43+
code: {
44+
type: 'string'
45+
},
46+
detail: {
47+
type: 'string'
48+
},
49+
index: {
50+
type: 'integer'
51+
}
52+
}
53+
}
54+
}
55+
}
56+
}
57+
}
58+
}
59+
60+
/**
61+
* @typedef {{ in: "body"|"query", detail: string, location?: string, index?: number, code?: string }} BadRequestError
62+
*/
63+
64+
class BadRequestResponseWithErrors extends Error {
65+
/**
66+
* @param {string} title
67+
* @param {string} detail
68+
* @param {Array<BadRequestError>} errors
69+
*/
70+
constructor(title, detail, errors) {
71+
super();
72+
73+
this.title = title;
74+
this.detail = detail;
75+
this.errors = errors;
76+
}
77+
toJSON() {
78+
const {
79+
title,
80+
detail,
81+
errors
82+
} = this;
83+
return {
84+
status: 400,
85+
title,
86+
detail,
87+
errors,
88+
};
89+
}
90+
}
1591

1692
const oapi = openapi({
1793
openapi: '3.0.0',
@@ -89,6 +165,78 @@ app.get('/', oapi.path({
89165
res.send('Dit is de landing pagina van deze API');
90166
});
91167

168+
app.post('/input', oapi.path({
169+
description: 'Input method',
170+
tags: [
171+
'input'
172+
],
173+
operationId: 'postInput',
174+
responses: {
175+
200: {
176+
description: 'Input',
177+
content: {
178+
'text/plain': {}
179+
},
180+
headers: {
181+
[API_VERSION_HEADER_NAME]: oapi.headers(API_VERSION_HEADER_SCHEMA_NAME)
182+
}
183+
},
184+
400: {
185+
description: 'Invalid input',
186+
content: BAD_REQUEST_RESPONSE,
187+
headers: {
188+
[API_VERSION_HEADER_NAME]: oapi.headers(API_VERSION_HEADER_SCHEMA_NAME)
189+
}
190+
}
191+
}
192+
}), (req, res) => {
193+
/**
194+
* @type {Array<BadRequestError>}
195+
*/
196+
const errors = [];
197+
const queryInput = req.query.queryInput;
198+
if (!queryInput) {
199+
errors.push({
200+
in: 'query',
201+
detail: 'Missing query parameter',
202+
location: 'queryInput',
203+
code: 'input.query.missing',
204+
})
205+
} else {
206+
let params;
207+
if (typeof queryInput === 'string') {
208+
params = [queryInput];
209+
} else {
210+
params = queryInput;
211+
}
212+
for (const [index, param] of params.entries()) {
213+
if (param !== '42') {
214+
errors.push({
215+
in: 'query',
216+
detail: 'Invalid query parameter. Expected 42, but was ' + param,
217+
location: 'queryInput',
218+
index,
219+
code: 'input.query.invalid',
220+
});
221+
}
222+
}
223+
}
224+
225+
if (errors.length > 0) {
226+
throw new BadRequestResponseWithErrors('title', 'detail', errors);
227+
}
228+
229+
res.send('Succesvolle response');
230+
});
231+
232+
app.use((err, req, res, next) => {
233+
if (err instanceof BadRequestResponseWithErrors) {
234+
return res.status(400).json(err.toJSON());
235+
}
236+
237+
next();
238+
});
239+
92240
app.listen(port, () => {
93241
console.log(`Example express app beschikbaar op poort ${port}`);
94242
});

0 commit comments

Comments
 (0)