@instantdb/admin 0.22.75 → 0.22.76
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/commonjs/__types__/typeUtils.d.ts +20 -0
- package/dist/commonjs/__types__/typeUtils.d.ts.map +1 -0
- package/dist/commonjs/__types__/typeUtils.js +5 -0
- package/dist/commonjs/__types__/typeUtils.js.map +1 -0
- package/dist/commonjs/__types__/typesTests.d.ts +2 -0
- package/dist/commonjs/__types__/typesTests.d.ts.map +1 -0
- package/dist/commonjs/__types__/typesTests.js +107 -0
- package/dist/commonjs/__types__/typesTests.js.map +1 -0
- package/dist/commonjs/index.d.ts +10 -8
- package/dist/commonjs/index.d.ts.map +1 -1
- package/dist/commonjs/index.js +15 -7
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/subscribe.d.ts +9 -9
- package/dist/commonjs/subscribe.d.ts.map +1 -1
- package/dist/commonjs/subscribe.js +2 -0
- package/dist/commonjs/subscribe.js.map +1 -1
- package/dist/esm/__types__/typeUtils.d.ts +20 -0
- package/dist/esm/__types__/typeUtils.d.ts.map +1 -0
- package/dist/esm/__types__/typeUtils.js +4 -0
- package/dist/esm/__types__/typeUtils.js.map +1 -0
- package/dist/esm/__types__/typesTests.d.ts +2 -0
- package/dist/esm/__types__/typesTests.d.ts.map +1 -0
- package/dist/esm/__types__/typesTests.js +105 -0
- package/dist/esm/__types__/typesTests.js.map +1 -0
- package/dist/esm/index.d.ts +10 -8
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +15 -7
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/subscribe.d.ts +9 -9
- package/dist/esm/subscribe.d.ts.map +1 -1
- package/dist/esm/subscribe.js +2 -0
- package/dist/esm/subscribe.js.map +1 -1
- package/package.json +5 -3
- package/src/__types__/typeUtils.ts +39 -0
- package/src/__types__/typesTests.ts +139 -0
- package/src/index.ts +40 -20
- package/src/subscribe.ts +33 -26
- package/tsconfig.json +3 -2
- package/tsconfig.test.json +5 -0
package/src/index.ts
CHANGED
|
@@ -109,7 +109,7 @@ export type InstantConfig<
|
|
|
109
109
|
adminToken?: string;
|
|
110
110
|
apiURI?: string;
|
|
111
111
|
schema?: Schema;
|
|
112
|
-
useDateObjects
|
|
112
|
+
useDateObjects: UseDates;
|
|
113
113
|
disableValidation?: boolean;
|
|
114
114
|
};
|
|
115
115
|
|
|
@@ -167,7 +167,7 @@ function withImpersonation(
|
|
|
167
167
|
|
|
168
168
|
function validateConfigAndImpersonation(
|
|
169
169
|
config: FilledConfig,
|
|
170
|
-
impersonationOpts: ImpersonationOpts,
|
|
170
|
+
impersonationOpts: ImpersonationOpts | undefined,
|
|
171
171
|
) {
|
|
172
172
|
if (
|
|
173
173
|
impersonationOpts &&
|
|
@@ -254,7 +254,7 @@ async function jsonFetch(
|
|
|
254
254
|
): Promise<any> {
|
|
255
255
|
const defaultFetchOpts = getDefaultFetchOpts();
|
|
256
256
|
const headers = {
|
|
257
|
-
...(init
|
|
257
|
+
...(init?.headers || {}),
|
|
258
258
|
'Instant-Admin-Version': version,
|
|
259
259
|
'Instant-Core-Version': coreVersion,
|
|
260
260
|
};
|
|
@@ -297,11 +297,22 @@ function init<
|
|
|
297
297
|
Schema extends InstantSchemaDef<any, any, any> = InstantUnknownSchema,
|
|
298
298
|
UseDates extends boolean = false,
|
|
299
299
|
>(
|
|
300
|
-
config
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
300
|
+
// Allows config with missing `useDateObjects`, but keeps `UseDates`
|
|
301
|
+
// as a non-nullable in the InstantConfig type.
|
|
302
|
+
config: Omit<InstantConfig<Schema, UseDates>, 'useDateObjects'> & {
|
|
303
|
+
useDateObjects?: UseDates;
|
|
304
|
+
},
|
|
305
|
+
): InstantAdminDatabase<Schema, UseDates, InstantConfig<Schema, UseDates>> {
|
|
306
|
+
const configStrict = {
|
|
307
|
+
...config,
|
|
308
|
+
useDateObjects: (config.useDateObjects ?? false) as UseDates,
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
return new InstantAdminDatabase<
|
|
312
|
+
Schema,
|
|
313
|
+
UseDates,
|
|
314
|
+
InstantConfig<Schema, UseDates>
|
|
315
|
+
>(configStrict);
|
|
305
316
|
}
|
|
306
317
|
|
|
307
318
|
/**
|
|
@@ -680,12 +691,12 @@ class Storage {
|
|
|
680
691
|
duplex = 'half'; // one-way stream
|
|
681
692
|
}
|
|
682
693
|
if (isNodeReadable(file) || isWebReadable(file)) {
|
|
683
|
-
headers['content-length'] = metadata.fileSize.toString();
|
|
684
694
|
if (!metadata.fileSize) {
|
|
685
695
|
throw new Error(
|
|
686
696
|
'fileSize is required in metadata when uploading streams',
|
|
687
697
|
);
|
|
688
698
|
}
|
|
699
|
+
headers['content-length'] = metadata.fileSize.toString();
|
|
689
700
|
}
|
|
690
701
|
|
|
691
702
|
let options = {
|
|
@@ -752,12 +763,15 @@ class Storage {
|
|
|
752
763
|
}),
|
|
753
764
|
},
|
|
754
765
|
);
|
|
766
|
+
const headers = {};
|
|
767
|
+
const contentType = metadata.contentType;
|
|
768
|
+
if (contentType) {
|
|
769
|
+
headers['Content-Type'] = contentType;
|
|
770
|
+
}
|
|
755
771
|
const { ok } = await fetch(presignedUrl, {
|
|
756
772
|
method: 'PUT',
|
|
757
773
|
body: file as unknown as BodyInit,
|
|
758
|
-
headers
|
|
759
|
-
'Content-Type': metadata.contentType,
|
|
760
|
-
},
|
|
774
|
+
headers,
|
|
761
775
|
});
|
|
762
776
|
|
|
763
777
|
return ok;
|
|
@@ -823,9 +837,13 @@ type AdminQueryOpts = {
|
|
|
823
837
|
*/
|
|
824
838
|
class InstantAdminDatabase<
|
|
825
839
|
Schema extends InstantSchemaDef<any, any, any>,
|
|
826
|
-
|
|
840
|
+
UseDates extends boolean = false,
|
|
841
|
+
Config extends InstantConfig<Schema, UseDates> = InstantConfig<
|
|
842
|
+
Schema,
|
|
843
|
+
UseDates
|
|
844
|
+
>,
|
|
827
845
|
> {
|
|
828
|
-
config: InstantConfigFilled<Schema,
|
|
846
|
+
config: InstantConfigFilled<Schema, UseDates>;
|
|
829
847
|
auth: Auth;
|
|
830
848
|
storage: Storage;
|
|
831
849
|
rooms: Rooms<Schema>;
|
|
@@ -849,8 +867,10 @@ class InstantAdminDatabase<
|
|
|
849
867
|
* @example
|
|
850
868
|
* await db.asUser({email: "stopa@instantdb.com"}).query({ goals: {} })
|
|
851
869
|
*/
|
|
852
|
-
asUser = (
|
|
853
|
-
|
|
870
|
+
asUser = (
|
|
871
|
+
opts: ImpersonationOpts,
|
|
872
|
+
): InstantAdminDatabase<Schema, UseDates, Config> => {
|
|
873
|
+
const newClient = new InstantAdminDatabase<Schema, UseDates, Config>({
|
|
854
874
|
...(this.config as Config),
|
|
855
875
|
});
|
|
856
876
|
newClient.impersonationOpts = opts;
|
|
@@ -876,7 +896,7 @@ class InstantAdminDatabase<
|
|
|
876
896
|
query = <Q extends ValidQuery<Q, Schema>>(
|
|
877
897
|
query: Q,
|
|
878
898
|
opts: AdminQueryOpts = {},
|
|
879
|
-
): Promise<InstaQLResponse<Schema, Q,
|
|
899
|
+
): Promise<InstaQLResponse<Schema, Q, UseDates>> => {
|
|
880
900
|
if (query && opts && 'ruleParams' in opts) {
|
|
881
901
|
query = { $$ruleParams: opts['ruleParams'], ...query };
|
|
882
902
|
}
|
|
@@ -938,9 +958,9 @@ class InstantAdminDatabase<
|
|
|
938
958
|
*/
|
|
939
959
|
subscribeQuery<Q extends ValidQuery<Q, Schema>>(
|
|
940
960
|
query: Q,
|
|
941
|
-
cb?: SubscribeQueryCallback<Schema, Q,
|
|
961
|
+
cb?: SubscribeQueryCallback<Schema, Q, UseDates>,
|
|
942
962
|
opts: AdminQueryOpts = {},
|
|
943
|
-
): SubscribeQueryResponse<Schema, Q,
|
|
963
|
+
): SubscribeQueryResponse<Schema, Q, UseDates> {
|
|
944
964
|
if (query && opts && 'ruleParams' in opts) {
|
|
945
965
|
query = { $$ruleParams: opts['ruleParams'], ...query };
|
|
946
966
|
}
|
|
@@ -1030,7 +1050,7 @@ class InstantAdminDatabase<
|
|
|
1030
1050
|
query: Q,
|
|
1031
1051
|
opts?: { rules?: any; ruleParams?: { [key: string]: any } },
|
|
1032
1052
|
): Promise<{
|
|
1033
|
-
result: InstaQLResponse<Schema, Q,
|
|
1053
|
+
result: InstaQLResponse<Schema, Q, UseDates>;
|
|
1034
1054
|
checkResults: DebugCheckResult[];
|
|
1035
1055
|
}> => {
|
|
1036
1056
|
if (query && opts && 'ruleParams' in opts) {
|
package/src/subscribe.ts
CHANGED
|
@@ -21,12 +21,12 @@ export type SubscribeQuerySessionInfo = {
|
|
|
21
21
|
export type SubscribeQueryPayload<
|
|
22
22
|
Schema extends InstantSchemaDef<any, any, any>,
|
|
23
23
|
Q extends ValidQuery<Q, Schema>,
|
|
24
|
-
|
|
24
|
+
UseDates extends boolean = false,
|
|
25
25
|
> =
|
|
26
26
|
| {
|
|
27
27
|
type: 'ok';
|
|
28
|
-
data: InstaQLResponse<Schema, Q,
|
|
29
|
-
pageInfo: PageInfoResponse<Q
|
|
28
|
+
data: InstaQLResponse<Schema, Q, UseDates>;
|
|
29
|
+
pageInfo: PageInfoResponse<Q> | undefined;
|
|
30
30
|
sessionInfo: SubscribeQuerySessionInfo | null;
|
|
31
31
|
}
|
|
32
32
|
| {
|
|
@@ -40,13 +40,13 @@ export type SubscribeQueryPayload<
|
|
|
40
40
|
export type SubscribeQueryCallback<
|
|
41
41
|
Schema extends InstantSchemaDef<any, any, any>,
|
|
42
42
|
Q extends ValidQuery<Q, Schema>,
|
|
43
|
-
|
|
44
|
-
> = (payload: SubscribeQueryPayload<Schema, Q,
|
|
43
|
+
UseDates extends boolean = false,
|
|
44
|
+
> = (payload: SubscribeQueryPayload<Schema, Q, UseDates>) => void;
|
|
45
45
|
|
|
46
46
|
export interface SubscribeQueryResponse<
|
|
47
47
|
Schema extends InstantSchemaDef<any, any, any>,
|
|
48
48
|
Q extends ValidQuery<Q, Schema>,
|
|
49
|
-
|
|
49
|
+
UseDates extends boolean = false,
|
|
50
50
|
> {
|
|
51
51
|
/** Stop the subscription and close the connection. */
|
|
52
52
|
close(): void;
|
|
@@ -56,7 +56,7 @@ export interface SubscribeQueryResponse<
|
|
|
56
56
|
|
|
57
57
|
/** Async iterator of query payloads */
|
|
58
58
|
[Symbol.asyncIterator](): AsyncIterableIterator<
|
|
59
|
-
SubscribeQueryPayload<Schema, Q,
|
|
59
|
+
SubscribeQueryPayload<Schema, Q, UseDates>
|
|
60
60
|
>;
|
|
61
61
|
|
|
62
62
|
/** Ready state of the connection */
|
|
@@ -72,19 +72,19 @@ export interface SubscribeQueryResponse<
|
|
|
72
72
|
function makeAsyncIterator<
|
|
73
73
|
Schema extends InstantSchemaDef<any, any, any>,
|
|
74
74
|
Q extends ValidQuery<Q, Schema>,
|
|
75
|
-
|
|
75
|
+
UseDates extends boolean = false,
|
|
76
76
|
>(
|
|
77
|
-
subscribe: (cb: SubscribeQueryCallback<Schema, Q,
|
|
77
|
+
subscribe: (cb: SubscribeQueryCallback<Schema, Q, UseDates>) => void,
|
|
78
78
|
subscribeOnClose: (cb: () => void) => void,
|
|
79
|
-
unsubscribe: (cb: SubscribeQueryCallback<Schema, Q,
|
|
79
|
+
unsubscribe: (cb: SubscribeQueryCallback<Schema, Q, UseDates>) => void,
|
|
80
80
|
readyState: () => SubscriptionReadyState,
|
|
81
|
-
): AsyncGenerator<SubscribeQueryPayload<Schema, Q,
|
|
82
|
-
let wakeup = null;
|
|
81
|
+
): AsyncGenerator<SubscribeQueryPayload<Schema, Q, UseDates>> {
|
|
82
|
+
let wakeup: (() => void) | null = null;
|
|
83
83
|
let closed = false;
|
|
84
84
|
|
|
85
|
-
const backlog: SubscribeQueryPayload<Schema, Q,
|
|
86
|
-
const handler: SubscribeQueryCallback<Schema, Q,
|
|
87
|
-
data: SubscribeQueryPayload<Schema, Q,
|
|
85
|
+
const backlog: SubscribeQueryPayload<Schema, Q, UseDates>[] = [];
|
|
86
|
+
const handler: SubscribeQueryCallback<Schema, Q, UseDates> = (
|
|
87
|
+
data: SubscribeQueryPayload<Schema, Q, UseDates>,
|
|
88
88
|
): void => {
|
|
89
89
|
backlog.push(data);
|
|
90
90
|
if (backlog.length > 100) {
|
|
@@ -102,7 +102,10 @@ function makeAsyncIterator<
|
|
|
102
102
|
|
|
103
103
|
subscribe(handler);
|
|
104
104
|
|
|
105
|
-
const done = ()
|
|
105
|
+
const done = (): Promise<{
|
|
106
|
+
done: true;
|
|
107
|
+
value: undefined;
|
|
108
|
+
}> => {
|
|
106
109
|
unsubscribe(handler);
|
|
107
110
|
return Promise.resolve({ done: true, value: undefined });
|
|
108
111
|
};
|
|
@@ -117,7 +120,9 @@ function makeAsyncIterator<
|
|
|
117
120
|
|
|
118
121
|
subscribeOnClose(onClose);
|
|
119
122
|
|
|
120
|
-
const next = async ()
|
|
123
|
+
const next = async (): Promise<
|
|
124
|
+
IteratorResult<SubscribeQueryPayload<Schema, Q, UseDates>, undefined>
|
|
125
|
+
> => {
|
|
121
126
|
while (true) {
|
|
122
127
|
if (readyState() === 'closed' || closed) {
|
|
123
128
|
return done();
|
|
@@ -128,7 +133,7 @@ function makeAsyncIterator<
|
|
|
128
133
|
return { value: nextValue, done: false };
|
|
129
134
|
}
|
|
130
135
|
|
|
131
|
-
const p = new Promise((resolve) => {
|
|
136
|
+
const p = new Promise<void>((resolve) => {
|
|
132
137
|
wakeup = resolve;
|
|
133
138
|
});
|
|
134
139
|
|
|
@@ -160,11 +165,13 @@ function esReadyState(es: EventSource): SubscriptionReadyState {
|
|
|
160
165
|
case es.OPEN: {
|
|
161
166
|
return 'open';
|
|
162
167
|
}
|
|
168
|
+
default:
|
|
169
|
+
return 'connecting';
|
|
163
170
|
}
|
|
164
171
|
}
|
|
165
172
|
|
|
166
|
-
function multiReadFetchResponse(r) {
|
|
167
|
-
let p = null;
|
|
173
|
+
function multiReadFetchResponse(r: Response) {
|
|
174
|
+
let p: null | Promise<string> = null;
|
|
168
175
|
return {
|
|
169
176
|
...r,
|
|
170
177
|
text() {
|
|
@@ -214,12 +221,12 @@ function formatPageInfo(
|
|
|
214
221
|
export function subscribe<
|
|
215
222
|
Schema extends InstantSchemaDef<any, any, any>,
|
|
216
223
|
Q extends ValidQuery<Q, Schema>,
|
|
217
|
-
|
|
224
|
+
UseDates extends boolean,
|
|
218
225
|
>(
|
|
219
226
|
query: Q,
|
|
220
|
-
cb,
|
|
227
|
+
cb: SubscribeQueryCallback<Schema, Q, UseDates> | undefined,
|
|
221
228
|
opts: { headers: HeadersInit; inference: boolean; apiURI: string },
|
|
222
|
-
): SubscribeQueryResponse<Schema, Q,
|
|
229
|
+
): SubscribeQueryResponse<Schema, Q, UseDates> {
|
|
223
230
|
let fetchErrorResponse;
|
|
224
231
|
let closed = false;
|
|
225
232
|
|
|
@@ -254,8 +261,8 @@ export function subscribe<
|
|
|
254
261
|
},
|
|
255
262
|
);
|
|
256
263
|
|
|
257
|
-
const subscribers: SubscribeQueryCallback<Schema, Q,
|
|
258
|
-
const onCloseSubscribers = [];
|
|
264
|
+
const subscribers: SubscribeQueryCallback<Schema, Q, UseDates>[] = [];
|
|
265
|
+
const onCloseSubscribers: (() => void)[] = [];
|
|
259
266
|
|
|
260
267
|
const subscribe = (cb) => {
|
|
261
268
|
subscribers.push(cb);
|
|
@@ -275,7 +282,7 @@ export function subscribe<
|
|
|
275
282
|
|
|
276
283
|
let sessionParams: SubscribeQuerySessionInfo | null = null;
|
|
277
284
|
|
|
278
|
-
function deliver(result: SubscribeQueryPayload<Schema, Q,
|
|
285
|
+
function deliver(result: SubscribeQueryPayload<Schema, Q, UseDates>) {
|
|
279
286
|
if (closed) {
|
|
280
287
|
return;
|
|
281
288
|
}
|
package/tsconfig.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"extends": "../../tsconfig.base.json",
|
|
3
3
|
"include": ["src"],
|
|
4
|
-
"exclude": ["node_modules", "dist"],
|
|
4
|
+
"exclude": ["node_modules", "dist", "src/__types__"],
|
|
5
5
|
"compilerOptions": {
|
|
6
6
|
"outDir": "dist/tsc",
|
|
7
7
|
"rewriteRelativeImportExtensions": true,
|
|
8
|
-
"rootDir": "src"
|
|
8
|
+
"rootDir": "src",
|
|
9
|
+
"strictNullChecks": true
|
|
9
10
|
}
|
|
10
11
|
}
|