index.cjs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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.jsonSchemaDialect !== void 0) {
  185. if (!documentObject.jsonSchemaDialect || typeof documentObject.jsonSchemaDialect !== "string") {
  186. throw new import_js_format2.InvalidArgumentError(
  187. 'Property "jsonSchemaDialect" must be a non-empty String, but %v was given.',
  188. documentObject.jsonSchemaDialect
  189. );
  190. }
  191. }
  192. if (documentObject.servers !== void 0) {
  193. if (!Array.isArray(documentObject.servers)) {
  194. throw new import_js_format2.InvalidArgumentError(
  195. 'Property "servers" must be an Array, but %v was given.',
  196. documentObject.servers
  197. );
  198. }
  199. documentObject.servers.forEach((serverObject, index) => {
  200. if (!serverObject || typeof serverObject !== "object" || Array.isArray(serverObject)) {
  201. throw new import_js_format2.InvalidArgumentError(
  202. 'Element "servers[%d]" must be an Object, but %v was given.',
  203. index,
  204. serverObject
  205. );
  206. }
  207. });
  208. }
  209. if (documentObject.paths !== void 0) {
  210. if (!documentObject.paths || typeof documentObject.paths !== "object" || Array.isArray(documentObject.paths)) {
  211. throw new import_js_format2.InvalidArgumentError(
  212. 'Property "paths" must be an Object, but %v was given.',
  213. documentObject.paths
  214. );
  215. }
  216. }
  217. if (documentObject.webhooks !== void 0) {
  218. if (!documentObject.webhooks || typeof documentObject.webhooks !== "object" || Array.isArray(documentObject.webhooks)) {
  219. throw new import_js_format2.InvalidArgumentError(
  220. 'Property "webhooks" must be an Object, but %v was given.',
  221. documentObject.webhooks
  222. );
  223. }
  224. }
  225. if (documentObject.components !== void 0) {
  226. if (!documentObject.components || typeof documentObject.components !== "object" || Array.isArray(documentObject.components)) {
  227. throw new import_js_format2.InvalidArgumentError(
  228. 'Property "components" must be an Object, but %v was given.',
  229. documentObject.components
  230. );
  231. }
  232. }
  233. if (documentObject.security !== void 0) {
  234. if (!Array.isArray(documentObject.security)) {
  235. throw new import_js_format2.InvalidArgumentError(
  236. 'Property "security" must be an Array, but %v was given.',
  237. documentObject.security
  238. );
  239. }
  240. documentObject.security.forEach((securityRequirementObject, index) => {
  241. if (!securityRequirementObject || typeof securityRequirementObject !== "object" || Array.isArray(securityRequirementObject)) {
  242. throw new import_js_format2.InvalidArgumentError(
  243. 'Element "security[%d]" must be an Object, but %v was given.',
  244. index,
  245. securityRequirementObject
  246. );
  247. }
  248. });
  249. }
  250. if (documentObject.tags !== void 0) {
  251. if (!Array.isArray(documentObject.tags)) {
  252. throw new import_js_format2.InvalidArgumentError(
  253. 'Property "tags" must be an Array, but %v was given.',
  254. documentObject.tags
  255. );
  256. }
  257. documentObject.tags.forEach((tagObject, index) => {
  258. if (!tagObject || typeof tagObject !== "object" || Array.isArray(tagObject)) {
  259. throw new import_js_format2.InvalidArgumentError(
  260. 'Element "tags[%d]" must be an Object, but %v was given.',
  261. index,
  262. tagObject
  263. );
  264. }
  265. });
  266. }
  267. if (documentObject.externalDocs !== void 0) {
  268. if (!documentObject.externalDocs || typeof documentObject.externalDocs !== "object" || Array.isArray(documentObject.externalDocs)) {
  269. throw new import_js_format2.InvalidArgumentError(
  270. 'Property "externalDocs" must be an Object, but %v was given.',
  271. documentObject.externalDocs
  272. );
  273. }
  274. }
  275. }
  276. __name(validateShallowOADocumentObject, "validateShallowOADocumentObject");
  277. // src/oa-document-builder.js
  278. var OADocumentBuilder = class extends import_js_service.Service {
  279. static {
  280. __name(this, "OADocumentBuilder");
  281. }
  282. /**
  283. * Document.
  284. *
  285. * @private
  286. * @type {object}
  287. */
  288. _document = {
  289. openapi: OPENAPI_VERSION,
  290. info: {
  291. title: "API Documentation",
  292. version: "0.0.1"
  293. }
  294. };
  295. /**
  296. * Constructor.
  297. *
  298. * @param {object} [containerOrDocument]
  299. * @param {object} [document]
  300. */
  301. constructor(containerOrDocument, document) {
  302. if ((0, import_js_service.isServiceContainer)(containerOrDocument)) {
  303. super(containerOrDocument);
  304. } else {
  305. super();
  306. document = containerOrDocument;
  307. }
  308. if (document !== void 0) {
  309. if (!document || typeof document !== "object" || Array.isArray(document)) {
  310. throw new import_js_format3.InvalidArgumentError(
  311. "OpenAPI Document Object must be an Object, but %v was given.",
  312. document
  313. );
  314. }
  315. document = structuredClone(document);
  316. if (!document.openapi) {
  317. document.openapi = this._document.openapi;
  318. }
  319. if (!document.info) {
  320. document.info = this._document.info;
  321. }
  322. if (!document.info || typeof document.info !== "object" || Array.isArray(document.info)) {
  323. throw new import_js_format3.InvalidArgumentError(
  324. 'Property "info" must be an Object, but %v was given.',
  325. document.info
  326. );
  327. }
  328. if (!document.info.title) {
  329. document.info.title = this._document.info.title;
  330. }
  331. if (!document.info.version) {
  332. document.info.version = this._document.info.version;
  333. }
  334. validateShallowOADocumentObject(document);
  335. this._document = document;
  336. }
  337. }
  338. /**
  339. * Define component.
  340. *
  341. * @param {string} segmentName
  342. * @param {string} propertyName
  343. * @param {object} definition
  344. * @returns {this}
  345. */
  346. _defineComponent(segmentName, propertyName, definition) {
  347. if (!Object.values(OAComponentsSegment).includes(segmentName)) {
  348. throw new import_js_format3.InvalidArgumentError(
  349. 'Property "segmentName" must be one of values: %l, but %v was given.',
  350. Object.values(OAComponentsSegment),
  351. segmentName
  352. );
  353. }
  354. if (!propertyName || typeof propertyName !== "string") {
  355. throw new import_js_format3.InvalidArgumentError(
  356. 'Property "propertyName" must be a non-empty String, but %v was given.',
  357. propertyName
  358. );
  359. }
  360. if (!definition || typeof definition !== "object" || Array.isArray(definition)) {
  361. throw new import_js_format3.InvalidArgumentError(
  362. "Component definition must be an Object, but %v was given.",
  363. definition
  364. );
  365. }
  366. if (!definition.name || typeof definition.name !== "string") {
  367. throw new import_js_format3.InvalidArgumentError(
  368. 'Property "name" must be a non-empty String, but %v was given.',
  369. definition.name
  370. );
  371. }
  372. const component = definition[propertyName];
  373. if (!component || typeof component !== "object" || Array.isArray(component)) {
  374. throw new import_js_format3.InvalidArgumentError(
  375. "Property %v must be an Object, but %v was given.",
  376. propertyName,
  377. component
  378. );
  379. }
  380. if (!this._document.components) {
  381. this._document.components = {};
  382. }
  383. if (!this._document.components[segmentName]) {
  384. this._document.components[segmentName] = {};
  385. }
  386. this._document.components[segmentName][definition.name] = structuredClone(component);
  387. return this;
  388. }
  389. /**
  390. * Define schema component.
  391. *
  392. * @param {object} schemaDef
  393. * @returns {this}
  394. */
  395. defineSchemaComponent(schemaDef) {
  396. return this._defineComponent(
  397. OAComponentsSegment.SCHEMAS,
  398. "schema",
  399. schemaDef
  400. );
  401. }
  402. /**
  403. * Define response component.
  404. *
  405. * @param {object} responseDef
  406. * @returns {this}
  407. */
  408. defineResponseComponent(responseDef) {
  409. return this._defineComponent(
  410. OAComponentsSegment.RESPONSES,
  411. "response",
  412. responseDef
  413. );
  414. }
  415. /**
  416. * Define parameter component.
  417. *
  418. * @param {object} parameterDef
  419. * @returns {this}
  420. */
  421. defineParameterComponent(parameterDef) {
  422. return this._defineComponent(
  423. OAComponentsSegment.PARAMETERS,
  424. "parameter",
  425. parameterDef
  426. );
  427. }
  428. /**
  429. * Define example component.
  430. *
  431. * @param {object} exampleDef
  432. * @returns {this}
  433. */
  434. defineExampleComponent(exampleDef) {
  435. return this._defineComponent(
  436. OAComponentsSegment.EXAMPLES,
  437. "example",
  438. exampleDef
  439. );
  440. }
  441. /**
  442. * Define request body component.
  443. *
  444. * @param {object} requestBodyDef
  445. * @returns {this}
  446. */
  447. defineRequestBodyComponent(requestBodyDef) {
  448. return this._defineComponent(
  449. OAComponentsSegment.REQUEST_BODIES,
  450. "requestBody",
  451. requestBodyDef
  452. );
  453. }
  454. /**
  455. * Define header component.
  456. *
  457. * @param {object} headerDef
  458. * @returns {this}
  459. */
  460. defineHeaderComponent(headerDef) {
  461. return this._defineComponent(
  462. OAComponentsSegment.HEADERS,
  463. "header",
  464. headerDef
  465. );
  466. }
  467. /**
  468. * Define security scheme component.
  469. *
  470. * @param {object} securitySchemeDef
  471. * @returns {this}
  472. */
  473. defineSecuritySchemeComponent(securitySchemeDef) {
  474. return this._defineComponent(
  475. OAComponentsSegment.SECURITY_SCHEMES,
  476. "securityScheme",
  477. securitySchemeDef
  478. );
  479. }
  480. /**
  481. * Define link component.
  482. *
  483. * @param {object} linkDef
  484. * @returns {this}
  485. */
  486. defineLinkComponent(linkDef) {
  487. return this._defineComponent(
  488. OAComponentsSegment.LINKS,
  489. "link",
  490. linkDef
  491. );
  492. }
  493. /**
  494. * Define callback component.
  495. *
  496. * @param {object} callbackDef
  497. * @returns {this}
  498. */
  499. defineCallbackComponent(callbackDef) {
  500. return this._defineComponent(
  501. OAComponentsSegment.CALLBACKS,
  502. "callback",
  503. callbackDef
  504. );
  505. }
  506. /**
  507. * Define path item component.
  508. *
  509. * @param {object} pathItemDef
  510. * @returns {this}
  511. */
  512. definePathItemComponent(pathItemDef) {
  513. return this._defineComponent(
  514. OAComponentsSegment.PATH_ITEMS,
  515. "pathItem",
  516. pathItemDef
  517. );
  518. }
  519. /**
  520. * Define operation.
  521. *
  522. * @param {object} operationDef
  523. */
  524. defineOperation(operationDef) {
  525. if (!operationDef || typeof operationDef !== "object" || Array.isArray(operationDef)) {
  526. throw new import_js_format3.InvalidArgumentError(
  527. "Operation Definition must be an Object, but %v was given.",
  528. operationDef
  529. );
  530. }
  531. if (!operationDef.path || typeof operationDef.path !== "string") {
  532. throw new import_js_format3.InvalidArgumentError(
  533. 'Property "path" must be a non-empty String, but %v was given.',
  534. operationDef.path
  535. );
  536. }
  537. if (operationDef.path[0] !== "/") {
  538. throw new import_js_format3.InvalidArgumentError(
  539. 'Property "path" must start with forward slash "/", but %v was given.',
  540. operationDef.path
  541. );
  542. }
  543. if (!operationDef.method || typeof operationDef.method !== "string") {
  544. throw new import_js_format3.InvalidArgumentError(
  545. 'Property "method" must be a non-empty String, but %v was given.',
  546. operationDef.method
  547. );
  548. }
  549. if (!Object.values(OAOperationMethod).includes(operationDef.method)) {
  550. throw new import_js_format3.InvalidArgumentError(
  551. 'Property "method" must be one of values: %l, but %v was given.',
  552. Object.values(OAOperationMethod)
  553. );
  554. }
  555. if (!operationDef.operation || typeof operationDef.operation !== "object" || Array.isArray(operationDef.operation)) {
  556. throw new import_js_format3.InvalidArgumentError(
  557. 'Property "operation" must be an Object, but %v was given.',
  558. operationDef.operation
  559. );
  560. }
  561. if (!this._document.paths) {
  562. this._document.paths = {};
  563. }
  564. if (!this._document.paths[operationDef.path]) {
  565. this._document.paths[operationDef.path] = {};
  566. }
  567. this._document.paths[operationDef.path][operationDef.method] = structuredClone(operationDef.operation);
  568. }
  569. /**
  570. * Create scope.
  571. *
  572. * @param {object} [options]
  573. * @returns {OADocumentScope}
  574. */
  575. createScope(options) {
  576. return new OADocumentScope(this, options);
  577. }
  578. /**
  579. * Build.
  580. *
  581. * @returns {object}
  582. */
  583. build() {
  584. return structuredClone(this._document);
  585. }
  586. /**
  587. * Build JSON.
  588. *
  589. * @param {string|number} [space]
  590. * @returns {string}
  591. */
  592. buildJson(space = 0) {
  593. return JSON.stringify(this._document, null, space);
  594. }
  595. };
  596. // src/oa-document-scope.js
  597. var OADocumentScope = class _OADocumentScope {
  598. static {
  599. __name(this, "OADocumentScope");
  600. }
  601. /**
  602. * @param {object} rootBuilder
  603. * @param {object} [options]
  604. */
  605. constructor(rootBuilder, options = {}) {
  606. if (!(rootBuilder instanceof OADocumentBuilder)) {
  607. throw new import_js_format4.InvalidArgumentError(
  608. 'Parameter "rootBuilder" must be an instance of OADocumentBuilder, but %v was given.',
  609. rootBuilder
  610. );
  611. }
  612. if (!options || typeof options !== "object" || Array.isArray(options)) {
  613. throw new import_js_format4.InvalidArgumentError(
  614. 'Parameter "options" must be an Object, but %v was given.',
  615. options
  616. );
  617. }
  618. if (options.pathPrefix !== void 0) {
  619. if (!options.pathPrefix || typeof options.pathPrefix !== "string") {
  620. throw new import_js_format4.InvalidArgumentError(
  621. 'Parameter "pathPrefix" must be a non-empty String, but %v was given.',
  622. options.pathPrefix
  623. );
  624. }
  625. }
  626. if (options.tags !== void 0) {
  627. if (!Array.isArray(options.tags)) {
  628. throw new import_js_format4.InvalidArgumentError(
  629. 'Parameter "tags" must be an Array, but %v was given.',
  630. options.tags
  631. );
  632. }
  633. options.tags.forEach((tag, index) => {
  634. if (!tag || typeof tag !== "string") {
  635. throw new import_js_format4.InvalidArgumentError(
  636. 'Element "tags[%d]" must be a non-empty String, but %v was given.',
  637. index,
  638. tag
  639. );
  640. }
  641. });
  642. }
  643. this.rootBuilder = rootBuilder;
  644. this.pathPrefix = options.pathPrefix || "/";
  645. this.tags = options.tags || [];
  646. }
  647. /**
  648. * Define operation.
  649. *
  650. * @param {object} operationDef
  651. * @returns {this}
  652. */
  653. defineOperation(operationDef) {
  654. if (!operationDef || typeof operationDef !== "object" || Array.isArray(operationDef)) {
  655. throw new import_js_format4.InvalidArgumentError(
  656. "Operation Definition must be an Object, but %v was given.",
  657. operationDef
  658. );
  659. }
  660. if (!operationDef.path || typeof operationDef.path !== "string") {
  661. throw new import_js_format4.InvalidArgumentError(
  662. 'Property "path" must be a non-empty String, but %v was given.',
  663. operationDef.path
  664. );
  665. }
  666. if (operationDef.path[0] !== "/") {
  667. throw new import_js_format4.InvalidArgumentError(
  668. 'Property "path" must start with forward slash "/", but %v was given.',
  669. operationDef.path
  670. );
  671. }
  672. if (!operationDef.method || typeof operationDef.method !== "string") {
  673. throw new import_js_format4.InvalidArgumentError(
  674. 'Property "method" must be a non-empty String, but %v was given.',
  675. operationDef.method
  676. );
  677. }
  678. if (!Object.values(OAOperationMethod).includes(operationDef.method)) {
  679. throw new import_js_format4.InvalidArgumentError(
  680. 'Property "method" must be one of values: %l, but %v was given.',
  681. Object.values(OAOperationMethod)
  682. );
  683. }
  684. if (!operationDef.operation || typeof operationDef.operation !== "object" || Array.isArray(operationDef.operation)) {
  685. throw new import_js_format4.InvalidArgumentError(
  686. 'Property "operation" must be an Object, but %v was given.',
  687. operationDef.operation
  688. );
  689. }
  690. const fullPath = joinPath(this.pathPrefix, operationDef.path);
  691. const operation = structuredClone(operationDef.operation);
  692. if (this.tags.length > 0) {
  693. operation.tags = [...this.tags, ...operation.tags || []];
  694. operation.tags = [...new Set(operation.tags)];
  695. }
  696. this.rootBuilder.defineOperation({
  697. ...operationDef,
  698. path: fullPath,
  699. operation
  700. });
  701. return this;
  702. }
  703. /**
  704. * Create scope.
  705. *
  706. * @param {object} [options]
  707. * @returns {OADocumentScope}
  708. */
  709. createScope(options = {}) {
  710. if (!options || typeof options !== "object" || Array.isArray(options)) {
  711. throw new import_js_format4.InvalidArgumentError(
  712. 'Parameter "options" must be an Object, but %v was given.',
  713. options
  714. );
  715. }
  716. if (options.pathPrefix !== void 0) {
  717. if (!options.pathPrefix || typeof options.pathPrefix !== "string") {
  718. throw new import_js_format4.InvalidArgumentError(
  719. 'Parameter "pathPrefix" must be a non-empty String, but %v was given.',
  720. options.pathPrefix
  721. );
  722. }
  723. }
  724. if (options.tags !== void 0) {
  725. if (!Array.isArray(options.tags)) {
  726. throw new import_js_format4.InvalidArgumentError(
  727. 'Parameter "tags" must be an Array, but %v was given.',
  728. options.tags
  729. );
  730. }
  731. options.tags.forEach((tag, index) => {
  732. if (!tag || typeof tag !== "string") {
  733. throw new import_js_format4.InvalidArgumentError(
  734. 'Element "tags[%d]" must be a non-empty String, but %v was given.',
  735. index,
  736. tag
  737. );
  738. }
  739. });
  740. }
  741. return new _OADocumentScope(this.rootBuilder, {
  742. pathPrefix: joinPath(this.pathPrefix, options.pathPrefix),
  743. tags: [...this.tags, ...options.tags || []]
  744. });
  745. }
  746. };
  747. // Annotate the CommonJS export names for ESM import in node:
  748. 0 && (module.exports = {
  749. OAApiKeyLocation,
  750. OAComponentsSegment,
  751. OADataFormat,
  752. OADataType,
  753. OADocumentBuilder,
  754. OADocumentScope,
  755. OAMediaType,
  756. OAOperationMethod,
  757. OAParameterLocation,
  758. OAParameterStyle,
  759. OASecuritySchemeType,
  760. OPENAPI_VERSION,
  761. oaCallbackRef,
  762. oaExampleRef,
  763. oaLinkRef,
  764. oaParameterRef,
  765. oaPathItemRef,
  766. oaRef,
  767. oaRequestBodyRef,
  768. oaResponseRef,
  769. oaSchemaRef,
  770. oaSecuritySchemeRef
  771. });