@redocly/cli 1.9.0 → 1.10.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.
@@ -1,11 +1,9 @@
1
1
  import { red, blue, yellow, green } from 'colorette';
2
2
  import * as fs from 'fs';
3
3
  import { parseYaml, slash, isRef, isTruthy } from '@redocly/openapi-core';
4
- import type { OasRef } from '@redocly/openapi-core';
5
4
  import * as path from 'path';
6
5
  import { performance } from 'perf_hooks';
7
6
  const isEqual = require('lodash.isequal');
8
-
9
7
  import {
10
8
  printExecutionTime,
11
9
  pathToFilename,
@@ -16,8 +14,16 @@ import {
16
14
  writeToFileByExtension,
17
15
  getAndValidateFileExtension,
18
16
  } from '../../utils/miscellaneous';
19
- import { isString, isObject, isEmptyObject } from '../../utils/js-utils';
17
+ import { isObject, isEmptyObject } from '../../utils/js-utils';
20
18
  import {
19
+ OPENAPI3_COMPONENT,
20
+ COMPONENTS,
21
+ OPENAPI3_METHOD_NAMES,
22
+ OPENAPI3_COMPONENT_NAMES,
23
+ } from './types';
24
+
25
+ import type { OasRef } from '@redocly/openapi-core';
26
+ import type {
21
27
  Definition,
22
28
  Oas2Definition,
23
29
  Oas3Schema,
@@ -26,13 +32,8 @@ import {
26
32
  Oas3Components,
27
33
  Oas3ComponentName,
28
34
  ComponentsFiles,
29
- refObj,
35
+ RefObject,
30
36
  Oas3PathItem,
31
- OPENAPI3_COMPONENT,
32
- COMPONENTS,
33
- componentsPath,
34
- OPENAPI3_METHOD_NAMES,
35
- OPENAPI3_COMPONENT_NAMES,
36
37
  Referenced,
37
38
  } from './types';
38
39
 
@@ -93,8 +94,8 @@ function splitDefinition(
93
94
  writeToFileByExtension(openapi, path.join(openapiDir, `openapi.${ext}`));
94
95
  }
95
96
 
96
- function isStartsWithComponents(node: string) {
97
- return node.startsWith(componentsPath);
97
+ export function startsWithComponents(node: string) {
98
+ return node.startsWith(`#/${COMPONENTS}/`);
98
99
  }
99
100
 
100
101
  function isSupportedExtension(filename: string) {
@@ -144,33 +145,31 @@ function traverseDirectoryDeepCallback(
144
145
  writeToFileByExtension(pathData, filename);
145
146
  }
146
147
 
147
- function crawl(object: any, visitor: any) {
148
+ export function crawl(object: unknown, visitor: (node: Record<string, unknown>) => void) {
148
149
  if (!isObject(object)) return;
150
+
151
+ visitor(object);
149
152
  for (const key of Object.keys(object)) {
150
- visitor(object, key);
151
153
  crawl(object[key], visitor);
152
154
  }
153
155
  }
154
156
 
155
- function replace$Refs(obj: any, relativeFrom: string, componentFiles = {} as ComponentsFiles) {
156
- crawl(obj, (node: any) => {
157
- if (node.$ref && isString(node.$ref) && isStartsWithComponents(node.$ref)) {
158
- replace(node, '$ref');
159
- } else if (
160
- node.discriminator &&
161
- node.discriminator.mapping &&
162
- isObject(node.discriminator.mapping)
163
- ) {
157
+ function replace$Refs(obj: unknown, relativeFrom: string, componentFiles = {} as ComponentsFiles) {
158
+ crawl(obj, (node: Record<string, unknown>) => {
159
+ if (node.$ref && typeof node.$ref === 'string' && startsWithComponents(node.$ref)) {
160
+ replace(node as RefObject, '$ref');
161
+ } else if (isObject(node.discriminator) && isObject(node.discriminator.mapping)) {
164
162
  const { mapping } = node.discriminator;
165
163
  for (const name of Object.keys(mapping)) {
166
- if (isString(mapping[name]) && isStartsWithComponents(mapping[name])) {
167
- replace(node.discriminator.mapping, name);
164
+ const mappingPointer = mapping[name];
165
+ if (typeof mappingPointer === 'string' && startsWithComponents(mappingPointer)) {
166
+ replace(node.discriminator.mapping as RefObject, name);
168
167
  }
169
168
  }
170
169
  }
171
170
  });
172
171
 
173
- function replace(node: refObj, key: string) {
172
+ function replace(node: RefObject, key: string) {
174
173
  const splittedNode = node[key].split('/');
175
174
  const name = splittedNode.pop();
176
175
  const groupName = splittedNode[2];
@@ -28,7 +28,7 @@ export type Definition = Oas3_1Definition | Oas3Definition | Oas2Definition;
28
28
  export interface ComponentsFiles {
29
29
  [schemas: string]: any;
30
30
  }
31
- export interface refObj {
31
+ export interface RefObject {
32
32
  [$ref: string]: string;
33
33
  }
34
34
 
@@ -36,8 +36,6 @@ export const COMPONENTS = 'components';
36
36
  export const PATHS = 'paths';
37
37
  export const WEBHOOKS = 'webhooks';
38
38
  export const xWEBHOOKS = 'x-webhooks';
39
- export const componentsPath = `#/${COMPONENTS}/`;
40
-
41
39
  export enum OPENAPI3_METHOD {
42
40
  get = 'get',
43
41
  put = 'put',
@@ -1,5 +1,6 @@
1
1
  import nodeFetch from 'node-fetch';
2
2
  import AbortController from 'abort-controller';
3
+ import { getProxyAgent } from '@redocly/openapi-core';
3
4
 
4
5
  const TIMEOUT = 3000;
5
6
 
@@ -10,7 +11,11 @@ export default async (url: string, options = {}) => {
10
11
  controller.abort();
11
12
  }, TIMEOUT);
12
13
 
13
- const res = await nodeFetch(url, { signal: controller.signal, ...options });
14
+ const res = await nodeFetch(url, {
15
+ signal: controller.signal,
16
+ ...options,
17
+ agent: getProxyAgent(),
18
+ });
14
19
  clearTimeout(timeout);
15
20
  return res;
16
21
  } catch (e) {
@@ -1,4 +1,4 @@
1
- export function isObject(obj: any) {
1
+ export function isObject(obj: unknown): obj is Record<string, unknown> {
2
2
  const type = typeof obj;
3
3
  return type === 'function' || (type === 'object' && !!obj);
4
4
  }