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,52 @@
'use strict'
const fastify = require('fastify')({
// Need to add a collectionFormat keyword to ajv in fastify instance
ajv: {
customOptions: {
keywords: ['collectionFormat']
}
}
})
fastify.register(require('../index'), {
exposeRoute: true
})
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
console.log(`listening on ${addr}`)
})

View File

@@ -0,0 +1,170 @@
'use strict'
const fastify = require('fastify')()
fastify.register(require('../index'), {
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,
exposeRoute: true
})
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.post('/subscribe', {
schema: {
description: 'subscribe for webhooks',
summary: 'webhook example',
security: [],
response: {
201: {
description: 'Succesful response'
}
},
body: {
type: 'object',
properties: {
callbackUrl: {
type: 'string',
examples: ['https://example.com']
}
}
},
callbacks: {
myEvent: {
'{$request.body#/callbackUrl}': {
post: {
requestBody: {
content: {
'application/json': {
schema: {
type: 'object',
properties: {
message: {
type: 'string',
example: 'Some event happened'
}
},
required: [
'message'
]
}
}
}
},
responses: {
200: {
description: 'Success'
}
}
}
}
}
}
}
})
fastify.listen({ port: 3000 }, err => {
if (err) throw err
})

View File

@@ -0,0 +1,63 @@
'use strict'
const fastify = require('fastify')()
fastify.register(require('../index'), {
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']
},
exposeRoute: true,
routePrefix: '/swagger-docs'
})
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' }
}
}
}
}
}, () => {})
})
fastify.listen({ port: 3000 }, err => {
if (err) throw err
})

View File

@@ -0,0 +1,109 @@
'use strict'
const fastify = require('fastify')()
fastify.register(require('../index'), {
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,
exposeRoute: true
})
fastify.addSchema({
$id: 'user',
type: 'object',
properties: {
id: {
type: 'string',
description: 'user id'
}
}
})
fastify.addSchema({
$id: 'some',
type: 'object',
properties: {
some: { type: 'string' }
}
})
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 }, err => {
if (err) throw err
})

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,72 @@
'use strict'
const qs = require('qs')
const Ajv = require('ajv')
const ajv = new Ajv({
removeAdditional: true,
useDefaults: true,
coerceTypes: true
})
const fastify = require('fastify')({
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: () => Promise.resolve(true)
})
fastify.setValidatorCompiler(({ schema }) => ajv.compile(schema))
fastify.register(require('../index'), {
openapi: {
info: {
title: 'Test swagger',
description: 'testing the fastify swagger api',
version: '0.1.0'
}
},
exposeRoute: true
})
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) => {
if (err) throw err
})

View File

@@ -0,0 +1,388 @@
'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'
},
bearerAuth: {
type: 'http',
scheme: 'bearer'
}
}
},
security: [{
apiKey: [],
bearerAuth: []
}],
externalDocs: {
description: 'Find more info here',
url: 'https://swagger.io'
}
}
}
const openapiWebHookOption = {
openapi: {
openapi: '3.1.0',
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'
}
},
schemas: {
Pet: {
require: ['id', 'name'],
properties: {
id: {
type: 'integer',
format: 'int64'
},
name: {
type: 'string'
},
tag: {
type: 'string'
}
}
}
}
},
security: [
{
apiKey: []
}
],
externalDocs: {
description: 'Find more info here',
url: 'https://swagger.io'
},
webhooks: {
newPet: {
post: {
requestBody: {
description: 'Information about a new pet in the system',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Pet'
}
}
}
},
responses: {
200: {
description:
'Return a 200 status to indicate that the data was received successfully'
}
}
}
}
}
}
}
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: {
type: 'object',
properties: {
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,
openapiWebHookOption,
swaggerOption,
schemaQuerystring,
schemaBody,
schemaParams,
schemaHeaders,
schemaHeadersParams,
schemaSecurity,
schemaConsumes,
schemaProduces,
schemaCookies,
schemaAllOf,
schemaExtension,
schemaOperationId
}

View File

@@ -0,0 +1,17 @@
'use strict'
const fastify = require('fastify')({ logger: true })
// const swagger = require('@fastify/swagger')
const swagger = require('..')
fastify.register(swagger, {
mode: 'static',
specification: {
path: './examples/example-static-specification.json'
},
exposeRoute: true
})
fastify.listen({ port: 3000 }, (err) => {
if (err) throw err
})

View File

@@ -0,0 +1,17 @@
'use strict'
const fastify = require('fastify')({ logger: true })
// const swagger = require('@fastify/swagger')
const swagger = require('..')
fastify.register(swagger, {
mode: 'static',
specification: {
path: './examples/example-static-specification.yaml'
},
exposeRoute: true
})
fastify.listen({ port: 3000 }, (err) => {
if (err) throw err
})

View File

@@ -0,0 +1,3 @@
{
"name": "test"
}