@cubicweb/client 3.0.0-alpha.9 → 3.0.2
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 +58 -34
- package/lib/Authenticator.d.ts +8 -0
- package/lib/Authenticator.d.ts.map +1 -0
- package/lib/Authenticator.js +2 -0
- package/lib/Authenticator.js.map +1 -0
- package/lib/api/Api.d.ts +56 -23
- package/lib/api/Api.d.ts.map +1 -1
- package/lib/api/Api.js +152 -149
- package/lib/api/Api.js.map +1 -1
- package/lib/api/Client.d.ts +26 -10
- package/lib/api/Client.d.ts.map +1 -1
- package/lib/api/Client.js +41 -58
- package/lib/api/Client.js.map +1 -1
- package/lib/api/ResultSet.d.ts +52 -0
- package/lib/api/ResultSet.d.ts.map +1 -0
- package/lib/api/ResultSet.js +64 -0
- package/lib/api/ResultSet.js.map +1 -0
- package/lib/api/authenticators/CookieAuthenticator.d.ts +10 -0
- package/lib/api/authenticators/CookieAuthenticator.d.ts.map +1 -0
- package/lib/api/authenticators/CookieAuthenticator.js +11 -0
- package/lib/api/authenticators/CookieAuthenticator.js.map +1 -0
- package/lib/api/authenticators/SignedRequestAuthenticator.d.ts +32 -0
- package/lib/api/authenticators/SignedRequestAuthenticator.d.ts.map +1 -0
- package/lib/api/authenticators/SignedRequestAuthenticator.js +89 -0
- package/lib/api/authenticators/SignedRequestAuthenticator.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 +41 -0
- package/lib/api/transaction/TransactionResult.d.ts.map +1 -0
- package/lib/api/transaction/TransactionResult.js +45 -0
- package/lib/api/transaction/TransactionResult.js.map +1 -0
- package/lib/encodeFormData.d.ts +5 -0
- package/lib/encodeFormData.d.ts.map +1 -0
- package/lib/encodeFormData.js +94 -0
- package/lib/encodeFormData.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 +19 -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 +15 -5
- package/src/Authenticator.ts +8 -0
- package/src/api/Api.ts +154 -125
- package/src/api/Client.ts +31 -18
- package/src/api/ResultSet.ts +95 -0
- package/src/api/authenticators/CookieAuthenticator.ts +17 -0
- package/src/api/authenticators/SignedRequestAuthenticator.ts +133 -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 +68 -0
- package/src/encodeFormData.ts +126 -0
- package/src/{Errors.ts → errors.ts} +6 -7
- package/src/index.ts +24 -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/lib/api/Api.js
CHANGED
|
@@ -1,37 +1,35 @@
|
|
|
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;
|
|
21
|
+
_authenticator;
|
|
23
22
|
/**
|
|
24
23
|
* Class used to handle requests to the CubicWeb API.
|
|
25
24
|
*
|
|
26
25
|
* You should never have to create this object yourself.
|
|
27
|
-
* Instead, retrieve this object from the methods in the
|
|
26
|
+
* Instead, retrieve this object from the methods in the {@link Client} class.
|
|
28
27
|
*
|
|
29
28
|
* @param apiUrl The API url. It should not include the API version as it will be automatically added.
|
|
30
29
|
* Example: if you can retrieve the schema with https://example.com/api/v1/schema,
|
|
31
30
|
* use https://example.com/api as `apiUrl`.
|
|
32
31
|
*/
|
|
33
|
-
constructor(apiUrl) {
|
|
34
|
-
this._version = "v1";
|
|
32
|
+
constructor(apiUrl, options) {
|
|
35
33
|
this._apiUrl = apiUrl;
|
|
36
34
|
this._rqlApiUrl = this.getApiRoute("rql");
|
|
37
35
|
this._schemaApiUrl = this.getApiRoute("schema");
|
|
@@ -39,6 +37,7 @@ class Api {
|
|
|
39
37
|
this._logoutApiUrl = this.getApiRoute("logout");
|
|
40
38
|
this._currentUserApiUrl = this.getApiRoute("current-user");
|
|
41
39
|
this._binaryApiUrl = this.getApiRoute("binary");
|
|
40
|
+
this._authenticator = options.authenticator;
|
|
42
41
|
}
|
|
43
42
|
get apiUrl() {
|
|
44
43
|
return this._apiUrl;
|
|
@@ -51,50 +50,41 @@ class Api {
|
|
|
51
50
|
*
|
|
52
51
|
* @returns The CubicWeb instance's JSON schema.
|
|
53
52
|
*/
|
|
54
|
-
schema() {
|
|
55
|
-
return
|
|
56
|
-
|
|
57
|
-
method: "GET",
|
|
58
|
-
credentials: "include",
|
|
59
|
-
});
|
|
53
|
+
async schema() {
|
|
54
|
+
return this.nonNullFetchApi(this._schemaApiUrl, {
|
|
55
|
+
method: "GET",
|
|
60
56
|
});
|
|
61
57
|
}
|
|
62
58
|
/**
|
|
63
59
|
* Tries to log in the user on the configured CubicWeb instance with the given credentials.
|
|
64
60
|
*
|
|
65
61
|
* @param login The user login
|
|
66
|
-
* @param password
|
|
62
|
+
* @param password The user password
|
|
67
63
|
* @returns A promise returning true if the login was successful, false otherwise
|
|
68
64
|
*/
|
|
69
|
-
login(login, password) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
return false;
|
|
84
|
-
}
|
|
85
|
-
});
|
|
65
|
+
async login(login, password) {
|
|
66
|
+
try {
|
|
67
|
+
await this.fetchApi(this._loginApiUrl, {
|
|
68
|
+
method: "POST",
|
|
69
|
+
body: JSON.stringify({
|
|
70
|
+
login,
|
|
71
|
+
password,
|
|
72
|
+
}),
|
|
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
|
-
method: "POST",
|
|
96
|
-
credentials: "include",
|
|
97
|
-
});
|
|
85
|
+
async logout() {
|
|
86
|
+
await this.fetchApi(this._logoutApiUrl, {
|
|
87
|
+
method: "POST",
|
|
98
88
|
});
|
|
99
89
|
}
|
|
100
90
|
/**
|
|
@@ -102,80 +92,85 @@ class Api {
|
|
|
102
92
|
*
|
|
103
93
|
* @returns A promise resolving to the current user
|
|
104
94
|
*/
|
|
105
|
-
currentUser() {
|
|
106
|
-
return
|
|
107
|
-
|
|
108
|
-
method: "GET",
|
|
109
|
-
credentials: "include",
|
|
110
|
-
});
|
|
95
|
+
async currentUser() {
|
|
96
|
+
return await this.nonNullFetchApi(this._currentUserApiUrl, {
|
|
97
|
+
method: "GET",
|
|
111
98
|
});
|
|
112
99
|
}
|
|
113
100
|
/**
|
|
114
101
|
* Get the binary for the given entity attribute
|
|
115
102
|
*
|
|
116
|
-
* @
|
|
103
|
+
* @param eid Entity eid to fetch the binary from
|
|
104
|
+
* @param attribute Attribute on the entity in which the binary is stored
|
|
105
|
+
* @returns A promise resolving to the binary as a Blob
|
|
117
106
|
*/
|
|
118
|
-
binary(eid, attribute) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
method: "GET",
|
|
126
|
-
credentials: "include",
|
|
127
|
-
});
|
|
107
|
+
async binary(eid, attribute) {
|
|
108
|
+
const params = new URLSearchParams({
|
|
109
|
+
eid: eid.toString(),
|
|
110
|
+
attribute,
|
|
111
|
+
});
|
|
112
|
+
const response = await this.fetchApi(`${this._binaryApiUrl}?${params}`, {
|
|
113
|
+
method: "GET",
|
|
128
114
|
});
|
|
115
|
+
const blob = await response?.blob();
|
|
116
|
+
if (blob == null) {
|
|
117
|
+
const error = {
|
|
118
|
+
status: 500,
|
|
119
|
+
title: "Unexpected null response",
|
|
120
|
+
message: `No blob found for ${params}`,
|
|
121
|
+
};
|
|
122
|
+
throw error;
|
|
123
|
+
}
|
|
124
|
+
return blob;
|
|
129
125
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
126
|
+
/**
|
|
127
|
+
* Executes the given transaction.
|
|
128
|
+
*
|
|
129
|
+
* If the {@link Transaction} object contains binaries, the request is automatically
|
|
130
|
+
* sent using `multipart/form-data` to allow uploading binaries.
|
|
131
|
+
*
|
|
132
|
+
* @param transaction Transaction to execute
|
|
133
|
+
* @returns A promise returning the transaction result on success
|
|
134
|
+
*/
|
|
135
|
+
async rql(transaction) {
|
|
136
|
+
const binaries = getBinariesFromTransaction(transaction);
|
|
137
|
+
try {
|
|
138
|
+
if (binaries.length > 0) {
|
|
139
|
+
return await this.multipartTransaction(transaction, binaries);
|
|
139
140
|
}
|
|
140
|
-
|
|
141
|
-
this.
|
|
141
|
+
else {
|
|
142
|
+
return await this.jsonTransaction(transaction);
|
|
142
143
|
}
|
|
143
|
-
}
|
|
144
|
+
}
|
|
145
|
+
catch (e) {
|
|
146
|
+
this.handleUserErrors(e);
|
|
147
|
+
}
|
|
144
148
|
}
|
|
145
149
|
getTransactionParams(transaction) {
|
|
146
|
-
return transaction.queries.map((
|
|
147
|
-
query:
|
|
148
|
-
params:
|
|
150
|
+
return transaction.queries.map((rqlQuery) => ({
|
|
151
|
+
query: rqlQuery.rql,
|
|
152
|
+
params: serializeQueryParams(transaction, rqlQuery.params),
|
|
149
153
|
}));
|
|
150
154
|
}
|
|
151
|
-
multipartTransaction(transaction) {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
credentials: "include",
|
|
162
|
-
body: formData,
|
|
163
|
-
headers: {
|
|
164
|
-
"Content-Type": "multipart/form-data",
|
|
165
|
-
},
|
|
166
|
-
});
|
|
167
|
-
return new Transaction_1.TransactionResult(resultSets);
|
|
155
|
+
async multipartTransaction(transaction, binaries) {
|
|
156
|
+
const jsonParams = this.getTransactionParams(transaction);
|
|
157
|
+
const formData = new FormData();
|
|
158
|
+
formData.append("queries", JSON.stringify(jsonParams));
|
|
159
|
+
binaries.forEach((binary) => {
|
|
160
|
+
formData.append(binary.uuid, binary.blob);
|
|
161
|
+
});
|
|
162
|
+
const resultSets = await this.nonNullFetchApi(this._rqlApiUrl, {
|
|
163
|
+
method: "POST",
|
|
164
|
+
body: formData,
|
|
168
165
|
});
|
|
166
|
+
return new TransactionResult(transaction, resultSets);
|
|
169
167
|
}
|
|
170
|
-
jsonTransaction(transaction) {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
credentials: "include",
|
|
175
|
-
body: JSON.stringify(this.getTransactionParams(transaction)),
|
|
176
|
-
});
|
|
177
|
-
return new Transaction_1.TransactionResult(resultSets);
|
|
168
|
+
async jsonTransaction(transaction) {
|
|
169
|
+
const resultSets = await this.nonNullFetchApi(this._rqlApiUrl, {
|
|
170
|
+
method: "POST",
|
|
171
|
+
body: JSON.stringify(this.getTransactionParams(transaction)),
|
|
178
172
|
});
|
|
173
|
+
return new TransactionResult(transaction, resultSets);
|
|
179
174
|
}
|
|
180
175
|
handleUserErrors(error) {
|
|
181
176
|
if (typeof error === "object" &&
|
|
@@ -185,26 +180,24 @@ class Api {
|
|
|
185
180
|
"message" in error &&
|
|
186
181
|
typeof error.message === "string") {
|
|
187
182
|
if (error.title === "ValidationError") {
|
|
188
|
-
throw new
|
|
183
|
+
throw new ValidationError(error.message);
|
|
189
184
|
}
|
|
190
185
|
throw error;
|
|
191
186
|
}
|
|
192
187
|
throw error;
|
|
193
188
|
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
207
|
-
const result = yield fetchApi(input, init);
|
|
189
|
+
/**
|
|
190
|
+
* Behaves the same as `fetchAPI` but forbids null responses.
|
|
191
|
+
* If this function detects a null response from the API,
|
|
192
|
+
* the promise will be rejected with a 500 error code.
|
|
193
|
+
*
|
|
194
|
+
* @param input The API route to use
|
|
195
|
+
* @param init Additional request parameters
|
|
196
|
+
* @returns A promise with the non-null fetched result
|
|
197
|
+
*/
|
|
198
|
+
async nonNullFetchApi(url, init) {
|
|
199
|
+
const response = await this.fetchApi(url, init);
|
|
200
|
+
const result = await response?.json();
|
|
208
201
|
if (result == null) {
|
|
209
202
|
const error = {
|
|
210
203
|
status: 500,
|
|
@@ -214,50 +207,60 @@ function nonNullFetchApi(input, init) {
|
|
|
214
207
|
throw error;
|
|
215
208
|
}
|
|
216
209
|
return result;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Sends a request to the given API route with the `Content-Type` header set and waits for a response.
|
|
213
|
+
* The returned promise will be rejected if the API returns HTTP error codes.
|
|
214
|
+
* The rejected promise will contain an `ApiErrorResponse` object.
|
|
215
|
+
*
|
|
216
|
+
* @param input The API route to use
|
|
217
|
+
* @param init Additional request parameters
|
|
218
|
+
* @returns A promise with the response
|
|
219
|
+
*/
|
|
220
|
+
async fetchApi(url, init) {
|
|
221
|
+
const headers = new Headers(init.headers);
|
|
222
|
+
// Add custom header used as CSRF protection
|
|
223
|
+
// note: it is only necessary for non GET requests but can also be useful
|
|
224
|
+
// while debugging.
|
|
225
|
+
if (!headers.has("X-Client-Name")) {
|
|
226
|
+
headers.append("X-Client-Name", "XMLHttpRequest");
|
|
227
|
+
}
|
|
228
|
+
// Add a "Content-Type" header whenever a body is passed.
|
|
229
|
+
// The header might already have been set (either by the caller of this
|
|
230
|
+
// function or by and encoder)
|
|
231
|
+
if (init.body &&
|
|
232
|
+
!headers.has("Content-Type") &&
|
|
233
|
+
!(init.body instanceof FormData)) {
|
|
234
|
+
headers.append("Content-Type", "application/json;charset=UTF-8");
|
|
235
|
+
}
|
|
236
|
+
init.headers = headers;
|
|
237
|
+
// Authenticate request
|
|
238
|
+
if (this._authenticator) {
|
|
239
|
+
const authenticatedRequest = await this._authenticator.authenticate(url, init);
|
|
240
|
+
url = authenticatedRequest.url;
|
|
241
|
+
init = authenticatedRequest.init;
|
|
235
242
|
}
|
|
236
|
-
|
|
243
|
+
// Execute request
|
|
244
|
+
const response = await fetch(url, init);
|
|
245
|
+
// Parse response body and handle errors
|
|
237
246
|
const status = response.status;
|
|
238
247
|
if (status >= 200 && status < 300) {
|
|
239
248
|
if (status === 204) {
|
|
240
249
|
return null;
|
|
241
250
|
}
|
|
242
|
-
|
|
243
|
-
return yield response.json();
|
|
244
|
-
}
|
|
245
|
-
catch (e) {
|
|
246
|
-
console.error(e);
|
|
247
|
-
return null;
|
|
248
|
-
}
|
|
251
|
+
return response;
|
|
249
252
|
}
|
|
250
253
|
else {
|
|
251
254
|
let json;
|
|
252
255
|
try {
|
|
253
|
-
json =
|
|
256
|
+
json = await response.json();
|
|
254
257
|
}
|
|
255
258
|
catch (e) {
|
|
256
259
|
console.error(e);
|
|
257
260
|
throw { status, title: "Error", message: "" };
|
|
258
261
|
}
|
|
259
|
-
throw
|
|
262
|
+
throw { status, ...json };
|
|
260
263
|
}
|
|
261
|
-
}
|
|
264
|
+
}
|
|
262
265
|
}
|
|
263
266
|
//# 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":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAE/C,OAAO,EAAE,0BAA0B,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAI/E,OAAO,EAEL,iBAAiB,GAClB,MAAM,oCAAoC,CAAC;AA6D5C;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,OAAO,GAAG;IACd,OAAO,CAAS;IAChB,QAAQ,GAAG,IAAI,CAAC;IAChB,UAAU,CAAS;IACnB,aAAa,CAAS;IACtB,YAAY,CAAS;IACrB,aAAa,CAAS;IACtB,kBAAkB,CAAS;IAC3B,aAAa,CAAS;IACtB,cAAc,CAAiB;IAEvC;;;;;;;;;OASG;IACH,YAAY,MAAc,EAAE,OAAmB;QAC7C,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QACtB,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;QAC1C,IAA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,MAAM,EAAE,GAAG,IAAI,EAAE,CAAC;QAC5B,CAAC;IACH,CAAC;CACF"}
|
package/lib/api/Client.d.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { Authenticator } from "../Authenticator.js";
|
|
2
|
+
import { Schema } from "../schema/classes/Schema.js";
|
|
3
|
+
import { RQLQueryParams, CurrentUser } from "./Api.js";
|
|
4
|
+
import { ResultSet } from "./ResultSet.js";
|
|
5
|
+
import { Transaction } from "./transaction/Transaction.js";
|
|
6
|
+
import { TransactionResult } from "./transaction/TransactionResult.js";
|
|
7
|
+
export interface ClientOptions {
|
|
8
|
+
authenticator?: Authenticator;
|
|
9
|
+
}
|
|
4
10
|
/**
|
|
5
11
|
* Class used to communicate with a CubicWeb instance's API.
|
|
6
12
|
*
|
|
@@ -15,27 +21,30 @@ export default class Client {
|
|
|
15
21
|
* Example: if you can retrieve the schema with https://example.com/api/v1/schema,
|
|
16
22
|
* use https://example.com/api as `apiUrl`.
|
|
17
23
|
*/
|
|
18
|
-
constructor(apiUrl: string);
|
|
24
|
+
constructor(apiUrl: string, options?: ClientOptions);
|
|
19
25
|
get apiUrl(): string;
|
|
20
26
|
/**
|
|
21
27
|
* Executes the given query and waits for the response.
|
|
22
28
|
*
|
|
23
|
-
*
|
|
29
|
+
* If binaries are given, the request is automatically
|
|
30
|
+
* sent using `multipart/form-data` to allow uploading binaries.
|
|
31
|
+
*
|
|
32
|
+
* @param rql The RQL query to send
|
|
24
33
|
* @param params The additional parameters for the request
|
|
25
34
|
* @returns A promise with the request's result set
|
|
26
35
|
*/
|
|
27
|
-
execute(
|
|
36
|
+
execute(rql: string, params?: RQLQueryParams): Promise<ResultSet>;
|
|
28
37
|
/**
|
|
29
38
|
* Retrieves the CubicWeb instance's JSON schema.
|
|
30
39
|
*
|
|
31
40
|
* @returns The CubicWeb instance's JSON schema.
|
|
32
41
|
*/
|
|
33
|
-
schema(): Promise<Schema<import("
|
|
42
|
+
schema(): Promise<Schema<import("../index.js").EntityRawSchemaArray, import("../index.js").RelationDefinitionRawSchemaArray<import("../index.js").EntityRawSchemaArray>>>;
|
|
34
43
|
/**
|
|
35
44
|
* Tries to log in the user on the configured CubicWeb instance with the given credentials.
|
|
36
45
|
*
|
|
37
46
|
* @param login The user login
|
|
38
|
-
* @param password
|
|
47
|
+
* @param password The user password
|
|
39
48
|
* @returns A promise returning true if the login was successful, false otherwise
|
|
40
49
|
*/
|
|
41
50
|
login(login: string, password: string): Promise<boolean>;
|
|
@@ -52,14 +61,21 @@ export default class Client {
|
|
|
52
61
|
*/
|
|
53
62
|
currentUser(): Promise<CurrentUser>;
|
|
54
63
|
/**
|
|
64
|
+
* Executes the given transaction.
|
|
65
|
+
*
|
|
66
|
+
* If the {@link Transaction} object contains binaries, the request is automatically
|
|
67
|
+
* sent using `multipart/form-data` to allow uploading binaries.
|
|
68
|
+
*
|
|
55
69
|
* @param transaction Transaction to execute
|
|
56
|
-
* @returns A promise
|
|
70
|
+
* @returns A promise returning the transaction result on success
|
|
57
71
|
*/
|
|
58
72
|
executeTransaction(transaction: Transaction): Promise<TransactionResult>;
|
|
59
73
|
/**
|
|
60
74
|
* Get the binary for the given entity attribute
|
|
61
75
|
*
|
|
62
|
-
* @
|
|
76
|
+
* @param eid Entity eid to fetch the binary from
|
|
77
|
+
* @param attribute Attribute on the entity in which the binary is stored
|
|
78
|
+
* @returns A promise resolving to the binary as a Blob
|
|
63
79
|
*/
|
|
64
80
|
getBinary(eid: number, attribute: string): Promise<Blob>;
|
|
65
81
|
}
|
package/lib/api/Client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Client.d.ts","sourceRoot":"","sources":["../../src/api/Client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"Client.d.ts","sourceRoot":"","sources":["../../src/api/Client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,MAAM,EAAE,MAAM,6BAA6B,CAAC;AACrD,OAAY,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAC5D,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C,OAAO,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAC3D,OAAO,EAAE,iBAAiB,EAAE,MAAM,oCAAoC,CAAC;AAEvE,MAAM,WAAW,aAAa;IAC5B,aAAa,CAAC,EAAE,aAAa,CAAC;CAC/B;AAED;;;;GAIG;AACH,MAAM,CAAC,OAAO,OAAO,MAAM;IACzB,OAAO,CAAC,GAAG,CAAM;IACjB;;;;;;OAMG;gBACS,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa;IAInD,IAAI,MAAM,WAET;IAED;;;;;;;;;OASG;IACG,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,GAAE,cAAmB,GAAG,OAAO,CAAC,SAAS,CAAC;IAQ3E;;;;OAIG;IACG,MAAM;IAKZ;;;;;;OAMG;IACG,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM;IAI3C;;;;OAIG;IACG,MAAM;IAIZ;;;;OAIG;IACG,WAAW,IAAI,OAAO,CAAC,WAAW,CAAC;IAIzC;;;;;;;;OAQG;IACG,kBAAkB,CACtB,WAAW,EAAE,WAAW,GACvB,OAAO,CAAC,iBAAiB,CAAC;IAI7B;;;;;;OAMG;IACG,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM;CAG/C"}
|