@twin.org/api-models 0.0.1-next.8 → 0.0.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 (48) hide show
  1. package/dist/cjs/index.cjs +42 -70
  2. package/dist/esm/index.mjs +42 -73
  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/helpers/httpParameterHelper.d.ts +8 -27
  7. package/dist/types/index.d.ts +9 -1
  8. package/dist/types/models/config/IBaseSocketClientConfig.d.ts +4 -0
  9. package/dist/types/models/routes/IBaseRouteEntryPoint.d.ts +22 -0
  10. package/dist/types/models/routes/IRestRouteEntryPoint.d.ts +2 -19
  11. package/dist/types/models/routes/ISocketRoute.d.ts +6 -8
  12. package/dist/types/models/routes/ISocketRouteEntryPoint.d.ts +6 -0
  13. package/dist/types/models/server/IBaseRouteProcessor.d.ts +34 -0
  14. package/dist/types/models/server/IMimeTypeProcessor.d.ts +17 -0
  15. package/dist/types/models/server/IRestRouteProcessor.d.ts +22 -0
  16. package/dist/types/models/server/{IHttpRestRouteProcessor.d.ts → ISocketRouteProcessor.d.ts} +12 -15
  17. package/dist/types/models/server/IWebServer.d.ts +7 -3
  18. package/docs/changelog.md +43 -1
  19. package/docs/reference/classes/HttpErrorHelper.md +19 -9
  20. package/docs/reference/classes/HttpParameterHelper.md +39 -63
  21. package/docs/reference/index.md +10 -2
  22. package/docs/reference/interfaces/IBaseRouteEntryPoint.md +55 -0
  23. package/docs/reference/interfaces/IBaseRouteProcessor.md +106 -0
  24. package/docs/reference/interfaces/IBaseSocketClientConfig.md +8 -0
  25. package/docs/reference/interfaces/IHealthInfo.md +18 -0
  26. package/docs/reference/interfaces/IHttpRequest.md +3 -1
  27. package/docs/reference/interfaces/IHttpRequestContext.md +2 -2
  28. package/docs/reference/interfaces/IHttpRequestPathParams.md +1 -1
  29. package/docs/reference/interfaces/IHttpRequestQuery.md +1 -1
  30. package/docs/reference/interfaces/IHttpResponse.md +3 -1
  31. package/docs/reference/interfaces/IHttpServerRequest.md +3 -1
  32. package/docs/reference/interfaces/IInformationComponent.md +13 -5
  33. package/docs/reference/interfaces/IMimeTypeProcessor.md +43 -0
  34. package/docs/reference/interfaces/IRestRoute.md +30 -4
  35. package/docs/reference/interfaces/{IHttpRestRouteProcessor.md → IRestRouteProcessor.md} +55 -23
  36. package/docs/reference/interfaces/IRestRouteRequestExample.md +3 -1
  37. package/docs/reference/interfaces/IRestRouteResponseExample.md +3 -1
  38. package/docs/reference/interfaces/ISocketRoute.md +22 -10
  39. package/docs/reference/interfaces/ISocketRouteProcessor.md +217 -0
  40. package/docs/reference/interfaces/IWebServer.md +26 -6
  41. package/docs/reference/type-aliases/HealthStatus.md +1 -1
  42. package/docs/reference/type-aliases/IRestRouteEntryPoint.md +5 -0
  43. package/docs/reference/type-aliases/ISocketRouteEntryPoint.md +5 -0
  44. package/docs/reference/variables/MimeTypeProcessorFactory.md +5 -0
  45. package/docs/reference/variables/RestRouteProcessorFactory.md +5 -0
  46. package/docs/reference/variables/SocketRouteProcessorFactory.md +5 -0
  47. package/package.json +6 -7
  48. 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
@@ -88,71 +108,20 @@ class HttpParameterHelper {
88
108
  return values?.join(",");
89
109
  }
90
110
  /**
91
- * Convert the conditions string to a list of comparators.
92
- * @param conditions The conditions query string.
93
- * @returns The list of comparators.
111
+ * Convert object string to object.
112
+ * @param value The value query string.
113
+ * @returns The object.
94
114
  */
95
- static conditionsFromString(conditions) {
96
- const conditionParts = conditions?.split(",") ?? [];
97
- const conditionsList = [];
98
- for (const conditionPart of conditionParts) {
99
- const parts = conditionPart.split("|");
100
- if (parts.length === 3) {
101
- conditionsList.push({
102
- property: parts[0],
103
- comparison: parts[1],
104
- value: parts[2]
105
- });
106
- }
107
- }
108
- return conditionsList.length === 0 ? undefined : conditionsList;
115
+ static objectFromString(value) {
116
+ return core.Is.json(value) ? JSON.parse(value) : undefined;
109
117
  }
110
118
  /**
111
- * Convert the conditions to a string parameter.
112
- * @param conditions The conditions to convert.
113
- * @returns The string version of the comparators.
119
+ * Convert object to query string.
120
+ * @param value The value to convert to a string.
121
+ * @returns The converted object.
114
122
  */
115
- static conditionsToString(conditions) {
116
- if (core.Is.arrayValue(conditions)) {
117
- const conditionsList = [];
118
- for (const conditionPart of conditions) {
119
- conditionsList.push(`${conditionPart.property}|${conditionPart.comparison}|${conditionPart.value}`);
120
- }
121
- return conditionsList.join(",");
122
- }
123
- }
124
- /**
125
- * Convert the sort string to a list of sort properties.
126
- * @param sortProperties The sort properties query string.
127
- * @returns The list of sort properties.
128
- */
129
- static sortPropertiesFromString(sortProperties) {
130
- const sortParts = sortProperties?.split(",") ?? [];
131
- const sortPropertyList = [];
132
- for (const conditionPart of sortParts) {
133
- const parts = conditionPart.split("|");
134
- if (parts.length === 2) {
135
- sortPropertyList.push({
136
- property: parts[0],
137
- sortDirection: parts[1]
138
- });
139
- }
140
- }
141
- return sortPropertyList.length === 0 ? undefined : sortPropertyList;
142
- }
143
- /**
144
- * Convert the sort properties to a string parameter.
145
- * @param sortProperties The sort properties to convert.
146
- * @returns The string version of the sort properties.
147
- */
148
- static sortPropertiesToString(sortProperties) {
149
- if (core.Is.arrayValue(sortProperties)) {
150
- const sortPropertyList = [];
151
- for (const conditionPart of sortProperties) {
152
- sortPropertyList.push(`${conditionPart.property}|${conditionPart.sortDirection}`);
153
- }
154
- return sortPropertyList.join(",");
155
- }
123
+ static objectToString(value) {
124
+ return core.Is.empty(value) ? undefined : JSON.stringify(value);
156
125
  }
157
126
  }
158
127
 
@@ -180,3 +149,6 @@ const HealthStatus = {
180
149
  exports.HealthStatus = HealthStatus;
181
150
  exports.HttpErrorHelper = HttpErrorHelper;
182
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, 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
@@ -86,71 +106,20 @@ class HttpParameterHelper {
86
106
  return values?.join(",");
87
107
  }
88
108
  /**
89
- * Convert the conditions string to a list of comparators.
90
- * @param conditions The conditions query string.
91
- * @returns The list of comparators.
109
+ * Convert object string to object.
110
+ * @param value The value query string.
111
+ * @returns The object.
92
112
  */
93
- static conditionsFromString(conditions) {
94
- const conditionParts = conditions?.split(",") ?? [];
95
- const conditionsList = [];
96
- for (const conditionPart of conditionParts) {
97
- const parts = conditionPart.split("|");
98
- if (parts.length === 3) {
99
- conditionsList.push({
100
- property: parts[0],
101
- comparison: parts[1],
102
- value: parts[2]
103
- });
104
- }
105
- }
106
- return conditionsList.length === 0 ? undefined : conditionsList;
113
+ static objectFromString(value) {
114
+ return Is.json(value) ? JSON.parse(value) : undefined;
107
115
  }
108
116
  /**
109
- * Convert the conditions to a string parameter.
110
- * @param conditions The conditions to convert.
111
- * @returns The string version of the comparators.
117
+ * Convert object to query string.
118
+ * @param value The value to convert to a string.
119
+ * @returns The converted object.
112
120
  */
113
- static conditionsToString(conditions) {
114
- if (Is.arrayValue(conditions)) {
115
- const conditionsList = [];
116
- for (const conditionPart of conditions) {
117
- conditionsList.push(`${conditionPart.property}|${conditionPart.comparison}|${conditionPart.value}`);
118
- }
119
- return conditionsList.join(",");
120
- }
121
- }
122
- /**
123
- * Convert the sort string to a list of sort properties.
124
- * @param sortProperties The sort properties query string.
125
- * @returns The list of sort properties.
126
- */
127
- static sortPropertiesFromString(sortProperties) {
128
- const sortParts = sortProperties?.split(",") ?? [];
129
- const sortPropertyList = [];
130
- for (const conditionPart of sortParts) {
131
- const parts = conditionPart.split("|");
132
- if (parts.length === 2) {
133
- sortPropertyList.push({
134
- property: parts[0],
135
- sortDirection: parts[1]
136
- });
137
- }
138
- }
139
- return sortPropertyList.length === 0 ? undefined : sortPropertyList;
140
- }
141
- /**
142
- * Convert the sort properties to a string parameter.
143
- * @param sortProperties The sort properties to convert.
144
- * @returns The string version of the sort properties.
145
- */
146
- static sortPropertiesToString(sortProperties) {
147
- if (Is.arrayValue(sortProperties)) {
148
- const sortPropertyList = [];
149
- for (const conditionPart of sortProperties) {
150
- sortPropertyList.push(`${conditionPart.property}|${conditionPart.sortDirection}`);
151
- }
152
- return sortPropertyList.join(",");
153
- }
121
+ static objectToString(value) {
122
+ return Is.empty(value) ? undefined : JSON.stringify(value);
154
123
  }
155
124
  }
156
125
 
@@ -175,4 +144,4 @@ const HealthStatus = {
175
144
  Error: "error"
176
145
  };
177
146
 
178
- 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,4 +1,3 @@
1
- import type { IComparator, SortDirection } from "@twin.org/entity";
2
1
  /**
3
2
  * Class to help with handling http parameters.
4
3
  */
@@ -16,33 +15,15 @@ export declare class HttpParameterHelper {
16
15
  */
17
16
  static arrayToString<T = string>(values?: T[]): string | undefined;
18
17
  /**
19
- * Convert the conditions string to a list of comparators.
20
- * @param conditions The conditions query string.
21
- * @returns The list of comparators.
18
+ * Convert object string to object.
19
+ * @param value The value query string.
20
+ * @returns The object.
22
21
  */
23
- static conditionsFromString(conditions?: string): IComparator[] | undefined;
22
+ static objectFromString<T = unknown>(value?: string): T | undefined;
24
23
  /**
25
- * Convert the conditions to a string parameter.
26
- * @param conditions The conditions to convert.
27
- * @returns The string version of the comparators.
24
+ * Convert object to query string.
25
+ * @param value The value to convert to a string.
26
+ * @returns The converted object.
28
27
  */
29
- static conditionsToString(conditions?: IComparator[]): string | undefined;
30
- /**
31
- * Convert the sort string to a list of sort properties.
32
- * @param sortProperties The sort properties query string.
33
- * @returns The list of sort properties.
34
- */
35
- static sortPropertiesFromString<T = unknown>(sortProperties?: string): {
36
- property: keyof T;
37
- sortDirection: SortDirection;
38
- }[] | undefined;
39
- /**
40
- * Convert the sort properties to a string parameter.
41
- * @param sortProperties The sort properties to convert.
42
- * @returns The string version of the sort properties.
43
- */
44
- static sortPropertiesToString<T = unknown>(sortProperties?: {
45
- property: keyof T;
46
- sortDirection: SortDirection;
47
- }[]): string | undefined;
28
+ static objectToString<T = unknown>(value?: T): string | undefined;
48
29
  }
@@ -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
  }