index.cjs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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. OAComponentsSegment: () => OAComponentsSegment,
  25. OADataFormat: () => OADataFormat,
  26. OADataType: () => OADataType,
  27. OADocumentBuilder: () => OADocumentBuilder,
  28. OADocumentScope: () => OADocumentScope,
  29. OAMediaType: () => OAMediaType,
  30. OAOperationMethod: () => OAOperationMethod,
  31. OAParameterLocation: () => OAParameterLocation,
  32. OAParameterStyle: () => OAParameterStyle,
  33. OASecuritySchemeType: () => OASecuritySchemeType,
  34. OPENAPI_VERSION: () => OPENAPI_VERSION,
  35. oaCallbackRef: () => oaCallbackRef,
  36. oaExampleRef: () => oaExampleRef,
  37. oaLinkRef: () => oaLinkRef,
  38. oaParameterRef: () => oaParameterRef,
  39. oaPathItemRef: () => oaPathItemRef,
  40. oaRef: () => oaRef,
  41. oaRequestBodyRef: () => oaRequestBodyRef,
  42. oaResponseRef: () => oaResponseRef,
  43. oaSchemaRef: () => oaSchemaRef,
  44. oaSecuritySchemeRef: () => oaSecuritySchemeRef
  45. });
  46. module.exports = __toCommonJS(index_exports);
  47. // src/constants.js
  48. var OPENAPI_VERSION = "3.1.0";
  49. var OAComponentsSegment = {
  50. SCHEMAS: "schemas",
  51. RESPONSES: "responses",
  52. PARAMETERS: "parameters",
  53. EXAMPLES: "examples",
  54. REQUEST_BODIES: "requestBodies",
  55. HEADERS: "headers",
  56. SECURITY_SCHEMES: "securitySchemes",
  57. LINKS: "links",
  58. CALLBACKS: "callbacks",
  59. PATH_ITEMS: "pathItems"
  60. };
  61. // src/oa-document-scope.js
  62. var import_js_format4 = require("@e22m4u/js-format");
  63. // src/utils/oa-ref.js
  64. var import_js_format = require("@e22m4u/js-format");
  65. function oaRef(name, segment) {
  66. if (!name || typeof name !== "string") {
  67. throw new import_js_format.InvalidArgumentError(
  68. 'Parameter "name" must be a non-empty String, but %v was given.',
  69. name
  70. );
  71. }
  72. if (!Object.values(OAComponentsSegment).includes(segment)) {
  73. throw new import_js_format.InvalidArgumentError(
  74. 'Parameter "segment" must be one of values: %l, but %v was given.',
  75. Object.values(OAComponentsSegment),
  76. segment
  77. );
  78. }
  79. return { $ref: `#/components/${segment}/${name}` };
  80. }
  81. __name(oaRef, "oaRef");
  82. var oaSchemaRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentsSegment.SCHEMAS), "oaSchemaRef");
  83. var oaResponseRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentsSegment.RESPONSES), "oaResponseRef");
  84. var oaParameterRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentsSegment.PARAMETERS), "oaParameterRef");
  85. var oaExampleRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentsSegment.EXAMPLES), "oaExampleRef");
  86. var oaRequestBodyRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentsSegment.REQUEST_BODIES), "oaRequestBodyRef");
  87. var oaSecuritySchemeRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentsSegment.SECURITY_SCHEMES), "oaSecuritySchemeRef");
  88. var oaLinkRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentsSegment.LINKS), "oaLinkRef");
  89. var oaCallbackRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentsSegment.CALLBACKS), "oaCallbackRef");
  90. var oaPathItemRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentsSegment.PATH_ITEMS), "oaPathItemRef");
  91. // src/utils/join-path.js
  92. function joinPath(...segments) {
  93. const path = segments.filter((seg) => seg != void 0).map((seg) => String(seg).replace(/(^\/+|\/+$)/g, "")).filter(Boolean).join("/");
  94. return ("/" + path).replace(/\/+/g, "/");
  95. }
  96. __name(joinPath, "joinPath");
  97. // src/utils/normalize-path.js
  98. function normalizePath(value, noStartingSlash = false) {
  99. if (typeof value !== "string") {
  100. return "/";
  101. }
  102. const res = value.trim().replace(/\/+/g, "/").replace(/(^\/|\/$)/g, "");
  103. return noStartingSlash ? res : "/" + res;
  104. }
  105. __name(normalizePath, "normalizePath");
  106. // src/oa-document-builder.js
  107. var import_js_format3 = require("@e22m4u/js-format");
  108. // src/document-specification.js
  109. var OAOperationMethod = {
  110. GET: "get",
  111. PUT: "put",
  112. POST: "post",
  113. DELETE: "delete",
  114. OPTIONS: "options",
  115. HEAD: "head",
  116. PATCH: "patch",
  117. TRACE: "trace"
  118. };
  119. var OAParameterLocation = {
  120. QUERY: "query",
  121. HEADER: "header",
  122. PATH: "path",
  123. COOKIE: "cookie"
  124. };
  125. var OAParameterStyle = {
  126. MATRIX: "matrix",
  127. LABEL: "label",
  128. FORM: "form",
  129. SIMPLE: "simple",
  130. SPACE_DELIMITED: "spaceDelimited",
  131. PIPE_DELIMITED: "pipeDelimited",
  132. DEEP_OBJECT: "deepObject"
  133. };
  134. var OADataType = {
  135. STRING: "string",
  136. NUMBER: "number",
  137. INTEGER: "integer",
  138. BOOLEAN: "boolean",
  139. OBJECT: "object",
  140. ARRAY: "array",
  141. NULL: "null"
  142. };
  143. var OADataFormat = {
  144. INT32: "int32",
  145. INT64: "int64",
  146. FLOAT: "float",
  147. DOUBLE: "double",
  148. PASSWORD: "password",
  149. BINARY: "binary"
  150. };
  151. var OAMediaType = {
  152. TEXT_PLAIN: "text/plain",
  153. TEXT_HTML: "text/html",
  154. APPLICATION_XML: "application/xml",
  155. APPLICATION_JSON: "application/json",
  156. MULTIPART_FORM_DATA: "multipart/form-data"
  157. };
  158. var OASecuritySchemeType = {
  159. API_KEY: "apiKey",
  160. HTTP: "http",
  161. MUTUAL_TLS: "mutualTLS",
  162. OAUTH_2: "oauth2",
  163. OPEN_ID_CONNECT: "openIdConnect"
  164. };
  165. var OAApiKeyLocation = {
  166. QUERY: "query",
  167. HEADER: "header",
  168. COOKIE: "cookie"
  169. };
  170. // src/oa-document-builder.js
  171. var import_js_service = require("@e22m4u/js-service");
  172. // src/document-validators.js
  173. var import_js_format2 = require("@e22m4u/js-format");
  174. function validateShallowOADocumentObject(documentObject) {
  175. if (!documentObject || typeof documentObject !== "object" || Array.isArray(documentObject)) {
  176. throw new import_js_format2.InvalidArgumentError(
  177. "OpenAPI Document Object must be an Object, but %v was given.",
  178. documentObject
  179. );
  180. }
  181. if (!documentObject.openapi || typeof documentObject.openapi !== "string") {
  182. throw new import_js_format2.InvalidArgumentError(
  183. 'Property "openapi" must be a non-empty String, but %v was given.',
  184. documentObject.openapi
  185. );
  186. }
  187. if (!documentObject.info || typeof documentObject.info !== "object" || Array.isArray(documentObject.info)) {
  188. throw new import_js_format2.InvalidArgumentError(
  189. 'Property "info" must be an Object, but %v was given.',
  190. documentObject.info
  191. );
  192. }
  193. if (!documentObject.info.title || typeof documentObject.info.title !== "string") {
  194. throw new import_js_format2.InvalidArgumentError(
  195. 'Property "info.title" must be a non-empty String, but %v was given.',
  196. documentObject.info.title
  197. );
  198. }
  199. if (!documentObject.info.version || typeof documentObject.info.version !== "string") {
  200. throw new import_js_format2.InvalidArgumentError(
  201. 'Property "info.version" must be a non-empty String, but %v was given.',
  202. documentObject.info.version
  203. );
  204. }
  205. if (documentObject.jsonSchemaDialect !== void 0) {
  206. if (!documentObject.jsonSchemaDialect || typeof documentObject.jsonSchemaDialect !== "string") {
  207. throw new import_js_format2.InvalidArgumentError(
  208. 'Property "jsonSchemaDialect" must be a non-empty String, but %v was given.',
  209. documentObject.jsonSchemaDialect
  210. );
  211. }
  212. }
  213. if (documentObject.servers !== void 0) {
  214. if (!Array.isArray(documentObject.servers)) {
  215. throw new import_js_format2.InvalidArgumentError(
  216. 'Property "servers" must be an Array, but %v was given.',
  217. documentObject.servers
  218. );
  219. }
  220. documentObject.servers.forEach((serverObject, index) => {
  221. if (!serverObject || typeof serverObject !== "object" || Array.isArray(serverObject)) {
  222. throw new import_js_format2.InvalidArgumentError(
  223. 'Element "servers[%d]" must be an Object, but %v was given.',
  224. index,
  225. serverObject
  226. );
  227. }
  228. });
  229. }
  230. if (documentObject.paths !== void 0) {
  231. if (!documentObject.paths || typeof documentObject.paths !== "object" || Array.isArray(documentObject.paths)) {
  232. throw new import_js_format2.InvalidArgumentError(
  233. 'Property "paths" must be an Object, but %v was given.',
  234. documentObject.paths
  235. );
  236. }
  237. }
  238. if (documentObject.webhooks !== void 0) {
  239. if (!documentObject.webhooks || typeof documentObject.webhooks !== "object" || Array.isArray(documentObject.webhooks)) {
  240. throw new import_js_format2.InvalidArgumentError(
  241. 'Property "webhooks" must be an Object, but %v was given.',
  242. documentObject.webhooks
  243. );
  244. }
  245. }
  246. if (documentObject.components !== void 0) {
  247. if (!documentObject.components || typeof documentObject.components !== "object" || Array.isArray(documentObject.components)) {
  248. throw new import_js_format2.InvalidArgumentError(
  249. 'Property "components" must be an Object, but %v was given.',
  250. documentObject.components
  251. );
  252. }
  253. }
  254. if (documentObject.security !== void 0) {
  255. if (!Array.isArray(documentObject.security)) {
  256. throw new import_js_format2.InvalidArgumentError(
  257. 'Property "security" must be an Array, but %v was given.',
  258. documentObject.security
  259. );
  260. }
  261. documentObject.security.forEach((securityRequirementObject, index) => {
  262. if (!securityRequirementObject || typeof securityRequirementObject !== "object" || Array.isArray(securityRequirementObject)) {
  263. throw new import_js_format2.InvalidArgumentError(
  264. 'Element "security[%d]" must be an Object, but %v was given.',
  265. index,
  266. securityRequirementObject
  267. );
  268. }
  269. });
  270. }
  271. if (documentObject.tags !== void 0) {
  272. if (!Array.isArray(documentObject.tags)) {
  273. throw new import_js_format2.InvalidArgumentError(
  274. 'Property "tags" must be an Array, but %v was given.',
  275. documentObject.tags
  276. );
  277. }
  278. documentObject.tags.forEach((tagObject, index) => {
  279. if (!tagObject || typeof tagObject !== "object" || Array.isArray(tagObject)) {
  280. throw new import_js_format2.InvalidArgumentError(
  281. 'Element "tags[%d]" must be an Object, but %v was given.',
  282. index,
  283. tagObject
  284. );
  285. }
  286. });
  287. }
  288. if (documentObject.externalDocs !== void 0) {
  289. if (!documentObject.externalDocs || typeof documentObject.externalDocs !== "object" || Array.isArray(documentObject.externalDocs)) {
  290. throw new import_js_format2.InvalidArgumentError(
  291. 'Property "externalDocs" must be an Object, but %v was given.',
  292. documentObject.externalDocs
  293. );
  294. }
  295. }
  296. }
  297. __name(validateShallowOADocumentObject, "validateShallowOADocumentObject");
  298. // src/oa-document-builder.js
  299. var OADocumentBuilder = class extends import_js_service.Service {
  300. static {
  301. __name(this, "OADocumentBuilder");
  302. }
  303. /**
  304. * Document.
  305. *
  306. * @private
  307. * @type {object}
  308. */
  309. _document = {
  310. openapi: OPENAPI_VERSION,
  311. info: {
  312. title: "API Documentation",
  313. version: "0.0.1"
  314. }
  315. };
  316. /**
  317. * Constructor.
  318. *
  319. * @param {object} [containerOrDocument]
  320. * @param {object} [document]
  321. */
  322. constructor(containerOrDocument, document) {
  323. if ((0, import_js_service.isServiceContainer)(containerOrDocument)) {
  324. super(containerOrDocument);
  325. } else {
  326. super();
  327. document = containerOrDocument;
  328. }
  329. if (document !== void 0) {
  330. if (!document || typeof document !== "object" || Array.isArray(document)) {
  331. throw new import_js_format3.InvalidArgumentError(
  332. "OpenAPI Document Object must be an Object, but %v was given.",
  333. document
  334. );
  335. }
  336. document = structuredClone(document);
  337. if (!document.openapi) {
  338. document.openapi = this._document.openapi;
  339. }
  340. if (!document.info) {
  341. document.info = this._document.info;
  342. }
  343. if (!document.info || typeof document.info !== "object" || Array.isArray(document.info)) {
  344. throw new import_js_format3.InvalidArgumentError(
  345. 'Property "info" must be an Object, but %v was given.',
  346. document.info
  347. );
  348. }
  349. if (!document.info.title) {
  350. document.info.title = this._document.info.title;
  351. }
  352. if (!document.info.version) {
  353. document.info.version = this._document.info.version;
  354. }
  355. validateShallowOADocumentObject(document);
  356. this._document = document;
  357. }
  358. }
  359. /**
  360. * Define component.
  361. *
  362. * @param {string} segmentName
  363. * @param {string} propertyName
  364. * @param {object} definition
  365. * @returns {this}
  366. */
  367. _defineComponent(segmentName, propertyName, definition) {
  368. if (!Object.values(OAComponentsSegment).includes(segmentName)) {
  369. throw new import_js_format3.InvalidArgumentError(
  370. 'Property "segmentName" must be one of values: %l, but %v was given.',
  371. Object.values(OAComponentsSegment),
  372. segmentName
  373. );
  374. }
  375. if (!propertyName || typeof propertyName !== "string") {
  376. throw new import_js_format3.InvalidArgumentError(
  377. 'Property "propertyName" must be a non-empty String, but %v was given.',
  378. propertyName
  379. );
  380. }
  381. if (!definition || typeof definition !== "object" || Array.isArray(definition)) {
  382. throw new import_js_format3.InvalidArgumentError(
  383. "Component definition must be an Object, but %v was given.",
  384. definition
  385. );
  386. }
  387. if (!definition.name || typeof definition.name !== "string") {
  388. throw new import_js_format3.InvalidArgumentError(
  389. 'Property "name" must be a non-empty String, but %v was given.',
  390. definition.name
  391. );
  392. }
  393. const component = definition[propertyName];
  394. if (!component || typeof component !== "object" || Array.isArray(component)) {
  395. throw new import_js_format3.InvalidArgumentError(
  396. "Property %v must be an Object, but %v was given.",
  397. propertyName,
  398. component
  399. );
  400. }
  401. if (!this._document.components) {
  402. this._document.components = {};
  403. }
  404. if (!this._document.components[segmentName]) {
  405. this._document.components[segmentName] = {};
  406. }
  407. this._document.components[segmentName][definition.name] = structuredClone(component);
  408. return this;
  409. }
  410. /**
  411. * Define schema component.
  412. *
  413. * @param {object} schemaDef
  414. * @returns {this}
  415. */
  416. defineSchemaComponent(schemaDef) {
  417. return this._defineComponent(
  418. OAComponentsSegment.SCHEMAS,
  419. "schema",
  420. schemaDef
  421. );
  422. }
  423. /**
  424. * Define response component.
  425. *
  426. * @param {object} responseDef
  427. * @returns {this}
  428. */
  429. defineResponseComponent(responseDef) {
  430. return this._defineComponent(
  431. OAComponentsSegment.RESPONSES,
  432. "response",
  433. responseDef
  434. );
  435. }
  436. /**
  437. * Define parameter component.
  438. *
  439. * @param {object} parameterDef
  440. * @returns {this}
  441. */
  442. defineParameterComponent(parameterDef) {
  443. return this._defineComponent(
  444. OAComponentsSegment.PARAMETERS,
  445. "parameter",
  446. parameterDef
  447. );
  448. }
  449. /**
  450. * Define example component.
  451. *
  452. * @param {object} exampleDef
  453. * @returns {this}
  454. */
  455. defineExampleComponent(exampleDef) {
  456. return this._defineComponent(
  457. OAComponentsSegment.EXAMPLES,
  458. "example",
  459. exampleDef
  460. );
  461. }
  462. /**
  463. * Define request body component.
  464. *
  465. * @param {object} requestBodyDef
  466. * @returns {this}
  467. */
  468. defineRequestBodyComponent(requestBodyDef) {
  469. return this._defineComponent(
  470. OAComponentsSegment.REQUEST_BODIES,
  471. "requestBody",
  472. requestBodyDef
  473. );
  474. }
  475. /**
  476. * Define header component.
  477. *
  478. * @param {object} headerDef
  479. * @returns {this}
  480. */
  481. defineHeaderComponent(headerDef) {
  482. return this._defineComponent(
  483. OAComponentsSegment.HEADERS,
  484. "header",
  485. headerDef
  486. );
  487. }
  488. /**
  489. * Define security scheme component.
  490. *
  491. * @param {object} securitySchemeDef
  492. * @returns {this}
  493. */
  494. defineSecuritySchemeComponent(securitySchemeDef) {
  495. return this._defineComponent(
  496. OAComponentsSegment.SECURITY_SCHEMES,
  497. "securityScheme",
  498. securitySchemeDef
  499. );
  500. }
  501. /**
  502. * Define link component.
  503. *
  504. * @param {object} linkDef
  505. * @returns {this}
  506. */
  507. defineLinkComponent(linkDef) {
  508. return this._defineComponent(
  509. OAComponentsSegment.LINKS,
  510. "link",
  511. linkDef
  512. );
  513. }
  514. /**
  515. * Define callback component.
  516. *
  517. * @param {object} callbackDef
  518. * @returns {this}
  519. */
  520. defineCallbackComponent(callbackDef) {
  521. return this._defineComponent(
  522. OAComponentsSegment.CALLBACKS,
  523. "callback",
  524. callbackDef
  525. );
  526. }
  527. /**
  528. * Define path item component.
  529. *
  530. * @param {object} pathItemDef
  531. * @returns {this}
  532. */
  533. definePathItemComponent(pathItemDef) {
  534. return this._defineComponent(
  535. OAComponentsSegment.PATH_ITEMS,
  536. "pathItem",
  537. pathItemDef
  538. );
  539. }
  540. /**
  541. * Define operation.
  542. *
  543. * @param {object} operationDef
  544. */
  545. defineOperation(operationDef) {
  546. if (!operationDef || typeof operationDef !== "object" || Array.isArray(operationDef)) {
  547. throw new import_js_format3.InvalidArgumentError(
  548. "Operation Definition must be an Object, but %v was given.",
  549. operationDef
  550. );
  551. }
  552. if (!operationDef.path || typeof operationDef.path !== "string") {
  553. throw new import_js_format3.InvalidArgumentError(
  554. 'Property "path" must be a non-empty String, but %v was given.',
  555. operationDef.path
  556. );
  557. }
  558. if (!operationDef.method || typeof operationDef.method !== "string") {
  559. throw new import_js_format3.InvalidArgumentError(
  560. 'Property "method" must be a non-empty String, but %v was given.',
  561. operationDef.method
  562. );
  563. }
  564. if (!Object.values(OAOperationMethod).includes(operationDef.method)) {
  565. throw new import_js_format3.InvalidArgumentError(
  566. 'Property "method" must be one of values: %l, but %v was given.',
  567. Object.values(OAOperationMethod)
  568. );
  569. }
  570. if (!operationDef.operation || typeof operationDef.operation !== "object" || Array.isArray(operationDef.operation)) {
  571. throw new import_js_format3.InvalidArgumentError(
  572. 'Property "operation" must be an Object, but %v was given.',
  573. operationDef.operation
  574. );
  575. }
  576. const normalizedPath = normalizePath(operationDef.path);
  577. if (!this._document.paths) {
  578. this._document.paths = {};
  579. }
  580. if (!this._document.paths[normalizedPath]) {
  581. this._document.paths[normalizedPath] = {};
  582. }
  583. this._document.paths[normalizedPath][operationDef.method] = structuredClone(
  584. operationDef.operation
  585. );
  586. }
  587. /**
  588. * Create scope.
  589. *
  590. * @param {object} [options]
  591. * @returns {OADocumentScope}
  592. */
  593. createScope(options) {
  594. return new OADocumentScope(this, options);
  595. }
  596. /**
  597. * Build.
  598. *
  599. * @returns {object}
  600. */
  601. build() {
  602. return structuredClone(this._document);
  603. }
  604. /**
  605. * Build JSON.
  606. *
  607. * @param {string|number} [space]
  608. * @returns {string}
  609. */
  610. buildJson(space = 0) {
  611. return JSON.stringify(this._document, null, space);
  612. }
  613. };
  614. // src/oa-document-scope.js
  615. var OADocumentScope = class _OADocumentScope {
  616. static {
  617. __name(this, "OADocumentScope");
  618. }
  619. /**
  620. * Builder.
  621. *
  622. * @type {OADocumentBuilder}
  623. */
  624. _builder;
  625. /**
  626. * Path prefix.
  627. *
  628. * @type {string}
  629. */
  630. _pathPrefix;
  631. /**
  632. * Tags.
  633. *
  634. * @type {string[]}
  635. */
  636. _tags;
  637. /**
  638. * @param {object} builder
  639. * @param {object} [options]
  640. */
  641. constructor(builder, options = {}) {
  642. if (!(builder instanceof OADocumentBuilder)) {
  643. throw new import_js_format4.InvalidArgumentError(
  644. 'Parameter "builder" must be an instance of OADocumentBuilder, but %v was given.',
  645. builder
  646. );
  647. }
  648. if (!options || typeof options !== "object" || Array.isArray(options)) {
  649. throw new import_js_format4.InvalidArgumentError(
  650. 'Parameter "options" must be an Object, but %v was given.',
  651. options
  652. );
  653. }
  654. if (options.pathPrefix !== void 0) {
  655. if (!options.pathPrefix || typeof options.pathPrefix !== "string") {
  656. throw new import_js_format4.InvalidArgumentError(
  657. 'Property "pathPrefix" must be a non-empty String, but %v was given.',
  658. options.pathPrefix
  659. );
  660. }
  661. }
  662. if (options.tags !== void 0) {
  663. if (!Array.isArray(options.tags)) {
  664. throw new import_js_format4.InvalidArgumentError(
  665. 'Property "tags" must be an Array, but %v was given.',
  666. options.tags
  667. );
  668. }
  669. options.tags.forEach((tag, index) => {
  670. if (!tag || typeof tag !== "string") {
  671. throw new import_js_format4.InvalidArgumentError(
  672. 'Element "tags[%d]" must be a non-empty String, but %v was given.',
  673. index,
  674. tag
  675. );
  676. }
  677. });
  678. }
  679. this._builder = builder;
  680. this._pathPrefix = normalizePath(options.pathPrefix || "/");
  681. this._tags = options.tags || [];
  682. }
  683. /**
  684. * Get builder.
  685. *
  686. * @returns {OADocumentBuilder}
  687. */
  688. getBuilder() {
  689. return this._builder;
  690. }
  691. /**
  692. * Get path prefix.
  693. *
  694. * @returns {string}
  695. */
  696. getPathPrefix() {
  697. return this._pathPrefix;
  698. }
  699. /**
  700. * Get tags.
  701. *
  702. * @returns {string[]}
  703. */
  704. getTags() {
  705. return [...this._tags];
  706. }
  707. /**
  708. * Define operation.
  709. *
  710. * @param {object} operationDef
  711. * @returns {this}
  712. */
  713. defineOperation(operationDef) {
  714. if (!operationDef || typeof operationDef !== "object" || Array.isArray(operationDef)) {
  715. throw new import_js_format4.InvalidArgumentError(
  716. "Operation Definition must be an Object, but %v was given.",
  717. operationDef
  718. );
  719. }
  720. if (!operationDef.path || typeof operationDef.path !== "string") {
  721. throw new import_js_format4.InvalidArgumentError(
  722. 'Property "path" must be a non-empty String, but %v was given.',
  723. operationDef.path
  724. );
  725. }
  726. if (!operationDef.method || typeof operationDef.method !== "string") {
  727. throw new import_js_format4.InvalidArgumentError(
  728. 'Property "method" must be a non-empty String, but %v was given.',
  729. operationDef.method
  730. );
  731. }
  732. if (!Object.values(OAOperationMethod).includes(operationDef.method)) {
  733. throw new import_js_format4.InvalidArgumentError(
  734. 'Property "method" must be one of values: %l, but %v was given.',
  735. Object.values(OAOperationMethod)
  736. );
  737. }
  738. if (!operationDef.operation || typeof operationDef.operation !== "object" || Array.isArray(operationDef.operation)) {
  739. throw new import_js_format4.InvalidArgumentError(
  740. 'Property "operation" must be an Object, but %v was given.',
  741. operationDef.operation
  742. );
  743. }
  744. const fullPath = joinPath(this._pathPrefix, operationDef.path);
  745. const operation = structuredClone(operationDef.operation);
  746. if (this._tags.length > 0) {
  747. operation.tags = [...this._tags, ...operation.tags || []];
  748. operation.tags = [...new Set(operation.tags)];
  749. }
  750. this._builder.defineOperation({
  751. ...operationDef,
  752. path: fullPath,
  753. operation
  754. });
  755. return this;
  756. }
  757. /**
  758. * Create scope.
  759. *
  760. * @param {object} [options]
  761. * @returns {OADocumentScope}
  762. */
  763. createScope(options = {}) {
  764. if (!options || typeof options !== "object" || Array.isArray(options)) {
  765. throw new import_js_format4.InvalidArgumentError(
  766. 'Parameter "options" must be an Object, but %v was given.',
  767. options
  768. );
  769. }
  770. if (options.pathPrefix !== void 0) {
  771. if (!options.pathPrefix || typeof options.pathPrefix !== "string") {
  772. throw new import_js_format4.InvalidArgumentError(
  773. 'Property "pathPrefix" must be a non-empty String, but %v was given.',
  774. options.pathPrefix
  775. );
  776. }
  777. }
  778. if (options.tags !== void 0) {
  779. if (!Array.isArray(options.tags)) {
  780. throw new import_js_format4.InvalidArgumentError(
  781. 'Property "tags" must be an Array, but %v was given.',
  782. options.tags
  783. );
  784. }
  785. options.tags.forEach((tag, index) => {
  786. if (!tag || typeof tag !== "string") {
  787. throw new import_js_format4.InvalidArgumentError(
  788. 'Element "tags[%d]" must be a non-empty String, but %v was given.',
  789. index,
  790. tag
  791. );
  792. }
  793. });
  794. }
  795. return new _OADocumentScope(this._builder, {
  796. pathPrefix: joinPath(this._pathPrefix, options.pathPrefix),
  797. tags: [...this._tags, ...options.tags || []]
  798. });
  799. }
  800. };
  801. // Annotate the CommonJS export names for ESM import in node:
  802. 0 && (module.exports = {
  803. OAApiKeyLocation,
  804. OAComponentsSegment,
  805. OADataFormat,
  806. OADataType,
  807. OADocumentBuilder,
  808. OADocumentScope,
  809. OAMediaType,
  810. OAOperationMethod,
  811. OAParameterLocation,
  812. OAParameterStyle,
  813. OASecuritySchemeType,
  814. OPENAPI_VERSION,
  815. oaCallbackRef,
  816. oaExampleRef,
  817. oaLinkRef,
  818. oaParameterRef,
  819. oaPathItemRef,
  820. oaRef,
  821. oaRequestBodyRef,
  822. oaResponseRef,
  823. oaSchemaRef,
  824. oaSecuritySchemeRef
  825. });