@backstage/plugin-catalog-backend-module-openapi 0.1.0 → 0.1.2-next.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.
- package/CHANGELOG.md +47 -0
- package/README.md +26 -0
- package/dist/index.cjs.js +63 -16
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +10 -3
- package/package.json +9 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,52 @@
|
|
|
1
1
|
# @backstage/plugin-catalog-backend-module-openapi
|
|
2
2
|
|
|
3
|
+
## 0.1.2-next.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies
|
|
8
|
+
- @backstage/backend-common@0.15.1-next.0
|
|
9
|
+
- @backstage/plugin-catalog-backend@1.3.2-next.0
|
|
10
|
+
- @backstage/integration@1.3.1-next.0
|
|
11
|
+
- @backstage/plugin-catalog-node@1.0.2-next.0
|
|
12
|
+
|
|
13
|
+
## 0.1.1
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- b50e8e533b: Add an `$openapi` placeholder resolver that supports more use cases for resolving `$ref` instances. This means that the quite recently added `OpenApiRefProcessor` has been deprecated in favor of the `openApiPlaceholderResolver`.
|
|
18
|
+
|
|
19
|
+
An example of how to use it can be seen below.
|
|
20
|
+
|
|
21
|
+
```yaml
|
|
22
|
+
apiVersion: backstage.io/v1alpha1
|
|
23
|
+
kind: API
|
|
24
|
+
metadata:
|
|
25
|
+
name: example
|
|
26
|
+
description: Example API
|
|
27
|
+
spec:
|
|
28
|
+
type: openapi
|
|
29
|
+
lifecycle: production
|
|
30
|
+
owner: team
|
|
31
|
+
definition:
|
|
32
|
+
$openapi: ./spec/openapi.yaml # by using $openapi Backstage will now resolve all $ref instances
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
- Updated dependencies
|
|
36
|
+
- @backstage/backend-common@0.15.0
|
|
37
|
+
- @backstage/plugin-catalog-node@1.0.1
|
|
38
|
+
- @backstage/integration@1.3.0
|
|
39
|
+
- @backstage/plugin-catalog-backend@1.3.1
|
|
40
|
+
|
|
41
|
+
## 0.1.1-next.0
|
|
42
|
+
|
|
43
|
+
### Patch Changes
|
|
44
|
+
|
|
45
|
+
- Updated dependencies
|
|
46
|
+
- @backstage/backend-common@0.15.0-next.0
|
|
47
|
+
- @backstage/integration@1.3.0-next.0
|
|
48
|
+
- @backstage/plugin-catalog-backend@1.3.1-next.0
|
|
49
|
+
|
|
3
50
|
## 0.1.0
|
|
4
51
|
|
|
5
52
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -15,6 +15,32 @@ yarn add --cwd packages/backend @backstage/plugin-catalog-backend-module-openapi
|
|
|
15
15
|
|
|
16
16
|
### Adding the plugin to your `packages/backend`
|
|
17
17
|
|
|
18
|
+
#### **openApiPlaceholderResolver**
|
|
19
|
+
|
|
20
|
+
The placeholder resolver can be added by importing `openApiPlaceholderResolver` in `src/plugins/catalog.ts` in your `backend` package and adding the following.
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
builder.setPlaceholderResolver('openapi', openApiPlaceholderResolver);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
This allows you to use the `$openapi` placeholder when referencing your OpenAPI specification. This will then resolve all `$ref` instances in your specification.
|
|
27
|
+
|
|
28
|
+
```yaml
|
|
29
|
+
apiVersion: backstage.io/v1alpha1
|
|
30
|
+
kind: API
|
|
31
|
+
metadata:
|
|
32
|
+
name: example
|
|
33
|
+
description: Example API
|
|
34
|
+
spec:
|
|
35
|
+
type: openapi
|
|
36
|
+
lifecycle: production
|
|
37
|
+
owner: team
|
|
38
|
+
definition:
|
|
39
|
+
$openapi: ./spec/openapi.yaml # by using $openapi Backstage will now resolve all $ref instances
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
#### **OpenAPIRefProcessor** (deprecated)
|
|
43
|
+
|
|
18
44
|
The processor can be added by importing `OpenApiRefProcessor` in `src/plugins/catalog.ts` in your `backend` package and adding the following.
|
|
19
45
|
|
|
20
46
|
```ts
|
package/dist/index.cjs.js
CHANGED
|
@@ -6,6 +6,7 @@ var integration = require('@backstage/integration');
|
|
|
6
6
|
var SwaggerParser = require('@apidevtools/swagger-parser');
|
|
7
7
|
var yaml = require('yaml');
|
|
8
8
|
var path = require('path');
|
|
9
|
+
var pluginCatalogNode = require('@backstage/plugin-catalog-node');
|
|
9
10
|
|
|
10
11
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
11
12
|
|
|
@@ -38,7 +39,7 @@ const getProtocol = (refPath) => {
|
|
|
38
39
|
}
|
|
39
40
|
return void 0;
|
|
40
41
|
};
|
|
41
|
-
async function bundleOpenApiSpecification(specification,
|
|
42
|
+
async function bundleOpenApiSpecification(specification, baseUrl, read, resolveUrl) {
|
|
42
43
|
const fileUrlReaderResolver = {
|
|
43
44
|
canRead: (file) => {
|
|
44
45
|
const protocol = getProtocol(file.url);
|
|
@@ -46,20 +47,10 @@ async function bundleOpenApiSpecification(specification, targetUrl, reader, scmI
|
|
|
46
47
|
},
|
|
47
48
|
read: async (file) => {
|
|
48
49
|
const relativePath = path__namespace.relative(".", file.url);
|
|
49
|
-
const url =
|
|
50
|
-
|
|
51
|
-
url: relativePath
|
|
52
|
-
});
|
|
53
|
-
if (reader.readUrl) {
|
|
54
|
-
const data = await reader.readUrl(url);
|
|
55
|
-
return data.buffer();
|
|
56
|
-
}
|
|
57
|
-
throw new Error("UrlReader has no readUrl method defined");
|
|
50
|
+
const url = resolveUrl(relativePath, baseUrl);
|
|
51
|
+
return await read(url);
|
|
58
52
|
}
|
|
59
53
|
};
|
|
60
|
-
if (!specification) {
|
|
61
|
-
return void 0;
|
|
62
|
-
}
|
|
63
54
|
const options = {
|
|
64
55
|
resolve: {
|
|
65
56
|
file: fileUrlReaderResolver,
|
|
@@ -88,17 +79,25 @@ class OpenApiRefProcessor {
|
|
|
88
79
|
return "OpenApiRefProcessor";
|
|
89
80
|
}
|
|
90
81
|
async preProcessEntity(entity, location) {
|
|
91
|
-
var _a;
|
|
92
82
|
if (!entity || entity.kind !== "API" || entity.spec && entity.spec.type !== "openapi") {
|
|
93
83
|
return entity;
|
|
94
84
|
}
|
|
95
85
|
const scmIntegration = this.integrations.byUrl(location.target);
|
|
96
|
-
|
|
86
|
+
const definition = entity.spec.definition;
|
|
87
|
+
if (!scmIntegration || !definition) {
|
|
97
88
|
return entity;
|
|
98
89
|
}
|
|
90
|
+
const resolveUrl = (url, base) => {
|
|
91
|
+
return scmIntegration.resolveUrl({ url, base });
|
|
92
|
+
};
|
|
99
93
|
this.logger.debug(`Bundling OpenAPI specification from ${location.target}`);
|
|
100
94
|
try {
|
|
101
|
-
const bundledSpec = await bundleOpenApiSpecification(
|
|
95
|
+
const bundledSpec = await bundleOpenApiSpecification(
|
|
96
|
+
definition.toString(),
|
|
97
|
+
location.target,
|
|
98
|
+
this.reader.read,
|
|
99
|
+
resolveUrl
|
|
100
|
+
);
|
|
102
101
|
return {
|
|
103
102
|
...entity,
|
|
104
103
|
spec: { ...entity.spec, definition: bundledSpec }
|
|
@@ -110,5 +109,53 @@ class OpenApiRefProcessor {
|
|
|
110
109
|
}
|
|
111
110
|
}
|
|
112
111
|
|
|
112
|
+
async function openApiPlaceholderResolver(params) {
|
|
113
|
+
const { content, url } = await readTextLocation(params);
|
|
114
|
+
params.emit(pluginCatalogNode.processingResult.refresh(`url:${url}`));
|
|
115
|
+
try {
|
|
116
|
+
return await bundleOpenApiSpecification(
|
|
117
|
+
content,
|
|
118
|
+
url,
|
|
119
|
+
params.read,
|
|
120
|
+
params.resolveUrl
|
|
121
|
+
);
|
|
122
|
+
} catch (error) {
|
|
123
|
+
throw new Error(
|
|
124
|
+
`Placeholder $${params.key} unable to bundle OpenAPI specification at ${params.value}, ${error}`
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
async function readTextLocation(params) {
|
|
129
|
+
const newUrl = relativeUrl(params);
|
|
130
|
+
try {
|
|
131
|
+
const data = await params.read(newUrl);
|
|
132
|
+
return { content: data.toString("utf-8"), url: newUrl };
|
|
133
|
+
} catch (e) {
|
|
134
|
+
throw new Error(
|
|
135
|
+
`Placeholder $${params.key} could not read location ${params.value}, ${e}`
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
function relativeUrl({
|
|
140
|
+
key,
|
|
141
|
+
value,
|
|
142
|
+
baseUrl,
|
|
143
|
+
resolveUrl
|
|
144
|
+
}) {
|
|
145
|
+
if (typeof value !== "string") {
|
|
146
|
+
throw new Error(
|
|
147
|
+
`Placeholder $${key} expected a string value parameter, in the form of an absolute URL or a relative path`
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
try {
|
|
151
|
+
return resolveUrl(value, baseUrl);
|
|
152
|
+
} catch (e) {
|
|
153
|
+
throw new Error(
|
|
154
|
+
`Placeholder $${key} could not form a URL out of ${baseUrl} and ${value}, ${e}`
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
113
159
|
exports.OpenApiRefProcessor = OpenApiRefProcessor;
|
|
160
|
+
exports.openApiPlaceholderResolver = openApiPlaceholderResolver;
|
|
114
161
|
//# sourceMappingURL=index.cjs.js.map
|
package/dist/index.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs.js","sources":["../src/lib/bundle.ts","../src/OpenApiRefProcessor.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { UrlReader } from '@backstage/backend-common';\nimport { ScmIntegration } from '@backstage/integration';\nimport SwaggerParser from '@apidevtools/swagger-parser';\nimport { parse, stringify } from 'yaml';\nimport * as path from 'path';\n\nconst protocolPattern = /^(\\w{2,}):\\/\\//i;\nconst getProtocol = (refPath: string) => {\n const match = protocolPattern.exec(refPath);\n if (match) {\n return match[1].toLowerCase();\n }\n return undefined;\n};\n\nexport async function bundleOpenApiSpecification(\n specification: string | undefined,\n targetUrl: string,\n reader: UrlReader,\n scmIntegration: ScmIntegration,\n): Promise<string | undefined> {\n const fileUrlReaderResolver: SwaggerParser.ResolverOptions = {\n canRead: file => {\n const protocol = getProtocol(file.url);\n return protocol === undefined || protocol === 'file';\n },\n read: async file => {\n const relativePath = path.relative('.', file.url);\n const url = scmIntegration.resolveUrl({\n base: targetUrl,\n url: relativePath,\n });\n if (reader.readUrl) {\n const data = await reader.readUrl(url);\n return data.buffer();\n }\n throw new Error('UrlReader has no readUrl method defined');\n },\n };\n\n if (!specification) {\n return undefined;\n }\n\n const options: SwaggerParser.Options = {\n resolve: {\n file: fileUrlReaderResolver,\n http: true,\n },\n };\n const specObject = parse(specification);\n const bundledSpec = await SwaggerParser.bundle(specObject, options);\n return stringify(bundledSpec);\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { UrlReader } from '@backstage/backend-common';\nimport { Entity } from '@backstage/catalog-model';\nimport { Config } from '@backstage/config';\nimport { ScmIntegrations } from '@backstage/integration';\nimport {\n CatalogProcessor,\n LocationSpec,\n} from '@backstage/plugin-catalog-backend';\nimport { bundleOpenApiSpecification } from './lib';\nimport { Logger } from 'winston';\n\n/** @public */\nexport class OpenApiRefProcessor implements CatalogProcessor {\n private readonly integrations: ScmIntegrations;\n private readonly logger: Logger;\n private readonly reader: UrlReader;\n\n static fromConfig(\n config: Config,\n options: { logger: Logger; reader: UrlReader },\n ) {\n const integrations = ScmIntegrations.fromConfig(config);\n\n return new OpenApiRefProcessor({\n ...options,\n integrations,\n });\n }\n\n constructor(options: {\n integrations: ScmIntegrations;\n logger: Logger;\n reader: UrlReader;\n }) {\n this.integrations = options.integrations;\n this.logger = options.logger;\n this.reader = options.reader;\n }\n\n getProcessorName(): string {\n return 'OpenApiRefProcessor';\n }\n\n async preProcessEntity(\n entity: Entity,\n location: LocationSpec,\n ): Promise<Entity> {\n if (\n !entity ||\n entity.kind !== 'API' ||\n (entity.spec && entity.spec.type !== 'openapi')\n ) {\n return entity;\n }\n\n const scmIntegration = this.integrations.byUrl(location.target);\n if (!scmIntegration) {\n return entity;\n }\n\n this.logger.debug(`Bundling OpenAPI specification from ${location.target}`);\n try {\n const bundledSpec = await bundleOpenApiSpecification(\n entity.spec!.definition?.toString(),\n location.target,\n this.reader,\n scmIntegration,\n );\n\n return {\n ...entity,\n spec: { ...entity.spec, definition: bundledSpec },\n };\n } catch (error) {\n this.logger.error(`Unable to bundle OpenAPI specification`, error);\n return entity;\n }\n }\n}\n"],"names":["path","parse","SwaggerParser","stringify","ScmIntegrations"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,MAAM,eAAe,GAAG,iBAAiB,CAAC;AAC1C,MAAM,WAAW,GAAG,CAAC,OAAO,KAAK;AACjC,EAAE,MAAM,KAAK,GAAG,eAAe,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;AAC9C,EAAE,IAAI,KAAK,EAAE;AACb,IAAI,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC;AAClC,GAAG;AACH,EAAE,OAAO,KAAK,CAAC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AAI,IAAI;AACR,MAAM,MAAM,WAAW,GAAG,MAAM,0BAA0B,CAAC,CAAC,EAAE,GAAG,MAAM,CAAC,IAAI,CAAC,UAAU,KAAK,IAAI,GAAG,KAAK,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,EAAE,QAAQ,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;AACzK,MAAM,OAAO;AACb,QAAQ,GAAG,MAAM;AACjB,QAAQ,IAAI,EAAE,EAAE,GAAG,MAAM,CAAC,IAAI,EAAE,UAAU,EAAE,WAAW,EAAE;AACzD,OAAO,CAAC;AACR,KAAK,CAAC,OAAO,KAAK,EAAE;AACpB,MAAM,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,sCAAsC,CAAC,EAAE,KAAK,CAAC,CAAC;AACzE,MAAM,OAAO,MAAM,CAAC;AACpB,KAAK;AACL,GAAG;AACH;;;;"}
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":["../src/lib/bundle.ts","../src/OpenApiRefProcessor.ts","../src/openApiPlaceholderResolver.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport SwaggerParser from '@apidevtools/swagger-parser';\nimport { parse, stringify } from 'yaml';\nimport * as path from 'path';\n\nconst protocolPattern = /^(\\w{2,}):\\/\\//i;\nconst getProtocol = (refPath: string) => {\n const match = protocolPattern.exec(refPath);\n if (match) {\n return match[1].toLowerCase();\n }\n return undefined;\n};\n\nexport type BundlerRead = (url: string) => Promise<Buffer>;\n\nexport type BundlerResolveUrl = (url: string, base: string) => string;\n\nexport async function bundleOpenApiSpecification(\n specification: string,\n baseUrl: string,\n read: BundlerRead,\n resolveUrl: BundlerResolveUrl,\n): Promise<string> {\n const fileUrlReaderResolver: SwaggerParser.ResolverOptions = {\n canRead: file => {\n const protocol = getProtocol(file.url);\n return protocol === undefined || protocol === 'file';\n },\n read: async file => {\n const relativePath = path.relative('.', file.url);\n const url = resolveUrl(relativePath, baseUrl);\n return await read(url);\n },\n };\n\n const options: SwaggerParser.Options = {\n resolve: {\n file: fileUrlReaderResolver,\n http: true,\n },\n };\n const specObject = parse(specification);\n const bundledSpec = await SwaggerParser.bundle(specObject, options);\n return stringify(bundledSpec);\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { UrlReader } from '@backstage/backend-common';\nimport { Entity } from '@backstage/catalog-model';\nimport { Config } from '@backstage/config';\nimport { ScmIntegrations } from '@backstage/integration';\nimport {\n CatalogProcessor,\n LocationSpec,\n} from '@backstage/plugin-catalog-backend';\nimport { bundleOpenApiSpecification } from './lib';\nimport { Logger } from 'winston';\n\n/**\n * @public\n * @deprecated replaced by the openApiPlaceholderResolver\n */\nexport class OpenApiRefProcessor implements CatalogProcessor {\n private readonly integrations: ScmIntegrations;\n private readonly logger: Logger;\n private readonly reader: UrlReader;\n\n static fromConfig(\n config: Config,\n options: { logger: Logger; reader: UrlReader },\n ) {\n const integrations = ScmIntegrations.fromConfig(config);\n\n return new OpenApiRefProcessor({\n ...options,\n integrations,\n });\n }\n\n constructor(options: {\n integrations: ScmIntegrations;\n logger: Logger;\n reader: UrlReader;\n }) {\n this.integrations = options.integrations;\n this.logger = options.logger;\n this.reader = options.reader;\n }\n\n getProcessorName(): string {\n return 'OpenApiRefProcessor';\n }\n\n async preProcessEntity(\n entity: Entity,\n location: LocationSpec,\n ): Promise<Entity> {\n if (\n !entity ||\n entity.kind !== 'API' ||\n (entity.spec && entity.spec.type !== 'openapi')\n ) {\n return entity;\n }\n\n const scmIntegration = this.integrations.byUrl(location.target);\n const definition = entity.spec!.definition;\n\n if (!scmIntegration || !definition) {\n return entity;\n }\n\n const resolveUrl = (url: string, base: string): string => {\n return scmIntegration.resolveUrl({ url, base });\n };\n\n this.logger.debug(`Bundling OpenAPI specification from ${location.target}`);\n try {\n const bundledSpec = await bundleOpenApiSpecification(\n definition.toString(),\n location.target,\n this.reader.read,\n resolveUrl,\n );\n\n return {\n ...entity,\n spec: { ...entity.spec, definition: bundledSpec },\n };\n } catch (error) {\n this.logger.error(`Unable to bundle OpenAPI specification`, error);\n return entity;\n }\n }\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { PlaceholderResolverParams } from '@backstage/plugin-catalog-backend';\nimport { JsonValue } from '@backstage/types';\nimport { processingResult } from '@backstage/plugin-catalog-node';\nimport { bundleOpenApiSpecification } from './lib';\n\n/** @public */\nexport async function openApiPlaceholderResolver(\n params: PlaceholderResolverParams,\n): Promise<JsonValue> {\n const { content, url } = await readTextLocation(params);\n\n params.emit(processingResult.refresh(`url:${url}`));\n\n try {\n return await bundleOpenApiSpecification(\n content,\n url,\n params.read,\n params.resolveUrl,\n );\n } catch (error) {\n throw new Error(\n `Placeholder \\$${params.key} unable to bundle OpenAPI specification at ${params.value}, ${error}`,\n );\n }\n}\n\n/*\n * Helpers, copied from PlaceholderProcessor\n */\n\nasync function readTextLocation(\n params: PlaceholderResolverParams,\n): Promise<{ content: string; url: string }> {\n const newUrl = relativeUrl(params);\n\n try {\n const data = await params.read(newUrl);\n return { content: data.toString('utf-8'), url: newUrl };\n } catch (e) {\n throw new Error(\n `Placeholder \\$${params.key} could not read location ${params.value}, ${e}`,\n );\n }\n}\n\nfunction relativeUrl({\n key,\n value,\n baseUrl,\n resolveUrl,\n}: PlaceholderResolverParams): string {\n if (typeof value !== 'string') {\n throw new Error(\n `Placeholder \\$${key} expected a string value parameter, in the form of an absolute URL or a relative path`,\n );\n }\n\n try {\n return resolveUrl(value, baseUrl);\n } catch (e) {\n // The only remaining case that isn't support is a relative file path that should be\n // resolved using a relative file location. Accessing local file paths can lead to\n // path traversal attacks and access to any file on the host system. Implementing this\n // would require additional security measures.\n throw new Error(\n `Placeholder \\$${key} could not form a URL out of ${baseUrl} and ${value}, ${e}`,\n );\n }\n}\n"],"names":["path","parse","SwaggerParser","stringify","ScmIntegrations","processingResult"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,MAAM,eAAe,GAAG,iBAAiB,CAAC;AAC1C,MAAM,WAAW,GAAG,CAAC,OAAO,KAAK;AACjC,EAAE,MAAM,KAAK,GAAG,eAAe,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;AAC9C,EAAE,IAAI,KAAK,EAAE;AACb,IAAI,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC;AAClC,GAAG;AACH,EAAE,OAAO,KAAK,CAAC,CAAC;AAChB,CAAC,CAAC;AACK,eAAe,0BAA0B,CAAC,aAAa,EAAE,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE;AAC3F,EAAE,MAAM,qBAAqB,GAAG;AAChC,IAAI,OAAO,EAAE,CAAC,IAAI,KAAK;AACvB,MAAM,MAAM,QAAQ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ACnB,MAAM,CAAC,aAAa,EAAE,MAAM,CAAC,GAAG,CAAC,yBAAyB,EAAE,MAAM,CAAC,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;AAChF,KAAK,CAAC;AACN,GAAG;AACH,CAAC;AACD,SAAS,WAAW,CAAC;AACrB,EAAE,GAAG;AACL,EAAE,KAAK;AACP,EAAE,OAAO;AACT,EAAE,UAAU;AACZ,CAAC,EAAE;AACH,EAAE,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;AACjC,IAAI,MAAM,IAAI,KAAK;AACnB,MAAM,CAAC,aAAa,EAAE,GAAG,CAAC,qFAAqF,CAAC;AAChH,KAAK,CAAC;AACN,GAAG;AACH,EAAE,IAAI;AACN,IAAI,OAAO,UAAU,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;AACtC,GAAG,CAAC,OAAO,CAAC,EAAE;AACd,IAAI,MAAM,IAAI,KAAK;AACnB,MAAM,CAAC,aAAa,EAAE,GAAG,CAAC,6BAA6B,EAAE,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;AACrF,KAAK,CAAC;AACN,GAAG;AACH;;;;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -2,10 +2,14 @@ import { UrlReader } from '@backstage/backend-common';
|
|
|
2
2
|
import { Entity } from '@backstage/catalog-model';
|
|
3
3
|
import { Config } from '@backstage/config';
|
|
4
4
|
import { ScmIntegrations } from '@backstage/integration';
|
|
5
|
-
import { CatalogProcessor, LocationSpec } from '@backstage/plugin-catalog-backend';
|
|
5
|
+
import { CatalogProcessor, LocationSpec, PlaceholderResolverParams } from '@backstage/plugin-catalog-backend';
|
|
6
6
|
import { Logger } from 'winston';
|
|
7
|
+
import { JsonValue } from '@backstage/types';
|
|
7
8
|
|
|
8
|
-
/**
|
|
9
|
+
/**
|
|
10
|
+
* @public
|
|
11
|
+
* @deprecated replaced by the openApiPlaceholderResolver
|
|
12
|
+
*/
|
|
9
13
|
declare class OpenApiRefProcessor implements CatalogProcessor {
|
|
10
14
|
private readonly integrations;
|
|
11
15
|
private readonly logger;
|
|
@@ -23,4 +27,7 @@ declare class OpenApiRefProcessor implements CatalogProcessor {
|
|
|
23
27
|
preProcessEntity(entity: Entity, location: LocationSpec): Promise<Entity>;
|
|
24
28
|
}
|
|
25
29
|
|
|
26
|
-
|
|
30
|
+
/** @public */
|
|
31
|
+
declare function openApiPlaceholderResolver(params: PlaceholderResolverParams): Promise<JsonValue>;
|
|
32
|
+
|
|
33
|
+
export { OpenApiRefProcessor, openApiPlaceholderResolver };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@backstage/plugin-catalog-backend-module-openapi",
|
|
3
3
|
"description": "A Backstage catalog backend module that helps with OpenAPI specifications",
|
|
4
|
-
"version": "0.1.0",
|
|
4
|
+
"version": "0.1.2-next.0",
|
|
5
5
|
"main": "dist/index.cjs.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"license": "Apache-2.0",
|
|
@@ -34,21 +34,23 @@
|
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"@apidevtools/swagger-parser": "^10.1.0",
|
|
37
|
-
"@backstage/backend-common": "^0.
|
|
37
|
+
"@backstage/backend-common": "^0.15.1-next.0",
|
|
38
38
|
"@backstage/catalog-model": "^1.1.0",
|
|
39
39
|
"@backstage/config": "^1.0.1",
|
|
40
|
-
"@backstage/integration": "^1.
|
|
41
|
-
"@backstage/plugin-catalog-backend": "^1.3.0",
|
|
40
|
+
"@backstage/integration": "^1.3.1-next.0",
|
|
41
|
+
"@backstage/plugin-catalog-backend": "^1.3.2-next.0",
|
|
42
|
+
"@backstage/plugin-catalog-node": "^1.0.2-next.0",
|
|
43
|
+
"@backstage/types": "^1.0.0",
|
|
42
44
|
"winston": "^3.2.1",
|
|
43
45
|
"yaml": "^2.1.1"
|
|
44
46
|
},
|
|
45
47
|
"devDependencies": {
|
|
46
|
-
"@backstage/backend-test-utils": "^0.1.
|
|
47
|
-
"@backstage/cli": "^0.18.0",
|
|
48
|
+
"@backstage/backend-test-utils": "^0.1.28-next.0",
|
|
49
|
+
"@backstage/cli": "^0.18.2-next.0",
|
|
48
50
|
"openapi-types": "^12.0.0"
|
|
49
51
|
},
|
|
50
52
|
"files": [
|
|
51
53
|
"dist"
|
|
52
54
|
],
|
|
53
|
-
"gitHead": "
|
|
55
|
+
"gitHead": "c6c0b1978a7ab4d29d813996c56beb7e6b48a268"
|
|
54
56
|
}
|