@tachybase/plugin-data-source-common 0.23.8
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/.turbo/turbo-build.log +14 -0
- package/LICENSE +201 -0
- package/README.md +1 -0
- package/client.d.ts +2 -0
- package/client.js +1 -0
- package/dist/client/features/rest-api/collection-add/AddCollection.d.ts +2 -0
- package/dist/client/features/rest-api/collection-add/AddCollectionAction.component.d.ts +2 -0
- package/dist/client/features/rest-api/collection-add/CreateCollection.schema.d.ts +389 -0
- package/dist/client/features/rest-api/collection-add/CreateCollection.view.d.ts +2 -0
- package/dist/client/features/rest-api/collection-add/PreviewComponent.d.ts +2 -0
- package/dist/client/features/rest-api/collection-add/PreviewFields.d.ts +5 -0
- package/dist/client/features/rest-api/collection-add/RequestActionItems.provider.d.ts +2 -0
- package/dist/client/features/rest-api/collection-add/getSchemaRequestAction.d.ts +57 -0
- package/dist/client/features/rest-api/collection-add/useActionCreateCollection.d.ts +3 -0
- package/dist/client/features/rest-api/collection-delete/DeleteCollection.d.ts +2 -0
- package/dist/client/features/rest-api/collection-delete/DeleteCollectionAction.component.d.ts +5 -0
- package/dist/client/features/rest-api/collection-delete/useBulkDestroyActionAndRefreshCM.d.ts +3 -0
- package/dist/client/features/rest-api/collection-delete/useDestroyActionAndRefreshCM.d.ts +3 -0
- package/dist/client/features/rest-api/collection-edit/EditCollection.d.ts +2 -0
- package/dist/client/features/rest-api/collection-edit/EditCollectionAction.component.d.ts +2 -0
- package/dist/client/features/rest-api/collection-edit/EditCollectionForm.view.d.ts +2 -0
- package/dist/client/features/rest-api/collection-edit/useActionEditCollection.d.ts +3 -0
- package/dist/client/features/rest-api/constants/mapListve.d.ts +7 -0
- package/dist/client/features/rest-api/constants/requestHeaderList.d.ts +4 -0
- package/dist/client/features/rest-api/contexts/RequestForm.context.d.ts +11 -0
- package/dist/client/features/rest-api/contexts/ResponseInfo.context.d.ts +12 -0
- package/dist/client/features/rest-api/form-data-source/DataSourceSettingsForm.d.ts +2 -0
- package/dist/client/features/rest-api/form-data-source/DataSourceSettingsForm.schema.d.ts +172 -0
- package/dist/client/features/rest-api/kit.d.ts +4 -0
- package/dist/client/features/rest-api/request-configs/debug-area/Debug.provider.d.ts +2 -0
- package/dist/client/features/rest-api/request-configs/debug-area/Debug.schema.d.ts +164 -0
- package/dist/client/features/rest-api/request-configs/debug-area/Debug.view.d.ts +2 -0
- package/dist/client/features/rest-api/request-configs/debug-area/DebugComponent.d.ts +2 -0
- package/dist/client/features/rest-api/request-configs/debug-area/components/AlertError.d.ts +2 -0
- package/dist/client/features/rest-api/request-configs/debug-area/components/DebugResponse.schema.d.ts +21 -0
- package/dist/client/features/rest-api/request-configs/debug-area/components/DebugResponse.view.d.ts +2 -0
- package/dist/client/features/rest-api/request-configs/debug-area/components/DebugResponseTabs.d.ts +2 -0
- package/dist/client/features/rest-api/request-configs/debug-area/components/DebugResponseTabs.items.d.ts +6 -0
- package/dist/client/features/rest-api/request-configs/debug-area/components/ExtractFieldMetadata.d.ts +2 -0
- package/dist/client/features/rest-api/request-configs/debug-area/components/Headers.schema.d.ts +60 -0
- package/dist/client/features/rest-api/request-configs/debug-area/components/RequestHeaders.view.d.ts +2 -0
- package/dist/client/features/rest-api/request-configs/debug-area/components/ResponseBody.schema.d.ts +21 -0
- package/dist/client/features/rest-api/request-configs/debug-area/components/ResponseBody.view.d.ts +2 -0
- package/dist/client/features/rest-api/request-configs/debug-area/components/ResponseHeaders.view.d.ts +2 -0
- package/dist/client/features/rest-api/request-configs/debug-area/components/ResponseTab.d.ts +2 -0
- package/dist/client/features/rest-api/request-configs/debug-area/components/ResponseTab.item.d.ts +6 -0
- package/dist/client/features/rest-api/request-configs/debug-area/components/TooltipContainer.d.ts +2 -0
- package/dist/client/features/rest-api/request-configs/debug-area/schemas/getSchemaAction.d.ts +88 -0
- package/dist/client/features/rest-api/request-configs/debug-area/schemas/getSchemaParam.d.ts +1 -0
- package/dist/client/features/rest-api/request-configs/debug-area/scopes/useCancelAction.d.ts +3 -0
- package/dist/client/features/rest-api/request-configs/debug-area/scopes/useDebugAction.d.ts +3 -0
- package/dist/client/features/rest-api/request-configs/method-path/FieldMethod.schema.d.ts +23 -0
- package/dist/client/features/rest-api/request-configs/method-path/FieldMethod.view.d.ts +2 -0
- package/dist/client/features/rest-api/request-configs/method-path/FieldPath.schema.d.ts +35 -0
- package/dist/client/features/rest-api/request-configs/method-path/FieldPath.view.d.ts +2 -0
- package/dist/client/features/rest-api/request-configs/method-path/MethodPathComponent.d.ts +2 -0
- package/dist/client/features/rest-api/request-configs/request-tab/RequestBody.schema.d.ts +124 -0
- package/dist/client/features/rest-api/request-configs/request-tab/RequestBody.view.d.ts +2 -0
- package/dist/client/features/rest-api/request-configs/request-tab/RequestHeaders.schema.d.ts +77 -0
- package/dist/client/features/rest-api/request-configs/request-tab/RequestHeaders.view.d.ts +2 -0
- package/dist/client/features/rest-api/request-configs/request-tab/RequestParams.schema.d.ts +81 -0
- package/dist/client/features/rest-api/request-configs/request-tab/RequestParams.view.d.ts +2 -0
- package/dist/client/features/rest-api/request-configs/request-tab/RequestTab.d.ts +2 -0
- package/dist/client/features/rest-api/request-configs/request-tab/RequestTab.items.d.ts +6 -0
- package/dist/client/features/rest-api/request-configs/request-tab/debounceClick.util.d.ts +2 -0
- package/dist/client/features/rest-api/request-configs/request-transformer/ResponseTransformerComponent.d.ts +2 -0
- package/dist/client/features/rest-api/scopes/useVariableOptions.d.ts +1 -0
- package/dist/client/features/rest-api/utils/filterObjectWithMethodAndPath.d.ts +1 -0
- package/dist/client/features/rest-api/utils/filterTree.d.ts +1 -0
- package/dist/client/features/rest-api/utils/getRequestActions.d.ts +1 -0
- package/dist/client/features/rest-api/utils/getRequestValues.d.ts +1 -0
- package/dist/client/features/rest-api/utils/responseTransformerAe.d.ts +4 -0
- package/dist/client/features/rest-api/utils/setFormValue.d.ts +1 -0
- package/dist/client/forms/msql.d.ts +2 -0
- package/dist/client/forms/pg.d.ts +2 -0
- package/dist/client/index.d.ts +6 -0
- package/dist/client/index.js +86 -0
- package/dist/client/locale.d.ts +4 -0
- package/dist/externalVersion.js +16 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +39 -0
- package/dist/locale/en-US.json +32 -0
- package/dist/locale/zh-CN.json +32 -0
- package/dist/server/http/plugin.d.ts +10 -0
- package/dist/server/http/plugin.js +78 -0
- package/dist/server/http/services/http-api-repository.d.ts +18 -0
- package/dist/server/http/services/http-api-repository.js +125 -0
- package/dist/server/http/services/http-collection-manager.d.ts +6 -0
- package/dist/server/http/services/http-collection-manager.js +36 -0
- package/dist/server/http/services/http-collection.d.ts +7 -0
- package/dist/server/http/services/http-collection.js +270 -0
- package/dist/server/http/services/http-data-source.d.ts +10 -0
- package/dist/server/http/services/http-data-source.js +67 -0
- package/dist/server/http/services/http-request-builder.d.ts +3 -0
- package/dist/server/http/services/http-request-builder.js +30 -0
- package/dist/server/http/services/transform-response.d.ts +1 -0
- package/dist/server/http/services/transform-response.js +185 -0
- package/dist/server/http/services/type-interface-map.d.ts +182 -0
- package/dist/server/http/services/type-interface-map.js +246 -0
- package/dist/server/http/services/utils.d.ts +2 -0
- package/dist/server/http/services/utils.js +49 -0
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.js +33 -0
- package/dist/server/mysql/database-introspector.d.ts +27 -0
- package/dist/server/mysql/database-introspector.js +254 -0
- package/dist/server/mysql/mysql-collection-manager.d.ts +3 -0
- package/dist/server/mysql/mysql-collection-manager.js +29 -0
- package/dist/server/mysql/mysql-data-source.d.ts +7 -0
- package/dist/server/mysql/mysql-data-source.js +111 -0
- package/dist/server/mysql/type-interface-map.d.ts +193 -0
- package/dist/server/mysql/type-interface-map.js +257 -0
- package/dist/server/pg/database-introspector.d.ts +54 -0
- package/dist/server/pg/database-introspector.js +286 -0
- package/dist/server/pg/postgres-collection-manager.d.ts +3 -0
- package/dist/server/pg/postgres-collection-manager.js +29 -0
- package/dist/server/pg/postgres-data-source.d.ts +8 -0
- package/dist/server/pg/postgres-data-source.js +127 -0
- package/dist/server/pg/type-interface-map.d.ts +183 -0
- package/dist/server/pg/type-interface-map.js +243 -0
- package/dist/server/pg/utils.d.ts +1 -0
- package/dist/server/pg/utils.js +48 -0
- package/dist/server/plugin.d.ts +13 -0
- package/dist/server/plugin.js +60 -0
- package/package.json +32 -0
- package/server.d.ts +2 -0
- package/server.js +1 -0
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var http_collection_exports = {};
|
|
29
|
+
__export(http_collection_exports, {
|
|
30
|
+
HttpCollection: () => HttpCollection
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(http_collection_exports);
|
|
33
|
+
var import_data_source = require("@tachybase/data-source");
|
|
34
|
+
var import_utils = require("@tachybase/utils");
|
|
35
|
+
var import_axios = __toESM(require("axios"));
|
|
36
|
+
var import_lodash = __toESM(require("lodash"));
|
|
37
|
+
var import_transform_response = require("./transform-response");
|
|
38
|
+
var import_type_interface_map = require("./type-interface-map");
|
|
39
|
+
var import_utils2 = require("./utils");
|
|
40
|
+
function compileTemplate(template, context) {
|
|
41
|
+
return (0, import_utils.parse)(template)(context);
|
|
42
|
+
}
|
|
43
|
+
function mergeRequestOptions(options) {
|
|
44
|
+
const { baseRequestConfig, actionOptions, templateContext = {} } = options;
|
|
45
|
+
const rawConfig = {
|
|
46
|
+
method: actionOptions.method,
|
|
47
|
+
url: actionOptions.path,
|
|
48
|
+
baseURL: baseRequestConfig.baseUrl,
|
|
49
|
+
headers: {
|
|
50
|
+
...baseRequestConfig.headers,
|
|
51
|
+
...actionOptions.headers
|
|
52
|
+
},
|
|
53
|
+
params: actionOptions.params,
|
|
54
|
+
data: null
|
|
55
|
+
};
|
|
56
|
+
if (actionOptions.body) {
|
|
57
|
+
rawConfig.data = actionOptions.body;
|
|
58
|
+
}
|
|
59
|
+
if (actionOptions.contentType) {
|
|
60
|
+
rawConfig.headers["Content-Type"] = actionOptions.contentType;
|
|
61
|
+
}
|
|
62
|
+
if (actionOptions.contentType === "application/x-www-form-urlencoded") {
|
|
63
|
+
rawConfig.data = (0, import_utils2.normalizeRequestOptionsKey)(rawConfig.data);
|
|
64
|
+
}
|
|
65
|
+
const config = compileTemplate(rawConfig, templateContext);
|
|
66
|
+
if (templateContext.values && !rawConfig.data) {
|
|
67
|
+
config.data = templateContext.values;
|
|
68
|
+
}
|
|
69
|
+
if (actionOptions.contentType === "application/x-www-form-urlencoded") {
|
|
70
|
+
config.data = new URLSearchParams(config.data).toString();
|
|
71
|
+
}
|
|
72
|
+
return config;
|
|
73
|
+
}
|
|
74
|
+
function buildTemplateContext(options) {
|
|
75
|
+
const { dataSourceRequestConfig, templateContext, debugVars = {} } = options;
|
|
76
|
+
const variables = {
|
|
77
|
+
dataSourceVariables: dataSourceRequestConfig.variables || {}
|
|
78
|
+
};
|
|
79
|
+
for (const variableKey of Object.keys(variables)) {
|
|
80
|
+
templateContext[variableKey] = variables[variableKey];
|
|
81
|
+
}
|
|
82
|
+
for (const variableKey of Object.keys(debugVars)) {
|
|
83
|
+
if (variableKey === "body") {
|
|
84
|
+
import_lodash.default.set(templateContext, ["request", "body"], debugVars[variableKey]);
|
|
85
|
+
} else {
|
|
86
|
+
import_lodash.default.set(templateContext, ["request", "params", variableKey], debugVars[variableKey]);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (templateContext.values && !import_lodash.default.get(templateContext, "request.body")) {
|
|
90
|
+
templateContext.body = templateContext.values;
|
|
91
|
+
import_lodash.default.set(templateContext, "request.body", templateContext.values);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function rawTypeToFieldType(rawType, exampleValue) {
|
|
95
|
+
const typeInfers = {
|
|
96
|
+
string: "string",
|
|
97
|
+
number: () => {
|
|
98
|
+
if (Number.isInteger(exampleValue)) {
|
|
99
|
+
return "integer";
|
|
100
|
+
}
|
|
101
|
+
return "float";
|
|
102
|
+
},
|
|
103
|
+
boolean: "boolean",
|
|
104
|
+
object: () => {
|
|
105
|
+
if (import_lodash.default.isNull(exampleValue)) {
|
|
106
|
+
return ["string", "integer", "float", "boolean", "json"];
|
|
107
|
+
}
|
|
108
|
+
return "json";
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
const inferType = typeInfers[rawType];
|
|
112
|
+
if (typeof inferType === "function") {
|
|
113
|
+
return inferType();
|
|
114
|
+
}
|
|
115
|
+
return inferType;
|
|
116
|
+
}
|
|
117
|
+
function getInterfaceOptionsByType(type) {
|
|
118
|
+
const interfaceConfig = import_type_interface_map.typeInterfaceMap[type];
|
|
119
|
+
if (typeof interfaceConfig === "function") {
|
|
120
|
+
return interfaceConfig();
|
|
121
|
+
} else {
|
|
122
|
+
return interfaceConfig;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
function parseResponseToFieldsOptions(responseData) {
|
|
126
|
+
let objectItem = {};
|
|
127
|
+
if (Array.isArray(responseData)) {
|
|
128
|
+
objectItem = responseData[0];
|
|
129
|
+
}
|
|
130
|
+
if (import_lodash.default.isPlainObject(responseData)) {
|
|
131
|
+
objectItem = responseData;
|
|
132
|
+
}
|
|
133
|
+
return Object.keys(objectItem).map((key) => {
|
|
134
|
+
const rawType = typeof objectItem[key];
|
|
135
|
+
const inferredFieldType = rawTypeToFieldType(rawType, objectItem[key]);
|
|
136
|
+
let fieldOptions = {
|
|
137
|
+
name: key,
|
|
138
|
+
rawType,
|
|
139
|
+
title: key,
|
|
140
|
+
field: key
|
|
141
|
+
};
|
|
142
|
+
let fieldType = inferredFieldType;
|
|
143
|
+
if (Array.isArray(inferredFieldType)) {
|
|
144
|
+
fieldType = inferredFieldType[0];
|
|
145
|
+
fieldOptions["possibleTypes"] = inferredFieldType;
|
|
146
|
+
}
|
|
147
|
+
fieldOptions = {
|
|
148
|
+
...fieldOptions,
|
|
149
|
+
type: fieldType,
|
|
150
|
+
...getInterfaceOptionsByType(fieldType)
|
|
151
|
+
};
|
|
152
|
+
import_lodash.default.set(fieldOptions, "uiSchema.title", key);
|
|
153
|
+
return fieldOptions;
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
function guessFilterTargetKeyName(fields) {
|
|
157
|
+
const keyNames = ["id", "nodeId", "node_id"];
|
|
158
|
+
for (const keyName of keyNames) {
|
|
159
|
+
if (fields.find((field) => field.field === keyName)) {
|
|
160
|
+
return keyName;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return void 0;
|
|
164
|
+
}
|
|
165
|
+
class HttpCollection extends import_data_source.Collection {
|
|
166
|
+
availableActions() {
|
|
167
|
+
const allActionOptions = this.options.actions || {};
|
|
168
|
+
const actions = ["list", "get", "create", "update", "destroy"];
|
|
169
|
+
return actions.filter((action) => allActionOptions[action]);
|
|
170
|
+
}
|
|
171
|
+
// send http request to external server
|
|
172
|
+
static async runAction(options) {
|
|
173
|
+
const { dataSource, actionOptions, templateContext = {}, parseField, debugVars, runAsDebug } = options;
|
|
174
|
+
(0, import_utils2.normalizeRequestOptions)(actionOptions);
|
|
175
|
+
const dataSourceRequestConfig = dataSource.requestConfig();
|
|
176
|
+
buildTemplateContext({
|
|
177
|
+
dataSourceRequestConfig,
|
|
178
|
+
templateContext,
|
|
179
|
+
debugVars
|
|
180
|
+
});
|
|
181
|
+
const requestConfig = mergeRequestOptions({
|
|
182
|
+
baseRequestConfig: dataSourceRequestConfig,
|
|
183
|
+
actionOptions,
|
|
184
|
+
templateContext
|
|
185
|
+
});
|
|
186
|
+
if (runAsDebug) {
|
|
187
|
+
requestConfig.validateStatus = () => true;
|
|
188
|
+
}
|
|
189
|
+
const handleResponse = (response2) => {
|
|
190
|
+
if (parseField && response2.transformedResponse.data) {
|
|
191
|
+
response2.fields = parseResponseToFieldsOptions(response2.transformedResponse.data);
|
|
192
|
+
const filterTargetKeyName = guessFilterTargetKeyName(response2.fields);
|
|
193
|
+
if (filterTargetKeyName) {
|
|
194
|
+
response2.filterTargetKey = filterTargetKeyName;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (options.runAsDebug && !response2.responseValidationErrorMessage) {
|
|
198
|
+
response2.debugBody = response2.transformedResponse;
|
|
199
|
+
}
|
|
200
|
+
return response2;
|
|
201
|
+
};
|
|
202
|
+
const response = await import_axios.default.request(requestConfig).catch((error) => {
|
|
203
|
+
throw new Error(`Failed to request external http datasource`, { cause: error });
|
|
204
|
+
});
|
|
205
|
+
const clientRequest = response.request;
|
|
206
|
+
const baseResponse = {
|
|
207
|
+
rawResponse: {
|
|
208
|
+
status: response.status,
|
|
209
|
+
headers: response.headers,
|
|
210
|
+
data: response.data,
|
|
211
|
+
body: response.data,
|
|
212
|
+
request: {
|
|
213
|
+
url: import_axios.default.getUri(requestConfig),
|
|
214
|
+
method: clientRequest.method,
|
|
215
|
+
headers: response.request.getHeaders()
|
|
216
|
+
}
|
|
217
|
+
},
|
|
218
|
+
data: null
|
|
219
|
+
};
|
|
220
|
+
const transformResponse = (baseResponse2, actionOptions2) => {
|
|
221
|
+
const transformer = actionOptions2.responseTransformer;
|
|
222
|
+
baseResponse2.transformedResponse = compileTemplate(transformer, {
|
|
223
|
+
rawResponse: baseResponse2.rawResponse
|
|
224
|
+
});
|
|
225
|
+
try {
|
|
226
|
+
(0, import_transform_response.transformResponseThroughMiddleware)({
|
|
227
|
+
transformedResponse: baseResponse2.transformedResponse,
|
|
228
|
+
actionOptions: actionOptions2,
|
|
229
|
+
templateContext
|
|
230
|
+
});
|
|
231
|
+
} catch (e) {
|
|
232
|
+
if (options.runAsDebug) {
|
|
233
|
+
baseResponse2.responseValidationErrorMessage = e.message;
|
|
234
|
+
} else {
|
|
235
|
+
throw e;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
transformResponse(baseResponse, actionOptions);
|
|
240
|
+
dataSource.logger.debug({
|
|
241
|
+
actionOptions,
|
|
242
|
+
requestConfig,
|
|
243
|
+
baseResponse
|
|
244
|
+
});
|
|
245
|
+
return handleResponse(baseResponse);
|
|
246
|
+
}
|
|
247
|
+
getActionOptions(action) {
|
|
248
|
+
const allActionOptions = this.options.actions || {};
|
|
249
|
+
const actionOption = allActionOptions[action];
|
|
250
|
+
if (!actionOption) {
|
|
251
|
+
throw new Error(`request config of action "${action}" is not set`);
|
|
252
|
+
}
|
|
253
|
+
return actionOption;
|
|
254
|
+
}
|
|
255
|
+
runAction(action, templateContext) {
|
|
256
|
+
const actionOptions = this.getActionOptions(action);
|
|
257
|
+
return HttpCollection.runAction({
|
|
258
|
+
actionOptions: {
|
|
259
|
+
...actionOptions,
|
|
260
|
+
type: action
|
|
261
|
+
},
|
|
262
|
+
dataSource: this.collectionManager.dataSource,
|
|
263
|
+
templateContext
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
268
|
+
0 && (module.exports = {
|
|
269
|
+
HttpCollection
|
|
270
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { DataSource } from '@tachybase/data-source';
|
|
2
|
+
import { HttpCollectionManager } from './http-collection-manager';
|
|
3
|
+
export declare class HttpDataSource extends DataSource {
|
|
4
|
+
load(options?: {
|
|
5
|
+
localData?: any;
|
|
6
|
+
}): Promise<void>;
|
|
7
|
+
createCollectionManager(options: any): HttpCollectionManager;
|
|
8
|
+
requestConfig(): Pick<any, string>;
|
|
9
|
+
publicOptions(): Pick<any, "variables" | "baseUrl">;
|
|
10
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var http_data_source_exports = {};
|
|
29
|
+
__export(http_data_source_exports, {
|
|
30
|
+
HttpDataSource: () => HttpDataSource
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(http_data_source_exports);
|
|
33
|
+
var import_data_source = require("@tachybase/data-source");
|
|
34
|
+
var import_lodash = __toESM(require("lodash"));
|
|
35
|
+
var import_http_api_repository = require("./http-api-repository");
|
|
36
|
+
var import_http_collection_manager = require("./http-collection-manager");
|
|
37
|
+
var import_utils = require("./utils");
|
|
38
|
+
class HttpDataSource extends import_data_source.DataSource {
|
|
39
|
+
async load(options = {}) {
|
|
40
|
+
const { localData } = options;
|
|
41
|
+
for (const collectionName of Object.keys(localData)) {
|
|
42
|
+
this.collectionManager.defineCollection(localData[collectionName]);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
createCollectionManager(options) {
|
|
46
|
+
const collectionManager = new import_http_collection_manager.HttpCollectionManager({
|
|
47
|
+
dataSource: this
|
|
48
|
+
});
|
|
49
|
+
collectionManager.registerRepositories({
|
|
50
|
+
Repository: import_http_api_repository.HttpApiRepository
|
|
51
|
+
});
|
|
52
|
+
return collectionManager;
|
|
53
|
+
}
|
|
54
|
+
requestConfig() {
|
|
55
|
+
const configKeys = ["baseUrl", "headers", "variables", "timeout", "responseType"];
|
|
56
|
+
const config = import_lodash.default.pick(this.options, configKeys);
|
|
57
|
+
(0, import_utils.normalizeRequestOptions)(config);
|
|
58
|
+
return config;
|
|
59
|
+
}
|
|
60
|
+
publicOptions() {
|
|
61
|
+
return import_lodash.default.pick(this.options, ["baseUrl", "variables"]);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
65
|
+
0 && (module.exports = {
|
|
66
|
+
HttpDataSource
|
|
67
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var http_request_builder_exports = {};
|
|
19
|
+
__export(http_request_builder_exports, {
|
|
20
|
+
HttpRequestBuilder: () => HttpRequestBuilder
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(http_request_builder_exports);
|
|
23
|
+
class HttpRequestBuilder {
|
|
24
|
+
constructor(dataSource) {
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
28
|
+
0 && (module.exports = {
|
|
29
|
+
HttpRequestBuilder
|
|
30
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function transformResponseThroughMiddleware(options: any): void;
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var transform_response_exports = {};
|
|
29
|
+
__export(transform_response_exports, {
|
|
30
|
+
transformResponseThroughMiddleware: () => transformResponseThroughMiddleware
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(transform_response_exports);
|
|
33
|
+
var import_database = require("@tachybase/database");
|
|
34
|
+
var import_lodash = __toESM(require("lodash"));
|
|
35
|
+
const middlewares = {
|
|
36
|
+
list: [
|
|
37
|
+
function validateListActionTransformedResponse(options) {
|
|
38
|
+
const { transformedResponse } = options;
|
|
39
|
+
const dataItem = import_lodash.default.get(transformedResponse, "data");
|
|
40
|
+
if (!Array.isArray(dataItem)) {
|
|
41
|
+
throw new Error(
|
|
42
|
+
`The transformed response for the list action should be an array. got ${getValueType(dataItem)}`
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
if (!dataItem.every((item) => import_lodash.default.isPlainObject(item))) {
|
|
46
|
+
throw new Error("Every item in the transformed response should be an object");
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
function handleFilter(options) {
|
|
50
|
+
if (checkVariablesAreUsed(options.actionOptions, ["request.params.filter"])) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const filter = import_lodash.default.get(options.templateContext, "request.params.filter") || {};
|
|
54
|
+
const { data } = options.transformedResponse;
|
|
55
|
+
options.transformedResponse.data = data.filter((item) => {
|
|
56
|
+
return (0, import_database.filterMatch)(item, filter);
|
|
57
|
+
});
|
|
58
|
+
},
|
|
59
|
+
function handleSort(options) {
|
|
60
|
+
if (checkVariablesAreUsed(options.actionOptions, ["request.params.sort"])) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const sort = import_lodash.default.get(options.templateContext, "request.params.sort") || [];
|
|
64
|
+
if (sort.length === 0) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const { data } = options.transformedResponse;
|
|
68
|
+
options.transformedResponse.data = data.sort((a, b) => {
|
|
69
|
+
for (const sortItem of sort) {
|
|
70
|
+
const isDesc = sortItem.startsWith("-");
|
|
71
|
+
const field = isDesc ? sortItem.slice(1) : sortItem;
|
|
72
|
+
if (a[field] > b[field]) {
|
|
73
|
+
return isDesc ? -1 : 1;
|
|
74
|
+
}
|
|
75
|
+
if (a[field] < b[field]) {
|
|
76
|
+
return isDesc ? 1 : -1;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return 0;
|
|
80
|
+
});
|
|
81
|
+
},
|
|
82
|
+
function handleFullList(options) {
|
|
83
|
+
if (checkVariablesAreUsed(options.actionOptions, ["request.params.page"])) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const page = parseInt(import_lodash.default.get(options.templateContext, "request.params.page", 1));
|
|
87
|
+
const pageSize = parseInt(import_lodash.default.get(options.templateContext, "request.params.pageSize", 20));
|
|
88
|
+
const { data } = options.transformedResponse;
|
|
89
|
+
const start = (page - 1) * pageSize;
|
|
90
|
+
const end = start + pageSize;
|
|
91
|
+
options.transformedResponse.data = data.slice(start, end);
|
|
92
|
+
import_lodash.default.set(options.transformedResponse, "meta.count", data.length);
|
|
93
|
+
import_lodash.default.set(options.transformedResponse, "meta.page", page);
|
|
94
|
+
import_lodash.default.set(options.transformedResponse, "meta.pageSize", pageSize);
|
|
95
|
+
import_lodash.default.set(options.transformedResponse, "meta.totalPage", Math.ceil(data.length / pageSize));
|
|
96
|
+
},
|
|
97
|
+
function handleFields(options) {
|
|
98
|
+
if (checkVariablesAreUsed(options.actionOptions, ["request.params.fields"])) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const fields = import_lodash.default.get(options.templateContext, "request.params.fields") || [];
|
|
102
|
+
if (fields.length === 0) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const { data } = options.transformedResponse;
|
|
106
|
+
options.transformedResponse.data = data.map((item) => {
|
|
107
|
+
return import_lodash.default.pick(item, fields);
|
|
108
|
+
});
|
|
109
|
+
},
|
|
110
|
+
function handleExpect(options) {
|
|
111
|
+
if (checkVariablesAreUsed(options.actionOptions, ["request.params.expect"])) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const expect = import_lodash.default.get(options.templateContext, "request.params.expect") || [];
|
|
115
|
+
if (expect.length === 0) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const { data } = options.transformedResponse;
|
|
119
|
+
options.transformedResponse.data = data.map((item) => {
|
|
120
|
+
return import_lodash.default.omit(item, expect);
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
],
|
|
124
|
+
get: [
|
|
125
|
+
function validateGetActionTransformedResponse(options) {
|
|
126
|
+
const { transformedResponse } = options;
|
|
127
|
+
const dataItem = import_lodash.default.get(transformedResponse, "data");
|
|
128
|
+
if (!import_lodash.default.isPlainObject(dataItem)) {
|
|
129
|
+
throw new Error(
|
|
130
|
+
`The transformed response for the get action should be an object. got ${getValueType(dataItem)}`
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
function handleFields2(options) {
|
|
135
|
+
if (checkVariablesAreUsed(options.actionOptions, ["request.params.fields"])) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
const fields = import_lodash.default.get(options.templateContext, "request.params.fields") || [];
|
|
139
|
+
if (fields.length === 0) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
const { data } = options.transformedResponse;
|
|
143
|
+
options.transformedResponse.data = import_lodash.default.pick(data, fields);
|
|
144
|
+
},
|
|
145
|
+
function handleExpect2(options) {
|
|
146
|
+
if (checkVariablesAreUsed(options.actionOptions, ["request.params.expect"])) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const expect = import_lodash.default.get(options.templateContext, "request.params.expect") || [];
|
|
150
|
+
if (expect.length === 0) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const { data } = options.transformedResponse;
|
|
154
|
+
options.transformedResponse.data = import_lodash.default.omit(data, expect);
|
|
155
|
+
}
|
|
156
|
+
],
|
|
157
|
+
create: [],
|
|
158
|
+
update: [],
|
|
159
|
+
destroy: []
|
|
160
|
+
};
|
|
161
|
+
function transformResponseThroughMiddleware(options) {
|
|
162
|
+
const { actionOptions } = options;
|
|
163
|
+
const { type } = actionOptions;
|
|
164
|
+
const actionMiddlewares = middlewares[type] || [];
|
|
165
|
+
actionMiddlewares.forEach((middleware) => {
|
|
166
|
+
middleware(options);
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
function checkVariablesAreUsed(options, variables) {
|
|
170
|
+
const template = JSON.stringify(options);
|
|
171
|
+
return variables.every((variable) => template.includes(`{{${variable}}}`));
|
|
172
|
+
}
|
|
173
|
+
function getValueType(value) {
|
|
174
|
+
if (Array.isArray(value)) {
|
|
175
|
+
return "array";
|
|
176
|
+
}
|
|
177
|
+
if (import_lodash.default.isPlainObject(value)) {
|
|
178
|
+
return "object";
|
|
179
|
+
}
|
|
180
|
+
return typeof value;
|
|
181
|
+
}
|
|
182
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
183
|
+
0 && (module.exports = {
|
|
184
|
+
transformResponseThroughMiddleware
|
|
185
|
+
});
|