@openapi-typescript-infra/service 4.7.0 → 4.8.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/build/types.d.ts CHANGED
@@ -8,6 +8,7 @@ import type { Request, Response } from 'express';
8
8
  import type { Application } from 'express-serve-static-core';
9
9
  import type { middleware } from 'express-openapi-validator';
10
10
  import type { Meter } from '@opentelemetry/api';
11
+ import { ShortstopHandler } from '@sesamecare-oss/confit';
11
12
  import { ConfigurationSchema } from './config/schema';
12
13
  export interface InternalLocals<SLocals extends AnyServiceLocals = ServiceLocals<ConfigurationSchema>> extends Record<string, unknown> {
13
14
  server?: Server;
@@ -66,6 +67,7 @@ export interface DelayLoadServiceStartOptions extends Omit<ServiceStartOptions,
66
67
  export interface ServiceOptions {
67
68
  configurationDirectories: string[];
68
69
  openApiOptions?: Parameters<typeof middleware>[0];
70
+ shortstopHandlers: Record<string, ShortstopHandler<string, unknown>>;
69
71
  }
70
72
  export interface ServiceLike<SLocals extends AnyServiceLocals = ServiceLocals<ConfigurationSchema>> {
71
73
  locals: SLocals;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openapi-typescript-infra/service",
3
- "version": "4.7.0",
3
+ "version": "4.8.1",
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": {
@@ -1,11 +1,16 @@
1
1
  import fs from 'fs';
2
2
  import path from 'path';
3
3
 
4
- import { BaseConfitSchema, Confit, Factory, confit } from '@sesamecare-oss/confit';
4
+ import {
5
+ BaseConfitSchema,
6
+ Confit,
7
+ Factory,
8
+ ShortstopHandler,
9
+ confit,
10
+ } from '@sesamecare-oss/confit';
5
11
 
6
12
  import { findPort } from '../development/port-finder';
7
13
 
8
- import { shortstops } from './shortstops';
9
14
  import type { ConfigurationSchema } from './schema';
10
15
 
11
16
  // Order matters here.
@@ -51,27 +56,23 @@ async function addDefaultConfiguration<Config extends ConfigurationSchema = Conf
51
56
  }
52
57
 
53
58
  export interface ServiceConfigurationSpec {
54
- // Used for "sourcerequire" and other source-relative paths and for the package name
55
- sourceDirectory: string;
56
59
  // The LAST configuration is the most "specific" - if a configuration value
57
60
  // exists in all directories, the last one wins
58
61
  configurationDirectories: string[];
59
- name: string;
62
+ shortstopHandlers: Record<string, ShortstopHandler<string, unknown>>;
60
63
  }
61
64
 
62
65
  export async function loadConfiguration<Config extends ConfigurationSchema>({
63
- name,
64
66
  configurationDirectories: dirs,
65
- sourceDirectory,
67
+ shortstopHandlers,
66
68
  }: ServiceConfigurationSpec): Promise<Config> {
67
- const defaultProtocols = shortstops({ name }, sourceDirectory);
68
69
  const specificConfig = dirs[dirs.length - 1];
69
70
 
70
71
  // This confit version just gets us environment info
71
72
  const envConfit = await confit({ basedir: specificConfig }).create();
72
73
  const configFactory = confit<Config>({
73
74
  basedir: specificConfig,
74
- protocols: defaultProtocols,
75
+ protocols: shortstopHandlers,
75
76
  });
76
77
 
77
78
  /**
@@ -122,7 +122,10 @@ export function shortstops(service: { name: string }, sourcedir: string) {
122
122
  },
123
123
  base64: base64Handler(),
124
124
  regex(v: string) {
125
- const [, pattern, flags] = v.match(/^\/(.*)\/([a-z]*)/) || [];
125
+ const [, pattern, flags] = v.match(/^\/(.*)\/([a-z]*)$/) || [];
126
+ if (pattern === undefined) {
127
+ throw new Error(`Invalid regular expression in configuration ${v}`);
128
+ }
126
129
  return new RegExp(pattern, flags);
127
130
  },
128
131
 
@@ -28,6 +28,7 @@ import type {
28
28
  ServiceStartOptions,
29
29
  } from '../types';
30
30
  import { ConfigurationSchema } from '../config/schema';
31
+ import { shortstops } from '../config/shortstops';
31
32
  import { getNodeEnv, isDev } from '../env';
32
33
  import { getGlobalPrometheusExporter } from '../telemetry/index';
33
34
 
@@ -76,15 +77,16 @@ export async function startApp<
76
77
  const serviceImpl = service();
77
78
  assert(serviceImpl?.start, 'Service function did not return a conforming object');
78
79
 
80
+ const sourceDirectory = path.join(rootDirectory, codepath);
79
81
  const baseOptions: ServiceOptions = {
80
82
  configurationDirectories: [path.resolve(rootDirectory, './config')],
83
+ shortstopHandlers: shortstops({ name }, sourceDirectory),
81
84
  };
82
85
  const options = serviceImpl.configure?.(startOptions, baseOptions) || baseOptions;
83
86
 
84
87
  const config = await loadConfiguration({
85
- name,
86
88
  configurationDirectories: options.configurationDirectories,
87
- sourceDirectory: path.join(rootDirectory, codepath),
89
+ shortstopHandlers: options.shortstopHandlers,
88
90
  });
89
91
 
90
92
  const logging = config.logging;
package/src/types.ts CHANGED
@@ -6,6 +6,7 @@ import type { Request, Response } from 'express';
6
6
  import type { Application } from 'express-serve-static-core';
7
7
  import type { middleware } from 'express-openapi-validator';
8
8
  import type { Meter } from '@opentelemetry/api';
9
+ import { ShortstopHandler } from '@sesamecare-oss/confit';
9
10
 
10
11
  import { ConfigurationSchema } from './config/schema';
11
12
 
@@ -143,6 +144,8 @@ export interface ServiceOptions {
143
144
 
144
145
  // Add or control OpenAPI options such as security handlers
145
146
  openApiOptions?: Parameters<typeof middleware>[0];
147
+
148
+ shortstopHandlers: Record<string, ShortstopHandler<string, unknown>>;
146
149
  }
147
150
 
148
151
  export interface ServiceLike<