@cubicweb/client 3.0.0-alpha.4 → 3.0.0-alpha.5
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 +6 -9
- package/lib/api/Transaction.d.ts.map +1 -1
- package/lib/api/Transaction.js +11 -21
- 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 +13 -5
- 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 +79 -38
- package/src/api/Client.ts +24 -20
- package/src/api/Transaction.ts +16 -12
- 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 +13 -5
- 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
package/src/api/Api.ts
CHANGED
|
@@ -8,12 +8,16 @@ import {
|
|
|
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,10 +159,36 @@ 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]) => [
|
|
@@ -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-Requested-With": "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
|
+
transaction.push(query, params);
|
|
42
|
+
transaction.setBinaries(binaries);
|
|
43
|
+
const result = await this.executeTransaction(transaction);
|
|
44
|
+
return result.get(0);
|
|
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,7 +1,10 @@
|
|
|
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,
|
|
@@ -16,6 +19,14 @@ 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
|
}
|
|
@@ -24,6 +35,10 @@ export class Transaction {
|
|
|
24
35
|
export class TransactionResult {
|
|
25
36
|
constructor(private resultSets: ReadonlyArray<ResultSet>) {}
|
|
26
37
|
|
|
38
|
+
get(queryIndex: number): ResultSet {
|
|
39
|
+
return this.resultSets[queryIndex];
|
|
40
|
+
}
|
|
41
|
+
|
|
27
42
|
resolve(ref: TransactionQueryRef) {
|
|
28
43
|
return this.resultSets[ref.queryIndex][ref.row][ref.column];
|
|
29
44
|
}
|
|
@@ -62,14 +77,3 @@ export function isTransactionQueryRef(
|
|
|
62
77
|
)
|
|
63
78
|
);
|
|
64
79
|
}
|
|
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,
|
|
@@ -14,6 +14,8 @@ const REVERSE_INDICATOR = "reverse_";
|
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* An entity role in a relation.
|
|
17
|
+
*
|
|
18
|
+
* @category Utils
|
|
17
19
|
*/
|
|
18
20
|
export type EntityRole = "subject" | "object";
|
|
19
21
|
|
|
@@ -23,6 +25,8 @@ export type EntityRole = "subject" | "object";
|
|
|
23
25
|
*
|
|
24
26
|
* @param schema The schema to create a loose type for
|
|
25
27
|
* @returns A loosely typed schema
|
|
28
|
+
*
|
|
29
|
+
* @category Utils
|
|
26
30
|
*/
|
|
27
31
|
export const getLooseSchema = <
|
|
28
32
|
E extends EntityRawSchemaArray = EntityRawSchemaArray,
|
|
@@ -38,6 +42,8 @@ export const getLooseSchema = <
|
|
|
38
42
|
*
|
|
39
43
|
* @param cardinality The cardinality to check
|
|
40
44
|
* @returns True if the cardinality is 1 or +
|
|
45
|
+
*
|
|
46
|
+
* @category Utils
|
|
41
47
|
*/
|
|
42
48
|
export function isCardinalityRequired(cardinality: Cardinality): boolean {
|
|
43
49
|
return cardinality === "1" || cardinality === "+";
|
|
@@ -48,6 +54,8 @@ export function isCardinalityRequired(cardinality: Cardinality): boolean {
|
|
|
48
54
|
*
|
|
49
55
|
* @param cardinality The cardinility to check
|
|
50
56
|
* @returns True if the cardinality is `*` or `+`
|
|
57
|
+
*
|
|
58
|
+
* @category Utils
|
|
51
59
|
*/
|
|
52
60
|
export const isCardinalityMultiple = (cardinality: Cardinality): boolean => {
|
|
53
61
|
return cardinality === "*" || cardinality === "+";
|
|
@@ -58,6 +66,8 @@ export const isCardinalityMultiple = (cardinality: Cardinality): boolean => {
|
|
|
58
66
|
*
|
|
59
67
|
* @param cardinality The cardinility to check
|
|
60
68
|
* @returns True if the cardinality is `?` or `*`
|
|
69
|
+
*
|
|
70
|
+
* @category Utils
|
|
61
71
|
*/
|
|
62
72
|
export const isCardinalityOptional = (cardinality: Cardinality): boolean => {
|
|
63
73
|
return cardinality === "?" || cardinality === "*";
|
|
@@ -68,6 +78,8 @@ export const isCardinalityOptional = (cardinality: Cardinality): boolean => {
|
|
|
68
78
|
*
|
|
69
79
|
* @param role The role to reverse
|
|
70
80
|
* @returns "object" if the given role was "subject", "subject" otherwise
|
|
81
|
+
*
|
|
82
|
+
* @category Utils
|
|
71
83
|
*/
|
|
72
84
|
export function getOppositeRelationRole(role: EntityRole): EntityRole {
|
|
73
85
|
return role === "subject" ? "object" : "subject";
|
|
@@ -82,6 +94,8 @@ export function getOppositeRelationRole(role: EntityRole): EntityRole {
|
|
|
82
94
|
*
|
|
83
95
|
* @param relationType The relation type to check
|
|
84
96
|
* @returns True if the relation type starts with the reverse indicator
|
|
97
|
+
*
|
|
98
|
+
* @category Utils
|
|
85
99
|
*/
|
|
86
100
|
export const isRelationTypeReversed = (relationType: string): boolean => {
|
|
87
101
|
return relationType.startsWith(REVERSE_INDICATOR);
|
|
@@ -97,6 +111,8 @@ export const isRelationTypeReversed = (relationType: string): boolean => {
|
|
|
97
111
|
*
|
|
98
112
|
* @param relationType The relation type to check
|
|
99
113
|
* @returns "subject" if the relation is not reversed, "object" otherwise
|
|
114
|
+
*
|
|
115
|
+
* @category Utils
|
|
100
116
|
*/
|
|
101
117
|
export const getRoleFromRelationType = (relationType: string): EntityRole => {
|
|
102
118
|
return isRelationTypeReversed(relationType) ? "object" : "subject";
|
|
@@ -111,6 +127,8 @@ export const getRoleFromRelationType = (relationType: string): EntityRole => {
|
|
|
111
127
|
*
|
|
112
128
|
* @param relationType The relation type to transform
|
|
113
129
|
* @returns The relation type with the reverse indicator added or removed
|
|
130
|
+
*
|
|
131
|
+
* @category Utils
|
|
114
132
|
*/
|
|
115
133
|
export const getReverseRelationType = (relationType: string): string => {
|
|
116
134
|
if (!isRelationTypeReversed(relationType)) {
|
|
@@ -129,6 +147,8 @@ export const getReverseRelationType = (relationType: string): string => {
|
|
|
129
147
|
*
|
|
130
148
|
* @param relationType The relation type to transform
|
|
131
149
|
* @returns The relation type with the reverse indicator added or removed
|
|
150
|
+
*
|
|
151
|
+
* @category Utils
|
|
132
152
|
*/
|
|
133
153
|
export const getRawRelationType = (relationType: string): string => {
|
|
134
154
|
if (isRelationTypeReversed(relationType)) {
|
|
@@ -148,6 +168,8 @@ export const getRawRelationType = (relationType: string): string => {
|
|
|
148
168
|
* @param relationType The relation type to transform
|
|
149
169
|
* @param role The entity role in the relation
|
|
150
170
|
* @returns A relation type with the reverse indicator if necessary
|
|
171
|
+
*
|
|
172
|
+
* @category Utils
|
|
151
173
|
*/
|
|
152
174
|
export const getRelationType = (relationType: string, role: EntityRole) => {
|
|
153
175
|
return role === "subject"
|
|
@@ -163,6 +185,8 @@ export const getRelationType = (relationType: string, role: EntityRole) => {
|
|
|
163
185
|
* @param relationType The type of the relation to use. Can have the reverse indicator to decide the role of the source/target entities
|
|
164
186
|
* @param targetType The entity type acting as target for the relation
|
|
165
187
|
* @returns The relation definition matching the triplet source relation target
|
|
188
|
+
*
|
|
189
|
+
* @category Utils
|
|
166
190
|
*/
|
|
167
191
|
export const getRelationDefinition = (
|
|
168
192
|
entitySchema: EntitySchema,
|
|
@@ -184,6 +208,8 @@ export const getRelationDefinition = (
|
|
|
184
208
|
|
|
185
209
|
/**
|
|
186
210
|
* Partial raw relation definition schema type.
|
|
211
|
+
*
|
|
212
|
+
* @category Utils
|
|
187
213
|
*/
|
|
188
214
|
export type RelationDefinitionMatcher<E extends EntityRawSchemaArray> = Partial<
|
|
189
215
|
RelationDefinitionRawSchema<E>
|
|
@@ -194,6 +220,8 @@ export type RelationDefinitionMatcher<E extends EntityRawSchemaArray> = Partial<
|
|
|
194
220
|
* If it finds a relation definition matching the partial one given, it will return its type.
|
|
195
221
|
* If it cannot find any relation definition matching, it will return the `never` type
|
|
196
222
|
* and TypeScript will give compile errors.
|
|
223
|
+
*
|
|
224
|
+
* @category Utils
|
|
197
225
|
*/
|
|
198
226
|
export type MatchedRelationDefinition<
|
|
199
227
|
E extends EntityRawSchemaArray,
|
|
@@ -10,7 +10,7 @@ type ConstraintBase<
|
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
*
|
|
13
|
+
* The type of a constraint message when one is present.
|
|
14
14
|
*/
|
|
15
15
|
type ConstraintMessage = string | null;
|
|
16
16
|
|
|
@@ -33,6 +33,8 @@ export type RegexpConstraint = ConstraintBase<
|
|
|
33
33
|
* Reference to an attribute used in constraints.
|
|
34
34
|
*
|
|
35
35
|
* @property \_\_attribute\_\_ name of the attribute in the same entity
|
|
36
|
+
*
|
|
37
|
+
* @category Schema
|
|
36
38
|
*/
|
|
37
39
|
export interface Attribute {
|
|
38
40
|
__attribute__: string;
|
|
@@ -50,6 +52,8 @@ export interface TimeDelta {
|
|
|
50
52
|
* @property \_\_today\_\_ always true
|
|
51
53
|
* @property offset delta to current date at the time of the check
|
|
52
54
|
* @property type name of the Python class used to do the comparison
|
|
55
|
+
*
|
|
56
|
+
* @category Schema
|
|
53
57
|
*/
|
|
54
58
|
export interface Today {
|
|
55
59
|
__today__: true;
|
|
@@ -62,6 +66,8 @@ export interface Today {
|
|
|
62
66
|
*
|
|
63
67
|
* @property \_\_now\_\_ always true
|
|
64
68
|
* @property offset delta to current date at the time of the check
|
|
69
|
+
*
|
|
70
|
+
* @category Schema
|
|
65
71
|
*/
|
|
66
72
|
export interface Now {
|
|
67
73
|
__now__: true;
|
|
@@ -70,13 +76,11 @@ export interface Now {
|
|
|
70
76
|
|
|
71
77
|
/**
|
|
72
78
|
* All the possible operators inside a constraint.
|
|
79
|
+
*
|
|
80
|
+
* @category Schema
|
|
73
81
|
*/
|
|
74
82
|
export type ConstraintOperator = ">" | ">=" | "<" | "<=";
|
|
75
83
|
|
|
76
|
-
/**
|
|
77
|
-
* @property value.boundary boudary value to compare the attribut
|
|
78
|
-
*
|
|
79
|
-
*/
|
|
80
84
|
export type BoundaryConstraint = ConstraintBase<
|
|
81
85
|
"BoundaryConstraint",
|
|
82
86
|
{
|
|
@@ -115,6 +119,8 @@ export type RQLConstraint = ConstraintBase<
|
|
|
115
119
|
|
|
116
120
|
/**
|
|
117
121
|
* Type representing all possible constraints for attributes.
|
|
122
|
+
*
|
|
123
|
+
* @category Schema
|
|
118
124
|
*/
|
|
119
125
|
export type AttributeConstraint = Readonly<
|
|
120
126
|
| UniqueConstraint
|
|
@@ -129,6 +135,8 @@ export type AttributeConstraint = Readonly<
|
|
|
129
135
|
|
|
130
136
|
/**
|
|
131
137
|
* Type representing all possible constraints for relations.
|
|
138
|
+
*
|
|
139
|
+
* @category Schema
|
|
132
140
|
*/
|
|
133
141
|
export type RelationConstraint = Readonly<
|
|
134
142
|
RQLVocabularyConstraint | RQLConstraint
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Type representing an entity schema.
|
|
3
|
+
*
|
|
4
|
+
* @category Schema
|
|
3
5
|
*/
|
|
4
6
|
export type EntityRawSchema = {
|
|
5
7
|
type: string;
|
|
@@ -9,5 +11,7 @@ export type EntityRawSchema = {
|
|
|
9
11
|
|
|
10
12
|
/**
|
|
11
13
|
* Type representing a list of entity schemas.
|
|
14
|
+
*
|
|
15
|
+
* @category Schema
|
|
12
16
|
*/
|
|
13
17
|
export type EntityRawSchemaArray = ReadonlyArray<EntityRawSchema>;
|
|
@@ -4,11 +4,15 @@ import { EntityRawSchema, EntityRawSchemaArray } from "./Entities";
|
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Type representing all the possible cardinalities in CubicWeb.
|
|
7
|
+
*
|
|
8
|
+
* @category Schema
|
|
7
9
|
*/
|
|
8
10
|
export type Cardinality = "*" | "1" | "?" | "+";
|
|
9
11
|
|
|
10
12
|
/**
|
|
11
13
|
* Type representing a cardinality pair as used in relation definitions.
|
|
14
|
+
*
|
|
15
|
+
* @category Schema
|
|
12
16
|
*/
|
|
13
17
|
export type CardinalityPair = `${Cardinality}${Cardinality}`;
|
|
14
18
|
|
|
@@ -110,6 +114,8 @@ export type BuildObj = Readonly<
|
|
|
110
114
|
|
|
111
115
|
/**
|
|
112
116
|
* Type representing the possible options for an attribute.
|
|
117
|
+
*
|
|
118
|
+
* @category Schema
|
|
113
119
|
*/
|
|
114
120
|
export type AttributeOptions = {
|
|
115
121
|
indexed?: boolean;
|
|
@@ -119,6 +125,8 @@ export type AttributeOptions = {
|
|
|
119
125
|
|
|
120
126
|
/**
|
|
121
127
|
* Type representing the possible options for a relation definition.
|
|
128
|
+
*
|
|
129
|
+
* @category Schema
|
|
122
130
|
*/
|
|
123
131
|
export type RelationOptions = {
|
|
124
132
|
symmetric?: boolean;
|
|
@@ -132,6 +140,8 @@ export type RelationOptions = {
|
|
|
132
140
|
* It can represent both attributes and relations depending on the value of Final.
|
|
133
141
|
*
|
|
134
142
|
* It can infer default values type depending on the object type if the relation is final.
|
|
143
|
+
*
|
|
144
|
+
* @category Schema
|
|
135
145
|
*/
|
|
136
146
|
export type RelationDefinitionRawSchema<
|
|
137
147
|
ESchemas extends EntityRawSchemaArray,
|
|
@@ -158,6 +168,8 @@ export type RelationDefinitionRawSchema<
|
|
|
158
168
|
|
|
159
169
|
/**
|
|
160
170
|
* Type representing a list of relation definitions.
|
|
171
|
+
*
|
|
172
|
+
* @category Schema
|
|
161
173
|
*/
|
|
162
174
|
export type RelationDefinitionRawSchemaArray<
|
|
163
175
|
ESchemas extends EntityRawSchemaArray
|
package/src/schema/raw/Schema.ts
CHANGED
|
@@ -6,6 +6,8 @@ import { RelationDefinitionRawSchemaArray } from "./RelationDefinitions";
|
|
|
6
6
|
* You can export this schema from your CubicWeb instance using the command:
|
|
7
7
|
*
|
|
8
8
|
* `cubicweb-ctl export-schema -f typescript <YOUR_INSTANCE_NAME>`
|
|
9
|
+
*
|
|
10
|
+
* @category Schema
|
|
9
11
|
*/
|
|
10
12
|
export type RawSchema<
|
|
11
13
|
E extends EntityRawSchemaArray = EntityRawSchemaArray,
|
package/src/schema/raw/Utils.ts
CHANGED
|
@@ -6,6 +6,8 @@ import { EntityRawSchemaArray } from "./Entities";
|
|
|
6
6
|
*
|
|
7
7
|
* In the given EntityRawSchemaArray, if the entities type is a string, the schema is loosely typed.
|
|
8
8
|
* If the schema is strongly typed, the entities type would be a word (which is stricter than string).
|
|
9
|
+
*
|
|
10
|
+
* @category Schema
|
|
9
11
|
*/
|
|
10
12
|
export type IsLooseSchema<E extends EntityRawSchemaArray> =
|
|
11
13
|
string extends E[number]["type"] ? true : false;
|
|
@@ -13,6 +15,8 @@ export type IsLooseSchema<E extends EntityRawSchemaArray> =
|
|
|
13
15
|
/**
|
|
14
16
|
* Checks if the given entity type is present in the EntityRawSchemaArray.
|
|
15
17
|
* Use this to create conditional types.
|
|
18
|
+
*
|
|
19
|
+
* @category Schema
|
|
16
20
|
*/
|
|
17
21
|
export type IsETypeInStrictSchema<
|
|
18
22
|
E extends EntityRawSchemaArray,
|
|
@@ -22,6 +26,8 @@ export type IsETypeInStrictSchema<
|
|
|
22
26
|
/**
|
|
23
27
|
* Type representing the entity's raw schema matching this type.
|
|
24
28
|
* This type only works for EntityRawSchemaArray with literals as "type" property.
|
|
29
|
+
*
|
|
30
|
+
* @category Schema
|
|
25
31
|
*/
|
|
26
32
|
export type EntityRawSchemaByType<
|
|
27
33
|
E extends EntityRawSchemaArray,
|