@twin.org/api-models 0.0.2-next.9 → 0.0.3-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 (164) hide show
  1. package/dist/es/factories/authenticationGeneratorFactory.js +9 -0
  2. package/dist/es/factories/authenticationGeneratorFactory.js.map +1 -0
  3. package/dist/es/factories/mimeTypeProcessorFactory.js +9 -0
  4. package/dist/es/factories/mimeTypeProcessorFactory.js.map +1 -0
  5. package/dist/es/factories/restRouteProcessorFactory.js +9 -0
  6. package/dist/es/factories/restRouteProcessorFactory.js.map +1 -0
  7. package/dist/es/factories/socketRouteProcessorFactory.js +9 -0
  8. package/dist/es/factories/socketRouteProcessorFactory.js.map +1 -0
  9. package/dist/es/helpers/httpErrorHelper.js +60 -0
  10. package/dist/es/helpers/httpErrorHelper.js.map +1 -0
  11. package/dist/es/helpers/httpParameterHelper.js +41 -0
  12. package/dist/es/helpers/httpParameterHelper.js.map +1 -0
  13. package/dist/es/index.js +59 -0
  14. package/dist/es/index.js.map +1 -0
  15. package/dist/es/models/api/IServerFavIconResponse.js +2 -0
  16. package/dist/es/models/api/IServerFavIconResponse.js.map +1 -0
  17. package/dist/es/models/api/IServerHealthResponse.js +2 -0
  18. package/dist/es/models/api/IServerHealthResponse.js.map +1 -0
  19. package/dist/es/models/api/IServerInfoResponse.js +2 -0
  20. package/dist/es/models/api/IServerInfoResponse.js.map +1 -0
  21. package/dist/es/models/api/IServerRootResponse.js +4 -0
  22. package/dist/es/models/api/IServerRootResponse.js.map +1 -0
  23. package/dist/es/models/api/IServerSpecResponse.js +2 -0
  24. package/dist/es/models/api/IServerSpecResponse.js.map +1 -0
  25. package/dist/es/models/client/IAuthenticationGenerator.js +2 -0
  26. package/dist/es/models/client/IAuthenticationGenerator.js.map +1 -0
  27. package/dist/es/models/config/IBaseRestClientConfig.js +2 -0
  28. package/dist/es/models/config/IBaseRestClientConfig.js.map +1 -0
  29. package/dist/es/models/config/IBaseSocketClientConfig.js +2 -0
  30. package/dist/es/models/config/IBaseSocketClientConfig.js.map +1 -0
  31. package/dist/es/models/protocol/IHttpRequest.js +2 -0
  32. package/dist/es/models/protocol/IHttpRequest.js.map +1 -0
  33. package/dist/es/models/protocol/IHttpRequestContext.js +2 -0
  34. package/dist/es/models/protocol/IHttpRequestContext.js.map +1 -0
  35. package/dist/es/models/protocol/IHttpRequestPathParams.js +4 -0
  36. package/dist/es/models/protocol/IHttpRequestPathParams.js.map +1 -0
  37. package/dist/es/models/protocol/IHttpRequestQuery.js +4 -0
  38. package/dist/es/models/protocol/IHttpRequestQuery.js.map +1 -0
  39. package/dist/es/models/protocol/IHttpResponse.js +2 -0
  40. package/dist/es/models/protocol/IHttpResponse.js.map +1 -0
  41. package/dist/es/models/protocol/IHttpServerRequest.js +2 -0
  42. package/dist/es/models/protocol/IHttpServerRequest.js.map +1 -0
  43. package/dist/es/models/protocol/ISocketRequestContext.js +2 -0
  44. package/dist/es/models/protocol/ISocketRequestContext.js.map +1 -0
  45. package/dist/es/models/protocol/ISocketServerRequest.js +2 -0
  46. package/dist/es/models/protocol/ISocketServerRequest.js.map +1 -0
  47. package/dist/es/models/requests/INoContentRequest.js +2 -0
  48. package/dist/es/models/requests/INoContentRequest.js.map +1 -0
  49. package/dist/es/models/responses/errors/IBadRequestResponse.js +2 -0
  50. package/dist/es/models/responses/errors/IBadRequestResponse.js.map +1 -0
  51. package/dist/es/models/responses/errors/IConflictResponse.js +2 -0
  52. package/dist/es/models/responses/errors/IConflictResponse.js.map +1 -0
  53. package/dist/es/models/responses/errors/IForbiddenResponse.js +2 -0
  54. package/dist/es/models/responses/errors/IForbiddenResponse.js.map +1 -0
  55. package/dist/es/models/responses/errors/IInternalServerErrorResponse.js +2 -0
  56. package/dist/es/models/responses/errors/IInternalServerErrorResponse.js.map +1 -0
  57. package/dist/es/models/responses/errors/INotFoundResponse.js +2 -0
  58. package/dist/es/models/responses/errors/INotFoundResponse.js.map +1 -0
  59. package/dist/es/models/responses/errors/IUnauthorizedResponse.js +2 -0
  60. package/dist/es/models/responses/errors/IUnauthorizedResponse.js.map +1 -0
  61. package/dist/es/models/responses/errors/IUnprocessableEntityResponse.js +2 -0
  62. package/dist/es/models/responses/errors/IUnprocessableEntityResponse.js.map +1 -0
  63. package/dist/es/models/responses/success/IAcceptedResponse.js +2 -0
  64. package/dist/es/models/responses/success/IAcceptedResponse.js.map +1 -0
  65. package/dist/es/models/responses/success/ICreatedResponse.js +2 -0
  66. package/dist/es/models/responses/success/ICreatedResponse.js.map +1 -0
  67. package/dist/es/models/responses/success/INoContentResponse.js +2 -0
  68. package/dist/es/models/responses/success/INoContentResponse.js.map +1 -0
  69. package/dist/es/models/responses/success/IOkResponse.js +2 -0
  70. package/dist/es/models/responses/success/IOkResponse.js.map +1 -0
  71. package/dist/es/models/routes/IBaseRoute.js +4 -0
  72. package/dist/es/models/routes/IBaseRoute.js.map +1 -0
  73. package/dist/es/models/routes/IBaseRouteEntryPoint.js +2 -0
  74. package/dist/es/models/routes/IBaseRouteEntryPoint.js.map +1 -0
  75. package/dist/es/models/routes/IRestRoute.js +2 -0
  76. package/dist/es/models/routes/IRestRoute.js.map +1 -0
  77. package/dist/es/models/routes/IRestRouteEntryPoint.js +2 -0
  78. package/dist/es/models/routes/IRestRouteEntryPoint.js.map +1 -0
  79. package/dist/es/models/routes/IRestRouteExample.js +4 -0
  80. package/dist/es/models/routes/IRestRouteExample.js.map +1 -0
  81. package/dist/es/models/routes/IRestRouteRequestExample.js +2 -0
  82. package/dist/es/models/routes/IRestRouteRequestExample.js.map +1 -0
  83. package/dist/es/models/routes/IRestRouteResponseAttachmentOptions.js +4 -0
  84. package/dist/es/models/routes/IRestRouteResponseAttachmentOptions.js.map +1 -0
  85. package/dist/es/models/routes/IRestRouteResponseExample.js +2 -0
  86. package/dist/es/models/routes/IRestRouteResponseExample.js.map +1 -0
  87. package/dist/es/models/routes/IRestRouteResponseOptions.js +2 -0
  88. package/dist/es/models/routes/IRestRouteResponseOptions.js.map +1 -0
  89. package/dist/es/models/routes/ISocketRoute.js +2 -0
  90. package/dist/es/models/routes/ISocketRoute.js.map +1 -0
  91. package/dist/es/models/routes/ISocketRouteEntryPoint.js +2 -0
  92. package/dist/es/models/routes/ISocketRouteEntryPoint.js.map +1 -0
  93. package/dist/es/models/routes/ITag.js +4 -0
  94. package/dist/es/models/routes/ITag.js.map +1 -0
  95. package/dist/es/models/server/IBaseRouteProcessor.js +2 -0
  96. package/dist/es/models/server/IBaseRouteProcessor.js.map +1 -0
  97. package/dist/es/models/server/IMimeTypeProcessor.js +2 -0
  98. package/dist/es/models/server/IMimeTypeProcessor.js.map +1 -0
  99. package/dist/es/models/server/IRestRouteProcessor.js +2 -0
  100. package/dist/es/models/server/IRestRouteProcessor.js.map +1 -0
  101. package/dist/es/models/server/ISocketRouteProcessor.js +2 -0
  102. package/dist/es/models/server/ISocketRouteProcessor.js.map +1 -0
  103. package/dist/es/models/server/IWebServer.js +2 -0
  104. package/dist/es/models/server/IWebServer.js.map +1 -0
  105. package/dist/es/models/server/IWebServerOptions.js +2 -0
  106. package/dist/es/models/server/IWebServerOptions.js.map +1 -0
  107. package/dist/es/models/services/IHealthInfo.js +2 -0
  108. package/dist/es/models/services/IHealthInfo.js.map +1 -0
  109. package/dist/es/models/services/IInformationComponent.js +2 -0
  110. package/dist/es/models/services/IInformationComponent.js.map +1 -0
  111. package/dist/es/models/services/IServerInfo.js +4 -0
  112. package/dist/es/models/services/IServerInfo.js.map +1 -0
  113. package/dist/es/models/services/healthStatus.js +21 -0
  114. package/dist/es/models/services/healthStatus.js.map +1 -0
  115. package/dist/types/factories/authenticationGeneratorFactory.d.ts +6 -0
  116. package/dist/types/factories/mimeTypeProcessorFactory.d.ts +1 -1
  117. package/dist/types/factories/restRouteProcessorFactory.d.ts +1 -1
  118. package/dist/types/factories/socketRouteProcessorFactory.d.ts +1 -1
  119. package/dist/types/helpers/httpErrorHelper.d.ts +1 -1
  120. package/dist/types/index.d.ts +56 -55
  121. package/dist/types/models/api/IServerHealthResponse.d.ts +1 -1
  122. package/dist/types/models/api/IServerInfoResponse.d.ts +1 -1
  123. package/dist/types/models/client/IAuthenticationGenerator.d.ts +14 -0
  124. package/dist/types/models/protocol/IHttpRequest.d.ts +2 -2
  125. package/dist/types/models/protocol/IHttpRequestContext.d.ts +2 -3
  126. package/dist/types/models/protocol/IHttpServerRequest.d.ts +1 -1
  127. package/dist/types/models/protocol/ISocketRequestContext.d.ts +1 -1
  128. package/dist/types/models/protocol/ISocketServerRequest.d.ts +1 -1
  129. package/dist/types/models/requests/INoContentRequest.d.ts +1 -1
  130. package/dist/types/models/routes/IBaseRoute.d.ts +10 -0
  131. package/dist/types/models/routes/IBaseRouteEntryPoint.d.ts +1 -1
  132. package/dist/types/models/routes/IRestRoute.d.ts +7 -7
  133. package/dist/types/models/routes/IRestRouteEntryPoint.d.ts +2 -2
  134. package/dist/types/models/routes/IRestRouteRequestExample.d.ts +1 -1
  135. package/dist/types/models/routes/IRestRouteResponseExample.d.ts +1 -1
  136. package/dist/types/models/routes/IRestRouteResponseOptions.d.ts +1 -1
  137. package/dist/types/models/routes/ISocketRoute.d.ts +4 -4
  138. package/dist/types/models/routes/ISocketRouteEntryPoint.d.ts +2 -2
  139. package/dist/types/models/server/IBaseRouteProcessor.d.ts +15 -8
  140. package/dist/types/models/server/IRestRouteProcessor.d.ts +5 -7
  141. package/dist/types/models/server/ISocketRouteProcessor.d.ts +5 -7
  142. package/dist/types/models/server/IWebServer.d.ts +5 -5
  143. package/dist/types/models/services/IHealthInfo.d.ts +1 -1
  144. package/dist/types/models/services/IInformationComponent.d.ts +3 -3
  145. package/docs/changelog.md +47 -0
  146. package/docs/reference/classes/HttpParameterHelper.md +8 -8
  147. package/docs/reference/index.md +2 -1
  148. package/docs/reference/interfaces/IAuthenticationGenerator.md +35 -0
  149. package/docs/reference/interfaces/IBaseRoute.md +20 -0
  150. package/docs/reference/interfaces/IBaseRouteProcessor.md +26 -10
  151. package/docs/reference/interfaces/IHttpRequestContext.md +0 -28
  152. package/docs/reference/interfaces/IInformationComponent.md +2 -2
  153. package/docs/reference/interfaces/IRestRoute.md +28 -0
  154. package/docs/reference/interfaces/IRestRouteProcessor.md +32 -18
  155. package/docs/reference/interfaces/ISocketRequestContext.md +0 -24
  156. package/docs/reference/interfaces/ISocketRoute.md +28 -0
  157. package/docs/reference/interfaces/ISocketRouteProcessor.md +34 -20
  158. package/docs/reference/variables/AuthenticationGeneratorFactory.md +5 -0
  159. package/locales/en.json +1 -9
  160. package/package.json +23 -8
  161. package/dist/cjs/index.cjs +0 -154
  162. package/dist/esm/index.mjs +0 -147
  163. package/dist/types/models/protocol/IHttpRequestIdentity.d.ts +0 -13
  164. package/docs/reference/interfaces/IHttpRequestIdentity.md +0 -23
@@ -8,9 +8,29 @@ The definition for a processor for handling socket routes.
8
8
 
9
9
  ## Methods
10
10
 
11
+ ### features()?
12
+
13
+ > `optional` **features**(): `string`[]
14
+
15
+ Features supported by this processor.
16
+ If a route has any of these features listed, this processor will be run for that route.
17
+ If this is not implemented, the processor will run for all routes.
18
+
19
+ #### Returns
20
+
21
+ `string`[]
22
+
23
+ The features supported by this processor.
24
+
25
+ #### Inherited from
26
+
27
+ [`IBaseRouteProcessor`](IBaseRouteProcessor.md).[`features`](IBaseRouteProcessor.md#features)
28
+
29
+ ***
30
+
11
31
  ### pre()?
12
32
 
13
- > `optional` **pre**(`request`, `response`, `route`, `requestIdentity`, `processorState`, `loggingComponentType?`): `Promise`\<`void`\>
33
+ > `optional` **pre**(`request`, `response`, `route`, `contextIds`, `processorState`, `loggingComponentType?`): `Promise`\<`void`\>
14
34
 
15
35
  Pre process the REST request for the specified route.
16
36
 
@@ -32,13 +52,13 @@ The response data to send if any.
32
52
 
33
53
  The route being requested, if a matching one was found.
34
54
 
35
- `undefined` | [`ISocketRoute`](ISocketRoute.md)\<`any`, `any`\>
55
+ [`ISocketRoute`](ISocketRoute.md)\<`any`, `any`\> | `undefined`
36
56
 
37
- ##### requestIdentity
57
+ ##### contextIds
38
58
 
39
- [`IHttpRequestIdentity`](IHttpRequestIdentity.md)
59
+ `IContextIds`
40
60
 
41
- The identity context for the request.
61
+ The context IDs of the request.
42
62
 
43
63
  ##### processorState
44
64
 
@@ -64,7 +84,7 @@ Promise that resolves when the request is processed.
64
84
 
65
85
  ### post()?
66
86
 
67
- > `optional` **post**(`request`, `response`, `route`, `requestIdentity`, `processorState`, `loggingComponentType?`): `Promise`\<`void`\>
87
+ > `optional` **post**(`request`, `response`, `route`, `contextIds`, `processorState`, `loggingComponentType?`): `Promise`\<`void`\>
68
88
 
69
89
  Post process the REST request for the specified route.
70
90
 
@@ -86,13 +106,13 @@ The response data to send if any.
86
106
 
87
107
  The route being requested, if a matching one was found.
88
108
 
89
- `undefined` | [`ISocketRoute`](ISocketRoute.md)\<`any`, `any`\>
109
+ [`ISocketRoute`](ISocketRoute.md)\<`any`, `any`\> | `undefined`
90
110
 
91
- ##### requestIdentity
111
+ ##### contextIds
92
112
 
93
- [`IHttpRequestIdentity`](IHttpRequestIdentity.md)
113
+ `IContextIds`
94
114
 
95
- The identity context for the request.
115
+ The context IDs of the request.
96
116
 
97
117
  ##### processorState
98
118
 
@@ -134,7 +154,7 @@ The server request object containing the socket id and other parameters.
134
154
 
135
155
  The route being requested, if a matching one was found.
136
156
 
137
- `undefined` | [`ISocketRoute`](ISocketRoute.md)\<`any`, `any`\>
157
+ [`ISocketRoute`](ISocketRoute.md)\<`any`, `any`\> | `undefined`
138
158
 
139
159
  ##### loggingComponentType?
140
160
 
@@ -168,7 +188,7 @@ The server request object containing the socket id and other parameters.
168
188
 
169
189
  The route being requested, if a matching one was found.
170
190
 
171
- `undefined` | [`ISocketRoute`](ISocketRoute.md)\<`any`, `any`\>
191
+ [`ISocketRoute`](ISocketRoute.md)\<`any`, `any`\> | `undefined`
172
192
 
173
193
  ##### loggingComponentType?
174
194
 
@@ -186,7 +206,7 @@ Promise that resolves when the request is processed.
186
206
 
187
207
  ### process()?
188
208
 
189
- > `optional` **process**(`request`, `response`, `route`, `requestIdentity`, `processorState`, `responseEmitter`, `loggingComponentType?`): `Promise`\<`void`\>
209
+ > `optional` **process**(`request`, `response`, `route`, `processorState`, `responseEmitter`, `loggingComponentType?`): `Promise`\<`void`\>
190
210
 
191
211
  Process the REST request for the specified route.
192
212
 
@@ -208,13 +228,7 @@ The response data to send if any.
208
228
 
209
229
  The route being requested, if a matching one was found.
210
230
 
211
- `undefined` | [`ISocketRoute`](ISocketRoute.md)\<`any`, `any`\>
212
-
213
- ##### requestIdentity
214
-
215
- [`IHttpRequestIdentity`](IHttpRequestIdentity.md)
216
-
217
- The identity context for the request.
231
+ [`ISocketRoute`](ISocketRoute.md)\<`any`, `any`\> | `undefined`
218
232
 
219
233
  ##### processorState
220
234
 
@@ -0,0 +1,5 @@
1
+ # Variable: AuthenticationGeneratorFactory
2
+
3
+ > `const` **AuthenticationGeneratorFactory**: `Factory`\<[`IAuthenticationGenerator`](../interfaces/IAuthenticationGenerator.md)\>
4
+
5
+ Factory for creating implementation of authentication generator types.
package/locales/en.json CHANGED
@@ -1,9 +1 @@
1
- {
2
- "error": {
3
- "baseRestClient": {
4
- "missingRouteProp": "Missing route parameter in data \"{routeProp}\" for route \"{route}\"",
5
- "decodingFailed": "Decoding JSON failed for route \"{route}\"",
6
- "failureStatusText": "The request to the API failed: \"{statusText}\""
7
- }
8
- }
9
- }
1
+ {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/api-models",
3
- "version": "0.0.2-next.9",
3
+ "version": "0.0.3-next.1",
4
4
  "description": "Contains models and classes for use with APIs",
5
5
  "repository": {
6
6
  "type": "git",
@@ -14,25 +14,40 @@
14
14
  "node": ">=20.0.0"
15
15
  },
16
16
  "dependencies": {
17
+ "@twin.org/context": "next",
17
18
  "@twin.org/core": "next",
18
19
  "@twin.org/nameof": "next",
19
20
  "@twin.org/web": "next"
20
21
  },
21
- "main": "./dist/cjs/index.cjs",
22
- "module": "./dist/esm/index.mjs",
22
+ "main": "./dist/es/index.js",
23
23
  "types": "./dist/types/index.d.ts",
24
24
  "exports": {
25
25
  ".": {
26
26
  "types": "./dist/types/index.d.ts",
27
- "require": "./dist/cjs/index.cjs",
28
- "import": "./dist/esm/index.mjs"
27
+ "import": "./dist/es/index.js",
28
+ "default": "./dist/es/index.js"
29
29
  }
30
30
  },
31
31
  "files": [
32
- "dist/cjs",
33
- "dist/esm",
32
+ "dist/es",
34
33
  "dist/types",
35
34
  "locales",
36
35
  "docs"
37
- ]
36
+ ],
37
+ "keywords": [
38
+ "twin",
39
+ "trade",
40
+ "iota",
41
+ "framework",
42
+ "blockchain",
43
+ "api",
44
+ "models",
45
+ "types",
46
+ "interfaces",
47
+ "schemas"
48
+ ],
49
+ "bugs": {
50
+ "url": "git+https://github.com/twinfoundation/api/issues"
51
+ },
52
+ "homepage": "https://twindev.org"
38
53
  }
@@ -1,154 +0,0 @@
1
- 'use strict';
2
-
3
- var core = require('@twin.org/core');
4
- var web = require('@twin.org/web');
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
-
30
- // Copyright 2024 IOTA Stiftung.
31
- // SPDX-License-Identifier: Apache-2.0.
32
- /**
33
- * Class to help with processing http errors.
34
- */
35
- class HttpErrorHelper {
36
- /**
37
- * Process the errors from the routes.
38
- * @param err The error to process.
39
- * @param includeStack Should the stack be included in the error.
40
- * @returns The status code and additional error data.
41
- */
42
- static processError(err, includeStack) {
43
- const error = core.BaseError.fromError(err);
44
- // If the error or any of its sub errors are of the specific
45
- // types then set the http response code accordingly
46
- const flattened = core.BaseError.flatten(error);
47
- let httpStatusCode = web.HttpStatusCode.internalServerError;
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))) {
50
- httpStatusCode = web.HttpStatusCode.badRequest;
51
- }
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))) {
54
- httpStatusCode = web.HttpStatusCode.conflict;
55
- }
56
- else if (flattened.some(e => core.BaseError.isErrorName(e, core.NotFoundError.CLASS_NAME))) {
57
- httpStatusCode = web.HttpStatusCode.notFound;
58
- }
59
- else if (flattened.some(e => core.BaseError.isErrorName(e, core.UnauthorizedError.CLASS_NAME))) {
60
- httpStatusCode = web.HttpStatusCode.unauthorized;
61
- }
62
- else if (flattened.some(e => core.BaseError.isErrorName(e, core.NotImplementedError.CLASS_NAME))) {
63
- httpStatusCode = web.HttpStatusCode.forbidden;
64
- }
65
- else if (flattened.some(e => core.BaseError.isErrorName(e, core.UnprocessableError.CLASS_NAME))) {
66
- httpStatusCode = web.HttpStatusCode.unprocessableEntity;
67
- }
68
- const returnError = error.toJsonObject(includeStack);
69
- return {
70
- error: returnError,
71
- httpStatusCode
72
- };
73
- }
74
- /**
75
- * Build an error response.
76
- * @param response The response to build the error into.
77
- * @param error The error to build the response for.
78
- * @param statusCode The status code to use for the error.
79
- */
80
- static buildResponse(response, error, statusCode) {
81
- response.headers ??= {};
82
- response.headers[web.HeaderTypes.ContentType] = `${web.MimeTypes.Json}; charset=utf-8`;
83
- response.body = error;
84
- response.statusCode = statusCode;
85
- }
86
- }
87
-
88
- // Copyright 2024 IOTA Stiftung.
89
- // SPDX-License-Identifier: Apache-2.0.
90
- /**
91
- * Class to help with handling http parameters.
92
- */
93
- class HttpParameterHelper {
94
- /**
95
- * Convert list query to array.
96
- * @param values The values query string.
97
- * @returns The array of values.
98
- */
99
- static arrayFromString(values) {
100
- return values?.split(",");
101
- }
102
- /**
103
- * Convert array of values to query string.
104
- * @param values The values to combine string.
105
- * @returns The combined.
106
- */
107
- static arrayToString(values) {
108
- return values?.join(",");
109
- }
110
- /**
111
- * Convert object string to object.
112
- * @param value The value query string.
113
- * @returns The object.
114
- */
115
- static objectFromString(value) {
116
- return core.Is.json(value) ? JSON.parse(value) : undefined;
117
- }
118
- /**
119
- * Convert object to query string.
120
- * @param value The value to convert to a string.
121
- * @returns The converted object.
122
- */
123
- static objectToString(value) {
124
- return core.Is.empty(value) ? undefined : JSON.stringify(value);
125
- }
126
- }
127
-
128
- // Copyright 2024 IOTA Stiftung.
129
- // SPDX-License-Identifier: Apache-2.0.
130
- /**
131
- * The health status of the component.
132
- */
133
- // eslint-disable-next-line @typescript-eslint/naming-convention
134
- const HealthStatus = {
135
- /**
136
- * OK.
137
- */
138
- Ok: "ok",
139
- /**
140
- * Warning.
141
- */
142
- Warning: "warning",
143
- /**
144
- * Error.
145
- */
146
- Error: "error"
147
- };
148
-
149
- exports.HealthStatus = HealthStatus;
150
- exports.HttpErrorHelper = HttpErrorHelper;
151
- exports.HttpParameterHelper = HttpParameterHelper;
152
- exports.MimeTypeProcessorFactory = MimeTypeProcessorFactory;
153
- exports.RestRouteProcessorFactory = RestRouteProcessorFactory;
154
- exports.SocketRouteProcessorFactory = SocketRouteProcessorFactory;
@@ -1,147 +0,0 @@
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");
27
-
28
- // Copyright 2024 IOTA Stiftung.
29
- // SPDX-License-Identifier: Apache-2.0.
30
- /**
31
- * Class to help with processing http errors.
32
- */
33
- class HttpErrorHelper {
34
- /**
35
- * Process the errors from the routes.
36
- * @param err The error to process.
37
- * @param includeStack Should the stack be included in the error.
38
- * @returns The status code and additional error data.
39
- */
40
- static processError(err, includeStack) {
41
- const error = BaseError.fromError(err);
42
- // If the error or any of its sub errors are of the specific
43
- // types then set the http response code accordingly
44
- const flattened = BaseError.flatten(error);
45
- let httpStatusCode = HttpStatusCode.internalServerError;
46
- if (flattened.some(e => BaseError.isErrorName(e, GuardError.CLASS_NAME)) ||
47
- flattened.some(e => BaseError.isErrorName(e, ValidationError.CLASS_NAME))) {
48
- httpStatusCode = HttpStatusCode.badRequest;
49
- }
50
- else if (flattened.some(e => BaseError.isErrorName(e, ConflictError.CLASS_NAME)) ||
51
- flattened.some(e => BaseError.isErrorName(e, AlreadyExistsError.CLASS_NAME))) {
52
- httpStatusCode = HttpStatusCode.conflict;
53
- }
54
- else if (flattened.some(e => BaseError.isErrorName(e, NotFoundError.CLASS_NAME))) {
55
- httpStatusCode = HttpStatusCode.notFound;
56
- }
57
- else if (flattened.some(e => BaseError.isErrorName(e, UnauthorizedError.CLASS_NAME))) {
58
- httpStatusCode = HttpStatusCode.unauthorized;
59
- }
60
- else if (flattened.some(e => BaseError.isErrorName(e, NotImplementedError.CLASS_NAME))) {
61
- httpStatusCode = HttpStatusCode.forbidden;
62
- }
63
- else if (flattened.some(e => BaseError.isErrorName(e, UnprocessableError.CLASS_NAME))) {
64
- httpStatusCode = HttpStatusCode.unprocessableEntity;
65
- }
66
- const returnError = error.toJsonObject(includeStack);
67
- return {
68
- error: returnError,
69
- httpStatusCode
70
- };
71
- }
72
- /**
73
- * Build an error response.
74
- * @param response The response to build the error into.
75
- * @param error The error to build the response for.
76
- * @param statusCode The status code to use for the error.
77
- */
78
- static buildResponse(response, error, statusCode) {
79
- response.headers ??= {};
80
- response.headers[HeaderTypes.ContentType] = `${MimeTypes.Json}; charset=utf-8`;
81
- response.body = error;
82
- response.statusCode = statusCode;
83
- }
84
- }
85
-
86
- // Copyright 2024 IOTA Stiftung.
87
- // SPDX-License-Identifier: Apache-2.0.
88
- /**
89
- * Class to help with handling http parameters.
90
- */
91
- class HttpParameterHelper {
92
- /**
93
- * Convert list query to array.
94
- * @param values The values query string.
95
- * @returns The array of values.
96
- */
97
- static arrayFromString(values) {
98
- return values?.split(",");
99
- }
100
- /**
101
- * Convert array of values to query string.
102
- * @param values The values to combine string.
103
- * @returns The combined.
104
- */
105
- static arrayToString(values) {
106
- return values?.join(",");
107
- }
108
- /**
109
- * Convert object string to object.
110
- * @param value The value query string.
111
- * @returns The object.
112
- */
113
- static objectFromString(value) {
114
- return Is.json(value) ? JSON.parse(value) : undefined;
115
- }
116
- /**
117
- * Convert object to query string.
118
- * @param value The value to convert to a string.
119
- * @returns The converted object.
120
- */
121
- static objectToString(value) {
122
- return Is.empty(value) ? undefined : JSON.stringify(value);
123
- }
124
- }
125
-
126
- // Copyright 2024 IOTA Stiftung.
127
- // SPDX-License-Identifier: Apache-2.0.
128
- /**
129
- * The health status of the component.
130
- */
131
- // eslint-disable-next-line @typescript-eslint/naming-convention
132
- const HealthStatus = {
133
- /**
134
- * OK.
135
- */
136
- Ok: "ok",
137
- /**
138
- * Warning.
139
- */
140
- Warning: "warning",
141
- /**
142
- * Error.
143
- */
144
- Error: "error"
145
- };
146
-
147
- export { HealthStatus, HttpErrorHelper, HttpParameterHelper, MimeTypeProcessorFactory, RestRouteProcessorFactory, SocketRouteProcessorFactory };
@@ -1,13 +0,0 @@
1
- /**
2
- * Context data from the HTTP request identity.
3
- */
4
- export interface IHttpRequestIdentity {
5
- /**
6
- * The identity of the node the request is being performed on.
7
- */
8
- nodeIdentity?: string;
9
- /**
10
- * The identity of the requestor if there is an authenticated user.
11
- */
12
- userIdentity?: string;
13
- }
@@ -1,23 +0,0 @@
1
- # Interface: IHttpRequestIdentity
2
-
3
- Context data from the HTTP request identity.
4
-
5
- ## Extended by
6
-
7
- - [`IHttpRequestContext`](IHttpRequestContext.md)
8
-
9
- ## Properties
10
-
11
- ### nodeIdentity?
12
-
13
- > `optional` **nodeIdentity**: `string`
14
-
15
- The identity of the node the request is being performed on.
16
-
17
- ***
18
-
19
- ### userIdentity?
20
-
21
- > `optional` **userIdentity**: `string`
22
-
23
- The identity of the requestor if there is an authenticated user.