build-openapi.mjs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #!/usr/bin/env node
  2. import { writeFileSync } from 'node:fs';
  3. import { join, dirname } from 'node:path';
  4. import { fileURLToPath, pathToFileURL } from 'node:url';
  5. import { sections } from '../src/pages/api-docs/endpoints.ts';
  6. import { EXAMPLES } from '../src/generated/examples.ts';
  7. import { SCHEMAS } from '../src/generated/schemas.ts';
  8. const __dirname = dirname(fileURLToPath(import.meta.url));
  9. const outPath = join(__dirname, '..', 'public', 'openapi.json');
  10. const PANEL_VERSION = process.env.X_UI_VERSION || '3.x';
  11. const SECURITY_SCHEMES = {
  12. bearerAuth: {
  13. type: 'http',
  14. scheme: 'bearer',
  15. description: 'API token from Settings → Security → API Token. Send as `Authorization: Bearer <token>`.',
  16. },
  17. cookieAuth: {
  18. type: 'apiKey',
  19. in: 'cookie',
  20. name: '3x-ui',
  21. description: 'Session cookie set by POST /login. Browser-only.',
  22. },
  23. };
  24. function ginPathToOpenApi(path) {
  25. return path.replace(/:([A-Za-z_][A-Za-z0-9_]*)/g, '{$1}');
  26. }
  27. function extractPathParams(openApiPath) {
  28. const params = [];
  29. const re = /\{([A-Za-z_][A-Za-z0-9_]*)\}/g;
  30. let m;
  31. while ((m = re.exec(openApiPath)) !== null) params.push(m[1]);
  32. return params;
  33. }
  34. function mapType(t) {
  35. const v = String(t || '').toLowerCase();
  36. if (v === 'number' || v === 'integer' || v === 'int') return 'integer';
  37. if (v === 'float' || v === 'double') return 'number';
  38. if (v === 'boolean' || v === 'bool') return 'boolean';
  39. if (v === 'array') return 'array';
  40. if (v === 'object') return 'object';
  41. return 'string';
  42. }
  43. function tryParseJson(raw) {
  44. if (typeof raw !== 'string') return undefined;
  45. try {
  46. return JSON.parse(raw);
  47. } catch {
  48. return undefined;
  49. }
  50. }
  51. function paramToOpenApi(p) {
  52. const out = {
  53. name: p.name,
  54. in: p.in,
  55. required: p.in === 'path' ? true : !p.optional,
  56. description: p.desc || '',
  57. schema: { type: mapType(p.type) },
  58. };
  59. if (p.defaultValue !== undefined) out.schema.default = p.defaultValue;
  60. return out;
  61. }
  62. function buildOperation(ep, tag) {
  63. const op = {
  64. tags: [tag],
  65. summary: ep.summary || '',
  66. operationId: `${ep.method.toLowerCase()}_${ep.path.replace(/[^A-Za-z0-9]+/g, '_').replace(/^_|_$/g, '')}`,
  67. };
  68. if (ep.description) op.description = ep.description;
  69. if (ep.deprecated) op.deprecated = true;
  70. const params = [];
  71. const bodyParams = [];
  72. for (const p of ep.params || []) {
  73. if (p.in === 'body') {
  74. bodyParams.push(p);
  75. } else if (p.in === 'path' || p.in === 'query' || p.in === 'header') {
  76. params.push(paramToOpenApi(p));
  77. }
  78. }
  79. const openApiPath = ginPathToOpenApi(ep.path);
  80. const declared = new Set(params.filter((x) => x.in === 'path').map((x) => x.name));
  81. for (const name of extractPathParams(openApiPath)) {
  82. if (declared.has(name)) continue;
  83. params.push({
  84. name,
  85. in: 'path',
  86. required: true,
  87. description: '',
  88. schema: { type: 'string' },
  89. });
  90. }
  91. if (params.length > 0) op.parameters = params;
  92. if (ep.body || bodyParams.length > 0) {
  93. const example = tryParseJson(ep.body);
  94. const properties = {};
  95. const required = [];
  96. for (const bp of bodyParams) {
  97. properties[bp.name] = {
  98. type: mapType(bp.type),
  99. description: bp.desc || '',
  100. };
  101. if (!bp.optional) required.push(bp.name);
  102. }
  103. const schema = bodyParams.length > 0
  104. ? { type: 'object', properties, ...(required.length > 0 ? { required } : {}) }
  105. : { type: 'object' };
  106. op.requestBody = {
  107. required: required.length > 0 || bodyParams.length === 0,
  108. content: {
  109. 'application/json': {
  110. schema,
  111. ...(example !== undefined ? { example } : {}),
  112. },
  113. },
  114. };
  115. }
  116. const responses = {};
  117. let successExample = tryParseJson(ep.response);
  118. let objSchema = {};
  119. if (ep.responseSchema) {
  120. const obj = EXAMPLES[ep.responseSchema];
  121. if (obj === undefined) {
  122. throw new Error(`${ep.method} ${ep.path}: responseSchema "${ep.responseSchema}" has no generated example`);
  123. }
  124. if (SCHEMAS[ep.responseSchema] === undefined) {
  125. throw new Error(`${ep.method} ${ep.path}: responseSchema "${ep.responseSchema}" has no generated schema`);
  126. }
  127. const ref = { $ref: `#/components/schemas/${ep.responseSchema}` };
  128. objSchema = ep.responseSchemaArray ? { type: 'array', items: ref } : ref;
  129. if (successExample === undefined) {
  130. successExample = { success: true, obj: ep.responseSchemaArray ? [obj] : obj };
  131. }
  132. }
  133. responses['200'] = {
  134. description: 'Successful response',
  135. content: {
  136. 'application/json': {
  137. schema: {
  138. type: 'object',
  139. properties: {
  140. success: { type: 'boolean' },
  141. msg: { type: 'string' },
  142. obj: objSchema,
  143. },
  144. },
  145. ...(successExample !== undefined ? { example: successExample } : {}),
  146. },
  147. },
  148. };
  149. const errExample = tryParseJson(ep.errorResponse);
  150. if (errExample !== undefined || ep.errorStatus) {
  151. const code = String(ep.errorStatus || 400);
  152. responses[code] = {
  153. description: 'Error response',
  154. content: {
  155. 'application/json': {
  156. schema: {
  157. type: 'object',
  158. properties: {
  159. success: { type: 'boolean' },
  160. msg: { type: 'string' },
  161. },
  162. },
  163. ...(errExample !== undefined ? { example: errExample } : {}),
  164. },
  165. },
  166. };
  167. }
  168. op.responses = responses;
  169. return op;
  170. }
  171. function buildSpec() {
  172. const paths = {};
  173. for (const section of sections) {
  174. const tag = section.title;
  175. for (const ep of section.endpoints) {
  176. const openApiPath = ginPathToOpenApi(ep.path);
  177. if (!paths[openApiPath]) paths[openApiPath] = {};
  178. paths[openApiPath][ep.method.toLowerCase()] = buildOperation(ep, tag);
  179. }
  180. }
  181. const tags = sections.map((s) => ({
  182. name: s.title,
  183. description: s.description || '',
  184. }));
  185. return {
  186. openapi: '3.0.3',
  187. info: {
  188. title: '3X-UI Panel API',
  189. version: PANEL_VERSION,
  190. description:
  191. 'Programmatic interface to a 3X-UI panel. Authenticate either by logging in (cookie) or with an API token from Settings → Security → API Token (Bearer). All endpoints under /panel/api/* honour both modes — an API token is a full-admin credential, so treat it like the panel password.',
  192. },
  193. servers: [
  194. { url: '/', description: 'Current panel (basePath aware)' },
  195. ],
  196. components: {
  197. securitySchemes: SECURITY_SCHEMES,
  198. schemas: SCHEMAS,
  199. },
  200. security: [{ bearerAuth: [] }, { cookieAuth: [] }],
  201. tags,
  202. paths,
  203. };
  204. }
  205. const spec = buildSpec();
  206. writeFileSync(outPath, JSON.stringify(spec, null, 2) + '\n');
  207. const pathCount = Object.keys(spec.paths).length;
  208. let opCount = 0;
  209. for (const ops of Object.values(spec.paths)) opCount += Object.keys(ops).length;
  210. console.log(`[openapi] wrote ${outPath}`);
  211. console.log(`[openapi] paths: ${pathCount}, operations: ${opCount}, tags: ${spec.tags.length}`);
  212. void pathToFileURL;