index.cjs 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
  7. var __export = (target, all) => {
  8. for (var name in all)
  9. __defProp(target, name, { get: all[name], enumerable: true });
  10. };
  11. var __copyProps = (to, from, except, desc) => {
  12. if (from && typeof from === "object" || typeof from === "function") {
  13. for (let key of __getOwnPropNames(from))
  14. if (!__hasOwnProp.call(to, key) && key !== except)
  15. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  16. }
  17. return to;
  18. };
  19. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  20. // src/index.js
  21. var index_exports = {};
  22. __export(index_exports, {
  23. OAApiKeyLocation: () => OAApiKeyLocation,
  24. OAComponentRegistry: () => OAComponentRegistry,
  25. OAComponentType: () => OAComponentType,
  26. OADataFormat: () => OADataFormat,
  27. OADataType: () => OADataType,
  28. OADocumentBuilder: () => OADocumentBuilder,
  29. OADocumentScope: () => OADocumentScope,
  30. OAMediaType: () => OAMediaType,
  31. OAOperationMethod: () => OAOperationMethod,
  32. OAParameterLocation: () => OAParameterLocation,
  33. OAParameterStyle: () => OAParameterStyle,
  34. OASecuritySchemeType: () => OASecuritySchemeType,
  35. OA_COMPONENT_TYPE_TO_COMPONENTS_KEY_MAP: () => OA_COMPONENT_TYPE_TO_COMPONENTS_KEY_MAP,
  36. OPENAPI_VERSION: () => OPENAPI_VERSION,
  37. oaCallbackRef: () => oaCallbackRef,
  38. oaExampleRef: () => oaExampleRef,
  39. oaLinkRef: () => oaLinkRef,
  40. oaParameterRef: () => oaParameterRef,
  41. oaPathItemRef: () => oaPathItemRef,
  42. oaRequestBodyRef: () => oaRequestBodyRef,
  43. oaResponseRef: () => oaResponseRef,
  44. oaSchemaRef: () => oaSchemaRef,
  45. oaSecuritySchemeRef: () => oaSecuritySchemeRef
  46. });
  47. module.exports = __toCommonJS(index_exports);
  48. // src/oa-document-scope.js
  49. var import_js_format5 = require("@e22m4u/js-format");
  50. // src/utils/oa-ref.js
  51. var import_js_format2 = require("@e22m4u/js-format");
  52. // src/oa-component-registry.js
  53. var import_js_service = require("@e22m4u/js-service");
  54. var import_js_format = require("@e22m4u/js-format");
  55. var OAComponentType = {
  56. SCHEMA: "schema",
  57. RESPONSE: "response",
  58. PARAMETER: "parameter",
  59. EXAMPLE: "example",
  60. REQUEST_BODY: "requestBody",
  61. HEADER: "header",
  62. SECURITY_SCHEME: "securityScheme",
  63. LINK: "link",
  64. CALLBACK: "callback",
  65. PATH_ITEM: "pathItem"
  66. };
  67. var OA_COMPONENT_TYPE_TO_COMPONENTS_KEY_MAP = {
  68. [OAComponentType.SCHEMA]: "schemas",
  69. [OAComponentType.RESPONSE]: "responses",
  70. [OAComponentType.PARAMETER]: "parameters",
  71. [OAComponentType.EXAMPLE]: "examples",
  72. [OAComponentType.REQUEST_BODY]: "requestBodies",
  73. [OAComponentType.HEADER]: "headers",
  74. [OAComponentType.SECURITY_SCHEME]: "securitySchemes",
  75. [OAComponentType.LINK]: "links",
  76. [OAComponentType.CALLBACK]: "callbacks",
  77. [OAComponentType.PATH_ITEM]: "pathItems"
  78. };
  79. var OAComponentRegistry = class extends import_js_service.Service {
  80. static {
  81. __name(this, "OAComponentRegistry");
  82. }
  83. /**
  84. * Components.
  85. */
  86. _components = {
  87. schemas: {},
  88. responses: {},
  89. parameters: {},
  90. examples: {},
  91. requestBodies: {},
  92. headers: {},
  93. securitySchemes: {},
  94. links: {},
  95. callbacks: {},
  96. pathItems: {}
  97. };
  98. /**
  99. * Get components object.
  100. *
  101. * @returns {object}
  102. */
  103. getComponentsObject() {
  104. return structuredClone(this._components);
  105. }
  106. /**
  107. * Define component.
  108. *
  109. * @protected
  110. * @param {string} type
  111. * @param {object} definition
  112. * @returns {this}
  113. */
  114. _defineComponent(type, definition) {
  115. if (!Object.values(OAComponentType).includes(type)) {
  116. throw new import_js_format.InvalidArgumentError(
  117. "Components type %v is not supported.",
  118. type
  119. );
  120. }
  121. if (!definition || typeof definition !== "object" || Array.isArray(definition)) {
  122. throw new import_js_format.InvalidArgumentError(
  123. "Component definition must be an Object, but %v was given.",
  124. definition
  125. );
  126. }
  127. if (!definition.name || typeof definition.name !== "string") {
  128. throw new import_js_format.InvalidArgumentError(
  129. 'Property "name" must be a non-empty String, but %v was given.',
  130. definition.name
  131. );
  132. }
  133. const component = definition[type];
  134. if (!component || typeof component !== "object" || Array.isArray(component)) {
  135. throw new import_js_format.InvalidArgumentError(
  136. "Property %v must be an Object, but %v was given.",
  137. type,
  138. component
  139. );
  140. }
  141. const key = OA_COMPONENT_TYPE_TO_COMPONENTS_KEY_MAP[type];
  142. if (!key) {
  143. throw new import_js_format.InvalidArgumentError(
  144. "Component type %v does not have a reference to the components key.",
  145. type
  146. );
  147. }
  148. this._components[key] = structuredClone(component);
  149. return this;
  150. }
  151. /**
  152. * Has component.
  153. *
  154. * @protected
  155. * @param {string} type
  156. * @param {string} name
  157. * @returns {boolean}
  158. */
  159. _hasComponent(type, name) {
  160. if (!name || typeof name !== "string") {
  161. throw new import_js_format.InvalidArgumentError(
  162. 'Parameter "name" must be a non-empty String, but %v was given.',
  163. name
  164. );
  165. }
  166. const key = OA_COMPONENT_TYPE_TO_COMPONENTS_KEY_MAP[type];
  167. if (!key) {
  168. throw new import_js_format.InvalidArgumentError(
  169. "Component type %v does not have a reference to the components key.",
  170. type
  171. );
  172. }
  173. return Boolean(this._components[key][name]);
  174. }
  175. /**
  176. * Get component.
  177. *
  178. * @param {string} type
  179. * @param {string} name
  180. * @returns {object}
  181. */
  182. _getComponent(type, name) {
  183. if (!name || typeof name !== "string") {
  184. throw new import_js_format.InvalidArgumentError(
  185. 'Parameter "name" must be a non-empty String, but %v was given.',
  186. name
  187. );
  188. }
  189. const key = OA_COMPONENT_TYPE_TO_COMPONENTS_KEY_MAP[type];
  190. if (!key) {
  191. throw new import_js_format.InvalidArgumentError(
  192. "Component type %v does not have a reference to the components key.",
  193. type
  194. );
  195. }
  196. const component = this._components[key][name];
  197. if (!component) {
  198. throw new import_js_format.InvalidArgumentError(
  199. 'Component "#/components/%s/%s" does not exist.',
  200. key,
  201. name
  202. );
  203. }
  204. return structuredClone(component);
  205. }
  206. /**
  207. * Define schema.
  208. *
  209. * @param {object} schemaDef
  210. * @returns {this}
  211. */
  212. defineSchema(schemaDef) {
  213. return this._defineComponent(OAComponentType.SCHEMA, schemaDef);
  214. }
  215. /**
  216. * Has schema.
  217. *
  218. * @param {string} name
  219. * @returns {boolean}
  220. */
  221. hasSchema(name) {
  222. return this._hasComponent(OAComponentType.SCHEMA, name);
  223. }
  224. /**
  225. * Get schema.
  226. *
  227. * @param {string} name
  228. * @returns {object}
  229. */
  230. getSchema(name) {
  231. return this._getComponent(OAComponentType.SCHEMA, name);
  232. }
  233. /**
  234. * Define response.
  235. *
  236. * @param {object} responseDef
  237. * @returns {this}
  238. */
  239. defineResponse(responseDef) {
  240. return this._defineComponent(OAComponentType.RESPONSE, responseDef);
  241. }
  242. /**
  243. * Has response.
  244. *
  245. * @param {string} name
  246. * @returns {boolean}
  247. */
  248. hasResponse(name) {
  249. return this._hasComponent(OAComponentType.RESPONSE, name);
  250. }
  251. /**
  252. * Get response.
  253. *
  254. * @param {string} name
  255. * @returns {object}
  256. */
  257. getResponse(name) {
  258. return this._getComponent(OAComponentType.RESPONSE, name);
  259. }
  260. /**
  261. * Define parameter.
  262. *
  263. * @param {object} parameterDef
  264. * @returns {this}
  265. */
  266. defineParameter(parameterDef) {
  267. return this._defineComponent(OAComponentType.PARAMETER, parameterDef);
  268. }
  269. /**
  270. * Has parameter.
  271. *
  272. * @param {string} name
  273. * @returns {boolean}
  274. */
  275. hasParameter(name) {
  276. return this._hasComponent(OAComponentType.PARAMETER, name);
  277. }
  278. /**
  279. * Get parameter.
  280. *
  281. * @param {string} name
  282. * @returns {object}
  283. */
  284. getParameter(name) {
  285. return this._getComponent(OAComponentType.PARAMETER, name);
  286. }
  287. /**
  288. * Define example.
  289. *
  290. * @param {object} exampleDef
  291. * @returns {this}
  292. */
  293. defineExample(exampleDef) {
  294. return this._defineComponent(OAComponentType.EXAMPLE, exampleDef);
  295. }
  296. /**
  297. * Has example.
  298. *
  299. * @param {string} name
  300. * @returns {boolean}
  301. */
  302. hasExample(name) {
  303. return this._hasComponent(OAComponentType.EXAMPLE, name);
  304. }
  305. /**
  306. * Get example.
  307. *
  308. * @param {string} name
  309. * @returns {object}
  310. */
  311. getExample(name) {
  312. return this._getComponent(OAComponentType.EXAMPLE, name);
  313. }
  314. /**
  315. * Define request body.
  316. *
  317. * @param {object} requestBodyDef
  318. * @returns {this}
  319. */
  320. defineRequestBody(requestBodyDef) {
  321. return this._defineComponent(OAComponentType.REQUEST_BODY, requestBodyDef);
  322. }
  323. /**
  324. * Has request body.
  325. *
  326. * @param {string} name
  327. * @returns {boolean}
  328. */
  329. hasRequestBody(name) {
  330. return this._hasComponent(OAComponentType.REQUEST_BODY, name);
  331. }
  332. /**
  333. * Get request body.
  334. *
  335. * @param {string} name
  336. * @returns {object}
  337. */
  338. getRequestBody(name) {
  339. return this._getComponent(OAComponentType.REQUEST_BODY, name);
  340. }
  341. /**
  342. * Define header.
  343. *
  344. * @param {object} headerDef
  345. * @returns {this}
  346. */
  347. defineHeader(headerDef) {
  348. return this._defineComponent(OAComponentType.HEADER, headerDef);
  349. }
  350. /**
  351. * Has header.
  352. *
  353. * @param {string} name
  354. * @returns {boolean}
  355. */
  356. hasHeader(name) {
  357. return this._hasComponent(OAComponentType.HEADER, name);
  358. }
  359. /**
  360. * Get header.
  361. *
  362. * @param {string} name
  363. * @returns {object}
  364. */
  365. getHeader(name) {
  366. return this._getComponent(OAComponentType.HEADER, name);
  367. }
  368. /**
  369. * Define security scheme.
  370. *
  371. * @param {object} securitySchemeDef
  372. * @returns {this}
  373. */
  374. defineSecurityScheme(securitySchemeDef) {
  375. return this._defineComponent(
  376. OAComponentType.SECURITY_SCHEME,
  377. securitySchemeDef
  378. );
  379. }
  380. /**
  381. * Has security scheme.
  382. *
  383. * @param {string} name
  384. * @returns {boolean}
  385. */
  386. hasSecurityScheme(name) {
  387. return this._hasComponent(OAComponentType.SECURITY_SCHEME, name);
  388. }
  389. /**
  390. * Get security scheme.
  391. *
  392. * @param {string} name
  393. * @returns {object}
  394. */
  395. getSecurityScheme(name) {
  396. return this._getComponent(OAComponentType.SECURITY_SCHEME, name);
  397. }
  398. /**
  399. * Define link.
  400. *
  401. * @param {object} linkDef
  402. * @returns {this}
  403. */
  404. defineLink(linkDef) {
  405. return this._defineComponent(OAComponentType.LINK, linkDef);
  406. }
  407. /**
  408. * Has link.
  409. *
  410. * @param {string} name
  411. * @returns {boolean}
  412. */
  413. hasLink(name) {
  414. return this._hasComponent(OAComponentType.LINK, name);
  415. }
  416. /**
  417. * Get link.
  418. *
  419. * @param {string} name
  420. * @returns {object}
  421. */
  422. getLink(name) {
  423. return this._getComponent(OAComponentType.LINK, name);
  424. }
  425. /**
  426. * Define callback.
  427. *
  428. * @param {object} callbackDef
  429. * @returns {this}
  430. */
  431. defineCallback(callbackDef) {
  432. return this._defineComponent(OAComponentType.CALLBACK, callbackDef);
  433. }
  434. /**
  435. * Has callback.
  436. *
  437. * @param {string} name
  438. * @returns {boolean}
  439. */
  440. hasCallback(name) {
  441. return this._hasComponent(OAComponentType.CALLBACK, name);
  442. }
  443. /**
  444. * Get callback.
  445. *
  446. * @param {string} name
  447. * @returns {object}
  448. */
  449. getCallback(name) {
  450. return this._getComponent(OAComponentType.CALLBACK, name);
  451. }
  452. /**
  453. * Define path item.
  454. *
  455. * @param {object} pathItemDef
  456. * @returns {this}
  457. */
  458. definePathItem(pathItemDef) {
  459. return this._defineComponent(OAComponentType.PATH_ITEM, pathItemDef);
  460. }
  461. /**
  462. * Has path item.
  463. *
  464. * @param {string} name
  465. * @returns {boolean}
  466. */
  467. hasPathItem(name) {
  468. return this._hasComponent(OAComponentType.PATH_ITEM, name);
  469. }
  470. /**
  471. * Get path item.
  472. *
  473. * @param {string} name
  474. * @returns {object}
  475. */
  476. getPathItem(name) {
  477. return this._getComponent(OAComponentType.PATH_ITEM, name);
  478. }
  479. };
  480. // src/utils/oa-ref.js
  481. function oaRef(name, type) {
  482. if (!name || typeof name !== "string") {
  483. throw new import_js_format2.InvalidArgumentError(
  484. 'Parameter "name" must be a non-empty String, but %v was given.',
  485. name
  486. );
  487. }
  488. if (!Object.values(OAComponentType).includes(type)) {
  489. throw new import_js_format2.InvalidArgumentError("Component type %v is not supported.", type);
  490. }
  491. const segment = OA_COMPONENT_TYPE_TO_COMPONENTS_KEY_MAP[type];
  492. return { $ref: `#/components/${segment}/${name}` };
  493. }
  494. __name(oaRef, "oaRef");
  495. var oaSchemaRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentType.SCHEMA), "oaSchemaRef");
  496. var oaResponseRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentType.RESPONSE), "oaResponseRef");
  497. var oaParameterRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentType.PARAMETER), "oaParameterRef");
  498. var oaExampleRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentType.EXAMPLE), "oaExampleRef");
  499. var oaRequestBodyRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentType.REQUEST_BODY), "oaRequestBodyRef");
  500. var oaSecuritySchemeRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentType.SECURITY_SCHEME), "oaSecuritySchemeRef");
  501. var oaLinkRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentType.LINK), "oaLinkRef");
  502. var oaCallbackRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentType.CALLBACK), "oaCallbackRef");
  503. var oaPathItemRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentType.PATH_ITEM), "oaPathItemRef");
  504. // src/utils/join-path.js
  505. function joinPath(...segments) {
  506. const path = segments.filter((seg) => seg != void 0).map((seg) => String(seg).replace(/(^\/+|\/+$)/g, "")).filter(Boolean).join("/");
  507. return ("/" + path).replace(/\/+/g, "/");
  508. }
  509. __name(joinPath, "joinPath");
  510. // src/utils/normalize-path.js
  511. function normalizePath(value, noStartingSlash = false) {
  512. if (typeof value !== "string") {
  513. return "/";
  514. }
  515. const res = value.trim().replace(/\/+/g, "/").replace(/(^\/|\/$)/g, "");
  516. return noStartingSlash ? res : "/" + res;
  517. }
  518. __name(normalizePath, "normalizePath");
  519. // src/oa-document-builder.js
  520. var import_js_format4 = require("@e22m4u/js-format");
  521. var import_js_service2 = require("@e22m4u/js-service");
  522. // src/document-validators.js
  523. var import_js_format3 = require("@e22m4u/js-format");
  524. function validateShallowOADocumentObject(documentObject) {
  525. if (!documentObject || typeof documentObject !== "object" || Array.isArray(documentObject)) {
  526. throw new import_js_format3.InvalidArgumentError(
  527. "OpenAPI Document Object must be an Object, but %v was given.",
  528. documentObject
  529. );
  530. }
  531. if (!documentObject.openapi || typeof documentObject.openapi !== "string") {
  532. throw new import_js_format3.InvalidArgumentError(
  533. 'Property "openapi" must be a non-empty String, but %v was given.',
  534. documentObject.openapi
  535. );
  536. }
  537. if (!documentObject.info || typeof documentObject.info !== "object" || Array.isArray(documentObject.info)) {
  538. throw new import_js_format3.InvalidArgumentError(
  539. 'Property "info" must be an Object, but %v was given.',
  540. documentObject.info
  541. );
  542. }
  543. if (!documentObject.info.title || typeof documentObject.info.title !== "string") {
  544. throw new import_js_format3.InvalidArgumentError(
  545. 'Property "info.title" must be a non-empty String, but %v was given.',
  546. documentObject.info.title
  547. );
  548. }
  549. if (!documentObject.info.version || typeof documentObject.info.version !== "string") {
  550. throw new import_js_format3.InvalidArgumentError(
  551. 'Property "info.version" must be a non-empty String, but %v was given.',
  552. documentObject.info.version
  553. );
  554. }
  555. if (documentObject.jsonSchemaDialect !== void 0) {
  556. if (!documentObject.jsonSchemaDialect || typeof documentObject.jsonSchemaDialect !== "string") {
  557. throw new import_js_format3.InvalidArgumentError(
  558. 'Property "jsonSchemaDialect" must be a non-empty String, but %v was given.',
  559. documentObject.jsonSchemaDialect
  560. );
  561. }
  562. }
  563. if (documentObject.servers !== void 0) {
  564. if (!Array.isArray(documentObject.servers)) {
  565. throw new import_js_format3.InvalidArgumentError(
  566. 'Property "servers" must be an Array, but %v was given.',
  567. documentObject.servers
  568. );
  569. }
  570. documentObject.servers.forEach((serverObject, index) => {
  571. if (!serverObject || typeof serverObject !== "object" || Array.isArray(serverObject)) {
  572. throw new import_js_format3.InvalidArgumentError(
  573. 'Element "servers[%d]" must be an Object, but %v was given.',
  574. index,
  575. serverObject
  576. );
  577. }
  578. });
  579. }
  580. if (documentObject.paths !== void 0) {
  581. if (!documentObject.paths || typeof documentObject.paths !== "object" || Array.isArray(documentObject.paths)) {
  582. throw new import_js_format3.InvalidArgumentError(
  583. 'Property "paths" must be an Object, but %v was given.',
  584. documentObject.paths
  585. );
  586. }
  587. }
  588. if (documentObject.webhooks !== void 0) {
  589. if (!documentObject.webhooks || typeof documentObject.webhooks !== "object" || Array.isArray(documentObject.webhooks)) {
  590. throw new import_js_format3.InvalidArgumentError(
  591. 'Property "webhooks" must be an Object, but %v was given.',
  592. documentObject.webhooks
  593. );
  594. }
  595. }
  596. if (documentObject.components !== void 0) {
  597. if (!documentObject.components || typeof documentObject.components !== "object" || Array.isArray(documentObject.components)) {
  598. throw new import_js_format3.InvalidArgumentError(
  599. 'Property "components" must be an Object, but %v was given.',
  600. documentObject.components
  601. );
  602. }
  603. }
  604. if (documentObject.security !== void 0) {
  605. if (!Array.isArray(documentObject.security)) {
  606. throw new import_js_format3.InvalidArgumentError(
  607. 'Property "security" must be an Array, but %v was given.',
  608. documentObject.security
  609. );
  610. }
  611. documentObject.security.forEach((securityRequirementObject, index) => {
  612. if (!securityRequirementObject || typeof securityRequirementObject !== "object" || Array.isArray(securityRequirementObject)) {
  613. throw new import_js_format3.InvalidArgumentError(
  614. 'Element "security[%d]" must be an Object, but %v was given.',
  615. index,
  616. securityRequirementObject
  617. );
  618. }
  619. });
  620. }
  621. if (documentObject.tags !== void 0) {
  622. if (!Array.isArray(documentObject.tags)) {
  623. throw new import_js_format3.InvalidArgumentError(
  624. 'Property "tags" must be an Array, but %v was given.',
  625. documentObject.tags
  626. );
  627. }
  628. documentObject.tags.forEach((tagObject, index) => {
  629. if (!tagObject || typeof tagObject !== "object" || Array.isArray(tagObject)) {
  630. throw new import_js_format3.InvalidArgumentError(
  631. 'Element "tags[%d]" must be an Object, but %v was given.',
  632. index,
  633. tagObject
  634. );
  635. }
  636. });
  637. }
  638. if (documentObject.externalDocs !== void 0) {
  639. if (!documentObject.externalDocs || typeof documentObject.externalDocs !== "object" || Array.isArray(documentObject.externalDocs)) {
  640. throw new import_js_format3.InvalidArgumentError(
  641. 'Property "externalDocs" must be an Object, but %v was given.',
  642. documentObject.externalDocs
  643. );
  644. }
  645. }
  646. }
  647. __name(validateShallowOADocumentObject, "validateShallowOADocumentObject");
  648. // src/document-specification.js
  649. var OPENAPI_VERSION = "3.1.0";
  650. var OAOperationMethod = {
  651. GET: "get",
  652. PUT: "put",
  653. POST: "post",
  654. DELETE: "delete",
  655. OPTIONS: "options",
  656. HEAD: "head",
  657. PATCH: "patch",
  658. TRACE: "trace"
  659. };
  660. var OAParameterLocation = {
  661. QUERY: "query",
  662. HEADER: "header",
  663. PATH: "path",
  664. COOKIE: "cookie"
  665. };
  666. var OAParameterStyle = {
  667. MATRIX: "matrix",
  668. LABEL: "label",
  669. FORM: "form",
  670. SIMPLE: "simple",
  671. SPACE_DELIMITED: "spaceDelimited",
  672. PIPE_DELIMITED: "pipeDelimited",
  673. DEEP_OBJECT: "deepObject"
  674. };
  675. var OADataType = {
  676. STRING: "string",
  677. NUMBER: "number",
  678. INTEGER: "integer",
  679. BOOLEAN: "boolean",
  680. OBJECT: "object",
  681. ARRAY: "array",
  682. NULL: "null"
  683. };
  684. var OADataFormat = {
  685. INT32: "int32",
  686. INT64: "int64",
  687. FLOAT: "float",
  688. DOUBLE: "double",
  689. PASSWORD: "password",
  690. BINARY: "binary"
  691. };
  692. var OAMediaType = {
  693. TEXT_PLAIN: "text/plain",
  694. TEXT_HTML: "text/html",
  695. APPLICATION_XML: "application/xml",
  696. APPLICATION_JSON: "application/json",
  697. MULTIPART_FORM_DATA: "multipart/form-data"
  698. };
  699. var OASecuritySchemeType = {
  700. API_KEY: "apiKey",
  701. HTTP: "http",
  702. MUTUAL_TLS: "mutualTLS",
  703. OAUTH_2: "oauth2",
  704. OPEN_ID_CONNECT: "openIdConnect"
  705. };
  706. var OAApiKeyLocation = {
  707. QUERY: "query",
  708. HEADER: "header",
  709. COOKIE: "cookie"
  710. };
  711. // src/oa-document-builder.js
  712. var OADocumentBuilder = class extends import_js_service2.Service {
  713. static {
  714. __name(this, "OADocumentBuilder");
  715. }
  716. /**
  717. * Document.
  718. *
  719. * @private
  720. * @type {object}
  721. */
  722. _document = {
  723. openapi: OPENAPI_VERSION,
  724. info: {
  725. title: "API Documentation",
  726. version: "0.0.1"
  727. }
  728. };
  729. /**
  730. * Constructor.
  731. *
  732. * @param {object} [containerOrDocument]
  733. * @param {object} [document]
  734. */
  735. constructor(containerOrDocument, document) {
  736. if ((0, import_js_service2.isServiceContainer)(containerOrDocument)) {
  737. super(containerOrDocument);
  738. } else {
  739. super();
  740. document = containerOrDocument;
  741. }
  742. if (document !== void 0) {
  743. if (!document || typeof document !== "object" || Array.isArray(document)) {
  744. throw new import_js_format4.InvalidArgumentError(
  745. "OpenAPI Document Object must be an Object, but %v was given.",
  746. document
  747. );
  748. }
  749. document = structuredClone(document);
  750. if (!document.openapi) {
  751. document.openapi = this._document.openapi;
  752. }
  753. if (!document.info) {
  754. document.info = this._document.info;
  755. }
  756. if (!document.info || typeof document.info !== "object" || Array.isArray(document.info)) {
  757. throw new import_js_format4.InvalidArgumentError(
  758. 'Property "info" must be an Object, but %v was given.',
  759. document.info
  760. );
  761. }
  762. if (!document.info.title) {
  763. document.info.title = this._document.info.title;
  764. }
  765. if (!document.info.version) {
  766. document.info.version = this._document.info.version;
  767. }
  768. validateShallowOADocumentObject(document);
  769. this._document = document;
  770. }
  771. }
  772. /**
  773. * Define schema component.
  774. *
  775. * @param {object} schemaDef
  776. * @returns {this}
  777. */
  778. defineSchemaComponent(schemaDef) {
  779. this.getService(OAComponentRegistry).defineSchema(schemaDef);
  780. return this;
  781. }
  782. /**
  783. * Define response component.
  784. *
  785. * @param {object} responseDef
  786. * @returns {this}
  787. */
  788. defineResponseComponent(responseDef) {
  789. this.getService(OAComponentRegistry).defineResponse(responseDef);
  790. return this;
  791. }
  792. /**
  793. * Define parameter component.
  794. *
  795. * @param {object} parameterDef
  796. * @returns {this}
  797. */
  798. defineParameterComponent(parameterDef) {
  799. this.getService(OAComponentRegistry).defineParameter(parameterDef);
  800. return this;
  801. }
  802. /**
  803. * Define example component.
  804. *
  805. * @param {object} exampleDef
  806. * @returns {this}
  807. */
  808. defineExampleComponent(exampleDef) {
  809. this.getService(OAComponentRegistry).defineExample(exampleDef);
  810. return this;
  811. }
  812. /**
  813. * Define request body component.
  814. *
  815. * @param {object} requestBodyDef
  816. * @returns {this}
  817. */
  818. defineRequestBodyComponent(requestBodyDef) {
  819. this.getService(OAComponentRegistry).defineRequestBody(requestBodyDef);
  820. return this;
  821. }
  822. /**
  823. * Define header component.
  824. *
  825. * @param {object} headerDef
  826. * @returns {this}
  827. */
  828. defineHeaderComponent(headerDef) {
  829. this.getService(OAComponentRegistry).defineHeader(headerDef);
  830. return this;
  831. }
  832. /**
  833. * Define security scheme component.
  834. *
  835. * @param {object} securitySchemeDef
  836. * @returns {this}
  837. */
  838. defineSecuritySchemeComponent(securitySchemeDef) {
  839. this.getService(OAComponentRegistry).defineSecurityScheme(
  840. securitySchemeDef
  841. );
  842. return this;
  843. }
  844. /**
  845. * Define link component.
  846. *
  847. * @param {object} linkDef
  848. * @returns {this}
  849. */
  850. defineLinkComponent(linkDef) {
  851. this.getService(OAComponentRegistry).defineLink(linkDef);
  852. return this;
  853. }
  854. /**
  855. * Define callback component.
  856. *
  857. * @param {object} callbackDef
  858. * @returns {this}
  859. */
  860. defineCallbackComponent(callbackDef) {
  861. this.getService(OAComponentRegistry).defineCallback(callbackDef);
  862. return this;
  863. }
  864. /**
  865. * Define path item component.
  866. *
  867. * @param {object} pathItemDef
  868. * @returns {this}
  869. */
  870. definePathItemComponent(pathItemDef) {
  871. this.getService(OAComponentRegistry).definePathItem(pathItemDef);
  872. return this;
  873. }
  874. /**
  875. * Define operation.
  876. *
  877. * @param {object} operationDef
  878. */
  879. defineOperation(operationDef) {
  880. if (!operationDef || typeof operationDef !== "object" || Array.isArray(operationDef)) {
  881. throw new import_js_format4.InvalidArgumentError(
  882. "Operation Definition must be an Object, but %v was given.",
  883. operationDef
  884. );
  885. }
  886. if (!operationDef.path || typeof operationDef.path !== "string") {
  887. throw new import_js_format4.InvalidArgumentError(
  888. 'Property "path" must be a non-empty String, but %v was given.',
  889. operationDef.path
  890. );
  891. }
  892. if (!operationDef.method || typeof operationDef.method !== "string") {
  893. throw new import_js_format4.InvalidArgumentError(
  894. 'Property "method" must be a non-empty String, but %v was given.',
  895. operationDef.method
  896. );
  897. }
  898. if (!Object.values(OAOperationMethod).includes(operationDef.method)) {
  899. throw new import_js_format4.InvalidArgumentError(
  900. 'Property "method" must be one of values: %l, but %v was given.',
  901. Object.values(OAOperationMethod)
  902. );
  903. }
  904. if (!operationDef.operation || typeof operationDef.operation !== "object" || Array.isArray(operationDef.operation)) {
  905. throw new import_js_format4.InvalidArgumentError(
  906. 'Property "operation" must be an Object, but %v was given.',
  907. operationDef.operation
  908. );
  909. }
  910. const normalizedPath = normalizePath(operationDef.path);
  911. if (!this._document.paths) {
  912. this._document.paths = {};
  913. }
  914. if (!this._document.paths[normalizedPath]) {
  915. this._document.paths[normalizedPath] = {};
  916. }
  917. this._document.paths[normalizedPath][operationDef.method] = structuredClone(
  918. operationDef.operation
  919. );
  920. }
  921. /**
  922. * Create scope.
  923. *
  924. * @param {object} [options]
  925. * @returns {OADocumentScope}
  926. */
  927. createScope(options) {
  928. return new OADocumentScope(this, options);
  929. }
  930. /**
  931. * Build.
  932. *
  933. * @returns {object}
  934. */
  935. build() {
  936. const doc = structuredClone(this._document);
  937. const compsMap = this.getService(OAComponentRegistry).getComponentsObject();
  938. doc.components = doc.components || {};
  939. Object.keys(compsMap).forEach((compsKey) => {
  940. const compsNames = Object.keys(compsMap[compsKey]);
  941. if (compsNames.length) {
  942. doc.components[compsKey] = doc.components[compsKey] || {};
  943. compsNames.forEach((compName) => {
  944. doc.components[compsKey][compName] = compsMap[compsKey][compName];
  945. });
  946. }
  947. });
  948. if (!Object.keys(doc.components).length) {
  949. delete doc.components;
  950. }
  951. return doc;
  952. }
  953. /**
  954. * Build JSON.
  955. *
  956. * @param {string|number} [space]
  957. * @returns {string}
  958. */
  959. buildJson(space = 0) {
  960. return JSON.stringify(this.build(), null, space);
  961. }
  962. };
  963. // src/oa-document-scope.js
  964. var OADocumentScope = class _OADocumentScope {
  965. static {
  966. __name(this, "OADocumentScope");
  967. }
  968. /**
  969. * Builder.
  970. *
  971. * @type {OADocumentBuilder}
  972. */
  973. _builder;
  974. /**
  975. * Path prefix.
  976. *
  977. * @type {string}
  978. */
  979. _pathPrefix;
  980. /**
  981. * Tags.
  982. *
  983. * @type {string[]}
  984. */
  985. _tags;
  986. /**
  987. * @param {object} builder
  988. * @param {object} [options]
  989. */
  990. constructor(builder, options = {}) {
  991. if (!(builder instanceof OADocumentBuilder)) {
  992. throw new import_js_format5.InvalidArgumentError(
  993. 'Parameter "builder" must be an instance of OADocumentBuilder, but %v was given.',
  994. builder
  995. );
  996. }
  997. if (!options || typeof options !== "object" || Array.isArray(options)) {
  998. throw new import_js_format5.InvalidArgumentError(
  999. 'Parameter "options" must be an Object, but %v was given.',
  1000. options
  1001. );
  1002. }
  1003. if (options.pathPrefix !== void 0) {
  1004. if (!options.pathPrefix || typeof options.pathPrefix !== "string") {
  1005. throw new import_js_format5.InvalidArgumentError(
  1006. 'Property "pathPrefix" must be a non-empty String, but %v was given.',
  1007. options.pathPrefix
  1008. );
  1009. }
  1010. }
  1011. if (options.tags !== void 0) {
  1012. if (!Array.isArray(options.tags)) {
  1013. throw new import_js_format5.InvalidArgumentError(
  1014. 'Property "tags" must be an Array, but %v was given.',
  1015. options.tags
  1016. );
  1017. }
  1018. options.tags.forEach((tag, index) => {
  1019. if (!tag || typeof tag !== "string") {
  1020. throw new import_js_format5.InvalidArgumentError(
  1021. 'Element "tags[%d]" must be a non-empty String, but %v was given.',
  1022. index,
  1023. tag
  1024. );
  1025. }
  1026. });
  1027. }
  1028. this._builder = builder;
  1029. this._pathPrefix = normalizePath(options.pathPrefix || "/");
  1030. this._tags = options.tags || [];
  1031. }
  1032. /**
  1033. * Get builder.
  1034. *
  1035. * @returns {OADocumentBuilder}
  1036. */
  1037. getBuilder() {
  1038. return this._builder;
  1039. }
  1040. /**
  1041. * Get path prefix.
  1042. *
  1043. * @returns {string}
  1044. */
  1045. getPathPrefix() {
  1046. return this._pathPrefix;
  1047. }
  1048. /**
  1049. * Get tags.
  1050. *
  1051. * @returns {string[]}
  1052. */
  1053. getTags() {
  1054. return [...this._tags];
  1055. }
  1056. /**
  1057. * Define operation.
  1058. *
  1059. * @param {object} operationDef
  1060. * @returns {this}
  1061. */
  1062. defineOperation(operationDef) {
  1063. if (!operationDef || typeof operationDef !== "object" || Array.isArray(operationDef)) {
  1064. throw new import_js_format5.InvalidArgumentError(
  1065. "Operation Definition must be an Object, but %v was given.",
  1066. operationDef
  1067. );
  1068. }
  1069. if (!operationDef.path || typeof operationDef.path !== "string") {
  1070. throw new import_js_format5.InvalidArgumentError(
  1071. 'Property "path" must be a non-empty String, but %v was given.',
  1072. operationDef.path
  1073. );
  1074. }
  1075. if (!operationDef.method || typeof operationDef.method !== "string") {
  1076. throw new import_js_format5.InvalidArgumentError(
  1077. 'Property "method" must be a non-empty String, but %v was given.',
  1078. operationDef.method
  1079. );
  1080. }
  1081. if (!Object.values(OAOperationMethod).includes(operationDef.method)) {
  1082. throw new import_js_format5.InvalidArgumentError(
  1083. 'Property "method" must be one of values: %l, but %v was given.',
  1084. Object.values(OAOperationMethod)
  1085. );
  1086. }
  1087. if (!operationDef.operation || typeof operationDef.operation !== "object" || Array.isArray(operationDef.operation)) {
  1088. throw new import_js_format5.InvalidArgumentError(
  1089. 'Property "operation" must be an Object, but %v was given.',
  1090. operationDef.operation
  1091. );
  1092. }
  1093. const fullPath = joinPath(this._pathPrefix, operationDef.path);
  1094. const operation = structuredClone(operationDef.operation);
  1095. if (this._tags.length > 0) {
  1096. operation.tags = [...this._tags, ...operation.tags || []];
  1097. operation.tags = [...new Set(operation.tags)];
  1098. }
  1099. this._builder.defineOperation({
  1100. ...operationDef,
  1101. path: fullPath,
  1102. operation
  1103. });
  1104. return this;
  1105. }
  1106. /**
  1107. * Create scope.
  1108. *
  1109. * @param {object} [options]
  1110. * @returns {OADocumentScope}
  1111. */
  1112. createScope(options = {}) {
  1113. if (!options || typeof options !== "object" || Array.isArray(options)) {
  1114. throw new import_js_format5.InvalidArgumentError(
  1115. 'Parameter "options" must be an Object, but %v was given.',
  1116. options
  1117. );
  1118. }
  1119. if (options.pathPrefix !== void 0) {
  1120. if (!options.pathPrefix || typeof options.pathPrefix !== "string") {
  1121. throw new import_js_format5.InvalidArgumentError(
  1122. 'Property "pathPrefix" must be a non-empty String, but %v was given.',
  1123. options.pathPrefix
  1124. );
  1125. }
  1126. }
  1127. if (options.tags !== void 0) {
  1128. if (!Array.isArray(options.tags)) {
  1129. throw new import_js_format5.InvalidArgumentError(
  1130. 'Property "tags" must be an Array, but %v was given.',
  1131. options.tags
  1132. );
  1133. }
  1134. options.tags.forEach((tag, index) => {
  1135. if (!tag || typeof tag !== "string") {
  1136. throw new import_js_format5.InvalidArgumentError(
  1137. 'Element "tags[%d]" must be a non-empty String, but %v was given.',
  1138. index,
  1139. tag
  1140. );
  1141. }
  1142. });
  1143. }
  1144. return new _OADocumentScope(this._builder, {
  1145. pathPrefix: joinPath(this._pathPrefix, options.pathPrefix),
  1146. tags: [...this._tags, ...options.tags || []]
  1147. });
  1148. }
  1149. };
  1150. // Annotate the CommonJS export names for ESM import in node:
  1151. 0 && (module.exports = {
  1152. OAApiKeyLocation,
  1153. OAComponentRegistry,
  1154. OAComponentType,
  1155. OADataFormat,
  1156. OADataType,
  1157. OADocumentBuilder,
  1158. OADocumentScope,
  1159. OAMediaType,
  1160. OAOperationMethod,
  1161. OAParameterLocation,
  1162. OAParameterStyle,
  1163. OASecuritySchemeType,
  1164. OA_COMPONENT_TYPE_TO_COMPONENTS_KEY_MAP,
  1165. OPENAPI_VERSION,
  1166. oaCallbackRef,
  1167. oaExampleRef,
  1168. oaLinkRef,
  1169. oaParameterRef,
  1170. oaPathItemRef,
  1171. oaRequestBodyRef,
  1172. oaResponseRef,
  1173. oaSchemaRef,
  1174. oaSecuritySchemeRef
  1175. });