1
0

build-openapi.mjs 6.3 KB

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