@cubicweb/client 2.1.1 → 3.0.0-alpha.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -16
- package/lib/Errors.js +3 -9
- package/lib/Errors.js.map +1 -1
- package/lib/api/Api.d.ts +35 -15
- package/lib/api/Api.d.ts.map +1 -1
- package/lib/api/Api.js +152 -133
- package/lib/api/Api.js.map +1 -1
- package/lib/api/Client.d.ts +20 -9
- package/lib/api/Client.d.ts.map +1 -1
- package/lib/api/Client.js +40 -49
- package/lib/api/Client.js.map +1 -1
- package/lib/api/Transaction.d.ts +33 -20
- package/lib/api/Transaction.d.ts.map +1 -1
- package/lib/api/Transaction.js +50 -46
- package/lib/api/Transaction.js.map +1 -1
- package/lib/index.d.ts +13 -13
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +12 -33
- package/lib/index.js.map +1 -1
- package/lib/schema/classes/EntitySchema.d.ts +21 -27
- package/lib/schema/classes/EntitySchema.d.ts.map +1 -1
- package/lib/schema/classes/EntitySchema.js +20 -30
- package/lib/schema/classes/EntitySchema.js.map +1 -1
- package/lib/schema/classes/RelationDefinitionSchema.d.ts +13 -15
- package/lib/schema/classes/RelationDefinitionSchema.d.ts.map +1 -1
- package/lib/schema/classes/RelationDefinitionSchema.js +14 -30
- package/lib/schema/classes/RelationDefinitionSchema.js.map +1 -1
- package/lib/schema/classes/Schema.d.ts +9 -7
- package/lib/schema/classes/Schema.d.ts.map +1 -1
- package/lib/schema/classes/Schema.js +10 -11
- package/lib/schema/classes/Schema.js.map +1 -1
- package/lib/schema/classes/Utils.d.ts +139 -5
- package/lib/schema/classes/Utils.d.ts.map +1 -1
- package/lib/schema/classes/Utils.js +157 -11
- package/lib/schema/classes/Utils.js.map +1 -1
- package/lib/schema/raw/Constraints.d.ts +17 -9
- package/lib/schema/raw/Constraints.d.ts.map +1 -1
- package/lib/schema/raw/Constraints.js +1 -2
- package/lib/schema/raw/Entities.d.ts +4 -0
- package/lib/schema/raw/Entities.d.ts.map +1 -1
- package/lib/schema/raw/Entities.js +1 -2
- package/lib/schema/raw/RelationDefinitions.d.ts +17 -4
- 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 +4 -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 +7 -1
- package/lib/schema/raw/Utils.d.ts.map +1 -1
- package/lib/schema/raw/Utils.js +1 -2
- package/package.json +6 -9
- package/src/api/Api.ts +110 -57
- package/src/api/Client.ts +36 -19
- package/src/api/Transaction.ts +56 -37
- package/src/index.ts +13 -13
- package/src/schema/classes/EntitySchema.ts +43 -46
- package/src/schema/classes/RelationDefinitionSchema.ts +16 -31
- package/src/schema/classes/Schema.ts +12 -7
- package/src/schema/classes/Utils.ts +182 -5
- package/src/schema/raw/Constraints.ts +20 -8
- package/src/schema/raw/Entities.ts +4 -0
- package/src/schema/raw/RelationDefinitions.ts +17 -4
- package/src/schema/raw/Schema.ts +4 -2
- package/src/schema/raw/Utils.ts +7 -1
package/README.md
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
# @cubicweb/client
|
|
2
2
|
|
|
3
|
-
This library
|
|
4
|
-
used to communicate with instances running with the
|
|
3
|
+
This library allows any JavaScript client to communicate with a [CubicWeb](https://cubicweb.org/) instance running with the
|
|
5
4
|
[API cube](https://forge.extranet.logilab.fr/cubicweb/cubes/api).
|
|
6
5
|
It replaces the [cwclientlibjs](https://forge.extranet.logilab.fr/open-source/cwclientlibjs)
|
|
7
6
|
library.
|
|
8
7
|
|
|
9
8
|
The full documentation is available [here](https://cubicweb.pages.logilab.fr/cubicwebjs/client).
|
|
10
9
|
|
|
10
|
+
**Compatible with [API cube](https://forge.extranet.logilab.fr/cubicweb/cubes/api) version 0.13 only.**
|
|
11
|
+
|
|
11
12
|
## Installation
|
|
12
13
|
|
|
13
|
-
@cubicweb/client is
|
|
14
|
+
@cubicweb/client is available from NPM:
|
|
14
15
|
```bash
|
|
15
16
|
# With NPM
|
|
16
17
|
npm i @cubicweb/client
|
|
@@ -76,20 +77,22 @@ client
|
|
|
76
77
|
});
|
|
77
78
|
|
|
78
79
|
// Execute a series of request in a transaction
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
80
|
+
const transaction = new Transaction()
|
|
81
|
+
transaction.push({
|
|
82
|
+
query: `INSERT Person X: X name "test"`
|
|
83
|
+
})
|
|
84
|
+
transaction.push({
|
|
85
|
+
query: `Any X Where X eid %(created_eid)s`,
|
|
86
|
+
params: {
|
|
87
|
+
created_eid: {
|
|
88
|
+
queryIndex: 0,
|
|
89
|
+
row: 0,
|
|
90
|
+
column: 0,
|
|
91
|
+
}
|
|
91
92
|
}
|
|
92
|
-
|
|
93
|
+
})
|
|
94
|
+
client
|
|
95
|
+
.executeTransaction(transaction)
|
|
93
96
|
.then((callbackResult) => {
|
|
94
97
|
console.log("Transaction was successul and the callback returned:");
|
|
95
98
|
console.log(callbackResult);
|
|
@@ -203,3 +206,12 @@ const entity = schema.getEntity("InvalidEntityType");
|
|
|
203
206
|
// TS will be happy and your entity will have the type 'ValidEntityType'.
|
|
204
207
|
const entity = schema.getEntity("ValidEntityType");
|
|
205
208
|
```
|
|
209
|
+
|
|
210
|
+
## Contribute
|
|
211
|
+
|
|
212
|
+
All `@cubicweb` libraries are in the [cubicwebjs monorepo](https://forge.extranet.logilab.fr/cubicweb/cubicwebjs).
|
|
213
|
+
Please refer to the main README.
|
|
214
|
+
|
|
215
|
+
## Get Help
|
|
216
|
+
|
|
217
|
+
Contact us on [Matrix](https://matrix.to/#/#cubicweb:matrix.logilab.org) and check the roadmap on the [CubicWeb Repository](https://forge.extranet.logilab.fr/cubicweb/cubicweb).
|
package/lib/Errors.js
CHANGED
|
@@ -1,25 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TransactionClosedError = exports.ValidationError = exports.InconsistentSchemaError = void 0;
|
|
4
|
-
class InconsistentSchemaError extends Error {
|
|
1
|
+
export class InconsistentSchemaError extends Error {
|
|
5
2
|
constructor(message) {
|
|
6
3
|
super(`Inconsistent schema: ${message}`);
|
|
7
4
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
8
5
|
}
|
|
9
6
|
}
|
|
10
|
-
|
|
11
|
-
class ValidationError extends Error {
|
|
7
|
+
export class ValidationError extends Error {
|
|
12
8
|
constructor(message) {
|
|
13
9
|
super(message);
|
|
14
10
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
15
11
|
}
|
|
16
12
|
}
|
|
17
|
-
|
|
18
|
-
class TransactionClosedError extends Error {
|
|
13
|
+
export class TransactionClosedError extends Error {
|
|
19
14
|
constructor() {
|
|
20
15
|
super("Trying to use a closed transaction");
|
|
21
16
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
22
17
|
}
|
|
23
18
|
}
|
|
24
|
-
exports.TransactionClosedError = TransactionClosedError;
|
|
25
19
|
//# sourceMappingURL=Errors.js.map
|
package/lib/Errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Errors.js","sourceRoot":"","sources":["../src/Errors.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Errors.js","sourceRoot":"","sources":["../src/Errors.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,uBAAwB,SAAQ,KAAK;IAChD,YAAY,OAAe;QACzB,KAAK,CAAC,wBAAwB,OAAO,EAAE,CAAC,CAAC;QACzC,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACpD,CAAC;CACF;AAED,MAAM,OAAO,eAAgB,SAAQ,KAAK;IACxC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACpD,CAAC;CACF;AAED,MAAM,OAAO,sBAAuB,SAAQ,KAAK;IAC/C;QACE,KAAK,CAAC,oCAAoC,CAAC,CAAC;QAC5C,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACpD,CAAC;CACF"}
|
package/lib/api/Api.d.ts
CHANGED
|
@@ -1,26 +1,34 @@
|
|
|
1
|
-
import { RawSchema } from "../schema/raw/Schema";
|
|
2
|
-
import { TransactionResult, Transaction } from "./Transaction";
|
|
1
|
+
import { RawSchema } from "../schema/raw/Schema.js";
|
|
2
|
+
import { TransactionResult, Transaction, TransactionQueryScalarRef, TransactionQueryBinaryRef } from "./Transaction.js";
|
|
3
3
|
/**
|
|
4
4
|
* Type representing an RQL request and its associated parameters.
|
|
5
|
+
*
|
|
6
|
+
* @category Api
|
|
5
7
|
*/
|
|
6
8
|
export type RQLQuery = [string, RQLParams];
|
|
7
9
|
export type RQLParamValue = string | number | boolean | null;
|
|
8
10
|
/**
|
|
9
11
|
* Type representing RQL parameters to send along a request.
|
|
12
|
+
*
|
|
13
|
+
* @category Api
|
|
10
14
|
*/
|
|
11
|
-
export
|
|
12
|
-
[key: string]: RQLParamValue;
|
|
13
|
-
}
|
|
15
|
+
export type RQLParams = Record<string, RQLParamValue | TransactionQueryScalarRef | TransactionQueryBinaryRef>;
|
|
14
16
|
/**
|
|
15
17
|
* Type representing a row returned in a result set.
|
|
18
|
+
*
|
|
19
|
+
* @category Api
|
|
16
20
|
*/
|
|
17
21
|
export type RqlRow = Array<string | number | boolean | null>;
|
|
18
22
|
/**
|
|
19
23
|
* Type representing a RQL request result.
|
|
24
|
+
*
|
|
25
|
+
* @category Api
|
|
20
26
|
*/
|
|
21
27
|
export type ResultSet = Array<RqlRow>;
|
|
22
28
|
/**
|
|
23
29
|
* Type representing the currently logged user's data
|
|
30
|
+
*
|
|
31
|
+
* @category Api
|
|
24
32
|
*/
|
|
25
33
|
export interface CurrentUser {
|
|
26
34
|
login: string;
|
|
@@ -29,6 +37,8 @@ export interface CurrentUser {
|
|
|
29
37
|
}
|
|
30
38
|
/**
|
|
31
39
|
* Type representing the body of an error sent by CubicWeb's API.
|
|
40
|
+
*
|
|
41
|
+
* @category Api
|
|
32
42
|
*/
|
|
33
43
|
type ApiError = {
|
|
34
44
|
title: string;
|
|
@@ -37,6 +47,8 @@ type ApiError = {
|
|
|
37
47
|
};
|
|
38
48
|
/**
|
|
39
49
|
* Type representing the error data sent to clients when retrieving an API error.
|
|
50
|
+
*
|
|
51
|
+
* @category Api
|
|
40
52
|
*/
|
|
41
53
|
export type ApiErrorResponse = {
|
|
42
54
|
status: number;
|
|
@@ -55,8 +67,9 @@ export default class Api {
|
|
|
55
67
|
private _rqlApiUrl;
|
|
56
68
|
private _schemaApiUrl;
|
|
57
69
|
private _loginApiUrl;
|
|
70
|
+
private _logoutApiUrl;
|
|
58
71
|
private _currentUserApiUrl;
|
|
59
|
-
private
|
|
72
|
+
private _binaryApiUrl;
|
|
60
73
|
/**
|
|
61
74
|
* Class used to handle requests to the CubicWeb API.
|
|
62
75
|
*
|
|
@@ -70,14 +83,6 @@ export default class Api {
|
|
|
70
83
|
constructor(apiUrl: string);
|
|
71
84
|
get apiUrl(): string;
|
|
72
85
|
private getApiRoute;
|
|
73
|
-
/**
|
|
74
|
-
* Executes the given query and waits for the response.
|
|
75
|
-
*
|
|
76
|
-
* @param query The RQL query to send
|
|
77
|
-
* @param params The additional parameters for the request
|
|
78
|
-
* @returns A promise with the request's result set
|
|
79
|
-
*/
|
|
80
|
-
execute(query: string, params: RQLParams): Promise<ResultSet>;
|
|
81
86
|
/**
|
|
82
87
|
* Retrieves the CubicWeb instance's JSON schema.
|
|
83
88
|
*
|
|
@@ -92,13 +97,28 @@ export default class Api {
|
|
|
92
97
|
* @returns A promise returning true if the login was successful, false otherwise
|
|
93
98
|
*/
|
|
94
99
|
login(login: string, password: string): Promise<boolean>;
|
|
100
|
+
/**
|
|
101
|
+
* Logs out the current user. Will invalidate any authentication cookie active.
|
|
102
|
+
*
|
|
103
|
+
* @returns A promise which will resolve when the user is properly logged out
|
|
104
|
+
*/
|
|
105
|
+
logout(): Promise<void>;
|
|
95
106
|
/**
|
|
96
107
|
* Get the current user associated to this client
|
|
97
108
|
*
|
|
98
109
|
* @returns A promise resolving to the current user
|
|
99
110
|
*/
|
|
100
111
|
currentUser(): Promise<CurrentUser>;
|
|
101
|
-
|
|
112
|
+
/**
|
|
113
|
+
* Get the binary for the given entity attribute
|
|
114
|
+
*
|
|
115
|
+
* @returns A promise resolving to the binary
|
|
116
|
+
*/
|
|
117
|
+
binary(eid: number, attribute: string): Promise<Blob>;
|
|
118
|
+
rql(transaction: Transaction): Promise<TransactionResult>;
|
|
119
|
+
private getTransactionParams;
|
|
120
|
+
private multipartTransaction;
|
|
121
|
+
private jsonTransaction;
|
|
102
122
|
private handleUserErrors;
|
|
103
123
|
}
|
|
104
124
|
export {};
|
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;AACpD,OAAO,EACL,iBAAiB,EACjB,WAAW,EACX,yBAAyB,EACzB,yBAAyB,EAC1B,MAAM,kBAAkB,CAAC;AAE1B;;;;GAIG;AACH,MAAM,MAAM,QAAQ,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;AAE3C,MAAM,MA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C;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;;;;OAIG;IACG,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAWrD,GAAG,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAY/D,OAAO,CAAC,oBAAoB;YAOd,oBAAoB;YAmBpB,eAAe;IAY7B,OAAO,CAAC,gBAAgB;CAgBzB"}
|
package/lib/api/Api.js
CHANGED
|
@@ -1,16 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
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 { TransactionResult, } from "./Transaction.js";
|
|
14
3
|
/**
|
|
15
4
|
* Class used to handle requests to the CubicWeb API.
|
|
16
5
|
*
|
|
@@ -19,7 +8,15 @@ const Transaction_1 = require("./Transaction");
|
|
|
19
8
|
*
|
|
20
9
|
* @internal
|
|
21
10
|
*/
|
|
22
|
-
class Api {
|
|
11
|
+
export default class Api {
|
|
12
|
+
_apiUrl;
|
|
13
|
+
_version = "v1";
|
|
14
|
+
_rqlApiUrl;
|
|
15
|
+
_schemaApiUrl;
|
|
16
|
+
_loginApiUrl;
|
|
17
|
+
_logoutApiUrl;
|
|
18
|
+
_currentUserApiUrl;
|
|
19
|
+
_binaryApiUrl;
|
|
23
20
|
/**
|
|
24
21
|
* Class used to handle requests to the CubicWeb API.
|
|
25
22
|
*
|
|
@@ -31,13 +28,13 @@ class Api {
|
|
|
31
28
|
* use https://example.com/api as `apiUrl`.
|
|
32
29
|
*/
|
|
33
30
|
constructor(apiUrl) {
|
|
34
|
-
this._version = "v1";
|
|
35
31
|
this._apiUrl = apiUrl;
|
|
36
32
|
this._rqlApiUrl = this.getApiRoute("rql");
|
|
37
33
|
this._schemaApiUrl = this.getApiRoute("schema");
|
|
38
34
|
this._loginApiUrl = this.getApiRoute("login");
|
|
35
|
+
this._logoutApiUrl = this.getApiRoute("logout");
|
|
39
36
|
this._currentUserApiUrl = this.getApiRoute("current-user");
|
|
40
|
-
this.
|
|
37
|
+
this._binaryApiUrl = this.getApiRoute("binary");
|
|
41
38
|
}
|
|
42
39
|
get apiUrl() {
|
|
43
40
|
return this._apiUrl;
|
|
@@ -45,38 +42,15 @@ class Api {
|
|
|
45
42
|
getApiRoute(route) {
|
|
46
43
|
return `${this._apiUrl}/${this._version}/${route}`;
|
|
47
44
|
}
|
|
48
|
-
/**
|
|
49
|
-
* Executes the given query and waits for the response.
|
|
50
|
-
*
|
|
51
|
-
* @param query The RQL query to send
|
|
52
|
-
* @param params The additional parameters for the request
|
|
53
|
-
* @returns A promise with the request's result set
|
|
54
|
-
*/
|
|
55
|
-
execute(query, params) {
|
|
56
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
57
|
-
try {
|
|
58
|
-
return yield nonNullFetchApi(this._rqlApiUrl, {
|
|
59
|
-
method: "POST",
|
|
60
|
-
body: JSON.stringify({ query, params }),
|
|
61
|
-
credentials: "include", // FIXME is it secure?
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
catch (e) {
|
|
65
|
-
this.handleUserErrors(e);
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
45
|
/**
|
|
70
46
|
* Retrieves the CubicWeb instance's JSON schema.
|
|
71
47
|
*
|
|
72
48
|
* @returns The CubicWeb instance's JSON schema.
|
|
73
49
|
*/
|
|
74
|
-
schema() {
|
|
75
|
-
return
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
credentials: "include",
|
|
79
|
-
});
|
|
50
|
+
async schema() {
|
|
51
|
+
return nonNullFetchApi(this._schemaApiUrl, {
|
|
52
|
+
method: "GET",
|
|
53
|
+
credentials: "include",
|
|
80
54
|
});
|
|
81
55
|
}
|
|
82
56
|
/**
|
|
@@ -86,22 +60,31 @@ class Api {
|
|
|
86
60
|
* @param password THe user password
|
|
87
61
|
* @returns A promise returning true if the login was successful, false otherwise
|
|
88
62
|
*/
|
|
89
|
-
login(login, password) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
63
|
+
async login(login, password) {
|
|
64
|
+
try {
|
|
65
|
+
await fetchApi(this._loginApiUrl, {
|
|
66
|
+
method: "POST",
|
|
67
|
+
body: JSON.stringify({
|
|
68
|
+
login,
|
|
69
|
+
password,
|
|
70
|
+
}),
|
|
71
|
+
credentials: "include",
|
|
72
|
+
});
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
catch (_error) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Logs out the current user. Will invalidate any authentication cookie active.
|
|
81
|
+
*
|
|
82
|
+
* @returns A promise which will resolve when the user is properly logged out
|
|
83
|
+
*/
|
|
84
|
+
async logout() {
|
|
85
|
+
await fetchApi(this._logoutApiUrl, {
|
|
86
|
+
method: "POST",
|
|
87
|
+
credentials: "include",
|
|
105
88
|
});
|
|
106
89
|
}
|
|
107
90
|
/**
|
|
@@ -109,41 +92,67 @@ class Api {
|
|
|
109
92
|
*
|
|
110
93
|
* @returns A promise resolving to the current user
|
|
111
94
|
*/
|
|
112
|
-
currentUser() {
|
|
113
|
-
return
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
credentials: "include",
|
|
117
|
-
});
|
|
95
|
+
async currentUser() {
|
|
96
|
+
return await nonNullFetchApi(this._currentUserApiUrl, {
|
|
97
|
+
method: "GET",
|
|
98
|
+
credentials: "include",
|
|
118
99
|
});
|
|
119
100
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
body: JSON.stringify(data),
|
|
140
|
-
});
|
|
141
|
-
return new Transaction_1.TransactionResult(resultSets);
|
|
101
|
+
/**
|
|
102
|
+
* Get the binary for the given entity attribute
|
|
103
|
+
*
|
|
104
|
+
* @returns A promise resolving to the binary
|
|
105
|
+
*/
|
|
106
|
+
async binary(eid, attribute) {
|
|
107
|
+
const params = new URLSearchParams({
|
|
108
|
+
eid: eid.toString(),
|
|
109
|
+
attribute,
|
|
110
|
+
});
|
|
111
|
+
return await nonNullFetchApi(this._binaryApiUrl + params, {
|
|
112
|
+
method: "GET",
|
|
113
|
+
credentials: "include",
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
async rql(transaction) {
|
|
117
|
+
try {
|
|
118
|
+
if (Object.keys(transaction.binaries).length > 0) {
|
|
119
|
+
return await this.multipartTransaction(transaction);
|
|
142
120
|
}
|
|
143
|
-
|
|
144
|
-
this.
|
|
121
|
+
else {
|
|
122
|
+
return await this.jsonTransaction(transaction);
|
|
145
123
|
}
|
|
124
|
+
}
|
|
125
|
+
catch (e) {
|
|
126
|
+
this.handleUserErrors(e);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
getTransactionParams(transaction) {
|
|
130
|
+
return transaction.queries.map((transactionQuery) => ({
|
|
131
|
+
query: transactionQuery.query,
|
|
132
|
+
params: transactionQuery.params,
|
|
133
|
+
}));
|
|
134
|
+
}
|
|
135
|
+
async multipartTransaction(transaction) {
|
|
136
|
+
const jsonParams = this.getTransactionParams(transaction);
|
|
137
|
+
const formData = new FormData();
|
|
138
|
+
formData.append("queries", JSON.stringify(jsonParams));
|
|
139
|
+
Object.entries(transaction.binaries).forEach(([key, value]) => {
|
|
140
|
+
formData.append(key, value);
|
|
141
|
+
});
|
|
142
|
+
const resultSets = await nonNullFetchApi(this._rqlApiUrl, {
|
|
143
|
+
method: "POST",
|
|
144
|
+
credentials: "include",
|
|
145
|
+
body: formData,
|
|
146
146
|
});
|
|
147
|
+
return new TransactionResult(resultSets);
|
|
148
|
+
}
|
|
149
|
+
async jsonTransaction(transaction) {
|
|
150
|
+
const resultSets = await nonNullFetchApi(this._rqlApiUrl, {
|
|
151
|
+
method: "POST",
|
|
152
|
+
credentials: "include",
|
|
153
|
+
body: JSON.stringify(this.getTransactionParams(transaction)),
|
|
154
|
+
});
|
|
155
|
+
return new TransactionResult(resultSets);
|
|
147
156
|
}
|
|
148
157
|
handleUserErrors(error) {
|
|
149
158
|
if (typeof error === "object" &&
|
|
@@ -153,14 +162,13 @@ class Api {
|
|
|
153
162
|
"message" in error &&
|
|
154
163
|
typeof error.message === "string") {
|
|
155
164
|
if (error.title === "ValidationError") {
|
|
156
|
-
throw new
|
|
165
|
+
throw new ValidationError(error.message);
|
|
157
166
|
}
|
|
158
167
|
throw error;
|
|
159
168
|
}
|
|
160
169
|
throw error;
|
|
161
170
|
}
|
|
162
171
|
}
|
|
163
|
-
exports.default = Api;
|
|
164
172
|
/**
|
|
165
173
|
* Behaves the same as `fetchAPI` but forbids null responses.
|
|
166
174
|
* If this function detects a null response from the API,
|
|
@@ -170,19 +178,17 @@ exports.default = Api;
|
|
|
170
178
|
* @param init Additional request parameters
|
|
171
179
|
* @returns A promise with the non-null fetched result
|
|
172
180
|
*/
|
|
173
|
-
function nonNullFetchApi(input, init) {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
return result;
|
|
185
|
-
});
|
|
181
|
+
async function nonNullFetchApi(input, init) {
|
|
182
|
+
const result = await fetchApi(input, init);
|
|
183
|
+
if (result == null) {
|
|
184
|
+
const error = {
|
|
185
|
+
status: 500,
|
|
186
|
+
title: "Unexpected null response",
|
|
187
|
+
message: "Response was null but a value was expected",
|
|
188
|
+
};
|
|
189
|
+
throw error;
|
|
190
|
+
}
|
|
191
|
+
return result;
|
|
186
192
|
}
|
|
187
193
|
/**
|
|
188
194
|
* Sends a request to the given API route with the `Content-Type` header set and waits for a response.
|
|
@@ -193,37 +199,50 @@ function nonNullFetchApi(input, init) {
|
|
|
193
199
|
* @param init Additional request parameters
|
|
194
200
|
* @returns A promise with the fetched result
|
|
195
201
|
*/
|
|
196
|
-
function fetchApi(input, init) {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
202
|
+
async function fetchApi(input, init) {
|
|
203
|
+
let headers = init?.headers;
|
|
204
|
+
if (init?.method && init.method !== "GET") {
|
|
205
|
+
headers = {
|
|
206
|
+
// CSRF custom header necessary for POST requests
|
|
207
|
+
"X-Client-Name": "XMLHttpRequest",
|
|
208
|
+
};
|
|
209
|
+
// Browsers will automatically set the multipart content type
|
|
210
|
+
// with the right boundary if the body is FormData
|
|
211
|
+
if (!(init.body instanceof FormData)) {
|
|
212
|
+
headers["Content-Type"] = "application/json;charset=UTF-8";
|
|
201
213
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
214
|
+
headers = {
|
|
215
|
+
...headers,
|
|
216
|
+
...init?.headers,
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
const response = await fetch(input, {
|
|
220
|
+
...init,
|
|
221
|
+
headers,
|
|
222
|
+
});
|
|
223
|
+
const status = response.status;
|
|
224
|
+
if (status >= 200 && status < 300) {
|
|
225
|
+
if (status === 204) {
|
|
226
|
+
return null;
|
|
215
227
|
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
try {
|
|
219
|
-
json = yield response.json();
|
|
220
|
-
}
|
|
221
|
-
catch (e) {
|
|
222
|
-
console.error(e);
|
|
223
|
-
throw { status, title: "Error", message: "" };
|
|
224
|
-
}
|
|
225
|
-
throw Object.assign({ status }, json);
|
|
228
|
+
try {
|
|
229
|
+
return await response.json();
|
|
226
230
|
}
|
|
227
|
-
|
|
231
|
+
catch (e) {
|
|
232
|
+
console.error(e);
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
let json;
|
|
238
|
+
try {
|
|
239
|
+
json = await response.json();
|
|
240
|
+
}
|
|
241
|
+
catch (e) {
|
|
242
|
+
console.error(e);
|
|
243
|
+
throw { status, title: "Error", message: "" };
|
|
244
|
+
}
|
|
245
|
+
throw { status, ...json };
|
|
246
|
+
}
|
|
228
247
|
}
|
|
229
248
|
//# 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"],"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