@opra/common 1.0.0-beta.2 → 1.0.0-beta.3
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/browser/index.cjs +5 -5
- package/browser/index.mjs +5 -5
- package/cjs/document/api-document.js +14 -0
- package/cjs/document/common/api-base.js +5 -3
- package/cjs/document/common/document-node.js +6 -2
- package/cjs/document/constants.js +2 -1
- package/cjs/document/decorators/http-controller.decorator.js +1 -1
- package/cjs/document/decorators/http-operation-entity.decorator.js +1 -1
- package/cjs/document/decorators/http-operation.decorator.js +5 -5
- package/cjs/document/decorators/msg-controller.decorator.js +64 -0
- package/cjs/document/decorators/msg-operation.decorator.js +80 -0
- package/cjs/document/factory/api-document.factory.js +9 -3
- package/cjs/document/factory/data-type.factory.js +20 -0
- package/cjs/document/factory/http-api.factory.js +50 -77
- package/cjs/document/factory/msg-api.factory.js +202 -0
- package/cjs/document/http/http-api.js +5 -4
- package/cjs/document/http/http-controller.js +16 -0
- package/cjs/document/index.js +6 -0
- package/cjs/document/msg/msg-api.js +54 -0
- package/cjs/document/msg/msg-controller.js +116 -0
- package/cjs/document/msg/msg-header.js +40 -0
- package/cjs/document/msg/msg-operation-response.js +58 -0
- package/cjs/document/msg/msg-operation.js +80 -0
- package/cjs/enums/index.js +7 -0
- package/cjs/exception/http-errors/forbidden.error.js +1 -1
- package/cjs/exception/http-errors/resource-not.available.error.js +1 -1
- package/cjs/index.js +1 -1
- package/cjs/schema/msg/msg-controller.interface.js +11 -0
- package/cjs/schema/msg/msg-header.interface.js +2 -0
- package/cjs/schema/msg/msg-operation.interface.js +7 -0
- package/cjs/schema/opra-schema.js +3 -0
- package/esm/document/api-document.js +14 -0
- package/esm/document/common/api-base.js +5 -3
- package/esm/document/common/document-node.js +6 -2
- package/esm/document/constants.js +1 -0
- package/esm/document/decorators/http-controller.decorator.js +1 -1
- package/esm/document/decorators/http-operation-entity.decorator.js +1 -1
- package/esm/document/decorators/http-operation.decorator.js +5 -5
- package/esm/document/decorators/msg-controller.decorator.js +60 -0
- package/esm/document/decorators/msg-operation.decorator.js +76 -0
- package/esm/document/factory/api-document.factory.js +9 -3
- package/esm/document/factory/data-type.factory.js +20 -0
- package/esm/document/factory/http-api.factory.js +50 -77
- package/esm/document/factory/msg-api.factory.js +198 -0
- package/esm/document/http/http-api.js +5 -4
- package/esm/document/http/http-controller.js +17 -1
- package/esm/document/index.js +6 -0
- package/esm/document/msg/msg-api.js +50 -0
- package/esm/document/msg/msg-controller.js +113 -0
- package/esm/document/msg/msg-header.js +37 -0
- package/esm/document/msg/msg-operation-response.js +54 -0
- package/esm/document/msg/msg-operation.js +77 -0
- package/esm/enums/index.js +4 -0
- package/esm/exception/http-errors/forbidden.error.js +1 -1
- package/esm/exception/http-errors/resource-not.available.error.js +1 -1
- package/esm/index.js +1 -1
- package/esm/schema/msg/msg-controller.interface.js +8 -0
- package/esm/schema/msg/msg-header.interface.js +1 -0
- package/esm/schema/msg/msg-operation.interface.js +4 -0
- package/esm/schema/opra-schema.js +3 -0
- package/package.json +2 -2
- package/types/document/api-document.d.ts +5 -2
- package/types/document/common/api-base.d.ts +3 -2
- package/types/document/constants.d.ts +1 -0
- package/types/document/decorators/http-operation.decorator.d.ts +3 -3
- package/types/document/decorators/msg-controller.decorator.d.ts +11 -0
- package/types/document/decorators/msg-operation.decorator.d.ts +29 -0
- package/types/document/factory/api-document.factory.d.ts +2 -1
- package/types/document/factory/data-type.factory.d.ts +1 -0
- package/types/document/factory/http-api.factory.d.ts +10 -6
- package/types/document/factory/msg-api.factory.d.ts +40 -0
- package/types/document/http/http-api.d.ts +8 -3
- package/types/document/http/http-controller.d.ts +8 -2
- package/types/document/index.d.ts +8 -0
- package/types/document/msg/msg-api.d.ts +27 -0
- package/types/document/msg/msg-controller.d.ts +89 -0
- package/types/document/msg/msg-header.d.ts +47 -0
- package/types/document/msg/msg-operation-response.d.ts +41 -0
- package/types/document/msg/msg-operation.d.ts +77 -0
- package/types/enums/index.d.ts +4 -0
- package/types/exception/http-errors/forbidden.error.d.ts +1 -1
- package/types/index.d.cts +1 -1
- package/types/index.d.ts +1 -1
- package/types/schema/api-document.interface.d.ts +27 -4
- package/types/schema/msg/msg-controller.interface.d.ts +22 -0
- package/types/schema/msg/msg-header.interface.d.ts +19 -0
- package/types/schema/msg/msg-operation.interface.d.ts +26 -0
- package/types/schema/opra-schema.d.ts +3 -0
- package/cjs/http/index.js +0 -7
- package/esm/http/index.js +0 -4
- package/types/http/index.d.ts +0 -4
- /package/cjs/{http/enums → enums}/http-headers-codes.enum.js +0 -0
- /package/cjs/{http/enums → enums}/http-status-codes.enum.js +0 -0
- /package/cjs/{http/enums → enums}/http-status-messages.js +0 -0
- /package/cjs/{http/enums → enums}/mime-types.enum.js +0 -0
- /package/esm/{http/enums → enums}/http-headers-codes.enum.js +0 -0
- /package/esm/{http/enums → enums}/http-status-codes.enum.js +0 -0
- /package/esm/{http/enums → enums}/http-status-messages.js +0 -0
- /package/esm/{http/enums → enums}/mime-types.enum.js +0 -0
- /package/types/{http/enums → enums}/http-headers-codes.enum.d.ts +0 -0
- /package/types/{http/enums → enums}/http-status-codes.enum.d.ts +0 -0
- /package/types/{http/enums → enums}/http-status-messages.d.ts +0 -0
- /package/types/{http/enums → enums}/mime-types.enum.d.ts +0 -0
|
@@ -9,6 +9,8 @@ const data_type_map_js_1 = require("./common/data-type-map.js");
|
|
|
9
9
|
const document_element_js_1 = require("./common/document-element.js");
|
|
10
10
|
const constants_js_1 = require("./constants.js");
|
|
11
11
|
const data_type_js_1 = require("./data-type/data-type.js");
|
|
12
|
+
const http_api_js_1 = require("./http/http-api.js");
|
|
13
|
+
const msg_api_js_1 = require("./msg/msg-api.js");
|
|
12
14
|
/**
|
|
13
15
|
*
|
|
14
16
|
* @class ApiDocument
|
|
@@ -44,6 +46,18 @@ class ApiDocument extends document_element_js_1.DocumentElement {
|
|
|
44
46
|
return d;
|
|
45
47
|
}
|
|
46
48
|
}
|
|
49
|
+
get httpApi() {
|
|
50
|
+
if (!(this.api && this.api instanceof http_api_js_1.HttpApi)) {
|
|
51
|
+
throw new TypeError('The document do not contains HttpApi instance');
|
|
52
|
+
}
|
|
53
|
+
return this.api;
|
|
54
|
+
}
|
|
55
|
+
get msgApi() {
|
|
56
|
+
if (!(this.api && this.api instanceof msg_api_js_1.MsgApi)) {
|
|
57
|
+
throw new TypeError('The document do not contains MsgApi instance');
|
|
58
|
+
}
|
|
59
|
+
return this.api;
|
|
60
|
+
}
|
|
47
61
|
toJSON() {
|
|
48
62
|
return this.export();
|
|
49
63
|
}
|
|
@@ -5,13 +5,15 @@ const index_js_1 = require("../../helpers/index.js");
|
|
|
5
5
|
const constants_js_1 = require("../constants.js");
|
|
6
6
|
const document_element_js_1 = require("./document-element.js");
|
|
7
7
|
class ApiBase extends document_element_js_1.DocumentElement {
|
|
8
|
-
constructor(
|
|
9
|
-
super(owner);
|
|
8
|
+
constructor(init) {
|
|
9
|
+
super(init.owner);
|
|
10
10
|
this.name = 'OpraApi';
|
|
11
|
+
this.name = init.name;
|
|
12
|
+
this.description = init.description;
|
|
11
13
|
}
|
|
12
14
|
toJSON() {
|
|
13
15
|
return (0, index_js_1.omitUndefined)({
|
|
14
|
-
|
|
16
|
+
transport: this.transport,
|
|
15
17
|
name: this.name,
|
|
16
18
|
description: this.description,
|
|
17
19
|
});
|
|
@@ -50,8 +50,12 @@ class DocumentNode {
|
|
|
50
50
|
const metadata = nameOrCtor[constants_js_1.DATATYPE_METADATA];
|
|
51
51
|
name = metadata?.name;
|
|
52
52
|
}
|
|
53
|
-
if (
|
|
54
|
-
|
|
53
|
+
if (!name) {
|
|
54
|
+
if (nameOrCtor && typeof nameOrCtor === 'string')
|
|
55
|
+
name = nameOrCtor;
|
|
56
|
+
else if (typeof nameOrCtor === 'function')
|
|
57
|
+
name = nameOrCtor.name;
|
|
58
|
+
}
|
|
55
59
|
throw new TypeError(`Unknown data type` + (name ? ' (' + name + ')' : ''));
|
|
56
60
|
}
|
|
57
61
|
getDataTypeNameWithNs(dataType) {
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.kTypeNSMap = exports.kCtorMap = exports.kDataTypeMap = exports.EXTRACT_TYPENAME_PATTERN = exports.CLASS_NAME_PATTERN = exports.NAMESPACE_PATTERN = exports.BUILTIN = exports.DECORATOR = exports.ENCODER = exports.DECODER = exports.HTTP_CONTROLLER_METADATA = exports.DATATYPE_METADATA = void 0;
|
|
3
|
+
exports.kTypeNSMap = exports.kCtorMap = exports.kDataTypeMap = exports.EXTRACT_TYPENAME_PATTERN = exports.CLASS_NAME_PATTERN = exports.NAMESPACE_PATTERN = exports.BUILTIN = exports.DECORATOR = exports.ENCODER = exports.DECODER = exports.MSG_CONTROLLER_METADATA = exports.HTTP_CONTROLLER_METADATA = exports.DATATYPE_METADATA = void 0;
|
|
4
4
|
exports.DATATYPE_METADATA = Symbol.for('opra.type.metadata');
|
|
5
5
|
exports.HTTP_CONTROLLER_METADATA = Symbol('opra.http-controller.metadata');
|
|
6
|
+
exports.MSG_CONTROLLER_METADATA = Symbol('opra.msg-controller.metadata');
|
|
6
7
|
exports.DECODER = Symbol.for('opra.type.decoder');
|
|
7
8
|
exports.ENCODER = Symbol('opra.type.encoder');
|
|
8
9
|
exports.DECORATOR = Symbol.for('DECORATOR');
|
|
@@ -31,7 +31,7 @@ function HttpControllerDecoratorFactory(options) {
|
|
|
31
31
|
}, { deep: true });
|
|
32
32
|
Reflect.defineMetadata(constants_js_1.HTTP_CONTROLLER_METADATA, metadata, target);
|
|
33
33
|
for (const fn of decoratorChain)
|
|
34
|
-
fn(metadata);
|
|
34
|
+
fn(metadata, target);
|
|
35
35
|
Reflect.defineMetadata(constants_js_1.HTTP_CONTROLLER_METADATA, metadata, target);
|
|
36
36
|
};
|
|
37
37
|
/**
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const index_js_1 = require("../../enums/index.js");
|
|
3
4
|
const filter_rules_js_1 = require("../../filter/filter-rules.js");
|
|
4
|
-
const index_js_1 = require("../../http/index.js");
|
|
5
5
|
const constants_js_1 = require("../constants.js");
|
|
6
6
|
const index_js_2 = require("../data-type/extended-types/index.js");
|
|
7
7
|
const index_js_3 = require("../data-type/primitive-types/index.js");
|
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.HttpOperationDecoratorFactory = HttpOperationDecoratorFactory;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const lodash_omit_1 = tslib_1.__importDefault(require("lodash.omit"));
|
|
6
|
-
const index_js_1 = require("../../
|
|
6
|
+
const index_js_1 = require("../../enums/index.js");
|
|
7
7
|
const index_js_2 = require("../../schema/index.js");
|
|
8
8
|
const constants_js_1 = require("../constants.js");
|
|
9
9
|
function HttpOperationDecoratorFactory(decoratorChain, options) {
|
|
@@ -17,13 +17,13 @@ function HttpOperationDecoratorFactory(decoratorChain, options) {
|
|
|
17
17
|
kind: index_js_2.OpraSchema.HttpOperation.Kind,
|
|
18
18
|
...(0, lodash_omit_1.default)(options, ['kind']),
|
|
19
19
|
};
|
|
20
|
-
const
|
|
20
|
+
const controllerMetadata = (Reflect.getOwnMetadata(constants_js_1.HTTP_CONTROLLER_METADATA, target.constructor) ||
|
|
21
21
|
{});
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
controllerMetadata.operations = controllerMetadata.operations || {};
|
|
23
|
+
controllerMetadata.operations[propertyKey] = operationMetadata;
|
|
24
24
|
for (const fn of decoratorChain)
|
|
25
25
|
fn(operationMetadata);
|
|
26
|
-
Reflect.defineMetadata(constants_js_1.HTTP_CONTROLLER_METADATA,
|
|
26
|
+
Reflect.defineMetadata(constants_js_1.HTTP_CONTROLLER_METADATA, controllerMetadata, target.constructor);
|
|
27
27
|
});
|
|
28
28
|
/**
|
|
29
29
|
*
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MsgControllerDecoratorFactory = MsgControllerDecoratorFactory;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const lodash_omit_1 = tslib_1.__importDefault(require("lodash.omit"));
|
|
6
|
+
const putil_merge_1 = tslib_1.__importDefault(require("putil-merge"));
|
|
7
|
+
const index_js_1 = require("../../schema/index.js");
|
|
8
|
+
const constants_js_1 = require("../constants.js");
|
|
9
|
+
const CLASS_NAME_PATTERN = /^(.*)(Controller)$/;
|
|
10
|
+
function MsgControllerDecoratorFactory(options) {
|
|
11
|
+
const decoratorChain = [];
|
|
12
|
+
/**
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
const decorator = function (target) {
|
|
16
|
+
let name = options?.name;
|
|
17
|
+
if (!name)
|
|
18
|
+
name = CLASS_NAME_PATTERN.exec(target.name)?.[1] || target.name;
|
|
19
|
+
const metadata = {};
|
|
20
|
+
const baseMetadata = Reflect.getOwnMetadata(constants_js_1.MSG_CONTROLLER_METADATA, Object.getPrototypeOf(target));
|
|
21
|
+
if (baseMetadata)
|
|
22
|
+
(0, putil_merge_1.default)(metadata, baseMetadata, { deep: true });
|
|
23
|
+
const oldMetadata = Reflect.getOwnMetadata(constants_js_1.MSG_CONTROLLER_METADATA, target);
|
|
24
|
+
if (oldMetadata)
|
|
25
|
+
(0, putil_merge_1.default)(metadata, oldMetadata, { deep: true });
|
|
26
|
+
(0, putil_merge_1.default)(metadata, {
|
|
27
|
+
kind: index_js_1.OpraSchema.MsgController.Kind,
|
|
28
|
+
name,
|
|
29
|
+
path: name,
|
|
30
|
+
...(0, lodash_omit_1.default)(options, ['kind', 'name', 'instance', 'endpoints', 'key']),
|
|
31
|
+
}, { deep: true });
|
|
32
|
+
Reflect.defineMetadata(constants_js_1.MSG_CONTROLLER_METADATA, metadata, target);
|
|
33
|
+
for (const fn of decoratorChain)
|
|
34
|
+
fn(metadata);
|
|
35
|
+
Reflect.defineMetadata(constants_js_1.MSG_CONTROLLER_METADATA, metadata, target);
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
*
|
|
39
|
+
*/
|
|
40
|
+
decorator.Header = (name, arg1) => {
|
|
41
|
+
decoratorChain.push((meta) => {
|
|
42
|
+
const paramMeta = typeof arg1 === 'string' || typeof arg1 === 'function'
|
|
43
|
+
? {
|
|
44
|
+
name,
|
|
45
|
+
type: arg1,
|
|
46
|
+
}
|
|
47
|
+
: { ...arg1, name };
|
|
48
|
+
meta.headers = meta.headers || [];
|
|
49
|
+
meta.headers.push(paramMeta);
|
|
50
|
+
});
|
|
51
|
+
return decorator;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
*
|
|
55
|
+
*/
|
|
56
|
+
decorator.UseType = (...type) => {
|
|
57
|
+
decoratorChain.push((meta) => {
|
|
58
|
+
meta.types = meta.types || [];
|
|
59
|
+
meta.types.push(...type);
|
|
60
|
+
});
|
|
61
|
+
return decorator;
|
|
62
|
+
};
|
|
63
|
+
return decorator;
|
|
64
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MsgOperationDecoratorFactory = MsgOperationDecoratorFactory;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const lodash_omit_1 = tslib_1.__importDefault(require("lodash.omit"));
|
|
6
|
+
const index_js_1 = require("../../schema/index.js");
|
|
7
|
+
const constants_js_1 = require("../constants.js");
|
|
8
|
+
const augmentationRegistry = [];
|
|
9
|
+
function MsgOperationDecoratorFactory(decoratorChain, payloadType, options) {
|
|
10
|
+
let inResponse = false;
|
|
11
|
+
/**
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
const decorator = ((target, propertyKey) => {
|
|
15
|
+
if (typeof propertyKey !== 'string')
|
|
16
|
+
throw new TypeError(`Symbol properties can not be decorated`);
|
|
17
|
+
const operationMetadata = {
|
|
18
|
+
kind: index_js_1.OpraSchema.MsgOperation.Kind,
|
|
19
|
+
channel: propertyKey,
|
|
20
|
+
...(0, lodash_omit_1.default)(options, ['kind']),
|
|
21
|
+
payloadType,
|
|
22
|
+
};
|
|
23
|
+
const controllerMetadata = (Reflect.getOwnMetadata(constants_js_1.MSG_CONTROLLER_METADATA, target.constructor) ||
|
|
24
|
+
{});
|
|
25
|
+
controllerMetadata.operations = controllerMetadata.operations || {};
|
|
26
|
+
controllerMetadata.operations[propertyKey] = operationMetadata;
|
|
27
|
+
for (const fn of decoratorChain)
|
|
28
|
+
fn(operationMetadata, target, propertyKey);
|
|
29
|
+
Reflect.defineMetadata(constants_js_1.MSG_CONTROLLER_METADATA, controllerMetadata, target.constructor);
|
|
30
|
+
});
|
|
31
|
+
/**
|
|
32
|
+
*
|
|
33
|
+
*/
|
|
34
|
+
decorator.UseType = (...type) => {
|
|
35
|
+
decoratorChain.push((meta) => {
|
|
36
|
+
meta.types = meta.types || [];
|
|
37
|
+
meta.types.push(...type);
|
|
38
|
+
});
|
|
39
|
+
return decorator;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
*
|
|
43
|
+
*/
|
|
44
|
+
decorator.Header = (name, arg1) => {
|
|
45
|
+
decoratorChain.push((meta) => {
|
|
46
|
+
const headerMetadata = typeof arg1 === 'string' || typeof arg1 === 'function'
|
|
47
|
+
? {
|
|
48
|
+
name,
|
|
49
|
+
type: arg1,
|
|
50
|
+
}
|
|
51
|
+
: { ...arg1, name };
|
|
52
|
+
const subMeta = inResponse ? meta.response : meta;
|
|
53
|
+
if (subMeta.headers) {
|
|
54
|
+
subMeta.headers = subMeta.headers.filter(p => String(p.name) !== String(name));
|
|
55
|
+
}
|
|
56
|
+
else
|
|
57
|
+
subMeta.headers = [];
|
|
58
|
+
subMeta.headers.push(headerMetadata);
|
|
59
|
+
});
|
|
60
|
+
return decorator;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
*
|
|
64
|
+
*/
|
|
65
|
+
decorator.Response = (_payloadType, _options) => {
|
|
66
|
+
decoratorChain.push((meta) => {
|
|
67
|
+
inResponse = true;
|
|
68
|
+
meta.response = {
|
|
69
|
+
..._options,
|
|
70
|
+
payloadType: _payloadType,
|
|
71
|
+
};
|
|
72
|
+
});
|
|
73
|
+
return decorator;
|
|
74
|
+
};
|
|
75
|
+
augmentationRegistry.forEach(fn => fn(decorator, decoratorChain, payloadType, options));
|
|
76
|
+
return decorator;
|
|
77
|
+
}
|
|
78
|
+
MsgOperationDecoratorFactory.augment = function (fn) {
|
|
79
|
+
augmentationRegistry.push(fn);
|
|
80
|
+
};
|
|
@@ -11,6 +11,7 @@ const index_js_3 = require("../data-type/extended-types/index.js");
|
|
|
11
11
|
const index_js_4 = require("../data-type/primitive-types/index.js");
|
|
12
12
|
const data_type_factory_js_1 = require("./data-type.factory.js");
|
|
13
13
|
const http_api_factory_js_1 = require("./http-api.factory.js");
|
|
14
|
+
const msg_api_factory_js_1 = require("./msg-api.factory.js");
|
|
14
15
|
const OPRA_SPEC_URL = 'https://oprajs.com/spec/v' + index_js_2.OpraSchema.SpecVersion;
|
|
15
16
|
/**
|
|
16
17
|
* @class ApiDocumentFactory
|
|
@@ -111,13 +112,18 @@ class ApiDocumentFactory {
|
|
|
111
112
|
}
|
|
112
113
|
if (init.api) {
|
|
113
114
|
await context.enterAsync(`.api`, async () => {
|
|
114
|
-
if (init.api.
|
|
115
|
-
const api = await http_api_factory_js_1.HttpApiFactory.createApi(context,
|
|
115
|
+
if (init.api && init.api.transport === 'http') {
|
|
116
|
+
const api = await http_api_factory_js_1.HttpApiFactory.createApi(context, { ...init.api, owner: document });
|
|
117
|
+
if (api)
|
|
118
|
+
document.api = api;
|
|
119
|
+
}
|
|
120
|
+
else if (init.api && init.api.transport === 'msg') {
|
|
121
|
+
const api = await msg_api_factory_js_1.MsgApiFactory.createApi(context, { ...init.api, owner: document });
|
|
116
122
|
if (api)
|
|
117
123
|
document.api = api;
|
|
118
124
|
}
|
|
119
125
|
else
|
|
120
|
-
context.addError(`Unknown service
|
|
126
|
+
context.addError(`Unknown service transport (${init.api.transport})`);
|
|
121
127
|
});
|
|
122
128
|
}
|
|
123
129
|
document.invalidate();
|
|
@@ -27,6 +27,26 @@ class DataTypeFactory {
|
|
|
27
27
|
return this._createDataType(context, owner, initArgs);
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
|
+
static async resolveDataType(context, owner, v) {
|
|
31
|
+
if (v) {
|
|
32
|
+
const dt = owner.node.findDataType(v);
|
|
33
|
+
if (dt)
|
|
34
|
+
return dt;
|
|
35
|
+
}
|
|
36
|
+
if (typeof v === 'object' || typeof v === 'function') {
|
|
37
|
+
const dt = await DataTypeFactory.createDataType(context, owner, v);
|
|
38
|
+
if (dt)
|
|
39
|
+
return dt;
|
|
40
|
+
}
|
|
41
|
+
if (v) {
|
|
42
|
+
/** To throw not found error */
|
|
43
|
+
const dt = owner.node.getDataType(v);
|
|
44
|
+
/** istanbul ignore next */
|
|
45
|
+
if (dt)
|
|
46
|
+
return dt;
|
|
47
|
+
}
|
|
48
|
+
return owner.node.getDataType('any');
|
|
49
|
+
}
|
|
30
50
|
/**
|
|
31
51
|
*
|
|
32
52
|
* @param context
|
|
@@ -19,35 +19,56 @@ class HttpApiFactory {
|
|
|
19
19
|
/**
|
|
20
20
|
* Generates HttpApi
|
|
21
21
|
* @param context
|
|
22
|
-
* @param document
|
|
23
22
|
* @param init
|
|
24
23
|
*/
|
|
25
|
-
static async createApi(context,
|
|
26
|
-
const api = new http_api_js_1.HttpApi(
|
|
27
|
-
api.name = init.name;
|
|
28
|
-
api.description = init.description;
|
|
29
|
-
api.url = init.url;
|
|
24
|
+
static async createApi(context, init) {
|
|
25
|
+
const api = new http_api_js_1.HttpApi(init);
|
|
30
26
|
if (init.controllers) {
|
|
31
27
|
await context.enterAsync('.controllers', async () => {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
28
|
+
await this._createControllers(context, api, init.controllers);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
return api;
|
|
32
|
+
}
|
|
33
|
+
static async _createControllers(context, parent, controllers) {
|
|
34
|
+
if (Array.isArray(controllers)) {
|
|
35
|
+
let i = 0;
|
|
36
|
+
for (const c of controllers) {
|
|
37
|
+
let r;
|
|
38
|
+
await context.enterAsync(`[${i++}]`, async () => {
|
|
39
|
+
r = await this._resolveControllerMetadata(context, parent, c);
|
|
40
|
+
});
|
|
41
|
+
if (!r)
|
|
42
|
+
continue;
|
|
43
|
+
await context.enterAsync(`[${r.metadata.name}]`, async () => {
|
|
44
|
+
const controller = await this._createController(context, parent, r.metadata, r.instance, r.ctor);
|
|
45
|
+
if (controller) {
|
|
46
|
+
if (parent.controllers.get(controller.name))
|
|
47
|
+
context.addError(`Duplicate controller name (${r.name})`);
|
|
48
|
+
parent.controllers.set(controller.name, controller);
|
|
44
49
|
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
for (const [k, c] of Object.entries(controllers)) {
|
|
55
|
+
await context.enterAsync(`[${k}]`, async () => {
|
|
56
|
+
const r = await this._resolveControllerMetadata(context, parent, c);
|
|
57
|
+
if (!r)
|
|
58
|
+
return;
|
|
59
|
+
const controller = await this._createController(context, parent, {
|
|
60
|
+
...r.metadata,
|
|
61
|
+
name: k,
|
|
62
|
+
}, r.instance, r.ctor);
|
|
63
|
+
if (controller) {
|
|
64
|
+
if (parent.controllers.get(controller.name))
|
|
65
|
+
context.addError(`Duplicate controller name (${k})`);
|
|
66
|
+
parent.controllers.set(controller.name, controller);
|
|
45
67
|
}
|
|
46
68
|
});
|
|
47
69
|
}
|
|
48
|
-
return api;
|
|
49
70
|
}
|
|
50
|
-
static async
|
|
71
|
+
static async _resolveControllerMetadata(context, parent, thunk) {
|
|
51
72
|
if (typeof thunk === 'function' && !(0, index_js_1.isConstructor)(thunk)) {
|
|
52
73
|
thunk = parent instanceof http_controller_js_1.HttpController ? thunk(parent.instance) : thunk();
|
|
53
74
|
}
|
|
@@ -79,12 +100,13 @@ class HttpApiFactory {
|
|
|
79
100
|
}
|
|
80
101
|
if (!metadata)
|
|
81
102
|
return context.addError(`Class "${ctor.name}" is not decorated with HttpController()`);
|
|
82
|
-
|
|
83
|
-
|
|
103
|
+
return { metadata, instance, ctor };
|
|
104
|
+
}
|
|
105
|
+
static async _createController(context, parent, metadata, instance, ctor) {
|
|
106
|
+
if (!metadata.name)
|
|
84
107
|
throw new TypeError(`Controller name required`);
|
|
85
108
|
const controller = new http_controller_js_1.HttpController(parent, {
|
|
86
109
|
...metadata,
|
|
87
|
-
name,
|
|
88
110
|
instance,
|
|
89
111
|
ctor,
|
|
90
112
|
});
|
|
@@ -100,13 +122,7 @@ class HttpApiFactory {
|
|
|
100
122
|
await context.enterAsync(`[${i++}]`, async () => {
|
|
101
123
|
const prmArgs = { ...v };
|
|
102
124
|
await context.enterAsync('.type', async () => {
|
|
103
|
-
|
|
104
|
-
prmArgs.type = controller.node.findDataType(v.type);
|
|
105
|
-
if (!prmArgs.type && typeof v.type === 'object') {
|
|
106
|
-
prmArgs.type = await data_type_factory_js_1.DataTypeFactory.createDataType(context, controller, v.type);
|
|
107
|
-
}
|
|
108
|
-
if (!prmArgs.type)
|
|
109
|
-
prmArgs.type = controller.node.getDataType('any');
|
|
125
|
+
prmArgs.type = await data_type_factory_js_1.DataTypeFactory.resolveDataType(context, controller, v.type);
|
|
110
126
|
});
|
|
111
127
|
const prm = new http_parameter_js_1.HttpParameter(controller, prmArgs);
|
|
112
128
|
controller.parameters.push(prm);
|
|
@@ -127,32 +143,7 @@ class HttpApiFactory {
|
|
|
127
143
|
}
|
|
128
144
|
if (metadata.controllers) {
|
|
129
145
|
await context.enterAsync('.controllers', async () => {
|
|
130
|
-
|
|
131
|
-
let k = 0;
|
|
132
|
-
for (const v of metadata.controllers) {
|
|
133
|
-
await context.enterAsync(`[${k}]`, async () => {
|
|
134
|
-
const r = await this._createController(context, controller, v);
|
|
135
|
-
if (r) {
|
|
136
|
-
if (controller.controllers.get(r.name))
|
|
137
|
-
context.addError(`Duplicate controller name (${r.name})`);
|
|
138
|
-
controller.controllers.set(r.name, r);
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
|
-
k++;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
else {
|
|
145
|
-
for (const [k, v] of Object.entries(metadata.controllers)) {
|
|
146
|
-
await context.enterAsync(`[${k}]`, async () => {
|
|
147
|
-
const r = await this._createController(context, controller, v, k);
|
|
148
|
-
if (r) {
|
|
149
|
-
if (controller.controllers.get(r.name))
|
|
150
|
-
context.addError(`Duplicate controller name (${r.name})`);
|
|
151
|
-
controller.controllers.set(r.name, r);
|
|
152
|
-
}
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
}
|
|
146
|
+
await this._createControllers(context, controller, metadata.controllers);
|
|
156
147
|
});
|
|
157
148
|
}
|
|
158
149
|
return controller;
|
|
@@ -183,13 +174,7 @@ class HttpApiFactory {
|
|
|
183
174
|
await context.enterAsync(`[${i++}]`, async () => {
|
|
184
175
|
const prmArgs = { ...v };
|
|
185
176
|
await context.enterAsync('.type', async () => {
|
|
186
|
-
|
|
187
|
-
prmArgs.type = operation.node.findDataType(v.type);
|
|
188
|
-
if (!prmArgs.type && typeof v.type === 'object') {
|
|
189
|
-
prmArgs.type = await data_type_factory_js_1.DataTypeFactory.createDataType(context, operation, v.type);
|
|
190
|
-
}
|
|
191
|
-
if (!prmArgs.type)
|
|
192
|
-
prmArgs.type = operation.node.getDataType('any');
|
|
177
|
+
prmArgs.type = await data_type_factory_js_1.DataTypeFactory.resolveDataType(context, operation, v.type);
|
|
193
178
|
});
|
|
194
179
|
const prm = new http_parameter_js_1.HttpParameter(operation, prmArgs);
|
|
195
180
|
operation.parameters.push(prm);
|
|
@@ -233,13 +218,7 @@ class HttpApiFactory {
|
|
|
233
218
|
});
|
|
234
219
|
if (metadata.type) {
|
|
235
220
|
await context.enterAsync('.type', async () => {
|
|
236
|
-
|
|
237
|
-
target.type = target.node.findDataType(metadata.type);
|
|
238
|
-
if (!target.type && (typeof metadata.type === 'object' || typeof metadata.type === 'function')) {
|
|
239
|
-
target.type = await data_type_factory_js_1.DataTypeFactory.createDataType(context, target, metadata.type);
|
|
240
|
-
}
|
|
241
|
-
if (!target.type)
|
|
242
|
-
target.type = target.node.getDataType('any');
|
|
221
|
+
target.type = await data_type_factory_js_1.DataTypeFactory.resolveDataType(context, target, metadata.type);
|
|
243
222
|
});
|
|
244
223
|
}
|
|
245
224
|
if (metadata.multipartFields) {
|
|
@@ -272,13 +251,7 @@ class HttpApiFactory {
|
|
|
272
251
|
await context.enterAsync(`[${i++}]`, async () => {
|
|
273
252
|
const prmArgs = { ...v };
|
|
274
253
|
await context.enterAsync('.type', async () => {
|
|
275
|
-
|
|
276
|
-
prmArgs.type = target.node.findDataType(v.type);
|
|
277
|
-
if (!prmArgs.type && typeof v.type === 'object') {
|
|
278
|
-
prmArgs.type = await data_type_factory_js_1.DataTypeFactory.createDataType(context, target, v.type);
|
|
279
|
-
}
|
|
280
|
-
if (!prmArgs.type)
|
|
281
|
-
prmArgs.type = target.node.getDataType('any');
|
|
254
|
+
prmArgs.type = await data_type_factory_js_1.DataTypeFactory.resolveDataType(context, target, v.type);
|
|
282
255
|
});
|
|
283
256
|
const prm = new http_parameter_js_1.HttpParameter(target, prmArgs);
|
|
284
257
|
target.parameters.push(prm);
|