@forklaunch/express 0.3.4 → 0.3.6

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/lib/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
- import { AnySchemaValidator, LiteralSchema, IdiomaticSchema, SchemaResolve } from '@forklaunch/validator';
2
1
  import * as _forklaunch_core_http from '@forklaunch/core/http';
3
- import { ForklaunchExpressLikeApplication, ForklaunchExpressLikeRouter, TypedMiddlewareDefinition, ParamsObject, ResponsesObject, Body, QueryObject, HeadersObject, ContractDetails, ExpressLikeSchemaHandler } from '@forklaunch/core/http';
2
+ import { ForklaunchExpressLikeApplication, OpenTelemetryCollector, MetricsDefinition, DocsConfiguration, ForklaunchExpressLikeRouter, TypedMiddlewareDefinition, ParamsObject, ResponsesObject, Body, QueryObject, HeadersObject, ContractDetails, ExpressLikeSchemaHandler } from '@forklaunch/core/http';
3
+ import { AnySchemaValidator, LiteralSchema, IdiomaticSchema, SchemaResolve } from '@forklaunch/validator';
4
4
  import { Express, RequestHandler, Request, Response, NextFunction, Router as Router$1 } from 'express';
5
5
  export { NextFunction, Request, Response } from 'express';
6
6
  import { Server } from 'http';
@@ -14,12 +14,13 @@ import * as express_serve_static_core from 'express-serve-static-core';
14
14
  * @template SV - A type that extends AnySchemaValidator.
15
15
  */
16
16
  declare class Application<SV extends AnySchemaValidator> extends ForklaunchExpressLikeApplication<SV, Express, RequestHandler, Request, Response, NextFunction> {
17
+ private readonly docsConfiguration?;
17
18
  /**
18
19
  * Creates an instance of Application.
19
20
  *
20
21
  * @param {SV} schemaValidator - The schema validator.
21
22
  */
22
- constructor(schemaValidator: SV);
23
+ constructor(schemaValidator: SV, openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>, docsConfiguration?: DocsConfiguration | undefined);
23
24
  /**
24
25
  * Starts the server and sets up Swagger documentation.
25
26
  *
@@ -48,7 +49,7 @@ declare class Router<SV extends AnySchemaValidator, BasePath extends `/${string}
48
49
  * @param {string} basePath - The base path for the router.
49
50
  * @param {SV} schemaValidator - The schema validator.
50
51
  */
51
- constructor(basePath: BasePath, schemaValidator: SV);
52
+ constructor(basePath: BasePath, schemaValidator: SV, openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>);
52
53
  route(path: string): this;
53
54
  param<ParamName extends string, Types extends {
54
55
  req?: LiteralSchema | SV['_SchemaCatchall'];
@@ -240,7 +241,7 @@ declare const unsubscribe: <SV extends AnySchemaValidator, Path extends `/${stri
240
241
  * @param {SV} schemaValidator - The schema validator.
241
242
  * @returns {Application<SV>} - The new application instance.
242
243
  */
243
- declare function forklaunchExpress<SV extends AnySchemaValidator>(schemaValidator: SV): Application<SV>;
244
+ declare function forklaunchExpress<SV extends AnySchemaValidator>(schemaValidator: SV, openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>, docsConfiguration?: DocsConfiguration): Application<SV>;
244
245
  /**
245
246
  * Creates a new instance of Router with the given base path and schema validator.
246
247
  *
@@ -249,7 +250,7 @@ declare function forklaunchExpress<SV extends AnySchemaValidator>(schemaValidato
249
250
  * @param {SV} schemaValidator - The schema validator.
250
251
  * @returns {Router<SV>} - The new router instance.
251
252
  */
252
- declare function forklaunchRouter<SV extends AnySchemaValidator, BasePath extends `/${string}`>(basePath: BasePath, schemaValidator: SV): Router<SV, BasePath>;
253
+ declare function forklaunchRouter<SV extends AnySchemaValidator, BasePath extends `/${string}`>(basePath: BasePath, schemaValidator: SV, openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>): Router<SV, BasePath>;
253
254
 
254
255
  declare const handlers: {
255
256
  checkout: typeof checkout;
package/lib/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { AnySchemaValidator, LiteralSchema, IdiomaticSchema, SchemaResolve } from '@forklaunch/validator';
2
1
  import * as _forklaunch_core_http from '@forklaunch/core/http';
3
- import { ForklaunchExpressLikeApplication, ForklaunchExpressLikeRouter, TypedMiddlewareDefinition, ParamsObject, ResponsesObject, Body, QueryObject, HeadersObject, ContractDetails, ExpressLikeSchemaHandler } from '@forklaunch/core/http';
2
+ import { ForklaunchExpressLikeApplication, OpenTelemetryCollector, MetricsDefinition, DocsConfiguration, ForklaunchExpressLikeRouter, TypedMiddlewareDefinition, ParamsObject, ResponsesObject, Body, QueryObject, HeadersObject, ContractDetails, ExpressLikeSchemaHandler } from '@forklaunch/core/http';
3
+ import { AnySchemaValidator, LiteralSchema, IdiomaticSchema, SchemaResolve } from '@forklaunch/validator';
4
4
  import { Express, RequestHandler, Request, Response, NextFunction, Router as Router$1 } from 'express';
5
5
  export { NextFunction, Request, Response } from 'express';
6
6
  import { Server } from 'http';
@@ -14,12 +14,13 @@ import * as express_serve_static_core from 'express-serve-static-core';
14
14
  * @template SV - A type that extends AnySchemaValidator.
15
15
  */
16
16
  declare class Application<SV extends AnySchemaValidator> extends ForklaunchExpressLikeApplication<SV, Express, RequestHandler, Request, Response, NextFunction> {
17
+ private readonly docsConfiguration?;
17
18
  /**
18
19
  * Creates an instance of Application.
19
20
  *
20
21
  * @param {SV} schemaValidator - The schema validator.
21
22
  */
22
- constructor(schemaValidator: SV);
23
+ constructor(schemaValidator: SV, openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>, docsConfiguration?: DocsConfiguration | undefined);
23
24
  /**
24
25
  * Starts the server and sets up Swagger documentation.
25
26
  *
@@ -48,7 +49,7 @@ declare class Router<SV extends AnySchemaValidator, BasePath extends `/${string}
48
49
  * @param {string} basePath - The base path for the router.
49
50
  * @param {SV} schemaValidator - The schema validator.
50
51
  */
51
- constructor(basePath: BasePath, schemaValidator: SV);
52
+ constructor(basePath: BasePath, schemaValidator: SV, openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>);
52
53
  route(path: string): this;
53
54
  param<ParamName extends string, Types extends {
54
55
  req?: LiteralSchema | SV['_SchemaCatchall'];
@@ -240,7 +241,7 @@ declare const unsubscribe: <SV extends AnySchemaValidator, Path extends `/${stri
240
241
  * @param {SV} schemaValidator - The schema validator.
241
242
  * @returns {Application<SV>} - The new application instance.
242
243
  */
243
- declare function forklaunchExpress<SV extends AnySchemaValidator>(schemaValidator: SV): Application<SV>;
244
+ declare function forklaunchExpress<SV extends AnySchemaValidator>(schemaValidator: SV, openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>, docsConfiguration?: DocsConfiguration): Application<SV>;
244
245
  /**
245
246
  * Creates a new instance of Router with the given base path and schema validator.
246
247
  *
@@ -249,7 +250,7 @@ declare function forklaunchExpress<SV extends AnySchemaValidator>(schemaValidato
249
250
  * @param {SV} schemaValidator - The schema validator.
250
251
  * @returns {Router<SV>} - The new router instance.
251
252
  */
252
- declare function forklaunchRouter<SV extends AnySchemaValidator, BasePath extends `/${string}`>(basePath: BasePath, schemaValidator: SV): Router<SV, BasePath>;
253
+ declare function forklaunchRouter<SV extends AnySchemaValidator, BasePath extends `/${string}`>(basePath: BasePath, schemaValidator: SV, openTelemetryCollector: OpenTelemetryCollector<MetricsDefinition>): Router<SV, BasePath>;
253
254
 
254
255
  declare const handlers: {
255
256
  checkout: typeof checkout;
package/lib/index.js CHANGED
@@ -47,40 +47,49 @@ var Application = class extends import_http.ForklaunchExpressLikeApplication {
47
47
  *
48
48
  * @param {SV} schemaValidator - The schema validator.
49
49
  */
50
- constructor(schemaValidator) {
51
- super(schemaValidator, (0, import_express.default)());
50
+ constructor(schemaValidator, openTelemetryCollector, docsConfiguration) {
51
+ super(schemaValidator, (0, import_express.default)(), openTelemetryCollector);
52
+ this.docsConfiguration = docsConfiguration;
52
53
  }
53
54
  listen(...args) {
54
55
  const port = typeof args[0] === "number" ? args[0] : Number(process.env.PORT);
55
- this.internal.use(
56
- `/api/${process.env.VERSION ?? "v1"}/swagger`,
57
- import_swagger_ui_express.default.serve,
58
- import_swagger_ui_express.default.setup(
59
- (0, import_http.generateSwaggerDocument)(this.schemaValidator, port, this.routers)
60
- )
61
- );
62
- this.internal.use(
63
- `/api/${process.env.VERSION ?? "v1"}${process.env.DOCS_PATH ?? "/docs"}`,
64
- (0, import_express_api_reference.apiReference)({
65
- spec: {
66
- content: (0, import_http.generateSwaggerDocument)(
67
- this.schemaValidator,
68
- port,
69
- this.routers
70
- )
71
- }
72
- })
73
- );
56
+ if (this.docsConfiguration == null || this.docsConfiguration.type === "scalar") {
57
+ this.internal.use(
58
+ `/api/${process.env.VERSION ?? "v1"}${process.env.DOCS_PATH ?? "/docs"}`,
59
+ (0, import_express_api_reference.apiReference)({
60
+ spec: {
61
+ content: (0, import_http.generateSwaggerDocument)(
62
+ this.schemaValidator,
63
+ port,
64
+ this.routers
65
+ )
66
+ },
67
+ ...this.docsConfiguration
68
+ })
69
+ );
70
+ } else if (this.docsConfiguration.type === "swagger") {
71
+ this.internal.use(
72
+ `/api/${process.env.VERSION ?? "v1"}${process.env.DOCS_PATH ?? "/docs"}`,
73
+ import_swagger_ui_express.default.serve,
74
+ import_swagger_ui_express.default.setup(
75
+ (0, import_http.generateSwaggerDocument)(this.schemaValidator, port, this.routers)
76
+ )
77
+ );
78
+ }
74
79
  const errorHandler = (err, req, res, _next) => {
75
80
  res.locals.errorMessage = err.message;
81
+ res.type("text/plain");
76
82
  res.status(res.statusCode && res.statusCode >= 400 ? res.statusCode : 500).send(
77
83
  `Internal server error:
78
84
 
79
- ${(0, import_http.isForklaunchRequest)(req) ? req.context.correlationId : "No correlation ID"}`
85
+ Correlation id: ${(0, import_http.isForklaunchRequest)(req) ? req.context.correlationId : "No correlation ID"}`
86
+ );
87
+ (0, import_http.logger)("error").error(
88
+ err.stack ?? err.message,
89
+ (0, import_http.meta)({
90
+ [import_http.ATTR_HTTP_RESPONSE_STATUS_CODE]: res.statusCode ?? 500
91
+ })
80
92
  );
81
- (0, import_http.logger)("error").error(err.stack ?? err.message, {
82
- [import_http.ATTR_HTTP_RESPONSE_STATUS_CODE]: res.statusCode ?? 500
83
- });
84
93
  };
85
94
  this.internal.use(errorHandler);
86
95
  return this.internal.listen(...args);
@@ -131,8 +140,8 @@ var Router = class extends import_http3.ForklaunchExpressLikeRouter {
131
140
  * @param {string} basePath - The base path for the router.
132
141
  * @param {SV} schemaValidator - The schema validator.
133
142
  */
134
- constructor(basePath, schemaValidator) {
135
- super(basePath, schemaValidator, import_express2.default.Router());
143
+ constructor(basePath, schemaValidator, openTelemetryCollector) {
144
+ super(basePath, schemaValidator, import_express2.default.Router(), openTelemetryCollector);
136
145
  this.basePath = basePath;
137
146
  this.internal.use(import_express2.default.json());
138
147
  this.internal.use(enrichResponseTransmission);
@@ -476,11 +485,15 @@ var unsubscribe = (schemaValidator, path, contractDetails, ...handlers2) => {
476
485
  };
477
486
 
478
487
  // index.ts
479
- function forklaunchExpress(schemaValidator) {
480
- return new Application(schemaValidator);
488
+ function forklaunchExpress(schemaValidator, openTelemetryCollector, docsConfiguration) {
489
+ return new Application(
490
+ schemaValidator,
491
+ openTelemetryCollector,
492
+ docsConfiguration
493
+ );
481
494
  }
482
- function forklaunchRouter(basePath, schemaValidator) {
483
- const router = new Router(basePath, schemaValidator);
495
+ function forklaunchRouter(basePath, schemaValidator, openTelemetryCollector) {
496
+ const router = new Router(basePath, schemaValidator, openTelemetryCollector);
484
497
  return router;
485
498
  }
486
499
  var handlers = {
package/lib/index.mjs CHANGED
@@ -4,7 +4,8 @@ import {
4
4
  ForklaunchExpressLikeApplication,
5
5
  generateSwaggerDocument,
6
6
  isForklaunchRequest,
7
- logger
7
+ logger,
8
+ meta
8
9
  } from "@forklaunch/core/http";
9
10
  import { apiReference } from "@scalar/express-api-reference";
10
11
  import express from "express";
@@ -15,40 +16,49 @@ var Application = class extends ForklaunchExpressLikeApplication {
15
16
  *
16
17
  * @param {SV} schemaValidator - The schema validator.
17
18
  */
18
- constructor(schemaValidator) {
19
- super(schemaValidator, express());
19
+ constructor(schemaValidator, openTelemetryCollector, docsConfiguration) {
20
+ super(schemaValidator, express(), openTelemetryCollector);
21
+ this.docsConfiguration = docsConfiguration;
20
22
  }
21
23
  listen(...args) {
22
24
  const port = typeof args[0] === "number" ? args[0] : Number(process.env.PORT);
23
- this.internal.use(
24
- `/api/${process.env.VERSION ?? "v1"}/swagger`,
25
- swaggerUi.serve,
26
- swaggerUi.setup(
27
- generateSwaggerDocument(this.schemaValidator, port, this.routers)
28
- )
29
- );
30
- this.internal.use(
31
- `/api/${process.env.VERSION ?? "v1"}${process.env.DOCS_PATH ?? "/docs"}`,
32
- apiReference({
33
- spec: {
34
- content: generateSwaggerDocument(
35
- this.schemaValidator,
36
- port,
37
- this.routers
38
- )
39
- }
40
- })
41
- );
25
+ if (this.docsConfiguration == null || this.docsConfiguration.type === "scalar") {
26
+ this.internal.use(
27
+ `/api/${process.env.VERSION ?? "v1"}${process.env.DOCS_PATH ?? "/docs"}`,
28
+ apiReference({
29
+ spec: {
30
+ content: generateSwaggerDocument(
31
+ this.schemaValidator,
32
+ port,
33
+ this.routers
34
+ )
35
+ },
36
+ ...this.docsConfiguration
37
+ })
38
+ );
39
+ } else if (this.docsConfiguration.type === "swagger") {
40
+ this.internal.use(
41
+ `/api/${process.env.VERSION ?? "v1"}${process.env.DOCS_PATH ?? "/docs"}`,
42
+ swaggerUi.serve,
43
+ swaggerUi.setup(
44
+ generateSwaggerDocument(this.schemaValidator, port, this.routers)
45
+ )
46
+ );
47
+ }
42
48
  const errorHandler = (err, req, res, _next) => {
43
49
  res.locals.errorMessage = err.message;
50
+ res.type("text/plain");
44
51
  res.status(res.statusCode && res.statusCode >= 400 ? res.statusCode : 500).send(
45
52
  `Internal server error:
46
53
 
47
- ${isForklaunchRequest(req) ? req.context.correlationId : "No correlation ID"}`
54
+ Correlation id: ${isForklaunchRequest(req) ? req.context.correlationId : "No correlation ID"}`
55
+ );
56
+ logger("error").error(
57
+ err.stack ?? err.message,
58
+ meta({
59
+ [ATTR_HTTP_RESPONSE_STATUS_CODE]: res.statusCode ?? 500
60
+ })
48
61
  );
49
- logger("error").error(err.stack ?? err.message, {
50
- [ATTR_HTTP_RESPONSE_STATUS_CODE]: res.statusCode ?? 500
51
- });
52
62
  };
53
63
  this.internal.use(errorHandler);
54
64
  return this.internal.listen(...args);
@@ -101,8 +111,8 @@ var Router = class extends ForklaunchExpressLikeRouter {
101
111
  * @param {string} basePath - The base path for the router.
102
112
  * @param {SV} schemaValidator - The schema validator.
103
113
  */
104
- constructor(basePath, schemaValidator) {
105
- super(basePath, schemaValidator, express2.Router());
114
+ constructor(basePath, schemaValidator, openTelemetryCollector) {
115
+ super(basePath, schemaValidator, express2.Router(), openTelemetryCollector);
106
116
  this.basePath = basePath;
107
117
  this.internal.use(express2.json());
108
118
  this.internal.use(enrichResponseTransmission);
@@ -502,11 +512,15 @@ var unsubscribe = (schemaValidator, path, contractDetails, ...handlers2) => {
502
512
  };
503
513
 
504
514
  // index.ts
505
- function forklaunchExpress(schemaValidator) {
506
- return new Application(schemaValidator);
515
+ function forklaunchExpress(schemaValidator, openTelemetryCollector, docsConfiguration) {
516
+ return new Application(
517
+ schemaValidator,
518
+ openTelemetryCollector,
519
+ docsConfiguration
520
+ );
507
521
  }
508
- function forklaunchRouter(basePath, schemaValidator) {
509
- const router = new Router(basePath, schemaValidator);
522
+ function forklaunchRouter(basePath, schemaValidator, openTelemetryCollector) {
523
+ const router = new Router(basePath, schemaValidator, openTelemetryCollector);
510
524
  return router;
511
525
  }
512
526
  var handlers = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@forklaunch/express",
3
- "version": "0.3.4",
3
+ "version": "0.3.6",
4
4
  "description": "Forklaunch framework for express.",
5
5
  "homepage": "https://github.com/forklaunch/forklaunch-js#readme",
6
6
  "bugs": {
@@ -32,7 +32,7 @@
32
32
  "swagger-ui-express": "^5.0.1",
33
33
  "@forklaunch/common": "0.2.3",
34
34
  "@forklaunch/validator": "0.4.6",
35
- "@forklaunch/core": "0.5.3"
35
+ "@forklaunch/core": "0.5.5"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@eslint/js": "^9.22.0",