[backend/web-api] Add endpoint security policy, response & request body types

This commit is contained in:
Laura Hausmann 2023-12-12 23:47:21 +01:00
parent f4dcd08e89
commit 2d6c4ef5d3
No known key found for this signature in database
GPG Key ID: D044E84C5BE01605
16 changed files with 211 additions and 39 deletions

58
.pnp.cjs generated
View File

@ -1898,10 +1898,10 @@ const RAW_RUNTIME_STATE =
}]\ }]\
]],\ ]],\
["@iceshrimp/koa-openapi", [\ ["@iceshrimp/koa-openapi", [\
["npm:2.7.0::__archiveUrl=https%3A%2F%2Ficeshrimp.dev%2Fapi%2Fpackages%2Ficeshrimp%2Fnpm%2F%2540iceshrimp%252Fkoa-openapi%2F-%2F2.7.0%2Fkoa-openapi-2.7.0.tgz", {\ ["npm:3.2.0::__archiveUrl=https%3A%2F%2Ficeshrimp.dev%2Fapi%2Fpackages%2Ficeshrimp%2Fnpm%2F%2540iceshrimp%252Fkoa-openapi%2F-%2F3.2.0%2Fkoa-openapi-3.2.0.tgz", {\
"packageLocation": "./.yarn/cache/@iceshrimp-koa-openapi-npm-2.7.0-37778d7452-21685db4ea.zip/node_modules/@iceshrimp/koa-openapi/",\ "packageLocation": "./.yarn/cache/@iceshrimp-koa-openapi-npm-3.2.0-d2c290057b-5ec6ea58be.zip/node_modules/@iceshrimp/koa-openapi/",\
"packageDependencies": [\ "packageDependencies": [\
["@iceshrimp/koa-openapi", "npm:2.7.0::__archiveUrl=https%3A%2F%2Ficeshrimp.dev%2Fapi%2Fpackages%2Ficeshrimp%2Fnpm%2F%2540iceshrimp%252Fkoa-openapi%2F-%2F2.7.0%2Fkoa-openapi-2.7.0.tgz"],\ ["@iceshrimp/koa-openapi", "npm:3.2.0::__archiveUrl=https%3A%2F%2Ficeshrimp.dev%2Fapi%2Fpackages%2Ficeshrimp%2Fnpm%2F%2540iceshrimp%252Fkoa-openapi%2F-%2F3.2.0%2Fkoa-openapi-3.2.0.tgz"],\
["@hapi/boom", "npm:10.0.1"],\ ["@hapi/boom", "npm:10.0.1"],\
["@koa/cors", "npm:4.0.0"],\ ["@koa/cors", "npm:4.0.0"],\
["@koa/router", "npm:12.0.1"],\ ["@koa/router", "npm:12.0.1"],\
@ -1911,7 +1911,7 @@ const RAW_RUNTIME_STATE =
["koa", "npm:2.14.2"],\ ["koa", "npm:2.14.2"],\
["koa-body", "npm:6.0.1"],\ ["koa-body", "npm:6.0.1"],\
["koa-helmet", "npm:7.0.2"],\ ["koa-helmet", "npm:7.0.2"],\
["koa2-swagger-ui", "virtual:37778d7452aa22a4b9b696d3401d761eb63cf07fe0900455c93a17ab008fd0bea8cb487e82bff1386539d52523936a6d87cd46d4e8395c6f97dee7afea734531#npm:5.10.0"],\ ["koa2-swagger-ui", "virtual:d2c290057b75f80fd76a22ec676ab3d63a5dabf80ff85b75aaf6bde56eebfb4dfa277221c743544b6d668bc070c759965f7368b428fc6cbd6ad12069d18f1396#npm:5.10.0"],\
["lodash", "npm:4.17.21"],\ ["lodash", "npm:4.17.21"],\
["openapi-types", "npm:12.1.3"],\ ["openapi-types", "npm:12.1.3"],\
["reflect-metadata", "npm:0.1.13"]\ ["reflect-metadata", "npm:0.1.13"]\
@ -1919,6 +1919,22 @@ const RAW_RUNTIME_STATE =
"linkType": "HARD"\ "linkType": "HARD"\
}]\ }]\
]],\ ]],\
["@iceshrimp/ts-json-schema-generator", [\
["npm:1.0.1::__archiveUrl=https%3A%2F%2Ficeshrimp.dev%2Fapi%2Fpackages%2Ficeshrimp%2Fnpm%2F%2540iceshrimp%252Fts-json-schema-generator%2F-%2F1.0.1%2Fts-json-schema-generator-1.0.1.tgz", {\
"packageLocation": "./.yarn/cache/@iceshrimp-ts-json-schema-generator-npm-1.0.1-fe26c7afc1-dbac3ba178.zip/node_modules/@iceshrimp/ts-json-schema-generator/",\
"packageDependencies": [\
["@iceshrimp/ts-json-schema-generator", "npm:1.0.1::__archiveUrl=https%3A%2F%2Ficeshrimp.dev%2Fapi%2Fpackages%2Ficeshrimp%2Fnpm%2F%2540iceshrimp%252Fts-json-schema-generator%2F-%2F1.0.1%2Fts-json-schema-generator-1.0.1.tgz"],\
["@types/json-schema", "npm:7.0.15"],\
["commander", "npm:11.1.0"],\
["glob", "npm:8.1.0"],\
["json5", "npm:2.2.3"],\
["normalize-path", "npm:3.0.0"],\
["safe-stable-stringify", "npm:2.4.3"],\
["typescript", "patch:typescript@npm%3A5.3.3#optional!builtin<compat/typescript>::version=5.3.3&hash=e012d7"]\
],\
"linkType": "HARD"\
}]\
]],\
["@ioredis/commands", [\ ["@ioredis/commands", [\
["npm:1.2.0", {\ ["npm:1.2.0", {\
"packageLocation": "./.yarn/cache/@ioredis-commands-npm-1.2.0-47541de88b-a8253c9539.zip/node_modules/@ioredis/commands/",\ "packageLocation": "./.yarn/cache/@ioredis-commands-npm-1.2.0-47541de88b-a8253c9539.zip/node_modules/@ioredis/commands/",\
@ -4147,6 +4163,13 @@ const RAW_RUNTIME_STATE =
["@types/json-schema", "npm:7.0.12"]\ ["@types/json-schema", "npm:7.0.12"]\
],\ ],\
"linkType": "HARD"\ "linkType": "HARD"\
}],\
["npm:7.0.15", {\
"packageLocation": "./.yarn/cache/@types-json-schema-npm-7.0.15-fd16381786-1a3c3e0623.zip/node_modules/@types/json-schema/",\
"packageDependencies": [\
["@types/json-schema", "npm:7.0.15"]\
],\
"linkType": "HARD"\
}]\ }]\
]],\ ]],\
["@types/json5", [\ ["@types/json5", [\
@ -7217,7 +7240,8 @@ const RAW_RUNTIME_STATE =
["@bull-board/ui", "npm:5.6.0"],\ ["@bull-board/ui", "npm:5.6.0"],\
["@discordapp/twemoji", "npm:14.1.2"],\ ["@discordapp/twemoji", "npm:14.1.2"],\
["@hapi/boom", "npm:10.0.1"],\ ["@hapi/boom", "npm:10.0.1"],\
["@iceshrimp/koa-openapi", "npm:2.7.0::__archiveUrl=https%3A%2F%2Ficeshrimp.dev%2Fapi%2Fpackages%2Ficeshrimp%2Fnpm%2F%2540iceshrimp%252Fkoa-openapi%2F-%2F2.7.0%2Fkoa-openapi-2.7.0.tgz"],\ ["@iceshrimp/koa-openapi", "npm:3.2.0::__archiveUrl=https%3A%2F%2Ficeshrimp.dev%2Fapi%2Fpackages%2Ficeshrimp%2Fnpm%2F%2540iceshrimp%252Fkoa-openapi%2F-%2F3.2.0%2Fkoa-openapi-3.2.0.tgz"],\
["@iceshrimp/ts-json-schema-generator", "npm:1.0.1::__archiveUrl=https%3A%2F%2Ficeshrimp.dev%2Fapi%2Fpackages%2Ficeshrimp%2Fnpm%2F%2540iceshrimp%252Fts-json-schema-generator%2F-%2F1.0.1%2Fts-json-schema-generator-1.0.1.tgz"],\
["@koa/cors", "npm:3.4.3"],\ ["@koa/cors", "npm:3.4.3"],\
["@koa/multer", "virtual:aa59773ac87791c4813d53447077fcf8a847d6de5a301d34dc31286584b1dbb26d30d3adb5b4c41c1e8aea04371e926fda05c09c6253647c432e11d872a304ba#npm:3.0.2"],\ ["@koa/multer", "virtual:aa59773ac87791c4813d53447077fcf8a847d6de5a301d34dc31286584b1dbb26d30d3adb5b4c41c1e8aea04371e926fda05c09c6253647c432e11d872a304ba#npm:3.0.2"],\
["@koa/router", "npm:9.0.1"],\ ["@koa/router", "npm:9.0.1"],\
@ -7238,6 +7262,7 @@ const RAW_RUNTIME_STATE =
["@types/formidable", "npm:2.0.6"],\ ["@types/formidable", "npm:2.0.6"],\
["@types/js-yaml", "npm:4.0.5"],\ ["@types/js-yaml", "npm:4.0.5"],\
["@types/jsdom", "npm:21.1.1"],\ ["@types/jsdom", "npm:21.1.1"],\
["@types/json-schema", "npm:7.0.15"],\
["@types/jsonld", "npm:1.5.9"],\ ["@types/jsonld", "npm:1.5.9"],\
["@types/jsrsasign", "npm:10.5.8"],\ ["@types/jsrsasign", "npm:10.5.8"],\
["@types/koa", "npm:2.13.7"],\ ["@types/koa", "npm:2.13.7"],\
@ -7344,6 +7369,7 @@ const RAW_RUNTIME_STATE =
["node-fetch", "npm:3.3.1"],\ ["node-fetch", "npm:3.3.1"],\
["nodemailer", "npm:6.9.3"],\ ["nodemailer", "npm:6.9.3"],\
["oauth", "npm:0.10.0"],\ ["oauth", "npm:0.10.0"],\
["openapi-types", "npm:12.1.3"],\
["os-utils", "npm:0.0.14"],\ ["os-utils", "npm:0.0.14"],\
["otpauth", "npm:9.1.4"],\ ["otpauth", "npm:9.1.4"],\
["parse-duration", "npm:1.1.0"],\ ["parse-duration", "npm:1.1.0"],\
@ -9067,6 +9093,13 @@ const RAW_RUNTIME_STATE =
],\ ],\
"linkType": "HARD"\ "linkType": "HARD"\
}],\ }],\
["npm:11.1.0", {\
"packageLocation": "./.yarn/cache/commander-npm-11.1.0-56e979613c-66bd2d8a05.zip/node_modules/commander/",\
"packageDependencies": [\
["commander", "npm:11.1.0"]\
],\
"linkType": "HARD"\
}],\
["npm:2.20.3", {\ ["npm:2.20.3", {\
"packageLocation": "./.yarn/cache/commander-npm-2.20.3-d8dcbaa39b-90c5b68986.zip/node_modules/commander/",\ "packageLocation": "./.yarn/cache/commander-npm-2.20.3-d8dcbaa39b-90c5b68986.zip/node_modules/commander/",\
"packageDependencies": [\ "packageDependencies": [\
@ -17145,10 +17178,10 @@ const RAW_RUNTIME_STATE =
],\ ],\
"linkType": "SOFT"\ "linkType": "SOFT"\
}],\ }],\
["virtual:37778d7452aa22a4b9b696d3401d761eb63cf07fe0900455c93a17ab008fd0bea8cb487e82bff1386539d52523936a6d87cd46d4e8395c6f97dee7afea734531#npm:5.10.0", {\ ["virtual:d2c290057b75f80fd76a22ec676ab3d63a5dabf80ff85b75aaf6bde56eebfb4dfa277221c743544b6d668bc070c759965f7368b428fc6cbd6ad12069d18f1396#npm:5.10.0", {\
"packageLocation": "./.yarn/__virtual__/koa2-swagger-ui-virtual-c4b42f8a3a/0/cache/koa2-swagger-ui-npm-5.10.0-54bce94261-40575d377d.zip/node_modules/koa2-swagger-ui/",\ "packageLocation": "./.yarn/__virtual__/koa2-swagger-ui-virtual-285d8b91f4/0/cache/koa2-swagger-ui-npm-5.10.0-54bce94261-40575d377d.zip/node_modules/koa2-swagger-ui/",\
"packageDependencies": [\ "packageDependencies": [\
["koa2-swagger-ui", "virtual:37778d7452aa22a4b9b696d3401d761eb63cf07fe0900455c93a17ab008fd0bea8cb487e82bff1386539d52523936a6d87cd46d4e8395c6f97dee7afea734531#npm:5.10.0"],\ ["koa2-swagger-ui", "virtual:d2c290057b75f80fd76a22ec676ab3d63a5dabf80ff85b75aaf6bde56eebfb4dfa277221c743544b6d668bc070c759965f7368b428fc6cbd6ad12069d18f1396#npm:5.10.0"],\
["@types/koa", null],\ ["@types/koa", null],\
["handlebars", "npm:4.7.8"],\ ["handlebars", "npm:4.7.8"],\
["lodash", "npm:4.17.21"],\ ["lodash", "npm:4.17.21"],\
@ -22153,6 +22186,15 @@ const RAW_RUNTIME_STATE =
"linkType": "HARD"\ "linkType": "HARD"\
}]\ }]\
]],\ ]],\
["safe-stable-stringify", [\
["npm:2.4.3", {\
"packageLocation": "./.yarn/cache/safe-stable-stringify-npm-2.4.3-d895741b40-a6c192bbef.zip/node_modules/safe-stable-stringify/",\
"packageDependencies": [\
["safe-stable-stringify", "npm:2.4.3"]\
],\
"linkType": "HARD"\
}]\
]],\
["safer-buffer", [\ ["safer-buffer", [\
["npm:2.1.2", {\ ["npm:2.1.2", {\
"packageLocation": "./.yarn/cache/safer-buffer-npm-2.1.2-8d5c0b705e-7eaf7a0cf3.zip/node_modules/safer-buffer/",\ "packageLocation": "./.yarn/cache/safer-buffer-npm-2.1.2-8d5c0b705e-7eaf7a0cf3.zip/node_modules/safer-buffer/",\

Binary file not shown.

BIN
.yarn/cache/@iceshrimp-koa-openapi-npm-3.2.0-d2c290057b-5ec6ea58be.zip (Stored with Git LFS) vendored Normal file

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:b24195bad73274f4cbfb09460be0eacca689dbeb6dbb7a53cab91e5d344146d2
size 6988

BIN
.yarn/cache/commander-npm-11.1.0-56e979613c-66bd2d8a05.zip (Stored with Git LFS) vendored Normal file

Binary file not shown.

BIN
.yarn/cache/safe-stable-stringify-npm-2.4.3-d895741b40-a6c192bbef.zip (Stored with Git LFS) vendored Normal file

Binary file not shown.

View File

@ -28,7 +28,7 @@
"@bull-board/ui": "5.6.0", "@bull-board/ui": "5.6.0",
"@discordapp/twemoji": "14.1.2", "@discordapp/twemoji": "14.1.2",
"@hapi/boom": "^10.0.1", "@hapi/boom": "^10.0.1",
"@iceshrimp/koa-openapi": "^2.7.0", "@iceshrimp/koa-openapi": "^3.2.0",
"@koa/cors": "3.4.3", "@koa/cors": "3.4.3",
"@koa/multer": "3.0.2", "@koa/multer": "3.0.2",
"@koa/router": "9.0.1", "@koa/router": "9.0.1",
@ -144,6 +144,7 @@
"xev": "3.0.2" "xev": "3.0.2"
}, },
"devDependencies": { "devDependencies": {
"@iceshrimp/ts-json-schema-generator": "^1.0.0",
"@swc/cli": "^0.1.62", "@swc/cli": "^0.1.62",
"@swc/core": "^1.3.68", "@swc/core": "^1.3.68",
"@types/adm-zip": "^0.5.0", "@types/adm-zip": "^0.5.0",
@ -154,6 +155,7 @@
"@types/fluent-ffmpeg": "2.1.21", "@types/fluent-ffmpeg": "2.1.21",
"@types/js-yaml": "4.0.5", "@types/js-yaml": "4.0.5",
"@types/jsdom": "21.1.1", "@types/jsdom": "21.1.1",
"@types/json-schema": "^7.0.15",
"@types/jsonld": "1.5.9", "@types/jsonld": "1.5.9",
"@types/jsrsasign": "10.5.8", "@types/jsrsasign": "10.5.8",
"@types/koa": "2.13.7", "@types/koa": "2.13.7",
@ -197,6 +199,7 @@
"execa": "6.1.0", "execa": "6.1.0",
"json5-loader": "4.0.1", "json5-loader": "4.0.1",
"mocha": "10.2.0", "mocha": "10.2.0",
"openapi-types": "^12.1.3",
"pug": "3.0.2", "pug": "3.0.2",
"strict-event-emitter-types": "2.0.0", "strict-event-emitter-types": "2.0.0",
"swc-loader": "^0.2.3", "swc-loader": "^0.2.3",

View File

@ -1,4 +1,15 @@
import { Controller, Get, Post, Body, CurrentUser, Flow, Description, Returns } from "@iceshrimp/koa-openapi"; import {
Controller,
Get,
Post,
Body,
CurrentUser,
Flow,
Description,
Returns,
Security,
Requests
} from "@iceshrimp/koa-openapi";
import type { ILocalUser } from "@/models/entities/user.js"; import type { ILocalUser } from "@/models/entities/user.js";
import type { AuthRequest, AuthResponse } from "@/server/api/web/entities/auth.js"; import type { AuthRequest, AuthResponse } from "@/server/api/web/entities/auth.js";
import type { Session } from "@/models/entities/session.js"; import type { Session } from "@/models/entities/session.js";
@ -9,8 +20,9 @@ import { AuthHandler } from "@/server/api/web/handlers/auth.js";
@Controller('/auth') @Controller('/auth')
export class AuthController { export class AuthController {
@Get('/') @Get('/')
@Security("user")
@Description("Get the authentication status") @Description("Get the authentication status")
@Returns(200, "Successful response") @Returns(200, "AuthResponse", "Successful response")
async getAuthStatus( async getAuthStatus(
@CurrentUser() me: ILocalUser | null, @CurrentUser() me: ILocalUser | null,
@CurrentSession() session: Session | null, @CurrentSession() session: Session | null,
@ -21,9 +33,10 @@ export class AuthController {
@Post('/') @Post('/')
@Flow([RatelimitRouteMiddleware("auth", 10, 60000, true)]) @Flow([RatelimitRouteMiddleware("auth", 10, 60000, true)])
@Description("Log in as a user and receive a auth token on success") @Description("Log in as a user and receive a auth token on success")
@Returns(200, "Successful response") @Requests("AuthRequest", "application/json")
@Returns(400, "Request body is missing or invalid") @Returns(200, "AuthResponse", "Successful response")
@Returns(401, "Specified username or password are invalid") @Returns(400, "ErrorResponse", "Request body is missing or invalid")
@Returns(401, "ErrorResponse", "Specified username or password are invalid")
async login(@Body({ required: true }) request: AuthRequest): Promise<AuthResponse> { async login(@Body({ required: true }) request: AuthRequest): Promise<AuthResponse> {
return AuthHandler.login(request); return AuthHandler.login(request);
} }

View File

@ -1,4 +1,4 @@
import { Controller, Get, CurrentUser, Params, Description, Returns } from "@iceshrimp/koa-openapi"; import { Controller, Get, CurrentUser, Params, Description, Returns, Security } from "@iceshrimp/koa-openapi";
import type { ILocalUser } from "@/models/entities/user.js"; import type { ILocalUser } from "@/models/entities/user.js";
import { NoteHandler } from "@/server/api/web/handlers/note.js"; import { NoteHandler } from "@/server/api/web/handlers/note.js";
import { NoteResponse } from "@/server/api/web/entities/note.js"; import { NoteResponse } from "@/server/api/web/entities/note.js";
@ -7,9 +7,10 @@ import { notFound } from "@hapi/boom";
@Controller('/note') @Controller('/note')
export class NoteController { export class NoteController {
@Get('/:id') @Get('/:id')
@Security("user")
@Description("Returns the specified note") @Description("Returns the specified note")
@Returns(200, "Successful response") @Returns(200, "NoteResponse", "Successful response")
@Returns(404, "The specified note either doesn't exist or is not visible for the authenticated user (if any)") @Returns(404, "ErrorResponse", "The specified note either doesn't exist or is not visible for the authenticated user (if any)")
async getNote( async getNote(
@CurrentUser() me: ILocalUser | null, @CurrentUser() me: ILocalUser | null,
@Params('id') id: string, @Params('id') id: string,

View File

@ -1,4 +1,14 @@
import { Controller, CurrentUser, Description, Flow, Get, Params, Query, Returns } from "@iceshrimp/koa-openapi"; import {
Controller,
CurrentUser,
Description,
Flow,
Get,
Params,
Query,
Returns,
Security
} from "@iceshrimp/koa-openapi";
import { UserResponse } from "@/server/api/web/entities/user.js"; import { UserResponse } from "@/server/api/web/entities/user.js";
import { TimelineResponse } from "@/server/api/web/entities/note.js"; import { TimelineResponse } from "@/server/api/web/entities/note.js";
import type { ILocalUser } from "@/models/entities/user.js"; import type { ILocalUser } from "@/models/entities/user.js";
@ -10,9 +20,10 @@ import { AuthorizationMiddleware } from "@/server/api/web/middleware/auth.js";
export class TimelineController { export class TimelineController {
@Get('/home') @Get('/home')
@Flow([AuthorizationMiddleware()]) @Flow([AuthorizationMiddleware()])
@Security("user")
@Description("Get the home timeline") @Description("Get the home timeline")
@Returns(200, "Successful response") @Returns(200, "TimelineResponse", "Successful response")
@Returns(401, "Authorization header is missing or invalid") @Returns(401, "ErrorResponse", "Authorization header is missing or invalid")
async getHomeTimeline( async getHomeTimeline(
@CurrentUser() me: ILocalUser, @CurrentUser() me: ILocalUser,
@Query('replies') replies: boolean = true, @Query('replies') replies: boolean = true,

View File

@ -1,4 +1,4 @@
import { Controller, CurrentUser, Description, Get, Params, Query, Returns } from "@iceshrimp/koa-openapi"; import { Controller, CurrentUser, Description, Get, Params, Query, Returns, Security } from "@iceshrimp/koa-openapi";
import { UserResponse } from "@/server/api/web/entities/user.js"; import { UserResponse } from "@/server/api/web/entities/user.js";
import { TimelineResponse } from "@/server/api/web/entities/note.js"; import { TimelineResponse } from "@/server/api/web/entities/note.js";
import type { ILocalUser } from "@/models/entities/user.js"; import type { ILocalUser } from "@/models/entities/user.js";
@ -7,9 +7,10 @@ import { UserHandler } from "@/server/api/web/handlers/user.js";
@Controller('/user') @Controller('/user')
export class UserController { export class UserController {
@Get('/:id') @Get('/:id')
@Security("user")
@Description("Returns information on the specified user") @Description("Returns information on the specified user")
@Returns(200, "Successful response") @Returns(200, "UserResponse", "Successful response")
@Returns(404, "The specified user does not exist") @Returns(404, "ErrorResponse", "The specified user does not exist")
async getUser( async getUser(
@CurrentUser() me: ILocalUser | null, @CurrentUser() me: ILocalUser | null,
@Params('id') id: string, @Params('id') id: string,
@ -19,9 +20,10 @@ export class UserController {
} }
@Get('/:id/notes') @Get('/:id/notes')
@Security("user")
@Description("Get the specified user's notes") @Description("Get the specified user's notes")
@Returns(200, "Successful response") @Returns(200, "TimelineResponse", "Successful response")
@Returns(404, "The specified user does not exist") @Returns(404, "ErrorResponse", "The specified user does not exist")
async getUserNotes( async getUserNotes(
@CurrentUser() me: ILocalUser | null, @CurrentUser() me: ILocalUser | null,
@Params('id') id: string, @Params('id') id: string,

View File

@ -0,0 +1,6 @@
export type ErrorResponse = {
statusCode: number;
error: string;
message: string;
errorDetails: string | null;
}

View File

@ -9,6 +9,8 @@ import { AuthController } from "@/server/api/web/controllers/auth.js";
import { NoteController } from "@/server/api/web/controllers/note.js"; import { NoteController } from "@/server/api/web/controllers/note.js";
import { WebContext, WebRouter } from "@/server/api/web/misc/koa.js"; import { WebContext, WebRouter } from "@/server/api/web/misc/koa.js";
import { TimelineController } from "@/server/api/web/controllers/timeline.js"; import { TimelineController } from "@/server/api/web/controllers/timeline.js";
import { genSchema } from "@/server/api/web/misc/schema.js";
import { OpenAPIV3_1 } from "openapi-types";
export class WebAPI { export class WebAPI {
private readonly router: WebRouter; private readonly router: WebRouter;
@ -18,7 +20,7 @@ export class WebAPI {
} }
public async setup(app: Koa): Promise<void> { public async setup(app: Koa): Promise<void> {
await bootstrapControllers({ await bootstrapControllers({
app: app, app: app,
router: this.router, router: this.router,
attachRoutes: true, attachRoutes: true,
@ -43,13 +45,32 @@ export class WebAPI {
enabled: true, enabled: true,
publicURL: '/api/iceshrimp', publicURL: '/api/iceshrimp',
options: { options: {
title: "Iceshrimp Web API documentation" title: "Iceshrimp Web API documentation",
swaggerOptions: {
urls: [{ url: "/api/iceshrimp/openapi.json", name: "/api/iceshrimp/openapi.json" }],
defaultModelsExpandDepth: "2",
defaultModelExpandDepth: "2",
persistAuthorization: "true",
docExpansion: "none",
},
favicon: '/favicon.ico'
}, },
spec: { spec: {
info: { info: {
title: "Iceshrimp Web API", title: "Iceshrimp Web API",
description: "Documentation for using Iceshrimp's Web API", description: "Documentation for using Iceshrimp's Web API",
version: "1.0.0" version: "1.0.0"
},
},
schemas: genSchema().definitions as Record<string, OpenAPIV3_1.SchemaObject>,
securitySchemes: {
"user": {
type: 'http',
scheme: 'bearer'
},
"admin": {
type: 'http',
scheme: 'bearer'
} }
} }
}, },

View File

@ -0,0 +1,20 @@
import { fileURLToPath } from "node:url";
import { dirname } from "node:path";
import { createGenerator } from "@iceshrimp/ts-json-schema-generator";
export function genSchema() {
const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);
const config = {
path: `${_dirname}/../../../../../src/server/api/web/entities/*.ts`,
tsconfig: `${_dirname}/../../../../../tsconfig.json`,
skipTypeCheck: true,
discriminatorType: 'open-api' as const,
};
const pre = new Date().getTime();
const schema = createGenerator(config).createSchema('*');
console.log(`Generated JSON Schema in ${new Date().getTime() - pre}ms`);
return schema;
}

View File

@ -1204,9 +1204,9 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"@iceshrimp/koa-openapi@npm:^2.7.0": "@iceshrimp/koa-openapi@npm:^3.2.0":
version: 2.7.0 version: 3.2.0
resolution: "@iceshrimp/koa-openapi@npm:2.7.0::__archiveUrl=https%3A%2F%2Ficeshrimp.dev%2Fapi%2Fpackages%2Ficeshrimp%2Fnpm%2F%2540iceshrimp%252Fkoa-openapi%2F-%2F2.7.0%2Fkoa-openapi-2.7.0.tgz" resolution: "@iceshrimp/koa-openapi@npm:3.2.0::__archiveUrl=https%3A%2F%2Ficeshrimp.dev%2Fapi%2Fpackages%2Ficeshrimp%2Fnpm%2F%2540iceshrimp%252Fkoa-openapi%2F-%2F3.2.0%2Fkoa-openapi-3.2.0.tgz"
dependencies: dependencies:
"@hapi/boom": "npm:^10.0.1" "@hapi/boom": "npm:^10.0.1"
"@koa/cors": "npm:^4.0.0" "@koa/cors": "npm:^4.0.0"
@ -1221,7 +1221,24 @@ __metadata:
lodash: "npm:^4.17.21" lodash: "npm:^4.17.21"
openapi-types: "npm:^12.1.3" openapi-types: "npm:^12.1.3"
reflect-metadata: "npm:*" reflect-metadata: "npm:*"
checksum: 21685db4ea05494b885461bddd40312707106cdbf11e68d167136e827fd8b27fc709c0933b81e51432711d7eafc1f47fc03c909d977e38f3a98a8f700d9715ed checksum: 5ec6ea58be8f06c65147232c3fac83a7cac4d8add7b39bfdca6b50c523ca307c13e4d533caebeadf0107a79ad555ebe906a52331900d6abd8fabe04c6f5e5690
languageName: node
linkType: hard
"@iceshrimp/ts-json-schema-generator@npm:^1.0.0":
version: 1.0.1
resolution: "@iceshrimp/ts-json-schema-generator@npm:1.0.1::__archiveUrl=https%3A%2F%2Ficeshrimp.dev%2Fapi%2Fpackages%2Ficeshrimp%2Fnpm%2F%2540iceshrimp%252Fts-json-schema-generator%2F-%2F1.0.1%2Fts-json-schema-generator-1.0.1.tgz"
dependencies:
"@types/json-schema": "npm:^7.0.15"
commander: "npm:^11.1.0"
glob: "npm:^8.0.3"
json5: "npm:^2.2.3"
normalize-path: "npm:^3.0.0"
safe-stable-stringify: "npm:^2.4.3"
typescript: "npm:~5.3.3"
bin:
ts-json-schema-generator: bin/ts-json-schema-generator
checksum: dbac3ba1787a15f45b1b6371e37ba5058cc6cb5b408804806d06e3abdf5c7ee505f9d617b9749289e86667cabae73142d491ebb801a15ff526887d6a2e7b079a
languageName: node languageName: node
linkType: hard linkType: hard
@ -3048,6 +3065,13 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"@types/json-schema@npm:^7.0.15":
version: 7.0.15
resolution: "@types/json-schema@npm:7.0.15"
checksum: 1a3c3e06236e4c4aab89499c428d585527ce50c24fe8259e8b3926d3df4cfbbbcf306cfc73ddfb66cbafc973116efd15967020b0f738f63e09e64c7d260519e7
languageName: node
linkType: hard
"@types/json5@npm:^0.0.29": "@types/json5@npm:^0.0.29":
version: 0.0.29 version: 0.0.29
resolution: "@types/json5@npm:0.0.29" resolution: "@types/json5@npm:0.0.29"
@ -5414,7 +5438,8 @@ __metadata:
"@bull-board/ui": "npm:5.6.0" "@bull-board/ui": "npm:5.6.0"
"@discordapp/twemoji": "npm:14.1.2" "@discordapp/twemoji": "npm:14.1.2"
"@hapi/boom": "npm:^10.0.1" "@hapi/boom": "npm:^10.0.1"
"@iceshrimp/koa-openapi": "npm:^2.7.0" "@iceshrimp/koa-openapi": "npm:^3.2.0"
"@iceshrimp/ts-json-schema-generator": "npm:^1.0.0"
"@koa/cors": "npm:3.4.3" "@koa/cors": "npm:3.4.3"
"@koa/multer": "npm:3.0.2" "@koa/multer": "npm:3.0.2"
"@koa/router": "npm:9.0.1" "@koa/router": "npm:9.0.1"
@ -5435,6 +5460,7 @@ __metadata:
"@types/formidable": "npm:^2.0.5" "@types/formidable": "npm:^2.0.5"
"@types/js-yaml": "npm:4.0.5" "@types/js-yaml": "npm:4.0.5"
"@types/jsdom": "npm:21.1.1" "@types/jsdom": "npm:21.1.1"
"@types/json-schema": "npm:^7.0.15"
"@types/jsonld": "npm:1.5.9" "@types/jsonld": "npm:1.5.9"
"@types/jsrsasign": "npm:10.5.8" "@types/jsrsasign": "npm:10.5.8"
"@types/koa": "npm:2.13.7" "@types/koa": "npm:2.13.7"
@ -5541,6 +5567,7 @@ __metadata:
node-fetch: "npm:3.3.1" node-fetch: "npm:3.3.1"
nodemailer: "npm:6.9.3" nodemailer: "npm:6.9.3"
oauth: "npm:^0.10.0" oauth: "npm:^0.10.0"
openapi-types: "npm:^12.1.3"
os-utils: "npm:0.0.14" os-utils: "npm:0.0.14"
otpauth: "npm:^9.1.3" otpauth: "npm:^9.1.3"
parse-duration: "npm:^1.1.0" parse-duration: "npm:^1.1.0"
@ -7073,6 +7100,13 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"commander@npm:^11.1.0":
version: 11.1.0
resolution: "commander@npm:11.1.0"
checksum: 66bd2d8a0547f6cb1d34022efb25f348e433b0e04ad76a65279b1b09da108f59a4d3001ca539c60a7a46ea38bcf399fc17d91adad76a8cf43845d8dcbaf5cda1
languageName: node
linkType: hard
"commander@npm:^2.20.0, commander@npm:^2.8.1": "commander@npm:^2.20.0, commander@npm:^2.8.1":
version: 2.20.3 version: 2.20.3
resolution: "commander@npm:2.20.3" resolution: "commander@npm:2.20.3"
@ -10704,7 +10738,7 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"glob@npm:^8.1.0": "glob@npm:^8.0.3, glob@npm:^8.1.0":
version: 8.1.0 version: 8.1.0
resolution: "glob@npm:8.1.0" resolution: "glob@npm:8.1.0"
dependencies: dependencies:
@ -13529,7 +13563,7 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"json5@npm:2.2.3, json5@npm:2.x, json5@npm:^2.1.2, json5@npm:^2.1.3, json5@npm:^2.2.2": "json5@npm:2.2.3, json5@npm:2.x, json5@npm:^2.1.2, json5@npm:^2.1.3, json5@npm:^2.2.2, json5@npm:^2.2.3":
version: 2.2.3 version: 2.2.3
resolution: "json5@npm:2.2.3" resolution: "json5@npm:2.2.3"
bin: bin:
@ -18560,6 +18594,13 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"safe-stable-stringify@npm:^2.4.3":
version: 2.4.3
resolution: "safe-stable-stringify@npm:2.4.3"
checksum: a6c192bbefe47770a11072b51b500ed29be7b1c15095371c1ee1dc13e45ce48ee3c80330214c56764d006c485b88bd0b24940d868948170dddc16eed312582d8
languageName: node
linkType: hard
"safer-buffer@npm:>= 2.1.2 < 3, safer-buffer@npm:>= 2.1.2 < 3.0.0, safer-buffer@npm:^2.0.2, safer-buffer@npm:^2.1.0, safer-buffer@npm:^2.1.2, safer-buffer@npm:~2.1.0": "safer-buffer@npm:>= 2.1.2 < 3, safer-buffer@npm:>= 2.1.2 < 3.0.0, safer-buffer@npm:^2.0.2, safer-buffer@npm:^2.1.0, safer-buffer@npm:^2.1.2, safer-buffer@npm:~2.1.0":
version: 2.1.2 version: 2.1.2
resolution: "safer-buffer@npm:2.1.2" resolution: "safer-buffer@npm:2.1.2"
@ -20768,7 +20809,7 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"typescript@npm:^5.1.6, typescript@npm:^5.2.2": "typescript@npm:^5.1.6, typescript@npm:^5.2.2, typescript@npm:~5.3.3":
version: 5.3.3 version: 5.3.3
resolution: "typescript@npm:5.3.3" resolution: "typescript@npm:5.3.3"
bin: bin:
@ -20788,7 +20829,7 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"typescript@patch:typescript@npm%3A^5.1.6#optional!builtin<compat/typescript>, typescript@patch:typescript@npm%3A^5.2.2#optional!builtin<compat/typescript>": "typescript@patch:typescript@npm%3A^5.1.6#optional!builtin<compat/typescript>, typescript@patch:typescript@npm%3A^5.2.2#optional!builtin<compat/typescript>, typescript@patch:typescript@npm%3A~5.3.3#optional!builtin<compat/typescript>":
version: 5.3.3 version: 5.3.3
resolution: "typescript@patch:typescript@npm%3A5.3.3#optional!builtin<compat/typescript>::version=5.3.3&hash=e012d7" resolution: "typescript@patch:typescript@npm%3A5.3.3#optional!builtin<compat/typescript>::version=5.3.3&hash=e012d7"
bin: bin: