From c76d6d9716239262b440290490afd0dc16efea85 Mon Sep 17 00:00:00 2001 From: mrkvon Date: Wed, 17 Jan 2024 10:16:35 +0100 Subject: [PATCH] Refactor body schemata and generate apidocs --- apidocs/openapi.json | 121 +++++++++++++++++++++++++++++++++++---- src/app.ts | 58 ++++--------------- src/generate-api-docs.ts | 3 +- src/schema.ts | 44 ++++++++++++++ 4 files changed, 167 insertions(+), 59 deletions(-) create mode 100644 src/schema.ts diff --git a/apidocs/openapi.json b/apidocs/openapi.json index 5017ede..5aa7466 100644 --- a/apidocs/openapi.json +++ b/apidocs/openapi.json @@ -24,17 +24,7 @@ "content": { "application/json": { "schema": { - "type": "object", - "properties": { - "email": { - "type": "string", - "format": "email" - } - }, - "required": [ - "email" - ], - "additionalProperties": false + "$ref": "#/components/schemas/init" } } } @@ -51,13 +41,23 @@ } } }, - "/webhook-receiver": { + "/notification": { "post": { "description": "", "responses": { "default": { "description": "" } + }, + "requestBody": { + "required": true, + "content": { + "application/ld+json": { + "schema": { + "$ref": "#/components/schemas/notification" + } + } + } } } }, @@ -81,5 +81,102 @@ } } } + }, + "components": { + "schemas": { + "init": { + "type": "object", + "properties": { + "email": { + "type": "string", + "format": "email" + } + }, + "required": [ + "email" + ], + "additionalProperties": false + }, + "notification": { + "type": "object", + "properties": { + "@context": { + "const": "https://www.w3.org/ns/activitystreams" + }, + "id": { + "type": "string" + }, + "type": { + "const": "Create" + }, + "actor": { + "type": "object", + "properties": { + "type": { + "const": "Person" + }, + "id": { + "type": "string", + "format": "uri" + }, + "name": { + "type": "string" + } + }, + "required": [ + "type", + "id" + ] + }, + "object": { + "type": "object", + "properties": { + "type": { + "const": "Note" + }, + "id": { + "type": "string", + "format": "uri" + }, + "content": { + "type": "string" + } + }, + "required": [ + "type", + "id", + "content" + ] + }, + "target": { + "type": "object", + "properties": { + "type": { + "const": "Person" + }, + "id": { + "type": "string", + "format": "uri" + }, + "name": { + "type": "string" + } + }, + "required": [ + "type", + "id" + ] + } + }, + "required": [ + "@context", + "type", + "actor", + "object", + "target" + ], + "additionalProperties": false + } + } } } \ No newline at end of file diff --git a/src/app.ts b/src/app.ts index 8d63099..f1ff699 100644 --- a/src/app.ts +++ b/src/app.ts @@ -18,6 +18,7 @@ import { } from './middlewares/authorizeGroup' import { solidAuth } from './middlewares/solidAuth' import { validateBody } from './middlewares/validate' +import * as schema from './schema' const app = new Koa() app.proxy = isBehindProxy @@ -33,23 +34,13 @@ router content: { 'application/json': { schema: { - type: 'object', - properties: { - email: { type: 'string', format: 'email' }, - }, - required: ['email'], - additionalProperties: false, + $ref: '#/components/schemas/init', }, }, }, } */ - validateBody({ - type: 'object', - properties: { email: { type: 'string', format: 'email' } }, - required: ['email'], - additionalProperties: false, - }), + validateBody(schema.init), initializeIntegration, ) .get('/verify-email', checkVerificationLink, finishIntegration) @@ -57,43 +48,18 @@ router '/notification', solidAuth, authorizeGroups(allowedGroups), - validateBody({ - type: 'object', - properties: { - '@context': { const: 'https://www.w3.org/ns/activitystreams' }, - id: { type: 'string' }, - type: { const: 'Create' }, - actor: { - type: 'object', - properties: { - type: { const: 'Person' }, - id: { type: 'string', format: 'uri' }, - name: { type: 'string' }, - }, - required: ['type', 'id'], - }, - object: { - type: 'object', - properties: { - type: { const: 'Note' }, - id: { type: 'string', format: 'uri' }, - content: { type: 'string' }, - }, - required: ['type', 'id', 'content'], - }, - target: { - type: 'object', - properties: { - type: { const: 'Person' }, - id: { type: 'string', format: 'uri' }, - name: { type: 'string' }, + /* #swagger.requestBody = { + required: true, + content: { + 'application/ld+json': { + schema: { + $ref: '#/components/schemas/notification', }, - required: ['type', 'id'], }, }, - required: ['@context', 'type', 'actor', 'object', 'target'], - additionalProperties: false, - }), + } + */ + validateBody(schema.notification), checkGroupMembership(allowedGroups, 'request.body.target.id', 400), notification, ) diff --git a/src/generate-api-docs.ts b/src/generate-api-docs.ts index 999fc1d..512cc08 100644 --- a/src/generate-api-docs.ts +++ b/src/generate-api-docs.ts @@ -1,5 +1,6 @@ // https://swagger-autogen.github.io/docs/getting-started/advanced-usage#openapi-3x import swaggerAutogen from 'swagger-autogen' +import { init, notification } from './schema' const doc = { info: { @@ -10,7 +11,7 @@ const doc = { }, servers: [{ url: '/' }], tags: [], - components: {}, + components: { '@schemas': { init, notification } }, } const outputFile = '../apidocs/openapi.json' diff --git a/src/schema.ts b/src/schema.ts new file mode 100644 index 0000000..23329b4 --- /dev/null +++ b/src/schema.ts @@ -0,0 +1,44 @@ +export const init = { + type: 'object', + properties: { email: { type: 'string', format: 'email' } }, + required: ['email'], + additionalProperties: false, +} + +export const notification = { + type: 'object', + properties: { + '@context': { const: 'https://www.w3.org/ns/activitystreams' }, + id: { type: 'string' }, + type: { const: 'Create' }, + actor: { + type: 'object', + properties: { + type: { const: 'Person' }, + id: { type: 'string', format: 'uri' }, + name: { type: 'string' }, + }, + required: ['type', 'id'], + }, + object: { + type: 'object', + properties: { + type: { const: 'Note' }, + id: { type: 'string', format: 'uri' }, + content: { type: 'string' }, + }, + required: ['type', 'id', 'content'], + }, + target: { + type: 'object', + properties: { + type: { const: 'Person' }, + id: { type: 'string', format: 'uri' }, + name: { type: 'string' }, + }, + required: ['type', 'id'], + }, + }, + required: ['@context', 'type', 'actor', 'object', 'target'], + additionalProperties: false, +}