@innet/server 2.0.0-alpha.10 → 2.0.0-alpha.11

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.
package/README.md CHANGED
@@ -477,15 +477,15 @@ You can use `PROTECTION` env to set default protection `value`.
477
477
 
478
478
  *src/app.tsx*
479
479
  ```typescript jsx
480
- import html from './protection.html'
481
-
482
480
  export default (
483
481
  <server>
484
482
  <api>
485
- <protection
486
- value='secret'
487
- html={html}
488
- />
483
+ <protection value='secret'>
484
+ <error
485
+ code='protection'
486
+ status='forbidden'
487
+ />
488
+ </protection>
489
489
  </api>
490
490
  </server>
491
491
  )
@@ -500,15 +500,17 @@ You can use `PROTECTION_MAX_AGE` env to set default `maxAge`.
500
500
 
501
501
  *src/app.tsx*
502
502
  ```typescript jsx
503
- import html from './protection.html'
504
-
505
503
  export default (
506
504
  <server>
507
505
  <api>
508
506
  <protection
509
507
  maxAge={24 * 60 * 60}
510
- html={html}
511
- />
508
+ value='secret'>
509
+ <error
510
+ code='protection'
511
+ status='forbidden'
512
+ />
513
+ </protection>
512
514
  </api>
513
515
  </server>
514
516
  )
@@ -522,15 +524,17 @@ You can use `PROTECTED_IP` env to set default `excludeIp`.
522
524
 
523
525
  *src/app.tsx*
524
526
  ```typescript jsx
525
- import html from './protection.html'
526
-
527
527
  export default (
528
528
  <server>
529
529
  <api>
530
530
  <protection
531
531
  excludeIp='0.0.0.0,127.0.0.0'
532
- html={html}
533
- />
532
+ value='secret'>
533
+ <error
534
+ code='protection'
535
+ status='forbidden'
536
+ />
537
+ </protection>
534
538
  </api>
535
539
  </server>
536
540
  )
@@ -545,15 +549,17 @@ You can use `PROTECTION_COOKIE_KEY` env to set default `cookieKey`.
545
549
 
546
550
  *src/app.tsx*
547
551
  ```typescript jsx
548
- import html from './protection.html'
549
-
550
552
  export default (
551
553
  <server>
552
554
  <api>
553
555
  <protection
554
556
  cookieKey='secret'
555
- html={html}
556
- />
557
+ value='secret'>
558
+ <error
559
+ code='protection'
560
+ status='forbidden'
561
+ />
562
+ </protection>
557
563
  </api>
558
564
  </server>
559
565
  )
@@ -568,15 +574,17 @@ You can use `PROTECTION_SEARCH_KEY` env to set default `searchKey`.
568
574
 
569
575
  *src/app.tsx*
570
576
  ```typescript jsx
571
- import html from './protection.html'
572
-
573
577
  export default (
574
578
  <server>
575
579
  <api>
576
580
  <protection
577
581
  searchKey='secret'
578
- html={html}
579
- />
582
+ value='secret'>
583
+ <error
584
+ code='protection'
585
+ status='forbidden'
586
+ />
587
+ </protection>
580
588
  </api>
581
589
  </server>
582
590
  )
@@ -1150,6 +1158,7 @@ This element MUST be placed inside `<endpoint>`.
1150
1158
  It defines request body for the endpoint.
1151
1159
  `@innet/server` formats and validate the value automatically (real-time).
1152
1160
 
1161
+ *src/app.tsx*
1153
1162
  ```typescript jsx
1154
1163
  return (
1155
1164
  <server>
@@ -1183,6 +1192,105 @@ return (
1183
1192
  This element MUST be placed inside `<endpoint>`.
1184
1193
  It defines response body for the endpoint.
1185
1194
 
1195
+ *src/app.tsx*
1196
+ ```typescript jsx
1197
+ return (
1198
+ <server>
1199
+ <api>
1200
+ <endpoint method='get' path='/settings'>
1201
+ <response>
1202
+ <object />
1203
+ </response>
1204
+ </endpoint>
1205
+ </api>
1206
+ </server>
1207
+ )
1208
+ ```
1209
+
1210
+ #### status
1211
+ A status of the `<response>`.
1212
+ Any [HTTP status code](https://swagger.io/specification/#http-codes) can be used as a number of the property.
1213
+
1214
+ By default, `status` equals `'default'`.
1215
+
1216
+ *src/app.tsx*
1217
+ ```typescript jsx
1218
+ return (
1219
+ <server>
1220
+ <api>
1221
+ <endpoint method='get' path='/settings'>
1222
+ <response status={200}>
1223
+ <object />
1224
+ </response>
1225
+ </endpoint>
1226
+ </api>
1227
+ </server>
1228
+ )
1229
+ ```
1230
+
1231
+ To define a range of response codes, this field MAY contain the uppercase wildcard character `X`.
1232
+ For example, `2XX` represents all response codes between \[200-299].
1233
+ Only the following range definitions are allowed: `1XX`, `2XX`, `3XX`, `4XX` and `5XX`.
1234
+
1235
+ *src/app.tsx*
1236
+ ```typescript jsx
1237
+ return (
1238
+ <server>
1239
+ <api>
1240
+ <endpoint method='get' path='/settings'>
1241
+ <response status='2XX'>
1242
+ <object />
1243
+ </response>
1244
+ </endpoint>
1245
+ </api>
1246
+ </server>
1247
+ )
1248
+ ```
1249
+
1250
+ Many number statuses have a string id you can use on the property.
1251
+
1252
+ *src/app.tsx*
1253
+ ```typescript jsx
1254
+ return (
1255
+ <server>
1256
+ <api>
1257
+ <endpoint method='get' path='/settings'>
1258
+ <response status='notFound'>
1259
+ <object />
1260
+ </response>
1261
+ </endpoint>
1262
+ </api>
1263
+ </server>
1264
+ )
1265
+ ```
1266
+
1267
+ You can use many `<response>` elements in an endpoint.
1268
+
1269
+ *src/app.tsx*
1270
+ ```typescript jsx
1271
+ return (
1272
+ <server>
1273
+ <api>
1274
+ <endpoint method='get' path='/settings'>
1275
+ <response status='2XX'>
1276
+ <object />
1277
+ </response>
1278
+ <response status='4XX'>
1279
+ <object>
1280
+ <field key='error'>
1281
+ <string />
1282
+ </field>
1283
+ <field optional key='data'>
1284
+ <object />
1285
+ </field>
1286
+ </object>
1287
+ </response>
1288
+ </endpoint>
1289
+ </api>
1290
+ </server>
1291
+ )
1292
+ ```
1293
+
1186
1294
  ## Primitive Data
1187
1295
 
1188
1296
  [← back](#index)
package/index.es6.js CHANGED
@@ -12,7 +12,7 @@ export { host } from './plugins/main/host/host.es6.js';
12
12
  export { variable } from './plugins/main/variable/variable.es6.js';
13
13
  export { tag } from './plugins/main/tag/tag.es6.js';
14
14
  export { endpoint } from './plugins/main/endpoint/endpoint.es6.js';
15
- export { response } from './plugins/main/response/response.es6.js';
15
+ export { response, statuses } from './plugins/main/response/response.es6.js';
16
16
  export { request } from './plugins/main/request/request.es6.js';
17
17
  export { param } from './plugins/main/param/param.es6.js';
18
18
  export { body } from './plugins/main/body/body.es6.js';
package/index.js CHANGED
@@ -133,6 +133,7 @@ exports.variable = variable.variable;
133
133
  exports.tag = tag.tag;
134
134
  exports.endpoint = endpoint.endpoint;
135
135
  exports.response = response.response;
136
+ exports.statuses = response.statuses;
136
137
  exports.request = request.request;
137
138
  exports.param = param.param;
138
139
  exports.body = body.body;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@innet/server",
3
- "version": "2.0.0-alpha.10",
3
+ "version": "2.0.0-alpha.11",
4
4
  "description": "Create server-side application with innet",
5
5
  "main": "index.js",
6
6
  "module": "index.es6.js",
@@ -1 +1 @@
1
- export { response } from './response.es6.js';
1
+ export { response, statuses } from './response.es6.js';
@@ -7,3 +7,4 @@ var response = require('./response.js');
7
7
 
8
8
 
9
9
  exports.response = response.response;
10
+ exports.statuses = response.statuses;
@@ -1,4 +1,6 @@
1
1
  import { type HandlerPlugin } from 'innet';
2
+ import { type ErrorStatuses, type RedirectStatuses, type SuccessStatuses } from '../../request';
3
+ export type StatusKey = ErrorStatuses | RedirectStatuses | SuccessStatuses;
2
4
  export interface ResponseProps {
3
5
  /**
4
6
  * A description of the response.
@@ -11,6 +13,7 @@ export interface ResponseProps {
11
13
  * For example, 2XX represents all response codes between [200-299].
12
14
  * Only the following range definitions are allowed: 1XX, 2XX, 3XX, 4XX, and 5XX.
13
15
  * */
14
- status?: number | 'default';
16
+ status?: number | `${1 | 2 | 3 | 4 | 5}XX` | 'default' | StatusKey;
15
17
  }
18
+ export declare const statuses: Record<StatusKey, number>;
16
19
  export declare const response: HandlerPlugin;
@@ -2,18 +2,26 @@ import innet, { useNewHandler } from 'innet';
2
2
  import { useProps, useChildren, useContext } from '@innet/jsx';
3
3
  import '../../../hooks/index.es6.js';
4
4
  import '../../../utils/index.es6.js';
5
+ import '../../request/index.es6.js';
6
+ import { errorStatuses } from '../../request/error/error.es6.js';
7
+ import { redirectStatuses } from '../../request/redirect/redirect.es6.js';
8
+ import { successStatuses } from '../../request/success/success.es6.js';
5
9
  import { useEndpoint, endpointContext } from '../../../hooks/useEndpoint/useEndpoint.es6.js';
6
10
  import { useThrow } from '../../../hooks/useThrow/useThrow.es6.js';
7
11
  import { schemaContext } from '../../../hooks/useSchemaContext/useSchemaContext.es6.js';
8
12
  import { getOrAdd } from '../../../utils/getOrAdd/getOrAdd.es6.js';
9
13
  import { ruleContext } from '../../../hooks/useRule/useRule.es6.js';
10
14
 
15
+ const statuses = Object.assign(Object.assign(Object.assign({}, errorStatuses), redirectStatuses), successStatuses);
11
16
  const response = () => {
12
- const { description = '', status = 'default' } = useProps() || {};
17
+ let { description = '', status = 'default' } = useProps() || {};
13
18
  const { operation, props: { path } } = useEndpoint();
14
19
  const children = useChildren();
15
20
  const handler = useNewHandler();
16
21
  const endpoint = useContext(endpointContext);
22
+ if (status in statuses) {
23
+ status = statuses[status];
24
+ }
17
25
  if (!endpoint) {
18
26
  useThrow('<{type}> MUST be placed in <endpoint> element');
19
27
  }
@@ -41,4 +49,4 @@ const response = () => {
41
49
  innet(children, handler);
42
50
  };
43
51
 
44
- export { response };
52
+ export { response, statuses };
@@ -6,6 +6,10 @@ var innet = require('innet');
6
6
  var jsx = require('@innet/jsx');
7
7
  require('../../../hooks/index.js');
8
8
  require('../../../utils/index.js');
9
+ require('../../request/index.js');
10
+ var error = require('../../request/error/error.js');
11
+ var redirect = require('../../request/redirect/redirect.js');
12
+ var success = require('../../request/success/success.js');
9
13
  var useEndpoint = require('../../../hooks/useEndpoint/useEndpoint.js');
10
14
  var useThrow = require('../../../hooks/useThrow/useThrow.js');
11
15
  var useSchemaContext = require('../../../hooks/useSchemaContext/useSchemaContext.js');
@@ -16,12 +20,16 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
16
20
 
17
21
  var innet__default = /*#__PURE__*/_interopDefaultLegacy(innet);
18
22
 
23
+ const statuses = Object.assign(Object.assign(Object.assign({}, error.errorStatuses), redirect.redirectStatuses), success.successStatuses);
19
24
  const response = () => {
20
- const { description = '', status = 'default' } = jsx.useProps() || {};
25
+ let { description = '', status = 'default' } = jsx.useProps() || {};
21
26
  const { operation, props: { path } } = useEndpoint.useEndpoint();
22
27
  const children = jsx.useChildren();
23
28
  const handler = innet.useNewHandler();
24
29
  const endpoint = jsx.useContext(useEndpoint.endpointContext);
30
+ if (status in statuses) {
31
+ status = statuses[status];
32
+ }
25
33
  if (!endpoint) {
26
34
  useThrow.useThrow('<{type}> MUST be placed in <endpoint> element');
27
35
  }
@@ -50,3 +58,4 @@ const response = () => {
50
58
  };
51
59
 
52
60
  exports.response = response;
61
+ exports.statuses = statuses;
@@ -9,9 +9,10 @@ export declare const redirectStatuses: {
9
9
  temporaryRedirect: number;
10
10
  permanentRedirect: number;
11
11
  };
12
+ export type RedirectStatuses = keyof typeof redirectStatuses;
12
13
  export interface RedirectProps {
13
14
  to: string;
14
15
  encode?: boolean;
15
- status?: number | keyof typeof redirectStatuses;
16
+ status?: number | RedirectStatuses;
16
17
  }
17
18
  export declare const redirect: HandlerPlugin;