@ebarahona/loopback-openapi-v3 1.0.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/src/index.ts ADDED
@@ -0,0 +1,10 @@
1
+ export {OpenApiVersionComponent} from './openapi-version.component';
2
+ export {OpenApiVersionEnhancer} from './openapi-version.enhancer';
3
+ export {OpenApiVersionBindings} from './keys';
4
+ export {OpenApiVersion, OpenApiVersionConfig} from './types';
5
+ export {
6
+ transformOpenApiSpec,
7
+ parseVersion,
8
+ TransformResult,
9
+ TransformWarning,
10
+ } from './transform';
package/src/keys.ts ADDED
@@ -0,0 +1,9 @@
1
+ import {BindingKey} from '@loopback/core';
2
+ import {OpenApiVersionConfig} from './types';
3
+
4
+ export namespace OpenApiVersionBindings {
5
+ export const CONFIG = BindingKey.create<OpenApiVersionConfig>(
6
+ 'openapi-version.config',
7
+ );
8
+ export const COMPONENT = 'openapi-version.component';
9
+ }
@@ -0,0 +1,27 @@
1
+ import {
2
+ Binding,
3
+ Component,
4
+ createBindingFromClass,
5
+ } from '@loopback/core';
6
+ import {asSpecEnhancer} from '@loopback/openapi-v3';
7
+ import {OpenApiVersionBindings} from './keys';
8
+ import {OpenApiVersionEnhancer} from './openapi-version.enhancer';
9
+ import {DEFAULT_CONFIG} from './types';
10
+
11
+ /**
12
+ * LoopBack 4 component that transforms OpenAPI spec output to 3.1 or 3.2.
13
+ *
14
+ * Usage:
15
+ * ```ts
16
+ * import {OpenApiVersionComponent, OpenApiVersionBindings} from '@ebarahona/loopback-openapi-v3';
17
+ *
18
+ * app.component(OpenApiVersionComponent);
19
+ * app.bind(OpenApiVersionBindings.CONFIG).to({version: '3.1.0'});
20
+ * ```
21
+ */
22
+ export class OpenApiVersionComponent implements Component {
23
+ bindings: Binding[] = [
24
+ Binding.bind(OpenApiVersionBindings.CONFIG).to(DEFAULT_CONFIG),
25
+ createBindingFromClass(OpenApiVersionEnhancer).apply(asSpecEnhancer),
26
+ ];
27
+ }
@@ -0,0 +1,61 @@
1
+ import {config, injectable} from '@loopback/core';
2
+ import {
3
+ OASEnhancer,
4
+ OpenApiSpec,
5
+ } from '@loopback/openapi-v3';
6
+ import debugFactory from 'debug';
7
+ import {OpenApiVersionBindings} from './keys';
8
+ import {transformOpenApiSpec} from './transform';
9
+ import {DEFAULT_CONFIG, OpenApiVersionConfig} from './types';
10
+
11
+ const debug = debugFactory('loopback:openapi-version');
12
+
13
+ /**
14
+ * OAS Enhancer that transforms the generated OpenAPI spec between
15
+ * versions 3.0, 3.1, and 3.2.
16
+ *
17
+ * Handles both upgrades (3.0 -> 3.1/3.2) and compatibility downgrades
18
+ * (3.2 -> 3.0/3.1). Downgrades are lossy: features that exist in
19
+ * higher versions but have no equivalent in lower versions are stripped.
20
+ * Warnings are logged for each stripped feature.
21
+ *
22
+ * Runs when LoopBack assembles the OpenAPI spec. The transformed
23
+ * spec is returned to LoopBack for caching/serving according to the
24
+ * application's OpenAPI configuration.
25
+ *
26
+ * Spec references:
27
+ * - 3.0: https://spec.openapis.org/oas/v3.0.3.html
28
+ * - 3.1: https://spec.openapis.org/oas/v3.1.0.html
29
+ * - 3.2: https://spec.openapis.org/oas/v3.2.0.html
30
+ */
31
+ @injectable()
32
+ export class OpenApiVersionEnhancer implements OASEnhancer {
33
+ name = 'openapi-version';
34
+
35
+ constructor(
36
+ @config({fromBinding: OpenApiVersionBindings.CONFIG, optional: true})
37
+ private options: OpenApiVersionConfig = DEFAULT_CONFIG,
38
+ ) {}
39
+
40
+ modifySpec(spec: OpenApiSpec): OpenApiSpec {
41
+ const opts = {...DEFAULT_CONFIG, ...this.options};
42
+ debug(
43
+ 'configured target OpenAPI version: %s (transformNullable=%s)',
44
+ opts.version,
45
+ opts.transformNullable !== false,
46
+ );
47
+ const result = transformOpenApiSpec(
48
+ spec as Record<string, unknown>,
49
+ opts,
50
+ );
51
+
52
+ if (result.warnings.length > 0) {
53
+ debug('emitted %d OpenAPI compatibility warnings', result.warnings.length);
54
+ for (const w of result.warnings) {
55
+ debug('warning [%s]: %s', w.field, w.message);
56
+ }
57
+ }
58
+
59
+ return result.spec as OpenApiSpec;
60
+ }
61
+ }