@redocly/openapi-core 1.18.1 → 1.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/lib/benchmark/benches/lint-with-many-rules.bench.js +2 -2
  3. package/lib/benchmark/benches/lint-with-nested-rule.bench.js +2 -2
  4. package/lib/benchmark/benches/lint-with-no-rules.bench.js +2 -2
  5. package/lib/benchmark/benches/lint-with-top-level-rule-report.bench.js +2 -2
  6. package/lib/benchmark/benches/lint-with-top-level-rule.bench.js +2 -2
  7. package/lib/benchmark/benches/recommended-oas3.bench.js +2 -2
  8. package/lib/benchmark/benches/resolve-with-no-external.bench.js +2 -2
  9. package/lib/benchmark/utils.js +7 -4
  10. package/lib/bundle.d.ts +2 -2
  11. package/lib/bundle.js +127 -120
  12. package/lib/config/all.js +9 -0
  13. package/lib/config/builtIn.js +7 -1
  14. package/lib/config/config-resolvers.js +179 -138
  15. package/lib/config/config.d.ts +2 -2
  16. package/lib/config/config.js +53 -34
  17. package/lib/config/load.js +105 -117
  18. package/lib/config/minimal.js +9 -0
  19. package/lib/config/recommended-strict.js +9 -0
  20. package/lib/config/recommended.js +9 -0
  21. package/lib/config/rules.d.ts +3 -3
  22. package/lib/config/rules.js +1 -2
  23. package/lib/config/types.d.ts +9 -3
  24. package/lib/config/utils.js +70 -49
  25. package/lib/decorators/async3/index.d.ts +1 -0
  26. package/lib/decorators/async3/index.js +4 -0
  27. package/lib/decorators/common/filters/filter-helper.js +2 -3
  28. package/lib/decorators/common/filters/filter-in.js +1 -1
  29. package/lib/decorators/common/filters/filter-out.js +1 -1
  30. package/lib/decorators/common/info-override.js +1 -12
  31. package/lib/decorators/common/media-type-examples-override.js +8 -2
  32. package/lib/decorators/common/remove-x-internal.js +4 -5
  33. package/lib/decorators/oas2/remove-unused-components.js +1 -2
  34. package/lib/decorators/oas3/remove-unused-components.js +1 -2
  35. package/lib/env.d.ts +0 -1
  36. package/lib/env.js +1 -1
  37. package/lib/format/codeframes.js +10 -8
  38. package/lib/format/format.js +23 -15
  39. package/lib/index.d.ts +2 -1
  40. package/lib/index.js +6 -4
  41. package/lib/js-yaml/index.js +1 -1
  42. package/lib/lint.d.ts +2 -0
  43. package/lib/lint.js +92 -99
  44. package/lib/oas-types.d.ts +9 -5
  45. package/lib/oas-types.js +22 -12
  46. package/lib/redocly/domains.js +6 -6
  47. package/lib/redocly/index.js +60 -73
  48. package/lib/redocly/registry-api.js +64 -82
  49. package/lib/ref-utils.js +13 -13
  50. package/lib/resolve.js +186 -205
  51. package/lib/rules/ajv.js +10 -8
  52. package/lib/rules/async3/channels-kebab-case.d.ts +2 -0
  53. package/lib/rules/async3/channels-kebab-case.js +19 -0
  54. package/lib/rules/async3/index.d.ts +3 -0
  55. package/lib/rules/async3/index.js +22 -0
  56. package/lib/rules/async3/no-channel-trailing-slash.d.ts +2 -0
  57. package/lib/rules/async3/no-channel-trailing-slash.js +16 -0
  58. package/lib/rules/common/assertions/asserts.js +5 -5
  59. package/lib/rules/common/assertions/index.d.ts +5 -4
  60. package/lib/rules/common/assertions/utils.js +43 -28
  61. package/lib/rules/common/no-invalid-parameter-examples.js +1 -2
  62. package/lib/rules/common/no-invalid-schema-examples.js +1 -2
  63. package/lib/rules/common/no-required-schema-properties-undefined.js +1 -2
  64. package/lib/rules/common/operation-tag-defined.js +1 -2
  65. package/lib/rules/common/path-http-verbs-order.js +1 -1
  66. package/lib/rules/common/required-string-property-missing-min-length.js +2 -2
  67. package/lib/rules/common/response-contains-header.js +2 -2
  68. package/lib/rules/common/security-defined.js +3 -7
  69. package/lib/rules/common/spec.d.ts +2 -2
  70. package/lib/rules/common/spec.js +6 -7
  71. package/lib/rules/no-unresolved-refs.js +3 -4
  72. package/lib/rules/oas2/response-contains-property.js +1 -2
  73. package/lib/rules/oas3/array-parameter-serialization.js +1 -2
  74. package/lib/rules/oas3/component-name-unique.js +2 -4
  75. package/lib/rules/oas3/no-invalid-media-type-examples.js +1 -2
  76. package/lib/rules/oas3/no-server-variables-empty-enum.js +1 -2
  77. package/lib/rules/oas3/no-undefined-server-variable.js +2 -3
  78. package/lib/rules/oas3/no-unused-components.js +1 -2
  79. package/lib/rules/oas3/response-contains-property.js +1 -2
  80. package/lib/rules/utils.js +14 -12
  81. package/lib/types/asyncapi2.d.ts +17 -0
  82. package/lib/types/{asyncapi.js → asyncapi2.js} +56 -52
  83. package/lib/types/asyncapi3.d.ts +2 -0
  84. package/lib/types/asyncapi3.js +347 -0
  85. package/lib/types/index.js +19 -10
  86. package/lib/types/json-schema-adapter.js +4 -18
  87. package/lib/types/oas2.js +6 -6
  88. package/lib/types/oas3.js +10 -10
  89. package/lib/types/oas3_1.js +14 -8
  90. package/lib/types/redocly-yaml.d.ts +3 -1
  91. package/lib/types/redocly-yaml.js +131 -35
  92. package/lib/typings/asyncapi3.d.ts +53 -0
  93. package/lib/typings/asyncapi3.js +2 -0
  94. package/lib/utils.d.ts +4 -3
  95. package/lib/utils.js +55 -72
  96. package/lib/visitors.d.ts +11 -0
  97. package/lib/visitors.js +21 -8
  98. package/lib/walk.js +30 -23
  99. package/package.json +2 -2
  100. package/src/__tests__/bundle.test.ts +142 -0
  101. package/src/bundle.ts +17 -3
  102. package/src/config/__tests__/__snapshots__/config-resolvers.test.ts.snap +22 -0
  103. package/src/config/__tests__/__snapshots__/config.test.ts.snap +24 -0
  104. package/src/config/__tests__/config.test.ts +11 -0
  105. package/src/config/all.ts +9 -0
  106. package/src/config/builtIn.ts +6 -0
  107. package/src/config/config-resolvers.ts +15 -2
  108. package/src/config/config.ts +24 -5
  109. package/src/config/minimal.ts +9 -0
  110. package/src/config/recommended-strict.ts +9 -0
  111. package/src/config/recommended.ts +9 -0
  112. package/src/config/rules.ts +12 -4
  113. package/src/config/types.ts +15 -2
  114. package/src/config/utils.ts +15 -0
  115. package/src/decorators/async3/index.ts +1 -0
  116. package/src/decorators/common/remove-x-internal.ts +2 -2
  117. package/src/index.ts +2 -1
  118. package/src/lint.ts +26 -3
  119. package/src/oas-types.ts +31 -13
  120. package/src/rules/arazzo/index.ts +1 -1
  121. package/src/rules/async2/index.ts +5 -5
  122. package/src/rules/async3/__tests__/channels-kebab-case.test.ts +141 -0
  123. package/src/rules/async3/__tests__/no-channel-trailing-slash.test.ts +96 -0
  124. package/src/rules/async3/channels-kebab-case.ts +19 -0
  125. package/src/rules/async3/index.ts +23 -0
  126. package/src/rules/async3/no-channel-trailing-slash.ts +16 -0
  127. package/src/rules/common/assertions/index.ts +13 -4
  128. package/src/rules/common/spec.ts +2 -2
  129. package/src/rules/oas2/index.ts +4 -4
  130. package/src/rules/oas3/index.ts +39 -37
  131. package/src/types/{asyncapi.ts → asyncapi2.ts} +37 -34
  132. package/src/types/asyncapi3.ts +381 -0
  133. package/src/types/oas3_1.ts +2 -1
  134. package/src/types/redocly-yaml.ts +14 -0
  135. package/src/typings/asyncapi3.ts +61 -0
  136. package/src/utils.ts +5 -3
  137. package/src/visitors.ts +18 -0
  138. package/tsconfig.tsbuildinfo +1 -1
  139. package/lib/types/asyncapi.d.ts +0 -2
@@ -9,7 +9,7 @@ const DEFAULT_SCHEMA_WITHOUT_TIMESTAMP = js_yaml_1.JSON_SCHEMA.extend({
9
9
  implicit: [js_yaml_1.types.merge],
10
10
  explicit: [js_yaml_1.types.binary, js_yaml_1.types.omap, js_yaml_1.types.pairs, js_yaml_1.types.set],
11
11
  });
12
- const parseYaml = (str, opts) => (0, js_yaml_1.load)(str, Object.assign({ schema: DEFAULT_SCHEMA_WITHOUT_TIMESTAMP }, opts));
12
+ const parseYaml = (str, opts) => (0, js_yaml_1.load)(str, { schema: DEFAULT_SCHEMA_WITHOUT_TIMESTAMP, ...opts });
13
13
  exports.parseYaml = parseYaml;
14
14
  const stringifyYaml = (obj, opts) => (0, js_yaml_1.dump)(obj, opts);
15
15
  exports.stringifyYaml = stringifyYaml;
package/lib/lint.d.ts CHANGED
@@ -3,10 +3,12 @@ import { StyleguideConfig, Config } from './config';
3
3
  import type { Document, ResolvedRefMap } from './resolve';
4
4
  import type { ProblemSeverity } from './walk';
5
5
  import type { NodeType } from './types';
6
+ import type { CollectFn } from './utils';
6
7
  export declare function lint(opts: {
7
8
  ref: string;
8
9
  config: Config;
9
10
  externalRefResolver?: BaseResolver;
11
+ collectSpecData?: CollectFn;
10
12
  }): Promise<import("./walk").NormalizedProblem[]>;
11
13
  export declare function lintFromString(opts: {
12
14
  source: string;
package/lib/lint.js CHANGED
@@ -1,125 +1,118 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.lintConfig = exports.lintDocument = exports.lintFromString = exports.lint = void 0;
3
+ exports.lint = lint;
4
+ exports.lintFromString = lintFromString;
5
+ exports.lintDocument = lintDocument;
6
+ exports.lintConfig = lintConfig;
7
+ const config_1 = require("@redocly/config");
13
8
  const resolve_1 = require("./resolve");
14
9
  const visitors_1 = require("./visitors");
15
10
  const walk_1 = require("./walk");
16
- const config_1 = require("./config");
11
+ const config_2 = require("./config");
17
12
  const types_1 = require("./types");
18
13
  const ajv_1 = require("./rules/ajv");
19
14
  const oas_types_1 = require("./oas-types");
20
15
  const redocly_yaml_1 = require("./types/redocly-yaml");
21
16
  const spec_1 = require("./rules/common/spec");
22
17
  const no_unresolved_refs_1 = require("./rules/no-unresolved-refs");
23
- const config_2 = require("@redocly/config");
24
- function lint(opts) {
25
- return __awaiter(this, void 0, void 0, function* () {
26
- const { ref, externalRefResolver = new resolve_1.BaseResolver(opts.config.resolve) } = opts;
27
- const document = (yield externalRefResolver.resolveDocument(null, ref, true));
28
- return lintDocument(Object.assign(Object.assign({ document }, opts), { externalRefResolver, config: opts.config.styleguide }));
18
+ async function lint(opts) {
19
+ const { ref, externalRefResolver = new resolve_1.BaseResolver(opts.config.resolve) } = opts;
20
+ const document = (await externalRefResolver.resolveDocument(null, ref, true));
21
+ opts.collectSpecData?.(document.parsed);
22
+ return lintDocument({
23
+ document,
24
+ ...opts,
25
+ externalRefResolver,
26
+ config: opts.config.styleguide,
29
27
  });
30
28
  }
31
- exports.lint = lint;
32
- function lintFromString(opts) {
33
- return __awaiter(this, void 0, void 0, function* () {
34
- const { source, absoluteRef, externalRefResolver = new resolve_1.BaseResolver(opts.config.resolve) } = opts;
35
- const document = (0, resolve_1.makeDocumentFromString)(source, absoluteRef || '/');
36
- return lintDocument(Object.assign(Object.assign({ document }, opts), { externalRefResolver, config: opts.config.styleguide }));
29
+ async function lintFromString(opts) {
30
+ const { source, absoluteRef, externalRefResolver = new resolve_1.BaseResolver(opts.config.resolve) } = opts;
31
+ const document = (0, resolve_1.makeDocumentFromString)(source, absoluteRef || '/');
32
+ return lintDocument({
33
+ document,
34
+ ...opts,
35
+ externalRefResolver,
36
+ config: opts.config.styleguide,
37
37
  });
38
38
  }
39
- exports.lintFromString = lintFromString;
40
- function lintDocument(opts) {
41
- return __awaiter(this, void 0, void 0, function* () {
42
- (0, ajv_1.releaseAjvInstance)(); // FIXME: preprocessors can modify nodes which are then cached to ajv-instance by absolute path
43
- const { document, customTypes, externalRefResolver, config } = opts;
44
- const specVersion = (0, oas_types_1.detectSpec)(document.parsed);
45
- const specMajorVersion = (0, oas_types_1.getMajorSpecVersion)(specVersion);
46
- const rules = config.getRulesForOasVersion(specMajorVersion);
47
- const types = (0, types_1.normalizeTypes)(config.extendTypes(customTypes !== null && customTypes !== void 0 ? customTypes : (0, oas_types_1.getTypes)(specVersion), specVersion), config);
48
- const ctx = {
49
- problems: [],
50
- oasVersion: specVersion,
51
- visitorsData: {},
52
- };
53
- const preprocessors = (0, config_1.initRules)(rules, config, 'preprocessors', specVersion);
54
- const regularRules = (0, config_1.initRules)(rules, config, 'rules', specVersion);
55
- let resolvedRefMap = yield (0, resolve_1.resolveDocument)({
56
- rootDocument: document,
57
- rootType: types.Root,
58
- externalRefResolver,
59
- });
60
- if (preprocessors.length > 0) {
61
- // Make additional pass to resolve refs defined in preprocessors.
62
- (0, walk_1.walkDocument)({
63
- document,
64
- rootType: types.Root,
65
- normalizedVisitors: (0, visitors_1.normalizeVisitors)(preprocessors, types),
66
- resolvedRefMap,
67
- ctx,
68
- });
69
- resolvedRefMap = yield (0, resolve_1.resolveDocument)({
70
- rootDocument: document,
71
- rootType: types.Root,
72
- externalRefResolver,
73
- });
74
- }
75
- const normalizedVisitors = (0, visitors_1.normalizeVisitors)(regularRules, types);
39
+ async function lintDocument(opts) {
40
+ (0, ajv_1.releaseAjvInstance)(); // FIXME: preprocessors can modify nodes which are then cached to ajv-instance by absolute path
41
+ const { document, customTypes, externalRefResolver, config } = opts;
42
+ const specVersion = (0, oas_types_1.detectSpec)(document.parsed);
43
+ const specMajorVersion = (0, oas_types_1.getMajorSpecVersion)(specVersion);
44
+ const rules = config.getRulesForOasVersion(specMajorVersion);
45
+ const types = (0, types_1.normalizeTypes)(config.extendTypes(customTypes ?? (0, oas_types_1.getTypes)(specVersion), specVersion), config);
46
+ const ctx = {
47
+ problems: [],
48
+ oasVersion: specVersion,
49
+ visitorsData: {},
50
+ };
51
+ const preprocessors = (0, config_2.initRules)(rules, config, 'preprocessors', specVersion);
52
+ const regularRules = (0, config_2.initRules)(rules, config, 'rules', specVersion);
53
+ let resolvedRefMap = await (0, resolve_1.resolveDocument)({
54
+ rootDocument: document,
55
+ rootType: types.Root,
56
+ externalRefResolver,
57
+ });
58
+ if (preprocessors.length > 0) {
59
+ // Make additional pass to resolve refs defined in preprocessors.
76
60
  (0, walk_1.walkDocument)({
77
61
  document,
78
62
  rootType: types.Root,
79
- normalizedVisitors,
63
+ normalizedVisitors: (0, visitors_1.normalizeVisitors)(preprocessors, types),
80
64
  resolvedRefMap,
81
65
  ctx,
82
66
  });
83
- return ctx.problems.map((problem) => config.addProblemToIgnore(problem));
67
+ resolvedRefMap = await (0, resolve_1.resolveDocument)({
68
+ rootDocument: document,
69
+ rootType: types.Root,
70
+ externalRefResolver,
71
+ });
72
+ }
73
+ const normalizedVisitors = (0, visitors_1.normalizeVisitors)(regularRules, types);
74
+ (0, walk_1.walkDocument)({
75
+ document,
76
+ rootType: types.Root,
77
+ normalizedVisitors,
78
+ resolvedRefMap,
79
+ ctx,
84
80
  });
81
+ return ctx.problems.map((problem) => config.addProblemToIgnore(problem));
85
82
  }
86
- exports.lintDocument = lintDocument;
87
- function lintConfig(opts) {
88
- return __awaiter(this, void 0, void 0, function* () {
89
- const { document, severity, externalRefResolver = new resolve_1.BaseResolver(), config } = opts;
90
- const ctx = {
91
- problems: [],
92
- oasVersion: oas_types_1.SpecVersion.OAS3_0,
93
- visitorsData: {},
94
- };
95
- const types = (0, types_1.normalizeTypes)(opts.externalConfigTypes || (0, redocly_yaml_1.createConfigTypes)(config_2.rootRedoclyConfigSchema, config), { doNotResolveExamples: config.styleguide.doNotResolveExamples });
96
- const rules = [
97
- {
98
- severity: severity || 'error',
99
- ruleId: 'configuration spec',
100
- visitor: (0, spec_1.Spec)({ severity: 'error' }),
101
- },
102
- {
103
- severity: severity || 'error',
104
- ruleId: 'configuration no-unresolved-refs',
105
- visitor: (0, no_unresolved_refs_1.NoUnresolvedRefs)({ severity: 'error' }),
106
- },
107
- ];
108
- const normalizedVisitors = (0, visitors_1.normalizeVisitors)(rules, types);
109
- const resolvedRefMap = opts.resolvedRefMap ||
110
- (yield (0, resolve_1.resolveDocument)({
111
- rootDocument: document,
112
- rootType: types.ConfigRoot,
113
- externalRefResolver,
114
- }));
115
- (0, walk_1.walkDocument)({
116
- document,
83
+ async function lintConfig(opts) {
84
+ const { document, severity, externalRefResolver = new resolve_1.BaseResolver(), config } = opts;
85
+ const ctx = {
86
+ problems: [],
87
+ oasVersion: oas_types_1.SpecVersion.OAS3_0,
88
+ visitorsData: {},
89
+ };
90
+ const types = (0, types_1.normalizeTypes)(opts.externalConfigTypes || (0, redocly_yaml_1.createConfigTypes)(config_1.rootRedoclyConfigSchema, config), { doNotResolveExamples: config.styleguide.doNotResolveExamples });
91
+ const rules = [
92
+ {
93
+ severity: severity || 'error',
94
+ ruleId: 'configuration spec',
95
+ visitor: (0, spec_1.Spec)({ severity: 'error' }),
96
+ },
97
+ {
98
+ severity: severity || 'error',
99
+ ruleId: 'configuration no-unresolved-refs',
100
+ visitor: (0, no_unresolved_refs_1.NoUnresolvedRefs)({ severity: 'error' }),
101
+ },
102
+ ];
103
+ const normalizedVisitors = (0, visitors_1.normalizeVisitors)(rules, types);
104
+ const resolvedRefMap = opts.resolvedRefMap ||
105
+ (await (0, resolve_1.resolveDocument)({
106
+ rootDocument: document,
117
107
  rootType: types.ConfigRoot,
118
- normalizedVisitors,
119
- resolvedRefMap,
120
- ctx,
121
- });
122
- return ctx.problems;
108
+ externalRefResolver,
109
+ }));
110
+ (0, walk_1.walkDocument)({
111
+ document,
112
+ rootType: types.ConfigRoot,
113
+ normalizedVisitors,
114
+ resolvedRefMap,
115
+ ctx,
123
116
  });
117
+ return ctx.problems;
124
118
  }
125
- exports.lintConfig = lintConfig;
@@ -1,32 +1,36 @@
1
- import { Oas3Rule, Oas3Preprocessor, Oas2Rule, Oas2Preprocessor, Async2Preprocessor, Async2Rule, ArazzoPreprocessor, ArazzoRule } from './visitors';
2
- import { BuiltInAsync2RuleId, BuiltInCommonOASRuleId, BuiltInArazzoRuleId, BuiltInOAS2RuleId, BuiltInOAS3RuleId } from './types/redocly-yaml';
3
- export type RuleSet<T> = Record<string, T>;
1
+ import type { Oas3Rule, Oas3Preprocessor, Oas2Rule, Oas2Preprocessor, Async2Preprocessor, Async2Rule, Async3Preprocessor, Async3Rule, ArazzoPreprocessor, ArazzoRule } from './visitors';
2
+ import type { BuiltInAsync2RuleId, BuiltInAsync3RuleId, BuiltInCommonOASRuleId, BuiltInArazzoRuleId, BuiltInOAS2RuleId, BuiltInOAS3RuleId } from './types/redocly-yaml';
4
3
  export declare enum SpecVersion {
5
4
  OAS2 = "oas2",
6
5
  OAS3_0 = "oas3_0",
7
6
  OAS3_1 = "oas3_1",
8
7
  Async2 = "async2",
8
+ Async3 = "async3",
9
9
  Arazzo = "arazzo"
10
10
  }
11
11
  export declare enum SpecMajorVersion {
12
12
  OAS2 = "oas2",
13
13
  OAS3 = "oas3",
14
14
  Async2 = "async2",
15
+ Async3 = "async3",
15
16
  Arazzo = "arazzo"
16
17
  }
17
18
  export type RuleMap<Key extends string, RuleConfig, T> = Record<T extends 'built-in' ? Key : string, RuleConfig>;
18
19
  export type Oas3RuleSet<T = undefined> = RuleMap<BuiltInCommonOASRuleId | BuiltInOAS3RuleId | 'assertions', Oas3Rule, T>;
19
20
  export type Oas2RuleSet<T = undefined> = RuleMap<BuiltInCommonOASRuleId | BuiltInOAS2RuleId | 'assertions', Oas2Rule, T>;
20
21
  export type Async2RuleSet<T = undefined> = RuleMap<BuiltInAsync2RuleId | 'assertions', Async2Rule, T>;
22
+ export type Async3RuleSet<T = undefined> = RuleMap<BuiltInAsync3RuleId | 'assertions', Async3Rule, T>;
21
23
  export type ArazzoRuleSet<T = undefined> = RuleMap<BuiltInArazzoRuleId | 'assertions', ArazzoRule, T>;
22
24
  export type Oas3PreprocessorsSet = Record<string, Oas3Preprocessor>;
23
25
  export type Oas2PreprocessorsSet = Record<string, Oas2Preprocessor>;
24
26
  export type Async2PreprocessorsSet = Record<string, Async2Preprocessor>;
27
+ export type Async3PreprocessorsSet = Record<string, Async3Preprocessor>;
25
28
  export type ArazzoPreprocessorsSet = Record<string, ArazzoPreprocessor>;
26
29
  export type Oas3DecoratorsSet = Record<string, Oas3Preprocessor>;
27
30
  export type Oas2DecoratorsSet = Record<string, Oas2Preprocessor>;
28
31
  export type Async2DecoratorsSet = Record<string, Async2Preprocessor>;
32
+ export type Async3DecoratorsSet = Record<string, Async3Preprocessor>;
29
33
  export type ArazzoDecoratorsSet = Record<string, ArazzoPreprocessor>;
30
- export declare function detectSpec(root: any): SpecVersion;
34
+ export declare function detectSpec(root: unknown): SpecVersion;
31
35
  export declare function getMajorSpecVersion(version: SpecVersion): SpecMajorVersion;
32
- export declare function getTypes(spec: SpecVersion): Record<string, import("./types").NodeType> | Record<"Root" | "Tag" | "ExternalDocs" | "SecurityRequirement" | "Info" | "Contact" | "License" | "Paths" | "PathItem" | "Parameter" | "Operation" | "Example" | "Header" | "Responses" | "Response" | "Schema" | "Xml" | "SchemaProperties" | "NamedSchemas" | "NamedResponses" | "NamedParameters" | "NamedSecuritySchemes" | "SecurityScheme" | "Examples" | "ExamplesMap" | "TagList" | "SecurityRequirementList" | "ParameterList" | "ParameterItems" | "TagGroup" | "TagGroups" | "EnumDescriptions" | "Logo" | "XCodeSample" | "XCodeSampleList" | "XServer" | "XServerList", import("./types").NodeType> | Record<"Root" | "Tag" | "ExternalDocs" | "Server" | "ServerVariable" | "SecurityRequirement" | "Info" | "Contact" | "License" | "Paths" | "PathItem" | "Callback" | "CallbacksMap" | "Parameter" | "Operation" | "RequestBody" | "MediaTypesMap" | "MediaType" | "Example" | "Encoding" | "Header" | "Responses" | "Response" | "Link" | "Schema" | "Xml" | "SchemaProperties" | "DiscriminatorMapping" | "Discriminator" | "Components" | "NamedSchemas" | "NamedResponses" | "NamedParameters" | "NamedExamples" | "NamedRequestBodies" | "NamedHeaders" | "NamedSecuritySchemes" | "NamedLinks" | "NamedCallbacks" | "ImplicitFlow" | "PasswordFlow" | "ClientCredentials" | "AuthorizationCode" | "OAuth2Flows" | "SecurityScheme" | "ServerVariablesMap" | "ExamplesMap" | "EncodingMap" | "HeadersMap" | "LinksMap" | "TagList" | "SecurityRequirementList" | "ParameterList" | "TagGroup" | "TagGroups" | "EnumDescriptions" | "Logo" | "XCodeSample" | "XCodeSampleList" | "ServerList" | "XUsePkce" | "WebhooksMap", import("./types").NodeType> | Record<"Root" | "Info" | "License" | "Operation" | "Schema" | "SchemaProperties" | "Components" | "SecurityScheme" | "NamedPathItems" | "DependentRequired", import("./types").NodeType>;
36
+ export declare function getTypes(spec: SpecVersion): Record<string, import("./types").NodeType> | Record<"Root" | "Tag" | "ExternalDocs" | "SecurityRequirement" | "Info" | "Contact" | "License" | "Paths" | "PathItem" | "Parameter" | "Operation" | "Example" | "Header" | "Responses" | "Response" | "Schema" | "Xml" | "SchemaProperties" | "NamedSchemas" | "NamedResponses" | "NamedParameters" | "NamedSecuritySchemes" | "SecurityScheme" | "Examples" | "ExamplesMap" | "TagList" | "SecurityRequirementList" | "ParameterList" | "ParameterItems" | "TagGroup" | "TagGroups" | "EnumDescriptions" | "Logo" | "XCodeSample" | "XCodeSampleList" | "XServer" | "XServerList", import("./types").NodeType> | Record<"Root" | "Tag" | "ExternalDocs" | "Server" | "ServerVariable" | "SecurityRequirement" | "Info" | "Contact" | "License" | "Paths" | "PathItem" | "Callback" | "CallbacksMap" | "Parameter" | "Operation" | "RequestBody" | "MediaTypesMap" | "MediaType" | "Example" | "Encoding" | "Header" | "Responses" | "Response" | "Link" | "Schema" | "Xml" | "SchemaProperties" | "DiscriminatorMapping" | "Discriminator" | "Components" | "NamedSchemas" | "NamedResponses" | "NamedParameters" | "NamedExamples" | "NamedRequestBodies" | "NamedHeaders" | "NamedSecuritySchemes" | "NamedLinks" | "NamedCallbacks" | "ImplicitFlow" | "PasswordFlow" | "ClientCredentials" | "AuthorizationCode" | "OAuth2Flows" | "SecurityScheme" | "ServerVariablesMap" | "ExamplesMap" | "EncodingMap" | "HeadersMap" | "LinksMap" | "TagList" | "SecurityRequirementList" | "ParameterList" | "TagGroup" | "TagGroups" | "EnumDescriptions" | "Logo" | "XCodeSample" | "XCodeSampleList" | "ServerList" | "XUsePkce" | "WebhooksMap", import("./types").NodeType> | Record<"Root" | "Info" | "License" | "Operation" | "Schema" | "SchemaProperties" | "Components" | "SecurityScheme" | "PatternProperties" | "NamedPathItems" | "DependentRequired", import("./types").NodeType>;
package/lib/oas-types.js CHANGED
@@ -1,17 +1,23 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getTypes = exports.getMajorSpecVersion = exports.detectSpec = exports.SpecMajorVersion = exports.SpecVersion = void 0;
3
+ exports.SpecMajorVersion = exports.SpecVersion = void 0;
4
+ exports.detectSpec = detectSpec;
5
+ exports.getMajorSpecVersion = getMajorSpecVersion;
6
+ exports.getTypes = getTypes;
4
7
  const oas2_1 = require("./types/oas2");
5
8
  const oas3_1 = require("./types/oas3");
6
9
  const oas3_1_1 = require("./types/oas3_1");
7
- const asyncapi_1 = require("./types/asyncapi");
10
+ const asyncapi2_1 = require("./types/asyncapi2");
11
+ const asyncapi3_1 = require("./types/asyncapi3");
8
12
  const arazzo_1 = require("./types/arazzo");
13
+ const utils_1 = require("./utils");
9
14
  var SpecVersion;
10
15
  (function (SpecVersion) {
11
16
  SpecVersion["OAS2"] = "oas2";
12
17
  SpecVersion["OAS3_0"] = "oas3_0";
13
18
  SpecVersion["OAS3_1"] = "oas3_1";
14
19
  SpecVersion["Async2"] = "async2";
20
+ SpecVersion["Async3"] = "async3";
15
21
  SpecVersion["Arazzo"] = "arazzo";
16
22
  })(SpecVersion || (exports.SpecVersion = SpecVersion = {}));
17
23
  var SpecMajorVersion;
@@ -19,47 +25,50 @@ var SpecMajorVersion;
19
25
  SpecMajorVersion["OAS2"] = "oas2";
20
26
  SpecMajorVersion["OAS3"] = "oas3";
21
27
  SpecMajorVersion["Async2"] = "async2";
28
+ SpecMajorVersion["Async3"] = "async3";
22
29
  SpecMajorVersion["Arazzo"] = "arazzo";
23
30
  })(SpecMajorVersion || (exports.SpecMajorVersion = SpecMajorVersion = {}));
24
31
  const typesMap = {
25
32
  [SpecVersion.OAS2]: oas2_1.Oas2Types,
26
33
  [SpecVersion.OAS3_0]: oas3_1.Oas3Types,
27
34
  [SpecVersion.OAS3_1]: oas3_1_1.Oas3_1Types,
28
- [SpecVersion.Async2]: asyncapi_1.AsyncApi2Types,
35
+ [SpecVersion.Async2]: asyncapi2_1.AsyncApi2Types,
36
+ [SpecVersion.Async3]: asyncapi3_1.AsyncApi3Types,
29
37
  [SpecVersion.Arazzo]: arazzo_1.ArazzoTypes,
30
38
  };
31
39
  function detectSpec(root) {
32
- if (typeof root !== 'object') {
40
+ if (!(0, utils_1.isPlainObject)(root)) {
33
41
  throw new Error(`Document must be JSON object, got ${typeof root}`);
34
42
  }
35
43
  if (root.openapi && typeof root.openapi !== 'string') {
36
44
  throw new Error(`Invalid OpenAPI version: should be a string but got "${typeof root.openapi}"`);
37
45
  }
38
- if (root.openapi && root.openapi.startsWith('3.0')) {
46
+ if (typeof root.openapi === 'string' && root.openapi.startsWith('3.0')) {
39
47
  return SpecVersion.OAS3_0;
40
48
  }
41
- if (root.openapi && root.openapi.startsWith('3.1')) {
49
+ if (typeof root.openapi === 'string' && root.openapi.startsWith('3.1')) {
42
50
  return SpecVersion.OAS3_1;
43
51
  }
44
52
  if (root.swagger && root.swagger === '2.0') {
45
53
  return SpecVersion.OAS2;
46
54
  }
47
- // if not detected yet
48
55
  if (root.openapi || root.swagger) {
49
56
  throw new Error(`Unsupported OpenAPI version: ${root.openapi || root.swagger}`);
50
57
  }
51
- if (root.asyncapi && root.asyncapi.startsWith('2.')) {
58
+ if (typeof root.asyncapi === 'string' && root.asyncapi.startsWith('2.')) {
52
59
  return SpecVersion.Async2;
53
60
  }
61
+ if (typeof root.asyncapi === 'string' && root.asyncapi.startsWith('3.')) {
62
+ return SpecVersion.Async3;
63
+ }
54
64
  if (root.asyncapi) {
55
65
  throw new Error(`Unsupported AsyncAPI version: ${root.asyncapi}`);
56
66
  }
57
- if (root.arazzo && root.arazzo.startsWith('1.')) {
67
+ if (typeof root.arazzo === 'string' && root.arazzo.startsWith('1.')) {
58
68
  return SpecVersion.Arazzo;
59
69
  }
60
70
  throw new Error(`Unsupported specification`);
61
71
  }
62
- exports.detectSpec = detectSpec;
63
72
  function getMajorSpecVersion(version) {
64
73
  if (version === SpecVersion.OAS2) {
65
74
  return SpecMajorVersion.OAS2;
@@ -67,6 +76,9 @@ function getMajorSpecVersion(version) {
67
76
  else if (version === SpecVersion.Async2) {
68
77
  return SpecMajorVersion.Async2;
69
78
  }
79
+ else if (version === SpecVersion.Async3) {
80
+ return SpecMajorVersion.Async3;
81
+ }
70
82
  else if (version === SpecVersion.Arazzo) {
71
83
  return SpecMajorVersion.Arazzo;
72
84
  }
@@ -74,8 +86,6 @@ function getMajorSpecVersion(version) {
74
86
  return SpecMajorVersion.OAS3;
75
87
  }
76
88
  }
77
- exports.getMajorSpecVersion = getMajorSpecVersion;
78
89
  function getTypes(spec) {
79
90
  return typesMap[spec];
80
91
  }
81
- exports.getTypes = getTypes;
@@ -1,6 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isRedoclyRegistryURL = exports.getRedoclyDomain = exports.setRedoclyDomain = exports.getDomains = exports.AVAILABLE_REGIONS = exports.DOMAINS = exports.DEFAULT_REGION = void 0;
3
+ exports.AVAILABLE_REGIONS = exports.DOMAINS = exports.DEFAULT_REGION = void 0;
4
+ exports.getDomains = getDomains;
5
+ exports.setRedoclyDomain = setRedoclyDomain;
6
+ exports.getRedoclyDomain = getRedoclyDomain;
7
+ exports.isRedoclyRegistryURL = isRedoclyRegistryURL;
4
8
  let redoclyDomain = 'redocly.com';
5
9
  exports.DEFAULT_REGION = 'us';
6
10
  exports.DOMAINS = getDomains();
@@ -12,7 +16,7 @@ function getDomains() {
12
16
  };
13
17
  // FIXME: temporary fix for our lab environments
14
18
  const domain = redoclyDomain;
15
- if (domain === null || domain === void 0 ? void 0 : domain.endsWith('.redocly.host')) {
19
+ if (domain?.endsWith('.redocly.host')) {
16
20
  domains[domain.split('.')[0]] = domain;
17
21
  }
18
22
  if (domain === 'redoc.online') {
@@ -20,15 +24,12 @@ function getDomains() {
20
24
  }
21
25
  return domains;
22
26
  }
23
- exports.getDomains = getDomains;
24
27
  function setRedoclyDomain(domain) {
25
28
  redoclyDomain = domain;
26
29
  }
27
- exports.setRedoclyDomain = setRedoclyDomain;
28
30
  function getRedoclyDomain() {
29
31
  return redoclyDomain;
30
32
  }
31
- exports.getRedoclyDomain = getRedoclyDomain;
32
33
  function isRedoclyRegistryURL(link) {
33
34
  const domain = getRedoclyDomain() || exports.DOMAINS[exports.DEFAULT_REGION];
34
35
  const legacyDomain = domain === 'redocly.com' ? 'redoc.ly' : domain;
@@ -38,4 +39,3 @@ function isRedoclyRegistryURL(link) {
38
39
  }
39
40
  return true;
40
41
  }
41
- exports.isRedoclyRegistryURL = isRedoclyRegistryURL;
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.RedoclyClient = exports.TOKEN_FILENAME = void 0;
13
4
  const fs_1 = require("fs");
@@ -47,10 +38,8 @@ class RedoclyClient {
47
38
  hasToken() {
48
39
  return !!this.accessTokens[this.region];
49
40
  }
50
- getAuthorizationHeader() {
51
- return __awaiter(this, void 0, void 0, function* () {
52
- return this.accessTokens[this.region];
53
- });
41
+ async getAuthorizationHeader() {
42
+ return this.accessTokens[this.region];
54
43
  }
55
44
  // </backward compatibility: portal>
56
45
  setAccessTokens(accessTokens) {
@@ -60,13 +49,19 @@ class RedoclyClient {
60
49
  const credentialsPath = (0, path_1.resolve)((0, os_1.homedir)(), exports.TOKEN_FILENAME);
61
50
  const credentials = this.readCredentialsFile(credentialsPath);
62
51
  if ((0, utils_1.isNotEmptyObject)(credentials)) {
63
- this.setAccessTokens(Object.assign(Object.assign({}, credentials), (credentials.token &&
64
- !credentials[this.region] && {
65
- [this.region]: credentials.token,
66
- })));
52
+ this.setAccessTokens({
53
+ ...credentials,
54
+ ...(credentials.token &&
55
+ !credentials[this.region] && {
56
+ [this.region]: credentials.token,
57
+ }),
58
+ });
67
59
  }
68
60
  if (env_1.env.REDOCLY_AUTHORIZATION) {
69
- this.setAccessTokens(Object.assign(Object.assign({}, this.accessTokens), { [this.region]: env_1.env.REDOCLY_AUTHORIZATION }));
61
+ this.setAccessTokens({
62
+ ...this.accessTokens,
63
+ [this.region]: env_1.env.REDOCLY_AUTHORIZATION,
64
+ });
70
65
  }
71
66
  }
72
67
  getAllTokens() {
@@ -74,65 +69,57 @@ class RedoclyClient {
74
69
  .filter(([region]) => domains_1.AVAILABLE_REGIONS.includes(region))
75
70
  .map(([region, token]) => ({ region, token }));
76
71
  }
77
- getValidTokens() {
78
- return __awaiter(this, void 0, void 0, function* () {
79
- const allTokens = this.getAllTokens();
80
- const verifiedTokens = yield Promise.allSettled(allTokens.map(({ token }) => this.verifyToken(token)));
81
- return allTokens
82
- .filter((_, index) => verifiedTokens[index].status === 'fulfilled')
83
- .map(({ token, region }) => ({ token, region, valid: true }));
84
- });
85
- }
86
- getTokens() {
87
- return __awaiter(this, void 0, void 0, function* () {
88
- return this.hasTokens() ? yield this.getValidTokens() : [];
89
- });
90
- }
91
- isAuthorizedWithRedoclyByRegion() {
92
- return __awaiter(this, void 0, void 0, function* () {
93
- if (!this.hasTokens()) {
94
- return false;
95
- }
96
- const accessToken = this.accessTokens[this.region];
97
- if (!accessToken) {
98
- return false;
99
- }
100
- try {
101
- yield this.verifyToken(accessToken);
102
- return true;
103
- }
104
- catch (err) {
105
- return false;
106
- }
107
- });
108
- }
109
- isAuthorizedWithRedocly() {
110
- return __awaiter(this, void 0, void 0, function* () {
111
- return this.hasTokens() && (0, utils_1.isNotEmptyObject)(yield this.getValidTokens());
112
- });
72
+ async getValidTokens() {
73
+ const allTokens = this.getAllTokens();
74
+ const verifiedTokens = await Promise.allSettled(allTokens.map(({ token }) => this.verifyToken(token)));
75
+ return allTokens
76
+ .filter((_, index) => verifiedTokens[index].status === 'fulfilled')
77
+ .map(({ token, region }) => ({ token, region, valid: true }));
78
+ }
79
+ async getTokens() {
80
+ return this.hasTokens() ? await this.getValidTokens() : [];
81
+ }
82
+ async isAuthorizedWithRedoclyByRegion() {
83
+ if (!this.hasTokens()) {
84
+ return false;
85
+ }
86
+ const accessToken = this.accessTokens[this.region];
87
+ if (!accessToken) {
88
+ return false;
89
+ }
90
+ try {
91
+ await this.verifyToken(accessToken);
92
+ return true;
93
+ }
94
+ catch (err) {
95
+ return false;
96
+ }
97
+ }
98
+ async isAuthorizedWithRedocly() {
99
+ return this.hasTokens() && (0, utils_1.isNotEmptyObject)(await this.getValidTokens());
113
100
  }
114
101
  readCredentialsFile(credentialsPath) {
115
102
  return (0, fs_1.existsSync)(credentialsPath) ? JSON.parse((0, fs_1.readFileSync)(credentialsPath, 'utf-8')) : {};
116
103
  }
117
- verifyToken(accessToken, verbose = false) {
118
- return __awaiter(this, void 0, void 0, function* () {
119
- return this.registryApi.authStatus(accessToken, verbose);
120
- });
121
- }
122
- login(accessToken, verbose = false) {
123
- return __awaiter(this, void 0, void 0, function* () {
124
- const credentialsPath = (0, path_1.resolve)((0, os_1.homedir)(), exports.TOKEN_FILENAME);
125
- try {
126
- yield this.verifyToken(accessToken, verbose);
127
- }
128
- catch (err) {
129
- throw new Error('Authorization failed. Please check if you entered a valid API key.');
130
- }
131
- const credentials = Object.assign(Object.assign({}, this.readCredentialsFile(credentialsPath)), { [this.region]: accessToken, token: accessToken });
132
- this.accessTokens = credentials;
133
- this.registryApi.setAccessTokens(credentials);
134
- (0, fs_1.writeFileSync)(credentialsPath, JSON.stringify(credentials, null, 2));
135
- });
104
+ async verifyToken(accessToken, verbose = false) {
105
+ return this.registryApi.authStatus(accessToken, verbose);
106
+ }
107
+ async login(accessToken, verbose = false) {
108
+ const credentialsPath = (0, path_1.resolve)((0, os_1.homedir)(), exports.TOKEN_FILENAME);
109
+ try {
110
+ await this.verifyToken(accessToken, verbose);
111
+ }
112
+ catch (err) {
113
+ throw new Error('Authorization failed. Please check if you entered a valid API key.');
114
+ }
115
+ const credentials = {
116
+ ...this.readCredentialsFile(credentialsPath),
117
+ [this.region]: accessToken,
118
+ token: accessToken, // FIXME: backward compatibility, remove on 1.0.0
119
+ };
120
+ this.accessTokens = credentials;
121
+ this.registryApi.setAccessTokens(credentials);
122
+ (0, fs_1.writeFileSync)(credentialsPath, JSON.stringify(credentials, null, 2));
136
123
  }
137
124
  logout() {
138
125
  const credentialsPath = (0, path_1.resolve)((0, os_1.homedir)(), exports.TOKEN_FILENAME);