refactor docMiddleware to support operationId-only form

This commit is contained in:
Rigel Kent 2021-06-04 08:57:07 +02:00
parent b96d21b744
commit 1c627fd8d2
No known key found for this signature in database
GPG Key ID: 5E53E96A494E452F
4 changed files with 15 additions and 12 deletions

View File

@ -2,7 +2,7 @@ import * as express from 'express'
import toInt from 'validator/lib/toInt' import toInt from 'validator/lib/toInt'
import { doJSONRequest } from '@server/helpers/requests' import { doJSONRequest } from '@server/helpers/requests'
import { LiveManager } from '@server/lib/live-manager' import { LiveManager } from '@server/lib/live-manager'
import { docMiddleware } from '@server/middlewares/doc' import { openapiOperationDoc } from '@server/middlewares/doc'
import { getServerActor } from '@server/models/application/application' import { getServerActor } from '@server/models/application/application'
import { MVideoAccountLight } from '@server/types/models' import { MVideoAccountLight } from '@server/types/models'
import { VideosCommonQuery } from '../../../../shared' import { VideosCommonQuery } from '../../../../shared'
@ -84,7 +84,7 @@ videosRouter.get('/:id/metadata/:videoFileId',
asyncMiddleware(getVideoFileMetadata) asyncMiddleware(getVideoFileMetadata)
) )
videosRouter.get('/:id', videosRouter.get('/:id',
docMiddleware('https://docs.joinpeertube.org/api-rest-reference.html#operation/getVideo'), openapiOperationDoc({ operationId: 'getVideo' }),
optionalAuthenticate, optionalAuthenticate,
asyncMiddleware(videosCustomGetValidator('only-video-with-rights')), asyncMiddleware(videosCustomGetValidator('only-video-with-rights')),
asyncMiddleware(checkVideoFollowConstraints), asyncMiddleware(checkVideoFollowConstraints),
@ -96,7 +96,7 @@ videosRouter.post('/:id/views',
) )
videosRouter.delete('/:id', videosRouter.delete('/:id',
docMiddleware('https://docs.joinpeertube.org/api-rest-reference.html#operation/delVideo'), openapiOperationDoc({ operationId: 'delVideo' }),
authenticate, authenticate,
asyncMiddleware(videosRemoveValidator), asyncMiddleware(videosRemoveValidator),
asyncRetryTransactionMiddleware(removeVideo) asyncRetryTransactionMiddleware(removeVideo)

View File

@ -20,7 +20,7 @@ import { autoBlacklistVideoIfNeeded } from '../../../lib/video-blacklist'
import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videosUpdateValidator } from '../../../middlewares' import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videosUpdateValidator } from '../../../middlewares'
import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update' import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update'
import { VideoModel } from '../../../models/video/video' import { VideoModel } from '../../../models/video/video'
import { docMiddleware } from '@server/middlewares/doc' import { openapiOperationDoc } from '@server/middlewares/doc'
const lTags = loggerTagsFactory('api', 'video') const lTags = loggerTagsFactory('api', 'video')
const auditLogger = auditLoggerFactory('videos') const auditLogger = auditLoggerFactory('videos')
@ -36,7 +36,7 @@ const reqVideoFileUpdate = createReqFiles(
) )
updateRouter.put('/:id', updateRouter.put('/:id',
docMiddleware('https://docs.joinpeertube.org/api-rest-reference.html#operation/putVideo'), openapiOperationDoc({ operationId: 'putVideo' }),
authenticate, authenticate,
reqVideoFileUpdate, reqVideoFileUpdate,
asyncMiddleware(videosUpdateValidator), asyncMiddleware(videosUpdateValidator),

View File

@ -6,7 +6,7 @@ import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url' import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url'
import { addOptimizeOrMergeAudioJob, buildLocalVideoFromReq, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video' import { addOptimizeOrMergeAudioJob, buildLocalVideoFromReq, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video'
import { generateVideoFilename, getVideoFilePath } from '@server/lib/video-paths' import { generateVideoFilename, getVideoFilePath } from '@server/lib/video-paths'
import { docMiddleware } from '@server/middlewares/doc' import { openapiOperationDoc } from '@server/middlewares/doc'
import { MVideo, MVideoFile, MVideoFullLight } from '@server/types/models' import { MVideo, MVideoFile, MVideoFullLight } from '@server/types/models'
import { uploadx } from '@uploadx/core' import { uploadx } from '@uploadx/core'
import { VideoCreate, VideoState } from '../../../../shared' import { VideoCreate, VideoState } from '../../../../shared'
@ -61,7 +61,7 @@ const reqVideoFileAddResumable = createReqFiles(
) )
uploadRouter.post('/upload', uploadRouter.post('/upload',
docMiddleware('https://docs.joinpeertube.org/api-rest-reference.html#operation/uploadLegacy'), openapiOperationDoc({ operationId: 'uploadLegacy' }),
authenticate, authenticate,
reqVideoFileAdd, reqVideoFileAdd,
asyncMiddleware(videosAddLegacyValidator), asyncMiddleware(videosAddLegacyValidator),
@ -69,7 +69,7 @@ uploadRouter.post('/upload',
) )
uploadRouter.post('/upload-resumable', uploadRouter.post('/upload-resumable',
docMiddleware('https://docs.joinpeertube.org/api-rest-reference.html#operation/uploadResumableInit'), openapiOperationDoc({ operationId: 'uploadResumableInit' }),
authenticate, authenticate,
reqVideoFileAddResumable, reqVideoFileAddResumable,
asyncMiddleware(videosAddResumableInitValidator), asyncMiddleware(videosAddResumableInitValidator),
@ -82,7 +82,7 @@ uploadRouter.delete('/upload-resumable',
) )
uploadRouter.put('/upload-resumable', uploadRouter.put('/upload-resumable',
docMiddleware('https://docs.joinpeertube.org/api-rest-reference.html#operation/uploadResumable'), openapiOperationDoc({ operationId: 'uploadResumable' }),
authenticate, authenticate,
uploadxMiddleware, // uploadx doesn't use call next() before the file upload completes uploadxMiddleware, // uploadx doesn't use call next() before the file upload completes
asyncMiddleware(videosAddResumableValidator), asyncMiddleware(videosAddResumableValidator),

View File

@ -1,13 +1,16 @@
import * as express from 'express' import * as express from 'express'
function docMiddleware (docUrl: string) { function openapiOperationDoc (options: {
url?: string
operationId?: string
}) {
return (req: express.Request, res: express.Response, next: express.NextFunction) => { return (req: express.Request, res: express.Response, next: express.NextFunction) => {
res.locals.docUrl = docUrl res.locals.docUrl = options.url || 'https://docs.joinpeertube.org/api-rest-reference.html#operation/' + options.operationId
if (next) return next() if (next) return next()
} }
} }
export { export {
docMiddleware openapiOperationDoc
} }