@cubicweb/client 1.5.0 → 1.7.0

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 ADDED
@@ -0,0 +1,207 @@
1
+ # @cubicweb/client
2
+
3
+ This library is the [CubicWeb](https://cubicweb.org/) JavaScript client
4
+ used to communicate with instances running with the
5
+ [API cube](https://forge.extranet.logilab.fr/cubicweb/cubes/api).
6
+ It replaces the [cwclientlibjs](https://forge.extranet.logilab.fr/open-source/cwclientlibjs)
7
+ library.
8
+
9
+ The full documentation is available [here](https://cubicweb.pages.logilab.fr/cubicwebjs/client).
10
+
11
+ ## Installation
12
+
13
+ @cubicweb/client is avalaible from NPM:
14
+ ```bash
15
+ # With NPM
16
+ npm i @cubicweb/client
17
+ # Or with Yarn
18
+ yarn add @cubicweb/client
19
+ ```
20
+
21
+ Then make sure you have a CubicWeb instance running with the
22
+ [API cube](https://forge.extranet.logilab.fr/cubicweb/cubes/api).
23
+
24
+ ## Usage
25
+
26
+ This library exposes a `Client` class used to send requests to the API.
27
+ This class requires the URL to your CubicWeb instance API endpoint
28
+ (eg: `https://example.com/api`).
29
+
30
+ ```js
31
+ const client = new Client("https://example.com/api")
32
+ ```
33
+
34
+ **⚠️ Real API routes include the API version in the URL (eg: `v1`),
35
+ but it must not appear here. The API version number will be automatically set by the client.**
36
+
37
+ For example, if you used `https://example.com/api/v1/schema` to retrieve your instance schema,
38
+ simply pass `https://example.com/api` to the client,
39
+ it will automatically add `/v1` on each request.
40
+
41
+ Your client object can then be used to send requests to the API.
42
+
43
+ See the [Client documentation](https://cubicweb.pages.logilab.fr/cubicwebjs/client/classes/Client.html) for more information.
44
+
45
+ ### Example
46
+
47
+ The following example tries to log in a user then performs some operations on the instance.
48
+
49
+ ```js
50
+ client
51
+ .login("login", "password")
52
+ // Login was successful
53
+ .then(() => {
54
+ // Retrieve the instance's schema
55
+ client
56
+ .schema()
57
+ .then((schema) => {
58
+ console.log("Found the instance's schema:");
59
+ console.log(schema);
60
+ })
61
+ .catch((e) => {
62
+ console.error("There was an error while fetching the schema:");
63
+ console.error(e);
64
+ });
65
+
66
+ // Execute an RQL query
67
+ client
68
+ .execute("Any X Where X eid 5")
69
+ .then((resultSet) => {
70
+ console.log("RQL request was successul and returned:");
71
+ console.log(resultSet);
72
+ })
73
+ .catch((e) => {
74
+ console.error("There was an error processing the RQL request:");
75
+ console.error(e);
76
+ });
77
+
78
+ // Execute a series of request in a transaction
79
+ client
80
+ .transaction(async (tr) => {
81
+ const insertResult = await tr.execute(
82
+ `INSERT Person X: X name "test"`
83
+ );
84
+ const insertedEid = insertResult?.[0]?.[0];
85
+ if (insertedEid != null) {
86
+ return tr.execute(`Any X Where X eid %(created_eid)s`, {
87
+ created_eid: insertedEid,
88
+ });
89
+ } else {
90
+ throw new Error("Error inserting Person");
91
+ }
92
+ })
93
+ .then((callbackResult) => {
94
+ console.log("Transaction was successul and the callback returned:");
95
+ console.log(callbackResult);
96
+ })
97
+ .catch((e) => {
98
+ console.error("There was an error processing the transaction:");
99
+ console.error(e);
100
+ });
101
+ })
102
+ // Login failed
103
+ .catch((e) => {
104
+ console.error("There was an error while loggin in the user:");
105
+ console.log(e);
106
+ });
107
+ ```
108
+
109
+ ## CORS handling
110
+
111
+ > Cross-Origin Resource Sharing (CORS) is an HTTP-header based mechanism that allows a server
112
+ > to indicate any origins (domain, scheme, or port) other than its own from which a browser
113
+ > should permit loading resources. -
114
+ > [MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS)
115
+
116
+ If the client is running on a different server from your CubicWeb instance,
117
+ you must authorize the URL the client is running on as well as the `Content-Type` header in your
118
+ [CubicWeb instance's configuration](https://cubicweb.readthedocs.io/en/latest/book/admin/instance-config#cross-origin-resource-sharing).
119
+
120
+ ## Authentication
121
+
122
+ The API cube uses cookie authentication using [JWT](https://jwt.io).
123
+ A `CW_JWT` [HTTP Only](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#security) cookie
124
+ will be automatically created by the browser when executing the login request.
125
+ This cookie cannot be accessed by the JavaScript so you will have to implement your own
126
+ logic to save the login state.
127
+
128
+ If the client is running on a different server from your CubicWeb instance,
129
+ make sure you setup CORS as described above.
130
+ For the login to work, you may also need to modify the cookie sent by the API cube to set
131
+ [`SameSite`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#samesite_attribute)
132
+ to `None`.
133
+
134
+ ## Using the Schema
135
+
136
+ This library provides CubicWeb [schema abstraction classes](packages/client/src/schema/Schema.ts). Three classes are available:
137
+ * `Schema` - Global handling of the schema.
138
+ Can be used to query entities and relations.
139
+ * `EntitySchema` - Handling of a single entity type.
140
+ Can be used to query relations and metadata about the entity.
141
+ * `RelationDefinitionSchema` - Handling of a single relation.
142
+ Can be used to retrieve associated entities and metadata the relation.
143
+
144
+ The `Schema` class is created from the JSON schema returned by the API.
145
+ The `Client.schema()` method takes care of this for you.
146
+ If your schema does not change often, it is recommended to export it using the following command
147
+ (replace `<YOUR_INSTANCE_NAME>` by the name of your instance):
148
+
149
+ ```bash
150
+ cubicweb-ctl export-schema -f json <YOUR_INSTANCE_NAME>
151
+ ```
152
+
153
+ You can then manually create the Schema object by passing your JSON schema to its constructor.
154
+
155
+ ```js
156
+ // Making the request with the client
157
+ const schema = client.schema();
158
+ // Using a local json schema
159
+ const schema = new Schema(your_raw_json_schema);
160
+ ```
161
+ `EntitySchema` and `RelationDefinitionSchema` objects should not be created manually.
162
+ These are returned when querying a `Schema` object.
163
+
164
+ ```js
165
+ // Retrieving an entity
166
+ const entitySchema = schema.getEntity("YourEntityType");
167
+ // Retrieving a relation
168
+ const relationDefinitionSchema = schema.matchRelationDefinitions({
169
+ subject: "YourEntityType",
170
+ type: "YourRelationType"',
171
+ object: "YourEntityType2"
172
+ });
173
+ ```
174
+
175
+ See the [documentation](https://cubicweb.pages.logilab.fr/cubicwebjs/client/modules.html) for more information.
176
+
177
+ ## TypeScript support
178
+
179
+ Both the client and the schema abstraction classes expose TypeScript types.
180
+
181
+ You can type the Schema class using the TypeScript representation of your instance schema
182
+ to benefit from the strong type inference. You can generate this representation using the command
183
+ (replace `<YOUR_INSTANCE_NAME>` by the name of your instance):
184
+
185
+ ```bash
186
+ cubicweb-ctl export-schema -f typescript <YOUR_INSTANCE_NAME>
187
+ ```
188
+
189
+ This command will generate a file `schema.ts` containing the typings.
190
+ You can then include it in your project. When using this generated type,
191
+ the TS engine will be able to detect invalid entity types and relations.
192
+
193
+ **⚠️ You cannot type the schema when using the `client.schema()` method.
194
+ This is intended to prevent errors if the JSON schema changes and not the TS schema.**
195
+ You must export both the JSON schema and TypeScript schema to use the typings.
196
+
197
+ ```js
198
+ // Type the schema when creating from a local JSON schema
199
+ const schema = new Schema<InstanceSchema["entities"], InstanceSchema["relations_definitions"]>(your_raw_json_schema);
200
+
201
+ // TS will complain it cannot assign type 'never'. This means your entity does not exist.
202
+ const entity = schema.getEntity("InvalidEntityType");
203
+ // TS will be happy and your entity will have the type 'ValidEntityType'.
204
+ const entity = schema.getEntity("ValidEntityType");
205
+ ```
206
+
207
+
package/lib/Errors.d.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  export declare class InconsistentSchemaError extends Error {
2
2
  constructor(message: string);
3
3
  }
4
+ export declare class ValidationError extends Error {
5
+ constructor(message: string);
6
+ }
4
7
  export declare class TransactionClosedError extends Error {
5
8
  constructor();
6
9
  }
@@ -1 +1 @@
1
- {"version":3,"file":"Errors.d.ts","sourceRoot":"","sources":["../src/Errors.ts"],"names":[],"mappings":"AAAA,qBAAa,uBAAwB,SAAQ,KAAK;gBACpC,OAAO,EAAE,MAAM;CAI5B;AAED,qBAAa,sBAAuB,SAAQ,KAAK;;CAKhD"}
1
+ {"version":3,"file":"Errors.d.ts","sourceRoot":"","sources":["../src/Errors.ts"],"names":[],"mappings":"AAAA,qBAAa,uBAAwB,SAAQ,KAAK;gBACpC,OAAO,EAAE,MAAM;CAI5B;AAED,qBAAa,eAAgB,SAAQ,KAAK;gBAC5B,OAAO,EAAE,MAAM;CAI5B;AAED,qBAAa,sBAAuB,SAAQ,KAAK;;CAKhD"}
package/lib/Errors.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TransactionClosedError = exports.InconsistentSchemaError = void 0;
3
+ exports.TransactionClosedError = exports.ValidationError = exports.InconsistentSchemaError = void 0;
4
4
  class InconsistentSchemaError extends Error {
5
5
  constructor(message) {
6
6
  super(`Inconsistent schema: ${message}`);
@@ -8,6 +8,13 @@ class InconsistentSchemaError extends Error {
8
8
  }
9
9
  }
10
10
  exports.InconsistentSchemaError = InconsistentSchemaError;
11
+ class ValidationError extends Error {
12
+ constructor(message) {
13
+ super(message);
14
+ Object.setPrototypeOf(this, new.target.prototype);
15
+ }
16
+ }
17
+ exports.ValidationError = ValidationError;
11
18
  class TransactionClosedError extends Error {
12
19
  constructor() {
13
20
  super("Trying to use a closed transaction");
package/lib/Errors.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"Errors.js","sourceRoot":"","sources":["../src/Errors.ts"],"names":[],"mappings":";;;AAAA,MAAa,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;AALD,0DAKC;AAED,MAAa,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;AALD,wDAKC"}
1
+ {"version":3,"file":"Errors.js","sourceRoot":"","sources":["../src/Errors.ts"],"names":[],"mappings":";;;AAAA,MAAa,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;AALD,0DAKC;AAED,MAAa,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;AALD,0CAKC;AAED,MAAa,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;AALD,wDAKC"}
package/lib/api/Api.d.ts CHANGED
@@ -2,7 +2,7 @@ import { RawSchema } from "../schema/raw/Schema";
2
2
  /**
3
3
  * Type representing an RQL request and its associated parameters.
4
4
  */
5
- export declare type RQLQuery = [string, RQLParams];
5
+ export type RQLQuery = [string, RQLParams];
6
6
  /**
7
7
  * Type representing RQL parameters to send along a request.
8
8
  */
@@ -12,15 +12,15 @@ export interface RQLParams {
12
12
  /**
13
13
  * Type representing a row returned in a result set.
14
14
  */
15
- export declare type RqlRow = Array<string | number | boolean | null>;
15
+ export type RqlRow = Array<string | number | boolean | null>;
16
16
  /**
17
17
  * Type representing a RQL request result.
18
18
  */
19
- export declare type ResultSet = Array<RqlRow>;
19
+ export type ResultSet = Array<RqlRow>;
20
20
  /**
21
21
  * Type representing the body of an error sent by CubicWeb's API.
22
22
  */
23
- declare type ApiError = {
23
+ type ApiError = {
24
24
  title: string;
25
25
  message: string;
26
26
  data?: Record<string, unknown>;
@@ -28,7 +28,7 @@ declare type ApiError = {
28
28
  /**
29
29
  * Type representing the error data sent to clients when retrieving an API error.
30
30
  */
31
- export declare type ApiErrorResponse = {
31
+ export type ApiErrorResponse = {
32
32
  status: number;
33
33
  } & ApiError;
34
34
  /**
@@ -108,6 +108,7 @@ export default class Api {
108
108
  * @param uuid The transaction's uuid
109
109
  */
110
110
  transactionRollback(uuid: string): Promise<null>;
111
+ private handleUserErrors;
111
112
  }
112
113
  export {};
113
114
  //# sourceMappingURL=Api.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Api.d.ts","sourceRoot":"","sources":["../../src/api/Api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEjD;;GAEG;AACH,oBAAY,QAAQ,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;AAE3C;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC;CAChC;AAED;;GAEG;AACH,oBAAY,MAAM,GAAG,KAAK,CAAC,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC,CAAC;AAE7D;;GAEG;AACH,oBAAY,SAAS,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC;AAEtC;;GAEG;AACH,aAAK,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;;GAEG;AACH,oBAAY,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,kBAAkB,CAKxB;IAEF;;;;;;;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AO,CAAC,IAAI,CAAC;IAQtD,OAAO,CAAC,gBAAgB;CAgBzB"}
package/lib/api/Api.js CHANGED
@@ -9,6 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
+ const Errors_1 = require("../Errors");
12
13
  /**
13
14
  * Class used to handle requests to the CubicWeb API.
14
15
  *
@@ -58,11 +59,16 @@ class Api {
58
59
  */
59
60
  execute(query, params) {
60
61
  return __awaiter(this, void 0, void 0, function* () {
61
- return nonNullFetchApi(this._rqlApiUrl, {
62
- method: "POST",
63
- body: JSON.stringify({ query, params }),
64
- credentials: "include", // FIXME is it secure?
65
- });
62
+ try {
63
+ return yield nonNullFetchApi(this._rqlApiUrl, {
64
+ method: "POST",
65
+ body: JSON.stringify({ query, params }),
66
+ credentials: "include", // FIXME is it secure?
67
+ });
68
+ }
69
+ catch (e) {
70
+ this.handleUserErrors(e);
71
+ }
66
72
  });
67
73
  }
68
74
  /**
@@ -87,16 +93,20 @@ class Api {
87
93
  */
88
94
  login(login, password) {
89
95
  return __awaiter(this, void 0, void 0, function* () {
90
- return fetchApi(this._loginApiUrl, {
91
- method: "POST",
92
- body: JSON.stringify({
93
- login,
94
- password,
95
- }),
96
- credentials: "include",
97
- })
98
- .then(() => true)
99
- .catch(() => false);
96
+ try {
97
+ yield fetchApi(this._loginApiUrl, {
98
+ method: "POST",
99
+ body: JSON.stringify({
100
+ login,
101
+ password,
102
+ }),
103
+ credentials: "include",
104
+ });
105
+ return true;
106
+ }
107
+ catch (_error) {
108
+ return false;
109
+ }
100
110
  });
101
111
  }
102
112
  /**
@@ -122,11 +132,16 @@ class Api {
122
132
  */
123
133
  transactionExecute(uuid, query, params) {
124
134
  return __awaiter(this, void 0, void 0, function* () {
125
- return fetchApi(this._transactionApiUrl.execute, {
126
- method: "POST",
127
- body: JSON.stringify({ uuid, query, params }),
128
- credentials: "include",
129
- });
135
+ try {
136
+ return yield fetchApi(this._transactionApiUrl.execute, {
137
+ method: "POST",
138
+ body: JSON.stringify({ uuid, query, params }),
139
+ credentials: "include",
140
+ });
141
+ }
142
+ catch (e) {
143
+ this.handleUserErrors(e);
144
+ }
130
145
  });
131
146
  }
132
147
  /**
@@ -136,11 +151,16 @@ class Api {
136
151
  */
137
152
  transactionCommit(uuid) {
138
153
  return __awaiter(this, void 0, void 0, function* () {
139
- return fetchApi(this._transactionApiUrl.commit, {
140
- method: "POST",
141
- body: JSON.stringify({ uuid }),
142
- credentials: "include",
143
- });
154
+ try {
155
+ return yield fetchApi(this._transactionApiUrl.commit, {
156
+ method: "POST",
157
+ body: JSON.stringify({ uuid }),
158
+ credentials: "include",
159
+ });
160
+ }
161
+ catch (e) {
162
+ this.handleUserErrors(e);
163
+ }
144
164
  });
145
165
  }
146
166
  /**
@@ -157,6 +177,20 @@ class Api {
157
177
  });
158
178
  });
159
179
  }
180
+ handleUserErrors(error) {
181
+ if (typeof error === "object" &&
182
+ error !== null &&
183
+ "title" in error &&
184
+ typeof error.title === "string" &&
185
+ "message" in error &&
186
+ typeof error.message === "string") {
187
+ if (error.title === "ValidationError") {
188
+ throw new Errors_1.ValidationError(error.message);
189
+ }
190
+ throw error;
191
+ }
192
+ throw error;
193
+ }
160
194
  }
161
195
  exports.default = Api;
162
196
  /**
@@ -168,23 +202,20 @@ exports.default = Api;
168
202
  * @param init Additional request parameters
169
203
  * @returns A promise with the non-null fetched result
170
204
  */
171
- const nonNullFetchApi = (input, init) => {
172
- return new Promise((resolve, reject) => fetchApi(input, init)
173
- .then((v) => {
174
- if (v == null) {
205
+ function nonNullFetchApi(input, init) {
206
+ return __awaiter(this, void 0, void 0, function* () {
207
+ const result = yield fetchApi(input, init);
208
+ if (result == null) {
175
209
  const error = {
176
210
  status: 500,
177
211
  title: "Unexpected null response",
178
212
  message: "Response was null but a value was expected",
179
213
  };
180
- reject(error);
214
+ throw error;
181
215
  }
182
- else {
183
- resolve(v);
184
- }
185
- })
186
- .catch(reject));
187
- };
216
+ return result;
217
+ });
218
+ }
188
219
  /**
189
220
  * Sends a request to the given API route with the `Content-Type` header set and waits for a response.
190
221
  * The returned promise will be rejected if the API returns HTTP error codes.
@@ -194,38 +225,37 @@ const nonNullFetchApi = (input, init) => {
194
225
  * @param init Additional request parameters
195
226
  * @returns A promise with the fetched result
196
227
  */
197
- const fetchApi = (input, init) => {
198
- let headers = init === null || init === void 0 ? void 0 : init.headers;
199
- if ((init === null || init === void 0 ? void 0 : init.method) && init.method !== "GET" && init.method !== "DELETE") {
200
- headers = Object.assign({ "Content-Type": "application/json;charset=UTF-8" }, init === null || init === void 0 ? void 0 : init.headers);
201
- }
202
- return new Promise((resolve, reject) => fetch(input, Object.assign(Object.assign({}, init), { headers })).then((response) => {
228
+ function fetchApi(input, init) {
229
+ return __awaiter(this, void 0, void 0, function* () {
230
+ let headers = init === null || init === void 0 ? void 0 : init.headers;
231
+ if ((init === null || init === void 0 ? void 0 : init.method) && init.method !== "GET" && init.method !== "DELETE") {
232
+ headers = Object.assign({ "Content-Type": "application/json;charset=UTF-8" }, init === null || init === void 0 ? void 0 : init.headers);
233
+ }
234
+ const response = yield fetch(input, Object.assign(Object.assign({}, init), { headers }));
203
235
  const status = response.status;
204
236
  if (status >= 200 && status < 300) {
205
237
  if (status === 204) {
206
- resolve(null);
238
+ return null;
207
239
  }
208
- else {
209
- response
210
- .json()
211
- .then(resolve)
212
- .catch((e) => {
213
- console.error(e);
214
- resolve(null);
215
- });
240
+ try {
241
+ return yield response.json();
242
+ }
243
+ catch (e) {
244
+ console.error(e);
245
+ return null;
216
246
  }
217
247
  }
218
248
  else {
219
- response
220
- .json()
221
- .then((data) => {
222
- reject(Object.assign({ status }, data));
223
- })
224
- .catch((e) => {
249
+ let json;
250
+ try {
251
+ json = yield response.json();
252
+ }
253
+ catch (e) {
225
254
  console.error(e);
226
- reject({ status, title: "Error", message: "" });
227
- });
255
+ throw { status, title: "Error", message: "" };
256
+ }
257
+ throw Object.assign({ status }, json);
228
258
  }
229
- }));
230
- };
259
+ });
260
+ }
231
261
  //# sourceMappingURL=Api.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"Api.js","sourceRoot":"","sources":["../../src/api/Api.ts"],"names":[],"mappings":";;;;;;;;;;;AAwCA;;;;;;;GAOG;AACH,MAAqB,GAAG;IAatB;;;;;;;;;OASG;IACH,YAAY,MAAc;QArBlB,aAAQ,GAAG,IAAI,CAAC;QAIhB,uBAAkB,GAAG;YAC3B,KAAK,EAAE,EAAE;YACT,OAAO,EAAE,EAAE;YACX,MAAM,EAAE,EAAE;YACV,QAAQ,EAAE,EAAE;SACb,CAAC;QAaA,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QACtB,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;QAC1C,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;QAChD,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;QAC9C,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAAC,OAAO,CAC1C,CAAC,CAAC,EAAE,EAAE,CACJ,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAAyC,CAAC;YACjE,IAAI,CAAC,WAAW,CAAC,eAAe,CAAC,EAAE,CAAC,CAAC,CAC1C,CAAC;IACJ,CAAC;IAED,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAEO,WAAW,CAAC,KAAa;QAC/B,OAAO,GAAG,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,QAAQ,IAAI,KAAK,EAAE,CAAC;IACrD,CAAC;IAED;;;;;;OAMG;IACG,OAAO,CAAC,KAAa,EAAE,MAAiB;;YAC5C,OAAO,eAAe,CAAY,IAAI,CAAC,UAAU,EAAE;gBACjD,MAAM,EAAE,MAAM;gBACd,IAAI,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AAO,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC;aAC/C;YACD,sBAAQ,MAAM,IAAK,IAAI,EAAG;SAC3B;IACH,CAAC;CAAA"}
@@ -1,5 +1,5 @@
1
1
  import { Schema } from "../schema/classes/Schema";
2
- import { RQLParams } from "./Api";
2
+ import { RQLParams, ResultSet } from "./Api";
3
3
  import Transaction from "./Transaction";
4
4
  /**
5
5
  * Class used to communicate with a CubicWeb instance's API.
@@ -22,7 +22,7 @@ export default class Client {
22
22
  * @param params The additional parameters for the request
23
23
  * @returns A promise with the request's result set
24
24
  */
25
- execute(query: string, params?: RQLParams): Promise<import("./Api").ResultSet>;
25
+ execute(query: string, params?: RQLParams): Promise<ResultSet>;
26
26
  /**
27
27
  * Retrieves the CubicWeb instance's JSON schema.
28
28
  *
@@ -41,6 +41,8 @@ export default class Client {
41
41
  * Creates a new transaction and exposes the Transaction object in the given callback.
42
42
  * Use this callback to execute RQL requests as part of the transaction.
43
43
  * Once the callback's end is reached, the transaction is automatically committed.
44
+ * Note that any exception thrown by the callback will lead to the
45
+ * transaction being rolled back.
44
46
  *
45
47
  * @param callback Function to execute transaction requests in.
46
48
  * @returns The promise returned by the callback
@@ -1 +1 @@
1
- {"version":3,"file":"Client.d.ts","sourceRoot":"","sources":["../../src/api/Client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAC;AAClD,OAAY,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AACvC,OAAO,WAAW,MAAM,eAAe,CAAC;AAExC;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,MAAM;IACzB,OAAO,CAAC,GAAG,CAAM;IAEjB;;;;;;OAMG;gBACS,MAAM,EAAE,MAAM;IAI1B,IAAI,MAAM,WAET;IAED;;;;;;OAMG;IACG,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,GAAE,SAAc;IAInD;;;;OAIG;IACG,MAAM;IAKZ;;;;;;OAMG;IACG,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM;IAI3C;;;;;;;OAOG;IACG,WAAW,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,EAAE,WAAW,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;CAS5E"}
1
+ {"version":3,"file":"Client.d.ts","sourceRoot":"","sources":["../../src/api/Client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAC;AAClD,OAAY,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAClD,OAAO,WAAW,MAAM,eAAe,CAAC;AAExC;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,MAAM;IACzB,OAAO,CAAC,GAAG,CAAM;IAEjB;;;;;;OAMG;gBACS,MAAM,EAAE,MAAM;IAI1B,IAAI,MAAM,WAET;IAED;;;;;;OAMG;IACG,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,GAAE,SAAc,GAAG,OAAO,CAAC,SAAS,CAAC;IAIxE;;;;OAIG;IACG,MAAM;IAKZ;;;;;;OAMG;IACG,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM;IAI3C;;;;;;;;;OASG;IACG,WAAW,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,EAAE,WAAW,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;CAkB5E"}
package/lib/api/Client.js CHANGED
@@ -71,6 +71,8 @@ class Client {
71
71
  * Creates a new transaction and exposes the Transaction object in the given callback.
72
72
  * Use this callback to execute RQL requests as part of the transaction.
73
73
  * Once the callback's end is reached, the transaction is automatically committed.
74
+ * Note that any exception thrown by the callback will lead to the
75
+ * transaction being rolled back.
74
76
  *
75
77
  * @param callback Function to execute transaction requests in.
76
78
  * @returns The promise returned by the callback
@@ -79,7 +81,16 @@ class Client {
79
81
  return __awaiter(this, void 0, void 0, function* () {
80
82
  const transaction = new Transaction_1.default(this.api);
81
83
  yield transaction.begin();
82
- const result = yield callback(transaction);
84
+ let result;
85
+ try {
86
+ result = yield callback(transaction);
87
+ }
88
+ catch (error) {
89
+ if (!transaction.isClosed()) {
90
+ yield transaction.rollback();
91
+ }
92
+ throw error;
93
+ }
83
94
  if (!transaction.isClosed()) {
84
95
  yield transaction.commit();
85
96
  }
@@ -1 +1 @@
1
- {"version":3,"file":"Client.js","sourceRoot":"","sources":["../../src/api/Client.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,qDAAkD;AAClD,gDAAuC;AACvC,gEAAwC;AAExC;;GAEG;AACH,MAAqB,MAAM;IAGzB;;;;;;OAMG;IACH,YAAY,MAAc;QACxB,IAAI,CAAC,GAAG,GAAG,IAAI,aAAG,CAAC,MAAM,CAAC,CAAC;IAC7B,CAAC;IAED,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC;IACzB,CAAC;IAED;;;;;;OAMG;IACG,OAAO,CAAC,KAAa,EAAE,SAAoB,EAAE;;YACjD,OAAO,MAAM,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QAC/C,CAAC;KAAA;IAED;;;;OAIG;IACG,MAAM;;YACV,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC;YACvC,OAAO,IAAI,eAAM,CAAC,MAAM,CAAC,CAAC;QAC5B,CAAC;KAAA;IAED;;;;;;OAMG;IACG,KAAK,CAAC,KAAa,EAAE,QAAgB;;YACzC,OAAO,MAAM,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;QAC/C,CAAC;KAAA;IAED;;;;;;;OAOG;IACG,WAAW,CAAI,QAAyC;;YAC5D,MAAM,WAAW,GAAG,IAAI,qBAAW,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;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,MAAM,WAAW,CAAC,KAAK,EAAE,CAAC;YAE1B,IAAI,MAAS,CAAC;YACd,IAAI;gBACF,MAAM,GAAG,MAAM,QAAQ,CAAC,WAAW,CAAC,CAAC;aACtC;YAAC,OAAO,KAAK,EAAE;gBACd,IAAI,CAAC,WAAW,CAAC,QAAQ,EAAE,EAAE;oBAC3B,MAAM,WAAW,CAAC,QAAQ,EAAE,CAAC;iBAC9B;gBACD,MAAM,KAAK,CAAC;aACb;YACD,IAAI,CAAC,WAAW,CAAC,QAAQ,EAAE,EAAE;gBAC3B,MAAM,WAAW,CAAC,MAAM,EAAE,CAAC;aAC5B;YACD,OAAO,MAAM,CAAC;QAChB,CAAC;KAAA;CACF;AA9ED,yBA8EC"}
@@ -19,12 +19,12 @@ export declare function cardinalityRequired(cardinality: Cardinality): boolean;
19
19
  /**
20
20
  * Partial raw relation definition schema type.
21
21
  */
22
- export declare type RelationDefinitionMatcher<E extends EntityRawSchemaArray> = Partial<RelationDefinitionRawSchema<E>>;
22
+ export type RelationDefinitionMatcher<E extends EntityRawSchemaArray> = Partial<RelationDefinitionRawSchema<E>>;
23
23
  /**
24
24
  * Type representing a raw relation definition matched from a partial raw relation definition schema.
25
25
  * If it finds a relation definition matching the partial one given, it will return its type.
26
26
  * If it cannot find any relation definition matching, it will return the `never` type
27
27
  * and TypeScript will give compile errors.
28
28
  */
29
- export declare type MatchedRelationDefinition<E extends EntityRawSchemaArray, R extends RelationDefinitionRawSchemaArray<EntityRawSchemaArray>, M extends RelationDefinitionMatcher<E>> = R[number] extends infer RDef ? (RDef extends M ? RDef : never) : never;
29
+ export type MatchedRelationDefinition<E extends EntityRawSchemaArray, R extends RelationDefinitionRawSchemaArray<EntityRawSchemaArray>, M extends RelationDefinitionMatcher<E>> = R[number] extends infer RDef ? (RDef extends M ? RDef : never) : never;
30
30
  //# sourceMappingURL=Utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Utils.d.ts","sourceRoot":"","sources":["../../../src/schema/classes/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