@terreno/syncdb 57.9.0 → 57.11.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/dist/client.d.ts +20 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +53 -1
- package/dist/client.js.map +1 -1
- package/dist/codegen/emitSdk.d.ts.map +1 -1
- package/dist/codegen/emitSdk.js +2 -0
- package/dist/codegen/emitSdk.js.map +1 -1
- package/dist/codegen/generate.test.js +2 -0
- package/dist/codegen/generate.test.js.map +1 -1
- package/dist/codegen/hookNames.d.ts +2 -1
- package/dist/codegen/hookNames.d.ts.map +1 -1
- package/dist/codegen/hookNames.js +2 -1
- package/dist/codegen/hookNames.js.map +1 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/query/localQuery.d.ts +22 -0
- package/dist/query/localQuery.d.ts.map +1 -0
- package/dist/query/localQuery.js +38 -0
- package/dist/query/localQuery.js.map +1 -0
- package/dist/query/queryWindows.d.ts +128 -0
- package/dist/query/queryWindows.d.ts.map +1 -0
- package/dist/query/queryWindows.edge.test.d.ts +2 -0
- package/dist/query/queryWindows.edge.test.d.ts.map +1 -0
- package/dist/query/queryWindows.edge.test.js +196 -0
- package/dist/query/queryWindows.edge.test.js.map +1 -0
- package/dist/query/queryWindows.js +621 -0
- package/dist/query/queryWindows.js.map +1 -0
- package/dist/query/queryWindows.test.d.ts +2 -0
- package/dist/query/queryWindows.test.d.ts.map +1 -0
- package/dist/query/queryWindows.test.js +565 -0
- package/dist/query/queryWindows.test.js.map +1 -0
- package/dist/query/rangePlan.d.ts +23 -0
- package/dist/query/rangePlan.d.ts.map +1 -0
- package/dist/query/rangePlan.js +20 -0
- package/dist/query/rangePlan.js.map +1 -0
- package/dist/query/sort.d.ts +13 -0
- package/dist/query/sort.d.ts.map +1 -0
- package/dist/query/sort.js +84 -0
- package/dist/query/sort.js.map +1 -0
- package/dist/query/where.d.ts +41 -0
- package/dist/query/where.d.ts.map +1 -0
- package/dist/query/where.js +243 -0
- package/dist/query/where.js.map +1 -0
- package/dist/query/where.test.d.ts +2 -0
- package/dist/query/where.test.d.ts.map +1 -0
- package/dist/query/where.test.js +130 -0
- package/dist/query/where.test.js.map +1 -0
- package/dist/react/collectionHooks.d.ts +6 -1
- package/dist/react/collectionHooks.d.ts.map +1 -1
- package/dist/react/collectionHooks.js +3 -1
- package/dist/react/collectionHooks.js.map +1 -1
- package/dist/react/hooks.d.ts +66 -2
- package/dist/react/hooks.d.ts.map +1 -1
- package/dist/react/hooks.js +91 -24
- package/dist/react/hooks.js.map +1 -1
- package/dist/react/windowHooks.test.d.ts +2 -0
- package/dist/react/windowHooks.test.d.ts.map +1 -0
- package/dist/react/windowHooks.test.js +132 -0
- package/dist/react/windowHooks.test.js.map +1 -0
- package/dist/storage/schema.d.ts.map +1 -1
- package/dist/storage/schema.js +13 -1
- package/dist/storage/schema.js.map +1 -1
- package/dist/storage/types.d.ts +5 -0
- package/dist/storage/types.d.ts.map +1 -1
- package/dist/storage/types.js +5 -0
- package/dist/storage/types.js.map +1 -1
- package/dist/sync/httpChannel.d.ts +40 -1
- package/dist/sync/httpChannel.d.ts.map +1 -1
- package/dist/sync/httpChannel.js +89 -0
- package/dist/sync/httpChannel.js.map +1 -1
- package/dist/sync/httpChannel.test.js +45 -1
- package/dist/sync/httpChannel.test.js.map +1 -1
- package/dist/types.d.ts +10 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +70 -1
- package/src/codegen/emitSdk.ts +2 -0
- package/src/codegen/generate.test.ts +2 -0
- package/src/codegen/hookNames.ts +3 -1
- package/src/index.ts +18 -0
- package/src/query/localQuery.ts +60 -0
- package/src/query/queryWindows.edge.test.ts +250 -0
- package/src/query/queryWindows.test.ts +665 -0
- package/src/query/queryWindows.ts +848 -0
- package/src/query/rangePlan.ts +39 -0
- package/src/query/sort.ts +101 -0
- package/src/query/where.test.ts +155 -0
- package/src/query/where.ts +290 -0
- package/src/react/collectionHooks.ts +16 -2
- package/src/react/hooks.ts +184 -26
- package/src/react/windowHooks.test.tsx +172 -0
- package/src/storage/schema.ts +13 -0
- package/src/storage/types.ts +6 -0
- package/src/sync/httpChannel.test.ts +65 -1
- package/src/sync/httpChannel.ts +142 -0
- package/src/types.ts +11 -0
package/src/sync/httpChannel.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import {encodeQueryParams} from "../query/where";
|
|
1
2
|
import type {
|
|
2
3
|
AuthProvider,
|
|
4
|
+
ListResponse,
|
|
3
5
|
SyncAck,
|
|
4
6
|
SyncEntitiesResponse,
|
|
5
7
|
SyncMutateBatchRequest,
|
|
@@ -52,6 +54,11 @@ export interface HttpChannel {
|
|
|
52
54
|
* membership change (INV-2).
|
|
53
55
|
*/
|
|
54
56
|
fetchStreams: () => Promise<SyncStreamInfo[]>;
|
|
57
|
+
/**
|
|
58
|
+
* `GET {path}?{params}` against a `modelRouter` list endpoint, for query windows.
|
|
59
|
+
* Params are qs-encoded so Mongo operators (`$in`, `$gte`, …) survive.
|
|
60
|
+
*/
|
|
61
|
+
fetchList?: (args: {path: string; params: Record<string, unknown>}) => Promise<ListResponse>;
|
|
55
62
|
/**
|
|
56
63
|
* POST the mutation; 200 resolves `{type: "ack"}`, nack statuses
|
|
57
64
|
* (409/403/422/500 with a `{nack}` body) resolve `{type: "nack"}`, 401
|
|
@@ -72,6 +79,125 @@ export interface HttpChannel {
|
|
|
72
79
|
fetchKeyMaterial: () => Promise<string>;
|
|
73
80
|
}
|
|
74
81
|
|
|
82
|
+
/** JSON:API-style error body `@terreno/api` returns (`APIError#toJSON`). */
|
|
83
|
+
interface ApiErrorBody {
|
|
84
|
+
code?: string;
|
|
85
|
+
detail?: string;
|
|
86
|
+
title?: string;
|
|
87
|
+
source?: {parameter?: string};
|
|
88
|
+
meta?: {allowedQueryFields?: unknown; model?: unknown; queryParam?: unknown};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** A `modelRouter` list request the server rejected (non-401). */
|
|
92
|
+
export class ListRequestError extends Error {
|
|
93
|
+
readonly status: number;
|
|
94
|
+
readonly code?: string;
|
|
95
|
+
readonly path: string;
|
|
96
|
+
readonly detail?: string;
|
|
97
|
+
|
|
98
|
+
constructor({
|
|
99
|
+
message,
|
|
100
|
+
status,
|
|
101
|
+
code,
|
|
102
|
+
path,
|
|
103
|
+
detail,
|
|
104
|
+
}: {
|
|
105
|
+
message: string;
|
|
106
|
+
status: number;
|
|
107
|
+
code?: string;
|
|
108
|
+
path: string;
|
|
109
|
+
detail?: string;
|
|
110
|
+
}) {
|
|
111
|
+
super(message);
|
|
112
|
+
this.name = "ListRequestError";
|
|
113
|
+
this.status = status;
|
|
114
|
+
this.code = code;
|
|
115
|
+
this.path = path;
|
|
116
|
+
this.detail = detail;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* A query window filtered on a field the route's `queryFields` does not allow
|
|
122
|
+
* (server code `query-param-not-allowed`). Fix: add `field` to `queryFields` on
|
|
123
|
+
* the `modelRouter`, or drop it from `where`.
|
|
124
|
+
*/
|
|
125
|
+
export class QueryFieldNotAllowedError extends ListRequestError {
|
|
126
|
+
readonly field: string;
|
|
127
|
+
readonly allowedQueryFields: string[];
|
|
128
|
+
readonly model?: string;
|
|
129
|
+
|
|
130
|
+
constructor({
|
|
131
|
+
path,
|
|
132
|
+
field,
|
|
133
|
+
allowedQueryFields,
|
|
134
|
+
model,
|
|
135
|
+
detail,
|
|
136
|
+
}: {
|
|
137
|
+
path: string;
|
|
138
|
+
field: string;
|
|
139
|
+
allowedQueryFields: string[];
|
|
140
|
+
model?: string;
|
|
141
|
+
detail?: string;
|
|
142
|
+
}) {
|
|
143
|
+
const allowed = allowedQueryFields.length > 0 ? allowedQueryFields.join(", ") : "(none)";
|
|
144
|
+
super({
|
|
145
|
+
code: "query-param-not-allowed",
|
|
146
|
+
detail,
|
|
147
|
+
message:
|
|
148
|
+
`GET ${path} rejected where field "${field}": it is not in queryFields on the ` +
|
|
149
|
+
`${model ?? "route's"} modelRouter. Add "${field}" to queryFields or remove it from ` +
|
|
150
|
+
`the window's where. Allowed: ${allowed}.`,
|
|
151
|
+
path,
|
|
152
|
+
status: 400,
|
|
153
|
+
});
|
|
154
|
+
this.name = "QueryFieldNotAllowedError";
|
|
155
|
+
this.field = field;
|
|
156
|
+
this.allowedQueryFields = allowedQueryFields;
|
|
157
|
+
this.model = model;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const readErrorBody = async (response: Response): Promise<ApiErrorBody> => {
|
|
162
|
+
try {
|
|
163
|
+
const body = (await response.json()) as ApiErrorBody | {errors?: ApiErrorBody[]};
|
|
164
|
+
if (body && "errors" in body && Array.isArray(body.errors)) {
|
|
165
|
+
return body.errors[0] ?? {};
|
|
166
|
+
}
|
|
167
|
+
return (body as ApiErrorBody) ?? {};
|
|
168
|
+
} catch {
|
|
169
|
+
return {};
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const listRequestError = async (path: string, response: Response): Promise<ListRequestError> => {
|
|
174
|
+
const body = await readErrorBody(response);
|
|
175
|
+
if (body.code === "query-param-not-allowed") {
|
|
176
|
+
const allowed = Array.isArray(body.meta?.allowedQueryFields)
|
|
177
|
+
? body.meta.allowedQueryFields.filter((field): field is string => typeof field === "string")
|
|
178
|
+
: [];
|
|
179
|
+
const field =
|
|
180
|
+
(typeof body.meta?.queryParam === "string" ? body.meta.queryParam : undefined) ??
|
|
181
|
+
body.source?.parameter ??
|
|
182
|
+
"unknown";
|
|
183
|
+
return new QueryFieldNotAllowedError({
|
|
184
|
+
allowedQueryFields: allowed,
|
|
185
|
+
detail: body.detail,
|
|
186
|
+
field,
|
|
187
|
+
model: typeof body.meta?.model === "string" ? body.meta.model : undefined,
|
|
188
|
+
path,
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
const reason = body.detail ?? body.title;
|
|
192
|
+
return new ListRequestError({
|
|
193
|
+
code: body.code,
|
|
194
|
+
detail: body.detail,
|
|
195
|
+
message: `List request for ${path} failed with status ${response.status}${reason ? `: ${reason}` : ""}`,
|
|
196
|
+
path,
|
|
197
|
+
status: response.status,
|
|
198
|
+
});
|
|
199
|
+
};
|
|
200
|
+
|
|
75
201
|
/** Minimal fetch signature (global fetch is assignable; tests inject stubs). */
|
|
76
202
|
export type FetchLike = (input: string, init?: RequestInit) => Promise<Response>;
|
|
77
203
|
|
|
@@ -149,6 +275,21 @@ export const createHttpChannel = ({
|
|
|
149
275
|
return (await response.json()) as SyncEntitiesResponse;
|
|
150
276
|
};
|
|
151
277
|
|
|
278
|
+
const fetchList = async ({
|
|
279
|
+
path,
|
|
280
|
+
params,
|
|
281
|
+
}: {
|
|
282
|
+
path: string;
|
|
283
|
+
params: Record<string, unknown>;
|
|
284
|
+
}): Promise<ListResponse> => {
|
|
285
|
+
const query = encodeQueryParams(params);
|
|
286
|
+
const response = await request(query ? `${path}?${query}` : path);
|
|
287
|
+
if (!response.ok) {
|
|
288
|
+
throw await listRequestError(path, response);
|
|
289
|
+
}
|
|
290
|
+
return (await response.json()) as ListResponse;
|
|
291
|
+
};
|
|
292
|
+
|
|
152
293
|
const fetchStreams = async (): Promise<SyncStreamInfo[]> => {
|
|
153
294
|
const response = await request("/sync/streams");
|
|
154
295
|
if (!response.ok) {
|
|
@@ -217,6 +358,7 @@ export const createHttpChannel = ({
|
|
|
217
358
|
return {
|
|
218
359
|
fetchEntities,
|
|
219
360
|
fetchKeyMaterial,
|
|
361
|
+
fetchList,
|
|
220
362
|
fetchSnapshotPage,
|
|
221
363
|
fetchStreams,
|
|
222
364
|
sendMutation,
|
package/src/types.ts
CHANGED
|
@@ -179,6 +179,17 @@ export interface SyncSnapshotResponse {
|
|
|
179
179
|
legacyCursor?: string;
|
|
180
180
|
}
|
|
181
181
|
|
|
182
|
+
/** Response shape of a `modelRouter` list endpoint (`GET /{collection}`), used by query windows. */
|
|
183
|
+
export interface ListResponse {
|
|
184
|
+
data: unknown[];
|
|
185
|
+
/** True when more pages exist past this one. */
|
|
186
|
+
more?: boolean;
|
|
187
|
+
/** Total matching documents across all pages. */
|
|
188
|
+
total?: number;
|
|
189
|
+
/** The page size the server applied (lower than requested when `maxLimit` clamped it). */
|
|
190
|
+
limit?: number;
|
|
191
|
+
}
|
|
192
|
+
|
|
182
193
|
/** One stream a user currently belongs to, from `GET /sync/streams`. */
|
|
183
194
|
export interface SyncStreamInfo {
|
|
184
195
|
/** Stream key (e.g. "todos|owner:123"). */
|