@twin.org/api-server-fastify 0.0.1-next.11 → 0.0.1-next.12

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.
@@ -53,11 +53,17 @@ class FastifyWebServer {
53
53
  * @internal
54
54
  */
55
55
  _started;
56
+ /**
57
+ * The mime type processors.
58
+ * @internal
59
+ */
60
+ _mimeTypeProcessors;
56
61
  /**
57
62
  * Create a new instance of FastifyWebServer.
58
63
  * @param options The options for the server.
59
64
  * @param options.loggingConnectorType The type of the logging connector to use, if undefined, no logging will happen.
60
65
  * @param options.config Additional options for the Fastify server.
66
+ * @param options.mimeTypeProcessors Additional MIME type processors.
61
67
  */
62
68
  constructor(options) {
63
69
  this._loggingConnector = core.Is.stringValue(options?.loggingConnectorType)
@@ -65,6 +71,7 @@ class FastifyWebServer {
65
71
  : undefined;
66
72
  this._fastify = Fastify({ maxParamLength: 2000, ...options?.config });
67
73
  this._started = false;
74
+ this._mimeTypeProcessors = options?.mimeTypeProcessors ?? [];
68
75
  }
69
76
  /**
70
77
  * Get the web server instance.
@@ -92,6 +99,19 @@ class FastifyWebServer {
92
99
  });
93
100
  this._options = options;
94
101
  await this._fastify.register(FastifyCompress);
102
+ if (core.Is.arrayValue(this._mimeTypeProcessors)) {
103
+ for (const contentTypeHandler of this._mimeTypeProcessors) {
104
+ this._fastify.addContentTypeParser(contentTypeHandler.getTypes(), { parseAs: "buffer" }, async (request, body, done) => {
105
+ try {
106
+ const processed = await contentTypeHandler.handle(body);
107
+ done(null, processed);
108
+ }
109
+ catch (err) {
110
+ done(core.BaseError.fromError(err));
111
+ }
112
+ });
113
+ }
114
+ }
95
115
  await this.initCors(options);
96
116
  this._fastify.setNotFoundHandler({}, async (request, reply) => this.handleRequest(restRouteProcessors, request, reply));
97
117
  this._fastify.setErrorHandler(async (error, request, reply) => {
@@ -51,11 +51,17 @@ class FastifyWebServer {
51
51
  * @internal
52
52
  */
53
53
  _started;
54
+ /**
55
+ * The mime type processors.
56
+ * @internal
57
+ */
58
+ _mimeTypeProcessors;
54
59
  /**
55
60
  * Create a new instance of FastifyWebServer.
56
61
  * @param options The options for the server.
57
62
  * @param options.loggingConnectorType The type of the logging connector to use, if undefined, no logging will happen.
58
63
  * @param options.config Additional options for the Fastify server.
64
+ * @param options.mimeTypeProcessors Additional MIME type processors.
59
65
  */
60
66
  constructor(options) {
61
67
  this._loggingConnector = Is.stringValue(options?.loggingConnectorType)
@@ -63,6 +69,7 @@ class FastifyWebServer {
63
69
  : undefined;
64
70
  this._fastify = Fastify({ maxParamLength: 2000, ...options?.config });
65
71
  this._started = false;
72
+ this._mimeTypeProcessors = options?.mimeTypeProcessors ?? [];
66
73
  }
67
74
  /**
68
75
  * Get the web server instance.
@@ -90,6 +97,19 @@ class FastifyWebServer {
90
97
  });
91
98
  this._options = options;
92
99
  await this._fastify.register(FastifyCompress);
100
+ if (Is.arrayValue(this._mimeTypeProcessors)) {
101
+ for (const contentTypeHandler of this._mimeTypeProcessors) {
102
+ this._fastify.addContentTypeParser(contentTypeHandler.getTypes(), { parseAs: "buffer" }, async (request, body, done) => {
103
+ try {
104
+ const processed = await contentTypeHandler.handle(body);
105
+ done(null, processed);
106
+ }
107
+ catch (err) {
108
+ done(BaseError.fromError(err));
109
+ }
110
+ });
111
+ }
112
+ }
93
113
  await this.initCors(options);
94
114
  this._fastify.setNotFoundHandler({}, async (request, reply) => this.handleRequest(restRouteProcessors, request, reply));
95
115
  this._fastify.setErrorHandler(async (error, request, reply) => {
@@ -1,5 +1,5 @@
1
- import { type IHttpRestRouteProcessor, type IRestRoute, type IWebServer, type IWebServerOptions } from "@twin.org/api-models";
2
- import { type FastifyServerOptions, type FastifyInstance } from "fastify";
1
+ import { type IHttpRestRouteProcessor, type IMimeTypeProcessor, type IRestRoute, type IWebServer, type IWebServerOptions } from "@twin.org/api-models";
2
+ import { type FastifyInstance, type FastifyServerOptions } from "fastify";
3
3
  /**
4
4
  * Implementation of the web server using Fastify.
5
5
  */
@@ -13,10 +13,12 @@ export declare class FastifyWebServer implements IWebServer<FastifyInstance> {
13
13
  * @param options The options for the server.
14
14
  * @param options.loggingConnectorType The type of the logging connector to use, if undefined, no logging will happen.
15
15
  * @param options.config Additional options for the Fastify server.
16
+ * @param options.mimeTypeProcessors Additional MIME type processors.
16
17
  */
17
18
  constructor(options?: {
18
19
  loggingConnectorType?: string;
19
20
  config?: FastifyServerOptions;
21
+ mimeTypeProcessors?: IMimeTypeProcessor[];
20
22
  });
21
23
  /**
22
24
  * Get the web server instance.
package/docs/changelog.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # @twin.org/api-server-fastify - Changelog
2
2
 
3
- ## v0.0.1-next.11
3
+ ## v0.0.1-next.12
4
4
 
5
5
  - Initial Release
@@ -28,6 +28,10 @@ The type of the logging connector to use, if undefined, no logging will happen.
28
28
 
29
29
  Additional options for the Fastify server.
30
30
 
31
+ • **options.mimeTypeProcessors?**: `IMimeTypeProcessor`[]
32
+
33
+ Additional MIME type processors.
34
+
31
35
  #### Returns
32
36
 
33
37
  [`FastifyWebServer`](FastifyWebServer.md)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/api-server-fastify",
3
- "version": "0.0.1-next.11",
3
+ "version": "0.0.1-next.12",
4
4
  "description": "Use Fastify as the core web server for APIs",
5
5
  "repository": {
6
6
  "type": "git",
@@ -16,8 +16,8 @@
16
16
  "dependencies": {
17
17
  "@fastify/compress": "8.0.1",
18
18
  "@fastify/cors": "10.0.1",
19
- "@twin.org/api-core": "0.0.1-next.11",
20
- "@twin.org/api-models": "0.0.1-next.11",
19
+ "@twin.org/api-core": "0.0.1-next.12",
20
+ "@twin.org/api-models": "0.0.1-next.12",
21
21
  "@twin.org/core": "next",
22
22
  "@twin.org/logging-models": "next",
23
23
  "@twin.org/nameof": "next",