@oino-ts/db 0.17.3 → 0.17.5
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/dist/cjs/OINODbApi.js +3 -2
- package/dist/cjs/OINODbParser.js +24 -35
- package/dist/esm/OINODbApi.js +2 -1
- package/dist/esm/OINODbParser.js +25 -36
- package/dist/types/OINODbApi.d.ts +1 -0
- package/dist/types/OINODbParser.d.ts +3 -18
- package/package.json +4 -4
- package/src/OINODbApi.ts +2 -1
- package/src/OINODbParser.ts +25 -36
package/dist/cjs/OINODbApi.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
8
|
exports.OINODbApi = exports.OINODbHtmlTemplate = exports.OINODbApiResult = exports.OINODbApiRequest = void 0;
|
|
9
|
+
const node_buffer_1 = require("node:buffer");
|
|
9
10
|
const index_js_1 = require("./index.js");
|
|
10
11
|
const common_1 = require("@oino-ts/common");
|
|
11
12
|
const hashid_1 = require("@oino-ts/hashid");
|
|
@@ -70,7 +71,7 @@ class OINODbApiRequest extends common_1.OINOHttpRequest {
|
|
|
70
71
|
url: new URL(request.url),
|
|
71
72
|
method: request.method,
|
|
72
73
|
headers: Object.fromEntries(request.headers),
|
|
73
|
-
data: Buffer.from(body),
|
|
74
|
+
data: node_buffer_1.Buffer.from(body),
|
|
74
75
|
});
|
|
75
76
|
}
|
|
76
77
|
}
|
|
@@ -292,7 +293,7 @@ class OINODbApi {
|
|
|
292
293
|
rows = request.data;
|
|
293
294
|
}
|
|
294
295
|
else if (request.data != null) {
|
|
295
|
-
rows = index_js_1.OINODbParser.createRows(this.datamodel, request.data, request);
|
|
296
|
+
rows = index_js_1.OINODbParser.createRows(this.datamodel, request.data, request.requestType, request);
|
|
296
297
|
}
|
|
297
298
|
}
|
|
298
299
|
catch (e) {
|
package/dist/cjs/OINODbParser.js
CHANGED
|
@@ -17,82 +17,67 @@ class OINODbParser {
|
|
|
17
17
|
*
|
|
18
18
|
* @param datamodel datamodel of the api
|
|
19
19
|
* @param data data as either serialized string or unserialized JS object or OINODataRow-array or Buffer/Uint8Array binary data
|
|
20
|
+
* @param contentType content type of the data
|
|
20
21
|
* @param request parameters
|
|
21
22
|
*
|
|
22
23
|
*/
|
|
23
|
-
static createRows(datamodel, data, request) {
|
|
24
|
+
static createRows(datamodel, data, contentType, request) {
|
|
24
25
|
let result = [];
|
|
25
26
|
if (typeof data == "string") {
|
|
26
|
-
result = this._createRowsFromText(datamodel, data, request);
|
|
27
|
+
result = this._createRowsFromText(datamodel, data, contentType, request);
|
|
27
28
|
}
|
|
28
29
|
else if ((data instanceof Buffer) || (data instanceof Uint8Array)) {
|
|
29
|
-
result = this._createRowsFromBlob(datamodel, data, request);
|
|
30
|
+
result = this._createRowsFromBlob(datamodel, data, contentType, request);
|
|
30
31
|
}
|
|
31
32
|
else if (typeof data == "object") {
|
|
32
33
|
result = [this._createRowFromObject(datamodel, data)];
|
|
33
34
|
}
|
|
34
35
|
return result;
|
|
35
36
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
*
|
|
39
|
-
* @param datamodel datamodel of the api
|
|
40
|
-
* @param data data as a string
|
|
41
|
-
* @param request request parameters
|
|
42
|
-
*
|
|
43
|
-
*/
|
|
44
|
-
static _createRowsFromText(datamodel, data, request) {
|
|
45
|
-
if ((request.requestType == index_js_1.OINOContentType.json) || (request.requestType == undefined)) {
|
|
37
|
+
static _createRowsFromText(datamodel, data, contentType, request) {
|
|
38
|
+
if ((contentType == index_js_1.OINOContentType.json) || (contentType == undefined)) {
|
|
46
39
|
return this._createRowFromJson(datamodel, data);
|
|
47
40
|
}
|
|
48
|
-
else if (
|
|
41
|
+
else if (contentType == index_js_1.OINOContentType.csv) {
|
|
49
42
|
return this._createRowFromCsv(datamodel, data);
|
|
50
43
|
}
|
|
51
|
-
else if (
|
|
52
|
-
return this._createRowFromFormdata(datamodel, Buffer.from(data, "utf8"), request
|
|
44
|
+
else if (contentType == index_js_1.OINOContentType.formdata) {
|
|
45
|
+
return this._createRowFromFormdata(datamodel, Buffer.from(data, "utf8"), request?.multipartBoundary || "");
|
|
53
46
|
}
|
|
54
|
-
else if (
|
|
47
|
+
else if (contentType == index_js_1.OINOContentType.urlencode) {
|
|
55
48
|
return this._createRowFromUrlencoded(datamodel, data);
|
|
56
49
|
}
|
|
57
|
-
else if (
|
|
50
|
+
else if (contentType == index_js_1.OINOContentType.html) {
|
|
58
51
|
index_js_1.OINOLog.error("@oino-ts/db", "OINODbParser", "createRowsFromText", "HTML can't be used as an input content type!", { contentType: index_js_1.OINOContentType.html });
|
|
59
52
|
return [];
|
|
60
53
|
}
|
|
61
54
|
else {
|
|
62
|
-
index_js_1.OINOLog.error("@oino-ts/db", "OINODbParser", "createRowsFromText", "Unrecognized input content type!", { contentType:
|
|
55
|
+
index_js_1.OINOLog.error("@oino-ts/db", "OINODbParser", "createRowsFromText", "Unrecognized input content type!", { contentType: contentType });
|
|
63
56
|
return [];
|
|
64
57
|
}
|
|
65
58
|
}
|
|
66
|
-
|
|
67
|
-
* Create data rows from request body based on the datamodel.
|
|
68
|
-
*
|
|
69
|
-
* @param datamodel datamodel of the api
|
|
70
|
-
* @param data data as an Buffer or Uint8Array
|
|
71
|
-
* @param request parameters
|
|
72
|
-
*
|
|
73
|
-
*/
|
|
74
|
-
static _createRowsFromBlob(datamodel, data, request) {
|
|
59
|
+
static _createRowsFromBlob(datamodel, data, contentType, request) {
|
|
75
60
|
if (data instanceof Uint8Array && !(data instanceof Buffer)) {
|
|
76
61
|
data = Buffer.from(data);
|
|
77
62
|
}
|
|
78
|
-
if ((
|
|
63
|
+
if ((contentType == index_js_1.OINOContentType.json) || (contentType == undefined)) {
|
|
79
64
|
return this._createRowFromJson(datamodel, data.toString()); // JSON is always a string
|
|
80
65
|
}
|
|
81
|
-
else if (
|
|
66
|
+
else if (contentType == index_js_1.OINOContentType.csv) {
|
|
82
67
|
return this._createRowFromCsv(datamodel, data.toString()); // binary data has to be base64 encoded so it's a string
|
|
83
68
|
}
|
|
84
|
-
else if (
|
|
85
|
-
return this._createRowFromFormdata(datamodel, data, request
|
|
69
|
+
else if (contentType == index_js_1.OINOContentType.formdata) {
|
|
70
|
+
return this._createRowFromFormdata(datamodel, data, request?.multipartBoundary || "");
|
|
86
71
|
}
|
|
87
|
-
else if (
|
|
72
|
+
else if (contentType == index_js_1.OINOContentType.urlencode) {
|
|
88
73
|
return this._createRowFromUrlencoded(datamodel, data.toString()); // data is urlencoded so it's a string
|
|
89
74
|
}
|
|
90
|
-
else if (
|
|
75
|
+
else if (contentType == index_js_1.OINOContentType.html) {
|
|
91
76
|
index_js_1.OINOLog.error("@oino-ts/db", "OINODbParser", "createRowsFromBlob", "HTML can't be used as an input content type!", { contentType: index_js_1.OINOContentType.html });
|
|
92
77
|
return [];
|
|
93
78
|
}
|
|
94
79
|
else {
|
|
95
|
-
index_js_1.OINOLog.error("@oino-ts/db", "OINODbParser", "createRowsFromBlob", "Unrecognized input content type!", { contentType:
|
|
80
|
+
index_js_1.OINOLog.error("@oino-ts/db", "OINODbParser", "createRowsFromBlob", "Unrecognized input content type!", { contentType: contentType });
|
|
96
81
|
return [];
|
|
97
82
|
}
|
|
98
83
|
}
|
|
@@ -331,6 +316,10 @@ class OINODbParser {
|
|
|
331
316
|
}
|
|
332
317
|
static _multipartHeaderRegex = /Content-Disposition\: (form-data|file); name=\"([^\"]+)\"(; filename=.*)?/i;
|
|
333
318
|
static _createRowFromFormdata(datamodel, data, multipartBoundary) {
|
|
319
|
+
if (!multipartBoundary || (multipartBoundary.length == 0)) {
|
|
320
|
+
index_js_1.OINOLog.error("@oino-ts/db", "OINODbParser", "_createRowFromFormdata", "Multipart boundary missing for formdata parsing!", {});
|
|
321
|
+
throw new Error(index_js_1.OINO_ERROR_PREFIX + "Multipart boundary missing for formdata parsing!");
|
|
322
|
+
}
|
|
334
323
|
let result = [];
|
|
335
324
|
try {
|
|
336
325
|
const n = data.length;
|
package/dist/esm/OINODbApi.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
4
4
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
5
5
|
*/
|
|
6
|
+
import { Buffer } from "node:buffer";
|
|
6
7
|
import { OINODbDataModel, OINOStringDataField, OINO_ERROR_PREFIX, OINODbModelSet, OINOBenchmark, OINODbConfig, OINOHtmlTemplate, OINONumberDataField, OINODbParser, OINODatetimeDataField, OINODbSqlAggregate, OINODbSqlSelect, OINODbSqlFilter, OINODbSqlOrder, OINODbSqlLimit } from "./index.js";
|
|
7
8
|
import { OINOLog, OINOResult, OINOHttpRequest } from "@oino-ts/common";
|
|
8
9
|
import { OINOHashid } from "@oino-ts/hashid";
|
|
@@ -286,7 +287,7 @@ export class OINODbApi {
|
|
|
286
287
|
rows = request.data;
|
|
287
288
|
}
|
|
288
289
|
else if (request.data != null) {
|
|
289
|
-
rows = OINODbParser.createRows(this.datamodel, request.data, request);
|
|
290
|
+
rows = OINODbParser.createRows(this.datamodel, request.data, request.requestType, request);
|
|
290
291
|
}
|
|
291
292
|
}
|
|
292
293
|
catch (e) {
|
package/dist/esm/OINODbParser.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
4
4
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
5
5
|
*/
|
|
6
|
-
import { OINOContentType, OINOStr, OINONumberDataField, OINOLog } from "./index.js";
|
|
6
|
+
import { OINOContentType, OINOStr, OINONumberDataField, OINOLog, OINO_ERROR_PREFIX } from "./index.js";
|
|
7
7
|
/**
|
|
8
8
|
* Static factory class for easily creating things based on data
|
|
9
9
|
*
|
|
@@ -14,82 +14,67 @@ export class OINODbParser {
|
|
|
14
14
|
*
|
|
15
15
|
* @param datamodel datamodel of the api
|
|
16
16
|
* @param data data as either serialized string or unserialized JS object or OINODataRow-array or Buffer/Uint8Array binary data
|
|
17
|
+
* @param contentType content type of the data
|
|
17
18
|
* @param request parameters
|
|
18
19
|
*
|
|
19
20
|
*/
|
|
20
|
-
static createRows(datamodel, data, request) {
|
|
21
|
+
static createRows(datamodel, data, contentType, request) {
|
|
21
22
|
let result = [];
|
|
22
23
|
if (typeof data == "string") {
|
|
23
|
-
result = this._createRowsFromText(datamodel, data, request);
|
|
24
|
+
result = this._createRowsFromText(datamodel, data, contentType, request);
|
|
24
25
|
}
|
|
25
26
|
else if ((data instanceof Buffer) || (data instanceof Uint8Array)) {
|
|
26
|
-
result = this._createRowsFromBlob(datamodel, data, request);
|
|
27
|
+
result = this._createRowsFromBlob(datamodel, data, contentType, request);
|
|
27
28
|
}
|
|
28
29
|
else if (typeof data == "object") {
|
|
29
30
|
result = [this._createRowFromObject(datamodel, data)];
|
|
30
31
|
}
|
|
31
32
|
return result;
|
|
32
33
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
*
|
|
36
|
-
* @param datamodel datamodel of the api
|
|
37
|
-
* @param data data as a string
|
|
38
|
-
* @param request request parameters
|
|
39
|
-
*
|
|
40
|
-
*/
|
|
41
|
-
static _createRowsFromText(datamodel, data, request) {
|
|
42
|
-
if ((request.requestType == OINOContentType.json) || (request.requestType == undefined)) {
|
|
34
|
+
static _createRowsFromText(datamodel, data, contentType, request) {
|
|
35
|
+
if ((contentType == OINOContentType.json) || (contentType == undefined)) {
|
|
43
36
|
return this._createRowFromJson(datamodel, data);
|
|
44
37
|
}
|
|
45
|
-
else if (
|
|
38
|
+
else if (contentType == OINOContentType.csv) {
|
|
46
39
|
return this._createRowFromCsv(datamodel, data);
|
|
47
40
|
}
|
|
48
|
-
else if (
|
|
49
|
-
return this._createRowFromFormdata(datamodel, Buffer.from(data, "utf8"), request
|
|
41
|
+
else if (contentType == OINOContentType.formdata) {
|
|
42
|
+
return this._createRowFromFormdata(datamodel, Buffer.from(data, "utf8"), request?.multipartBoundary || "");
|
|
50
43
|
}
|
|
51
|
-
else if (
|
|
44
|
+
else if (contentType == OINOContentType.urlencode) {
|
|
52
45
|
return this._createRowFromUrlencoded(datamodel, data);
|
|
53
46
|
}
|
|
54
|
-
else if (
|
|
47
|
+
else if (contentType == OINOContentType.html) {
|
|
55
48
|
OINOLog.error("@oino-ts/db", "OINODbParser", "createRowsFromText", "HTML can't be used as an input content type!", { contentType: OINOContentType.html });
|
|
56
49
|
return [];
|
|
57
50
|
}
|
|
58
51
|
else {
|
|
59
|
-
OINOLog.error("@oino-ts/db", "OINODbParser", "createRowsFromText", "Unrecognized input content type!", { contentType:
|
|
52
|
+
OINOLog.error("@oino-ts/db", "OINODbParser", "createRowsFromText", "Unrecognized input content type!", { contentType: contentType });
|
|
60
53
|
return [];
|
|
61
54
|
}
|
|
62
55
|
}
|
|
63
|
-
|
|
64
|
-
* Create data rows from request body based on the datamodel.
|
|
65
|
-
*
|
|
66
|
-
* @param datamodel datamodel of the api
|
|
67
|
-
* @param data data as an Buffer or Uint8Array
|
|
68
|
-
* @param request parameters
|
|
69
|
-
*
|
|
70
|
-
*/
|
|
71
|
-
static _createRowsFromBlob(datamodel, data, request) {
|
|
56
|
+
static _createRowsFromBlob(datamodel, data, contentType, request) {
|
|
72
57
|
if (data instanceof Uint8Array && !(data instanceof Buffer)) {
|
|
73
58
|
data = Buffer.from(data);
|
|
74
59
|
}
|
|
75
|
-
if ((
|
|
60
|
+
if ((contentType == OINOContentType.json) || (contentType == undefined)) {
|
|
76
61
|
return this._createRowFromJson(datamodel, data.toString()); // JSON is always a string
|
|
77
62
|
}
|
|
78
|
-
else if (
|
|
63
|
+
else if (contentType == OINOContentType.csv) {
|
|
79
64
|
return this._createRowFromCsv(datamodel, data.toString()); // binary data has to be base64 encoded so it's a string
|
|
80
65
|
}
|
|
81
|
-
else if (
|
|
82
|
-
return this._createRowFromFormdata(datamodel, data, request
|
|
66
|
+
else if (contentType == OINOContentType.formdata) {
|
|
67
|
+
return this._createRowFromFormdata(datamodel, data, request?.multipartBoundary || "");
|
|
83
68
|
}
|
|
84
|
-
else if (
|
|
69
|
+
else if (contentType == OINOContentType.urlencode) {
|
|
85
70
|
return this._createRowFromUrlencoded(datamodel, data.toString()); // data is urlencoded so it's a string
|
|
86
71
|
}
|
|
87
|
-
else if (
|
|
72
|
+
else if (contentType == OINOContentType.html) {
|
|
88
73
|
OINOLog.error("@oino-ts/db", "OINODbParser", "createRowsFromBlob", "HTML can't be used as an input content type!", { contentType: OINOContentType.html });
|
|
89
74
|
return [];
|
|
90
75
|
}
|
|
91
76
|
else {
|
|
92
|
-
OINOLog.error("@oino-ts/db", "OINODbParser", "createRowsFromBlob", "Unrecognized input content type!", { contentType:
|
|
77
|
+
OINOLog.error("@oino-ts/db", "OINODbParser", "createRowsFromBlob", "Unrecognized input content type!", { contentType: contentType });
|
|
93
78
|
return [];
|
|
94
79
|
}
|
|
95
80
|
}
|
|
@@ -328,6 +313,10 @@ export class OINODbParser {
|
|
|
328
313
|
}
|
|
329
314
|
static _multipartHeaderRegex = /Content-Disposition\: (form-data|file); name=\"([^\"]+)\"(; filename=.*)?/i;
|
|
330
315
|
static _createRowFromFormdata(datamodel, data, multipartBoundary) {
|
|
316
|
+
if (!multipartBoundary || (multipartBoundary.length == 0)) {
|
|
317
|
+
OINOLog.error("@oino-ts/db", "OINODbParser", "_createRowFromFormdata", "Multipart boundary missing for formdata parsing!", {});
|
|
318
|
+
throw new Error(OINO_ERROR_PREFIX + "Multipart boundary missing for formdata parsing!");
|
|
319
|
+
}
|
|
331
320
|
let result = [];
|
|
332
321
|
try {
|
|
333
322
|
const n = data.length;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
1
2
|
import { OINODbApiParams, OINODb, OINODbDataModel, OINODataRow, OINODbModelSet, OINOHttpResult, OINOHtmlTemplate, OINODbSqlParams, OINODbSqlAggregate, OINODbSqlSelect, OINODbSqlFilter, OINODbSqlOrder, OINODbSqlLimit } from "./index.js";
|
|
2
3
|
import { OINOResult, OINOHttpRequest, OINOHttpRequestInit } from "@oino-ts/common";
|
|
3
4
|
import { OINOHashid } from "@oino-ts/hashid";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { OINODbDataModel, OINODataRow } from "./index.js";
|
|
1
|
+
import { OINOContentType, OINODbDataModel, OINODataRow } from "./index.js";
|
|
2
2
|
import { OINODbApiRequest } from "./OINODbApi.js";
|
|
3
3
|
/**
|
|
4
4
|
* Static factory class for easily creating things based on data
|
|
@@ -10,27 +10,12 @@ export declare class OINODbParser {
|
|
|
10
10
|
*
|
|
11
11
|
* @param datamodel datamodel of the api
|
|
12
12
|
* @param data data as either serialized string or unserialized JS object or OINODataRow-array or Buffer/Uint8Array binary data
|
|
13
|
+
* @param contentType content type of the data
|
|
13
14
|
* @param request parameters
|
|
14
15
|
*
|
|
15
16
|
*/
|
|
16
|
-
static createRows(datamodel: OINODbDataModel, data: string | object | Buffer | Uint8Array,
|
|
17
|
-
/**
|
|
18
|
-
* Create data rows from request body based on the datamodel.
|
|
19
|
-
*
|
|
20
|
-
* @param datamodel datamodel of the api
|
|
21
|
-
* @param data data as a string
|
|
22
|
-
* @param request request parameters
|
|
23
|
-
*
|
|
24
|
-
*/
|
|
17
|
+
static createRows(datamodel: OINODbDataModel, data: string | object | Buffer | Uint8Array, contentType: OINOContentType, request?: OINODbApiRequest): OINODataRow[];
|
|
25
18
|
private static _createRowsFromText;
|
|
26
|
-
/**
|
|
27
|
-
* Create data rows from request body based on the datamodel.
|
|
28
|
-
*
|
|
29
|
-
* @param datamodel datamodel of the api
|
|
30
|
-
* @param data data as an Buffer or Uint8Array
|
|
31
|
-
* @param request parameters
|
|
32
|
-
*
|
|
33
|
-
*/
|
|
34
19
|
private static _createRowsFromBlob;
|
|
35
20
|
/**
|
|
36
21
|
* Create one data row from javascript object based on the datamodel.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oino-ts/db",
|
|
3
|
-
"version": "0.17.
|
|
3
|
+
"version": "0.17.5",
|
|
4
4
|
"description": "OINO TS library package for publishing an SQL database tables as a REST API.",
|
|
5
5
|
"author": "Matias Kiviniemi (pragmatta)",
|
|
6
6
|
"license": "MPL-2.0",
|
|
@@ -19,13 +19,13 @@
|
|
|
19
19
|
"module": "./dist/esm/index.js",
|
|
20
20
|
"types": "./dist/types/index.d.ts",
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@oino-ts/common": "0.17.
|
|
22
|
+
"@oino-ts/common": "0.17.5",
|
|
23
23
|
"oino-ts": "file:.."
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@oino-ts/types": "0.17.
|
|
26
|
+
"@oino-ts/types": "0.17.5",
|
|
27
27
|
"@types/bun": "^1.1.14",
|
|
28
|
-
"@types/node": "^20.17.
|
|
28
|
+
"@types/node": "^20.17.50",
|
|
29
29
|
"typescript": "~5.9.0"
|
|
30
30
|
},
|
|
31
31
|
"files": [
|
package/src/OINODbApi.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
+
import { Buffer } from "node:buffer";
|
|
7
8
|
import { OINODbApiParams, OINODb, OINODbDataSet, OINODbDataModel, OINODbDataField, OINOStringDataField, OINO_ERROR_PREFIX, OINODataRow, OINODataCell, OINODbModelSet, OINOBenchmark, OINODbConfig, OINOHttpResult, OINOHtmlTemplate, OINONumberDataField, OINODbParser, OINODatetimeDataField, OINODbSqlParams, OINODbSqlAggregate, OINODbSqlSelect, OINODbSqlFilter, OINODbSqlOrder, OINODbSqlLimit } from "./index.js"
|
|
8
9
|
import { OINOLog, OINOResult, OINOHttpRequest, OINOHttpRequestInit } from "@oino-ts/common";
|
|
9
10
|
import { OINOHashid } from "@oino-ts/hashid"
|
|
@@ -314,7 +315,7 @@ export class OINODbApi {
|
|
|
314
315
|
if (Array.isArray(request.data)) {
|
|
315
316
|
rows = request.data as OINODataRow[]
|
|
316
317
|
} else if (request.data != null) {
|
|
317
|
-
rows = OINODbParser.createRows(this.datamodel, request.data, request)
|
|
318
|
+
rows = OINODbParser.createRows(this.datamodel, request.data, request.requestType, request)
|
|
318
319
|
}
|
|
319
320
|
|
|
320
321
|
} catch (e:any) {
|
package/src/OINODbParser.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import { OINOContentType, OINODbDataModel, OINODbDataField, OINODataRow, OINOStr, OINONumberDataField, OINOLog } from "./index.js"
|
|
7
|
+
import { OINOContentType, OINODbDataModel, OINODbDataField, OINODataRow, OINOStr, OINONumberDataField, OINOLog, OINO_ERROR_PREFIX } from "./index.js"
|
|
8
8
|
import { OINODbApiRequest } from "./OINODbApi.js"
|
|
9
9
|
|
|
10
10
|
/**
|
|
@@ -17,16 +17,17 @@ export class OINODbParser {
|
|
|
17
17
|
*
|
|
18
18
|
* @param datamodel datamodel of the api
|
|
19
19
|
* @param data data as either serialized string or unserialized JS object or OINODataRow-array or Buffer/Uint8Array binary data
|
|
20
|
+
* @param contentType content type of the data
|
|
20
21
|
* @param request parameters
|
|
21
22
|
*
|
|
22
23
|
*/
|
|
23
|
-
static createRows(datamodel:OINODbDataModel, data:string|object|Buffer|Uint8Array, request
|
|
24
|
+
static createRows(datamodel:OINODbDataModel, data:string|object|Buffer|Uint8Array, contentType:OINOContentType, request?:OINODbApiRequest ):OINODataRow[] {
|
|
24
25
|
let result:OINODataRow[] = []
|
|
25
26
|
if (typeof data == "string") {
|
|
26
|
-
result = this._createRowsFromText(datamodel, data, request)
|
|
27
|
+
result = this._createRowsFromText(datamodel, data, contentType, request)
|
|
27
28
|
|
|
28
29
|
} else if ((data instanceof Buffer) || (data instanceof Uint8Array)) {
|
|
29
|
-
result = this._createRowsFromBlob(datamodel, data, request)
|
|
30
|
+
result = this._createRowsFromBlob(datamodel, data, contentType, request)
|
|
30
31
|
|
|
31
32
|
} else if (typeof data == "object") {
|
|
32
33
|
result = [this._createRowFromObject(datamodel, data)]
|
|
@@ -34,64 +35,48 @@ export class OINODbParser {
|
|
|
34
35
|
return result
|
|
35
36
|
}
|
|
36
37
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
*
|
|
40
|
-
* @param datamodel datamodel of the api
|
|
41
|
-
* @param data data as a string
|
|
42
|
-
* @param request request parameters
|
|
43
|
-
*
|
|
44
|
-
*/
|
|
45
|
-
private static _createRowsFromText(datamodel:OINODbDataModel, data:string, request:OINODbApiRequest ):OINODataRow[] {
|
|
46
|
-
if ((request.requestType == OINOContentType.json) || (request.requestType == undefined)) {
|
|
38
|
+
private static _createRowsFromText(datamodel:OINODbDataModel, data:string, contentType:OINOContentType, request?:OINODbApiRequest ):OINODataRow[] {
|
|
39
|
+
if ((contentType == OINOContentType.json) || (contentType == undefined)) {
|
|
47
40
|
return this._createRowFromJson(datamodel, data)
|
|
48
41
|
|
|
49
|
-
} else if (
|
|
42
|
+
} else if (contentType == OINOContentType.csv) {
|
|
50
43
|
return this._createRowFromCsv(datamodel, data)
|
|
51
44
|
|
|
52
|
-
} else if (
|
|
53
|
-
return this._createRowFromFormdata(datamodel, Buffer.from(data, "utf8"), request
|
|
45
|
+
} else if (contentType == OINOContentType.formdata) {
|
|
46
|
+
return this._createRowFromFormdata(datamodel, Buffer.from(data, "utf8"), request?.multipartBoundary || "")
|
|
54
47
|
|
|
55
|
-
} else if (
|
|
48
|
+
} else if (contentType == OINOContentType.urlencode) {
|
|
56
49
|
return this._createRowFromUrlencoded(datamodel, data)
|
|
57
50
|
|
|
58
|
-
} else if (
|
|
51
|
+
} else if (contentType == OINOContentType.html) {
|
|
59
52
|
OINOLog.error("@oino-ts/db", "OINODbParser", "createRowsFromText", "HTML can't be used as an input content type!", {contentType:OINOContentType.html})
|
|
60
53
|
return []
|
|
61
54
|
} else {
|
|
62
|
-
OINOLog.error("@oino-ts/db", "OINODbParser", "createRowsFromText", "Unrecognized input content type!", {contentType:
|
|
55
|
+
OINOLog.error("@oino-ts/db", "OINODbParser", "createRowsFromText", "Unrecognized input content type!", {contentType:contentType})
|
|
63
56
|
return []
|
|
64
57
|
}
|
|
65
58
|
}
|
|
66
|
-
|
|
67
|
-
* Create data rows from request body based on the datamodel.
|
|
68
|
-
*
|
|
69
|
-
* @param datamodel datamodel of the api
|
|
70
|
-
* @param data data as an Buffer or Uint8Array
|
|
71
|
-
* @param request parameters
|
|
72
|
-
*
|
|
73
|
-
*/
|
|
74
|
-
private static _createRowsFromBlob(datamodel:OINODbDataModel, data:Buffer|Uint8Array, request:OINODbApiRequest ):OINODataRow[] {
|
|
59
|
+
private static _createRowsFromBlob(datamodel:OINODbDataModel, data:Buffer|Uint8Array, contentType:OINOContentType, request?:OINODbApiRequest ):OINODataRow[] {
|
|
75
60
|
if (data instanceof Uint8Array && !(data instanceof Buffer)) {
|
|
76
61
|
data = Buffer.from(data) as Buffer
|
|
77
62
|
}
|
|
78
|
-
if ((
|
|
63
|
+
if ((contentType == OINOContentType.json) || (contentType == undefined)) {
|
|
79
64
|
return this._createRowFromJson(datamodel, data.toString()) // JSON is always a string
|
|
80
65
|
|
|
81
|
-
} else if (
|
|
66
|
+
} else if (contentType == OINOContentType.csv) {
|
|
82
67
|
return this._createRowFromCsv(datamodel, data.toString()) // binary data has to be base64 encoded so it's a string
|
|
83
68
|
|
|
84
|
-
} else if (
|
|
85
|
-
return this._createRowFromFormdata(datamodel, data as Buffer, request
|
|
69
|
+
} else if (contentType == OINOContentType.formdata) {
|
|
70
|
+
return this._createRowFromFormdata(datamodel, data as Buffer, request?.multipartBoundary || "")
|
|
86
71
|
|
|
87
|
-
} else if (
|
|
72
|
+
} else if (contentType == OINOContentType.urlencode) {
|
|
88
73
|
return this._createRowFromUrlencoded(datamodel, data.toString()) // data is urlencoded so it's a string
|
|
89
74
|
|
|
90
|
-
} else if (
|
|
75
|
+
} else if (contentType == OINOContentType.html) {
|
|
91
76
|
OINOLog.error("@oino-ts/db", "OINODbParser", "createRowsFromBlob", "HTML can't be used as an input content type!", {contentType:OINOContentType.html})
|
|
92
77
|
return []
|
|
93
78
|
} else {
|
|
94
|
-
OINOLog.error("@oino-ts/db", "OINODbParser", "createRowsFromBlob", "Unrecognized input content type!", {contentType:
|
|
79
|
+
OINOLog.error("@oino-ts/db", "OINODbParser", "createRowsFromBlob", "Unrecognized input content type!", {contentType:contentType})
|
|
95
80
|
return []
|
|
96
81
|
}
|
|
97
82
|
}
|
|
@@ -330,6 +315,10 @@ export class OINODbParser {
|
|
|
330
315
|
private static _multipartHeaderRegex:RegExp = /Content-Disposition\: (form-data|file); name=\"([^\"]+)\"(; filename=.*)?/i
|
|
331
316
|
|
|
332
317
|
private static _createRowFromFormdata(datamodel:OINODbDataModel, data:Buffer, multipartBoundary:string):OINODataRow[] {
|
|
318
|
+
if (!multipartBoundary || (multipartBoundary.length == 0)) {
|
|
319
|
+
OINOLog.error("@oino-ts/db", "OINODbParser", "_createRowFromFormdata", "Multipart boundary missing for formdata parsing!", {})
|
|
320
|
+
throw new Error(OINO_ERROR_PREFIX + "Multipart boundary missing for formdata parsing!")
|
|
321
|
+
}
|
|
333
322
|
let result:OINODataRow[] = []
|
|
334
323
|
try {
|
|
335
324
|
const n = data.length
|