From 7f56d9b2c664bea68251795ae2b8349177186aa2 Mon Sep 17 00:00:00 2001 From: Kevin Cador Date: Thu, 12 Mar 2026 17:11:43 +0100 Subject: [PATCH] feat(oauth2): add support for oauth2 (all routes but /oauth) that should enable documentation "try" feature --- projects/openapi/generate.ts | 70 ++++++++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 19 deletions(-) diff --git a/projects/openapi/generate.ts b/projects/openapi/generate.ts index 973ba142..07bd4698 100644 --- a/projects/openapi/generate.ts +++ b/projects/openapi/generate.ts @@ -2,28 +2,60 @@ import { traktContract } from '@trakt/api'; import { generateOpenApi } from './generateOpenApi.ts'; export function generate(): ReturnType { - return generateOpenApi(traktContract, { - info: { - title: 'Trakt API', - version: '2.0.0', + return generateOpenApi( + traktContract, + { + info: { + title: 'Trakt API', + version: '2.0.0', + }, + components: { + securitySchemes: { + traktOAuth: { + type: 'oauth2', + flows: { + authorizationCode: { + authorizationUrl: 'https://api.trakt.tv/oauth/authorize', + tokenUrl: 'https://api.trakt.tv/oauth/token', + scopes: { + public: 'Access public data', + }, + }, + }, + }, + }, + }, }, - }, { - operationMapper: (operation, route, id) => { - const parts = [ - ...(operation.tags ?? []), - id, - ]; + { + operationMapper: (operation, route, id) => { + const parts = [ + ...(operation.tags ?? []), + id, + ]; - return { - ...operation, - operationId: `${route.method.toLowerCase()}${ - parts - .map((part) => part.charAt(0).toUpperCase() + part.slice(1)) - .join('') - }`, - }; + const isOAuthRoute = (operation.tags ?? []).includes('oauth'); + + const security = isOAuthRoute + ? [] + : [ + { + traktOAuth: [], + }, + {}, + ]; + + return { + ...operation, + security, + operationId: `${route.method.toLowerCase()}${ + parts + .map((part) => part.charAt(0).toUpperCase() + part.slice(1)) + .join('') + }`, + }; + }, }, - }); + ); } if (import.meta.main) {