@openapi-typescript-infra/service 4.0.0 → 4.1.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/build/types.d.ts CHANGED
@@ -21,6 +21,10 @@ export interface ServiceLocals<Config extends ConfigurationSchema = Configuratio
21
21
  config: Config;
22
22
  meter: Meter;
23
23
  internalApp: Application<InternalLocals<this>>;
24
+ /**
25
+ * This is the parsed OpenAPI spec we are hosting (if openapi is enabled)
26
+ */
27
+ openApiSpecification?: ReturnType<typeof JSON.parse>;
24
28
  }
25
29
  export interface RequestLocals {
26
30
  rawBody?: Buffer | true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openapi-typescript-infra/service",
3
- "version": "4.0.0",
3
+ "version": "4.1.0",
4
4
  "description": "An opinionated framework for building configuration driven services - web, api, or ob. Uses OpenAPI, pino logging, express, confit, Typescript and vitest.",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
@@ -360,6 +360,12 @@ export async function listen<SLocals extends AnyServiceLocals = ServiceLocals<Co
360
360
  } else {
361
361
  locals.logger.info('No metrics will be exported');
362
362
  }
363
+ if (app.locals.openApiSpecification) {
364
+ locals.internalApp.get('/api-docs/openapi.json', (req, res) => {
365
+ res.json(app.locals.openApiSpecification);
366
+ });
367
+ locals.logger.info('OpenAPI specification available at /api-docs/openapi.json');
368
+ }
363
369
  accept();
364
370
  })
365
371
  .catch((error) => {
package/src/openapi.ts CHANGED
@@ -2,6 +2,7 @@ import path from 'path';
2
2
 
3
3
  import _ from 'lodash';
4
4
  import * as OpenApiValidator from 'express-openapi-validator';
5
+ import { OpenAPIFramework } from 'express-openapi-validator/dist/framework/index';
5
6
  import type { Handler } from 'express';
6
7
 
7
8
  import type { AnyServiceLocals, ServiceExpress, ServiceLocals } from './types';
@@ -64,6 +65,12 @@ export async function openApi<
64
65
  {} as Record<string, Record<string, unknown>>,
65
66
  );
66
67
 
68
+ app.locals.openApiSpecification = await new OpenAPIFramework({ apiDoc: apiSpec })
69
+ .initialize({ visitApi() {} })
70
+ .catch((error) => {
71
+ app.locals.logger.error(error, 'Failed to parse and load OpenAPI spec');
72
+ });
73
+
67
74
  const defaultOptions: OAPIOpts = {
68
75
  apiSpec,
69
76
  ignoreUndocumented: true,
package/src/types.ts CHANGED
@@ -28,6 +28,10 @@ export interface ServiceLocals<Config extends ConfigurationSchema = Configuratio
28
28
  config: Config;
29
29
  meter: Meter;
30
30
  internalApp: Application<InternalLocals<this>>;
31
+ /**
32
+ * This is the parsed OpenAPI spec we are hosting (if openapi is enabled)
33
+ */
34
+ openApiSpecification?: ReturnType<typeof JSON.parse>;
31
35
  }
32
36
 
33
37
  export interface RequestLocals {