@openapi-typescript-infra/service 4.4.0 → 4.5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openapi-typescript-infra/service",
3
- "version": "4.4.0",
3
+ "version": "4.5.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": {
@@ -121,3 +121,4 @@ export function insertConfigurationBefore(
121
121
  }
122
122
 
123
123
  export * from './schema';
124
+ export * from './validation';
@@ -66,7 +66,7 @@ export interface ConfigurationSchema extends BaseConfitSchema {
66
66
  // Note that generally it's better to offload tls termination,
67
67
  // but this is useful for dev.
68
68
  key?: string | Uint8Array;
69
- certificate?: string;
69
+ certificate?: string | Uint8Array;
70
70
  // If you have an alternate host name (other than localhost) that
71
71
  // should be used when referring to this service, set it here.
72
72
  hostname?: string;
@@ -0,0 +1,22 @@
1
+ import { ConfigurationSchema } from './schema';
2
+
3
+ export interface ConfigValidationError {
4
+ path: string;
5
+ message: string;
6
+ }
7
+
8
+ export type ConfigurationValidator<Config extends ConfigurationSchema> = (config: Config) => {
9
+ success: boolean;
10
+ errors: ConfigValidationError[];
11
+ };
12
+
13
+ export function validateConfiguration<Config extends ConfigurationSchema>(
14
+ config: Config,
15
+ validator: ConfigurationValidator<Config>,
16
+ ) {
17
+ const result = validator(config);
18
+ if (!result.success) {
19
+ throw new Error(`Configuration validation failed:
20
+ ${result.errors.map((e) => ` - ${e.path}: ${e.message}`).join('\n')}`);
21
+ }
22
+ }
@@ -271,7 +271,7 @@ function httpServer<
271
271
  return https.createServer(
272
272
  {
273
273
  key: config.key ? Buffer.from(config.key) : undefined,
274
- cert: config.certificate,
274
+ cert: config.certificate ? Buffer.from(config.certificate) : undefined,
275
275
  },
276
276
  app,
277
277
  );