@zenstackhq/client-helpers 3.5.6 → 3.6.1
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/dist/{fetch.d.ts → fetch.d.mts} +6 -4
- package/dist/fetch.mjs +90 -0
- package/dist/fetch.mjs.map +1 -0
- package/dist/{index.d.ts → index.d.mts} +154 -141
- package/dist/index.mjs +643 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +15 -15
- package/dist/fetch.js +0 -101
- package/dist/fetch.js.map +0 -1
- package/dist/index.js +0 -822
- package/dist/index.js.map +0 -1
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//#region src/fetch.d.ts
|
|
1
2
|
/**
|
|
2
3
|
* Function signature for `fetch`.
|
|
3
4
|
*/
|
|
@@ -15,8 +16,8 @@ declare function makeUrl(endpoint: string, model: string, operation: string, arg
|
|
|
15
16
|
* Serialize the given value with superjson
|
|
16
17
|
*/
|
|
17
18
|
declare function serialize(value: unknown): {
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
data: unknown;
|
|
20
|
+
meta: unknown;
|
|
20
21
|
};
|
|
21
22
|
/**
|
|
22
23
|
* Deserialize the given value with superjson using the given metadata
|
|
@@ -31,5 +32,6 @@ declare function marshal(value: unknown): string;
|
|
|
31
32
|
* object containing the serialized data and serialization metadata.
|
|
32
33
|
*/
|
|
33
34
|
declare function unmarshal(value: string): any;
|
|
34
|
-
|
|
35
|
-
export {
|
|
35
|
+
//#endregion
|
|
36
|
+
export { FetchFn, deserialize, fetcher, makeUrl, marshal, serialize, unmarshal };
|
|
37
|
+
//# sourceMappingURL=fetch.d.mts.map
|
package/dist/fetch.mjs
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { lowerCaseFirst } from "@zenstackhq/common-helpers";
|
|
2
|
+
import Decimal from "decimal.js";
|
|
3
|
+
import SuperJSON from "superjson";
|
|
4
|
+
//#region src/fetch.ts
|
|
5
|
+
/**
|
|
6
|
+
* A fetcher function that uses fetch API to make HTTP requests and automatically unmarshals
|
|
7
|
+
* the response using superjson.
|
|
8
|
+
*/
|
|
9
|
+
async function fetcher(url, options, customFetch) {
|
|
10
|
+
const res = await (customFetch ?? fetch)(url, options);
|
|
11
|
+
if (!res.ok) {
|
|
12
|
+
const errData = unmarshal(await res.text());
|
|
13
|
+
if (errData.error?.rejectedByPolicy && errData.error?.rejectReason === "cannot-read-back") return;
|
|
14
|
+
const error = /* @__PURE__ */ new Error("An error occurred while fetching the data.");
|
|
15
|
+
error.info = errData.error;
|
|
16
|
+
error.status = res.status;
|
|
17
|
+
throw error;
|
|
18
|
+
}
|
|
19
|
+
const textResult = await res.text();
|
|
20
|
+
try {
|
|
21
|
+
return unmarshal(textResult).data;
|
|
22
|
+
} catch (err) {
|
|
23
|
+
console.error(`Unable to deserialize data:`, textResult);
|
|
24
|
+
throw err;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Makes a URL for the given endpoint, model, operation, and args that matches the RPC-style server API.
|
|
29
|
+
*/
|
|
30
|
+
function makeUrl(endpoint, model, operation, args) {
|
|
31
|
+
const baseUrl = `${endpoint}/${lowerCaseFirst(model)}/${operation}`;
|
|
32
|
+
if (!args) return baseUrl;
|
|
33
|
+
const { data, meta } = serialize(args);
|
|
34
|
+
let result = `${baseUrl}?q=${encodeURIComponent(JSON.stringify(data))}`;
|
|
35
|
+
if (meta) result += `&meta=${encodeURIComponent(JSON.stringify({ serialization: meta }))}`;
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
SuperJSON.registerCustom({
|
|
39
|
+
isApplicable: (v) => v instanceof Decimal || v?.toStringTag === "[object Decimal]",
|
|
40
|
+
serialize: (v) => v.toJSON(),
|
|
41
|
+
deserialize: (v) => new Decimal(v)
|
|
42
|
+
}, "Decimal");
|
|
43
|
+
/**
|
|
44
|
+
* Serialize the given value with superjson
|
|
45
|
+
*/
|
|
46
|
+
function serialize(value) {
|
|
47
|
+
const { json, meta } = SuperJSON.serialize(value);
|
|
48
|
+
return {
|
|
49
|
+
data: json,
|
|
50
|
+
meta
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Deserialize the given value with superjson using the given metadata
|
|
55
|
+
*/
|
|
56
|
+
function deserialize(value, meta) {
|
|
57
|
+
return SuperJSON.deserialize({
|
|
58
|
+
json: value,
|
|
59
|
+
meta
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Marshal the given value to a string using superjson
|
|
64
|
+
*/
|
|
65
|
+
function marshal(value) {
|
|
66
|
+
const { data, meta } = serialize(value);
|
|
67
|
+
if (meta) return JSON.stringify({
|
|
68
|
+
...data,
|
|
69
|
+
meta: { serialization: meta }
|
|
70
|
+
});
|
|
71
|
+
else return JSON.stringify(data);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Unmarshal the given string value using superjson, assuming the value is a JSON stringified
|
|
75
|
+
* object containing the serialized data and serialization metadata.
|
|
76
|
+
*/
|
|
77
|
+
function unmarshal(value) {
|
|
78
|
+
const parsed = JSON.parse(value);
|
|
79
|
+
if (typeof parsed === "object" && parsed?.data && parsed?.meta?.serialization) {
|
|
80
|
+
const deserializedData = deserialize(parsed.data, parsed.meta.serialization);
|
|
81
|
+
return {
|
|
82
|
+
...parsed,
|
|
83
|
+
data: deserializedData
|
|
84
|
+
};
|
|
85
|
+
} else return parsed;
|
|
86
|
+
}
|
|
87
|
+
//#endregion
|
|
88
|
+
export { deserialize, fetcher, makeUrl, marshal, serialize, unmarshal };
|
|
89
|
+
|
|
90
|
+
//# sourceMappingURL=fetch.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch.mjs","names":[],"sources":["../src/fetch.ts"],"sourcesContent":["import { lowerCaseFirst } from '@zenstackhq/common-helpers';\nimport Decimal from 'decimal.js';\nimport SuperJSON from 'superjson';\nimport type { QueryError } from './types';\n\n/**\n * Function signature for `fetch`.\n */\nexport type FetchFn = (url: string, options?: RequestInit) => Promise<Response>;\n\n/**\n * A fetcher function that uses fetch API to make HTTP requests and automatically unmarshals\n * the response using superjson.\n */\nexport async function fetcher<R>(url: string, options?: RequestInit, customFetch?: FetchFn): Promise<R> {\n const _fetch = customFetch ?? fetch;\n const res = await _fetch(url, options);\n if (!res.ok) {\n const errData = unmarshal(await res.text());\n if (errData.error?.rejectedByPolicy && errData.error?.rejectReason === 'cannot-read-back') {\n // policy doesn't allow mutation result to be read back, just return undefined\n return undefined as any;\n }\n const error: QueryError = new Error('An error occurred while fetching the data.');\n error.info = errData.error;\n error.status = res.status;\n throw error;\n }\n\n const textResult = await res.text();\n try {\n return unmarshal(textResult).data as R;\n } catch (err) {\n console.error(`Unable to deserialize data:`, textResult);\n throw err;\n }\n}\n\n/**\n * Makes a URL for the given endpoint, model, operation, and args that matches the RPC-style server API.\n */\nexport function makeUrl(endpoint: string, model: string, operation: string, args?: unknown) {\n const baseUrl = `${endpoint}/${lowerCaseFirst(model)}/${operation}`;\n if (!args) {\n return baseUrl;\n }\n\n const { data, meta } = serialize(args);\n let result = `${baseUrl}?q=${encodeURIComponent(JSON.stringify(data))}`;\n if (meta) {\n result += `&meta=${encodeURIComponent(JSON.stringify({ serialization: meta }))}`;\n }\n return result;\n}\n\nSuperJSON.registerCustom<Decimal, string>(\n {\n isApplicable: (v): v is Decimal =>\n v instanceof Decimal ||\n // interop with decimal.js\n v?.toStringTag === '[object Decimal]',\n serialize: (v) => v.toJSON(),\n deserialize: (v) => new Decimal(v),\n },\n 'Decimal',\n);\n\n/**\n * Serialize the given value with superjson\n */\nexport function serialize(value: unknown): { data: unknown; meta: unknown } {\n const { json, meta } = SuperJSON.serialize(value);\n return { data: json, meta };\n}\n\n/**\n * Deserialize the given value with superjson using the given metadata\n */\nexport function deserialize(value: unknown, meta: any): unknown {\n return SuperJSON.deserialize({ json: value as any, meta });\n}\n\n/**\n * Marshal the given value to a string using superjson\n */\nexport function marshal(value: unknown) {\n const { data, meta } = serialize(value);\n if (meta) {\n return JSON.stringify({ ...(data as any), meta: { serialization: meta } });\n } else {\n return JSON.stringify(data);\n }\n}\n\n/**\n * Unmarshal the given string value using superjson, assuming the value is a JSON stringified\n * object containing the serialized data and serialization metadata.\n */\nexport function unmarshal(value: string) {\n const parsed = JSON.parse(value);\n if (typeof parsed === 'object' && parsed?.data && parsed?.meta?.serialization) {\n const deserializedData = deserialize(parsed.data, parsed.meta.serialization);\n return { ...parsed, data: deserializedData };\n } else {\n return parsed;\n }\n}\n"],"mappings":";;;;;;;;AAcA,eAAsB,QAAW,KAAa,SAAuB,aAAmC;CAEpG,MAAM,MAAM,OADG,eAAe,OACL,KAAK,QAAQ;AACtC,KAAI,CAAC,IAAI,IAAI;EACT,MAAM,UAAU,UAAU,MAAM,IAAI,MAAM,CAAC;AAC3C,MAAI,QAAQ,OAAO,oBAAoB,QAAQ,OAAO,iBAAiB,mBAEnE;EAEJ,MAAM,wBAAoB,IAAI,MAAM,6CAA6C;AACjF,QAAM,OAAO,QAAQ;AACrB,QAAM,SAAS,IAAI;AACnB,QAAM;;CAGV,MAAM,aAAa,MAAM,IAAI,MAAM;AACnC,KAAI;AACA,SAAO,UAAU,WAAW,CAAC;UACxB,KAAK;AACV,UAAQ,MAAM,+BAA+B,WAAW;AACxD,QAAM;;;;;;AAOd,SAAgB,QAAQ,UAAkB,OAAe,WAAmB,MAAgB;CACxF,MAAM,UAAU,GAAG,SAAS,GAAG,eAAe,MAAM,CAAC,GAAG;AACxD,KAAI,CAAC,KACD,QAAO;CAGX,MAAM,EAAE,MAAM,SAAS,UAAU,KAAK;CACtC,IAAI,SAAS,GAAG,QAAQ,KAAK,mBAAmB,KAAK,UAAU,KAAK,CAAC;AACrE,KAAI,KACA,WAAU,SAAS,mBAAmB,KAAK,UAAU,EAAE,eAAe,MAAM,CAAC,CAAC;AAElF,QAAO;;AAGX,UAAU,eACN;CACI,eAAe,MACX,aAAa,WAEb,GAAG,gBAAgB;CACvB,YAAY,MAAM,EAAE,QAAQ;CAC5B,cAAc,MAAM,IAAI,QAAQ,EAAE;CACrC,EACD,UACH;;;;AAKD,SAAgB,UAAU,OAAkD;CACxE,MAAM,EAAE,MAAM,SAAS,UAAU,UAAU,MAAM;AACjD,QAAO;EAAE,MAAM;EAAM;EAAM;;;;;AAM/B,SAAgB,YAAY,OAAgB,MAAoB;AAC5D,QAAO,UAAU,YAAY;EAAE,MAAM;EAAc;EAAM,CAAC;;;;;AAM9D,SAAgB,QAAQ,OAAgB;CACpC,MAAM,EAAE,MAAM,SAAS,UAAU,MAAM;AACvC,KAAI,KACA,QAAO,KAAK,UAAU;EAAE,GAAI;EAAc,MAAM,EAAE,eAAe,MAAM;EAAE,CAAC;KAE1E,QAAO,KAAK,UAAU,KAAK;;;;;;AAQnC,SAAgB,UAAU,OAAe;CACrC,MAAM,SAAS,KAAK,MAAM,MAAM;AAChC,KAAI,OAAO,WAAW,YAAY,QAAQ,QAAQ,QAAQ,MAAM,eAAe;EAC3E,MAAM,mBAAmB,YAAY,OAAO,MAAM,OAAO,KAAK,cAAc;AAC5E,SAAO;GAAE,GAAG;GAAQ,MAAM;GAAkB;OAE5C,QAAO"}
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ClientContract, QueryOptions } from
|
|
1
|
+
import { FieldDef, SchemaDef } from "@zenstackhq/schema";
|
|
2
|
+
import { ClientContract, QueryOptions } from "@zenstackhq/orm";
|
|
3
3
|
|
|
4
|
+
//#region src/constants.d.ts
|
|
4
5
|
/**
|
|
5
6
|
* The default query endpoint.
|
|
6
7
|
*/
|
|
7
8
|
declare const DEFAULT_QUERY_ENDPOINT = "/api/model";
|
|
8
|
-
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region src/logging.d.ts
|
|
9
11
|
/**
|
|
10
12
|
* Logger configuration. `true` enables console logging. A function can be provided for custom logging.
|
|
11
13
|
*/
|
|
@@ -14,7 +16,8 @@ type Logger = boolean | ((message: string) => void);
|
|
|
14
16
|
* Logs a message using the provided logger.
|
|
15
17
|
*/
|
|
16
18
|
declare function log(logger: Logger, message: string): void;
|
|
17
|
-
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/types.d.ts
|
|
18
21
|
/**
|
|
19
22
|
* A type that represents either a value of type T or a Promise that resolves to type T.
|
|
20
23
|
*/
|
|
@@ -23,7 +26,7 @@ type MaybePromise<T> = T | Promise<T> | PromiseLike<T>;
|
|
|
23
26
|
* Infers the schema definition from a client contract type, or passes through a raw SchemaDef.
|
|
24
27
|
*/
|
|
25
28
|
type InferSchema<T> = T extends {
|
|
26
|
-
|
|
29
|
+
$schema: infer S extends SchemaDef;
|
|
27
30
|
} ? S : T extends SchemaDef ? T : never;
|
|
28
31
|
/**
|
|
29
32
|
* Extracts the ExtResult type from a client contract, or defaults to `{}`.
|
|
@@ -33,7 +36,7 @@ type InferExtResult<T> = T extends ClientContract<any, any, any, any, infer E> ?
|
|
|
33
36
|
* Infers query options from a client contract type, or defaults to `QueryOptions<Schema>`.
|
|
34
37
|
*/
|
|
35
38
|
type InferOptions<T, Schema extends SchemaDef> = T extends {
|
|
36
|
-
|
|
39
|
+
$options: infer O extends QueryOptions<Schema>;
|
|
37
40
|
} ? O : QueryOptions<Schema>;
|
|
38
41
|
/**
|
|
39
42
|
* List of ORM write actions.
|
|
@@ -47,55 +50,59 @@ type ORMWriteActionType = (typeof ORMWriteActions)[number];
|
|
|
47
50
|
* Type for query and mutation errors.
|
|
48
51
|
*/
|
|
49
52
|
type QueryError = Error & {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
53
|
+
/**
|
|
54
|
+
* Additional error information.
|
|
55
|
+
*/
|
|
56
|
+
info?: unknown;
|
|
57
|
+
/**
|
|
58
|
+
* HTTP status code.
|
|
59
|
+
*/
|
|
60
|
+
status?: number;
|
|
58
61
|
};
|
|
59
62
|
/**
|
|
60
63
|
* Information about a cached query.
|
|
61
64
|
*/
|
|
62
65
|
type QueryInfo = {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
66
|
+
/**
|
|
67
|
+
* Model of the query.
|
|
68
|
+
*/
|
|
69
|
+
model: string;
|
|
70
|
+
/**
|
|
71
|
+
* Query operation, e.g., `findUnique`
|
|
72
|
+
*/
|
|
73
|
+
operation: string;
|
|
74
|
+
/**
|
|
75
|
+
* Query arguments.
|
|
76
|
+
*/
|
|
77
|
+
args: unknown;
|
|
78
|
+
/**
|
|
79
|
+
* Current data cached for this query.
|
|
80
|
+
*/
|
|
81
|
+
data: unknown;
|
|
82
|
+
/**
|
|
83
|
+
* Whether optimistic update is enabled for this query.
|
|
84
|
+
*/
|
|
85
|
+
optimisticUpdate: boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Function to update the cached data.
|
|
88
|
+
*
|
|
89
|
+
* @param data New data to set.
|
|
90
|
+
* @param cancelOnTheFlyQueries Whether to cancel on-the-fly queries to avoid accidentally
|
|
91
|
+
* overwriting the optimistic update.
|
|
92
|
+
*/
|
|
93
|
+
updateData: (data: unknown, cancelOnTheFlyQueries: boolean) => void;
|
|
91
94
|
};
|
|
92
|
-
|
|
95
|
+
//#endregion
|
|
96
|
+
//#region src/invalidation.d.ts
|
|
93
97
|
/**
|
|
94
98
|
* Type for a predicate that determines whether a query should be invalidated.
|
|
95
99
|
*/
|
|
96
|
-
type InvalidationPredicate = ({
|
|
97
|
-
|
|
98
|
-
|
|
100
|
+
type InvalidationPredicate = ({
|
|
101
|
+
model,
|
|
102
|
+
args
|
|
103
|
+
}: {
|
|
104
|
+
model: string;
|
|
105
|
+
args: unknown;
|
|
99
106
|
}) => boolean;
|
|
100
107
|
/**
|
|
101
108
|
* Type for a function that invalidates queries matching the given predicate.
|
|
@@ -112,7 +119,8 @@ type InvalidateFunc = (predicate: InvalidationPredicate) => MaybePromise<void>;
|
|
|
112
119
|
* @param logging Logging option.
|
|
113
120
|
*/
|
|
114
121
|
declare function createInvalidator(model: string, operation: string, schema: SchemaDef, invalidator: InvalidateFunc, logging: Logger | undefined): (...args: unknown[]) => Promise<void>;
|
|
115
|
-
|
|
122
|
+
//#endregion
|
|
123
|
+
//#region src/mutator.d.ts
|
|
116
124
|
/**
|
|
117
125
|
* Tries to apply a mutation to a query result.
|
|
118
126
|
*
|
|
@@ -127,50 +135,52 @@ declare function createInvalidator(model: string, operation: string, schema: Sch
|
|
|
127
135
|
* @returns the updated query data if the mutation is applicable, otherwise undefined
|
|
128
136
|
*/
|
|
129
137
|
declare function applyMutation(queryModel: string, queryOp: string, queryData: any, mutationModel: string, mutationOp: ORMWriteActionType, mutationArgs: any, schema: SchemaDef, logging: Logger | undefined): Promise<any>;
|
|
130
|
-
|
|
138
|
+
//#endregion
|
|
139
|
+
//#region src/nested-read-visitor.d.ts
|
|
131
140
|
/**
|
|
132
141
|
* Callback functions for nested read visitor.
|
|
133
142
|
*/
|
|
134
143
|
type NestedReadVisitorCallback = {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
144
|
+
/**
|
|
145
|
+
* Callback for each field visited.
|
|
146
|
+
* @returns If returns false, traversal will not continue into this field.
|
|
147
|
+
*/
|
|
148
|
+
field?: (model: string, field: FieldDef | undefined, kind: 'include' | 'select' | undefined, args: unknown) => void | boolean;
|
|
140
149
|
};
|
|
141
150
|
/**
|
|
142
151
|
* Visitor for nested read payload.
|
|
143
152
|
*/
|
|
144
153
|
declare class NestedReadVisitor {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
154
|
+
private readonly schema;
|
|
155
|
+
private readonly callback;
|
|
156
|
+
constructor(schema: SchemaDef, callback: NestedReadVisitorCallback);
|
|
157
|
+
private doVisit;
|
|
158
|
+
visit(model: string, args: unknown): void;
|
|
150
159
|
}
|
|
151
|
-
|
|
160
|
+
//#endregion
|
|
161
|
+
//#region src/nested-write-visitor.d.ts
|
|
152
162
|
type NestingPathItem = {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
163
|
+
field?: FieldDef;
|
|
164
|
+
model: string;
|
|
165
|
+
where: any;
|
|
166
|
+
unique: boolean;
|
|
157
167
|
};
|
|
158
168
|
/**
|
|
159
169
|
* Context for visiting
|
|
160
170
|
*/
|
|
161
171
|
type NestedWriteVisitorContext = {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
172
|
+
/**
|
|
173
|
+
* Parent data, can be used to replace fields
|
|
174
|
+
*/
|
|
175
|
+
parent: any;
|
|
176
|
+
/**
|
|
177
|
+
* Current field, undefined if toplevel
|
|
178
|
+
*/
|
|
179
|
+
field?: FieldDef;
|
|
180
|
+
/**
|
|
181
|
+
* A top-down path of all nested update conditions and corresponding field till now
|
|
182
|
+
*/
|
|
183
|
+
nestingPath: NestingPathItem[];
|
|
174
184
|
};
|
|
175
185
|
/**
|
|
176
186
|
* NestedWriteVisitor's callback actions. A call back function should return true or void to indicate
|
|
@@ -178,51 +188,52 @@ type NestedWriteVisitorContext = {
|
|
|
178
188
|
* to let the visitor traverse it instead of its original children.
|
|
179
189
|
*/
|
|
180
190
|
type NestedWriteVisitorCallback = {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
191
|
+
create?: (model: string, data: any, context: NestedWriteVisitorContext) => MaybePromise<boolean | object | void>;
|
|
192
|
+
createMany?: (model: string, args: {
|
|
193
|
+
data: any;
|
|
194
|
+
skipDuplicates?: boolean;
|
|
195
|
+
}, context: NestedWriteVisitorContext) => MaybePromise<boolean | object | void>;
|
|
196
|
+
connectOrCreate?: (model: string, args: {
|
|
197
|
+
where: object;
|
|
198
|
+
create: any;
|
|
199
|
+
}, context: NestedWriteVisitorContext) => MaybePromise<boolean | object | void>;
|
|
200
|
+
connect?: (model: string, args: object, context: NestedWriteVisitorContext) => MaybePromise<boolean | object | void>;
|
|
201
|
+
disconnect?: (model: string, args: object, context: NestedWriteVisitorContext) => MaybePromise<boolean | object | void>;
|
|
202
|
+
set?: (model: string, args: object, context: NestedWriteVisitorContext) => MaybePromise<boolean | object | void>;
|
|
203
|
+
update?: (model: string, args: object, context: NestedWriteVisitorContext) => MaybePromise<boolean | object | void>;
|
|
204
|
+
updateMany?: (model: string, args: {
|
|
205
|
+
where?: object;
|
|
206
|
+
data: any;
|
|
207
|
+
}, context: NestedWriteVisitorContext) => MaybePromise<boolean | object | void>;
|
|
208
|
+
upsert?: (model: string, args: {
|
|
209
|
+
where: object;
|
|
210
|
+
create: any;
|
|
211
|
+
update: any;
|
|
212
|
+
}, context: NestedWriteVisitorContext) => MaybePromise<boolean | object | void>;
|
|
213
|
+
delete?: (model: string, args: object | boolean, context: NestedWriteVisitorContext) => MaybePromise<boolean | object | void>;
|
|
214
|
+
deleteMany?: (model: string, args: any | object, context: NestedWriteVisitorContext) => MaybePromise<boolean | object | void>;
|
|
215
|
+
field?: (field: FieldDef, action: ORMWriteActionType, data: any, context: NestedWriteVisitorContext) => MaybePromise<void>;
|
|
206
216
|
};
|
|
207
217
|
/**
|
|
208
218
|
* Recursive visitor for nested write (create/update) payload.
|
|
209
219
|
*/
|
|
210
220
|
declare class NestedWriteVisitor {
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
221
|
+
private readonly schema;
|
|
222
|
+
private readonly callback;
|
|
223
|
+
constructor(schema: SchemaDef, callback: NestedWriteVisitorCallback);
|
|
224
|
+
private isWriteAction;
|
|
225
|
+
/**
|
|
226
|
+
* Start visiting
|
|
227
|
+
*
|
|
228
|
+
* @see NestedWriteVisitorCallback
|
|
229
|
+
*/
|
|
230
|
+
visit(model: string, action: ORMWriteActionType, args: any): Promise<void>;
|
|
231
|
+
private doVisit;
|
|
232
|
+
private visitSubPayload;
|
|
233
|
+
private enumerateReverse;
|
|
224
234
|
}
|
|
225
|
-
|
|
235
|
+
//#endregion
|
|
236
|
+
//#region src/optimistic.d.ts
|
|
226
237
|
/**
|
|
227
238
|
* Custom optimistic data provider. It takes query information (usually fetched from query cache)
|
|
228
239
|
* and returns a verdict on how to optimistically update the query data.
|
|
@@ -235,40 +246,40 @@ declare class NestedWriteVisitor {
|
|
|
235
246
|
* @param args.mutationArgs The arguments of the mutation.
|
|
236
247
|
*/
|
|
237
248
|
type OptimisticDataProvider = (args: {
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
249
|
+
queryModel: string;
|
|
250
|
+
queryOperation: string;
|
|
251
|
+
queryArgs: any;
|
|
252
|
+
currentData: any;
|
|
253
|
+
mutationArgs: any;
|
|
243
254
|
}) => OptimisticDataProviderResult | Promise<OptimisticDataProviderResult>;
|
|
244
255
|
/**
|
|
245
256
|
* Result of optimistic data provider.
|
|
246
257
|
*/
|
|
247
258
|
type OptimisticDataProviderResult = {
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
259
|
+
/**
|
|
260
|
+
* Kind of the result.
|
|
261
|
+
* - Update: use the `data` field to update the query cache.
|
|
262
|
+
* - Skip: skip the optimistic update for this query.
|
|
263
|
+
* - ProceedDefault: proceed with the default optimistic update.
|
|
264
|
+
*/
|
|
265
|
+
kind: 'Update' | 'Skip' | 'ProceedDefault';
|
|
266
|
+
/**
|
|
267
|
+
* Data to update the query cache. Only applicable if `kind` is 'Update'.
|
|
268
|
+
*
|
|
269
|
+
* If the data is an object with fields updated, it should have a `$optimistic`
|
|
270
|
+
* field set to `true`. If it's an array and an element object is created or updated,
|
|
271
|
+
* the element should have a `$optimistic` field set to `true`.
|
|
272
|
+
*/
|
|
273
|
+
data?: any;
|
|
263
274
|
};
|
|
264
275
|
/**
|
|
265
276
|
* Options for optimistic update.
|
|
266
277
|
*/
|
|
267
278
|
type OptimisticUpdateOptions = {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
279
|
+
/**
|
|
280
|
+
* A custom optimistic data provider.
|
|
281
|
+
*/
|
|
282
|
+
optimisticDataProvider?: OptimisticDataProvider;
|
|
272
283
|
};
|
|
273
284
|
/**
|
|
274
285
|
* Creates a function that performs optimistic updates for queries potentially
|
|
@@ -282,7 +293,8 @@ type OptimisticUpdateOptions = {
|
|
|
282
293
|
* @param logging Logging option.
|
|
283
294
|
*/
|
|
284
295
|
declare function createOptimisticUpdater(model: string, operation: string, schema: SchemaDef, options: OptimisticUpdateOptions, getAllQueries: () => readonly QueryInfo[], logging: Logger | undefined): (...args: unknown[]) => Promise<void>;
|
|
285
|
-
|
|
296
|
+
//#endregion
|
|
297
|
+
//#region src/query-analysis.d.ts
|
|
286
298
|
/**
|
|
287
299
|
* Gets models read (including nested ones) given a query args.
|
|
288
300
|
*/
|
|
@@ -291,5 +303,6 @@ declare function getReadModels(model: string, schema: SchemaDef, args: any): str
|
|
|
291
303
|
* Gets mutated models (including nested ones) given a mutation args.
|
|
292
304
|
*/
|
|
293
305
|
declare function getMutatedModels(model: string, operation: ORMWriteActionType, mutationArgs: any, schema: SchemaDef): Promise<string[]>;
|
|
294
|
-
|
|
295
|
-
export { DEFAULT_QUERY_ENDPOINT,
|
|
306
|
+
//#endregion
|
|
307
|
+
export { DEFAULT_QUERY_ENDPOINT, InferExtResult, InferOptions, InferSchema, InvalidateFunc, InvalidationPredicate, Logger, MaybePromise, NestedReadVisitor, NestedReadVisitorCallback, NestedWriteVisitor, NestedWriteVisitorCallback, NestedWriteVisitorContext, ORMWriteActionType, ORMWriteActions, OptimisticDataProvider, OptimisticDataProviderResult, OptimisticUpdateOptions, QueryError, QueryInfo, applyMutation, createInvalidator, createOptimisticUpdater, getMutatedModels, getReadModels, log };
|
|
308
|
+
//# sourceMappingURL=index.d.mts.map
|