@innet/server 2.0.0-alpha.3 → 2.0.0-alpha.31

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 (310) hide show
  1. package/README.md +1380 -523
  2. package/handler/handler.d.ts +19 -13
  3. package/handler/handler.es6.js +17 -11
  4. package/handler/handler.js +17 -11
  5. package/hooks/index.d.ts +7 -0
  6. package/hooks/index.es6.js +7 -0
  7. package/hooks/index.js +7 -0
  8. package/hooks/useAction/useAction.es6.js +1 -1
  9. package/hooks/useAction/useAction.js +1 -1
  10. package/hooks/useApi/useApi.d.ts +1 -3
  11. package/hooks/useClientIp/index.d.ts +1 -0
  12. package/hooks/useClientIp/index.es6.js +1 -0
  13. package/hooks/useClientIp/index.js +9 -0
  14. package/hooks/useClientIp/useClientIp.d.ts +1 -0
  15. package/hooks/useClientIp/useClientIp.es6.js +9 -0
  16. package/hooks/useClientIp/useClientIp.js +13 -0
  17. package/hooks/useComponentName/useComponentName.d.ts +1 -1
  18. package/hooks/useEndpoint/useEndpoint.d.ts +1 -1
  19. package/hooks/useEndpoint/useEndpoint.es6.js +3 -1
  20. package/hooks/useEndpoint/useEndpoint.js +3 -1
  21. package/hooks/useIsServerHttps/index.d.ts +1 -0
  22. package/hooks/useIsServerHttps/index.es6.js +1 -0
  23. package/hooks/useIsServerHttps/index.js +10 -0
  24. package/hooks/useIsServerHttps/useIsServerHttps.d.ts +3 -0
  25. package/hooks/useIsServerHttps/useIsServerHttps.es6.js +14 -0
  26. package/hooks/useIsServerHttps/useIsServerHttps.js +19 -0
  27. package/hooks/useObjectSchemaContext/index.d.ts +1 -0
  28. package/hooks/useObjectSchemaContext/index.es6.js +1 -0
  29. package/hooks/useObjectSchemaContext/index.js +10 -0
  30. package/hooks/useObjectSchemaContext/useObjectSchemaContext.d.ts +3 -0
  31. package/hooks/useObjectSchemaContext/useObjectSchemaContext.es6.js +14 -0
  32. package/hooks/useObjectSchemaContext/useObjectSchemaContext.js +19 -0
  33. package/hooks/useRequest/index.es6.js +1 -1
  34. package/hooks/useRequest/index.js +0 -1
  35. package/hooks/useRequest/useRequest.d.ts +1 -4
  36. package/hooks/useRequest/useRequest.es6.js +4 -4
  37. package/hooks/useRequest/useRequest.js +3 -4
  38. package/hooks/useRequestHandler/index.d.ts +1 -0
  39. package/hooks/useRequestHandler/index.es6.js +1 -0
  40. package/hooks/useRequestHandler/index.js +10 -0
  41. package/hooks/useRequestHandler/useRequestHandler.d.ts +4 -0
  42. package/hooks/useRequestHandler/useRequestHandler.es6.js +14 -0
  43. package/hooks/useRequestHandler/useRequestHandler.js +19 -0
  44. package/hooks/useResponse/index.es6.js +1 -1
  45. package/hooks/useResponse/index.js +0 -1
  46. package/hooks/useResponse/useResponse.d.ts +1 -4
  47. package/hooks/useResponse/useResponse.es6.js +4 -4
  48. package/hooks/useResponse/useResponse.js +3 -4
  49. package/hooks/useSchemaType/useSchemaType.d.ts +3 -2
  50. package/hooks/useSchemaType/useSchemaType.es6.js +5 -6
  51. package/hooks/useSchemaType/useSchemaType.js +5 -6
  52. package/hooks/useServer/useServer.d.ts +2 -1
  53. package/hooks/useServer/useServer.es6.js +3 -1
  54. package/hooks/useServer/useServer.js +3 -1
  55. package/hooks/useServerPlugin/index.d.ts +1 -0
  56. package/hooks/useServerPlugin/index.es6.js +1 -0
  57. package/hooks/useServerPlugin/index.js +9 -0
  58. package/hooks/useServerPlugin/useServerPlugin.d.ts +2 -0
  59. package/hooks/useServerPlugin/useServerPlugin.es6.js +13 -0
  60. package/hooks/useServerPlugin/useServerPlugin.js +17 -0
  61. package/hooks/useServerPlugins/index.d.ts +1 -0
  62. package/hooks/useServerPlugins/index.es6.js +1 -0
  63. package/hooks/useServerPlugins/index.js +10 -0
  64. package/hooks/useServerPlugins/useServerPlugins.d.ts +4 -0
  65. package/hooks/useServerPlugins/useServerPlugins.es6.js +14 -0
  66. package/hooks/useServerPlugins/useServerPlugins.js +19 -0
  67. package/hooks/useServerPort/index.d.ts +1 -0
  68. package/hooks/useServerPort/index.es6.js +1 -0
  69. package/hooks/useServerPort/index.js +10 -0
  70. package/hooks/useServerPort/useServerPort.d.ts +3 -0
  71. package/hooks/useServerPort/useServerPort.es6.js +14 -0
  72. package/hooks/useServerPort/useServerPort.js +19 -0
  73. package/index.es6.js +18 -8
  74. package/index.js +37 -12
  75. package/package.json +2 -2
  76. package/plugins/main/api/api.d.ts +10 -8
  77. package/plugins/main/api/api.es6.js +53 -55
  78. package/plugins/main/api/api.js +53 -55
  79. package/plugins/main/contact/contact.d.ts +2 -2
  80. package/plugins/main/endpoint/endpoint.d.ts +15 -15
  81. package/plugins/main/endpoint/endpoint.es6.js +7 -4
  82. package/plugins/main/endpoint/endpoint.js +7 -4
  83. package/plugins/main/host/host.d.ts +5 -5
  84. package/plugins/main/index.d.ts +2 -2
  85. package/plugins/main/index.es6.js +2 -2
  86. package/plugins/main/index.js +2 -2
  87. package/plugins/main/license/license.d.ts +5 -5
  88. package/plugins/main/param/param.d.ts +11 -11
  89. package/plugins/main/param/param.es6.js +3 -3
  90. package/plugins/main/param/param.js +3 -3
  91. package/plugins/main/preset/index.d.ts +1 -0
  92. package/plugins/main/preset/index.es6.js +1 -0
  93. package/plugins/{utils/prod → main/preset}/index.js +2 -2
  94. package/plugins/main/preset/preset.d.ts +3 -0
  95. package/plugins/main/preset/preset.es6.js +13 -0
  96. package/plugins/{utils/dev/dev.js → main/preset/preset.js} +9 -6
  97. package/plugins/main/response/index.es6.js +1 -1
  98. package/plugins/main/response/index.js +1 -0
  99. package/plugins/main/response/response.d.ts +6 -2
  100. package/plugins/main/response/response.es6.js +21 -11
  101. package/plugins/main/response/response.js +21 -10
  102. package/plugins/main/return/index.d.ts +1 -0
  103. package/plugins/main/return/index.es6.js +1 -0
  104. package/plugins/main/return/index.js +9 -0
  105. package/plugins/main/return/return.d.ts +4 -0
  106. package/plugins/main/return/return.es6.js +10 -0
  107. package/plugins/main/return/return.js +14 -0
  108. package/plugins/main/server/server.d.ts +5 -5
  109. package/plugins/main/server/server.es6.js +38 -10
  110. package/plugins/main/server/server.js +38 -10
  111. package/plugins/main/tag/tag.d.ts +2 -2
  112. package/plugins/main/tag/tag.es6.js +5 -2
  113. package/plugins/main/tag/tag.js +5 -2
  114. package/plugins/main/variable/variable.d.ts +5 -5
  115. package/plugins/main/variable/variable.es6.js +3 -1
  116. package/plugins/main/variable/variable.js +3 -1
  117. package/plugins/request/cms/cms.es6.js +2 -2
  118. package/plugins/request/cms/cms.js +2 -2
  119. package/plugins/request/cookie/cookie.es6.js +3 -19
  120. package/plugins/request/cookie/cookie.js +3 -23
  121. package/plugins/request/error/error.d.ts +40 -40
  122. package/plugins/request/error/error.es6.js +45 -42
  123. package/plugins/request/error/error.js +45 -42
  124. package/plugins/request/file/file.es6.js +2 -3
  125. package/plugins/request/file/file.js +2 -3
  126. package/plugins/request/header/header.es6.js +1 -1
  127. package/plugins/request/header/header.js +1 -1
  128. package/plugins/request/proxy/proxy.d.ts +4 -1
  129. package/plugins/request/proxy/proxy.es6.js +5 -2
  130. package/plugins/request/proxy/proxy.js +5 -2
  131. package/plugins/request/redirect/redirect.d.ts +8 -7
  132. package/plugins/request/redirect/redirect.es6.js +7 -7
  133. package/plugins/request/redirect/redirect.js +7 -7
  134. package/plugins/request/success/success.d.ts +7 -6
  135. package/plugins/request/success/success.es6.js +21 -10
  136. package/plugins/request/success/success.js +21 -10
  137. package/plugins/schema/any/any.d.ts +5 -0
  138. package/plugins/schema/any/any.es6.js +27 -0
  139. package/plugins/schema/any/any.js +31 -0
  140. package/plugins/schema/binary/binary.d.ts +3 -3
  141. package/plugins/schema/boolean/boolean.es6.js +2 -4
  142. package/plugins/schema/boolean/boolean.js +1 -3
  143. package/plugins/schema/date/date.d.ts +1 -1
  144. package/plugins/schema/date/date.es6.js +3 -3
  145. package/plugins/schema/date/date.js +3 -3
  146. package/plugins/schema/field/field.d.ts +1 -0
  147. package/plugins/schema/field/field.es6.js +7 -6
  148. package/plugins/schema/field/field.js +6 -5
  149. package/plugins/schema/index.d.ts +1 -0
  150. package/plugins/schema/index.es6.js +1 -0
  151. package/plugins/schema/index.js +1 -0
  152. package/plugins/schema/integer/integer.d.ts +3 -3
  153. package/plugins/schema/integer/integer.es6.js +1 -1
  154. package/plugins/schema/integer/integer.js +1 -1
  155. package/plugins/schema/number/number.d.ts +2 -2
  156. package/plugins/schema/number/number.es6.js +1 -1
  157. package/plugins/schema/number/number.js +1 -1
  158. package/plugins/schema/object/object.es6.js +17 -4
  159. package/plugins/schema/object/object.js +17 -4
  160. package/plugins/schema/string/string.d.ts +2 -2
  161. package/plugins/schema/string/string.es6.js +44 -30
  162. package/plugins/schema/string/string.js +44 -30
  163. package/plugins/utils/blacklist/blacklist.d.ts +4 -0
  164. package/plugins/utils/blacklist/blacklist.es6.js +18 -0
  165. package/plugins/utils/blacklist/blacklist.js +22 -0
  166. package/plugins/utils/blacklist/index.d.ts +1 -0
  167. package/plugins/utils/blacklist/index.es6.js +1 -0
  168. package/plugins/utils/blacklist/index.js +9 -0
  169. package/plugins/utils/dts/dts.d.ts +3 -3
  170. package/plugins/utils/dts/dts.es6.js +6 -25
  171. package/plugins/utils/dts/dts.js +5 -29
  172. package/plugins/utils/env/env.d.ts +6 -0
  173. package/plugins/utils/env/env.es6.js +11 -0
  174. package/plugins/utils/{prod/prod.js → env/env.js} +4 -3
  175. package/plugins/utils/env/index.d.ts +1 -0
  176. package/plugins/utils/env/index.es6.js +1 -0
  177. package/plugins/utils/{dev → env}/index.js +2 -2
  178. package/plugins/utils/index.d.ts +4 -2
  179. package/plugins/utils/index.es6.js +4 -2
  180. package/plugins/utils/index.js +4 -2
  181. package/plugins/utils/protection/index.d.ts +1 -0
  182. package/plugins/utils/protection/index.es6.js +1 -0
  183. package/plugins/utils/protection/index.js +9 -0
  184. package/plugins/utils/protection/protection.d.ts +8 -0
  185. package/plugins/utils/protection/protection.es6.js +36 -0
  186. package/plugins/utils/protection/protection.js +40 -0
  187. package/plugins/utils/swagger/swagger.es6.js +10 -12
  188. package/plugins/utils/swagger/swagger.js +10 -12
  189. package/plugins/utils/whitelist/index.d.ts +1 -0
  190. package/plugins/utils/whitelist/index.es6.js +1 -0
  191. package/plugins/utils/whitelist/index.js +9 -0
  192. package/plugins/utils/whitelist/whitelist.d.ts +4 -0
  193. package/plugins/utils/whitelist/whitelist.es6.js +20 -0
  194. package/plugins/utils/whitelist/whitelist.js +24 -0
  195. package/types.d.ts +15 -23
  196. package/utils/FileData/Bin.d.ts +13 -13
  197. package/utils/FileData/Bin.es6.js +2 -2
  198. package/utils/FileData/Bin.js +2 -2
  199. package/utils/action/Action.d.ts +17 -14
  200. package/utils/action/Action.es6.js +102 -78
  201. package/utils/action/Action.js +102 -78
  202. package/utils/dateFormat/dateFormat.d.ts +1 -1
  203. package/utils/dateFormat/dateFormat.es6.js +1 -1
  204. package/utils/dateFormat/dateFormat.js +1 -1
  205. package/utils/generateTypes/generateTypes.d.ts +3 -0
  206. package/utils/generateTypes/generateTypes.es6.js +162 -0
  207. package/utils/generateTypes/generateTypes.js +167 -0
  208. package/utils/generateTypes/index.d.ts +1 -0
  209. package/utils/generateTypes/index.es6.js +1 -0
  210. package/utils/generateTypes/index.js +10 -0
  211. package/utils/getEndpoint/getEndpoint.es6.js +2 -2
  212. package/utils/getEndpoint/getEndpoint.js +2 -2
  213. package/utils/getOrAdd/getOrAdd.d.ts +1 -1
  214. package/utils/httpOnStart/httpOnStart.d.ts +1 -1
  215. package/utils/httpOnStart/httpOnStart.es6.js +1 -1
  216. package/utils/httpOnStart/httpOnStart.js +1 -1
  217. package/utils/index.d.ts +1 -0
  218. package/utils/index.es6.js +1 -0
  219. package/utils/index.js +1 -0
  220. package/utils/rules/binaryAccept/binaryAccept.es6.js +2 -2
  221. package/utils/rules/binaryAccept/binaryAccept.js +2 -2
  222. package/utils/rules/dateTo/dateTo.d.ts +1 -1
  223. package/utils/rules/helpers.d.ts +1 -1
  224. package/utils/rules/index.d.ts +0 -1
  225. package/utils/rules/index.es6.js +0 -1
  226. package/utils/rules/index.js +0 -1
  227. package/utils/rules/int/int.es6.js +2 -2
  228. package/utils/rules/int/int.js +2 -2
  229. package/utils/rules/max/max.d.ts +1 -1
  230. package/utils/rules/max/max.es6.js +3 -3
  231. package/utils/rules/max/max.js +3 -3
  232. package/utils/rules/maxBin/maxBin.es6.js +2 -2
  233. package/utils/rules/maxBin/maxBin.js +2 -2
  234. package/utils/rules/maxDate/maxDate.es6.js +1 -1
  235. package/utils/rules/maxDate/maxDate.js +1 -1
  236. package/utils/rules/maxLength/maxLength.es6.js +2 -2
  237. package/utils/rules/maxLength/maxLength.js +2 -2
  238. package/utils/rules/min/min.d.ts +1 -1
  239. package/utils/rules/min/min.es6.js +3 -3
  240. package/utils/rules/min/min.js +3 -3
  241. package/utils/rules/minBin/minBin.es6.js +2 -2
  242. package/utils/rules/minBin/minBin.js +2 -2
  243. package/utils/rules/minDate/minDate.es6.js +1 -1
  244. package/utils/rules/minDate/minDate.js +1 -1
  245. package/utils/rules/minLength/minLength.es6.js +2 -2
  246. package/utils/rules/minLength/minLength.js +2 -2
  247. package/utils/rules/objectOf/objectOf.d.ts +1 -1
  248. package/utils/rules/objectOf/objectOf.es6.js +15 -2
  249. package/utils/rules/objectOf/objectOf.js +15 -2
  250. package/utils/rules/pattern/pattern.d.ts +1 -1
  251. package/utils/rules/pattern/pattern.es6.js +2 -1
  252. package/utils/rules/pattern/pattern.js +2 -1
  253. package/example/app/App/App.d.ts +0 -1
  254. package/example/app/App/index.d.ts +0 -1
  255. package/example/index.d.ts +0 -1
  256. package/example/requests/index.d.ts +0 -1
  257. package/example/requests/partners/EditPartner/EditPartner.d.ts +0 -1
  258. package/example/requests/partners/EditPartner/index.d.ts +0 -1
  259. package/example/requests/partners/GetPartner/GetPartner.d.ts +0 -1
  260. package/example/requests/partners/GetPartner/index.d.ts +0 -1
  261. package/example/requests/partners/GetPartners/GetPartners.d.ts +0 -1
  262. package/example/requests/partners/GetPartners/index.d.ts +0 -1
  263. package/example/requests/partners/index.d.ts +0 -3
  264. package/example/schemas/address/AddressSchema/AddressSchema.d.ts +0 -1
  265. package/example/schemas/address/AddressSchema/index.d.ts +0 -1
  266. package/example/schemas/address/index.d.ts +0 -1
  267. package/example/schemas/app/ListSchema/ListSchema.d.ts +0 -1
  268. package/example/schemas/app/ListSchema/index.d.ts +0 -1
  269. package/example/schemas/app/index.d.ts +0 -1
  270. package/example/schemas/index.d.ts +0 -4
  271. package/example/schemas/location/LocationSchema/LocationSchema.d.ts +0 -1
  272. package/example/schemas/location/LocationSchema/index.d.ts +0 -1
  273. package/example/schemas/location/index.d.ts +0 -1
  274. package/example/schemas/partner/EditPartnerSchema/EditPartnerSchema.d.ts +0 -1
  275. package/example/schemas/partner/EditPartnerSchema/index.d.ts +0 -1
  276. package/example/schemas/partner/PartnerSchema/PartnerSchema.d.ts +0 -1
  277. package/example/schemas/partner/PartnerSchema/index.d.ts +0 -1
  278. package/example/schemas/partner/index.d.ts +0 -2
  279. package/example/tags/Partner/Partner.d.ts +0 -1
  280. package/example/tags/Partner/index.d.ts +0 -1
  281. package/example/tags/index.d.ts +0 -1
  282. package/openApi.test.d.ts +0 -1
  283. package/plugins/main/fallback/fallback.d.ts +0 -4
  284. package/plugins/main/fallback/fallback.es6.js +0 -19
  285. package/plugins/main/fallback/fallback.js +0 -23
  286. package/plugins/main/fallback/index.d.ts +0 -1
  287. package/plugins/main/fallback/index.es6.js +0 -1
  288. package/plugins/main/fallback/index.js +0 -9
  289. package/plugins/main/request/index.d.ts +0 -1
  290. package/plugins/main/request/index.es6.js +0 -1
  291. package/plugins/main/request/index.js +0 -9
  292. package/plugins/main/request/request.d.ts +0 -4
  293. package/plugins/main/request/request.es6.js +0 -21
  294. package/plugins/main/request/request.js +0 -25
  295. package/plugins/utils/dev/dev.d.ts +0 -4
  296. package/plugins/utils/dev/dev.es6.js +0 -10
  297. package/plugins/utils/dev/index.d.ts +0 -1
  298. package/plugins/utils/dev/index.es6.js +0 -1
  299. package/plugins/utils/prod/index.d.ts +0 -1
  300. package/plugins/utils/prod/index.es6.js +0 -1
  301. package/plugins/utils/prod/prod.d.ts +0 -4
  302. package/plugins/utils/prod/prod.es6.js +0 -10
  303. package/utils/decorators/once/once.test.d.ts +0 -1
  304. package/utils/parseSearch/parseSearch.test.d.ts +0 -1
  305. package/utils/rules/any/any.d.ts +0 -1
  306. package/utils/rules/any/any.es6.js +0 -5
  307. package/utils/rules/any/any.js +0 -9
  308. /package/{utils/rules → plugins/schema}/any/index.d.ts +0 -0
  309. /package/{utils/rules → plugins/schema}/any/index.es6.js +0 -0
  310. /package/{utils/rules → plugins/schema}/any/index.js +0 -0
package/README.md CHANGED
@@ -46,7 +46,7 @@ Customize
46
46
 
47
47
  [← back](#index)
48
48
 
49
- The simplest way to start working with `@innet/server`, it is `innetjs` usage.
49
+ The simplest way to start working with `@innet/server`, it is [innetjs](https://www.npmjs.com/package/innetjs) usage.
50
50
 
51
51
  ```shell
52
52
  npx innetjs init my-app -t api
@@ -77,6 +77,28 @@ innet(app, server)
77
77
 
78
78
  Here is a **Hello World** example:
79
79
 
80
+ *src/app.tsx*
81
+ ```typescript jsx
82
+ export default (
83
+ <server>
84
+ <return>
85
+ <success>
86
+ Hello World!
87
+ </success>
88
+ </return>
89
+ </server>
90
+ )
91
+ ```
92
+
93
+ *Use `npm start` to run this server.*
94
+
95
+ Open http://localhost
96
+ You will see the `Hello Word!` string.
97
+
98
+ ---
99
+
100
+ Here is the simplest [api](#api) example:
101
+
80
102
  *src/app.tsx*
81
103
  ```typescript jsx
82
104
  export default (
@@ -88,7 +110,7 @@ export default (
88
110
 
89
111
  *Use `npm start` to run this server.*
90
112
 
91
- Open http://localhost:80
113
+ Open http://localhost
92
114
  You will see a base Open API JSON structure.
93
115
 
94
116
  ```json
@@ -107,7 +129,9 @@ You will see a base Open API JSON structure.
107
129
  [← back](#index)
108
130
 
109
131
  [\<server>](#server)
110
- [\<api>](#api)
132
+ [\<api>](#api)
133
+ [\<return>](#return)
134
+ [\<preset>](#preset)
111
135
 
112
136
  ---
113
137
 
@@ -136,8 +160,8 @@ export default (
136
160
  ```
137
161
 
138
162
  - By default, it uses port `80` for `http` and port `442` for `https`.
139
- - You can use `PORT` environment variable to set it up on CI level.
140
- - [innetjs](https://www.npmjs.com/package/innetjs) allows you to use `PORT` in `.env` file of local environment.
163
+ - You can use `INNET_PORT` environment variable to set it up on CI level.
164
+ - [innetjs](https://www.npmjs.com/package/innetjs) allows you to use `INNET_PORT` in `.env` file of local environment.
141
165
 
142
166
  #### ssl
143
167
 
@@ -155,8 +179,8 @@ export default (
155
179
  )
156
180
  ```
157
181
 
158
- - You can use `SSL_KEY` and `SSL_CRT` environment variables to set it up on CI level.
159
- - [innetjs](https://www.npmjs.com/package/innetjs) allows you to use `SSL_KEY` and `SSL_CRT` in `.env` file.
182
+ - You can use `INNET_SSL_KEY` and `INNET_SSL_CRT` environment variables to set it up on CI level.
183
+ - [innetjs](https://www.npmjs.com/package/innetjs) allows you to use `INNET_SSL_KEY` and `INNET_SSL_CRT` in `.env` file.
160
184
  - You can add `localhost.key` and `localhost.crt` files in your project folder.
161
185
 
162
186
  #### onStart
@@ -206,12 +230,25 @@ export default (
206
230
  )
207
231
  ```
208
232
 
233
+ #### onClose
234
+
235
+ Use `onClose` to handle server close action.
236
+
237
+ *src/app.tsx*
238
+ ```typescript jsx
239
+ export default (
240
+ <server
241
+ onClose={() => console.log('Close')}
242
+ />
243
+ )
244
+ ```
245
+
209
246
  ### \<api>
210
247
 
211
248
  [← back](#main)
212
249
 
213
- `<api>` element MUST be placed in `<server>` element.
214
250
  This element defines a REST API on the server.
251
+ This element MUST be placed in [\<server>](#server) element.
215
252
 
216
253
  #### title
217
254
 
@@ -292,7 +329,7 @@ export default (
292
329
  )
293
330
  ```
294
331
 
295
- *default: 0.0.0*
332
+ *default: `INNET_API_VERSION` || `'0.0.0'`*
296
333
 
297
334
  #### prefix
298
335
 
@@ -308,6 +345,235 @@ export default (
308
345
  </server>
309
346
  )
310
347
  ```
348
+ *default: `INNET_API_PREFIX` || `''`*
349
+
350
+ #### include
351
+
352
+ A regular expression scopes the API.
353
+
354
+ *src/app.tsx*
355
+ ```typescript jsx
356
+ export default (
357
+ <server>
358
+ <api
359
+ include={/^\/(api|openapi)/}
360
+ />
361
+ </server>
362
+ )
363
+ ```
364
+
365
+ #### exclude
366
+
367
+ A regular expression does not scope the API.
368
+
369
+ *src/app.tsx*
370
+ ```typescript jsx
371
+ export default (
372
+ <server>
373
+ <api prefix='/api' />
374
+ <api prefix='/openapi' />
375
+ <api exclude={/^\/(api|openapi)/} />
376
+ </server>
377
+ )
378
+ ```
379
+
380
+ ### \<return>
381
+
382
+ [← back](#main)
383
+
384
+ This element MUST be placed in [\<server>](#server) element.
385
+ It defines a run-time call handler for parent element.
386
+
387
+ *src/app.tsx*
388
+ ```typescript jsx
389
+ export default (
390
+ <server>
391
+ <return>
392
+ <error status={404} />
393
+ </return>
394
+ </server>
395
+ )
396
+ ```
397
+ *Any request returns 404*
398
+
399
+ The code runs from top to bottom and from left to right.
400
+ You cannot use two [\<return>](#return) elements one by one,
401
+ the same as you cannot use two `return` one by one for a JS `function`.
402
+
403
+ *src/app.tsx*
404
+ ```typescript jsx
405
+ export default (
406
+ <server>
407
+ <return>
408
+ <error status={404} />
409
+ </return>
410
+ <return>
411
+ <success />
412
+ </return>
413
+ </server>
414
+ )
415
+ ```
416
+
417
+ like
418
+
419
+ ```javascript
420
+ function server () {
421
+ return 'error'
422
+ return 'success'
423
+ }
424
+ ```
425
+
426
+ *The second [\<return>](#return) will newer run.*
427
+
428
+ You can use [\<return>](#return) in some elements like you use `return` in `if` or `while`.
429
+
430
+ *src/app.tsx*
431
+ ```typescript jsx
432
+ export default (
433
+ <server>
434
+ <env is='dev'>
435
+ <return>
436
+ <error status={404} />
437
+ </return>
438
+ </env>
439
+ <return>
440
+ <success />
441
+ </return>
442
+ </server>
443
+ )
444
+ ```
445
+
446
+ like
447
+
448
+ ```javascript
449
+ function server () {
450
+ if (process.env.NODE_ENV === 'dev') {
451
+ return 'error'
452
+ }
453
+
454
+ return 'success'
455
+ }
456
+ ```
457
+
458
+ Place [\<return>](#return) in [\<api>](#api) to handle any unknown request in the [\<api>](#api).
459
+
460
+ *src/app.tsx*
461
+ ```typescript jsx
462
+ export default (
463
+ <server>
464
+ <api>
465
+ <return>
466
+ <error status={404} />
467
+ </return>
468
+ </api>
469
+ </server>
470
+ )
471
+ ```
472
+
473
+ Place [\<return>](#return) in [\<endpoint>](#endpoint) to handle the [\<endpoint>](#endpoint) request.
474
+
475
+ *src/app.tsx*
476
+ ```typescript jsx
477
+ export default (
478
+ <server>
479
+ <api>
480
+ <endpoint method='get' path='/my-endpoint'>
481
+ <return>
482
+ <success>
483
+ My Endpoint
484
+ </success>
485
+ </return>
486
+ </endpoint>
487
+ </api>
488
+ <return>
489
+ <success>
490
+ Any other request
491
+ </success>
492
+ </return>
493
+ </server>
494
+ )
495
+ ```
496
+
497
+ You can place a component inside [\<return>](#return).
498
+ The component will run when the request will be triggered.
499
+
500
+ *src/app.tsx*
501
+ ```typescript jsx
502
+ import { GetPartners } from './GetPartners'
503
+
504
+ export default (
505
+ <server>
506
+ <api>
507
+ <endpoint method='get' path='/partners'>
508
+ <return>
509
+ <GetPartners />
510
+ </return>
511
+ </endpoint>
512
+ </api>
513
+ </server>
514
+ )
515
+ ```
516
+
517
+ *src/GetPartners.tsx*
518
+ ```typescript jsx
519
+ export const GetPartners = () => (
520
+ <success>
521
+ {{partners: []}}
522
+ </success>
523
+ )
524
+ ```
525
+
526
+ ### \<preset>
527
+
528
+ [← back](#main)
529
+
530
+ `<preset>` element MUST be placed in `<server>` element.
531
+ This element adds handling of each request.
532
+ It works the same as [\<return>](#return), but do not interrupt the running.
533
+ You can use it to add a `header` or `cookie` or some setup of a parent element.
534
+
535
+ *src/app.tsx*
536
+ ```typescript jsx
537
+ export default (
538
+ <server>
539
+ <preset>
540
+ <header
541
+ key='Test'
542
+ value='Ok'
543
+ />
544
+ </preset>
545
+ </server>
546
+ )
547
+ ```
548
+
549
+ Place the element inside [\<api>](#api) to preset it on the api requests scope.
550
+
551
+ *src/app.tsx*
552
+ ```typescript jsx
553
+ export default (
554
+ <server>
555
+ <api prefix='/api'>
556
+ <preset>
557
+ <header
558
+ key='Cache-Control'
559
+ value='no-cache, no-store, must-revalidate'
560
+ />
561
+ </preset>
562
+ ...Endpoints
563
+ <return>
564
+ <success>
565
+ Header contains `Cache-Control`
566
+ </success>
567
+ </return>
568
+ </api>
569
+ <return>
570
+ <success>
571
+ Header do not contain `Cache-Control`
572
+ </success>
573
+ </return>
574
+ </server>
575
+ )
576
+ ```
311
577
 
312
578
  ## Utils
313
579
 
@@ -316,8 +582,11 @@ This section contains elements of utils.
316
582
  [← back](#index)
317
583
 
318
584
  [\<swagger>](#swagger)
319
- [\<dev>](#dev)
320
- [\<dts>](#dts)
585
+ [\<env>](#env)
586
+ [\<dts>](#dts)
587
+ [\<blacklist>](#blacklist)
588
+ [\<whitelist>](#whitelist)
589
+ [\<protection>](#protection)
321
590
 
322
591
  ---
323
592
 
@@ -355,20 +624,48 @@ export default (
355
624
  )
356
625
  ```
357
626
 
358
- ### \<dev>
627
+ ### \<env>
359
628
 
360
629
  [← back](#utils)
361
630
 
362
- Everything inside <dev> will work when `NODE_ENV` equals `development`.
631
+ This element helps to control content by `process.env`.
632
+
633
+ There are a required field of `is`.
634
+ If it's a `string` then an environment variable must be equal to the `string`.
635
+ If it's an `array of string` then an environment variable must be included into the `array of string`.
363
636
 
364
637
  *src/app.tsx*
365
638
  ```typescript jsx
366
639
  export default (
367
640
  <server>
368
641
  <api>
369
- <dev>
642
+ <env is='dev'>
370
643
  <swagger />
371
- </dev>
644
+ </env>
645
+ </api>
646
+ </server>
647
+ )
648
+ ```
649
+
650
+ *The `<swagger />` will work only if `NODE_ENV` equals `dev`*
651
+
652
+ #### of
653
+
654
+ By default `of` equals `NODE_ENV`. You can check eny other environment variable.
655
+
656
+ *src/app.tsx*
657
+ ```typescript jsx
658
+ export default (
659
+ <server>
660
+ <api>
661
+ <env
662
+ of='PORT'
663
+ is={[
664
+ '3000',
665
+ '8080',
666
+ ]}>
667
+ <swagger />
668
+ </env>
372
669
  </api>
373
670
  </server>
374
671
  )
@@ -381,37 +678,303 @@ export default (
381
678
  Use `<dts>` element to add types generation.
382
679
  `<dts>` element MUST be placed in `<api>` element.
383
680
 
384
- `<dts>` has a required prop of `path`. This is a path of api TypeScript types file, `<dts>` generates it.
385
-
386
681
  *src/app.tsx*
387
682
  ```typescript jsx
388
683
  export default (
389
684
  <server>
390
685
  <api>
391
- <dev>
392
- <dts path='src/api.d.ts' />
393
- </dev>
686
+ <dts />
394
687
  </api>
395
688
  </server>
396
689
  )
397
690
  ```
398
691
 
399
- > You MUST add some [endpoint](#endpoint) with some schema otherwise you get the `Error: There is no schema in the input contents`.
400
-
692
+ You do not need to import types, use `Api` namespace everywhere.
401
693
  Here is an example of generated types usage.
402
694
 
403
- *src/GetPartner.tsx*
404
695
  ```typescript jsx
405
696
  import { useParams } from '@innet/server'
406
697
 
407
- export function GetPartner () {
408
- const { id } = useParams<Paths.Partners$Id.Get.PathParameters>()
409
- return <success>{{ id }}</success>
698
+ import { todos } from '../todos'
699
+
700
+ export function DeleteTodo () {
701
+ const { todoId } = useParams<Api.Endpoints['DELETE:/todos/{todoId}']['Params']>()
702
+
703
+ const todoIndex = todos.findIndex(({ id }) => id === todoId)
704
+
705
+ if (todoIndex === -1) {
706
+ return <error code='todoNotFound' status={404} />
707
+ }
708
+
709
+ todos.splice(todoIndex, 1)
710
+
711
+ return <success />
410
712
  }
411
713
  ```
412
714
 
413
- You do not need to import types, they generate as namespaces.
414
-
715
+ #### path
716
+
717
+ This is a path of api TypeScript types file, `<dts>` generates it.
718
+ `'src/api.d.ts'` by default.
719
+
720
+ *src/app.tsx*
721
+ ```typescript jsx
722
+ export default (
723
+ <server>
724
+ <api>
725
+ <dts path='src/types.d.ts' />
726
+ </api>
727
+ </server>
728
+ )
729
+ ```
730
+
731
+ #### namespace
732
+
733
+ This prop changes namespace for generated types. `'Api'` by default.
734
+
735
+ *src/app.tsx*
736
+ ```typescript jsx
737
+ export default (
738
+ <server>
739
+ <api>
740
+ <dts namespace='API' />
741
+ </api>
742
+ </server>
743
+ )
744
+ ```
745
+
746
+ ### \<blacklist>
747
+
748
+ This element MUST be placed in `<api>` element.
749
+
750
+ [← back](#utils)
751
+
752
+ This element returns own content for a user from IPs list.
753
+
754
+ *src/app.tsx*
755
+ ```typescript jsx
756
+ export default (
757
+ <server>
758
+ <api>
759
+ <blacklist>
760
+ <error />
761
+ </blacklist>
762
+ </api>
763
+ </server>
764
+ )
765
+ ```
766
+
767
+ #### ip
768
+
769
+ `ip` prop sets black IPs. By default, it equals `INNET_BLACKLIST_IP` node environment variable.
770
+
771
+ You can split IPs by `,` char.
772
+
773
+ *src/app.tsx*
774
+ ```typescript jsx
775
+ export default (
776
+ <server>
777
+ <api>
778
+ <blacklist
779
+ ip='0.1.2.3,3.2.1.0'>
780
+ <error />
781
+ </blacklist>
782
+ </api>
783
+ </server>
784
+ )
785
+ ```
786
+
787
+ ### \<whitelist>
788
+
789
+ This element MUST be placed in `<api>` element.
790
+
791
+ [← back](#utils)
792
+
793
+ This element returns own content for a user IP, which is not in a list.
794
+
795
+ *src/app.tsx*
796
+ ```typescript jsx
797
+ export default (
798
+ <server>
799
+ <api>
800
+ <whitelist>
801
+ <error />
802
+ </whitelist>
803
+ </api>
804
+ </server>
805
+ )
806
+ ```
807
+
808
+ #### ip
809
+
810
+ `ip` prop sets white IPs. By default, it equals `INNET_WHITELIST_IP` node environment variable.
811
+
812
+ You can split IPs by `,` char.
813
+
814
+ *src/app.tsx*
815
+ ```typescript jsx
816
+ export default (
817
+ <server>
818
+ <api>
819
+ <whitelist
820
+ ip='0.1.2.3,3.2.1.0'>
821
+ <error />
822
+ </whitelist>
823
+ </api>
824
+ </server>
825
+ )
826
+ ```
827
+
828
+ ### \<protection>
829
+
830
+ This element MUST be placed in `<api>` element.
831
+
832
+ [← back](#utils)
833
+
834
+ This element adds protection page.
835
+ You can use it when you want to protect your application.
836
+
837
+ If protection failed content of the element should be used.
838
+
839
+ *src/app.tsx*
840
+ ```typescript jsx
841
+ export default (
842
+ <server>
843
+ <api>
844
+ <protection>
845
+ <error
846
+ code='protection'
847
+ status='forbidden'
848
+ />
849
+ </protection>
850
+ </api>
851
+ </server>
852
+ )
853
+ ```
854
+
855
+ #### value
856
+
857
+ This prop is a secret string of protection value.
858
+ User must provide a protection query param equals the `value`.
859
+
860
+ By default, the value is `undefined` and protection does not work.
861
+ You can use `PROTECTION` env to set default protection `value`.
862
+
863
+ *src/app.tsx*
864
+ ```typescript jsx
865
+ export default (
866
+ <server>
867
+ <api>
868
+ <protection value='secret'>
869
+ <error
870
+ code='protection'
871
+ status='forbidden'
872
+ />
873
+ </protection>
874
+ </api>
875
+ </server>
876
+ )
877
+ ```
878
+
879
+ #### maxAge
880
+
881
+ This prop sets how much time protection is qualified.
882
+
883
+ By default, the prop equals a year.
884
+ You can use `INNET_PROTECTION_MAX_AGE` env to set default `maxAge`.
885
+
886
+ *src/app.tsx*
887
+ ```typescript jsx
888
+ export default (
889
+ <server>
890
+ <api>
891
+ <protection
892
+ maxAge={24 * 60 * 60}
893
+ value='secret'>
894
+ <error
895
+ code='protection'
896
+ status='forbidden'
897
+ />
898
+ </protection>
899
+ </api>
900
+ </server>
901
+ )
902
+ ```
903
+
904
+ #### excludeIp
905
+
906
+ This prop sets a list of IP addresses (split by `,`) to ignore the protection.
907
+
908
+ You can use `INNET_PROTECTED_IP` env to set default `excludeIp`.
909
+
910
+ *src/app.tsx*
911
+ ```typescript jsx
912
+ export default (
913
+ <server>
914
+ <api>
915
+ <protection
916
+ excludeIp='0.0.0.0,127.0.0.0'
917
+ value='secret'>
918
+ <error
919
+ code='protection'
920
+ status='forbidden'
921
+ />
922
+ </protection>
923
+ </api>
924
+ </server>
925
+ )
926
+ ```
927
+
928
+ #### cookieKey
929
+
930
+ This prop sets a cookie field name used to store protection of a user.
931
+
932
+ By default, it equals `protection`.
933
+ You can use `INNET_PROTECTION_COOKIE_KEY` env to set default `cookieKey`.
934
+
935
+ *src/app.tsx*
936
+ ```typescript jsx
937
+ export default (
938
+ <server>
939
+ <api>
940
+ <protection
941
+ cookieKey='secret'
942
+ value='secret'>
943
+ <error
944
+ code='protection'
945
+ status='forbidden'
946
+ />
947
+ </protection>
948
+ </api>
949
+ </server>
950
+ )
951
+ ```
952
+
953
+ #### searchKey
954
+
955
+ This prop sets a search query field name used to check protection.
956
+
957
+ By default, it equals `protection`.
958
+ You can use `INNET_PROTECTION_SEARCH_KEY` env to set default `searchKey`.
959
+
960
+ *src/app.tsx*
961
+ ```typescript jsx
962
+ export default (
963
+ <server>
964
+ <api>
965
+ <protection
966
+ searchKey='secret'
967
+ value='secret'>
968
+ <error
969
+ code='protection'
970
+ status='forbidden'
971
+ />
972
+ </protection>
973
+ </api>
974
+ </server>
975
+ )
976
+ ```
977
+
415
978
  ## API Info
416
979
 
417
980
  The API information elements are here.
@@ -815,216 +1378,475 @@ export default (
815
1378
 
816
1379
  #### private
817
1380
 
818
- Declares this operation to make an endpoint private.
819
- That means the endpoint should not be described and will not be shown in the Open API documentation.
1381
+ Declares this operation to make an endpoint private.
1382
+ That means the endpoint should not be described and will not be shown in the Open API documentation.
1383
+
1384
+ *src/app.tsx*
1385
+ ```typescript jsx
1386
+ export default (
1387
+ <server>
1388
+ <api>
1389
+ <endpoint
1390
+ method='get'
1391
+ path='/users'
1392
+ private
1393
+ />
1394
+ </api>
1395
+ </server>
1396
+ )
1397
+ ```
1398
+
1399
+ ### \<tag>
1400
+
1401
+ [← back](#endpoints)
1402
+
1403
+ You can wrap endpoints by `<tag>` element to group the endpoints.
1404
+ You can see the changes in Swagger UI.
1405
+
1406
+ *src/app.tsx*
1407
+ ```typescript jsx
1408
+ export default (
1409
+ <server>
1410
+ <api>
1411
+ <tag name='user'>
1412
+ <endpoint
1413
+ method='get'
1414
+ path='/users'
1415
+ />
1416
+ <endpoint
1417
+ method='post'
1418
+ path='/users'
1419
+ />
1420
+ </tag>
1421
+ </api>
1422
+ </server>
1423
+ )
1424
+ ```
1425
+
1426
+ ### \<param>
1427
+
1428
+ [← back](#endpoints)
1429
+
1430
+ Describes a single operation parameter.
1431
+
1432
+ A unique parameter is defined by a combination of a `name` and location.
1433
+
1434
+ ##### Parameter Locations
1435
+
1436
+ There are four possible parameter locations specified by the `in` prop:
1437
+
1438
+ - **path** - Used together with [Path Templating](https://swagger.io/specification/#path-templating), where the parameter value is actually part of the operation's URL.
1439
+ This does not include the host or base path of the API. For example, in `/items/{itemId}`, the path parameter is `itemId`.
1440
+ - **query** - Parameters that are appended to the URL. For example, in `/items?id=###`, the query parameter is `id`.
1441
+ - **header** - Custom headers that are expected as part of the request. Note that RFC7230 states header names are case insensitive.
1442
+ - **cookie** - Used to pass a specific cookie value to the API.
1443
+
1444
+ #### in
1445
+
1446
+ The location of the parameter.
1447
+ Possible values are `"query"`, `"header"`, `"path"` or `"cookie"`.
1448
+
1449
+ #### name
1450
+
1451
+ The name of the parameter. Parameter names are *case sensitive*.
1452
+
1453
+ - If `in` is "path", the `name` field MUST correspond to a template expression occurring within the `path` field in the `endpoint`. See [Path Templating](https://swagger.io/specification/#path-templating) for further information.
1454
+ - If `in` is "header" and the `name` field is "Accept", "Content-Type" or "Authorization", the parameter definition SHALL be ignored.
1455
+ - For all other cases, the `name` corresponds to the parameter name used by the `in` property.
1456
+
1457
+ *src/app.tsx*
1458
+ ```typescript jsx
1459
+ export default (
1460
+ <server>
1461
+ <api>
1462
+ <endpoint method='get' path='/users/{userId}'>
1463
+ <param in='path' name='userId' />
1464
+ </endpoint>
1465
+ </api>
1466
+ </server>
1467
+ )
1468
+ ```
1469
+
1470
+ #### description
1471
+
1472
+ A brief description of the parameter.
1473
+ This could contain examples of use.
1474
+ [CommonMark syntax](https://spec.commonmark.org) MAY be used for rich text representation.
1475
+
1476
+ *src/app.tsx*
1477
+ ```typescript jsx
1478
+ export default (
1479
+ <server>
1480
+ <api>
1481
+ <endpoint method='get' path='/users/{userId}'>
1482
+ <param
1483
+ in='path'
1484
+ name='userId'
1485
+ description='User identification number'
1486
+ />
1487
+ </endpoint>
1488
+ </api>
1489
+ </server>
1490
+ )
1491
+ ```
1492
+
1493
+ #### required
1494
+
1495
+ Determines whether this parameter is mandatory.
1496
+ If the parameter location is "path", this property is `true` and its value MUST be `true`.
1497
+ Otherwise, the property MAY be included and its default value is `false`.
1498
+
1499
+ *src/app.tsx*
1500
+ ```typescript jsx
1501
+ export default (
1502
+ <server>
1503
+ <api>
1504
+ <endpoint method='get' path='/users'>
1505
+ <param
1506
+ in='cookie'
1507
+ name='token'
1508
+ required
1509
+ />
1510
+ </endpoint>
1511
+ </api>
1512
+ </server>
1513
+ )
1514
+ ```
1515
+
1516
+ #### deprecated
1517
+
1518
+ Specifies that a parameter is deprecated and SHOULD be transitioned out of usage.
1519
+ Default value is `false`.
1520
+
1521
+ *src/app.tsx*
1522
+ ```typescript jsx
1523
+ export default (
1524
+ <server>
1525
+ <api>
1526
+ <endpoint method='get' path='/users'>
1527
+ <param
1528
+ in='query'
1529
+ name='status'
1530
+ deprecated
1531
+ />
1532
+ </endpoint>
1533
+ </api>
1534
+ </server>
1535
+ )
1536
+ ```
1537
+
1538
+ ### \<body>
1539
+
1540
+ [← back](#endpoints)
1541
+
1542
+ This element MUST be placed inside `<endpoint>`.
1543
+ It defines request body for the endpoint.
1544
+ `@innet/server` formats and validate the value automatically (real-time).
1545
+
1546
+ *src/app.tsx*
1547
+ ```typescript jsx
1548
+ return (
1549
+ <server>
1550
+ <api>
1551
+ <endpoint method='post' path='/partners'>
1552
+ <body>
1553
+ <object>
1554
+ <field key='name'>
1555
+ <string example='CANTent.' />
1556
+ </field>
1557
+ <field key='gift'>
1558
+ <boolean />
1559
+ </field>
1560
+ <field optional key='addresses'>
1561
+ <array>
1562
+ <number description='address id' />
1563
+ </array>
1564
+ </field>
1565
+ </object>
1566
+ </body>
1567
+ </endpoint>
1568
+ </api>
1569
+ </server>
1570
+ )
1571
+ ```
1572
+
1573
+ ### \<response>
1574
+
1575
+ [← back](#endpoints)
1576
+
1577
+ This element MUST be placed inside `<endpoint>`.
1578
+ It defines response body for the endpoint.
1579
+
1580
+ *src/app.tsx*
1581
+ ```typescript jsx
1582
+ return (
1583
+ <server>
1584
+ <api>
1585
+ <endpoint method='get' path='/settings'>
1586
+ <response>
1587
+ <object />
1588
+ </response>
1589
+ </endpoint>
1590
+ </api>
1591
+ </server>
1592
+ )
1593
+ ```
1594
+
1595
+ #### status
1596
+ A status of the `<response>`.
1597
+ Any [HTTP status code](https://swagger.io/specification/#http-codes) can be used as a number of the property.
1598
+
1599
+ By default, `status` equals `'default'`.
1600
+
1601
+ *src/app.tsx*
1602
+ ```typescript jsx
1603
+ return (
1604
+ <server>
1605
+ <api>
1606
+ <endpoint method='get' path='/settings'>
1607
+ <response status={200}>
1608
+ <object />
1609
+ </response>
1610
+ </endpoint>
1611
+ </api>
1612
+ </server>
1613
+ )
1614
+ ```
1615
+
1616
+ To define a range of response codes, this field MAY contain the uppercase wildcard character `X`.
1617
+ For example, `2XX` represents all response codes between \[200-299].
1618
+ Only the following range definitions are allowed: `1XX`, `2XX`, `3XX`, `4XX` and `5XX`.
1619
+
1620
+ *src/app.tsx*
1621
+ ```typescript jsx
1622
+ return (
1623
+ <server>
1624
+ <api>
1625
+ <endpoint method='get' path='/settings'>
1626
+ <response status='2XX'>
1627
+ <object />
1628
+ </response>
1629
+ </endpoint>
1630
+ </api>
1631
+ </server>
1632
+ )
1633
+ ```
1634
+
1635
+ Many number statuses have a string id you can use on the property.
1636
+
1637
+ *src/app.tsx*
1638
+ ```typescript jsx
1639
+ return (
1640
+ <server>
1641
+ <api>
1642
+ <endpoint method='get' path='/settings'>
1643
+ <response status='notFound'>
1644
+ <object />
1645
+ </response>
1646
+ </endpoint>
1647
+ </api>
1648
+ </server>
1649
+ )
1650
+ ```
1651
+
1652
+ You can use many `<response>` elements in an endpoint.
820
1653
 
821
1654
  *src/app.tsx*
822
1655
  ```typescript jsx
823
- export default (
1656
+ return (
824
1657
  <server>
825
1658
  <api>
826
- <endpoint
827
- method='get'
828
- path='/users'
829
- private
830
- />
1659
+ <endpoint method='get' path='/settings'>
1660
+ <response status='2XX'>
1661
+ <object />
1662
+ </response>
1663
+ <response status='4XX'>
1664
+ <object>
1665
+ <field key='error'>
1666
+ <string />
1667
+ </field>
1668
+ <field optional key='data'>
1669
+ <object />
1670
+ </field>
1671
+ </object>
1672
+ </response>
1673
+ </endpoint>
831
1674
  </api>
832
1675
  </server>
833
1676
  )
834
1677
  ```
835
1678
 
836
- ### \<tag>
837
-
838
- [← back](#endpoints)
1679
+ #### type
1680
+ A media type of the `<response>`.
839
1681
 
840
- You can wrap endpoints by `<tag>` element to group the endpoints.
841
- You can see the changes in Swagger UI.
1682
+ By default, `type` equals `'application/json'`.
842
1683
 
843
1684
  *src/app.tsx*
844
1685
  ```typescript jsx
845
- export default (
1686
+ return (
846
1687
  <server>
847
1688
  <api>
848
- <tag name='user'>
849
- <endpoint
850
- method='get'
851
- path='/users'
852
- />
853
- <endpoint
854
- method='post'
855
- path='/users'
856
- />
857
- </tag>
1689
+ <endpoint method='get' path='/hello'>
1690
+ <response type='text/html'>
1691
+ Hello World!
1692
+ </response>
1693
+ </endpoint>
858
1694
  </api>
859
1695
  </server>
860
1696
  )
861
1697
  ```
862
1698
 
863
- ### \<param>
864
-
865
- [← back](#endpoints)
866
-
867
- Describes a single operation parameter.
868
-
869
- A unique parameter is defined by a combination of a `name` and location.
870
-
871
- ##### Parameter Locations
872
-
873
- There are four possible parameter locations specified by the `in` prop:
1699
+ ## Primitive Data
874
1700
 
875
- - **path** - Used together with [Path Templating](https://swagger.io/specification/#path-templating), where the parameter value is actually part of the operation's URL.
876
- This does not include the host or base path of the API. For example, in `/items/{itemId}`, the path parameter is `itemId`.
877
- - **query** - Parameters that are appended to the URL. For example, in `/items?id=###`, the query parameter is `id`.
878
- - **header** - Custom headers that are expected as part of the request. Note that RFC7230 states header names are case insensitive.
879
- - **cookie** - Used to pass a specific cookie value to the API.
1701
+ [ back](#index)
880
1702
 
881
- #### in
1703
+ [\<any>](#any)
1704
+ [\<null>](#null)
1705
+ [\<boolean>](#boolean)
1706
+ [\<string>](#string)
1707
+ [\<number>](#number)
1708
+ [\<integer>](#integer)
1709
+ [\<date>](#date)
1710
+ [\<uuid>](#uuid)
1711
+ [\<binary>](#binary)
882
1712
 
883
- The location of the parameter.
884
- Possible values are `"query"`, `"header"`, `"path"` or `"cookie"`.
1713
+ ---
885
1714
 
886
- #### name
1715
+ ### \<any>
887
1716
 
888
- The name of the parameter. Parameter names are *case sensitive*.
1717
+ [← back](#primitive-data)
889
1718
 
890
- - If `in` is "path", the `name` field MUST correspond to a template expression occurring within the `path` field in the `endpoint`. See [Path Templating](https://swagger.io/specification/#path-templating) for further information.
891
- - If `in` is "header" and the `name` field is "Accept", "Content-Type" or "Authorization", the parameter definition SHALL be ignored.
892
- - For all other cases, the `name` corresponds to the parameter name used by the `in` property.
1719
+ The element MUST be placed inside one of [\<response>](#response), [\<param>](#param), [\<body>](#body).
1720
+ It defines `any` value for a parent element.
1721
+ `@innet/server` formats and validate the value automatically (real-time).
893
1722
 
894
1723
  *src/app.tsx*
895
1724
  ```typescript jsx
896
1725
  export default (
897
1726
  <server>
898
1727
  <api>
899
- <endpoint method='get' path='/users/{userId}'>
900
- <param in='path' name='userId' />
1728
+ <endpoint method='get' path='/todos'>
1729
+ <param
1730
+ in='query'
1731
+ name='search'>
1732
+ <any />
1733
+ </param>
901
1734
  </endpoint>
902
1735
  </api>
903
1736
  </server>
904
1737
  )
905
1738
  ```
906
1739
 
907
- #### description
1740
+ #### default
908
1741
 
909
- A brief description of the parameter.
910
- This could contain examples of use.
911
- [CommonMark syntax](https://spec.commonmark.org) MAY be used for rich text representation.
1742
+ A default value for the `any`.
912
1743
 
913
1744
  *src/app.tsx*
914
1745
  ```typescript jsx
915
1746
  export default (
916
1747
  <server>
917
1748
  <api>
918
- <endpoint method='get' path='/users/{userId}'>
1749
+ <endpoint method='get' path='/users'>
919
1750
  <param
920
- in='path'
921
- name='userId'
922
- description='User identification number'
923
- />
1751
+ in='query'
1752
+ name='search'>
1753
+ <any default={null} />
1754
+ </param>
924
1755
  </endpoint>
925
1756
  </api>
926
1757
  </server>
927
1758
  )
928
1759
  ```
929
1760
 
930
- #### required
1761
+ #### example
931
1762
 
932
- Determines whether this parameter is mandatory.
933
- If the parameter location is "path", this property is `true` and its value MUST be `true`.
934
- Otherwise, the property MAY be included and its default value is `false`.
1763
+ An example value.
935
1764
 
936
1765
  *src/app.tsx*
937
1766
  ```typescript jsx
938
1767
  export default (
939
1768
  <server>
940
1769
  <api>
941
- <endpoint method='get' path='/users'>
1770
+ <endpoint method='get' path='/products'>
942
1771
  <param
943
- in='cookie'
944
- name='token'
945
- required
946
- />
1772
+ in='query'
1773
+ name='active'>
1774
+ <any example={false} />
1775
+ </param>
947
1776
  </endpoint>
948
1777
  </api>
949
1778
  </server>
950
1779
  )
951
1780
  ```
952
1781
 
953
- #### deprecated
1782
+ #### description
954
1783
 
955
- Specifies that a parameter is deprecated and SHOULD be transitioned out of usage.
956
- Default value is `false`.
1784
+ A description of the `any`.
957
1785
 
958
1786
  *src/app.tsx*
959
1787
  ```typescript jsx
960
1788
  export default (
961
1789
  <server>
962
1790
  <api>
963
- <endpoint method='get' path='/users'>
1791
+ <endpoint method='get' path='/products'>
964
1792
  <param
965
1793
  in='query'
966
- name='status'
967
- deprecated
968
- />
1794
+ name='active'>
1795
+ <any
1796
+ description='Active products param'
1797
+ />
1798
+ </param>
969
1799
  </endpoint>
970
1800
  </api>
971
1801
  </server>
972
1802
  )
973
1803
  ```
974
1804
 
975
- ### \<body>
1805
+ ### \<null>
976
1806
 
977
- [← back](#endpoints)
1807
+ [← back](#primitive-data)
978
1808
 
979
- This element MUST be placed inside `<endpoint>`.
980
- It defines request body for the endpoint.
1809
+ The element MUST be placed inside one of [\<response>](#response), [\<param>](#param), [\<body>](#body).
1810
+ It defines `null` value for a parent element.
981
1811
  `@innet/server` formats and validate the value automatically (real-time).
982
1812
 
1813
+ *src/app.tsx*
983
1814
  ```typescript jsx
984
- return (
1815
+ export default (
985
1816
  <server>
986
1817
  <api>
987
- <endpoint method='post' path='/partners'>
988
- <body>
989
- <object>
990
- <field key='name'>
991
- <string example='CANTent.' />
992
- </field>
993
- <field key='gift'>
994
- <boolean />
995
- </field>
996
- <field optional key='addresses'>
997
- <array>
998
- <number description='address id' />
999
- </array>
1000
- </field>
1001
- </object>
1002
- </body>
1818
+ <endpoint method='get' path='/todos'>
1819
+ <param
1820
+ in='query'
1821
+ name='search'>
1822
+ <null />
1823
+ </param>
1003
1824
  </endpoint>
1004
1825
  </api>
1005
1826
  </server>
1006
1827
  )
1007
1828
  ```
1008
1829
 
1009
- ### \<response>
1010
-
1011
- [← back](#endpoints)
1012
-
1013
- This element MUST be placed inside `<endpoint>`.
1014
- It defines response body for the endpoint.
1015
-
1016
- ## Primitive Data
1830
+ #### description
1017
1831
 
1018
- [← back](#index)
1832
+ A description of the `null`.
1019
1833
 
1020
- [\<null>](#null)
1021
- [\<boolean>](#boolean)
1022
- [\<string>](#string)
1023
- [\<number>](#number)
1024
- [\<integer>](#integer)
1025
- [\<date>](#date)
1026
- [\<uuid>](#uuid)
1027
- [\<binary>](#binary)
1834
+ *src/app.tsx*
1835
+ ```typescript jsx
1836
+ export default (
1837
+ <server>
1838
+ <api>
1839
+ <endpoint method='get' path='/products'>
1840
+ <param
1841
+ in='query'
1842
+ name='active'>
1843
+ <null description='FIXME!' />
1844
+ </param>
1845
+ </endpoint>
1846
+ </api>
1847
+ </server>
1848
+ )
1849
+ ```
1028
1850
 
1029
1851
  ### \<boolean>
1030
1852
 
@@ -2306,7 +3128,9 @@ export default (
2306
3128
  <api>
2307
3129
  <endpoint method='post' path='/users'>
2308
3130
  <body>
2309
- <object />
3131
+ <object>
3132
+ <string />
3133
+ </object>
2310
3134
  </body>
2311
3135
  </endpoint>
2312
3136
  </api>
@@ -2367,9 +3191,9 @@ export default (
2367
3191
  <api>
2368
3192
  <endpoint method='post' path='/users'>
2369
3193
  <body>
2370
- <object
2371
- description='The object of a user'
2372
- />
3194
+ <object
3195
+ description='The object of a user'
3196
+ />
2373
3197
  </body>
2374
3198
  </endpoint>
2375
3199
  </api>
@@ -2385,7 +3209,7 @@ The element MUST be placed inside [\<object>](#object).
2385
3209
  It defines a `field` of an `object` value for a parent element.
2386
3210
  `@innet/server` formats and validate the value automatically (real-time).
2387
3211
 
2388
- `key` is REQUIRED prop of `<field>`, it defines a field name of the `<object>`.
3212
+ `key` is REQUIRED prop of `<field>`, it defines a field name of the [\<object>](#object).
2389
3213
 
2390
3214
  *src/app.tsx*
2391
3215
  ```typescript jsx
@@ -2406,139 +3230,69 @@ export default (
2406
3230
  )
2407
3231
  ```
2408
3232
 
2409
- #### optional
2410
-
2411
- By default, any field is required. You can set it as `optional` by this prop.
2412
-
2413
- *src/app.tsx*
2414
- ```typescript jsx
2415
- export default (
2416
- <server>
2417
- <api>
2418
- <endpoint method='post' path='/users'>
2419
- <body>
2420
- <object>
2421
- <field key='name' />
2422
- <field key='surname' />
2423
- <field optional key='birthbay' />
2424
- </object>
2425
- </body>
2426
- </endpoint>
2427
- </api>
2428
- </server>
2429
- )
2430
- ```
2431
-
2432
- ## Run-Time
2433
-
2434
- Next elements relate to run-time action.
2435
- This action calls on user request.
2436
-
2437
- [← back](#index)
2438
-
2439
- Parent
2440
- [\<fallback>](#fallback)
2441
- [\<request>](#request)
2442
-
2443
- Children
2444
- [\<success>](#success)
2445
- [\<error>](#error)
2446
- [\<proxy>](#proxy)
2447
- [\<redirect>](#redirect)
2448
- [\<cms>](#cms)
2449
- [\<file>](#file)
2450
- [\<header>](#header)
2451
- [\<cookie>](#cookie)
2452
-
2453
- ---
2454
-
2455
- ### \<fallback>
2456
-
2457
- [← back](#run-time)
2458
-
2459
- By default, `<api>` server returns 404 with empty body.
2460
- [\<fallback>](#fallback) element defines default server response.
2461
- This element MUST be placed in `<api>`.
2462
- You MUST use one [\<fallback>](#fallback) per `<api>`.
2463
- Can contain elements available inside [\<request>](#request).
2464
-
2465
- *src/app.tsx*
2466
- ```typescript jsx
2467
- export default (
2468
- <server>
2469
- <api>
2470
- <fallback>
2471
- <error
2472
- code='unknownEndpoint'
2473
- />
2474
- </fallback>
2475
- </api>
2476
- </server>
2477
- )
2478
- ```
2479
-
2480
- If you open the application on any URL except for `/`, you can see the next response.
2481
-
2482
- ```json
2483
- {
2484
- "error": "unknownEndpoint"
2485
- }
2486
- ```
2487
-
2488
- The next elements are placed in [\<request>](#request) or [\<fallback>](#fallback)
2489
-
2490
- ### \<request>
2491
-
2492
- [← back](#run-time)
3233
+ #### optional
2493
3234
 
2494
- This element MUST be placed in `<endpoint>` element.
2495
- It defines run-time call handler for the endpoint.
3235
+ By default, any field is required. You can set it as `optional` by this prop.
2496
3236
 
2497
3237
  *src/app.tsx*
2498
3238
  ```typescript jsx
2499
3239
  export default (
2500
3240
  <server>
2501
3241
  <api>
2502
- <endpoint method='get' path='/partners'>
2503
- <request>
2504
- <success>
2505
- {{partners: []}}
2506
- </success>
2507
- </request>
3242
+ <endpoint method='post' path='/users'>
3243
+ <body>
3244
+ <object>
3245
+ <field key='name' />
3246
+ <field key='surname' />
3247
+ <field optional key='birthbay' />
3248
+ </object>
3249
+ </body>
2508
3250
  </endpoint>
2509
3251
  </api>
2510
3252
  </server>
2511
3253
  )
2512
3254
  ```
2513
3255
 
2514
- You can place a component inside it.
2515
- The component will run when the endpoint will be triggered.
3256
+ #### deprecated
3257
+
3258
+ You can deprecate a field.
2516
3259
 
2517
3260
  *src/app.tsx*
2518
3261
  ```typescript jsx
2519
- import { GetPartners } from './GetPartners'
2520
-
2521
3262
  export default (
2522
3263
  <server>
2523
3264
  <api>
2524
- <endpoint method='get' path='/partners'>
2525
- <request>
2526
- <GetPartners />
2527
- </request>
3265
+ <endpoint method='post' path='/users'>
3266
+ <body>
3267
+ <object>
3268
+ <field key='name' />
3269
+ <field key='surname' />
3270
+ <field deprecated optional key='birthbay' />
3271
+ </object>
3272
+ </body>
2528
3273
  </endpoint>
2529
3274
  </api>
2530
3275
  </server>
2531
3276
  )
2532
3277
  ```
2533
3278
 
2534
- *src/GetPartners.tsx*
2535
- ```typescript jsx
2536
- export const GetPartners = () => (
2537
- <success>
2538
- {{partners: []}}
2539
- </success>
2540
- )
2541
- ```
3279
+ ## Run-Time
3280
+
3281
+ Next elements relate to run-time action.
3282
+ This action calls on user request.
3283
+
3284
+ [← back](#index)
3285
+
3286
+ [\<success>](#success)
3287
+ [\<error>](#error)
3288
+ [\<proxy>](#proxy)
3289
+ [\<redirect>](#redirect)
3290
+ [\<cms>](#cms)
3291
+ [\<file>](#file)
3292
+ [\<header>](#header)
3293
+ [\<cookie>](#cookie)
3294
+
3295
+ ---
2542
3296
 
2543
3297
  ### \<success>
2544
3298
 
@@ -2550,11 +3304,9 @@ This is a base element to return a success data.
2550
3304
  ```typescript jsx
2551
3305
  export default (
2552
3306
  <server>
2553
- <api>
2554
- <fallback>
2555
- <success />
2556
- </fallback>
2557
- </api>
3307
+ <response>
3308
+ <success />
3309
+ </response>
2558
3310
  </server>
2559
3311
  )
2560
3312
  ```
@@ -2569,13 +3321,11 @@ const data = {...}
2569
3321
 
2570
3322
  export default (
2571
3323
  <server>
2572
- <api>
2573
- <fallback>
2574
- <success>
2575
- {data}
2576
- </success>
2577
- </fallback>
2578
- </api>
3324
+ <response>
3325
+ <success>
3326
+ {data}
3327
+ </success>
3328
+ </response>
2579
3329
  </server>
2580
3330
  )
2581
3331
  ```
@@ -2592,13 +3342,11 @@ const data = {...}
2592
3342
 
2593
3343
  export default (
2594
3344
  <server>
2595
- <api>
2596
- <fallback>
2597
- <success status='created'>
2598
- {data}
2599
- </success>
2600
- </fallback>
2601
- </api>
3345
+ <response>
3346
+ <success status='created'>
3347
+ {data}
3348
+ </success>
3349
+ </response>
2602
3350
  </server>
2603
3351
  )
2604
3352
  ```
@@ -2613,13 +3361,29 @@ const data = {...}
2613
3361
 
2614
3362
  export default (
2615
3363
  <server>
2616
- <api>
2617
- <fallback>
2618
- <success status={201}>
2619
- {data}
2620
- </success>
2621
- </fallback>
2622
- </api>
3364
+ <response>
3365
+ <success status={201}>
3366
+ {data}
3367
+ </success>
3368
+ </response>
3369
+ </server>
3370
+ )
3371
+ ```
3372
+
3373
+ #### contentType
3374
+
3375
+ This props sets response content type.
3376
+ By default, it checks children element to define the prop.
3377
+
3378
+ *src/app.tsx*
3379
+ ```typescript jsx
3380
+ export default (
3381
+ <server>
3382
+ <response>
3383
+ <success contentType='text/html'>
3384
+ Hello World!
3385
+ </success>
3386
+ </response>
2623
3387
  </server>
2624
3388
  )
2625
3389
  ```
@@ -2629,17 +3393,15 @@ export default (
2629
3393
  [← back](#run-time)
2630
3394
 
2631
3395
  Returns an error.
2632
- This element MUST be placed in [\<request>](#request) or [\<fallback>](#fallback).
3396
+ This element MUST be placed in [\<return>](#return) or [\<response>](#response).
2633
3397
 
2634
3398
  *src/app.tsx*
2635
3399
  ```typescript jsx
2636
3400
  export default (
2637
3401
  <server>
2638
- <api>
2639
- <fallback>
2640
- <error />
2641
- </fallback>
2642
- </api>
3402
+ <response>
3403
+ <error />
3404
+ </response>
2643
3405
  </server>
2644
3406
  )
2645
3407
  ```
@@ -2652,13 +3414,11 @@ const data = {...}
2652
3414
 
2653
3415
  export default (
2654
3416
  <server>
2655
- <api>
2656
- <fallback>
2657
- <error>
2658
- {data}
2659
- </error>
2660
- </fallback>
2661
- </api>
3417
+ <response>
3418
+ <error>
3419
+ {data}
3420
+ </error>
3421
+ </response>
2662
3422
  </server>
2663
3423
  )
2664
3424
  ```
@@ -2674,13 +3434,11 @@ const data = {
2674
3434
 
2675
3435
  export default (
2676
3436
  <server>
2677
- <api>
2678
- <fallback>
2679
- <error status='notFound'>
2680
- {data}
2681
- </error>
2682
- </fallback>
2683
- </api>
3437
+ <response>
3438
+ <error status='notFound'>
3439
+ {data}
3440
+ </error>
3441
+ </response>
2684
3442
  </server>
2685
3443
  )
2686
3444
  ```
@@ -2695,13 +3453,11 @@ const data = {
2695
3453
 
2696
3454
  export default (
2697
3455
  <server>
2698
- <api>
2699
- <fallback>
2700
- <error status={404}>
2701
- {data}
2702
- </error>
2703
- </fallback>
2704
- </api>
3456
+ <response>
3457
+ <error status={404}>
3458
+ {data}
3459
+ </error>
3460
+ </response>
2705
3461
  </server>
2706
3462
  )
2707
3463
  ```
@@ -2729,15 +3485,13 @@ const data = {
2729
3485
 
2730
3486
  export default (
2731
3487
  <server>
2732
- <api>
2733
- <fallback>
2734
- <error
2735
- code='noUser'
2736
- status='notFound'>
2737
- {data}
2738
- </error>
2739
- </fallback>
2740
- </api>
3488
+ <response>
3489
+ <error
3490
+ code='noUser'
3491
+ status='notFound'>
3492
+ {data}
3493
+ </error>
3494
+ </response>
2741
3495
  </server>
2742
3496
  )
2743
3497
  ```
@@ -2765,7 +3519,7 @@ There are some default errors:
2765
3519
 
2766
3520
  [← back](#run-time)
2767
3521
 
2768
- MUST be placed in [\<request>](#request) or [\<fallback>](#fallback).
3522
+ MUST be placed in [\<return>](#return) or [\<response>](#response).
2769
3523
 
2770
3524
  You can easy proxy endpoints to another server/service.
2771
3525
 
@@ -2777,14 +3531,14 @@ export default (
2777
3531
  <endpoint
2778
3532
  path='/test'
2779
3533
  method='get'>
2780
- <request>
3534
+ <return>
2781
3535
  <proxy to='https://...' />
2782
- </request>
3536
+ </return>
2783
3537
  </endpoint>
2784
- <fallback>
2785
- <proxy to='https://...' />
2786
- </fallback>
2787
3538
  </api>
3539
+ <response>
3540
+ <proxy to='https://...' />
3541
+ </response>
2788
3542
  </server>
2789
3543
  )
2790
3544
  ```
@@ -2793,7 +3547,7 @@ export default (
2793
3547
 
2794
3548
  [← back](#run-time)
2795
3549
 
2796
- MUST be placed in [\<request>](#request) or [\<fallback>](#fallback).
3550
+ MUST be placed in [\<return>](#return) or [\<response>](#response).
2797
3551
 
2798
3552
  You can redirect users to another resource. It adds `Cache-Control` header by default.
2799
3553
 
@@ -2805,14 +3559,14 @@ export default (
2805
3559
  <endpoint
2806
3560
  path='/test'
2807
3561
  method='get'>
2808
- <request>
3562
+ <return>
2809
3563
  <redirect to='https://...' />
2810
- </request>
3564
+ </return>
2811
3565
  </endpoint>
2812
- <fallback>
2813
- <redirect to='https://...' />
2814
- </fallback>
2815
3566
  </api>
3567
+ <return>
3568
+ <redirect to='https://...' />
3569
+ </return>
2816
3570
  </server>
2817
3571
  )
2818
3572
  ```
@@ -2830,20 +3584,20 @@ export default (
2830
3584
  <endpoint
2831
3585
  path='/test'
2832
3586
  method='get'>
2833
- <request>
3587
+ <return>
2834
3588
  <redirect
2835
3589
  status='found'
2836
3590
  to='https://...'
2837
3591
  />
2838
- </request>
3592
+ </return>
2839
3593
  </endpoint>
2840
- <fallback>
2841
- <redirect
2842
- status={303}
2843
- to='https://...'
2844
- />
2845
- </fallback>
2846
3594
  </api>
3595
+ <response>
3596
+ <redirect
3597
+ status={303}
3598
+ to='https://...'
3599
+ />
3600
+ </response>
2847
3601
  </server>
2848
3602
  )
2849
3603
  ```
@@ -2852,7 +3606,7 @@ export default (
2852
3606
 
2853
3607
  [← back](#run-time)
2854
3608
 
2855
- MUST be placed in [\<request>](#request) or [\<fallback>](#fallback).
3609
+ MUST be placed in [\<return>](#return) or [\<response>](#response).
2856
3610
 
2857
3611
  `<cms>` helps to return files from a folder by path. It checks files run-time on the server.
2858
3612
 
@@ -2860,18 +3614,16 @@ MUST be placed in [\<request>](#request) or [\<fallback>](#fallback).
2860
3614
  ```typescript jsx
2861
3615
  export default (
2862
3616
  <server>
2863
- <api>
2864
- <fallback>
2865
- <cms />
2866
- </fallback>
2867
- </api>
3617
+ <response>
3618
+ <cms />
3619
+ </response>
2868
3620
  </server>
2869
3621
  )
2870
3622
  ```
2871
3623
 
2872
3624
  #### dir
2873
3625
 
2874
- By default, it looks at project folder.
3626
+ By default, it equals `INNET_CMS_DIR` node env variable or the project folder.
2875
3627
  If you try the previous example on [http://localhost/package.json](http://localhost/package.json)
2876
3628
  you get the project `package.json` file.
2877
3629
 
@@ -2881,11 +3633,9 @@ You can change root folder by `dir` property.
2881
3633
  ```typescript jsx
2882
3634
  export default (
2883
3635
  <server>
2884
- <api>
2885
- <fallback>
2886
- <cms dir='src' />
2887
- </fallback>
2888
- </api>
3636
+ <response>
3637
+ <cms dir='src' />
3638
+ </response>
2889
3639
  </server>
2890
3640
  )
2891
3641
  ```
@@ -2896,15 +3646,16 @@ you get the index file in `src` folder.
2896
3646
  #### prefix
2897
3647
 
2898
3648
  `<cms>` matches full `path`, you should take it into account if you add `prefix` on `<api>`.
3649
+ By default, it equals `INNET_CMS_PREFIX` node env variable or `/`.
2899
3650
 
2900
3651
  *src/app.tsx*
2901
3652
  ```typescript jsx
2902
3653
  export default (
2903
3654
  <server>
2904
3655
  <api prefix='/src'>
2905
- <fallback>
3656
+ <response>
2906
3657
  <cms />
2907
- </fallback>
3658
+ </response>
2908
3659
  </api>
2909
3660
  </server>
2910
3661
  )
@@ -2920,9 +3671,9 @@ You can reduce the path for matching by prefix property of `<cms>`.
2920
3671
  export default (
2921
3672
  <server>
2922
3673
  <api prefix='/api'>
2923
- <fallback>
3674
+ <response>
2924
3675
  <cms prefix='/api' />
2925
- </fallback>
3676
+ </response>
2926
3677
  </api>
2927
3678
  </server>
2928
3679
  )
@@ -2938,13 +3689,11 @@ You can handle if a file was not found by children elements of `<cms>`.
2938
3689
  ```typescript jsx
2939
3690
  export default (
2940
3691
  <server>
2941
- <api prefix='/src'>
2942
- <fallback>
2943
- <cms>
2944
- <error status={404} />
2945
- </cms>
2946
- </fallback>
2947
- </api>
3692
+ <response>
3693
+ <cms>
3694
+ <error status={404} />
3695
+ </cms>
3696
+ </response>
2948
3697
  </server>
2949
3698
  )
2950
3699
  ```
@@ -2953,7 +3702,7 @@ export default (
2953
3702
 
2954
3703
  [← back](#run-time)
2955
3704
 
2956
- It returns a file. MUST be placed in [\<request>](#request) or [\<fallback>](#fallback).
3705
+ It returns a file. MUST be placed in [\<return>](#return) or [\<response>](#response).
2957
3706
 
2958
3707
  It adds `Content-Length` and `Content-Type` automatically.
2959
3708
 
@@ -2963,13 +3712,9 @@ It has a REQUIRED property of `path`.
2963
3712
  ```typescript jsx
2964
3713
  export default (
2965
3714
  <server>
2966
- <api>
2967
- <fallback>
2968
- <file
2969
- path='package.json'
2970
- />
2971
- </fallback>
2972
- </api>
3715
+ <response>
3716
+ <file path='package.json' />
3717
+ </response>
2973
3718
  </server>
2974
3719
  )
2975
3720
  ```
@@ -2984,20 +3729,18 @@ You can handle if a file was not found by children elements of `<file>`.
2984
3729
  ```typescript jsx
2985
3730
  export default (
2986
3731
  <server>
2987
- <api prefix='/src'>
2988
- <fallback>
2989
- <file path='file_is_not_exist.txt'>
2990
- <error status={404} />
2991
- </file>
2992
- </fallback>
2993
- </api>
3732
+ <response>
3733
+ <file path='file_is_not_exist.txt'>
3734
+ <error status={404} />
3735
+ </file>
3736
+ </response>
2994
3737
  </server>
2995
3738
  )
2996
3739
  ```
2997
3740
 
2998
3741
  ### \<header>
2999
3742
 
3000
- MUST be placed in [\<request>](#request) or [\<fallback>](#fallback).
3743
+ MUST be placed in [\<return>](#return) or [\<response>](#response).
3001
3744
 
3002
3745
  [← back](#run-time)
3003
3746
 
@@ -3007,22 +3750,20 @@ You can add an HTTP header into response by `<header>` element.
3007
3750
  ```typescript jsx
3008
3751
  export default (
3009
3752
  <server>
3010
- <api prefix='/src'>
3011
- <fallback>
3012
- <header
3013
- key='Cache-Control'
3014
- value='no-cache, no-store, must-revalidate'
3015
- />
3016
- <success />
3017
- </fallback>
3018
- </api>
3753
+ <response>
3754
+ <header
3755
+ key='Cache-Control'
3756
+ value='no-cache, no-store, must-revalidate'
3757
+ />
3758
+ <success />
3759
+ </response>
3019
3760
  </server>
3020
3761
  )
3021
3762
  ```
3022
3763
 
3023
3764
  ### \<cookie>
3024
3765
 
3025
- MUST be placed in [\<request>](#request) or [\<fallback>](#fallback).
3766
+ MUST be placed in [\<return>](#return) or [\<response>](#response).
3026
3767
 
3027
3768
  [← back](#run-time)
3028
3769
 
@@ -3032,18 +3773,16 @@ You can add/remove a cookie into response by `<cookie>` element.
3032
3773
  ```typescript jsx
3033
3774
  export default (
3034
3775
  <server>
3035
- <api prefix='/src'>
3036
- <fallback>
3037
- <cookie
3038
- key='token'
3039
- value='...'
3040
- />
3041
- <cookie
3042
- key='removedCookie'
3043
- />
3044
- <success />
3045
- </fallback>
3046
- </api>
3776
+ <response>
3777
+ <cookie
3778
+ key='token'
3779
+ value='...'
3780
+ />
3781
+ <cookie
3782
+ key='removedCookie'
3783
+ />
3784
+ <success />
3785
+ </response>
3047
3786
  </server>
3048
3787
  )
3049
3788
  ```
@@ -3057,16 +3796,14 @@ By default, no domain is set, and most clients will consider the cookie to apply
3057
3796
  ```typescript jsx
3058
3797
  export default (
3059
3798
  <server>
3060
- <api prefix='/src'>
3061
- <fallback>
3062
- <cookie
3063
- domain='.example.com'
3064
- key='token'
3065
- value='...'
3066
- />
3067
- <success />
3068
- </fallback>
3069
- </api>
3799
+ <response>
3800
+ <cookie
3801
+ domain='.example.com'
3802
+ key='token'
3803
+ value='...'
3804
+ />
3805
+ <success />
3806
+ </response>
3070
3807
  </server>
3071
3808
  )
3072
3809
  ```
@@ -3085,16 +3822,14 @@ Note the [cookie storage model specification](https://datatracker.ietf.org/doc/h
3085
3822
  ```typescript jsx
3086
3823
  export default (
3087
3824
  <server>
3088
- <api prefix='/src'>
3089
- <fallback>
3090
- <cookie
3091
- expires={new Date('2050-01-01')}
3092
- key='token'
3093
- value='...'
3094
- />
3095
- <success />
3096
- </fallback>
3097
- </api>
3825
+ <response>
3826
+ <cookie
3827
+ expires={new Date('2050-01-01')}
3828
+ key='token'
3829
+ value='...'
3830
+ />
3831
+ <success />
3832
+ </response>
3098
3833
  </server>
3099
3834
  )
3100
3835
  ```
@@ -3108,16 +3843,14 @@ Note be careful when setting this to true, as compliant clients will not allow c
3108
3843
  ```typescript jsx
3109
3844
  export default (
3110
3845
  <server>
3111
- <api prefix='/src'>
3112
- <fallback>
3113
- <cookie
3114
- httpOnly
3115
- key='token'
3116
- value='...'
3117
- />
3118
- <success />
3119
- </fallback>
3120
- </api>
3846
+ <response>
3847
+ <cookie
3848
+ httpOnly
3849
+ key='token'
3850
+ value='...'
3851
+ />
3852
+ <success />
3853
+ </response>
3121
3854
  </server>
3122
3855
  )
3123
3856
  ```
@@ -3131,17 +3864,15 @@ Note the [cookie storage model specification](https://datatracker.ietf.org/doc/h
3131
3864
  ```typescript jsx
3132
3865
  export default (
3133
3866
  <server>
3134
- <api prefix='/src'>
3135
- <fallback>
3136
- <cookie
3137
- httpOnly
3138
- maxAge={9999}
3139
- key='token'
3140
- value='...'
3141
- />
3142
- <success />
3143
- </fallback>
3144
- </api>
3867
+ <response>
3868
+ <cookie
3869
+ httpOnly
3870
+ maxAge={9999}
3871
+ key='token'
3872
+ value='...'
3873
+ />
3874
+ <success />
3875
+ </response>
3145
3876
  </server>
3146
3877
  )
3147
3878
  ```
@@ -3155,18 +3886,16 @@ By default, the path is considered the “default path”.
3155
3886
  ```typescript jsx
3156
3887
  export default (
3157
3888
  <server>
3158
- <api prefix='/src'>
3159
- <fallback>
3160
- <cookie
3161
- httpOnly
3162
- maxAge={9999}
3163
- path='/src'
3164
- key='token'
3165
- value='...'
3166
- />
3167
- <success />
3168
- </fallback>
3169
- </api>
3889
+ <response>
3890
+ <cookie
3891
+ httpOnly
3892
+ maxAge={9999}
3893
+ path='/src'
3894
+ key='token'
3895
+ value='...'
3896
+ />
3897
+ <success />
3898
+ </response>
3170
3899
  </server>
3171
3900
  )
3172
3901
  ```
@@ -3185,18 +3914,16 @@ note This is an attribute that has not yet been fully standardized, and may chan
3185
3914
  ```typescript jsx
3186
3915
  export default (
3187
3916
  <server>
3188
- <api prefix='/src'>
3189
- <fallback>
3190
- <cookie
3191
- httpOnly
3192
- priority='high'
3193
- path='/src'
3194
- key='token'
3195
- value='...'
3196
- />
3197
- <success />
3198
- </fallback>
3199
- </api>
3917
+ <response>
3918
+ <cookie
3919
+ httpOnly
3920
+ priority='high'
3921
+ path='/src'
3922
+ key='token'
3923
+ value='...'
3924
+ />
3925
+ <success />
3926
+ </response>
3200
3927
  </server>
3201
3928
  )
3202
3929
  ```
@@ -3218,19 +3945,17 @@ This also means many clients may ignore this attribute until they understand it.
3218
3945
  ```typescript jsx
3219
3946
  export default (
3220
3947
  <server>
3221
- <api prefix='/src'>
3222
- <fallback>
3223
- <cookie
3224
- httpOnly
3225
- sameSite
3226
- priority='high'
3227
- path='/src'
3228
- key='token'
3229
- value='...'
3230
- />
3231
- <success />
3232
- </fallback>
3233
- </api>
3948
+ <response>
3949
+ <cookie
3950
+ httpOnly
3951
+ sameSite
3952
+ priority='high'
3953
+ path='/src'
3954
+ key='token'
3955
+ value='...'
3956
+ />
3957
+ <success />
3958
+ </response>
3234
3959
  </server>
3235
3960
  )
3236
3961
  ```
@@ -3247,17 +3972,15 @@ Note be careful when setting this to true, as compliant clients will not send th
3247
3972
  ```typescript jsx
3248
3973
  export default (
3249
3974
  <server>
3250
- <api prefix='/src'>
3251
- <fallback>
3252
- <cookie
3253
- httpOnly
3254
- secure
3255
- key='token'
3256
- value='...'
3257
- />
3258
- <success />
3259
- </fallback>
3260
- </api>
3975
+ <response>
3976
+ <cookie
3977
+ httpOnly
3978
+ secure
3979
+ key='token'
3980
+ value='...'
3981
+ />
3982
+ <success />
3983
+ </response>
3261
3984
  </server>
3262
3985
  )
3263
3986
  ```
@@ -3290,14 +4013,12 @@ import { SetToken } from './SetToken'
3290
4013
 
3291
4014
  export default (
3292
4015
  <server>
3293
- <api prefix='/src'>
3294
- <fallback>
3295
- <SetToken
3296
- value='...'
3297
- />
3298
- <success />
3299
- </fallback>
3300
- </api>
4016
+ <response>
4017
+ <SetToken
4018
+ value='...'
4019
+ />
4020
+ <success />
4021
+ </response>
3301
4022
  </server>
3302
4023
  )
3303
4024
  ```
@@ -3310,7 +4031,7 @@ Hook functions give you all features to control parent element functionality.
3310
4031
 
3311
4032
  [← back](#index)
3312
4033
 
3313
- [useServer](#useserver)
4034
+ Real-time
3314
4035
  [useRequest](#userequest)
3315
4036
  [useResponse](#useresponse)
3316
4037
  [useHeaders](#useheaders)
@@ -3319,35 +4040,24 @@ Hook functions give you all features to control parent element functionality.
3319
4040
  [useParams](#useparams)
3320
4041
  [useSearch](#usesearch)
3321
4042
  [useBody](#usebody)
4043
+ [useClientIp](#useclientip)
3322
4044
 
3323
- ---
3324
-
3325
- ### useServer
3326
-
3327
- [← back](#hooks)
3328
-
3329
- This hook MUST be used in a component placed in `<server>`.
3330
- This hook returns current http(s) server instance.
3331
-
3332
- *src/Component.tsx*
3333
-
3334
- ```typescript jsx
3335
- import { useServer } from '@innet/sever'
3336
-
3337
- export function Component () {
3338
- const server = useServer()
4045
+ Server start
4046
+ [useServerPlugin](#useserverplugin)
3339
4047
 
3340
- console.log(server)
4048
+ Both
4049
+ [useServer](#useserver)
4050
+ [usePort](#useport)
4051
+ [useIsServerHttps](#useisserverhttps)
4052
+ [useComponentName](#usecomponentname)
3341
4053
 
3342
- return <success />
3343
- }
3344
- ```
4054
+ ---
3345
4055
 
3346
4056
  ### useRequest
3347
4057
 
3348
4058
  [← back](#hooks)
3349
4059
 
3350
- This hook MUST be used in a component placed in [\<request>](#request) or [\<fallback>](#fallback).
4060
+ This hook MUST be used in a component placed in [\<return>](#return) or [\<response>](#response).
3351
4061
  This hook returns current request instance.
3352
4062
 
3353
4063
  *src/Component.tsx*
@@ -3368,7 +4078,7 @@ export function Component () {
3368
4078
 
3369
4079
  [← back](#hooks)
3370
4080
 
3371
- This hook MUST be used in a component placed in [\<request>](#request) or [\<fallback>](#fallback).
4081
+ This hook MUST be used in a component placed in [\<return>](#return) or [\<response>](#response).
3372
4082
  This hook returns current response instance.
3373
4083
 
3374
4084
  *src/Component.tsx*
@@ -3389,7 +4099,7 @@ export function Component () {
3389
4099
 
3390
4100
  [← back](#hooks)
3391
4101
 
3392
- This hook MUST be used in a component placed in [\<request>](#request) or [\<fallback>](#fallback).
4102
+ This hook MUST be used in a component placed in [\<return>](#return) or [\<response>](#response).
3393
4103
  This hook returns current request headers object.
3394
4104
 
3395
4105
  *src/Component.tsx*
@@ -3408,7 +4118,7 @@ export function Component () {
3408
4118
 
3409
4119
  [← back](#hooks)
3410
4120
 
3411
- This hook MUST be used in a component placed in [\<request>](#request) or [\<fallback>](#fallback).
4121
+ This hook MUST be used in a component placed in [\<return>](#return) or [\<response>](#response).
3412
4122
  This hook returns current request cookies object.
3413
4123
 
3414
4124
  *src/Component.tsx*
@@ -3427,7 +4137,7 @@ export function Component () {
3427
4137
 
3428
4138
  [← back](#hooks)
3429
4139
 
3430
- This hook MUST be used in a component placed in [\<request>](#request) or [\<fallback>](#fallback).
4140
+ This hook MUST be used in a component placed in [\<return>](#return) or [\<response>](#response).
3431
4141
  This hook returns current request URL path as a `string`.
3432
4142
 
3433
4143
  *src/Component.tsx*
@@ -3446,7 +4156,7 @@ export function Component () {
3446
4156
 
3447
4157
  [← back](#hooks)
3448
4158
 
3449
- This hook MUST be used in a component placed in [\<request>](#request) or [\<fallback>](#fallback).
4159
+ This hook MUST be used in a component placed in [\<return>](#return) or [\<response>](#response).
3450
4160
  This hook returns an object of URL params you set by [\<param>](#param).
3451
4161
 
3452
4162
  *src/Component.tsx*
@@ -3464,7 +4174,7 @@ export function Component () {
3464
4174
 
3465
4175
  [← back](#hooks)
3466
4176
 
3467
- This hook MUST be used in a component placed in [\<request>](#request) or [\<fallback>](#fallback).
4177
+ This hook MUST be used in a component placed in [\<return>](#return) or [\<response>](#response).
3468
4178
  This hook returns an object of URL query params.
3469
4179
 
3470
4180
  *src/Component.tsx*
@@ -3482,7 +4192,7 @@ export function Component () {
3482
4192
 
3483
4193
  [← back](#hooks)
3484
4194
 
3485
- This hook MUST be used in a component placed in [\<request>](#request) or [\<fallback>](#fallback).
4195
+ This hook MUST be used in a component placed in [\<return>](#return) or [\<response>](#response).
3486
4196
  This hook returns current request body.
3487
4197
 
3488
4198
  *src/Component.tsx*
@@ -3496,6 +4206,153 @@ export function Component () {
3496
4206
  }
3497
4207
  ```
3498
4208
 
4209
+ ### useClientIp
4210
+
4211
+ [← back](#hooks)
4212
+
4213
+ This hook returns request user IP.
4214
+ This hook MUST be used in a component placed in [\<return>](#return) or [\<response>](#response).
4215
+
4216
+ *src/Component.tsx*
4217
+ ```typescript jsx
4218
+ import { useClientIp } from '@innet/sever'
4219
+
4220
+ export function Component () {
4221
+ const ip = useClientIp()
4222
+
4223
+ return <success>{{ ip }}</success>
4224
+ }
4225
+ ```
4226
+
4227
+ ### useServerPlugin
4228
+
4229
+ [← back](#hooks)
4230
+
4231
+ This hook adds a request plugin function.
4232
+ The function runs before check endpoints.
4233
+ If the function returns `true` the request handling stops, and you get full control over the request.
4234
+
4235
+ This hook MUST be used in a component placed in [\<server>](#server).
4236
+
4237
+ *src/SecretEndpoint.tsx*
4238
+ ```typescript jsx
4239
+ import { useRequestPlugin, useAction } from '@innet/sever'
4240
+
4241
+ export function SecretEndpoint () {
4242
+ useServerPlugin(() => {
4243
+ const action = useAction()
4244
+
4245
+ if (action.path.startsWith('/secret-endpoint')) {
4246
+ return <success>A secret message</success>
4247
+ }
4248
+ })
4249
+ }
4250
+ ```
4251
+
4252
+ Then use the plugin in [\<server>](#server) or [\<api>](#api).
4253
+
4254
+ *src/app.tsx*
4255
+ ```typescript jsx
4256
+ import { SecretEndpoint } from './SecretEndpoint'
4257
+
4258
+ export default (
4259
+ <server>
4260
+ <SecretEndpoint />
4261
+ <response>
4262
+ <error />
4263
+ </response>
4264
+ </server>
4265
+ )
4266
+ ```
4267
+
4268
+ Any endpoint returns an error except for `/secret-endpoint`.
4269
+ Elements order does not matter.
4270
+
4271
+ ### useServer
4272
+
4273
+ [← back](#hooks)
4274
+
4275
+ This hook MUST be used in a component placed in [\<server>](#server).
4276
+ This hook returns current http(s) server instance.
4277
+
4278
+ *src/Component.tsx*
4279
+ ```typescript jsx
4280
+ import { useServer } from '@innet/sever'
4281
+
4282
+ export function Component () {
4283
+ const server = useServer()
4284
+
4285
+ console.log(server)
4286
+
4287
+ return <success />
4288
+ }
4289
+ ```
4290
+
4291
+ ### usePort
4292
+
4293
+ [← back](#hooks)
4294
+
4295
+ This hook MUST be used in a component placed in [\<server>](#server).
4296
+ This hook returns current http(s) server port.
4297
+
4298
+ *src/LocalHost.tsx*
4299
+ ```typescript jsx
4300
+ import { usePort } from '@innet/sever'
4301
+
4302
+ export function LocalHost () {
4303
+ const port = usePort()
4304
+
4305
+ return (
4306
+ <host
4307
+ description='Development'
4308
+ url={`http://localhost:${port}/api`}
4309
+ />
4310
+ )
4311
+ }
4312
+ ```
4313
+
4314
+ ### useIsServerHttps
4315
+
4316
+ [← back](#hooks)
4317
+
4318
+ This hook MUST be used in a component placed in [\<server>](#server).
4319
+ This hook returns `true` if it is https server and `false` if not.
4320
+
4321
+ *src/LocalHost.tsx*
4322
+ ```typescript jsx
4323
+ import { usePort, useIsServerHttps } from '@innet/sever'
4324
+
4325
+ export function LocalHost () {
4326
+ const https = useIsServerHttps() ? 'https' : 'http'
4327
+ const port = usePort()
4328
+
4329
+ return (
4330
+ <host
4331
+ description='Development'
4332
+ url={`${https}://localhost:${port}/api`}
4333
+ />
4334
+ )
4335
+ }
4336
+ ```
4337
+
4338
+ ### useComponentName
4339
+
4340
+ [← back](#hooks)
4341
+
4342
+ This hook returns name of current component.
4343
+
4344
+ *src/Component.tsx*
4345
+ ```typescript jsx
4346
+ import { useComponentName } from '@innet/sever'
4347
+
4348
+ export function Component () {
4349
+ // returns this ^-------^
4350
+ const name = useComponentName()
4351
+
4352
+ return <success>{{ name }}</success>
4353
+ }
4354
+ ```
4355
+
3499
4356
  ## Issues
3500
4357
  If you find a bug or have a suggestion, please file an issue on [GitHub](https://github.com/d8corp/innet-server/issues).
3501
4358