@backstage/backend-openapi-utils 0.6.7-next.0 → 0.6.7-next.1

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 CHANGED
@@ -1,5 +1,12 @@
1
1
  # @backstage/backend-openapi-utils
2
2
 
3
+ ## 0.6.7-next.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies
8
+ - @backstage/backend-plugin-api@1.8.0-next.1
9
+
3
10
  ## 0.6.7-next.0
4
11
 
5
12
  ### Patch Changes
package/README.md CHANGED
@@ -4,6 +4,8 @@
4
4
 
5
5
  This package is meant to provide a typed Express router for an OpenAPI spec. Based on the [`oatx`](https://github.com/varanauskas/oatx) library and adapted to override Express values.
6
6
 
7
+ Only supports OpenAPI 3.1 specifications.
8
+
7
9
  ## Getting Started
8
10
 
9
11
  ### Configuration
package/dist/index.d.ts CHANGED
@@ -618,6 +618,7 @@ interface TypedRouter<GeneratedEndpointMap extends EndpointMap> extends Router {
618
618
  declare function getOpenApiSpecRoute(baseUrl: string): string;
619
619
  /**
620
620
  * Create a new OpenAPI router with some default middleware.
621
+ * Only supports OpenAPI 3.1 specifications.
621
622
  * @param spec - Your OpenAPI spec imported as a JSON object.
622
623
  * @param validatorOptions - `openapi-express-validator` options to override the defaults.
623
624
  * @returns A new express router with validation middleware.
@@ -629,6 +630,7 @@ declare function createValidatedOpenApiRouter<T extends RequiredDoc>(spec: T, op
629
630
  }): ApiRouter<typeof spec>;
630
631
  /**
631
632
  * Create a new OpenAPI router with some default middleware.
633
+ * Only supports OpenAPI 3.1 specifications.
632
634
  * @param spec - Your OpenAPI spec imported as a JSON object.
633
635
  * @param validatorOptions - `openapi-express-validator` options to override the defaults.
634
636
  * @returns A new express router with validation middleware.
@@ -1 +1 @@
1
- {"version":3,"file":"stub.cjs.js","sources":["../src/stub.ts"],"sourcesContent":["/*\n * Copyright 2023 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 */\n\nimport PromiseRouter from 'express-promise-router';\nimport { ApiRouter, TypedRouter } from './router';\nimport { EndpointMap, RequiredDoc } from './types';\nimport {\n ErrorRequestHandler,\n RequestHandler,\n NextFunction,\n Request,\n Response,\n json,\n Router,\n} from 'express';\nimport { InputError } from '@backstage/errors';\nimport { middleware as OpenApiValidator } from 'express-openapi-validator';\nimport { OPENAPI_SPEC_ROUTE } from './constants';\nimport { isErrorResult, merge } from 'openapi-merge';\n\nfunction validatorErrorTransformer(): ErrorRequestHandler {\n return (error: Error, _: Request, _2: Response, next: NextFunction) => {\n next(new InputError(error.message));\n };\n}\n\nexport function getDefaultRouterMiddleware() {\n return [json()];\n}\n\n/**\n * Given a base url for a plugin, find the given OpenAPI spec for that plugin.\n * @param baseUrl - Plugin base url.\n * @returns OpenAPI spec route for the base url.\n * @public\n */\nexport function getOpenApiSpecRoute(baseUrl: string) {\n return `${baseUrl}${OPENAPI_SPEC_ROUTE}`;\n}\n\n/**\n * Create a router with validation middleware. This is used by typing methods to create an\n * \"OpenAPI router\" with all of the expected validation + metadata.\n * @param spec - Your OpenAPI spec imported as a JSON object.\n * @param validatorOptions - `openapi-express-validator` options to override the defaults.\n * @returns A new express router with validation middleware.\n */\nfunction createRouterWithValidation(\n spec: RequiredDoc,\n options?: {\n validatorOptions?: Partial<Parameters<typeof OpenApiValidator>['0']>;\n middleware?: RequestHandler[];\n },\n): Router {\n const router = PromiseRouter();\n router.use(options?.middleware || getDefaultRouterMiddleware());\n\n // TODO: Handle errors by converting from OpenApiValidator errors to known @backstage/errors errors.\n router.use(\n OpenApiValidator({\n validateRequests: {\n coerceTypes: false,\n allowUnknownQueryParameters: false,\n },\n useRequestUrl: true,\n ignoreUndocumented: true,\n validateResponses: false,\n ...options?.validatorOptions,\n apiSpec: spec as any,\n }),\n );\n\n // Any errors from the middleware get through here.\n router.use(validatorErrorTransformer());\n\n router.get(OPENAPI_SPEC_ROUTE, async (req, res) => {\n const mergeOutput = merge([\n {\n oas: spec as any,\n pathModification: {\n /**\n * Get the route that this OpenAPI spec is hosted on. The other\n * approach of using the discovery API increases the router constructor\n * significantly and since we're just looking for path and not full URL,\n * this works.\n *\n * If we wanted to add a list of servers, there may be a case for adding\n * discovery API to get an exhaustive list of upstream servers, but that's\n * also not currently supported.\n */\n prepend: req.originalUrl.replace(OPENAPI_SPEC_ROUTE, ''),\n },\n },\n ]);\n if (isErrorResult(mergeOutput)) {\n throw new InputError('Invalid spec defined');\n }\n res.json(mergeOutput.output);\n });\n return router;\n}\n\n/**\n * Create a new OpenAPI router with some default middleware.\n * @param spec - Your OpenAPI spec imported as a JSON object.\n * @param validatorOptions - `openapi-express-validator` options to override the defaults.\n * @returns A new express router with validation middleware.\n * @public\n */\nexport function createValidatedOpenApiRouter<T extends RequiredDoc>(\n spec: T,\n options?: {\n validatorOptions?: Partial<Parameters<typeof OpenApiValidator>['0']>;\n middleware?: RequestHandler[];\n },\n) {\n return createRouterWithValidation(spec, options) as ApiRouter<typeof spec>;\n}\n\n/**\n * Create a new OpenAPI router with some default middleware.\n * @param spec - Your OpenAPI spec imported as a JSON object.\n * @param validatorOptions - `openapi-express-validator` options to override the defaults.\n * @returns A new express router with validation middleware.\n * @public\n */\nexport function createValidatedOpenApiRouterFromGeneratedEndpointMap<\n T extends EndpointMap,\n>(\n spec: RequiredDoc,\n options?: {\n validatorOptions?: Partial<Parameters<typeof OpenApiValidator>['0']>;\n middleware?: RequestHandler[];\n },\n) {\n return createRouterWithValidation(spec, options) as TypedRouter<T>;\n}\n"],"names":["InputError","json","OPENAPI_SPEC_ROUTE","PromiseRouter","OpenApiValidator","merge","isErrorResult"],"mapp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AAA,CAAQH,8BAAoB,EAAE;AAAA;AACzD;AACF,KACD,CAAA;AACD,IAAA,IAAII,0BAAA,CAAc,WAAW,CAAA,EAAG;AAC9B,MAAA,MAAM,IAAIN,kBAAW,sBAAsB,CAAA;AAAA,IAC7C;AACA,IAAA,GAAA,CAAI,IAAA,CAAK,YAAY,MAAM,CAAA;AAAA,EAC7B,CAAC,CAAA;AACD,EAAA,OAAO,MAAA;AACT;AASO,SAAS,4BAAA,CACd,MACA,OAAA,EAIA;AACA,EAAA,OAAO,0BAAA,CAA2B,MAAM,OAAO,CAAA;AACjD;AASO,SAAS,oDAAA,CAGd,MACA,OAAA,EAIA;AACA,EAAA,OAAO,0BAAA,CAA2B,MAAM,OAAO,CAAA;AACjD;;;;;;;"}
1
+ {"version":3,"file":"stub.cjs.js","sources":["../src/stub.ts"],"sourcesContent":["/*\n * Copyright 2023 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 */\n\nimport PromiseRouter from 'express-promise-router';\nimport { ApiRouter, TypedRouter } from './router';\nimport { EndpointMap, RequiredDoc } from './types';\nimport {\n ErrorRequestHandler,\n RequestHandler,\n NextFunction,\n Request,\n Response,\n json,\n Router,\n} from 'express';\nimport { InputError } from '@backstage/errors';\nimport { middleware as OpenApiValidator } from 'express-openapi-validator';\nimport { OPENAPI_SPEC_ROUTE } from './constants';\nimport { isErrorResult, merge } from 'openapi-merge';\n\nfunction validatorErrorTransformer(): ErrorRequestHandler {\n return (error: Error, _: Request, _2: Response, next: NextFunction) => {\n next(new InputError(error.message));\n };\n}\n\nexport function getDefaultRouterMiddleware() {\n return [json()];\n}\n\n/**\n * Given a base url for a plugin, find the given OpenAPI spec for that plugin.\n * @param baseUrl - Plugin base url.\n * @returns OpenAPI spec route for the base url.\n * @public\n */\nexport function getOpenApiSpecRoute(baseUrl: string) {\n return `${baseUrl}${OPENAPI_SPEC_ROUTE}`;\n}\n\n/**\n * Create a router with validation middleware. This is used by typing methods to create an\n * \"OpenAPI router\" with all of the expected validation + metadata.\n * Only supports OpenAPI 3.1 specifications.\n * @param spec - Your OpenAPI spec imported as a JSON object.\n * @param validatorOptions - `openapi-express-validator` options to override the defaults.\n * @returns A new express router with validation middleware.\n */\nfunction createRouterWithValidation(\n spec: RequiredDoc,\n options?: {\n validatorOptions?: Partial<Parameters<typeof OpenApiValidator>['0']>;\n middleware?: RequestHandler[];\n },\n): Router {\n const router = PromiseRouter();\n router.use(options?.middleware || getDefaultRouterMiddleware());\n\n // TODO: Handle errors by converting from OpenApiValidator errors to known @backstage/errors errors.\n router.use(\n OpenApiValidator({\n validateRequests: {\n coerceTypes: false,\n allowUnknownQueryParameters: false,\n },\n useRequestUrl: true,\n ignoreUndocumented: true,\n validateResponses: false,\n ...options?.validatorOptions,\n apiSpec: spec as any,\n }),\n );\n\n // Any errors from the middleware get through here.\n router.use(validatorErrorTransformer());\n\n router.get(OPENAPI_SPEC_ROUTE, async (req, res) => {\n const mergeOutput = merge([\n {\n oas: spec as any,\n pathModification: {\n /**\n * Get the route that this OpenAPI spec is hosted on. The other\n * approach of using the discovery API increases the router constructor\n * significantly and since we're just looking for path and not full URL,\n * this works.\n *\n * If we wanted to add a list of servers, there may be a case for adding\n * discovery API to get an exhaustive list of upstream servers, but that's\n * also not currently supported.\n */\n prepend: req.originalUrl.replace(OPENAPI_SPEC_ROUTE, ''),\n },\n },\n ]);\n if (isErrorResult(mergeOutput)) {\n throw new InputError('Invalid spec defined');\n }\n res.json(mergeOutput.output);\n });\n return router;\n}\n\n/**\n * Create a new OpenAPI router with some default middleware.\n * Only supports OpenAPI 3.1 specifications.\n * @param spec - Your OpenAPI spec imported as a JSON object.\n * @param validatorOptions - `openapi-express-validator` options to override the defaults.\n * @returns A new express router with validation middleware.\n * @public\n */\nexport function createValidatedOpenApiRouter<T extends RequiredDoc>(\n spec: T,\n options?: {\n validatorOptions?: Partial<Parameters<typeof OpenApiValidator>['0']>;\n middleware?: RequestHandler[];\n },\n) {\n return createRouterWithValidation(spec, options) as ApiRouter<typeof spec>;\n}\n\n/**\n * Create a new OpenAPI router with some default middleware.\n * Only supports OpenAPI 3.1 specifications.\n * @param spec - Your OpenAPI spec imported as a JSON object.\n * @param validatorOptions - `openapi-express-validator` options to override the defaults.\n * @returns A new express router with validation middleware.\n * @public\n */\nexport function createValidatedOpenApiRouterFromGeneratedEndpointMap<\n T extends EndpointMap,\n>(\n spec: RequiredDoc,\n options?: {\n validatorOptions?: Partial<Parameters<typeof OpenApiValidator>['0']>;\n middleware?: RequestHandler[];\n },\n) {\n return createRouterWithValidation(spec, options) as TypedRouter<T>;\n}\n"],"names":["InputError","json","OPENAPI_SPEC_ROUTE","PromiseRouter","OpenApiValidator","merge","isErrorResult"],"mappings":";;;;;;;;;;;;;AAiCA,SAAS,yBAAA,GAAiD;AACxD,EAAA,OAAO,CAAC,KAAA,EAAc,CAAA,EAAY,EAAA,EAAc,IAAA,KAAuB;AACrE,IAAA,IAAA,CAAK,IAAIA,iBAAA,CAAW,KAAA,CAAM,OAAO,CAAC,CAAA;AAAA,EACpC,CAAA;AACF;AAEO,SAAS,0BAAA,GAA6B;AAC3C,EAAA,OAAO,CAACC,cAAM,CAAA;AAChB;AAQO,SAAS,oBAAoB,OAAA,EAAiB;AACnD,EAAA,OAAO,CAAA,EAAG,OAAO,CAAA,EAAGC,4BAAkB,CAAA,CAAA;AACxC;AAUA,SAAS,0BAAA,CACP,MACA,OAAA,EAIQ;AACR,EAAA,MAAM,SAASC,8BAAA,EAAc;AAC7B,EAAA,MAAA,CAAO,GAAA,CAAI,OAAA,EAAS,UAAA,IAAc,0BAAA,EAA4B,CAAA;AAG9D,EA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",
3
+ "version": "0.6.7-next.1",
4
4
  "description": "OpenAPI typescript support.",
5
5
  "backstage": {
6
6
  "role": "node-library"
@@ -54,7 +54,7 @@
54
54
  },
55
55
  "dependencies": {
56
56
  "@apidevtools/swagger-parser": "^10.1.0",
57
- "@backstage/backend-plugin-api": "1.7.1-next.0",
57
+ "@backstage/backend-plugin-api": "1.8.0-next.1",
58
58
  "@backstage/errors": "1.2.7",
59
59
  "@backstage/types": "1.2.2",
60
60
  "@types/express": "^4.17.6",
@@ -71,7 +71,7 @@
71
71
  "openapi3-ts": "^3.1.2"
72
72
  },
73
73
  "devDependencies": {
74
- "@backstage/cli": "0.35.5-next.0",
74
+ "@backstage/cli": "0.36.0-next.2",
75
75
  "@backstage/test-utils": "1.7.16-next.0",
76
76
  "msw": "^1.0.0",
77
77
  "supertest": "^7.0.0"