Projektstart

This commit is contained in:
2026-01-22 15:49:12 +01:00
parent 7212eb6f7a
commit 57e5f652f8
10637 changed files with 2598792 additions and 64 deletions

View File

@@ -0,0 +1,57 @@
'use strict'
const Fastify = require('fastify')
; (async () => {
const fastify = Fastify({
logger: true,
// Need to add a collectionFormat keyword to ajv in fastify instance
ajv: {
customOptions: {
keywords: ['collectionFormat']
}
}
})
await fastify.register(require('@fastify/swagger'))
await fastify.register(require('../index'))
fastify.route({
method: 'GET',
url: '/',
schema: {
querystring: {
type: 'object',
required: ['fields'],
additionalProperties: false,
properties: {
fields: {
type: 'array',
items: {
type: 'string'
},
minItems: 1,
//
// Note that this is an Open API version 2 configuration option. The
// options changed in version 3. The plugin currently only supports
// version 2 of Open API.
//
// Put `collectionFormat` on the same property which you are defining
// as an array of values. (i.e. `collectionFormat` should be a sibling
// of the `type: "array"` specification.)
collectionFormat: 'multi'
}
}
}
},
handler (request, reply) {
reply.send(request.query.fields)
}
})
fastify.listen({ port: 3000 }, (err, addr) => {
if (err) throw err
fastify.log.info(`Visit the documentation at ${addr}/documentation/`)
})
}
)()

View File

@@ -0,0 +1,124 @@
'use strict'
const Fastify = require('fastify')
; (async () => {
const fastify = Fastify({ logger: true })
await fastify.register(require('@fastify/swagger'), {
openapi: {
info: {
title: 'Test swagger',
description: 'testing the fastify swagger api',
version: '0.1.0'
},
servers: [{
url: 'http://localhost'
}],
components: {
securitySchemes: {
apiKey: {
type: 'apiKey',
name: 'apiKey',
in: 'header'
}
}
}
},
hideUntagged: true
})
await fastify.register(require('../index'), {
validatorUrl: false
})
await fastify.register(async function (fastify) {
fastify.put('/some-route/:id', {
schema: {
description: 'post some data',
tags: ['user', 'code'],
summary: 'qwerty',
security: [{ apiKey: [] }],
params: {
type: 'object',
properties: {
id: {
type: 'string',
description: 'user id'
}
}
},
body: {
type: 'object',
properties: {
hello: { type: 'string' },
obj: {
type: 'object',
properties: {
some: { type: 'string' }
}
}
}
},
response: {
201: {
description: 'Succesful response',
type: 'object',
properties: {
hello: { type: 'string' }
}
},
default: {
description: 'Default response',
type: 'object',
properties: {
foo: { type: 'string' }
}
}
}
}
}, (req, reply) => { reply.send({ hello: `Hello ${req.body.hello}` }) })
fastify.post('/some-route/:id', {
schema: {
description: 'post some data',
summary: 'qwerty',
security: [{ apiKey: [] }],
params: {
type: 'object',
properties: {
id: {
type: 'string',
description: 'user id'
}
}
},
body: {
type: 'object',
properties: {
hello: { type: 'string' },
obj: {
type: 'object',
properties: {
some: { type: 'string' }
}
}
}
},
response: {
201: {
description: 'Succesful response',
type: 'object',
properties: {
hello: { type: 'string' }
}
}
}
}
}, (req, reply) => { reply.send({ hello: `Hello ${req.body.hello}` }) })
})
fastify.listen({ port: 3000, hostname: '0.0.0.0' }, (err, addr) => {
if (err) throw err
fastify.log.info(`Visit the documentation at ${addr}/documentation/`)
})
})()

View File

@@ -0,0 +1,70 @@
'use strict'
const Fastify = require('fastify')
; (async () => {
const fastify = Fastify({ logger: true })
await fastify.register(require('@fastify/swagger'), {
swagger: {
info: {
title: 'Test swagger',
description: 'testing the fastify swagger api',
version: '0.1.0'
},
host: 'localhost',
schemes: ['http'],
consumes: ['application/json'],
produces: ['application/json']
}
})
await fastify.register(require('../index'), {
routePrefix: '/swagger-docs'
})
await fastify.register(async function (fastify) {
fastify.put('/some-route/:id', {
schema: {
description: 'post some data',
tags: ['user', 'code'],
summary: 'qwerty',
params: {
type: 'object',
properties: {
id: {
type: 'string',
description: 'user id'
}
}
},
body: {
type: 'object',
properties: {
hello: { type: 'string' },
obj: {
type: 'object',
properties: {
some: { type: 'string' }
}
}
}
},
response: {
201: {
description: 'Succesful response',
type: 'object',
properties: {
hello: { type: 'string' }
}
}
}
}
}, (req, reply) => {})
})
fastify.listen({ port: 3000, hostname: '0.0.0.0' }, (err, addr) => {
if (err) throw err
fastify.log.info(`Visit the documentation at ${addr}/swagger-docs/`)
})
})()

View File

@@ -0,0 +1,117 @@
'use strict'
const Fastify = require('fastify')
; (async () => {
const fastify = Fastify({ logger: true })
await fastify.register(require('@fastify/swagger'), {
swagger: {
info: {
title: 'Test swagger',
description: 'testing the fastify swagger api',
version: '0.1.0'
},
securityDefinitions: {
apiKey: {
type: 'apiKey',
name: 'apiKey',
in: 'header'
}
},
host: 'localhost:3000',
schemes: ['http'],
consumes: ['application/json'],
produces: ['application/json']
},
hideUntagged: true
})
await fastify.register(require('../index'), {
routePrefix: '/swagger-docs'
})
fastify.addSchema({
$id: 'user',
type: 'object',
properties: {
id: {
type: 'string',
description: 'user id'
}
}
})
fastify.addSchema({
$id: 'some',
type: 'object',
properties: {
some: { type: 'string' }
}
})
await fastify.register(async function (fastify) {
fastify.put('/some-route/:id', {
schema: {
description: 'post some data',
tags: ['user', 'code'],
summary: 'qwerty',
security: [{ apiKey: [] }],
params: { $ref: 'user#' },
body: {
type: 'object',
properties: {
hello: { type: 'string' },
obj: { $ref: 'some#' }
}
},
response: {
201: {
description: 'Succesful response',
type: 'object',
properties: {
hello: { type: 'string' }
}
},
default: {
description: 'Default response',
type: 'object',
properties: {
foo: { type: 'string' }
}
}
}
}
}, (req, reply) => { reply.send({ hello: `Hello ${req.body.hello}` }) })
fastify.post('/some-route/:id', {
schema: {
description: 'post some data',
summary: 'qwerty',
security: [{ apiKey: [] }],
params: { $ref: 'user#' },
body: {
type: 'object',
properties: {
hello: { type: 'string' },
obj: { $ref: 'some#' }
}
},
response: {
201: {
description: 'Succesful response',
type: 'object',
properties: {
hello: { type: 'string' }
}
}
}
}
}, (req, reply) => { reply.send({ hello: `Hello ${req.body.hello}` }) })
})
fastify.listen({ port: 3000, hostname: '0.0.0.0' }, (err, addr) => {
if (err) throw err
fastify.log.info(`Visit the documentation at ${addr}/swagger-docs/`)
})
})()

View File

@@ -0,0 +1,50 @@
'use strict'
const Fastify = require('fastify')
const readFileSync = require('node:fs').readFileSync
const resolve = require('node:path').resolve
const exampleLogo = readFileSync(
resolve(__dirname, '..', 'examples/static', 'example-logo.svg'),
'utf8'
)
; (async () => {
const fastify = Fastify({ logger: true })
await fastify.register(require('@fastify/swagger'), {
mode: 'static',
specification: {
path: './examples/example-static-specification.json'
}
})
await fastify.register(require('../index'), {
theme: {
js: [
{ filename: 'unloaded.js', content: 'window.onbeforeunload = function(){alert("unloaded test-theme")}' }
],
css: [
{ filename: 'theme.css', content: '.download-url-button {background: red !important;}' }
],
favicon: [
{
filename: 'favicon.svg',
rel: 'icon',
sizes: '16x16',
type: 'image/svg+xml',
content: exampleLogo
}
]
},
logo: {
type: 'image/svg+xml',
content: exampleLogo
}
})
fastify.listen({ port: process.env.PORT }, (err, addr) => {
if (err) throw err
fastify.log.info(`Visit the documentation at ${addr}/documentation/`)
})
})()

View File

@@ -0,0 +1,51 @@
{
"openapi": "3.0.0",
"info": {
"description": "Test swagger specification",
"version": "1.0.0",
"title": "Test swagger specification",
"contact": {
"email": "super.developer@gmail.com"
}
},
"servers": [
{
"url": "http://localhost:3000/",
"description": "Localhost (uses test data)"
}
],
"paths": {
"/status": {
"get": {
"description": "Status route, so we can check if server is alive",
"tags": [
"Status"
],
"responses": {
"200": {
"description": "Server is alive",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"health": {
"type": "boolean"
},
"date": {
"type": "string"
}
},
"example": {
"health": true,
"date": "2018-02-19T15:36:46.758Z"
}
}
}
}
}
}
}
}
}
}

View File

@@ -0,0 +1,31 @@
openapi: 3.0.0
info:
description: "Test swagger specification"
version: "1.0.0"
title: "Test swagger specification"
contact:
email: "super.developer@gmail.com"
servers:
- url: http://localhost:3000/
description: Localhost (uses test data)
paths:
/status:
get:
description: Status route, so we can check if server is alive
tags:
- Status
responses:
200:
description: 'Server is alive'
content:
application/json:
schema:
type: object
properties:
health:
type: boolean
date:
type: string
example:
health: true
date: "2018-02-19T15:36:46.758Z"

View File

@@ -0,0 +1,77 @@
'use strict'
const Fastify = require('fastify')
const qs = require('qs')
const Ajv = require('ajv')
; (async () => {
const ajv = new Ajv({
removeAdditional: true,
useDefaults: true,
coerceTypes: true
})
const fastify = Fastify({
logger: true,
querystringParser: (str) => {
const result = qs.parse(str)
if (result.filter && typeof result.filter === 'string') {
result.filter = JSON.parse(result.filter)
}
return result
}
})
ajv.addKeyword({
keyword: 'x-consume',
code: (ctx) => Promise.resolve(true)
})
fastify.setValidatorCompiler(({ schema }) => ajv.compile(schema))
await fastify.register(require('@fastify/swagger'), {
openapi: {
info: {
title: 'Test swagger',
description: 'testing the fastify swagger api',
version: '0.1.0'
}
}
})
await fastify.register(require('../index'))
await fastify.register(async function (fastify) {
fastify.route({
method: 'GET',
url: '/',
schema: {
querystring: {
type: 'object',
required: ['filter'],
additionalProperties: false,
properties: {
filter: {
type: 'object',
required: ['foo'],
properties: {
foo: { type: 'string' },
bar: { type: 'string' }
},
'x-consume': 'application/json'
}
}
}
},
handler (request, reply) {
reply.send(request.query.filter)
}
})
})
fastify.listen({ port: 3000 }, (err, addr) => {
if (err) throw err
fastify.log.info(`Visit the documentation at ${addr}/documentation/`)
})
})()

View File

@@ -0,0 +1,305 @@
'use strict'
const swaggerOption = {
swagger: {
info: {
title: 'Test swagger',
description: 'testing the fastify swagger api',
version: '0.1.0'
},
host: 'localhost',
schemes: ['http'],
consumes: ['application/json'],
produces: ['application/json'],
tags: [
{ name: 'tag' }
],
externalDocs: {
description: 'Find more info here',
url: 'https://swagger.io'
},
securityDefinitions: {
apiKey: {
type: 'apiKey',
name: 'apiKey',
in: 'header'
}
},
security: [{
apiKey: []
}]
}
}
const openapiOption = {
openapi: {
info: {
title: 'Test swagger',
description: 'testing the fastify swagger api',
version: '0.1.0'
},
servers: [
{
url: 'http://localhost'
}
],
tags: [
{ name: 'tag' }
],
components: {
securitySchemes: {
apiKey: {
type: 'apiKey',
name: 'apiKey',
in: 'header'
}
}
},
security: [{
apiKey: []
}],
externalDocs: {
description: 'Find more info here',
url: 'https://swagger.io'
}
}
}
const openapiRelativeOptions = {
openapi: {
info: {
title: 'Test swagger',
description: 'testing the fastify swagger api',
version: '0.1.0'
},
servers: [
{
url: '/test'
}
],
tags: [
{ name: 'tag' }
],
components: {
securitySchemes: {
apiKey: {
type: 'apiKey',
name: 'apiKey',
in: 'header'
}
}
},
security: [{
apiKey: []
}],
externalDocs: {
description: 'Find more info here',
url: 'https://swagger.io'
}
},
stripBasePath: false
}
const schemaQuerystring = {
schema: {
response: {
200: {
type: 'object',
properties: {
hello: { type: 'string' }
}
}
},
querystring: {
hello: { type: 'string' },
world: { type: 'string' }
}
}
}
const schemaBody = {
schema: {
body: {
type: 'object',
properties: {
hello: { type: 'string' },
obj: {
type: 'object',
properties: {
some: { type: 'string' },
constantProp: { const: 'my-const' }
}
}
},
required: ['hello']
}
}
}
const schemaParams = {
schema: {
params: {
type: 'object',
properties: {
id: {
type: 'string',
description: 'user id'
}
}
}
}
}
const schemaHeaders = {
schema: {
headers: {
type: 'object',
properties: {
authorization: {
type: 'string',
description: 'api token'
}
},
required: ['authorization']
}
}
}
const schemaHeadersParams = {
schema: {
headers: {
type: 'object',
properties: {
'x-api-token': {
type: 'string',
description: 'optional api token'
},
'x-api-version': {
type: 'string',
description: 'optional api version'
}
}
},
params: {
type: 'object',
properties: {
id: {
type: 'string',
description: 'user id'
}
}
}
}
}
const schemaSecurity = {
schema: {
security: [
{
apiKey: []
}
]
}
}
const schemaConsumes = {
schema: {
consumes: ['application/x-www-form-urlencoded'],
body: {
type: 'object',
properties: {
hello: {
description: 'hello',
type: 'string'
}
},
required: ['hello']
}
}
}
const schemaProduces = {
schema: {
produces: ['*/*'],
response: {
200: {
type: 'object',
properties: {
hello: {
description: 'hello',
type: 'string'
}
},
required: ['hello']
}
}
}
}
const schemaCookies = {
schema: {
cookies: {
type: 'object',
properties: {
bar: { type: 'string' }
}
}
}
}
const schemaAllOf = {
schema: {
querystring: {
allOf: [
{
type: 'object',
properties: {
foo: { type: 'string' }
}
}
]
}
}
}
const schemaExtension = {
schema: {
'x-tension': true
}
}
const schemaOperationId = {
schema: {
operationId: 'helloWorld',
response: {
200: {
type: 'object',
properties: {
hello: {
description: 'hello',
type: 'string'
}
},
required: ['hello']
}
}
}
}
module.exports = {
openapiOption,
openapiRelativeOptions,
swaggerOption,
schemaQuerystring,
schemaBody,
schemaParams,
schemaHeaders,
schemaHeadersParams,
schemaSecurity,
schemaConsumes,
schemaProduces,
schemaCookies,
schemaAllOf,
schemaExtension,
schemaOperationId
}

View File

@@ -0,0 +1,21 @@
'use strict'
const Fastify = require('fastify')
; (async () => {
const fastify = Fastify({ logger: true })
await fastify.register(require('@fastify/swagger'), {
mode: 'static',
specification: {
path: './examples/example-static-specification.json'
}
})
await fastify.register(require('../index'))
fastify.listen({ port: 3000 }, (err, addr) => {
if (err) throw err
fastify.log.info(`Visit the documentation at ${addr}/documentation/`)
})
})()

View File

@@ -0,0 +1,21 @@
'use strict'
const Fastify = require('fastify')
; (async () => {
const fastify = Fastify({ logger: true })
await fastify.register(require('@fastify/swagger'), {
mode: 'static',
specification: {
path: './examples/example-static-specification.yaml'
}
})
await fastify.register(require('../index'))
fastify.listen({ port: 3000 }, (err, addr) => {
if (err) throw err
fastify.log.info(`Visit the documentation at ${addr}/documentation/`)
})
})()

View File

@@ -0,0 +1 @@
<svg version="1.1" id="example-logo" xmlns="http://www.w3.org/2000/svg" x="0" y="0" viewBox="0 0 80.3 80.3" style="enable-background:new 0 0 80.3 80.3" xml:space="preserve"><style>.st4{fill:#fff}.st6{fill:#0c2c48}</style><circle cx="40.2" cy="40.2" r="40.2" style="fill:#f6a09c"/><path class="st6" d="M67.7 56.2c0 .7-.5 1.2-1.2 1.2H13.8c-.7 0-1.2-.5-1.2-1.2V17.4c0-.7.5-1.2 1.2-1.2h52.8c.7 0 1.2.5 1.2 1.2v38.8z"/><path style="fill:#bababa" d="M29.8 56.3h20.7v7H29.8z"/><path style="fill:#a3b9c4" d="M15.5 18.6h49.2v30.5H15.5z"/><path class="st4" d="M12.6 48.1v8.1c0 .7.5 1.2 1.2 1.2h52.8c.7 0 1.2-.5 1.2-1.2v-8.1H12.6zM55.7 68.1v-2.2c0-1.7-1.3-3-3-3h-25c-1.7 0-3 1.3-3 3v2.2h31z"/><circle class="st6" cx="40.2" cy="52.5" r="1.5"/><g><path class="st4" d="M32.8 38.2 23.5 34v-1.6l9.3-4.5v2.4l-6.6 2.8v.1l6.6 2.7v2.3z"/><path d="m44.6 24.6-7 16.7h-2.8l6.9-16.7h2.9z" style="fill:#50646f"/><path d="M56.2 33.8 46.9 38v-2.4l6.7-2.7-6.7-2.8v-2.4l9.3 4.4v1.7z" style="fill:#ed6b5a"/></g></svg>

After

Width:  |  Height:  |  Size: 988 B

View File

@@ -0,0 +1 @@
{}

View File

@@ -0,0 +1,38 @@
'use strict'
const Fastify = require('fastify')
; (async () => {
const fastify = Fastify({ logger: true })
await fastify.register(require('@fastify/swagger'), {
mode: 'static',
specification: {
path: './examples/example-static-specification.json'
}
})
await fastify.register(require('../index'), {
theme: {
js: [
{ filename: 'special.js', content: 'alert("loaded test-theme")' }
],
css: [
{ filename: 'theme.css', content: '* {border: 1px red solid;}' }
],
favicon: [
{
filename: 'favicon.png',
rel: 'icon',
sizes: '16x16',
type: 'image/png',
content: Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAA8AAAAQCAQAAABjX+2PAAAAIGNIUk0AAHomAACAhAAA+gAAAIDoAAB1MAAA6mAAADqYAAAXcJy6UTwAAAACYktHRAD/h4/MvwAAAAd0SU1FB+cCEQ06N8A8CiUAAADnSURBVBjTrdE/K8QBAMbxz/0TLrnUWcTg7ySLewGEwWDRzSYpyULJbGG6wWBTlMEbkHsFNnVloAwXudIlnDru1O9nOCex3rM89TzL0/eh1Ypo//Zk5CdM6JP2IWFOxbmMKZVmPWzbrJSamG5FNXUFx42yV16oqCQUerNr2pghsSgS1sw4kxNVVvbu3rwjSwJ67Kgq2XMjtO/AnWsnVgwQNy6rQ8GkURWBpCebXnR5gA11j5b1OxT4EKq6dGurMWvQqqw2LPoUKDq1LqPzN4q0rCuvckbE/pOakHdhQfwvwKan8Nzad74AkR8/Ir6qAvAAAAAldEVYdGRhdGU6Y3JlYXRlADIwMjMtMDItMTdUMTM6NTg6NTUrMDA6MDBjkr64AAAAJXRFWHRkYXRlOm1vZGlmeQAyMDIzLTAyLTE3VDEzOjU4OjU1KzAwOjAwEs8GBAAAACh0RVh0ZGF0ZTp0aW1lc3RhbXAAMjAyMy0wMi0xN1QxMzo1ODo1NSswMDowMEXaJ9sAAAAASUVORK5CYII=', 'base64')
}
]
}
})
fastify.listen({ port: 3000 }, (err, addr) => {
if (err) throw err
fastify.log.info(`Visit the documentation at ${addr}/documentation/`)
})
})()