index.cjs 24 KB

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