@redocly/openapi-core 1.15.0 → 1.16.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.
@@ -0,0 +1,52 @@
1
+ import { RedoclyClient } from '../../index';
2
+ import { setRedoclyDomain, getRedoclyDomain, getDomains, AVAILABLE_REGIONS } from '../domains';
3
+
4
+ describe('domains', () => {
5
+ const REDOCLY_DOMAIN_US = 'redocly.com';
6
+ const TEST_DOMAIN = 'redoclyDomain.com';
7
+ const TEST_LAB_DOMAIN = 'lab.redocly.host';
8
+ const TEST_REDOC_ONLINE_DOMAIN = 'redoc.online';
9
+
10
+ afterEach(() => {
11
+ delete process.env.REDOCLY_DOMAIN;
12
+ setRedoclyDomain('');
13
+ });
14
+
15
+ it('should resolve the US domain by default', () => {
16
+ expect(getRedoclyDomain()).toBe(REDOCLY_DOMAIN_US);
17
+ });
18
+
19
+ it('should resolve the US and EU regions by default', () => {
20
+ expect(AVAILABLE_REGIONS).toStrictEqual(['us', 'eu']);
21
+ });
22
+
23
+ it('should resolve the specified domain if used with setter', () => {
24
+ setRedoclyDomain(TEST_DOMAIN);
25
+ expect(getRedoclyDomain()).toBe(TEST_DOMAIN);
26
+ });
27
+
28
+ it('should resolve the specified domain provided in environmental variable, after initializing RedoclyClient', () => {
29
+ process.env.REDOCLY_DOMAIN = TEST_DOMAIN;
30
+ const client = new RedoclyClient();
31
+ expect(getRedoclyDomain()).toBe(TEST_DOMAIN);
32
+ expect(client.domain).toBe(TEST_DOMAIN);
33
+ });
34
+
35
+ it('should return correct object when redocly domain is set to lab env', () => {
36
+ setRedoclyDomain(TEST_LAB_DOMAIN);
37
+ const domains = getDomains();
38
+ expect(domains).toEqual({ us: 'redocly.com', eu: 'eu.redocly.com', lab: 'lab.redocly.host' });
39
+ expect(getRedoclyDomain()).toBe(TEST_LAB_DOMAIN);
40
+ });
41
+
42
+ it('should return correct object when redocly domain is set to redoc.online env', () => {
43
+ setRedoclyDomain(TEST_REDOC_ONLINE_DOMAIN);
44
+ const domains = getDomains();
45
+ expect(domains).toEqual({
46
+ us: 'redocly.com',
47
+ eu: 'eu.redocly.com',
48
+ 'redoc.online': 'redoc.online',
49
+ });
50
+ expect(getRedoclyDomain()).toBe(TEST_REDOC_ONLINE_DOMAIN);
51
+ });
52
+ });
@@ -1,3 +1,4 @@
1
+ import { setRedoclyDomain } from '../domains';
1
2
  import { RedoclyClient } from '../index';
2
3
 
3
4
  jest.mock('node-fetch', () => ({
@@ -16,6 +17,7 @@ describe('RedoclyClient', () => {
16
17
 
17
18
  afterEach(() => {
18
19
  delete process.env.REDOCLY_DOMAIN;
20
+ setRedoclyDomain('');
19
21
  });
20
22
 
21
23
  it('should resolve the US domain by default', () => {
@@ -40,19 +42,19 @@ describe('RedoclyClient', () => {
40
42
  });
41
43
 
42
44
  it('should resolve domain by EU region prioritizing flag over env variable', () => {
43
- process.env.REDOCLY_DOMAIN = testRedoclyDomain;
45
+ setRedoclyDomain(testRedoclyDomain);
44
46
  const client = new RedoclyClient('eu');
45
47
  expect(client.domain).toBe(REDOCLY_DOMAIN_EU);
46
48
  });
47
49
 
48
50
  it('should resolve domain by US region prioritizing flag over env variable', () => {
49
- process.env.REDOCLY_DOMAIN = testRedoclyDomain;
51
+ setRedoclyDomain(testRedoclyDomain);
50
52
  const client = new RedoclyClient('us');
51
53
  expect(client.domain).toBe(REDOCLY_DOMAIN_US);
52
54
  });
53
55
 
54
56
  it('should resolve domain by US region when REDOCLY_DOMAIN consists EU domain', () => {
55
- process.env.REDOCLY_DOMAIN = REDOCLY_DOMAIN_EU;
57
+ setRedoclyDomain(REDOCLY_DOMAIN_EU);
56
58
  const client = new RedoclyClient();
57
59
  expect(client.getRegion()).toBe('eu');
58
60
  });
@@ -0,0 +1,48 @@
1
+ import { Region } from '../config/types';
2
+
3
+ let redoclyDomain = 'redocly.com';
4
+
5
+ export const DEFAULT_REGION = 'us';
6
+
7
+ export const DOMAINS = getDomains();
8
+ export const AVAILABLE_REGIONS = Object.keys(DOMAINS) as Region[];
9
+
10
+ export function getDomains() {
11
+ const domains: { [region in Region]: string } = {
12
+ us: 'redocly.com',
13
+ eu: 'eu.redocly.com',
14
+ };
15
+
16
+ // FIXME: temporary fix for our lab environments
17
+ const domain = redoclyDomain;
18
+ if (domain?.endsWith('.redocly.host')) {
19
+ domains[domain.split('.')[0] as Region] = domain;
20
+ }
21
+ if (domain === 'redoc.online') {
22
+ domains[domain as Region] = domain;
23
+ }
24
+ return domains;
25
+ }
26
+
27
+ export function setRedoclyDomain(domain: string) {
28
+ redoclyDomain = domain;
29
+ }
30
+
31
+ export function getRedoclyDomain(): string {
32
+ return redoclyDomain;
33
+ }
34
+
35
+ export function isRedoclyRegistryURL(link: string): boolean {
36
+ const domain = getRedoclyDomain() || DOMAINS[DEFAULT_REGION];
37
+
38
+ const legacyDomain = domain === 'redocly.com' ? 'redoc.ly' : domain;
39
+
40
+ if (
41
+ !link.startsWith(`https://api.${domain}/registry/`) &&
42
+ !link.startsWith(`https://api.${legacyDomain}/registry/`)
43
+ ) {
44
+ return false;
45
+ }
46
+
47
+ return true;
48
+ }
@@ -2,13 +2,19 @@ import { existsSync, readFileSync, writeFileSync, unlinkSync } from 'fs';
2
2
  import { resolve } from 'path';
3
3
  import { homedir } from 'os';
4
4
  import { RegistryApi } from './registry-api';
5
- import { DEFAULT_REGION, DOMAINS, AVAILABLE_REGIONS } from '../config/config';
6
5
  import { env } from '../env';
7
6
  import { RegionalToken, RegionalTokenWithValidity } from './redocly-client-types';
8
7
  import { isNotEmptyObject } from '../utils';
9
8
  import { colorize } from '../logger';
10
9
 
11
10
  import type { AccessTokens, Region } from '../config/types';
11
+ import {
12
+ AVAILABLE_REGIONS,
13
+ DEFAULT_REGION,
14
+ DOMAINS,
15
+ getRedoclyDomain,
16
+ setRedoclyDomain,
17
+ } from './domains';
12
18
 
13
19
  export const TOKEN_FILENAME = '.redocly-config.json';
14
20
 
@@ -23,7 +29,7 @@ export class RedoclyClient {
23
29
  this.loadTokens();
24
30
  this.domain = region ? DOMAINS[region] : env.REDOCLY_DOMAIN || DOMAINS[DEFAULT_REGION];
25
31
 
26
- env.REDOCLY_DOMAIN = this.domain; // isRedoclyRegistryURL depends on the value to be set
32
+ setRedoclyDomain(this.domain);
27
33
  this.registryApi = new RegistryApi(this.accessTokens, this.region);
28
34
  }
29
35
 
@@ -36,9 +42,9 @@ export class RedoclyClient {
36
42
  );
37
43
  }
38
44
 
39
- if (env.REDOCLY_DOMAIN) {
45
+ if (getRedoclyDomain()) {
40
46
  return (AVAILABLE_REGIONS.find(
41
- (region) => DOMAINS[region as Region] === env.REDOCLY_DOMAIN
47
+ (region) => DOMAINS[region as Region] === getRedoclyDomain()
42
48
  ) || DEFAULT_REGION) as Region;
43
49
  }
44
50
  return region || DEFAULT_REGION;
@@ -96,7 +102,7 @@ export class RedoclyClient {
96
102
  const allTokens = this.getAllTokens();
97
103
 
98
104
  const verifiedTokens = await Promise.allSettled(
99
- allTokens.map(({ token, region }) => this.verifyToken(token, region))
105
+ allTokens.map(({ token }) => this.verifyToken(token))
100
106
  );
101
107
 
102
108
  return allTokens
@@ -120,7 +126,7 @@ export class RedoclyClient {
120
126
  }
121
127
 
122
128
  try {
123
- await this.verifyToken(accessToken, this.region);
129
+ await this.verifyToken(accessToken);
124
130
 
125
131
  return true;
126
132
  } catch (err) {
@@ -138,17 +144,16 @@ export class RedoclyClient {
138
144
 
139
145
  async verifyToken(
140
146
  accessToken: string,
141
- region: Region,
142
147
  verbose: boolean = false
143
148
  ): Promise<{ viewerId: string; organizations: string[] }> {
144
- return this.registryApi.authStatus(accessToken, region, verbose);
149
+ return this.registryApi.authStatus(accessToken, verbose);
145
150
  }
146
151
 
147
152
  async login(accessToken: string, verbose: boolean = false) {
148
153
  const credentialsPath = resolve(homedir(), TOKEN_FILENAME);
149
154
 
150
155
  try {
151
- await this.verifyToken(accessToken, this.region, verbose);
156
+ await this.verifyToken(accessToken, verbose);
152
157
  } catch (err) {
153
158
  throw new Error('Authorization failed. Please check if you entered a valid API key.');
154
159
  }
@@ -170,18 +175,3 @@ export class RedoclyClient {
170
175
  }
171
176
  }
172
177
  }
173
-
174
- export function isRedoclyRegistryURL(link: string): boolean {
175
- const domain = env.REDOCLY_DOMAIN || DOMAINS[DEFAULT_REGION];
176
-
177
- const legacyDomain = domain === 'redocly.com' ? 'redoc.ly' : domain;
178
-
179
- if (
180
- !link.startsWith(`https://api.${domain}/registry/`) &&
181
- !link.startsWith(`https://api.${legacyDomain}/registry/`)
182
- ) {
183
- return false;
184
- }
185
-
186
- return true;
187
- }
@@ -6,8 +6,8 @@ import type {
6
6
  PushApiParams,
7
7
  } from './registry-api-types';
8
8
  import type { AccessTokens, Region } from '../config/types';
9
- import { DEFAULT_REGION, DOMAINS } from '../config/config';
10
- import { isNotEmptyObject } from '../utils';
9
+ import { getProxyAgent, isNotEmptyObject } from '../utils';
10
+ import { getRedoclyDomain } from './domains';
11
11
 
12
12
  const version = require('../../package.json').version;
13
13
 
@@ -18,8 +18,8 @@ export class RegistryApi {
18
18
  return isNotEmptyObject(this.accessTokens) && this.accessTokens[this.region];
19
19
  }
20
20
 
21
- getBaseUrl(region: Region = DEFAULT_REGION) {
22
- return `https://api.${DOMAINS[region]}/registry`;
21
+ getBaseUrl() {
22
+ return `https://api.${getRedoclyDomain()}/registry`;
23
23
  }
24
24
 
25
25
  setAccessTokens(accessTokens: AccessTokens) {
@@ -27,7 +27,7 @@ export class RegistryApi {
27
27
  return this;
28
28
  }
29
29
 
30
- private async request(path = '', options: RequestInit = {}, region?: Region) {
30
+ private async request(path = '', options: RequestInit = {}) {
31
31
  const currentCommand =
32
32
  typeof process !== 'undefined' ? process.env?.REDOCLY_CLI_COMMAND || '' : '';
33
33
  const redoclyEnv = typeof process !== 'undefined' ? process.env?.REDOCLY_ENVIRONMENT || '' : '';
@@ -42,8 +42,8 @@ export class RegistryApi {
42
42
  }
43
43
 
44
44
  const response = await fetch(
45
- `${this.getBaseUrl(region)}${path}`,
46
- Object.assign({}, options, { headers })
45
+ `${this.getBaseUrl()}${path}`,
46
+ Object.assign({}, options, { headers, agent: getProxyAgent() })
47
47
  );
48
48
 
49
49
  if (response.status === 401) {
@@ -60,11 +60,10 @@ export class RegistryApi {
60
60
 
61
61
  async authStatus(
62
62
  accessToken: string,
63
- region: Region,
64
63
  verbose = false
65
64
  ): Promise<{ viewerId: string; organizations: string[] }> {
66
65
  try {
67
- const response = await this.request('', { headers: { authorization: accessToken } }, region);
66
+ const response = await this.request('', { headers: { authorization: accessToken } });
68
67
 
69
68
  return await response.json();
70
69
  } catch (error) {
@@ -97,8 +96,7 @@ export class RegistryApi {
97
96
  filename,
98
97
  isUpsert,
99
98
  }),
100
- },
101
- this.region
99
+ }
102
100
  );
103
101
 
104
102
  if (response.ok) {
@@ -120,26 +118,22 @@ export class RegistryApi {
120
118
  batchId,
121
119
  batchSize,
122
120
  }: PushApiParams) {
123
- const response = await this.request(
124
- `/${organizationId}/${name}/${version}`,
125
- {
126
- method: 'PUT',
127
- headers: {
128
- 'content-type': 'application/json',
129
- authorization: this.accessToken,
130
- } as HeadersInit,
131
- body: JSON.stringify({
132
- rootFilePath,
133
- filePaths,
134
- branch,
135
- isUpsert,
136
- isPublic,
137
- batchId,
138
- batchSize,
139
- }),
140
- },
141
- this.region
142
- );
121
+ const response = await this.request(`/${organizationId}/${name}/${version}`, {
122
+ method: 'PUT',
123
+ headers: {
124
+ 'content-type': 'application/json',
125
+ authorization: this.accessToken,
126
+ } as HeadersInit,
127
+ body: JSON.stringify({
128
+ rootFilePath,
129
+ filePaths,
130
+ branch,
131
+ isUpsert,
132
+ isPublic,
133
+ batchId,
134
+ batchSize,
135
+ }),
136
+ });
143
137
 
144
138
  if (response.ok) {
145
139
  return;
package/src/utils.ts CHANGED
@@ -5,9 +5,10 @@ import fetch from 'node-fetch';
5
5
  import * as pluralize from 'pluralize';
6
6
  import { parseYaml } from './js-yaml';
7
7
  import { UserContext } from './walk';
8
- import { HttpResolveConfig } from './config';
9
8
  import { env } from './env';
10
9
  import { logger, colorize } from './logger';
10
+ import { HttpResolveConfig } from './config';
11
+ import { HttpsProxyAgent } from 'https-proxy-agent';
11
12
 
12
13
  export { parseYaml, stringifyYaml } from './js-yaml';
13
14
 
@@ -278,3 +279,8 @@ export async function pause(ms: number): Promise<void> {
278
279
  function getUpdatedFieldName(updatedField: string, updatedObject?: string) {
279
280
  return `${typeof updatedObject !== 'undefined' ? `${updatedObject}.` : ''}${updatedField}`;
280
281
  }
282
+
283
+ export function getProxyAgent() {
284
+ const proxy = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
285
+ return proxy ? new HttpsProxyAgent(proxy) : undefined;
286
+ }