Skip to content

Commit

Permalink
Refactor body schemata and generate apidocs
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkvon committed Jan 17, 2024
1 parent b330fa8 commit c76d6d9
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 59 deletions.
121 changes: 109 additions & 12 deletions apidocs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,7 @@
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"email": {
"type": "string",
"format": "email"
}
},
"required": [
"email"
],
"additionalProperties": false
"$ref": "#/components/schemas/init"
}
}
}
Expand All @@ -51,13 +41,23 @@
}
}
},
"/webhook-receiver": {
"/notification": {
"post": {
"description": "",
"responses": {
"default": {
"description": ""
}
},
"requestBody": {
"required": true,
"content": {
"application/ld+json": {
"schema": {
"$ref": "#/components/schemas/notification"
}
}
}
}
}
},
Expand All @@ -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
}
}
}
}
58 changes: 12 additions & 46 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,67 +34,32 @@ 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)
.post(
'/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,
)
Expand Down
3 changes: 2 additions & 1 deletion src/generate-api-docs.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -10,7 +11,7 @@ const doc = {
},
servers: [{ url: '/' }],
tags: [],
components: {},
components: { '@schemas': { init, notification } },
}

const outputFile = '../apidocs/openapi.json'
Expand Down
44 changes: 44 additions & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
@@ -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,
}

0 comments on commit c76d6d9

Please sign in to comment.