index.cjs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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. OAComponentType: () => OAComponentType,
  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. OA_COMPONENT_TYPE_TO_KEY_MAP: () => OA_COMPONENT_TYPE_TO_KEY_MAP,
  35. OPENAPI_VERSION: () => OPENAPI_VERSION,
  36. oaCallbackRef: () => oaCallbackRef,
  37. oaExampleRef: () => oaExampleRef,
  38. oaLinkRef: () => oaLinkRef,
  39. oaParameterRef: () => oaParameterRef,
  40. oaPathItemRef: () => oaPathItemRef,
  41. oaRef: () => oaRef,
  42. oaRequestBodyRef: () => oaRequestBodyRef,
  43. oaResponseRef: () => oaResponseRef,
  44. oaSchemaRef: () => oaSchemaRef,
  45. oaSecuritySchemeRef: () => oaSecuritySchemeRef
  46. });
  47. module.exports = __toCommonJS(index_exports);
  48. // src/constants.js
  49. var OPENAPI_VERSION = "3.1.0";
  50. // src/utils/oa-ref.js
  51. var import_js_format = require("@e22m4u/js-format");
  52. var OAComponentType = {
  53. SCHEMA: "schema",
  54. RESPONSE: "response",
  55. PARAMETER: "parameter",
  56. EXAMPLE: "example",
  57. REQUEST_BODY: "requestBody",
  58. HEADER: "header",
  59. SECURITY_SCHEME: "securityScheme",
  60. LINK: "link",
  61. CALLBACK: "callback",
  62. PATH_ITEM: "pathItem"
  63. };
  64. var OA_COMPONENT_TYPE_TO_KEY_MAP = {
  65. [OAComponentType.SCHEMA]: "schemas",
  66. [OAComponentType.RESPONSE]: "responses",
  67. [OAComponentType.PARAMETER]: "parameters",
  68. [OAComponentType.EXAMPLE]: "examples",
  69. [OAComponentType.REQUEST_BODY]: "requestBodies",
  70. [OAComponentType.HEADER]: "headers",
  71. [OAComponentType.SECURITY_SCHEME]: "securitySchemes",
  72. [OAComponentType.LINK]: "links",
  73. [OAComponentType.CALLBACK]: "callbacks",
  74. [OAComponentType.PATH_ITEM]: "pathItems"
  75. };
  76. function oaRef(name, type) {
  77. if (!name || typeof name !== "string") {
  78. throw new import_js_format.InvalidArgumentError(
  79. 'Parameter "name" must be a non-empty String, but %v was given.',
  80. name
  81. );
  82. }
  83. if (!type || typeof type !== "string") {
  84. throw new import_js_format.InvalidArgumentError(
  85. 'Parameter "type" must be a non-empty String, but %v was given.',
  86. type
  87. );
  88. }
  89. const key = OA_COMPONENT_TYPE_TO_KEY_MAP[type];
  90. if (!key) {
  91. throw new import_js_format.InvalidArgumentError("Component type %v is not supported.", type);
  92. }
  93. return { $ref: `#/components/${key}/${name}` };
  94. }
  95. __name(oaRef, "oaRef");
  96. var oaSchemaRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentType.SCHEMA), "oaSchemaRef");
  97. var oaResponseRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentType.RESPONSE), "oaResponseRef");
  98. var oaParameterRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentType.PARAMETER), "oaParameterRef");
  99. var oaExampleRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentType.EXAMPLE), "oaExampleRef");
  100. var oaRequestBodyRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentType.REQUEST_BODY), "oaRequestBodyRef");
  101. var oaSecuritySchemeRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentType.SECURITY_SCHEME), "oaSecuritySchemeRef");
  102. var oaLinkRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentType.LINK), "oaLinkRef");
  103. var oaCallbackRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentType.CALLBACK), "oaCallbackRef");
  104. var oaPathItemRef = /* @__PURE__ */ __name((name) => oaRef(name, OAComponentType.PATH_ITEM), "oaPathItemRef");
  105. // src/utils/join-path.js
  106. function joinPath(...segments) {
  107. const path = segments.filter(Boolean).map((seg) => seg.replace(/(^\/|\/$)/g, "")).filter(Boolean).join("/");
  108. return "/" + path;
  109. }
  110. __name(joinPath, "joinPath");
  111. // src/oa-document-scope.js
  112. var import_js_format4 = require("@e22m4u/js-format");
  113. // src/oa-document-builder.js
  114. var import_js_format3 = require("@e22m4u/js-format");
  115. // src/document-specification.js
  116. var OAOperationMethod = {
  117. GET: "get",
  118. PUT: "put",
  119. POST: "post",
  120. DELETE: "delete",
  121. OPTIONS: "options",
  122. HEAD: "head",
  123. PATCH: "patch",
  124. TRACE: "trace"
  125. };
  126. var OAParameterLocation = {
  127. QUERY: "query",
  128. HEADER: "header",
  129. PATH: "path",
  130. COOKIE: "cookie"
  131. };
  132. var OAParameterStyle = {
  133. MATRIX: "matrix",
  134. LABEL: "label",
  135. FORM: "form",
  136. SIMPLE: "simple",
  137. SPACE_DELIMITED: "spaceDelimited",
  138. PIPE_DELIMITED: "pipeDelimited",
  139. DEEP_OBJECT: "deepObject"
  140. };
  141. var OADataType = {
  142. STRING: "string",
  143. NUMBER: "number",
  144. INTEGER: "integer",
  145. BOOLEAN: "boolean",
  146. OBJECT: "object",
  147. ARRAY: "array",
  148. NULL: "null"
  149. };
  150. var OADataFormat = {
  151. INT32: "int32",
  152. INT64: "int64",
  153. FLOAT: "float",
  154. DOUBLE: "double",
  155. PASSWORD: "password",
  156. BINARY: "binary"
  157. };
  158. var OAMediaType = {
  159. TEXT_PLAIN: "text/plain",
  160. TEXT_HTML: "text/html",
  161. APPLICATION_XML: "application/xml",
  162. APPLICATION_JSON: "application/json",
  163. MULTIPART_FORM_DATA: "multipart/form-data"
  164. };
  165. var OASecuritySchemeType = {
  166. API_KEY: "apiKey",
  167. HTTP: "http",
  168. MUTUAL_TLS: "mutualTLS",
  169. OAUTH_2: "oauth2",
  170. OPEN_ID_CONNECT: "openIdConnect"
  171. };
  172. var OAApiKeyLocation = {
  173. QUERY: "query",
  174. HEADER: "header",
  175. COOKIE: "cookie"
  176. };
  177. // src/oa-document-builder.js
  178. var import_js_service = require("@e22m4u/js-service");
  179. // src/document-validators.js
  180. var import_js_format2 = require("@e22m4u/js-format");
  181. function validateShallowOADocumentObject(documentObject) {
  182. if (!documentObject || typeof documentObject !== "object" || Array.isArray(documentObject)) {
  183. throw new import_js_format2.InvalidArgumentError(
  184. "OpenAPI Document Object must be an Object, but %v was given.",
  185. documentObject
  186. );
  187. }
  188. if (!documentObject.openapi || typeof documentObject.openapi !== "string") {
  189. throw new import_js_format2.InvalidArgumentError(
  190. 'Property "openapi" must be a non-empty String, but %v was given.',
  191. documentObject.openapi
  192. );
  193. }
  194. if (!documentObject.info || typeof documentObject.info !== "object" || Array.isArray(documentObject.info)) {
  195. throw new import_js_format2.InvalidArgumentError(
  196. 'Property "info" must be an Object, but %v was given.',
  197. documentObject.info
  198. );
  199. }
  200. if (documentObject.jsonSchemaDialect !== void 0) {
  201. if (!documentObject.jsonSchemaDialect || typeof documentObject.jsonSchemaDialect !== "string") {
  202. throw new import_js_format2.InvalidArgumentError(
  203. 'Property "jsonSchemaDialect" must be a non-empty String, but %v was given.',
  204. documentObject.jsonSchemaDialect
  205. );
  206. }
  207. }
  208. if (documentObject.servers !== void 0) {
  209. if (!Array.isArray(documentObject.servers)) {
  210. throw new import_js_format2.InvalidArgumentError(
  211. 'Property "servers" must be an Array, but %v was given.',
  212. documentObject.servers
  213. );
  214. }
  215. documentObject.servers.forEach((serverObject, index) => {
  216. if (!serverObject || typeof serverObject !== "object" || Array.isArray(serverObject)) {
  217. throw new import_js_format2.InvalidArgumentError(
  218. 'Element "servers[%d]" must be an Object, but %v was given.',
  219. index,
  220. serverObject
  221. );
  222. }
  223. });
  224. }
  225. if (documentObject.paths !== void 0) {
  226. if (!documentObject.paths || typeof documentObject.paths !== "object" || Array.isArray(documentObject.paths)) {
  227. throw new import_js_format2.InvalidArgumentError(
  228. 'Property "paths" must be an Object, but %v was given.',
  229. documentObject.paths
  230. );
  231. }
  232. }
  233. if (documentObject.webhooks !== void 0) {
  234. if (!documentObject.webhooks || typeof documentObject.webhooks !== "object" || Array.isArray(documentObject.webhooks)) {
  235. throw new import_js_format2.InvalidArgumentError(
  236. 'Property "webhooks" must be an Object, but %v was given.',
  237. documentObject.webhooks
  238. );
  239. }
  240. }
  241. if (documentObject.components !== void 0) {
  242. if (!documentObject.components || typeof documentObject.components !== "object" || Array.isArray(documentObject.components)) {
  243. throw new import_js_format2.InvalidArgumentError(
  244. 'Property "components" must be an Object, but %v was given.',
  245. documentObject.components
  246. );
  247. }
  248. }
  249. if (documentObject.security !== void 0) {
  250. if (!Array.isArray(documentObject.security)) {
  251. throw new import_js_format2.InvalidArgumentError(
  252. 'Property "security" must be an Array, but %v was given.',
  253. documentObject.security
  254. );
  255. }
  256. documentObject.security.forEach((securityRequirementObject, index) => {
  257. if (!securityRequirementObject || typeof securityRequirementObject !== "object" || Array.isArray(securityRequirementObject)) {
  258. throw new import_js_format2.InvalidArgumentError(
  259. 'Element "security[%d]" must be an Object, but %v was given.',
  260. index,
  261. securityRequirementObject
  262. );
  263. }
  264. });
  265. }
  266. if (documentObject.tags !== void 0) {
  267. if (!Array.isArray(documentObject.tags)) {
  268. throw new import_js_format2.InvalidArgumentError(
  269. 'Property "tags" must be an Array, but %v was given.',
  270. documentObject.tags
  271. );
  272. }
  273. documentObject.tags.forEach((tagObject, index) => {
  274. if (!tagObject || typeof tagObject !== "object" || Array.isArray(tagObject)) {
  275. throw new import_js_format2.InvalidArgumentError(
  276. 'Element "tags[%d]" must be an Object, but %v was given.',
  277. index,
  278. tagObject
  279. );
  280. }
  281. });
  282. }
  283. if (documentObject.externalDocs !== void 0) {
  284. if (!documentObject.externalDocs || typeof documentObject.externalDocs !== "object" || Array.isArray(documentObject.externalDocs)) {
  285. throw new import_js_format2.InvalidArgumentError(
  286. 'Property "externalDocs" must be an Object, but %v was given.',
  287. documentObject.externalDocs
  288. );
  289. }
  290. }
  291. }
  292. __name(validateShallowOADocumentObject, "validateShallowOADocumentObject");
  293. // src/oa-document-builder.js
  294. var _OADocumentBuilder = class _OADocumentBuilder extends import_js_service.Service {
  295. /**
  296. * Document.
  297. *
  298. * @private
  299. * @type {object}
  300. */
  301. _document = {
  302. openapi: OPENAPI_VERSION,
  303. info: {
  304. title: "API Documentation",
  305. version: "0.0.1"
  306. }
  307. };
  308. /**
  309. * Constructor.
  310. *
  311. * @param {object} [containerOrDocument]
  312. * @param {object} [document]
  313. */
  314. constructor(containerOrDocument, document) {
  315. if ((0, import_js_service.isServiceContainer)(containerOrDocument)) {
  316. super(containerOrDocument);
  317. } else {
  318. super();
  319. document = containerOrDocument;
  320. }
  321. if (document !== void 0) {
  322. if (!document || typeof document !== "object" || Array.isArray(document)) {
  323. throw new import_js_format3.InvalidArgumentError(
  324. "OpenAPI Document Object must be an Object, but %v was given.",
  325. document
  326. );
  327. }
  328. document = structuredClone(document);
  329. if (!document.openapi) {
  330. document.openapi = this._document.openapi;
  331. }
  332. if (!document.info) {
  333. document.info = this._document.info;
  334. }
  335. if (!document.info || typeof document.info !== "object" || Array.isArray(document.info)) {
  336. throw new import_js_format3.InvalidArgumentError(
  337. 'Property "info" must be an Object, but %v was given.',
  338. document.info
  339. );
  340. }
  341. if (!document.info.title) {
  342. document.info.title = this._document.info.title;
  343. }
  344. if (!document.info.version) {
  345. document.info.version = this._document.info.version;
  346. }
  347. validateShallowOADocumentObject(document);
  348. this._document = document;
  349. }
  350. }
  351. /**
  352. * Define component.
  353. *
  354. * @param {string} targetKey
  355. * @param {string} componentKey
  356. * @param {string} definitionLabel
  357. * @param {object} definition
  358. * @returns {this}
  359. */
  360. _defineComponent(targetKey, componentKey, definitionLabel, definition) {
  361. if (!targetKey || typeof targetKey !== "string") {
  362. throw new import_js_format3.InvalidArgumentError(
  363. 'Property "targetKey" must be a non-empty String, but %v was given.',
  364. targetKey
  365. );
  366. }
  367. if (!componentKey || typeof componentKey !== "string") {
  368. throw new import_js_format3.InvalidArgumentError(
  369. 'Property "componentKey" must be a non-empty String, but %v was given.',
  370. componentKey
  371. );
  372. }
  373. if (!definitionLabel || typeof definitionLabel !== "string") {
  374. throw new import_js_format3.InvalidArgumentError(
  375. 'Property "definitionLabel" must be a non-empty String, but %v was given.',
  376. definitionLabel
  377. );
  378. }
  379. if (!definition || typeof definition !== "object" || Array.isArray(definition)) {
  380. throw new import_js_format3.InvalidArgumentError(
  381. "%s Definition must be an Object, but %v was given.",
  382. definitionLabel,
  383. definition
  384. );
  385. }
  386. if (!definition.name || typeof definition.name !== "string") {
  387. throw new import_js_format3.InvalidArgumentError(
  388. 'Property "name" must be a non-empty String, but %v was given.',
  389. definition.name
  390. );
  391. }
  392. const component = definition[componentKey];
  393. if (!component || typeof component !== "object" || Array.isArray(component)) {
  394. throw new import_js_format3.InvalidArgumentError(
  395. "Property %v must be an Object, but %v was given.",
  396. componentKey,
  397. component
  398. );
  399. }
  400. if (!this._document.components) {
  401. this._document.components = {};
  402. }
  403. if (!this._document.components[targetKey]) {
  404. this._document.components[targetKey] = {};
  405. }
  406. this._document.components[targetKey][definition.name] = component;
  407. return this;
  408. }
  409. /**
  410. * Define schema component.
  411. *
  412. * @param {import('./oa-document-builder.js').OASchemaComponentDefinition} schemaDef
  413. * @returns {this}
  414. */
  415. defineSchemaComponent(schemaDef) {
  416. return this._defineComponent("schemas", "schema", "Schema", schemaDef);
  417. }
  418. /**
  419. * Define response component.
  420. *
  421. * @param {import('./oa-document-builder.js').OAResponseComponentDefinition} responseDef
  422. * @returns {this}
  423. */
  424. defineResponseComponent(responseDef) {
  425. return this._defineComponent(
  426. "responses",
  427. "response",
  428. "Response",
  429. responseDef
  430. );
  431. }
  432. /**
  433. * Define parameter component.
  434. *
  435. * @param {import('./oa-document-builder.js').OAParameterComponentDefinition} parameterDef
  436. * @returns {this}
  437. */
  438. defineParameterComponent(parameterDef) {
  439. return this._defineComponent(
  440. "parameters",
  441. "parameter",
  442. "Parameter",
  443. parameterDef
  444. );
  445. }
  446. /**
  447. * Define example component.
  448. *
  449. * @param {import('./oa-document-builder.js').OAExampleComponentDefinition} exampleDef
  450. * @returns {this}
  451. */
  452. defineExampleComponent(exampleDef) {
  453. return this._defineComponent("examples", "example", "Example", exampleDef);
  454. }
  455. /**
  456. * Define request body component.
  457. *
  458. * @param {import('./oa-document-builder.js').OARequestBodyComponentDefinition} requestBodyDef
  459. * @returns {this}
  460. */
  461. defineRequestBodyComponent(requestBodyDef) {
  462. return this._defineComponent(
  463. "requestBodies",
  464. "requestBody",
  465. "Request body",
  466. requestBodyDef
  467. );
  468. }
  469. /**
  470. * Define header component.
  471. *
  472. * @param {import('./oa-document-builder.js').OAHeaderComponentDefinition} headerDef
  473. * @returns {this}
  474. */
  475. defineHeaderComponent(headerDef) {
  476. return this._defineComponent("headers", "header", "Header", headerDef);
  477. }
  478. /**
  479. * Define security scheme component.
  480. *
  481. * @param {import('./oa-document-builder.js').OASecuritySchemeComponentDefinition} securitySchemeDef
  482. * @returns {this}
  483. */
  484. defineSecuritySchemeComponent(securitySchemeDef) {
  485. return this._defineComponent(
  486. "securitySchemes",
  487. "securityScheme",
  488. "Security Scheme",
  489. securitySchemeDef
  490. );
  491. }
  492. /**
  493. * Define link component.
  494. *
  495. * @param {import('./oa-document-builder.js').OALinkComponentDefinition} linkDef
  496. * @returns {this}
  497. */
  498. defineLinkComponent(linkDef) {
  499. return this._defineComponent("links", "link", "Link", linkDef);
  500. }
  501. /**
  502. * Define callback component.
  503. *
  504. * @param {import('./oa-document-builder.js').OACallbackComponentDefinition} callbackDef
  505. * @returns {this}
  506. */
  507. defineCallbackComponent(callbackDef) {
  508. return this._defineComponent(
  509. "callbacks",
  510. "callback",
  511. "Callback",
  512. callbackDef
  513. );
  514. }
  515. /**
  516. * Define path item component.
  517. *
  518. * @param {import('./oa-document-builder.js').OAPathItemComponentDefinition} pathItemDef
  519. * @returns {this}
  520. */
  521. definePathItemComponent(pathItemDef) {
  522. return this._defineComponent(
  523. "pathItems",
  524. "pathItem",
  525. "Path Item",
  526. pathItemDef
  527. );
  528. }
  529. /**
  530. * Define operation.
  531. *
  532. * @param {object} operationDef
  533. */
  534. defineOperation(operationDef) {
  535. if (!operationDef || typeof operationDef !== "object" || Array.isArray(operationDef)) {
  536. throw new import_js_format3.InvalidArgumentError(
  537. "Operation Definition must be an Object, but %v was given.",
  538. operationDef
  539. );
  540. }
  541. if (!operationDef.path || typeof operationDef.path !== "string") {
  542. throw new import_js_format3.InvalidArgumentError(
  543. 'Property "path" must be a non-empty String, but %v was given.',
  544. operationDef.path
  545. );
  546. }
  547. if (operationDef.path[0] !== "/") {
  548. throw new import_js_format3.InvalidArgumentError(
  549. 'Property "path" must start with forward slash "/", but %v was given.',
  550. operationDef.path
  551. );
  552. }
  553. if (!operationDef.method || typeof operationDef.method !== "string") {
  554. throw new import_js_format3.InvalidArgumentError(
  555. 'Property "method" must be a non-empty String, but %v was given.',
  556. operationDef.method
  557. );
  558. }
  559. if (!Object.values(OAOperationMethod).includes(operationDef.method)) {
  560. throw new import_js_format3.InvalidArgumentError(
  561. 'Property "method" must be one of values: %l, but %v was given.',
  562. Object.values(OAOperationMethod)
  563. );
  564. }
  565. if (!operationDef.operation || typeof operationDef.operation !== "object" || Array.isArray(operationDef.operation)) {
  566. throw new import_js_format3.InvalidArgumentError(
  567. 'Property "operation" must be an Object, but %v was given.',
  568. operationDef.operation
  569. );
  570. }
  571. if (!this._document.paths) {
  572. this._document.paths = {};
  573. }
  574. if (!this._document.paths[operationDef.path]) {
  575. this._document.paths[operationDef.path] = {};
  576. }
  577. this._document.paths[operationDef.path][operationDef.method] = structuredClone(operationDef.operation);
  578. }
  579. /**
  580. * Create scope.
  581. *
  582. * @param {object} [options]
  583. * @returns {OADocumentScope}
  584. */
  585. createScope(options) {
  586. return new OADocumentScope(this, options);
  587. }
  588. /**
  589. * Build.
  590. *
  591. * @returns {object}
  592. */
  593. build() {
  594. return structuredClone(this._document);
  595. }
  596. };
  597. __name(_OADocumentBuilder, "OADocumentBuilder");
  598. var OADocumentBuilder = _OADocumentBuilder;
  599. // src/oa-document-scope.js
  600. var _OADocumentScope = class _OADocumentScope {
  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. * Build.
  748. *
  749. * @returns {object}
  750. */
  751. build() {
  752. return this.rootBuilder.build();
  753. }
  754. };
  755. __name(_OADocumentScope, "OADocumentScope");
  756. var OADocumentScope = _OADocumentScope;
  757. // Annotate the CommonJS export names for ESM import in node:
  758. 0 && (module.exports = {
  759. OAApiKeyLocation,
  760. OAComponentType,
  761. OADataFormat,
  762. OADataType,
  763. OADocumentBuilder,
  764. OADocumentScope,
  765. OAMediaType,
  766. OAOperationMethod,
  767. OAParameterLocation,
  768. OAParameterStyle,
  769. OASecuritySchemeType,
  770. OA_COMPONENT_TYPE_TO_KEY_MAP,
  771. OPENAPI_VERSION,
  772. oaCallbackRef,
  773. oaExampleRef,
  774. oaLinkRef,
  775. oaParameterRef,
  776. oaPathItemRef,
  777. oaRef,
  778. oaRequestBodyRef,
  779. oaResponseRef,
  780. oaSchemaRef,
  781. oaSecuritySchemeRef
  782. });