@cubicweb/client 3.0.0-alpha.10 → 3.0.0-alpha.12
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 +11 -20
- package/lib/api/Api.d.ts +30 -22
- package/lib/api/Api.d.ts.map +1 -1
- package/lib/api/Api.js +148 -154
- package/lib/api/Api.js.map +1 -1
- package/lib/api/Client.d.ts +21 -9
- package/lib/api/Client.d.ts.map +1 -1
- package/lib/api/Client.js +40 -57
- package/lib/api/Client.js.map +1 -1
- package/lib/api/ResultSet.d.ts +25 -0
- package/lib/api/ResultSet.d.ts.map +1 -0
- package/lib/api/ResultSet.js +12 -0
- package/lib/api/ResultSet.js.map +1 -0
- package/lib/api/binary/RQLBinary.d.ts +6 -0
- package/lib/api/binary/RQLBinary.d.ts.map +1 -0
- package/lib/api/binary/RQLBinary.js +10 -0
- package/lib/api/binary/RQLBinary.js.map +1 -0
- package/lib/api/query/RQLQuery.d.ts +40 -0
- package/lib/api/query/RQLQuery.d.ts.map +1 -0
- package/lib/api/query/RQLQuery.js +48 -0
- package/lib/api/query/RQLQuery.js.map +1 -0
- package/lib/api/query/references.d.ts +41 -0
- package/lib/api/query/references.d.ts.map +1 -0
- package/lib/api/query/references.js +50 -0
- package/lib/api/query/references.js.map +1 -0
- package/lib/api/transaction/Transaction.d.ts +25 -0
- package/lib/api/transaction/Transaction.d.ts.map +1 -0
- package/lib/api/transaction/Transaction.js +28 -0
- package/lib/api/transaction/Transaction.js.map +1 -0
- package/lib/api/transaction/TransactionResult.d.ts +32 -0
- package/lib/api/transaction/TransactionResult.d.ts.map +1 -0
- package/lib/api/transaction/TransactionResult.js +41 -0
- package/lib/api/transaction/TransactionResult.js.map +1 -0
- package/lib/{Errors.d.ts → errors.d.ts} +7 -4
- package/lib/errors.d.ts.map +1 -0
- package/lib/errors.js +19 -0
- package/lib/errors.js.map +1 -0
- package/lib/index.d.ts +18 -13
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +17 -34
- package/lib/index.js.map +1 -1
- package/lib/schema/classes/EntitySchema.d.ts +5 -5
- package/lib/schema/classes/EntitySchema.d.ts.map +1 -1
- package/lib/schema/classes/EntitySchema.js +3 -5
- package/lib/schema/classes/EntitySchema.js.map +1 -1
- package/lib/schema/classes/RelationDefinitionSchema.d.ts +6 -6
- package/lib/schema/classes/RelationDefinitionSchema.d.ts.map +1 -1
- package/lib/schema/classes/RelationDefinitionSchema.js +8 -10
- package/lib/schema/classes/RelationDefinitionSchema.js.map +1 -1
- package/lib/schema/classes/Schema.d.ts +7 -7
- package/lib/schema/classes/Schema.d.ts.map +1 -1
- package/lib/schema/classes/Schema.js +8 -11
- package/lib/schema/classes/Schema.js.map +1 -1
- package/lib/schema/classes/Utils.d.ts +10 -10
- package/lib/schema/classes/Utils.d.ts.map +1 -1
- package/lib/schema/classes/Utils.js +19 -35
- package/lib/schema/classes/Utils.js.map +1 -1
- package/lib/schema/raw/Constraints.d.ts +27 -0
- package/lib/schema/raw/Constraints.d.ts.map +1 -1
- package/lib/schema/raw/Constraints.js +1 -2
- package/lib/schema/raw/Entities.js +1 -2
- package/lib/schema/raw/RelationDefinitions.d.ts +3 -3
- package/lib/schema/raw/RelationDefinitions.d.ts.map +1 -1
- package/lib/schema/raw/RelationDefinitions.js +1 -2
- package/lib/schema/raw/Schema.d.ts +2 -2
- package/lib/schema/raw/Schema.d.ts.map +1 -1
- package/lib/schema/raw/Schema.js +1 -2
- package/lib/schema/raw/Utils.d.ts +1 -1
- package/lib/schema/raw/Utils.d.ts.map +1 -1
- package/lib/schema/raw/Utils.js +1 -2
- package/lib/utils.d.ts +39 -0
- package/lib/utils.d.ts.map +1 -0
- package/lib/utils.js +58 -0
- package/lib/utils.js.map +1 -0
- package/package.json +12 -5
- package/src/api/Api.ts +49 -42
- package/src/api/Client.ts +24 -15
- package/src/api/ResultSet.ts +29 -0
- package/src/api/binary/RQLBinary.ts +9 -0
- package/src/api/query/RQLQuery.ts +51 -0
- package/src/api/query/references.ts +43 -0
- package/src/api/transaction/Transaction.ts +31 -0
- package/src/api/transaction/TransactionResult.ts +46 -0
- package/src/{Errors.ts → errors.ts} +6 -7
- package/src/index.ts +21 -17
- package/src/schema/classes/EntitySchema.ts +8 -5
- package/src/schema/classes/RelationDefinitionSchema.ts +7 -7
- package/src/schema/classes/Schema.ts +10 -7
- package/src/schema/classes/Utils.ts +4 -4
- package/src/schema/raw/Constraints.ts +28 -0
- package/src/schema/raw/RelationDefinitions.ts +3 -3
- package/src/schema/raw/Schema.ts +2 -2
- package/src/schema/raw/Utils.ts +1 -1
- package/src/utils.ts +91 -0
- package/lib/Errors.d.ts.map +0 -1
- package/lib/Errors.js +0 -25
- package/lib/Errors.js.map +0 -1
- package/lib/api/Transaction.d.ts +0 -46
- package/lib/api/Transaction.d.ts.map +0 -1
- package/lib/api/Transaction.js +0 -75
- package/lib/api/Transaction.js.map +0 -1
- package/src/api/Transaction.ts +0 -94
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@ library.
|
|
|
7
7
|
|
|
8
8
|
The full documentation is available [here](https://cubicweb.pages.logilab.fr/cubicwebjs/client).
|
|
9
9
|
|
|
10
|
-
**Compatible with [API cube](https://forge.extranet.logilab.fr/cubicweb/cubes/api) version 0.
|
|
10
|
+
**Compatible with [API cube](https://forge.extranet.logilab.fr/cubicweb/cubes/api) version 0.14 only.**
|
|
11
11
|
|
|
12
12
|
## Installation
|
|
13
13
|
|
|
@@ -78,23 +78,18 @@ client
|
|
|
78
78
|
|
|
79
79
|
// Execute a series of request in a transaction
|
|
80
80
|
const transaction = new Transaction()
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
row: 0,
|
|
90
|
-
column: 0,
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
})
|
|
81
|
+
const insertQuery = new RQLQuery(`INSERT Person X: X name "test"`)
|
|
82
|
+
transaction.push(insertQuery)
|
|
83
|
+
transaction.push(
|
|
84
|
+
new RQLQuery(
|
|
85
|
+
`INSERT Person X: X name "test"`,
|
|
86
|
+
{ created_eid: insertQuery.cellRef(0, 0) }
|
|
87
|
+
)
|
|
88
|
+
)
|
|
94
89
|
client
|
|
95
90
|
.executeTransaction(transaction)
|
|
96
91
|
.then((callbackResult) => {
|
|
97
|
-
console.log("Transaction was
|
|
92
|
+
console.log("Transaction was successful and the callback returned:");
|
|
98
93
|
console.log(callbackResult);
|
|
99
94
|
})
|
|
100
95
|
.catch((e) => {
|
|
@@ -122,11 +117,7 @@ you must authorize the URL the client is running on as well as the `Content-Type
|
|
|
122
117
|
|
|
123
118
|
## Authentication
|
|
124
119
|
|
|
125
|
-
The API cube uses cookie authentication
|
|
126
|
-
A `CW_JWT` [HTTP Only](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#security) cookie
|
|
127
|
-
will be automatically created by the browser when executing the login request.
|
|
128
|
-
This cookie cannot be accessed by the JavaScript so you will have to implement your own
|
|
129
|
-
logic to save the login state.
|
|
120
|
+
The API cube uses a cookie authentication method by default. When you call the login route, the cookie will be automatically created by the browser.
|
|
130
121
|
|
|
131
122
|
If the client is running on a different server from your CubicWeb instance,
|
|
132
123
|
make sure you setup CORS as described above.
|
package/lib/api/Api.d.ts
CHANGED
|
@@ -1,32 +1,26 @@
|
|
|
1
|
-
import { RawSchema } from "../schema/raw/Schema";
|
|
2
|
-
import {
|
|
1
|
+
import { RawSchema } from "../schema/raw/Schema.js";
|
|
2
|
+
import { RQLBinary } from "./binary/RQLBinary.js";
|
|
3
|
+
import { RQLQueryCellRef } from "./query/references.js";
|
|
4
|
+
import { Transaction } from "./transaction/Transaction.js";
|
|
5
|
+
import { TransactionResult } from "./transaction/TransactionResult.js";
|
|
3
6
|
/**
|
|
4
|
-
* Type representing
|
|
7
|
+
* Type representing valid final values to use as parameters in a RQL query.
|
|
8
|
+
*
|
|
9
|
+
* For references to requests/binaries, see {@link RQLQueryCellRef} and {@link RQLQueryBinaryRef}
|
|
5
10
|
*
|
|
6
11
|
* @category Api
|
|
7
12
|
*/
|
|
8
|
-
export type
|
|
9
|
-
export type RQLParamValue = string | number | boolean | null;
|
|
13
|
+
export type RQLQueryParamValue = string | number | boolean | null;
|
|
10
14
|
/**
|
|
11
15
|
* Type representing RQL parameters to send along a request.
|
|
12
16
|
*
|
|
13
|
-
*
|
|
14
|
-
*/
|
|
15
|
-
export type RQLParams = Record<string, RQLParamValue | TransactionQueryScalarRef | TransactionQueryBinaryRef>;
|
|
16
|
-
/**
|
|
17
|
-
* Type representing a row returned in a result set.
|
|
17
|
+
* Each key must be referenced in the RQL query.
|
|
18
18
|
*
|
|
19
19
|
* @category Api
|
|
20
20
|
*/
|
|
21
|
-
export type
|
|
21
|
+
export type RQLQueryParams = Record<string, RQLQueryParamValue | RQLQueryCellRef | RQLBinary>;
|
|
22
22
|
/**
|
|
23
|
-
* Type representing
|
|
24
|
-
*
|
|
25
|
-
* @category Api
|
|
26
|
-
*/
|
|
27
|
-
export type ResultSet = Array<RqlRow>;
|
|
28
|
-
/**
|
|
29
|
-
* Type representing the currently logged user's data
|
|
23
|
+
* Type representing the currently logged user's data as returned by the `/current-user` route
|
|
30
24
|
*
|
|
31
25
|
* @category Api
|
|
32
26
|
*/
|
|
@@ -48,6 +42,9 @@ type ApiError = {
|
|
|
48
42
|
/**
|
|
49
43
|
* Type representing the error data sent to clients when retrieving an API error.
|
|
50
44
|
*
|
|
45
|
+
* It simply adds the HTTP status code in a `status` key to the regular CubicWeb api error,
|
|
46
|
+
* so users can handle HTTP status codes in their code.
|
|
47
|
+
*
|
|
51
48
|
* @category Api
|
|
52
49
|
*/
|
|
53
50
|
export type ApiErrorResponse = {
|
|
@@ -57,7 +54,7 @@ export type ApiErrorResponse = {
|
|
|
57
54
|
* Class used to handle requests to the CubicWeb API.
|
|
58
55
|
*
|
|
59
56
|
* You should never have to create this object yourself.
|
|
60
|
-
* Instead, retrieve this object from the methods in the
|
|
57
|
+
* Instead, retrieve this object from the methods in the {@link Client} class.
|
|
61
58
|
*
|
|
62
59
|
* @internal
|
|
63
60
|
*/
|
|
@@ -74,7 +71,7 @@ export default class Api {
|
|
|
74
71
|
* Class used to handle requests to the CubicWeb API.
|
|
75
72
|
*
|
|
76
73
|
* You should never have to create this object yourself.
|
|
77
|
-
* Instead, retrieve this object from the methods in the
|
|
74
|
+
* Instead, retrieve this object from the methods in the {@link Client} class.
|
|
78
75
|
*
|
|
79
76
|
* @param apiUrl The API url. It should not include the API version as it will be automatically added.
|
|
80
77
|
* Example: if you can retrieve the schema with https://example.com/api/v1/schema,
|
|
@@ -93,7 +90,7 @@ export default class Api {
|
|
|
93
90
|
* Tries to log in the user on the configured CubicWeb instance with the given credentials.
|
|
94
91
|
*
|
|
95
92
|
* @param login The user login
|
|
96
|
-
* @param password
|
|
93
|
+
* @param password The user password
|
|
97
94
|
* @returns A promise returning true if the login was successful, false otherwise
|
|
98
95
|
*/
|
|
99
96
|
login(login: string, password: string): Promise<boolean>;
|
|
@@ -112,9 +109,20 @@ export default class Api {
|
|
|
112
109
|
/**
|
|
113
110
|
* Get the binary for the given entity attribute
|
|
114
111
|
*
|
|
115
|
-
* @
|
|
112
|
+
* @param eid Entity eid to fetch the binary from
|
|
113
|
+
* @param attribute Attribute on the entity in which the binary is stored
|
|
114
|
+
* @returns A promise resolving to the binary as a Blob
|
|
116
115
|
*/
|
|
117
116
|
binary(eid: number, attribute: string): Promise<Blob>;
|
|
117
|
+
/**
|
|
118
|
+
* Executes the given transaction.
|
|
119
|
+
*
|
|
120
|
+
* If the {@link Transaction} object contains binaries, the request is automatically
|
|
121
|
+
* sent using `multipart/form-data` to allow uploading binaries.
|
|
122
|
+
*
|
|
123
|
+
* @param transaction Transaction to execute
|
|
124
|
+
* @returns A promise returning the transaction result on success
|
|
125
|
+
*/
|
|
118
126
|
rql(transaction: Transaction): Promise<TransactionResult>;
|
|
119
127
|
private getTransactionParams;
|
|
120
128
|
private multipartTransaction;
|
package/lib/api/Api.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Api.d.ts","sourceRoot":"","sources":["../../src/api/Api.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"Api.d.ts","sourceRoot":"","sources":["../../src/api/Api.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAGpD,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAClD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAC3D,OAAO,EAAE,iBAAiB,EAAE,MAAM,oCAAoC,CAAC;AAEvE;;;;;;GAMG;AACH,MAAM,MAAM,kBAAkB,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;AAElE;;;;;;GAMG;AACH,MAAM,MAAM,cAAc,GAAG,MAAM,CACjC,MAAM,EACN,kBAAkB,GAAG,eAAe,GAAG,SAAS,CACjD,CAAC;AAEF;;;;GAIG;AACH,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;;;GAIG;AACH,KAAK,QAAQ,GAAG;IACd,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAChC,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,MAAM,EAAE,MAAM,CAAC;CAChB,GAAG,QAAQ,CAAC;AAEb;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,OAAO,GAAG;IACtB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,QAAQ,CAAQ;IACxB,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,kBAAkB,CAAS;IACnC,OAAO,CAAC,aAAa,CAAS;IAE9B;;;;;;;;;OASG;gBACS,MAAM,EAAE,MAAM;IAU1B,IAAI,MAAM,WAET;IAED,OAAO,CAAC,WAAW;IAInB;;;;OAIG;IACG,MAAM,IAAI,OAAO,CAAC,SAAS,CAAC;IAOlC;;;;;;OAMG;IACG,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAgB9D;;;;OAIG;IACG,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;IAO7B;;;;OAIG;IACG,WAAW,IAAI,OAAO,CAAC,WAAW,CAAC;IAOzC;;;;;;OAMG;IACG,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAW3D;;;;;;;;OAQG;IACG,GAAG,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAa/D,OAAO,CAAC,oBAAoB;YAOd,oBAAoB;YAsBpB,eAAe;IAY7B,OAAO,CAAC,gBAAgB;CAgBzB"}
|
package/lib/api/Api.js
CHANGED
|
@@ -1,37 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
const Errors_1 = require("../Errors");
|
|
13
|
-
const Transaction_1 = require("./Transaction");
|
|
1
|
+
import { ValidationError } from "../errors.js";
|
|
2
|
+
import { getBinariesFromTransaction, serializeQueryParams } from "../utils.js";
|
|
3
|
+
import { TransactionResult } from "./transaction/TransactionResult.js";
|
|
14
4
|
/**
|
|
15
5
|
* Class used to handle requests to the CubicWeb API.
|
|
16
6
|
*
|
|
17
7
|
* You should never have to create this object yourself.
|
|
18
|
-
* Instead, retrieve this object from the methods in the
|
|
8
|
+
* Instead, retrieve this object from the methods in the {@link Client} class.
|
|
19
9
|
*
|
|
20
10
|
* @internal
|
|
21
11
|
*/
|
|
22
|
-
class Api {
|
|
12
|
+
export default class Api {
|
|
13
|
+
_apiUrl;
|
|
14
|
+
_version = "v1";
|
|
15
|
+
_rqlApiUrl;
|
|
16
|
+
_schemaApiUrl;
|
|
17
|
+
_loginApiUrl;
|
|
18
|
+
_logoutApiUrl;
|
|
19
|
+
_currentUserApiUrl;
|
|
20
|
+
_binaryApiUrl;
|
|
23
21
|
/**
|
|
24
22
|
* Class used to handle requests to the CubicWeb API.
|
|
25
23
|
*
|
|
26
24
|
* You should never have to create this object yourself.
|
|
27
|
-
* Instead, retrieve this object from the methods in the
|
|
25
|
+
* Instead, retrieve this object from the methods in the {@link Client} class.
|
|
28
26
|
*
|
|
29
27
|
* @param apiUrl The API url. It should not include the API version as it will be automatically added.
|
|
30
28
|
* Example: if you can retrieve the schema with https://example.com/api/v1/schema,
|
|
31
29
|
* use https://example.com/api as `apiUrl`.
|
|
32
30
|
*/
|
|
33
31
|
constructor(apiUrl) {
|
|
34
|
-
this._version = "v1";
|
|
35
32
|
this._apiUrl = apiUrl;
|
|
36
33
|
this._rqlApiUrl = this.getApiRoute("rql");
|
|
37
34
|
this._schemaApiUrl = this.getApiRoute("schema");
|
|
@@ -51,50 +48,44 @@ class Api {
|
|
|
51
48
|
*
|
|
52
49
|
* @returns The CubicWeb instance's JSON schema.
|
|
53
50
|
*/
|
|
54
|
-
schema() {
|
|
55
|
-
return
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
credentials: "include",
|
|
59
|
-
});
|
|
51
|
+
async schema() {
|
|
52
|
+
return nonNullFetchApi(this._schemaApiUrl, {
|
|
53
|
+
method: "GET",
|
|
54
|
+
credentials: "include",
|
|
60
55
|
});
|
|
61
56
|
}
|
|
62
57
|
/**
|
|
63
58
|
* Tries to log in the user on the configured CubicWeb instance with the given credentials.
|
|
64
59
|
*
|
|
65
60
|
* @param login The user login
|
|
66
|
-
* @param password
|
|
61
|
+
* @param password The user password
|
|
67
62
|
* @returns A promise returning true if the login was successful, false otherwise
|
|
68
63
|
*/
|
|
69
|
-
login(login, password) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}
|
|
85
|
-
});
|
|
64
|
+
async login(login, password) {
|
|
65
|
+
try {
|
|
66
|
+
await fetchApi(this._loginApiUrl, {
|
|
67
|
+
method: "POST",
|
|
68
|
+
body: JSON.stringify({
|
|
69
|
+
login,
|
|
70
|
+
password,
|
|
71
|
+
}),
|
|
72
|
+
credentials: "include",
|
|
73
|
+
});
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
catch (_error) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
86
79
|
}
|
|
87
80
|
/**
|
|
88
81
|
* Logs out the current user. Will invalidate any authentication cookie active.
|
|
89
82
|
*
|
|
90
83
|
* @returns A promise which will resolve when the user is properly logged out
|
|
91
84
|
*/
|
|
92
|
-
logout() {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
credentials: "include",
|
|
97
|
-
});
|
|
85
|
+
async logout() {
|
|
86
|
+
await fetchApi(this._logoutApiUrl, {
|
|
87
|
+
method: "POST",
|
|
88
|
+
credentials: "include",
|
|
98
89
|
});
|
|
99
90
|
}
|
|
100
91
|
/**
|
|
@@ -102,77 +93,79 @@ class Api {
|
|
|
102
93
|
*
|
|
103
94
|
* @returns A promise resolving to the current user
|
|
104
95
|
*/
|
|
105
|
-
currentUser() {
|
|
106
|
-
return
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
credentials: "include",
|
|
110
|
-
});
|
|
96
|
+
async currentUser() {
|
|
97
|
+
return await nonNullFetchApi(this._currentUserApiUrl, {
|
|
98
|
+
method: "GET",
|
|
99
|
+
credentials: "include",
|
|
111
100
|
});
|
|
112
101
|
}
|
|
113
102
|
/**
|
|
114
103
|
* Get the binary for the given entity attribute
|
|
115
104
|
*
|
|
116
|
-
* @
|
|
105
|
+
* @param eid Entity eid to fetch the binary from
|
|
106
|
+
* @param attribute Attribute on the entity in which the binary is stored
|
|
107
|
+
* @returns A promise resolving to the binary as a Blob
|
|
117
108
|
*/
|
|
118
|
-
binary(eid, attribute) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
credentials: "include",
|
|
127
|
-
});
|
|
109
|
+
async binary(eid, attribute) {
|
|
110
|
+
const params = new URLSearchParams({
|
|
111
|
+
eid: eid.toString(),
|
|
112
|
+
attribute,
|
|
113
|
+
});
|
|
114
|
+
return await nonNullFetchApi(this._binaryApiUrl + params, {
|
|
115
|
+
method: "GET",
|
|
116
|
+
credentials: "include",
|
|
128
117
|
});
|
|
129
118
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
119
|
+
/**
|
|
120
|
+
* Executes the given transaction.
|
|
121
|
+
*
|
|
122
|
+
* If the {@link Transaction} object contains binaries, the request is automatically
|
|
123
|
+
* sent using `multipart/form-data` to allow uploading binaries.
|
|
124
|
+
*
|
|
125
|
+
* @param transaction Transaction to execute
|
|
126
|
+
* @returns A promise returning the transaction result on success
|
|
127
|
+
*/
|
|
128
|
+
async rql(transaction) {
|
|
129
|
+
const binaries = getBinariesFromTransaction(transaction);
|
|
130
|
+
try {
|
|
131
|
+
if (binaries.length > 0) {
|
|
132
|
+
return await this.multipartTransaction(transaction, binaries);
|
|
139
133
|
}
|
|
140
|
-
|
|
141
|
-
this.
|
|
134
|
+
else {
|
|
135
|
+
return await this.jsonTransaction(transaction);
|
|
142
136
|
}
|
|
143
|
-
}
|
|
137
|
+
}
|
|
138
|
+
catch (e) {
|
|
139
|
+
this.handleUserErrors(e);
|
|
140
|
+
}
|
|
144
141
|
}
|
|
145
142
|
getTransactionParams(transaction) {
|
|
146
|
-
return transaction.queries.map((
|
|
147
|
-
query:
|
|
148
|
-
params:
|
|
143
|
+
return transaction.queries.map((rqlQuery) => ({
|
|
144
|
+
query: rqlQuery.rql,
|
|
145
|
+
params: serializeQueryParams(transaction, rqlQuery.params),
|
|
149
146
|
}));
|
|
150
147
|
}
|
|
151
|
-
multipartTransaction(transaction) {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
formData.append(key, value);
|
|
158
|
-
});
|
|
159
|
-
const resultSets = yield nonNullFetchApi(this._rqlApiUrl, {
|
|
160
|
-
method: "POST",
|
|
161
|
-
credentials: "include",
|
|
162
|
-
body: formData,
|
|
163
|
-
});
|
|
164
|
-
return new Transaction_1.TransactionResult(resultSets);
|
|
148
|
+
async multipartTransaction(transaction, binaries) {
|
|
149
|
+
const jsonParams = this.getTransactionParams(transaction);
|
|
150
|
+
const formData = new FormData();
|
|
151
|
+
formData.append("queries", JSON.stringify(jsonParams));
|
|
152
|
+
binaries.forEach((binary) => {
|
|
153
|
+
formData.append(binary.uuid, binary.blob);
|
|
165
154
|
});
|
|
155
|
+
const resultSets = await nonNullFetchApi(this._rqlApiUrl, {
|
|
156
|
+
method: "POST",
|
|
157
|
+
credentials: "include",
|
|
158
|
+
body: formData,
|
|
159
|
+
});
|
|
160
|
+
return new TransactionResult(transaction, resultSets);
|
|
166
161
|
}
|
|
167
|
-
jsonTransaction(transaction) {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
body: JSON.stringify(this.getTransactionParams(transaction)),
|
|
173
|
-
});
|
|
174
|
-
return new Transaction_1.TransactionResult(resultSets);
|
|
162
|
+
async jsonTransaction(transaction) {
|
|
163
|
+
const resultSets = await nonNullFetchApi(this._rqlApiUrl, {
|
|
164
|
+
method: "POST",
|
|
165
|
+
credentials: "include",
|
|
166
|
+
body: JSON.stringify(this.getTransactionParams(transaction)),
|
|
175
167
|
});
|
|
168
|
+
return new TransactionResult(transaction, resultSets);
|
|
176
169
|
}
|
|
177
170
|
handleUserErrors(error) {
|
|
178
171
|
if (typeof error === "object" &&
|
|
@@ -182,14 +175,13 @@ class Api {
|
|
|
182
175
|
"message" in error &&
|
|
183
176
|
typeof error.message === "string") {
|
|
184
177
|
if (error.title === "ValidationError") {
|
|
185
|
-
throw new
|
|
178
|
+
throw new ValidationError(error.message);
|
|
186
179
|
}
|
|
187
180
|
throw error;
|
|
188
181
|
}
|
|
189
182
|
throw error;
|
|
190
183
|
}
|
|
191
184
|
}
|
|
192
|
-
exports.default = Api;
|
|
193
185
|
/**
|
|
194
186
|
* Behaves the same as `fetchAPI` but forbids null responses.
|
|
195
187
|
* If this function detects a null response from the API,
|
|
@@ -199,19 +191,17 @@ exports.default = Api;
|
|
|
199
191
|
* @param init Additional request parameters
|
|
200
192
|
* @returns A promise with the non-null fetched result
|
|
201
193
|
*/
|
|
202
|
-
function nonNullFetchApi(input, init) {
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
return result;
|
|
214
|
-
});
|
|
194
|
+
async function nonNullFetchApi(input, init) {
|
|
195
|
+
const result = await fetchApi(input, init);
|
|
196
|
+
if (result == null) {
|
|
197
|
+
const error = {
|
|
198
|
+
status: 500,
|
|
199
|
+
title: "Unexpected null response",
|
|
200
|
+
message: "Response was null but a value was expected",
|
|
201
|
+
};
|
|
202
|
+
throw error;
|
|
203
|
+
}
|
|
204
|
+
return result;
|
|
215
205
|
}
|
|
216
206
|
/**
|
|
217
207
|
* Sends a request to the given API route with the `Content-Type` header set and waits for a response.
|
|
@@ -222,46 +212,50 @@ function nonNullFetchApi(input, init) {
|
|
|
222
212
|
* @param init Additional request parameters
|
|
223
213
|
* @returns A promise with the fetched result
|
|
224
214
|
*/
|
|
225
|
-
function fetchApi(input, init) {
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
headers["Content-Type"] = "application/json;charset=UTF-8";
|
|
237
|
-
}
|
|
238
|
-
headers = Object.assign(Object.assign({}, headers), init === null || init === void 0 ? void 0 : init.headers);
|
|
215
|
+
async function fetchApi(input, init) {
|
|
216
|
+
let headers = init?.headers;
|
|
217
|
+
if (init?.method && init.method !== "GET") {
|
|
218
|
+
headers = {
|
|
219
|
+
// CSRF custom header necessary for POST requests
|
|
220
|
+
"X-Client-Name": "XMLHttpRequest",
|
|
221
|
+
};
|
|
222
|
+
// Browsers will automatically set the multipart content type
|
|
223
|
+
// with the right boundary if the body is FormData
|
|
224
|
+
if (!(init.body instanceof FormData)) {
|
|
225
|
+
headers["Content-Type"] = "application/json;charset=UTF-8";
|
|
239
226
|
}
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
227
|
+
headers = {
|
|
228
|
+
...headers,
|
|
229
|
+
...init?.headers,
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
const response = await fetch(input, {
|
|
233
|
+
...init,
|
|
234
|
+
headers,
|
|
235
|
+
});
|
|
236
|
+
const status = response.status;
|
|
237
|
+
if (status >= 200 && status < 300) {
|
|
238
|
+
if (status === 204) {
|
|
239
|
+
return null;
|
|
253
240
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
try {
|
|
257
|
-
json = yield response.json();
|
|
258
|
-
}
|
|
259
|
-
catch (e) {
|
|
260
|
-
console.error(e);
|
|
261
|
-
throw { status, title: "Error", message: "" };
|
|
262
|
-
}
|
|
263
|
-
throw Object.assign({ status }, json);
|
|
241
|
+
try {
|
|
242
|
+
return await response.json();
|
|
264
243
|
}
|
|
265
|
-
|
|
244
|
+
catch (e) {
|
|
245
|
+
console.error(e);
|
|
246
|
+
return null;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
let json;
|
|
251
|
+
try {
|
|
252
|
+
json = await response.json();
|
|
253
|
+
}
|
|
254
|
+
catch (e) {
|
|
255
|
+
console.error(e);
|
|
256
|
+
throw { status, title: "Error", message: "" };
|
|
257
|
+
}
|
|
258
|
+
throw { status, ...json };
|
|
259
|
+
}
|
|
266
260
|
}
|
|
267
261
|
//# sourceMappingURL=Api.js.map
|
package/lib/api/Api.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Api.js","sourceRoot":"","sources":["../../src/api/Api.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Api.js","sourceRoot":"","sources":["../../src/api/Api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAE/C,OAAO,EAAE,0BAA0B,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAK/E,OAAO,EAAE,iBAAiB,EAAE,MA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