@twin.org/api-models 0.0.1-next.9 → 0.0.2-next.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.
Files changed (47) hide show
  1. package/dist/cjs/index.cjs +34 -11
  2. package/dist/esm/index.mjs +34 -14
  3. package/dist/types/factories/mimeTypeProcessorFactory.d.ts +6 -0
  4. package/dist/types/factories/restRouteProcessorFactory.d.ts +6 -0
  5. package/dist/types/factories/socketRouteProcessorFactory.d.ts +6 -0
  6. package/dist/types/index.d.ts +9 -1
  7. package/dist/types/models/config/IBaseSocketClientConfig.d.ts +4 -0
  8. package/dist/types/models/routes/IBaseRouteEntryPoint.d.ts +22 -0
  9. package/dist/types/models/routes/IRestRouteEntryPoint.d.ts +2 -19
  10. package/dist/types/models/routes/ISocketRoute.d.ts +6 -8
  11. package/dist/types/models/routes/ISocketRouteEntryPoint.d.ts +6 -0
  12. package/dist/types/models/server/IBaseRouteProcessor.d.ts +34 -0
  13. package/dist/types/models/server/IMimeTypeProcessor.d.ts +17 -0
  14. package/dist/types/models/server/IRestRouteProcessor.d.ts +22 -0
  15. package/dist/types/models/server/{IHttpRestRouteProcessor.d.ts → ISocketRouteProcessor.d.ts} +12 -15
  16. package/dist/types/models/server/IWebServer.d.ts +7 -3
  17. package/docs/changelog.md +53 -1
  18. package/docs/reference/classes/HttpErrorHelper.md +19 -9
  19. package/docs/reference/classes/HttpParameterHelper.md +31 -15
  20. package/docs/reference/index.md +10 -2
  21. package/docs/reference/interfaces/IBaseRouteEntryPoint.md +55 -0
  22. package/docs/reference/interfaces/IBaseRouteProcessor.md +106 -0
  23. package/docs/reference/interfaces/IBaseSocketClientConfig.md +8 -0
  24. package/docs/reference/interfaces/IHealthInfo.md +18 -0
  25. package/docs/reference/interfaces/IHttpRequest.md +3 -1
  26. package/docs/reference/interfaces/IHttpRequestContext.md +2 -2
  27. package/docs/reference/interfaces/IHttpRequestPathParams.md +1 -1
  28. package/docs/reference/interfaces/IHttpRequestQuery.md +1 -1
  29. package/docs/reference/interfaces/IHttpResponse.md +3 -1
  30. package/docs/reference/interfaces/IHttpServerRequest.md +3 -1
  31. package/docs/reference/interfaces/IInformationComponent.md +13 -5
  32. package/docs/reference/interfaces/IMimeTypeProcessor.md +43 -0
  33. package/docs/reference/interfaces/IRestRoute.md +30 -4
  34. package/docs/reference/interfaces/{IHttpRestRouteProcessor.md → IRestRouteProcessor.md} +55 -23
  35. package/docs/reference/interfaces/IRestRouteRequestExample.md +3 -1
  36. package/docs/reference/interfaces/IRestRouteResponseExample.md +3 -1
  37. package/docs/reference/interfaces/ISocketRoute.md +22 -10
  38. package/docs/reference/interfaces/ISocketRouteProcessor.md +217 -0
  39. package/docs/reference/interfaces/IWebServer.md +26 -6
  40. package/docs/reference/type-aliases/HealthStatus.md +1 -1
  41. package/docs/reference/type-aliases/IRestRouteEntryPoint.md +5 -0
  42. package/docs/reference/type-aliases/ISocketRouteEntryPoint.md +5 -0
  43. package/docs/reference/variables/MimeTypeProcessorFactory.md +5 -0
  44. package/docs/reference/variables/RestRouteProcessorFactory.md +5 -0
  45. package/docs/reference/variables/SocketRouteProcessorFactory.md +5 -0
  46. package/package.json +3 -3
  47. package/docs/reference/interfaces/IRestRouteEntryPoint.md +0 -45
@@ -3,6 +3,30 @@
3
3
  var core = require('@twin.org/core');
4
4
  var web = require('@twin.org/web');
5
5
 
6
+ // Copyright 2024 IOTA Stiftung.
7
+ // SPDX-License-Identifier: Apache-2.0.
8
+ /**
9
+ * Factory for creating implementation of mime type processor types.
10
+ */
11
+ // eslint-disable-next-line @typescript-eslint/naming-convention
12
+ const MimeTypeProcessorFactory = core.Factory.createFactory("mime-type-processor");
13
+
14
+ // Copyright 2024 IOTA Stiftung.
15
+ // SPDX-License-Identifier: Apache-2.0.
16
+ /**
17
+ * Factory for creating implementation of REST route processor types.
18
+ */
19
+ // eslint-disable-next-line @typescript-eslint/naming-convention
20
+ const RestRouteProcessorFactory = core.Factory.createFactory("rest-route-processor");
21
+
22
+ // Copyright 2024 IOTA Stiftung.
23
+ // SPDX-License-Identifier: Apache-2.0.
24
+ /**
25
+ * Factory for creating implementation of socket route processor types.
26
+ */
27
+ // eslint-disable-next-line @typescript-eslint/naming-convention
28
+ const SocketRouteProcessorFactory = core.Factory.createFactory("socket-route-processor");
29
+
6
30
  // Copyright 2024 IOTA Stiftung.
7
31
  // SPDX-License-Identifier: Apache-2.0.
8
32
  /**
@@ -21,18 +45,17 @@ class HttpErrorHelper {
21
45
  // types then set the http response code accordingly
22
46
  const flattened = core.BaseError.flatten(error);
23
47
  let httpStatusCode = web.HttpStatusCode.internalServerError;
24
- if (flattened.some(e => core.BaseError.isErrorName(e, core.GuardError.CLASS_NAME))) {
48
+ if (flattened.some(e => core.BaseError.isErrorName(e, core.GuardError.CLASS_NAME)) ||
49
+ flattened.some(e => core.BaseError.isErrorName(e, core.ValidationError.CLASS_NAME))) {
25
50
  httpStatusCode = web.HttpStatusCode.badRequest;
26
51
  }
27
- else if (flattened.some(e => core.BaseError.isErrorName(e, core.ConflictError.CLASS_NAME))) {
52
+ else if (flattened.some(e => core.BaseError.isErrorName(e, core.ConflictError.CLASS_NAME)) ||
53
+ flattened.some(e => core.BaseError.isErrorName(e, core.AlreadyExistsError.CLASS_NAME))) {
28
54
  httpStatusCode = web.HttpStatusCode.conflict;
29
55
  }
30
56
  else if (flattened.some(e => core.BaseError.isErrorName(e, core.NotFoundError.CLASS_NAME))) {
31
57
  httpStatusCode = web.HttpStatusCode.notFound;
32
58
  }
33
- else if (flattened.some(e => core.BaseError.isErrorName(e, core.AlreadyExistsError.CLASS_NAME))) {
34
- httpStatusCode = web.HttpStatusCode.conflict;
35
- }
36
59
  else if (flattened.some(e => core.BaseError.isErrorName(e, core.UnauthorizedError.CLASS_NAME))) {
37
60
  httpStatusCode = web.HttpStatusCode.unauthorized;
38
61
  }
@@ -42,10 +65,7 @@ class HttpErrorHelper {
42
65
  else if (flattened.some(e => core.BaseError.isErrorName(e, core.UnprocessableError.CLASS_NAME))) {
43
66
  httpStatusCode = web.HttpStatusCode.unprocessableEntity;
44
67
  }
45
- const returnError = error.toJsonObject();
46
- if (!includeStack) {
47
- delete returnError.stack;
48
- }
68
+ const returnError = error.toJsonObject(includeStack);
49
69
  return {
50
70
  error: returnError,
51
71
  httpStatusCode
@@ -93,7 +113,7 @@ class HttpParameterHelper {
93
113
  * @returns The object.
94
114
  */
95
115
  static objectFromString(value) {
96
- return core.Coerce.object(value);
116
+ return core.Is.json(value) ? JSON.parse(value) : undefined;
97
117
  }
98
118
  /**
99
119
  * Convert object to query string.
@@ -101,7 +121,7 @@ class HttpParameterHelper {
101
121
  * @returns The converted object.
102
122
  */
103
123
  static objectToString(value) {
104
- return core.Is.object(value) ? JSON.stringify(value) : undefined;
124
+ return core.Is.empty(value) ? undefined : JSON.stringify(value);
105
125
  }
106
126
  }
107
127
 
@@ -129,3 +149,6 @@ const HealthStatus = {
129
149
  exports.HealthStatus = HealthStatus;
130
150
  exports.HttpErrorHelper = HttpErrorHelper;
131
151
  exports.HttpParameterHelper = HttpParameterHelper;
152
+ exports.MimeTypeProcessorFactory = MimeTypeProcessorFactory;
153
+ exports.RestRouteProcessorFactory = RestRouteProcessorFactory;
154
+ exports.SocketRouteProcessorFactory = SocketRouteProcessorFactory;
@@ -1,5 +1,29 @@
1
- import { BaseError, GuardError, ConflictError, NotFoundError, AlreadyExistsError, UnauthorizedError, NotImplementedError, UnprocessableError, Coerce, Is } from '@twin.org/core';
2
- import { HttpStatusCode, HeaderTypes, MimeTypes } from '@twin.org/web';
1
+ import { Factory, BaseError, GuardError, ValidationError, ConflictError, AlreadyExistsError, NotFoundError, UnauthorizedError, NotImplementedError, UnprocessableError, Is } from '@twin.org/core';
2
+ import { HttpStatusCode, MimeTypes, HeaderTypes } from '@twin.org/web';
3
+
4
+ // Copyright 2024 IOTA Stiftung.
5
+ // SPDX-License-Identifier: Apache-2.0.
6
+ /**
7
+ * Factory for creating implementation of mime type processor types.
8
+ */
9
+ // eslint-disable-next-line @typescript-eslint/naming-convention
10
+ const MimeTypeProcessorFactory = Factory.createFactory("mime-type-processor");
11
+
12
+ // Copyright 2024 IOTA Stiftung.
13
+ // SPDX-License-Identifier: Apache-2.0.
14
+ /**
15
+ * Factory for creating implementation of REST route processor types.
16
+ */
17
+ // eslint-disable-next-line @typescript-eslint/naming-convention
18
+ const RestRouteProcessorFactory = Factory.createFactory("rest-route-processor");
19
+
20
+ // Copyright 2024 IOTA Stiftung.
21
+ // SPDX-License-Identifier: Apache-2.0.
22
+ /**
23
+ * Factory for creating implementation of socket route processor types.
24
+ */
25
+ // eslint-disable-next-line @typescript-eslint/naming-convention
26
+ const SocketRouteProcessorFactory = Factory.createFactory("socket-route-processor");
3
27
 
4
28
  // Copyright 2024 IOTA Stiftung.
5
29
  // SPDX-License-Identifier: Apache-2.0.
@@ -19,18 +43,17 @@ class HttpErrorHelper {
19
43
  // types then set the http response code accordingly
20
44
  const flattened = BaseError.flatten(error);
21
45
  let httpStatusCode = HttpStatusCode.internalServerError;
22
- if (flattened.some(e => BaseError.isErrorName(e, GuardError.CLASS_NAME))) {
46
+ if (flattened.some(e => BaseError.isErrorName(e, GuardError.CLASS_NAME)) ||
47
+ flattened.some(e => BaseError.isErrorName(e, ValidationError.CLASS_NAME))) {
23
48
  httpStatusCode = HttpStatusCode.badRequest;
24
49
  }
25
- else if (flattened.some(e => BaseError.isErrorName(e, ConflictError.CLASS_NAME))) {
50
+ else if (flattened.some(e => BaseError.isErrorName(e, ConflictError.CLASS_NAME)) ||
51
+ flattened.some(e => BaseError.isErrorName(e, AlreadyExistsError.CLASS_NAME))) {
26
52
  httpStatusCode = HttpStatusCode.conflict;
27
53
  }
28
54
  else if (flattened.some(e => BaseError.isErrorName(e, NotFoundError.CLASS_NAME))) {
29
55
  httpStatusCode = HttpStatusCode.notFound;
30
56
  }
31
- else if (flattened.some(e => BaseError.isErrorName(e, AlreadyExistsError.CLASS_NAME))) {
32
- httpStatusCode = HttpStatusCode.conflict;
33
- }
34
57
  else if (flattened.some(e => BaseError.isErrorName(e, UnauthorizedError.CLASS_NAME))) {
35
58
  httpStatusCode = HttpStatusCode.unauthorized;
36
59
  }
@@ -40,10 +63,7 @@ class HttpErrorHelper {
40
63
  else if (flattened.some(e => BaseError.isErrorName(e, UnprocessableError.CLASS_NAME))) {
41
64
  httpStatusCode = HttpStatusCode.unprocessableEntity;
42
65
  }
43
- const returnError = error.toJsonObject();
44
- if (!includeStack) {
45
- delete returnError.stack;
46
- }
66
+ const returnError = error.toJsonObject(includeStack);
47
67
  return {
48
68
  error: returnError,
49
69
  httpStatusCode
@@ -91,7 +111,7 @@ class HttpParameterHelper {
91
111
  * @returns The object.
92
112
  */
93
113
  static objectFromString(value) {
94
- return Coerce.object(value);
114
+ return Is.json(value) ? JSON.parse(value) : undefined;
95
115
  }
96
116
  /**
97
117
  * Convert object to query string.
@@ -99,7 +119,7 @@ class HttpParameterHelper {
99
119
  * @returns The converted object.
100
120
  */
101
121
  static objectToString(value) {
102
- return Is.object(value) ? JSON.stringify(value) : undefined;
122
+ return Is.empty(value) ? undefined : JSON.stringify(value);
103
123
  }
104
124
  }
105
125
 
@@ -124,4 +144,4 @@ const HealthStatus = {
124
144
  Error: "error"
125
145
  };
126
146
 
127
- export { HealthStatus, HttpErrorHelper, HttpParameterHelper };
147
+ export { HealthStatus, HttpErrorHelper, HttpParameterHelper, MimeTypeProcessorFactory, RestRouteProcessorFactory, SocketRouteProcessorFactory };
@@ -0,0 +1,6 @@
1
+ import { Factory } from "@twin.org/core";
2
+ import type { IMimeTypeProcessor } from "../models/server/IMimeTypeProcessor";
3
+ /**
4
+ * Factory for creating implementation of mime type processor types.
5
+ */
6
+ export declare const MimeTypeProcessorFactory: Factory<IMimeTypeProcessor>;
@@ -0,0 +1,6 @@
1
+ import { Factory } from "@twin.org/core";
2
+ import type { IRestRouteProcessor } from "../models/server/IRestRouteProcessor";
3
+ /**
4
+ * Factory for creating implementation of REST route processor types.
5
+ */
6
+ export declare const RestRouteProcessorFactory: Factory<IRestRouteProcessor>;
@@ -0,0 +1,6 @@
1
+ import { Factory } from "@twin.org/core";
2
+ import type { ISocketRouteProcessor } from "../models/server/ISocketRouteProcessor";
3
+ /**
4
+ * Factory for creating implementation of socket route processor types.
5
+ */
6
+ export declare const SocketRouteProcessorFactory: Factory<ISocketRouteProcessor>;
@@ -1,3 +1,6 @@
1
+ export * from "./factories/mimeTypeProcessorFactory";
2
+ export * from "./factories/restRouteProcessorFactory";
3
+ export * from "./factories/socketRouteProcessorFactory";
1
4
  export * from "./helpers/httpErrorHelper";
2
5
  export * from "./helpers/httpParameterHelper";
3
6
  export * from "./models/api/IServerHealthResponse";
@@ -25,6 +28,7 @@ export * from "./models/responses/success/ICreatedResponse";
25
28
  export * from "./models/responses/success/INoContentResponse";
26
29
  export * from "./models/responses/success/IOkResponse";
27
30
  export * from "./models/routes/IBaseRoute";
31
+ export * from "./models/routes/IBaseRouteEntryPoint";
28
32
  export * from "./models/routes/IRestRoute";
29
33
  export * from "./models/routes/IRestRouteEntryPoint";
30
34
  export * from "./models/routes/IRestRouteExample";
@@ -33,8 +37,12 @@ export * from "./models/routes/IRestRouteResponseAttachmentOptions";
33
37
  export * from "./models/routes/IRestRouteResponseExample";
34
38
  export * from "./models/routes/IRestRouteResponseOptions";
35
39
  export * from "./models/routes/ISocketRoute";
40
+ export * from "./models/routes/ISocketRouteEntryPoint";
36
41
  export * from "./models/routes/ITag";
37
- export * from "./models/server/IHttpRestRouteProcessor";
42
+ export * from "./models/server/IBaseRouteProcessor";
43
+ export * from "./models/server/IMimeTypeProcessor";
44
+ export * from "./models/server/IRestRouteProcessor";
45
+ export * from "./models/server/ISocketRouteProcessor";
38
46
  export * from "./models/server/IWebServer";
39
47
  export * from "./models/server/IWebServerOptions";
40
48
  export * from "./models/services/healthStatus";
@@ -3,6 +3,10 @@ import type { IHttpHeaders } from "@twin.org/web";
3
3
  * Definition for the configuration of a socket service.
4
4
  */
5
5
  export interface IBaseSocketClientConfig {
6
+ /**
7
+ * Base path for the socket service, defaults to /socket.
8
+ */
9
+ basePath?: string;
6
10
  /**
7
11
  * The endpoint where the api is hosted.
8
12
  */
@@ -0,0 +1,22 @@
1
+ import type { ITag } from "./ITag";
2
+ /**
3
+ * Route entry points are used for exposing the routes from a package.
4
+ */
5
+ export interface IBaseRouteEntryPoint<T> {
6
+ /**
7
+ * The name of the routes.
8
+ */
9
+ name: string;
10
+ /**
11
+ * The default base route name for the routes.
12
+ */
13
+ defaultBaseRoute: string;
14
+ /**
15
+ * The tags for the routes.
16
+ */
17
+ tags: ITag[];
18
+ /**
19
+ * The method to generate the routes.
20
+ */
21
+ generateRoutes: (baseRouteName: string, componentName: string) => T[];
22
+ }
@@ -1,23 +1,6 @@
1
+ import type { IBaseRouteEntryPoint } from "./IBaseRouteEntryPoint";
1
2
  import type { IRestRoute } from "./IRestRoute";
2
- import type { ITag } from "./ITag";
3
3
  /**
4
4
  * Route entry points are used for exposing the REST routes from a package.
5
5
  */
6
- export interface IRestRouteEntryPoint {
7
- /**
8
- * The name of the REST routes.
9
- */
10
- name: string;
11
- /**
12
- * The default base route name for the REST routes.
13
- */
14
- defaultBaseRoute: string;
15
- /**
16
- * The tags for the REST routes.
17
- */
18
- tags: ITag[];
19
- /**
20
- * The method to generate the REST routes.
21
- */
22
- generateRoutes: (baseRouteName: string, componentName: string) => IRestRoute[];
23
- }
6
+ export type IRestRouteEntryPoint = IBaseRouteEntryPoint<IRestRoute>;
@@ -1,9 +1,11 @@
1
1
  import type { IBaseRoute } from "./IBaseRoute";
2
+ import type { IHttpRequest } from "../protocol/IHttpRequest";
2
3
  import type { IHttpRequestContext } from "../protocol/IHttpRequestContext";
4
+ import type { IHttpResponse } from "../protocol/IHttpResponse";
3
5
  /**
4
6
  * Interface which defines a socket route.
5
7
  */
6
- export interface ISocketRoute extends IBaseRoute {
8
+ export interface ISocketRoute<T extends IHttpRequest = any, U extends IHttpResponse = any> extends IBaseRoute {
7
9
  /**
8
10
  * The handler module.
9
11
  */
@@ -12,16 +14,12 @@ export interface ISocketRoute extends IBaseRoute {
12
14
  * The request context.
13
15
  */
14
16
  httpRequestContext: IHttpRequestContext,
15
- /**
16
- * The id of the socket the request is arriving on.
17
- */
18
- socketId: string,
19
17
  /**
20
18
  * The request object.
21
19
  */
22
- request: unknown,
20
+ request: T,
23
21
  /**
24
- * Method to emit data on the socket.
22
+ * The function to emit an event.
25
23
  */
26
- emitter: (topic: string, response?: unknown) => Promise<void>) => Promise<void>;
24
+ emit: (event: string, response: U) => Promise<void>) => void;
27
25
  }
@@ -0,0 +1,6 @@
1
+ import type { IBaseRouteEntryPoint } from "./IBaseRouteEntryPoint";
2
+ import type { ISocketRoute } from "./ISocketRoute";
3
+ /**
4
+ * Route entry points are used for exposing the socket routes from a package.
5
+ */
6
+ export type ISocketRouteEntryPoint = IBaseRouteEntryPoint<ISocketRoute>;
@@ -0,0 +1,34 @@
1
+ import type { IComponent } from "@twin.org/core";
2
+ import type { IHttpRequestIdentity } from "../protocol/IHttpRequestIdentity";
3
+ import type { IHttpResponse } from "../protocol/IHttpResponse";
4
+ import type { IHttpServerRequest } from "../protocol/IHttpServerRequest";
5
+ import type { IBaseRoute } from "../routes/IBaseRoute";
6
+ /**
7
+ * The definition for a base processor for handling REST routes.
8
+ */
9
+ export interface IBaseRouteProcessor<T = IBaseRoute> extends IComponent {
10
+ /**
11
+ * Pre process the REST request for the specified route.
12
+ * @param request The request to handle.
13
+ * @param response The response data to send if any.
14
+ * @param route The route being requested, if a matching one was found.
15
+ * @param requestIdentity The identity context for the request.
16
+ * @param processorState The state handed through the processors.
17
+ * @returns Promise that resolves when the request is processed.
18
+ */
19
+ pre?(request: IHttpServerRequest, response: IHttpResponse, route: T | undefined, requestIdentity: IHttpRequestIdentity, processorState: {
20
+ [id: string]: unknown;
21
+ }): Promise<void>;
22
+ /**
23
+ * Post process the REST request for the specified route.
24
+ * @param request The request to handle.
25
+ * @param response The response data to send if any.
26
+ * @param route The route being requested, if a matching one was found.
27
+ * @param requestIdentity The identity context for the request.
28
+ * @param processorState The state handed through the processors.
29
+ * @returns Promise that resolves when the request is processed.
30
+ */
31
+ post?(request: IHttpServerRequest, response: IHttpResponse, route: T | undefined, requestIdentity: IHttpRequestIdentity, processorState: {
32
+ [id: string]: unknown;
33
+ }): Promise<void>;
34
+ }
@@ -0,0 +1,17 @@
1
+ import type { IComponent } from "@twin.org/core";
2
+ /**
3
+ * The definition for a handler for a specific MIME type.
4
+ */
5
+ export interface IMimeTypeProcessor extends IComponent {
6
+ /**
7
+ * Get the MIME types that this handler can handle.
8
+ * @returns The MIME types that this handler can handle.
9
+ */
10
+ getTypes(): string[];
11
+ /**
12
+ * Handle content.
13
+ * @param body The body to process.
14
+ * @returns The processed body.
15
+ */
16
+ handle(body: Uint8Array): Promise<unknown>;
17
+ }
@@ -0,0 +1,22 @@
1
+ import type { IBaseRouteProcessor } from "./IBaseRouteProcessor";
2
+ import type { IHttpRequestIdentity } from "../protocol/IHttpRequestIdentity";
3
+ import type { IHttpResponse } from "../protocol/IHttpResponse";
4
+ import type { IHttpServerRequest } from "../protocol/IHttpServerRequest";
5
+ import type { IRestRoute } from "../routes/IRestRoute";
6
+ /**
7
+ * The definition for a processor for handling REST routes.
8
+ */
9
+ export interface IRestRouteProcessor extends IBaseRouteProcessor<IRestRoute> {
10
+ /**
11
+ * Process the REST request for the specified route.
12
+ * @param request The request to handle.
13
+ * @param response The response data to send if any.
14
+ * @param route The route being requested, if a matching one was found.
15
+ * @param requestIdentity The identity context for the request.
16
+ * @param processorState The state handed through the processors.
17
+ * @returns Promise that resolves when the request is processed.
18
+ */
19
+ process?(request: IHttpServerRequest, response: IHttpResponse, route: IRestRoute | undefined, requestIdentity: IHttpRequestIdentity, processorState: {
20
+ [id: string]: unknown;
21
+ }): Promise<void>;
22
+ }
@@ -1,46 +1,43 @@
1
- import type { IComponent } from "@twin.org/core";
1
+ import type { IBaseRouteProcessor } from "./IBaseRouteProcessor";
2
2
  import type { IHttpRequestIdentity } from "../protocol/IHttpRequestIdentity";
3
3
  import type { IHttpResponse } from "../protocol/IHttpResponse";
4
4
  import type { IHttpServerRequest } from "../protocol/IHttpServerRequest";
5
- import type { IRestRoute } from "../routes/IRestRoute";
5
+ import type { ISocketRoute } from "../routes/ISocketRoute";
6
6
  /**
7
- * The definition for a processor for handling REST routes.
7
+ * The definition for a processor for handling socket routes.
8
8
  */
9
- export interface IHttpRestRouteProcessor extends IComponent {
9
+ export interface ISocketRouteProcessor extends IBaseRouteProcessor<ISocketRoute> {
10
10
  /**
11
- * Pre process the REST request for the specified route.
11
+ * Process the connected event.
12
12
  * @param request The request to handle.
13
- * @param response The response data to send if any.
14
13
  * @param route The route being requested, if a matching one was found.
15
- * @param requestIdentity The identity context for the request.
16
14
  * @param processorState The state handed through the processors.
17
15
  * @returns Promise that resolves when the request is processed.
18
16
  */
19
- pre?(request: IHttpServerRequest, response: IHttpResponse, route: IRestRoute | undefined, requestIdentity: IHttpRequestIdentity, processorState: {
17
+ connected?(request: IHttpServerRequest, route: ISocketRoute | undefined, processorState: {
20
18
  [id: string]: unknown;
21
19
  }): Promise<void>;
22
20
  /**
23
- * Process the REST request for the specified route.
21
+ * Process the disconnected event.
24
22
  * @param request The request to handle.
25
- * @param response The response data to send if any.
26
23
  * @param route The route being requested, if a matching one was found.
27
- * @param requestIdentity The identity context for the request.
28
24
  * @param processorState The state handed through the processors.
29
25
  * @returns Promise that resolves when the request is processed.
30
26
  */
31
- process?(request: IHttpServerRequest, response: IHttpResponse, route: IRestRoute | undefined, requestIdentity: IHttpRequestIdentity, processorState: {
27
+ disconnected?(request: IHttpServerRequest, route: ISocketRoute | undefined, processorState: {
32
28
  [id: string]: unknown;
33
29
  }): Promise<void>;
34
30
  /**
35
- * Post process the REST request for the specified route.
31
+ * Process the REST request for the specified route.
36
32
  * @param request The request to handle.
37
33
  * @param response The response data to send if any.
38
34
  * @param route The route being requested, if a matching one was found.
39
35
  * @param requestIdentity The identity context for the request.
40
36
  * @param processorState The state handed through the processors.
37
+ * @param responseEmitter The function to emit a response.
41
38
  * @returns Promise that resolves when the request is processed.
42
39
  */
43
- post?(request: IHttpServerRequest, response: IHttpResponse, route: IRestRoute | undefined, requestIdentity: IHttpRequestIdentity, processorState: {
40
+ process?(request: IHttpServerRequest, response: IHttpResponse, route: ISocketRoute | undefined, requestIdentity: IHttpRequestIdentity, processorState: {
44
41
  [id: string]: unknown;
45
- }): Promise<void>;
42
+ }, responseEmitter: (topic: string, response: IHttpResponse) => Promise<void>): Promise<void>;
46
43
  }
@@ -1,6 +1,8 @@
1
- import type { IHttpRestRouteProcessor } from "./IHttpRestRouteProcessor";
1
+ import type { IRestRouteProcessor } from "./IRestRouteProcessor";
2
+ import type { ISocketRouteProcessor } from "./ISocketRouteProcessor";
2
3
  import type { IWebServerOptions } from "./IWebServerOptions";
3
4
  import type { IRestRoute } from "../routes/IRestRoute";
5
+ import type { ISocketRoute } from "../routes/ISocketRoute";
4
6
  /**
5
7
  * Interface describing a web server.
6
8
  */
@@ -12,12 +14,14 @@ export interface IWebServer<T> {
12
14
  getInstance(): T;
13
15
  /**
14
16
  * Build the server.
15
- * @param restRouteProcessors The hooks to process the incoming requests.
17
+ * @param restRouteProcessors The processors for incoming requests over REST.
16
18
  * @param restRoutes The REST routes.
19
+ * @param socketRouteProcessors The processors for incoming requests over Sockets.
20
+ * @param socketRoutes The socket routes.
17
21
  * @param options Options for building the server.
18
22
  * @returns Nothing.
19
23
  */
20
- build(restRouteProcessors: IHttpRestRouteProcessor[], restRoutes: IRestRoute[], options?: IWebServerOptions): Promise<void>;
24
+ build(restRouteProcessors?: IRestRouteProcessor[], restRoutes?: IRestRoute[], socketRouteProcessors?: ISocketRouteProcessor[], socketRoutes?: ISocketRoute[], options?: IWebServerOptions): Promise<void>;
21
25
  /**
22
26
  * Start the server.
23
27
  * @returns Nothing.
package/docs/changelog.md CHANGED
@@ -1,5 +1,57 @@
1
1
  # @twin.org/api-models - Changelog
2
2
 
3
- ## v0.0.1-next.9
3
+ ## [0.0.2-next.1](https://github.com/twinfoundation/api/compare/api-models-v0.0.2-next.0...api-models-v0.0.2-next.1) (2025-07-08)
4
+
5
+
6
+ ### Features
7
+
8
+ * update dependencies ([1171dc4](https://github.com/twinfoundation/api/commit/1171dc416a9481737f6a640e3cf30145768f37e9))
9
+ * use new includeStackTrace flag for toJsonObject ([6452b15](https://github.com/twinfoundation/api/commit/6452b153af786eee14b21152420f8a2578b70593))
10
+ * use shared store mechanism ([#19](https://github.com/twinfoundation/api/issues/19)) ([32116df](https://github.com/twinfoundation/api/commit/32116df3b4380a30137f5056f242a5c99afa2df9))
11
+ * validationError mapped to http status badrequest ([adc5eb1](https://github.com/twinfoundation/api/commit/adc5eb11d987abb0ab9f7e0dc8e1fdae207e436e))
12
+
13
+ ## 0.0.1 (2025-07-03)
14
+
15
+
16
+ ### Features
17
+
18
+ * release to production ([70ee2d5](https://github.com/twinfoundation/api/commit/70ee2d56a1dc9537d7c9c154d4cb78a235678a3a))
19
+
20
+ ## [0.0.1-next.36](https://github.com/twinfoundation/api/compare/api-models-v0.0.1-next.35...api-models-v0.0.1-next.36) (2025-06-17)
21
+
22
+
23
+ ### Features
24
+
25
+ * use new includeStackTrace flag for toJsonObject ([6452b15](https://github.com/twinfoundation/api/commit/6452b153af786eee14b21152420f8a2578b70593))
26
+
27
+ ## [0.0.1-next.35](https://github.com/twinfoundation/api/compare/api-models-v0.0.1-next.34...api-models-v0.0.1-next.35) (2025-06-11)
28
+
29
+
30
+ ### Features
31
+
32
+ * update dependencies ([1171dc4](https://github.com/twinfoundation/api/commit/1171dc416a9481737f6a640e3cf30145768f37e9))
33
+
34
+ ## [0.0.1-next.34](https://github.com/twinfoundation/api/compare/api-models-v0.0.1-next.33...api-models-v0.0.1-next.34) (2025-05-27)
35
+
36
+
37
+ ### Features
38
+
39
+ * validationError mapped to http status badrequest ([adc5eb1](https://github.com/twinfoundation/api/commit/adc5eb11d987abb0ab9f7e0dc8e1fdae207e436e))
40
+
41
+ ## [0.0.1-next.33](https://github.com/twinfoundation/api/compare/api-models-v0.0.1-next.32...api-models-v0.0.1-next.33) (2025-04-17)
42
+
43
+
44
+ ### Features
45
+
46
+ * use shared store mechanism ([#19](https://github.com/twinfoundation/api/issues/19)) ([32116df](https://github.com/twinfoundation/api/commit/32116df3b4380a30137f5056f242a5c99afa2df9))
47
+
48
+ ## [0.0.1-next.32](https://github.com/twinfoundation/api/compare/api-models-v0.0.1-next.31...api-models-v0.0.1-next.32) (2025-03-28)
49
+
50
+
51
+ ### Miscellaneous Chores
52
+
53
+ * **api-models:** Synchronize repo versions
54
+
55
+ ## v0.0.1-next.31
4
56
 
5
57
  - Initial Release
@@ -4,29 +4,33 @@ Class to help with processing http errors.
4
4
 
5
5
  ## Constructors
6
6
 
7
- ### new HttpErrorHelper()
7
+ ### Constructor
8
8
 
9
- > **new HttpErrorHelper**(): [`HttpErrorHelper`](HttpErrorHelper.md)
9
+ > **new HttpErrorHelper**(): `HttpErrorHelper`
10
10
 
11
11
  #### Returns
12
12
 
13
- [`HttpErrorHelper`](HttpErrorHelper.md)
13
+ `HttpErrorHelper`
14
14
 
15
15
  ## Methods
16
16
 
17
17
  ### processError()
18
18
 
19
- > `static` **processError**(`err`, `includeStack`?): `object`
19
+ > `static` **processError**(`err`, `includeStack?`): `object`
20
20
 
21
21
  Process the errors from the routes.
22
22
 
23
23
  #### Parameters
24
24
 
25
- **err**: `unknown`
25
+ ##### err
26
+
27
+ `unknown`
26
28
 
27
29
  The error to process.
28
30
 
29
- **includeStack?**: `boolean`
31
+ ##### includeStack?
32
+
33
+ `boolean`
30
34
 
31
35
  Should the stack be included in the error.
32
36
 
@@ -54,15 +58,21 @@ Build an error response.
54
58
 
55
59
  #### Parameters
56
60
 
57
- **response**: [`IHttpResponse`](../interfaces/IHttpResponse.md)\<`any`\>
61
+ ##### response
62
+
63
+ [`IHttpResponse`](../interfaces/IHttpResponse.md)
58
64
 
59
65
  The response to build the error into.
60
66
 
61
- **error**: `IError`
67
+ ##### error
68
+
69
+ `IError`
62
70
 
63
71
  The error to build the response for.
64
72
 
65
- **statusCode**: `HttpStatusCode`
73
+ ##### statusCode
74
+
75
+ `HttpStatusCode`
66
76
 
67
77
  The status code to use for the error.
68
78