@redocly/openapi-core 1.10.2 → 1.10.3

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.
@@ -2,19 +2,13 @@ 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';
5
6
  import { env } from '../env';
6
7
  import { RegionalToken, RegionalTokenWithValidity } from './redocly-client-types';
7
8
  import { isNotEmptyObject } from '../utils';
8
9
  import { colorize } from '../logger';
9
10
 
10
11
  import type { AccessTokens, Region } from '../config/types';
11
- import {
12
- AVAILABLE_REGIONS,
13
- DEFAULT_REGION,
14
- DOMAINS,
15
- getRedoclyDomain,
16
- setRedoclyDomain,
17
- } from './domains';
18
12
 
19
13
  export const TOKEN_FILENAME = '.redocly-config.json';
20
14
 
@@ -29,7 +23,7 @@ export class RedoclyClient {
29
23
  this.loadTokens();
30
24
  this.domain = region ? DOMAINS[region] : env.REDOCLY_DOMAIN || DOMAINS[DEFAULT_REGION];
31
25
 
32
- setRedoclyDomain(this.domain);
26
+ env.REDOCLY_DOMAIN = this.domain; // isRedoclyRegistryURL depends on the value to be set
33
27
  this.registryApi = new RegistryApi(this.accessTokens, this.region);
34
28
  }
35
29
 
@@ -42,9 +36,9 @@ export class RedoclyClient {
42
36
  );
43
37
  }
44
38
 
45
- if (getRedoclyDomain()) {
39
+ if (env.REDOCLY_DOMAIN) {
46
40
  return (AVAILABLE_REGIONS.find(
47
- (region) => DOMAINS[region as Region] === getRedoclyDomain()
41
+ (region) => DOMAINS[region as Region] === env.REDOCLY_DOMAIN
48
42
  ) || DEFAULT_REGION) as Region;
49
43
  }
50
44
  return region || DEFAULT_REGION;
@@ -102,7 +96,7 @@ export class RedoclyClient {
102
96
  const allTokens = this.getAllTokens();
103
97
 
104
98
  const verifiedTokens = await Promise.allSettled(
105
- allTokens.map(({ token }) => this.verifyToken(token))
99
+ allTokens.map(({ token, region }) => this.verifyToken(token, region))
106
100
  );
107
101
 
108
102
  return allTokens
@@ -126,7 +120,7 @@ export class RedoclyClient {
126
120
  }
127
121
 
128
122
  try {
129
- await this.verifyToken(accessToken);
123
+ await this.verifyToken(accessToken, this.region);
130
124
 
131
125
  return true;
132
126
  } catch (err) {
@@ -144,16 +138,17 @@ export class RedoclyClient {
144
138
 
145
139
  async verifyToken(
146
140
  accessToken: string,
141
+ region: Region,
147
142
  verbose: boolean = false
148
143
  ): Promise<{ viewerId: string; organizations: string[] }> {
149
- return this.registryApi.authStatus(accessToken, verbose);
144
+ return this.registryApi.authStatus(accessToken, region, verbose);
150
145
  }
151
146
 
152
147
  async login(accessToken: string, verbose: boolean = false) {
153
148
  const credentialsPath = resolve(homedir(), TOKEN_FILENAME);
154
149
 
155
150
  try {
156
- await this.verifyToken(accessToken, verbose);
151
+ await this.verifyToken(accessToken, this.region, verbose);
157
152
  } catch (err) {
158
153
  throw new Error('Authorization failed. Please check if you entered a valid API key.');
159
154
  }
@@ -175,3 +170,18 @@ export class RedoclyClient {
175
170
  }
176
171
  }
177
172
  }
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 { getProxyAgent, isNotEmptyObject } from '../utils';
10
- import { getRedoclyDomain } from './domains';
9
+ import { DEFAULT_REGION, DOMAINS } from '../config/config';
10
+ import { isNotEmptyObject } from '../utils';
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() {
22
- return `https://api.${getRedoclyDomain()}/registry`;
21
+ getBaseUrl(region: Region = DEFAULT_REGION) {
22
+ return `https://api.${DOMAINS[region]}/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 = {}) {
30
+ private async request(path = '', options: RequestInit = {}, region?: Region) {
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()}${path}`,
46
- Object.assign({}, options, { headers, agent: getProxyAgent() })
45
+ `${this.getBaseUrl(region)}${path}`,
46
+ Object.assign({}, options, { headers })
47
47
  );
48
48
 
49
49
  if (response.status === 401) {
@@ -60,10 +60,11 @@ export class RegistryApi {
60
60
 
61
61
  async authStatus(
62
62
  accessToken: string,
63
+ region: Region,
63
64
  verbose = false
64
65
  ): Promise<{ viewerId: string; organizations: string[] }> {
65
66
  try {
66
- const response = await this.request('', { headers: { authorization: accessToken } });
67
+ const response = await this.request('', { headers: { authorization: accessToken } }, region);
67
68
 
68
69
  return await response.json();
69
70
  } catch (error) {
@@ -96,7 +97,8 @@ export class RegistryApi {
96
97
  filename,
97
98
  isUpsert,
98
99
  }),
99
- }
100
+ },
101
+ this.region
100
102
  );
101
103
 
102
104
  if (response.ok) {
@@ -118,22 +120,26 @@ export class RegistryApi {
118
120
  batchId,
119
121
  batchSize,
120
122
  }: PushApiParams) {
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
- });
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
+ );
137
143
 
138
144
  if (response.ok) {
139
145
  return;
package/src/utils.ts CHANGED
@@ -5,10 +5,9 @@ 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';
8
9
  import { env } from './env';
9
10
  import { logger, colorize } from './logger';
10
- import { HttpResolveConfig } from './config';
11
- import { HttpsProxyAgent } from 'https-proxy-agent';
12
11
 
13
12
  export { parseYaml, stringifyYaml } from './js-yaml';
14
13
 
@@ -275,8 +274,3 @@ export function nextTick() {
275
274
  function getUpdatedFieldName(updatedField: string, updatedObject?: string) {
276
275
  return `${typeof updatedObject !== 'undefined' ? `${updatedObject}.` : ''}${updatedField}`;
277
276
  }
278
-
279
- export function getProxyAgent() {
280
- const proxy = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
281
- return proxy ? new HttpsProxyAgent(proxy) : undefined;
282
- }