@redocly/openapi-core 1.0.0-beta.66 → 1.0.0-beta.70
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.
- package/__tests__/__snapshots__/bundle.test.ts.snap +126 -0
- package/__tests__/bundle.test.ts +53 -1
- package/__tests__/fixtures/refs/definitions.yaml +3 -0
- package/__tests__/fixtures/refs/external-request-body.yaml +13 -0
- package/__tests__/fixtures/refs/externalref.yaml +35 -0
- package/__tests__/fixtures/refs/hosted.yaml +35 -0
- package/__tests__/fixtures/refs/rename.yaml +1 -0
- package/__tests__/fixtures/refs/requestBody.yaml +9 -0
- package/__tests__/fixtures/refs/simple.yaml +1 -0
- package/__tests__/fixtures/refs/vendor.schema.yaml +20 -0
- package/__tests__/lint.test.ts +1 -1
- package/__tests__/login.test.ts +17 -0
- package/lib/bundle.d.ts +4 -0
- package/lib/bundle.js +25 -7
- package/lib/config/all.js +3 -0
- package/lib/config/config.d.ts +10 -0
- package/lib/config/config.js +7 -1
- package/lib/config/load.js +17 -8
- package/lib/config/minimal.js +1 -0
- package/lib/config/recommended.js +1 -0
- package/lib/index.d.ts +2 -2
- package/lib/lint.js +2 -0
- package/lib/redocly/index.d.ts +25 -20
- package/lib/redocly/index.js +77 -214
- package/lib/redocly/registry-api-types.d.ts +28 -0
- package/lib/redocly/registry-api-types.js +2 -0
- package/lib/redocly/registry-api.d.ts +14 -0
- package/lib/redocly/registry-api.js +105 -0
- package/lib/ref-utils.js +1 -2
- package/lib/rules/common/no-invalid-parameter-examples.d.ts +1 -0
- package/lib/rules/common/no-invalid-parameter-examples.js +25 -0
- package/lib/rules/common/no-invalid-schema-examples.d.ts +1 -0
- package/lib/rules/common/no-invalid-schema-examples.js +23 -0
- package/lib/rules/common/operation-4xx-response.d.ts +2 -0
- package/lib/rules/common/operation-4xx-response.js +17 -0
- package/lib/rules/common/paths-kebab-case.js +1 -1
- package/lib/rules/common/registry-dependencies.js +4 -7
- package/lib/rules/oas2/index.d.ts +3 -0
- package/lib/rules/oas2/index.js +6 -0
- package/lib/rules/oas3/index.js +6 -0
- package/lib/rules/oas3/no-invalid-media-type-examples.js +5 -26
- package/lib/rules/oas3/no-server-trailing-slash.js +1 -1
- package/lib/rules/utils.d.ts +3 -0
- package/lib/rules/utils.js +26 -1
- package/lib/typings/openapi.d.ts +3 -0
- package/lib/utils.d.ts +1 -0
- package/lib/utils.js +5 -1
- package/lib/walk.d.ts +2 -0
- package/lib/walk.js +7 -0
- package/package.json +1 -1
- package/src/bundle.ts +51 -9
- package/src/config/__tests__/load.test.ts +35 -0
- package/src/config/all.ts +3 -0
- package/src/config/config.ts +11 -0
- package/src/config/load.ts +20 -9
- package/src/config/minimal.ts +1 -0
- package/src/config/recommended.ts +1 -0
- package/src/index.ts +2 -8
- package/src/lint.ts +2 -0
- package/src/redocly/__tests__/redocly-client.test.ts +114 -0
- package/src/redocly/index.ts +90 -227
- package/src/redocly/registry-api-types.ts +31 -0
- package/src/redocly/registry-api.ts +110 -0
- package/src/ref-utils.ts +1 -3
- package/src/rules/common/__tests__/operation-4xx-response.test.ts +108 -0
- package/src/rules/common/__tests__/paths-kebab-case.test.ts +23 -0
- package/src/rules/common/no-invalid-parameter-examples.ts +36 -0
- package/src/rules/common/no-invalid-schema-examples.ts +27 -0
- package/src/rules/common/operation-4xx-response.ts +17 -0
- package/src/rules/common/paths-kebab-case.ts +1 -1
- package/src/rules/common/registry-dependencies.ts +6 -8
- package/src/rules/oas2/index.ts +6 -0
- package/src/rules/oas3/__tests__/no-server-trailing-slash.test.ts +19 -0
- package/src/rules/oas3/index.ts +6 -0
- package/src/rules/oas3/no-invalid-media-type-examples.ts +16 -36
- package/src/rules/oas3/no-server-trailing-slash.ts +1 -1
- package/src/rules/utils.ts +43 -2
- package/src/typings/openapi.ts +4 -0
- package/src/utils.ts +5 -1
- package/src/walk.ts +10 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/lib/redocly/query.d.ts +0 -4
- package/lib/redocly/query.js +0 -44
- package/src/redocly/query.ts +0 -38
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { RedoclyClient } from '../index';
|
|
2
|
+
|
|
3
|
+
describe('RedoclyClient', () => {
|
|
4
|
+
const REDOCLY_DOMAIN_US = 'redoc.ly';
|
|
5
|
+
const REDOCLY_DOMAIN_EU = 'eu.redocly.com';
|
|
6
|
+
const REDOCLY_AUTHORIZATION_TOKEN = 'redocly-auth-token';
|
|
7
|
+
const testRedoclyDomain = 'redoclyDomain.com';
|
|
8
|
+
const testToken = 'test-token';
|
|
9
|
+
|
|
10
|
+
afterEach(() => {
|
|
11
|
+
delete process.env.REDOCLY_DOMAIN;
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('should resolve the US domain by default', () => {
|
|
15
|
+
const client = new RedoclyClient();
|
|
16
|
+
expect(client.domain).toBe(REDOCLY_DOMAIN_US);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('should resolve domain from RedoclyDomain env', () => {
|
|
20
|
+
process.env.REDOCLY_DOMAIN = testRedoclyDomain;
|
|
21
|
+
const client = new RedoclyClient();
|
|
22
|
+
expect(client.domain).toBe(testRedoclyDomain);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should resolve a domain by US region', () => {
|
|
26
|
+
const client = new RedoclyClient('us');
|
|
27
|
+
expect(client.domain).toBe(REDOCLY_DOMAIN_US);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('should resolve a domain by EU region', () => {
|
|
31
|
+
const client = new RedoclyClient('eu');
|
|
32
|
+
expect(client.domain).toBe(REDOCLY_DOMAIN_EU);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should resolve domain by EU region prioritizing flag over env variable', () => {
|
|
36
|
+
process.env.REDOCLY_DOMAIN = testRedoclyDomain;
|
|
37
|
+
const client = new RedoclyClient('eu');
|
|
38
|
+
expect(client.domain).toBe(REDOCLY_DOMAIN_EU);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('should resolve domain by US region prioritizing flag over env variable', () => {
|
|
42
|
+
process.env.REDOCLY_DOMAIN = testRedoclyDomain;
|
|
43
|
+
const client = new RedoclyClient('us');
|
|
44
|
+
expect(client.domain).toBe(REDOCLY_DOMAIN_US);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('should resolve valid tokens data', async () => {
|
|
48
|
+
let spy = jest.spyOn(RedoclyClient.prototype, 'readCredentialsFile').mockImplementation(() => {
|
|
49
|
+
return { us: "accessToken", eu: "eu-accessToken" }
|
|
50
|
+
});
|
|
51
|
+
const client = new RedoclyClient();
|
|
52
|
+
const tokens = await client.getValidTokens();
|
|
53
|
+
expect(tokens).toStrictEqual([
|
|
54
|
+
{ region: 'us', token: 'accessToken', valid: true },
|
|
55
|
+
{ region: 'eu', token: 'eu-accessToken', valid: true }
|
|
56
|
+
]);
|
|
57
|
+
spy.mockRestore();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('should not call setAccessTokens by default', () => {
|
|
61
|
+
let spy = jest.spyOn(RedoclyClient.prototype, 'readCredentialsFile').mockImplementation(() => ({}));
|
|
62
|
+
jest.spyOn(RedoclyClient.prototype, 'setAccessTokens').mockImplementation();
|
|
63
|
+
const client = new RedoclyClient();
|
|
64
|
+
expect(client.setAccessTokens).not.toHaveBeenCalled()
|
|
65
|
+
spy.mockRestore();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('should set correct accessTokens - backward compatibility: default US region', () => {
|
|
69
|
+
let spy = jest.spyOn(RedoclyClient.prototype, 'readCredentialsFile').mockImplementation(() => ({ token: testToken }));
|
|
70
|
+
jest.spyOn(RedoclyClient.prototype, 'setAccessTokens').mockImplementation();
|
|
71
|
+
const client = new RedoclyClient();
|
|
72
|
+
expect(client.setAccessTokens).toBeCalledWith(
|
|
73
|
+
expect.objectContaining({ us: testToken })
|
|
74
|
+
);
|
|
75
|
+
spy.mockRestore();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('should set correct accessTokens - backward compatibility: EU region', () => {
|
|
79
|
+
let spy = jest.spyOn(RedoclyClient.prototype, 'readCredentialsFile').mockImplementation(() => ({ token: testToken }));
|
|
80
|
+
jest.spyOn(RedoclyClient.prototype, 'setAccessTokens').mockImplementation();
|
|
81
|
+
const client = new RedoclyClient('eu');
|
|
82
|
+
expect(client.setAccessTokens).toBeCalledWith(
|
|
83
|
+
expect.objectContaining({ eu: testToken })
|
|
84
|
+
);
|
|
85
|
+
spy.mockRestore();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('should set correct accessTokens - REDOCLY_AUTHORIZATION env', () => {
|
|
89
|
+
process.env.REDOCLY_AUTHORIZATION = REDOCLY_AUTHORIZATION_TOKEN;
|
|
90
|
+
let spy = jest.spyOn(RedoclyClient.prototype, 'readCredentialsFile').mockImplementation();
|
|
91
|
+
jest.spyOn(RedoclyClient.prototype, 'setAccessTokens').mockImplementation();
|
|
92
|
+
const client = new RedoclyClient();
|
|
93
|
+
expect(client.setAccessTokens).toHaveBeenNthCalledWith(1, { "us": REDOCLY_AUTHORIZATION_TOKEN });
|
|
94
|
+
spy.mockRestore();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('should set correct accessTokens prioritizing REDOCLY_AUTHORIZATION env over token in file', () => {
|
|
98
|
+
process.env.REDOCLY_AUTHORIZATION = REDOCLY_AUTHORIZATION_TOKEN;
|
|
99
|
+
let spy = jest.spyOn(RedoclyClient.prototype, 'readCredentialsFile').mockImplementation(() => ({ token: testToken }));
|
|
100
|
+
jest.spyOn(RedoclyClient.prototype, 'setAccessTokens').mockImplementation();
|
|
101
|
+
const client = new RedoclyClient();
|
|
102
|
+
expect(client.setAccessTokens).toHaveBeenNthCalledWith(2, { "us": REDOCLY_AUTHORIZATION_TOKEN });
|
|
103
|
+
spy.mockRestore();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('should set correct accessTokens prioritizing REDOCLY_AUTHORIZATION env over EU token', () => {
|
|
107
|
+
process.env.REDOCLY_AUTHORIZATION = REDOCLY_AUTHORIZATION_TOKEN;
|
|
108
|
+
let spy = jest.spyOn(RedoclyClient.prototype, 'readCredentialsFile').mockImplementation(() => ({ us: testToken }));
|
|
109
|
+
jest.spyOn(RedoclyClient.prototype, 'setAccessTokens').mockImplementation();
|
|
110
|
+
const client = new RedoclyClient('eu');
|
|
111
|
+
expect(client.setAccessTokens).toHaveBeenNthCalledWith(2, { "eu": REDOCLY_AUTHORIZATION_TOKEN });
|
|
112
|
+
spy.mockRestore();
|
|
113
|
+
});
|
|
114
|
+
});
|
package/src/redocly/index.ts
CHANGED
|
@@ -1,65 +1,105 @@
|
|
|
1
1
|
import { existsSync, readFileSync, writeFileSync, unlinkSync } from 'fs';
|
|
2
2
|
import { resolve } from 'path';
|
|
3
3
|
import { homedir } from 'os';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import { red, green, gray } from 'colorette';
|
|
5
|
+
import { RegistryApi } from './registry-api';
|
|
6
|
+
import { AccessTokens, DEFAULT_REGION, DOMAINS, Region } from '../config/config';
|
|
7
|
+
import { isNotEmptyObject } from '../utils';
|
|
6
8
|
|
|
7
9
|
const TOKEN_FILENAME = '.redocly-config.json';
|
|
8
10
|
|
|
9
11
|
export class RedoclyClient {
|
|
10
|
-
private
|
|
12
|
+
private accessTokens: AccessTokens = {};
|
|
13
|
+
private region: Region;
|
|
14
|
+
domain: string;
|
|
15
|
+
registryApi: RegistryApi;
|
|
16
|
+
|
|
17
|
+
constructor(region?: Region) {
|
|
18
|
+
this.region = this.loadRegion(region);
|
|
19
|
+
this.loadTokens();
|
|
20
|
+
this.domain = region
|
|
21
|
+
? DOMAINS[region]
|
|
22
|
+
: process.env.REDOCLY_DOMAIN || DOMAINS[DEFAULT_REGION];
|
|
23
|
+
this.registryApi = new RegistryApi(this.accessTokens, this.region);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
loadRegion(region?: Region) {
|
|
27
|
+
if (region && !DOMAINS[region]) {
|
|
28
|
+
process.stdout.write(
|
|
29
|
+
red(`Invalid argument: region in config file.\nGiven: ${green(region)}, choices: "us", "eu".\n`),
|
|
30
|
+
);
|
|
31
|
+
process.exit(1);
|
|
32
|
+
}
|
|
33
|
+
return region || DEFAULT_REGION;
|
|
34
|
+
}
|
|
11
35
|
|
|
12
|
-
|
|
13
|
-
this.
|
|
36
|
+
hasTokens(): boolean {
|
|
37
|
+
return isNotEmptyObject(this.accessTokens);
|
|
14
38
|
}
|
|
15
39
|
|
|
16
|
-
|
|
17
|
-
|
|
40
|
+
setAccessTokens(accessTokens: AccessTokens) {
|
|
41
|
+
this.accessTokens = accessTokens;
|
|
18
42
|
}
|
|
19
43
|
|
|
20
|
-
|
|
44
|
+
loadTokens(): void {
|
|
45
|
+
const credentialsPath = resolve(homedir(), TOKEN_FILENAME);
|
|
46
|
+
const credentials = this.readCredentialsFile(credentialsPath);
|
|
47
|
+
if (isNotEmptyObject(credentials)) {
|
|
48
|
+
this.setAccessTokens({
|
|
49
|
+
...credentials,
|
|
50
|
+
...(credentials.token && !credentials[this.region] && {
|
|
51
|
+
[this.region]: credentials.token
|
|
52
|
+
})
|
|
53
|
+
})
|
|
54
|
+
}
|
|
21
55
|
if (process.env.REDOCLY_AUTHORIZATION) {
|
|
22
|
-
this.
|
|
23
|
-
|
|
56
|
+
this.setAccessTokens({
|
|
57
|
+
...this.accessTokens,
|
|
58
|
+
[this.region]: process.env.REDOCLY_AUTHORIZATION
|
|
59
|
+
})
|
|
24
60
|
}
|
|
61
|
+
}
|
|
25
62
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
63
|
+
async getValidTokens(): Promise<{
|
|
64
|
+
region: string;
|
|
65
|
+
token: string;
|
|
66
|
+
valid: boolean;
|
|
67
|
+
}[]> {
|
|
68
|
+
return (await Promise.all(
|
|
69
|
+
Object.entries(this.accessTokens).map(async ([key, value]) => {
|
|
70
|
+
return { region: key, token: value, valid: await this.verifyToken(value, key as Region) }
|
|
71
|
+
})
|
|
72
|
+
)).filter(item => Boolean(item.valid));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async getTokens() {
|
|
76
|
+
return this.hasTokens() ? await this.getValidTokens() : [];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async isAuthorizedWithRedoclyByRegion(): Promise<boolean> {
|
|
80
|
+
if (!this.hasTokens()) return false;
|
|
81
|
+
const accessToken = this.accessTokens[this.region];
|
|
82
|
+
return !!accessToken && await this.verifyToken(accessToken, this.region);
|
|
31
83
|
}
|
|
32
84
|
|
|
33
85
|
async isAuthorizedWithRedocly(): Promise<boolean> {
|
|
34
|
-
return this.
|
|
86
|
+
return this.hasTokens() && isNotEmptyObject(await this.getValidTokens());
|
|
35
87
|
}
|
|
36
88
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const authDetails = await RedoclyClient.authorize(accessToken, { verbose });
|
|
40
|
-
if (!authDetails) return false;
|
|
41
|
-
return true;
|
|
89
|
+
readCredentialsFile(credentialsPath: string) {
|
|
90
|
+
return existsSync(credentialsPath) ? JSON.parse(readFileSync(credentialsPath, 'utf-8')) : {};
|
|
42
91
|
}
|
|
43
92
|
|
|
44
|
-
async
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
process.stderr.write(
|
|
48
|
-
`${yellow(
|
|
49
|
-
'Warning:',
|
|
50
|
-
)} invalid Redocly API key. Use "npx @redocly/openapi-cli login" to provide your API key\n`,
|
|
51
|
-
);
|
|
52
|
-
return undefined;
|
|
53
|
-
}
|
|
54
|
-
return this.accessToken;
|
|
93
|
+
async verifyToken(accessToken: string, region: Region, verbose: boolean = false): Promise<boolean> {
|
|
94
|
+
if (!accessToken) return false;
|
|
95
|
+
return this.registryApi.authStatus(accessToken, region, verbose);
|
|
55
96
|
}
|
|
56
97
|
|
|
57
98
|
async login(accessToken: string, verbose: boolean = false) {
|
|
58
99
|
const credentialsPath = resolve(homedir(), TOKEN_FILENAME);
|
|
59
100
|
process.stdout.write(gray('\n Logging in...\n'));
|
|
60
101
|
|
|
61
|
-
const authorized = await this.verifyToken(accessToken, verbose);
|
|
62
|
-
|
|
102
|
+
const authorized = await this.verifyToken(accessToken, this.region, verbose);
|
|
63
103
|
if (!authorized) {
|
|
64
104
|
process.stdout.write(
|
|
65
105
|
red('Authorization failed. Please check if you entered a valid API key.\n'),
|
|
@@ -67,11 +107,12 @@ export class RedoclyClient {
|
|
|
67
107
|
process.exit(1);
|
|
68
108
|
}
|
|
69
109
|
|
|
70
|
-
this.accessToken = accessToken;
|
|
71
110
|
const credentials = {
|
|
72
|
-
|
|
111
|
+
...this.readCredentialsFile(credentialsPath),
|
|
112
|
+
[this.region!]: accessToken,
|
|
73
113
|
};
|
|
74
|
-
|
|
114
|
+
this.accessTokens = credentials;
|
|
115
|
+
this.registryApi.setAccessTokens(credentials);
|
|
75
116
|
writeFileSync(credentialsPath, JSON.stringify(credentials, null, 2));
|
|
76
117
|
process.stdout.write(green(' Authorization confirmed. ✅\n\n'));
|
|
77
118
|
}
|
|
@@ -83,198 +124,20 @@ export class RedoclyClient {
|
|
|
83
124
|
}
|
|
84
125
|
process.stdout.write('Logged out from the Redocly account. ✋\n');
|
|
85
126
|
}
|
|
127
|
+
}
|
|
86
128
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
static async authorize(accessToken: string, options: { queryName?: string; verbose?: boolean }) {
|
|
95
|
-
const { queryName = '', verbose = false } = options;
|
|
96
|
-
try {
|
|
97
|
-
const queryStr = `query ${queryName}{ viewer { id } }`;
|
|
98
|
-
|
|
99
|
-
return await query(queryStr, {}, { Authorization: accessToken });
|
|
100
|
-
} catch (e) {
|
|
101
|
-
if (verbose) console.log(e);
|
|
102
|
-
return null;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
async updateDependencies(dependencies: string[] | undefined): Promise<void> {
|
|
107
|
-
const definitionId = process.env.DEFINITION;
|
|
108
|
-
const versionId = process.env.DEFINITION;
|
|
109
|
-
const branchId = process.env.BRANCH;
|
|
110
|
-
|
|
111
|
-
if (!definitionId || !versionId || !branchId) return;
|
|
112
|
-
|
|
113
|
-
await this.query(
|
|
114
|
-
`
|
|
115
|
-
mutation UpdateBranchDependenciesFromURLs(
|
|
116
|
-
$urls: [String!]!
|
|
117
|
-
$definitionId: Int!
|
|
118
|
-
$versionId: Int!
|
|
119
|
-
$branchId: Int!
|
|
120
|
-
) {
|
|
121
|
-
updateBranchDependenciesFromURLs(
|
|
122
|
-
definitionId: $definitionId
|
|
123
|
-
versionId: $versionId
|
|
124
|
-
branchId: $branchId
|
|
125
|
-
urls: $urls
|
|
126
|
-
) {
|
|
127
|
-
branchName
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
`,
|
|
131
|
-
{
|
|
132
|
-
urls: dependencies || [],
|
|
133
|
-
definitionId: parseInt(definitionId, 10),
|
|
134
|
-
versionId: parseInt(versionId, 10),
|
|
135
|
-
branchId: parseInt(branchId, 10),
|
|
136
|
-
},
|
|
137
|
-
);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
updateDefinitionVersion(definitionId: number, versionId: number, updatePatch: object): Promise<void> {
|
|
141
|
-
return this.query(`
|
|
142
|
-
mutation UpdateDefinitionVersion($definitionId: Int!, $versionId: Int!, $updatePatch: DefinitionVersionPatch!) {
|
|
143
|
-
updateDefinitionVersionByDefinitionIdAndId(input: {definitionId: $definitionId, id: $versionId, patch: $updatePatch}) {
|
|
144
|
-
definitionVersion {
|
|
145
|
-
...VersionDetails
|
|
146
|
-
__typename
|
|
147
|
-
}
|
|
148
|
-
__typename
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
fragment VersionDetails on DefinitionVersion {
|
|
153
|
-
id
|
|
154
|
-
nodeId
|
|
155
|
-
uuid
|
|
156
|
-
definitionId
|
|
157
|
-
name
|
|
158
|
-
description
|
|
159
|
-
sourceType
|
|
160
|
-
source
|
|
161
|
-
registryAccess
|
|
162
|
-
__typename
|
|
163
|
-
}
|
|
164
|
-
`,
|
|
165
|
-
{
|
|
166
|
-
definitionId,
|
|
167
|
-
versionId,
|
|
168
|
-
updatePatch,
|
|
169
|
-
},
|
|
170
|
-
);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
getOrganizationId(organizationId: string) {
|
|
174
|
-
return this.query(`
|
|
175
|
-
query ($organizationId: String!) {
|
|
176
|
-
organizationById(id: $organizationId) {
|
|
177
|
-
id
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
`, {
|
|
181
|
-
organizationId
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
getDefinitionByName(name: string, organizationId: string) {
|
|
186
|
-
return this.query(`
|
|
187
|
-
query ($name: String!, $organizationId: String!) {
|
|
188
|
-
definition: definitionByOrganizationIdAndName(name: $name, organizationId: $organizationId) {
|
|
189
|
-
id
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
`, {
|
|
193
|
-
name,
|
|
194
|
-
organizationId
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
createDefinition(organizationId: string, name: string) {
|
|
199
|
-
return this.query(`
|
|
200
|
-
mutation CreateDefinition($organizationId: String!, $name: String!) {
|
|
201
|
-
def: createDefinition(input: {organizationId: $organizationId, name: $name }) {
|
|
202
|
-
definition {
|
|
203
|
-
id
|
|
204
|
-
nodeId
|
|
205
|
-
name
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
`, {
|
|
210
|
-
organizationId,
|
|
211
|
-
name
|
|
212
|
-
})
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
createDefinitionVersion(definitionId: string, name: string, sourceType: string, source: any) {
|
|
216
|
-
return this.query(`
|
|
217
|
-
mutation CreateVersion($definitionId: Int!, $name: String!, $sourceType: DvSourceType!, $source: JSON) {
|
|
218
|
-
createDefinitionVersion(input: {definitionId: $definitionId, name: $name, sourceType: $sourceType, source: $source }) {
|
|
219
|
-
definitionVersion {
|
|
220
|
-
id
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
`, {
|
|
225
|
-
definitionId,
|
|
226
|
-
name,
|
|
227
|
-
sourceType,
|
|
228
|
-
source
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
getSignedUrl(organizationId: string, filesHash: string, fileName: string) {
|
|
233
|
-
return this.query(`
|
|
234
|
-
query ($organizationId: String!, $filesHash: String!, $fileName: String!) {
|
|
235
|
-
signFileUploadCLI(organizationId: $organizationId, filesHash: $filesHash, fileName: $fileName) {
|
|
236
|
-
signedFileUrl
|
|
237
|
-
uploadedFilePath
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
`, {
|
|
241
|
-
organizationId,
|
|
242
|
-
filesHash,
|
|
243
|
-
fileName
|
|
244
|
-
})
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
getDefinitionVersion(organizationId: string, definitionName: string, versionName: string) {
|
|
248
|
-
return this.query(`
|
|
249
|
-
query ($organizationId: String!, $definitionName: String!, $versionName: String!) {
|
|
250
|
-
version: definitionVersionByOrganizationDefinitionAndName(organizationId: $organizationId, definitionName: $definitionName, versionName: $versionName) {
|
|
251
|
-
id
|
|
252
|
-
definitionId
|
|
253
|
-
defaultBranch {
|
|
254
|
-
name
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
`, {
|
|
259
|
-
organizationId,
|
|
260
|
-
definitionName,
|
|
261
|
-
versionName
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
static isRegistryURL(link: string): boolean {
|
|
266
|
-
const domain = process.env.REDOCLY_DOMAIN || 'redoc.ly';
|
|
267
|
-
if (!link.startsWith(`https://api.${domain}/registry/`)) return false;
|
|
268
|
-
const registryPath = link.replace(`https://api.${domain}/registry/`, '');
|
|
129
|
+
export function isRedoclyRegistryURL(link: string): boolean {
|
|
130
|
+
const domain = process.env.REDOCLY_DOMAIN || DOMAINS[DEFAULT_REGION];
|
|
131
|
+
if (!link.startsWith(`https://api.${domain}/registry/`)) return false;
|
|
132
|
+
const registryPath = link.replace(`https://api.${domain}/registry/`, '');
|
|
269
133
|
|
|
270
|
-
|
|
134
|
+
const pathParts = registryPath.split('/');
|
|
271
135
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
136
|
+
// we can be sure, that there is job UUID present
|
|
137
|
+
// (org, definition, version, bundle, branch, job, "openapi.yaml" 🤦♂️)
|
|
138
|
+
// so skip this link.
|
|
139
|
+
// FIXME
|
|
140
|
+
if (pathParts.length === 7) return false;
|
|
277
141
|
|
|
278
|
-
|
|
279
|
-
}
|
|
142
|
+
return true;
|
|
280
143
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export namespace RegistryApiTypes {
|
|
2
|
+
interface VersionParams {
|
|
3
|
+
organizationId: string;
|
|
4
|
+
name: string;
|
|
5
|
+
version: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface PrepareFileuploadParams extends VersionParams {
|
|
9
|
+
filesHash: string;
|
|
10
|
+
filename: string;
|
|
11
|
+
isUpsert?: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface PushApiParams extends VersionParams {
|
|
15
|
+
rootFilePath: string;
|
|
16
|
+
filePaths: string[];
|
|
17
|
+
branch?: string;
|
|
18
|
+
isUpsert?: boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface PrepareFileuploadOKResponse {
|
|
22
|
+
filePath: string;
|
|
23
|
+
signedUploadUrl: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface NotFoundProblemResponse {
|
|
27
|
+
status: 404;
|
|
28
|
+
title: 'Not Found';
|
|
29
|
+
code: 'ORGANIZATION_NOT_FOUND' | 'API_VERSION_NOT_FOUND';
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import fetch, { RequestInit, HeadersInit } from 'node-fetch';
|
|
2
|
+
import { RegistryApiTypes } from './registry-api-types';
|
|
3
|
+
import { AccessTokens, Region, DEFAULT_REGION, DOMAINS } from '../config/config';
|
|
4
|
+
import { isNotEmptyObject } from '../utils';
|
|
5
|
+
const version = require('../../package.json').version;
|
|
6
|
+
|
|
7
|
+
export class RegistryApi {
|
|
8
|
+
constructor(private accessTokens: AccessTokens, private region: Region) {}
|
|
9
|
+
|
|
10
|
+
get accessToken() {
|
|
11
|
+
return isNotEmptyObject(this.accessTokens) && this.accessTokens[this.region];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
getBaseUrl(region: Region = DEFAULT_REGION) {
|
|
15
|
+
return `https://api.${DOMAINS[region]}/registry`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
setAccessTokens(accessTokens: AccessTokens) {
|
|
19
|
+
this.accessTokens = accessTokens;
|
|
20
|
+
return this;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
private async request(path = '', options: RequestInit = {}, region?: Region) {
|
|
24
|
+
const headers = Object.assign({}, options.headers || {}, { 'x-redocly-cli-version': version });
|
|
25
|
+
if (!headers.hasOwnProperty('authorization')) { throw new Error('Unauthorized'); }
|
|
26
|
+
const response = await fetch(`${this.getBaseUrl(region)}${path}`, Object.assign({}, options, { headers }));
|
|
27
|
+
if (response.status === 401) { throw new Error('Unauthorized'); }
|
|
28
|
+
if (response.status === 404) {
|
|
29
|
+
const body: RegistryApiTypes.NotFoundProblemResponse = await response.json();
|
|
30
|
+
throw new Error(body.code);
|
|
31
|
+
}
|
|
32
|
+
return response;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async authStatus(accessToken: string, region: Region, verbose = false) {
|
|
36
|
+
try {
|
|
37
|
+
const response = await this.request('', { headers: { authorization: accessToken }}, region);
|
|
38
|
+
return response.ok;
|
|
39
|
+
} catch (error) {
|
|
40
|
+
if (verbose) {
|
|
41
|
+
console.log(error);
|
|
42
|
+
}
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async prepareFileUpload({
|
|
48
|
+
organizationId,
|
|
49
|
+
name,
|
|
50
|
+
version,
|
|
51
|
+
filesHash,
|
|
52
|
+
filename,
|
|
53
|
+
isUpsert,
|
|
54
|
+
}: RegistryApiTypes.PrepareFileuploadParams): Promise<RegistryApiTypes.PrepareFileuploadOKResponse> {
|
|
55
|
+
const response = await this.request(
|
|
56
|
+
`/${organizationId}/${name}/${version}/prepare-file-upload`,
|
|
57
|
+
{
|
|
58
|
+
method: 'POST',
|
|
59
|
+
headers: {
|
|
60
|
+
'content-type': 'application/json',
|
|
61
|
+
authorization: this.accessToken,
|
|
62
|
+
} as HeadersInit,
|
|
63
|
+
body: JSON.stringify({
|
|
64
|
+
filesHash,
|
|
65
|
+
filename,
|
|
66
|
+
isUpsert,
|
|
67
|
+
}),
|
|
68
|
+
},
|
|
69
|
+
this.region
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
if (response.ok) {
|
|
73
|
+
return response.json();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
throw new Error('Could not prepare file upload');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async pushApi({
|
|
80
|
+
organizationId,
|
|
81
|
+
name,
|
|
82
|
+
version,
|
|
83
|
+
rootFilePath,
|
|
84
|
+
filePaths,
|
|
85
|
+
branch,
|
|
86
|
+
isUpsert,
|
|
87
|
+
}: RegistryApiTypes.PushApiParams) {
|
|
88
|
+
const response = await this.request(`/${organizationId}/${name}/${version}`, {
|
|
89
|
+
method: 'PUT',
|
|
90
|
+
headers: {
|
|
91
|
+
'content-type': 'application/json',
|
|
92
|
+
authorization: this.accessToken
|
|
93
|
+
} as HeadersInit,
|
|
94
|
+
body: JSON.stringify({
|
|
95
|
+
rootFilePath,
|
|
96
|
+
filePaths,
|
|
97
|
+
branch,
|
|
98
|
+
isUpsert,
|
|
99
|
+
}),
|
|
100
|
+
},
|
|
101
|
+
this.region
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
if (response.ok) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
throw new Error('Could not push api');
|
|
109
|
+
}
|
|
110
|
+
}
|
package/src/ref-utils.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { sep as platformDependentSeparator } from 'path';
|
|
2
|
-
|
|
3
1
|
import { Source } from './resolve';
|
|
4
2
|
import { OasRef } from './typings/openapi';
|
|
5
3
|
|
|
@@ -61,7 +59,7 @@ export function pointerBaseName(pointer: string) {
|
|
|
61
59
|
}
|
|
62
60
|
|
|
63
61
|
export function refBaseName(ref: string) {
|
|
64
|
-
const parts = ref.split(
|
|
62
|
+
const parts = ref.split(/[\/\\]/); // split by '\' and '/'
|
|
65
63
|
return parts[parts.length - 1].split('.')[0];
|
|
66
64
|
}
|
|
67
65
|
|