@cubicweb/client 3.0.0-alpha.4 → 3.0.0-alpha.6
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/api/Api.d.ts +25 -10
- package/lib/api/Api.d.ts.map +1 -1
- package/lib/api/Api.js +69 -43
- package/lib/api/Api.js.map +1 -1
- package/lib/api/Client.d.ts +11 -6
- package/lib/api/Client.d.ts.map +1 -1
- package/lib/api/Client.js +20 -11
- package/lib/api/Client.js.map +1 -1
- package/lib/api/Transaction.d.ts +30 -21
- package/lib/api/Transaction.d.ts.map +1 -1
- package/lib/api/Transaction.js +44 -36
- package/lib/api/Transaction.js.map +1 -1
- package/lib/schema/classes/EntitySchema.d.ts +2 -0
- package/lib/schema/classes/EntitySchema.d.ts.map +1 -1
- package/lib/schema/classes/EntitySchema.js +2 -0
- package/lib/schema/classes/EntitySchema.js.map +1 -1
- package/lib/schema/classes/RelationDefinitionSchema.d.ts +2 -0
- package/lib/schema/classes/RelationDefinitionSchema.d.ts.map +1 -1
- package/lib/schema/classes/RelationDefinitionSchema.js +2 -0
- package/lib/schema/classes/RelationDefinitionSchema.js.map +1 -1
- package/lib/schema/classes/Schema.d.ts +2 -0
- package/lib/schema/classes/Schema.d.ts.map +1 -1
- package/lib/schema/classes/Schema.js +2 -0
- package/lib/schema/classes/Schema.js.map +1 -1
- package/lib/schema/classes/Utils.d.ts +28 -0
- package/lib/schema/classes/Utils.d.ts.map +1 -1
- package/lib/schema/classes/Utils.js +22 -0
- 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/Entities.d.ts +4 -0
- package/lib/schema/raw/Entities.d.ts.map +1 -1
- package/lib/schema/raw/RelationDefinitions.d.ts +12 -0
- package/lib/schema/raw/RelationDefinitions.d.ts.map +1 -1
- package/lib/schema/raw/Schema.d.ts +2 -0
- package/lib/schema/raw/Schema.d.ts.map +1 -1
- package/lib/schema/raw/Utils.d.ts +6 -0
- package/lib/schema/raw/Utils.d.ts.map +1 -1
- package/package.json +4 -8
- package/src/api/Api.ts +81 -40
- package/src/api/Client.ts +24 -20
- package/src/api/Transaction.ts +57 -31
- package/src/schema/classes/EntitySchema.ts +2 -0
- package/src/schema/classes/RelationDefinitionSchema.ts +2 -0
- package/src/schema/classes/Schema.ts +2 -0
- package/src/schema/classes/Utils.ts +28 -0
- package/src/schema/raw/Constraints.ts +20 -8
- package/src/schema/raw/Entities.ts +4 -0
- package/src/schema/raw/RelationDefinitions.ts +12 -0
- package/src/schema/raw/Schema.ts +2 -0
- package/src/schema/raw/Utils.ts +6 -0
|
@@ -3,10 +3,14 @@ import { AttributeConstraint, RelationConstraint } from "./Constraints";
|
|
|
3
3
|
import { EntityRawSchema, EntityRawSchemaArray } from "./Entities";
|
|
4
4
|
/**
|
|
5
5
|
* Type representing all the possible cardinalities in CubicWeb.
|
|
6
|
+
*
|
|
7
|
+
* @category Schema
|
|
6
8
|
*/
|
|
7
9
|
export type Cardinality = "*" | "1" | "?" | "+";
|
|
8
10
|
/**
|
|
9
11
|
* Type representing a cardinality pair as used in relation definitions.
|
|
12
|
+
*
|
|
13
|
+
* @category Schema
|
|
10
14
|
*/
|
|
11
15
|
export type CardinalityPair = `${Cardinality}${Cardinality}`;
|
|
12
16
|
/**
|
|
@@ -74,6 +78,8 @@ interface CustomBuildObj<T extends string> extends BuildObjBase<T> {
|
|
|
74
78
|
export type BuildObj = Readonly<StringBuildObj | PasswordBuildObj | BytesBuildObj | IntBuildObj | BigIntBuildObj | FloatBuildObj | BooleanBuildObj | DecimalBuildObj | DateBuildObj | TimeBuildObj | DatetimeBuildObj | TZTimeBuildObj | TZDatetimeBuildObj | IntervalBuildObj | CustomBuildObj<string>>;
|
|
75
79
|
/**
|
|
76
80
|
* Type representing the possible options for an attribute.
|
|
81
|
+
*
|
|
82
|
+
* @category Schema
|
|
77
83
|
*/
|
|
78
84
|
export type AttributeOptions = {
|
|
79
85
|
indexed?: boolean;
|
|
@@ -82,6 +88,8 @@ export type AttributeOptions = {
|
|
|
82
88
|
};
|
|
83
89
|
/**
|
|
84
90
|
* Type representing the possible options for a relation definition.
|
|
91
|
+
*
|
|
92
|
+
* @category Schema
|
|
85
93
|
*/
|
|
86
94
|
export type RelationOptions = {
|
|
87
95
|
symmetric?: boolean;
|
|
@@ -94,6 +102,8 @@ export type RelationOptions = {
|
|
|
94
102
|
* It can represent both attributes and relations depending on the value of Final.
|
|
95
103
|
*
|
|
96
104
|
* It can infer default values type depending on the object type if the relation is final.
|
|
105
|
+
*
|
|
106
|
+
* @category Schema
|
|
97
107
|
*/
|
|
98
108
|
export type RelationDefinitionRawSchema<ESchemas extends EntityRawSchemaArray, Final extends boolean = boolean> = Readonly<{
|
|
99
109
|
type: string;
|
|
@@ -112,6 +122,8 @@ export type RelationDefinitionRawSchema<ESchemas extends EntityRawSchemaArray, F
|
|
|
112
122
|
})>;
|
|
113
123
|
/**
|
|
114
124
|
* Type representing a list of relation definitions.
|
|
125
|
+
*
|
|
126
|
+
* @category Schema
|
|
115
127
|
*/
|
|
116
128
|
export type RelationDefinitionRawSchemaArray<ESchemas extends EntityRawSchemaArray> = ReadonlyArray<RelationDefinitionRawSchema<ESchemas, boolean>>;
|
|
117
129
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RelationDefinitions.d.ts","sourceRoot":"","sources":["../../../src/schema/raw/RelationDefinitions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACxE,OAAO,EAAE,eAAe,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAEnE
|
|
1
|
+
{"version":3,"file":"RelationDefinitions.d.ts","sourceRoot":"","sources":["../../../src/schema/raw/RelationDefinitions.ts"],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IG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,kBAAkB,CAAC,EAAE,UAAU,CAAC;IAChC,SAAS,CAAC,EAAE,UAAU,CAAC;CACxB,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,MAAM,2BAA2B,CACrC,QAAQ,SAAS,oBAAoB,EACrC,KAAK,SAAS,OAAO,GAAG,OAAO,IAC7B,QAAQ,CACV;IACE,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,eAAe,CAAC;IAC7B,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC;CACnC,GAAG,CAAC,KAAK,SAAS,IAAI,GACnB,QAAQ,GAAG;IACT,KAAK,EAAE,IAAI,CAAC;IACZ,WAAW,EAAE,mBAAmB,EAAE,CAAC;IACnC,OAAO,EAAE,gBAAgB,CAAC;CAC3B,GACD;IACE,MAAM,EAAE,eAAe,CAAC,MAAM,CAAC,CAAC;IAChC,KAAK,EAAE,KAAK,CAAC;IACb,WAAW,EAAE,kBAAkB,EAAE,CAAC;IAClC,OAAO,EAAE,eAAe,CAAC;CAC1B,CAAC,CACP,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,gCAAgC,CAC1C,QAAQ,SAAS,oBAAoB,IACnC,aAAa,CAAC,2BAA2B,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC,CAAC"}
|
|
@@ -5,6 +5,8 @@ import { RelationDefinitionRawSchemaArray } from "./RelationDefinitions";
|
|
|
5
5
|
* You can export this schema from your CubicWeb instance using the command:
|
|
6
6
|
*
|
|
7
7
|
* `cubicweb-ctl export-schema -f typescript <YOUR_INSTANCE_NAME>`
|
|
8
|
+
*
|
|
9
|
+
* @category Schema
|
|
8
10
|
*/
|
|
9
11
|
export type RawSchema<E extends EntityRawSchemaArray = EntityRawSchemaArray, R extends RelationDefinitionRawSchemaArray<E> = RelationDefinitionRawSchemaArray<E>> = {
|
|
10
12
|
entities: E;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../../../src/schema/raw/Schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAClD,OAAO,EAAE,gCAAgC,EAAE,MAAM,uBAAuB,CAAC;AAEzE
|
|
1
|
+
{"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../../../src/schema/raw/Schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAClD,OAAO,EAAE,gCAAgC,EAAE,MAAM,uBAAuB,CAAC;AAEzE;;;;;;;GAOG;AACH,MAAM,MAAM,SAAS,CACnB,CAAC,SAAS,oBAAoB,GAAG,oBAAoB,EACrD,CAAC,SAAS,gCAAgC,CAAC,CAAC,CAAC,GAAG,gCAAgC,CAAC,CAAC,CAAC,IACjF;IACF,QAAQ,EAAE,CAAC,CAAC;IACZ,qBAAqB,EAAE,CAAC,CAAC;CAC1B,CAAC"}
|
|
@@ -5,16 +5,22 @@ import { EntityRawSchemaArray } from "./Entities";
|
|
|
5
5
|
*
|
|
6
6
|
* In the given EntityRawSchemaArray, if the entities type is a string, the schema is loosely typed.
|
|
7
7
|
* If the schema is strongly typed, the entities type would be a word (which is stricter than string).
|
|
8
|
+
*
|
|
9
|
+
* @category Schema
|
|
8
10
|
*/
|
|
9
11
|
export type IsLooseSchema<E extends EntityRawSchemaArray> = string extends E[number]["type"] ? true : false;
|
|
10
12
|
/**
|
|
11
13
|
* Checks if the given entity type is present in the EntityRawSchemaArray.
|
|
12
14
|
* Use this to create conditional types.
|
|
15
|
+
*
|
|
16
|
+
* @category Schema
|
|
13
17
|
*/
|
|
14
18
|
export type IsETypeInStrictSchema<E extends EntityRawSchemaArray, EType extends string> = EType extends E[number]["type"] ? true : false;
|
|
15
19
|
/**
|
|
16
20
|
* Type representing the entity's raw schema matching this type.
|
|
17
21
|
* This type only works for EntityRawSchemaArray with literals as "type" property.
|
|
22
|
+
*
|
|
23
|
+
* @category Schema
|
|
18
24
|
*/
|
|
19
25
|
export type EntityRawSchemaByType<E extends EntityRawSchemaArray, T extends string> = E[number] extends infer ESchema ? ESchema extends {
|
|
20
26
|
type: T;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Utils.d.ts","sourceRoot":"","sources":["../../../src/schema/raw/Utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAElD
|
|
1
|
+
{"version":3,"file":"Utils.d.ts","sourceRoot":"","sources":["../../../src/schema/raw/Utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAElD;;;;;;;;GAQG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,SAAS,oBAAoB,IACtD,MAAM,SAAS,CAAC,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,GAAG,IAAI,GAAG,KAAK,CAAC;AAElD;;;;;GAKG;AACH,MAAM,MAAM,qBAAqB,CAC/B,CAAC,SAAS,oBAAoB,EAC9B,KAAK,SAAS,MAAM,IAClB,KAAK,SAAS,CAAC,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,GAAG,IAAI,GAAG,KAAK,CAAC;AAEnD;;;;;GAKG;AACH,MAAM,MAAM,qBAAqB,CAC/B,CAAC,SAAS,oBAAoB,EAC9B,CAAC,SAAS,MAAM,IACd,CAAC,CAAC,MAAM,CAAC,SAAS,MAAM,OAAO,GAC/B,OAAO,SAAS;IAAE,IAAI,EAAE,CAAC,CAAA;CAAE,GACzB,OAAO,GACP,KAAK,GACP,KAAK,CAAC"}
|
package/package.json
CHANGED
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
"name": "@cubicweb/client",
|
|
3
3
|
"description": "Official JS client for CubicWeb API",
|
|
4
4
|
"author": "Logilab",
|
|
5
|
-
"version": "3.0.0-alpha.
|
|
5
|
+
"version": "3.0.0-alpha.6",
|
|
6
6
|
"license": "LGPL-3.0-or-later",
|
|
7
7
|
"main": "lib/index.js",
|
|
8
8
|
"types": "lib/index.d.ts",
|
|
9
9
|
"scripts": {
|
|
10
|
-
"build": "tsc --project tsconfig.build.json",
|
|
11
|
-
"docs": "typedoc"
|
|
10
|
+
"build": "run -T tsc --project tsconfig.build.json",
|
|
11
|
+
"docs": "run -T typedoc"
|
|
12
12
|
},
|
|
13
13
|
"repository": {
|
|
14
14
|
"type": "hg",
|
|
@@ -23,9 +23,5 @@
|
|
|
23
23
|
"lib/**",
|
|
24
24
|
"src/**",
|
|
25
25
|
"!**/__tests__/**"
|
|
26
|
-
]
|
|
27
|
-
"devDependencies": {
|
|
28
|
-
"typedoc": "^0.23.24",
|
|
29
|
-
"typescript": "^4.9.4"
|
|
30
|
-
}
|
|
26
|
+
]
|
|
31
27
|
}
|
package/src/api/Api.ts
CHANGED
|
@@ -3,17 +3,21 @@ import { RawSchema } from "../schema/raw/Schema";
|
|
|
3
3
|
import {
|
|
4
4
|
TransactionResult,
|
|
5
5
|
Transaction,
|
|
6
|
-
|
|
6
|
+
isTransactionQueryScalarRef,
|
|
7
7
|
} from "./Transaction";
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Type representing an RQL request and its associated parameters.
|
|
11
|
+
*
|
|
12
|
+
* @category Api
|
|
11
13
|
*/
|
|
12
14
|
export type RQLQuery = [string, RQLParams];
|
|
13
15
|
|
|
14
16
|
export type RQLParamValue = string | number | boolean | null;
|
|
15
17
|
/**
|
|
16
18
|
* Type representing RQL parameters to send along a request.
|
|
19
|
+
*
|
|
20
|
+
* @category Api
|
|
17
21
|
*/
|
|
18
22
|
export interface RQLParams {
|
|
19
23
|
[key: string]: RQLParamValue;
|
|
@@ -21,16 +25,22 @@ export interface RQLParams {
|
|
|
21
25
|
|
|
22
26
|
/**
|
|
23
27
|
* Type representing a row returned in a result set.
|
|
28
|
+
*
|
|
29
|
+
* @category Api
|
|
24
30
|
*/
|
|
25
31
|
export type RqlRow = Array<string | number | boolean | null>;
|
|
26
32
|
|
|
27
33
|
/**
|
|
28
34
|
* Type representing a RQL request result.
|
|
35
|
+
*
|
|
36
|
+
* @category Api
|
|
29
37
|
*/
|
|
30
38
|
export type ResultSet = Array<RqlRow>;
|
|
31
39
|
|
|
32
40
|
/**
|
|
33
41
|
* Type representing the currently logged user's data
|
|
42
|
+
*
|
|
43
|
+
* @category Api
|
|
34
44
|
*/
|
|
35
45
|
export interface CurrentUser {
|
|
36
46
|
login: string;
|
|
@@ -40,6 +50,8 @@ export interface CurrentUser {
|
|
|
40
50
|
|
|
41
51
|
/**
|
|
42
52
|
* Type representing the body of an error sent by CubicWeb's API.
|
|
53
|
+
*
|
|
54
|
+
* @category Api
|
|
43
55
|
*/
|
|
44
56
|
type ApiError = {
|
|
45
57
|
title: string;
|
|
@@ -49,6 +61,8 @@ type ApiError = {
|
|
|
49
61
|
|
|
50
62
|
/**
|
|
51
63
|
* Type representing the error data sent to clients when retrieving an API error.
|
|
64
|
+
*
|
|
65
|
+
* @category Api
|
|
52
66
|
*/
|
|
53
67
|
export type ApiErrorResponse = {
|
|
54
68
|
status: number;
|
|
@@ -69,7 +83,7 @@ export default class Api {
|
|
|
69
83
|
private _schemaApiUrl: string;
|
|
70
84
|
private _loginApiUrl: string;
|
|
71
85
|
private _currentUserApiUrl: string;
|
|
72
|
-
private
|
|
86
|
+
private _binaryApiUrl: string;
|
|
73
87
|
|
|
74
88
|
/**
|
|
75
89
|
* Class used to handle requests to the CubicWeb API.
|
|
@@ -87,7 +101,7 @@ export default class Api {
|
|
|
87
101
|
this._schemaApiUrl = this.getApiRoute("schema");
|
|
88
102
|
this._loginApiUrl = this.getApiRoute("login");
|
|
89
103
|
this._currentUserApiUrl = this.getApiRoute("current-user");
|
|
90
|
-
this.
|
|
104
|
+
this._binaryApiUrl = this.getApiRoute("binary");
|
|
91
105
|
}
|
|
92
106
|
|
|
93
107
|
get apiUrl() {
|
|
@@ -98,25 +112,6 @@ export default class Api {
|
|
|
98
112
|
return `${this._apiUrl}/${this._version}/${route}`;
|
|
99
113
|
}
|
|
100
114
|
|
|
101
|
-
/**
|
|
102
|
-
* Executes the given query and waits for the response.
|
|
103
|
-
*
|
|
104
|
-
* @param query The RQL query to send
|
|
105
|
-
* @param params The additional parameters for the request
|
|
106
|
-
* @returns A promise with the request's result set
|
|
107
|
-
*/
|
|
108
|
-
async execute(query: string, params: RQLParams): Promise<ResultSet> {
|
|
109
|
-
try {
|
|
110
|
-
return await nonNullFetchApi<ResultSet>(this._rqlApiUrl, {
|
|
111
|
-
method: "POST",
|
|
112
|
-
body: JSON.stringify({ query, params }),
|
|
113
|
-
credentials: "include", // FIXME is it secure?
|
|
114
|
-
});
|
|
115
|
-
} catch (e) {
|
|
116
|
-
this.handleUserErrors(e);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
115
|
/**
|
|
121
116
|
* Retrieves the CubicWeb instance's JSON schema.
|
|
122
117
|
*
|
|
@@ -164,15 +159,41 @@ export default class Api {
|
|
|
164
159
|
});
|
|
165
160
|
}
|
|
166
161
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
162
|
+
/**
|
|
163
|
+
* Get the binary for the given entity attribute
|
|
164
|
+
*
|
|
165
|
+
* @returns A promise resolving to the binary
|
|
166
|
+
*/
|
|
167
|
+
async binary(eid: number, attribute: string): Promise<Blob> {
|
|
168
|
+
const params = new URLSearchParams({
|
|
169
|
+
eid: eid.toString(),
|
|
170
|
+
attribute,
|
|
171
|
+
});
|
|
172
|
+
return await nonNullFetchApi<Blob>(this._binaryApiUrl + params, {
|
|
173
|
+
method: "GET",
|
|
174
|
+
credentials: "include",
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async rql(transaction: Transaction): Promise<TransactionResult> {
|
|
179
|
+
try {
|
|
180
|
+
if (Object.keys(transaction.binaries).length > 0) {
|
|
181
|
+
return await this.multipartTransaction(transaction);
|
|
182
|
+
} else {
|
|
183
|
+
return await this.jsonTransaction(transaction);
|
|
184
|
+
}
|
|
185
|
+
} catch (e) {
|
|
186
|
+
this.handleUserErrors(e);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
private getTransactionParams(transaction: Transaction) {
|
|
191
|
+
return transaction.queries.map((transactionQuery) => ({
|
|
171
192
|
query: transactionQuery.query,
|
|
172
193
|
params: Object.fromEntries(
|
|
173
194
|
Object.entries(transactionQuery.params).map(([key, value]) => [
|
|
174
195
|
key,
|
|
175
|
-
|
|
196
|
+
isTransactionQueryScalarRef(value)
|
|
176
197
|
? {
|
|
177
198
|
queryIndex: value.queryIndex,
|
|
178
199
|
row: value.row,
|
|
@@ -182,19 +203,37 @@ export default class Api {
|
|
|
182
203
|
])
|
|
183
204
|
),
|
|
184
205
|
}));
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
this.
|
|
197
|
-
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private async multipartTransaction(transaction: Transaction) {
|
|
209
|
+
const jsonParams = this.getTransactionParams(transaction);
|
|
210
|
+
const formData = new FormData();
|
|
211
|
+
formData.append("queries", JSON.stringify(jsonParams));
|
|
212
|
+
Object.entries(transaction.binaries).forEach(([key, value]) => {
|
|
213
|
+
formData.append(key, value);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
const resultSets = await nonNullFetchApi<ReadonlyArray<ResultSet>>(
|
|
217
|
+
this._rqlApiUrl,
|
|
218
|
+
{
|
|
219
|
+
method: "POST",
|
|
220
|
+
credentials: "include",
|
|
221
|
+
body: formData,
|
|
222
|
+
}
|
|
223
|
+
);
|
|
224
|
+
return new TransactionResult(resultSets);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private async jsonTransaction(transaction: Transaction) {
|
|
228
|
+
const resultSets = await nonNullFetchApi<ReadonlyArray<ResultSet>>(
|
|
229
|
+
this._rqlApiUrl,
|
|
230
|
+
{
|
|
231
|
+
method: "POST",
|
|
232
|
+
credentials: "include",
|
|
233
|
+
body: JSON.stringify(this.getTransactionParams(transaction)),
|
|
234
|
+
}
|
|
235
|
+
);
|
|
236
|
+
return new TransactionResult(resultSets);
|
|
198
237
|
}
|
|
199
238
|
|
|
200
239
|
private handleUserErrors(error: unknown): never {
|
|
@@ -257,6 +296,8 @@ async function fetchApi<R = unknown>(
|
|
|
257
296
|
if (init?.method && init.method !== "GET" && init.method !== "DELETE") {
|
|
258
297
|
headers = {
|
|
259
298
|
"Content-Type": "application/json;charset=UTF-8",
|
|
299
|
+
// CSRF custom header necessary for POST requests
|
|
300
|
+
"X-Client-Name": "XMLHttpRequest",
|
|
260
301
|
...init?.headers,
|
|
261
302
|
};
|
|
262
303
|
}
|
package/src/api/Client.ts
CHANGED
|
@@ -1,18 +1,14 @@
|
|
|
1
1
|
import { Schema } from "../schema/classes/Schema";
|
|
2
2
|
import Api, { RQLParams, ResultSet, CurrentUser } from "./Api";
|
|
3
|
-
import {
|
|
4
|
-
NativeTransactionBackend,
|
|
5
|
-
Transaction,
|
|
6
|
-
TransactionBackend,
|
|
7
|
-
TransactionResult,
|
|
8
|
-
} from "./Transaction";
|
|
3
|
+
import { BinariesParam, Transaction, TransactionResult } from "./Transaction";
|
|
9
4
|
|
|
10
5
|
/**
|
|
11
6
|
* Class used to communicate with a CubicWeb instance's API.
|
|
7
|
+
*
|
|
8
|
+
* @category Api
|
|
12
9
|
*/
|
|
13
10
|
export default class Client {
|
|
14
11
|
private api: Api;
|
|
15
|
-
private transactionBackend: TransactionBackend;
|
|
16
12
|
|
|
17
13
|
/**
|
|
18
14
|
* Class used to communicate with a CubicWeb instance's API.
|
|
@@ -21,17 +17,8 @@ export default class Client {
|
|
|
21
17
|
* Example: if you can retrieve the schema with https://example.com/api/v1/schema,
|
|
22
18
|
* use https://example.com/api as `apiUrl`.
|
|
23
19
|
*/
|
|
24
|
-
constructor(
|
|
25
|
-
apiUrl: string,
|
|
26
|
-
options: { transactionBackend?: TransactionBackend } = {}
|
|
27
|
-
) {
|
|
20
|
+
constructor(apiUrl: string) {
|
|
28
21
|
this.api = new Api(apiUrl);
|
|
29
|
-
const { transactionBackend } = options;
|
|
30
|
-
if (transactionBackend === undefined) {
|
|
31
|
-
this.transactionBackend = new NativeTransactionBackend(this.api);
|
|
32
|
-
} else {
|
|
33
|
-
this.transactionBackend = transactionBackend;
|
|
34
|
-
}
|
|
35
22
|
}
|
|
36
23
|
|
|
37
24
|
get apiUrl() {
|
|
@@ -45,8 +32,16 @@ export default class Client {
|
|
|
45
32
|
* @param params The additional parameters for the request
|
|
46
33
|
* @returns A promise with the request's result set
|
|
47
34
|
*/
|
|
48
|
-
async execute(
|
|
49
|
-
|
|
35
|
+
async execute(
|
|
36
|
+
query: string,
|
|
37
|
+
params: RQLParams = {},
|
|
38
|
+
binaries?: BinariesParam
|
|
39
|
+
): Promise<ResultSet> {
|
|
40
|
+
const transaction = new Transaction();
|
|
41
|
+
const transactionQuery = transaction.push(query, params);
|
|
42
|
+
transaction.setBinaries(binaries);
|
|
43
|
+
const result = await this.executeTransaction(transaction);
|
|
44
|
+
return result.resolveQuery(transactionQuery.ref());
|
|
50
45
|
}
|
|
51
46
|
|
|
52
47
|
/**
|
|
@@ -86,6 +81,15 @@ export default class Client {
|
|
|
86
81
|
async executeTransaction(
|
|
87
82
|
transaction: Transaction
|
|
88
83
|
): Promise<TransactionResult> {
|
|
89
|
-
return await this.
|
|
84
|
+
return await this.api.rql(transaction);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Get the binary for the given entity attribute
|
|
89
|
+
*
|
|
90
|
+
* @returns A promise resolving to the binary
|
|
91
|
+
*/
|
|
92
|
+
async getBinary(eid: number, attribute: string) {
|
|
93
|
+
return await this.api.binary(eid, attribute);
|
|
90
94
|
}
|
|
91
95
|
}
|
package/src/api/Transaction.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { ResultSet, RQLParamValue } from "./Api";
|
|
2
|
+
|
|
3
|
+
export type BinariesParam = Record<string, Blob>;
|
|
2
4
|
|
|
3
5
|
export class Transaction {
|
|
4
6
|
private _queries: Array<TransactionQuery> = [];
|
|
7
|
+
private _binaries: BinariesParam = {};
|
|
5
8
|
|
|
6
9
|
push(
|
|
7
10
|
query: string,
|
|
8
|
-
params: Record<string, RQLParamValue |
|
|
11
|
+
params: Record<string, RQLParamValue | TransactionQueryScalarRef>
|
|
9
12
|
): TransactionQuery {
|
|
10
13
|
const stackedQuery = new TransactionQuery(
|
|
11
14
|
query,
|
|
@@ -16,44 +19,78 @@ export class Transaction {
|
|
|
16
19
|
return stackedQuery;
|
|
17
20
|
}
|
|
18
21
|
|
|
22
|
+
setBinaries(binaries?: BinariesParam) {
|
|
23
|
+
this._binaries = binaries ?? {};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
get binaries(): BinariesParam {
|
|
27
|
+
return this._binaries;
|
|
28
|
+
}
|
|
29
|
+
|
|
19
30
|
get queries(): ReadonlyArray<TransactionQuery> {
|
|
20
31
|
return this._queries;
|
|
21
32
|
}
|
|
22
33
|
}
|
|
23
34
|
|
|
35
|
+
export class TransactionQuery {
|
|
36
|
+
constructor(
|
|
37
|
+
public readonly query: string,
|
|
38
|
+
public readonly params: Record<
|
|
39
|
+
string,
|
|
40
|
+
RQLParamValue | TransactionQueryScalarRef
|
|
41
|
+
>,
|
|
42
|
+
public readonly index: number
|
|
43
|
+
) {}
|
|
44
|
+
|
|
45
|
+
ref(): TransactionQueryRef {
|
|
46
|
+
return new TransactionQueryRef(this.index);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
24
50
|
export class TransactionResult {
|
|
25
51
|
constructor(private resultSets: ReadonlyArray<ResultSet>) {}
|
|
26
52
|
|
|
27
|
-
|
|
53
|
+
resolveQuery(ref: TransactionQueryRef) {
|
|
54
|
+
return this.resultSets[ref.queryIndex];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
resolveRow(ref: TransactionQueryRowRef) {
|
|
58
|
+
return this.resultSets[ref.queryIndex][ref.row];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
resolveScalar(ref: TransactionQueryScalarRef) {
|
|
28
62
|
return this.resultSets[ref.queryIndex][ref.row][ref.column];
|
|
29
63
|
}
|
|
30
64
|
}
|
|
31
65
|
|
|
32
|
-
export
|
|
33
|
-
queryIndex: number
|
|
34
|
-
|
|
35
|
-
|
|
66
|
+
export class TransactionQueryRef {
|
|
67
|
+
constructor(public readonly queryIndex: number) {}
|
|
68
|
+
|
|
69
|
+
row(r: number): TransactionQueryRowRef {
|
|
70
|
+
return new TransactionQueryRowRef(this.queryIndex, r);
|
|
71
|
+
}
|
|
36
72
|
}
|
|
37
73
|
|
|
38
|
-
export class
|
|
74
|
+
export class TransactionQueryRowRef {
|
|
39
75
|
constructor(
|
|
40
|
-
public readonly
|
|
41
|
-
public readonly
|
|
42
|
-
public readonly index: number
|
|
76
|
+
public readonly queryIndex: number,
|
|
77
|
+
public readonly row: number
|
|
43
78
|
) {}
|
|
44
79
|
|
|
45
|
-
|
|
46
|
-
return {
|
|
47
|
-
queryIndex: this.index,
|
|
48
|
-
row,
|
|
49
|
-
column,
|
|
50
|
-
};
|
|
80
|
+
column(c: number): TransactionQueryScalarRef {
|
|
81
|
+
return { queryIndex: this.queryIndex, row: this.row, column: c };
|
|
51
82
|
}
|
|
52
83
|
}
|
|
53
84
|
|
|
54
|
-
export
|
|
55
|
-
|
|
56
|
-
|
|
85
|
+
export interface TransactionQueryScalarRef {
|
|
86
|
+
readonly queryIndex: number;
|
|
87
|
+
readonly row: number;
|
|
88
|
+
readonly column: number;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function isTransactionQueryScalarRef(
|
|
92
|
+
value: RQLParamValue | TransactionQueryScalarRef
|
|
93
|
+
): value is TransactionQueryScalarRef {
|
|
57
94
|
return (
|
|
58
95
|
typeof value === "object" &&
|
|
59
96
|
value !== null &&
|
|
@@ -62,14 +99,3 @@ export function isTransactionQueryRef(
|
|
|
62
99
|
)
|
|
63
100
|
);
|
|
64
101
|
}
|
|
65
|
-
|
|
66
|
-
export interface TransactionBackend {
|
|
67
|
-
execute(transaction: Transaction): Promise<TransactionResult>;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export class NativeTransactionBackend implements TransactionBackend {
|
|
71
|
-
constructor(private _api: Api) {}
|
|
72
|
-
async execute(transaction: Transaction): Promise<TransactionResult> {
|
|
73
|
-
return this._api.executeTransaction(transaction);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
@@ -8,6 +8,8 @@ import { MatchedRelationDefinition, RelationDefinitionMatcher } from "./Utils";
|
|
|
8
8
|
* Wrapper around raw entity schemas from the Schema.
|
|
9
9
|
* You should never have to create this object yourself.
|
|
10
10
|
* Instead, retrieve these objects from the methods in the `Schema` class.
|
|
11
|
+
*
|
|
12
|
+
* @category Schema
|
|
11
13
|
*/
|
|
12
14
|
export class EntitySchema<
|
|
13
15
|
E extends EntityRawSchemaArray = EntityRawSchemaArray,
|
|
@@ -13,6 +13,8 @@ import { EntityRole, isCardinalityRequired } from "./Utils";
|
|
|
13
13
|
* Wrapper around raw relation definition schemas from the Schema.
|
|
14
14
|
* You should never have to create this object yourself.
|
|
15
15
|
* Instead, retrieve these objects from the methods in the `Schema` class.
|
|
16
|
+
*
|
|
17
|
+
* @category Schema
|
|
16
18
|
*/
|
|
17
19
|
export class RelationDefinitionSchema<
|
|
18
20
|
E extends EntityRawSchemaArray = EntityRawSchemaArray,
|
|
@@ -19,6 +19,8 @@ const rawSchemaKey: unique symbol = Symbol("Indexer of the rawSchema");
|
|
|
19
19
|
* Wrapper around the raw JSON Schema.
|
|
20
20
|
* It is recommended to use this class instead of the raw JSON Schema
|
|
21
21
|
* as it is more robust to schema changes and provides utility functions.
|
|
22
|
+
*
|
|
23
|
+
* @category Schema
|
|
22
24
|
*/
|
|
23
25
|
export class Schema<
|
|
24
26
|
E extends EntityRawSchemaArray = EntityRawSchemaArray,
|