@instantdb/core 0.22.89-experimental.drewh-ssr.20280366757.1 → 0.22.89-experimental.split-store.20276199573.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/commonjs/Reactor.d.ts +1 -13
- package/dist/commonjs/Reactor.d.ts.map +1 -1
- package/dist/commonjs/Reactor.js +5 -69
- package/dist/commonjs/Reactor.js.map +1 -1
- package/dist/commonjs/index.d.ts +1 -5
- package/dist/commonjs/index.d.ts.map +1 -1
- package/dist/commonjs/index.js +1 -7
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/reactorTypes.d.ts +4 -5
- package/dist/commonjs/reactorTypes.d.ts.map +1 -1
- package/dist/commonjs/reactorTypes.js.map +1 -1
- package/dist/esm/Reactor.d.ts +1 -13
- package/dist/esm/Reactor.d.ts.map +1 -1
- package/dist/esm/Reactor.js +5 -69
- package/dist/esm/Reactor.js.map +1 -1
- package/dist/esm/index.d.ts +1 -5
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +2 -5
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/reactorTypes.d.ts +4 -5
- package/dist/esm/reactorTypes.d.ts.map +1 -1
- package/dist/esm/reactorTypes.js.map +1 -1
- package/dist/standalone/index.js +2377 -2697
- package/dist/standalone/index.umd.cjs +3 -3
- package/package.json +2 -2
- package/src/Reactor.js +6 -84
- package/src/index.ts +0 -9
- package/src/reactorTypes.ts +4 -5
- package/__tests__/src/serializeSchema.test.ts +0 -123
- package/dist/commonjs/createRouteHandler.d.ts +0 -8
- package/dist/commonjs/createRouteHandler.d.ts.map +0 -1
- package/dist/commonjs/createRouteHandler.js +0 -57
- package/dist/commonjs/createRouteHandler.js.map +0 -1
- package/dist/commonjs/framework.d.ts +0 -77
- package/dist/commonjs/framework.d.ts.map +0 -1
- package/dist/commonjs/framework.js +0 -209
- package/dist/commonjs/framework.js.map +0 -1
- package/dist/commonjs/parseSchemaFromJSON.d.ts +0 -3
- package/dist/commonjs/parseSchemaFromJSON.d.ts.map +0 -1
- package/dist/commonjs/parseSchemaFromJSON.js +0 -148
- package/dist/commonjs/parseSchemaFromJSON.js.map +0 -1
- package/dist/esm/createRouteHandler.d.ts +0 -8
- package/dist/esm/createRouteHandler.d.ts.map +0 -1
- package/dist/esm/createRouteHandler.js +0 -53
- package/dist/esm/createRouteHandler.js.map +0 -1
- package/dist/esm/framework.d.ts +0 -77
- package/dist/esm/framework.d.ts.map +0 -1
- package/dist/esm/framework.js +0 -169
- package/dist/esm/framework.js.map +0 -1
- package/dist/esm/parseSchemaFromJSON.d.ts +0 -3
- package/dist/esm/parseSchemaFromJSON.d.ts.map +0 -1
- package/dist/esm/parseSchemaFromJSON.js +0 -144
- package/dist/esm/parseSchemaFromJSON.js.map +0 -1
- package/src/createRouteHandler.ts +0 -44
- package/src/framework.ts +0 -294
- package/src/parseSchemaFromJSON.ts +0 -176
package/dist/esm/framework.js
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import { coerceQuery, weakHash, } from "./index.js";
|
|
11
|
-
import * as s from './store.js';
|
|
12
|
-
import instaql from './instaql.js';
|
|
13
|
-
import { createLinkIndex } from "./utils/linkIndex.js";
|
|
14
|
-
export const isServer = typeof window === 'undefined' || 'Deno' in globalThis;
|
|
15
|
-
export class FrameworkClient {
|
|
16
|
-
constructor(params) {
|
|
17
|
-
this.resultMap = new Map();
|
|
18
|
-
this.queryResolvedCallbacks = [];
|
|
19
|
-
this.subscribe = (callback) => {
|
|
20
|
-
this.queryResolvedCallbacks.push(callback);
|
|
21
|
-
};
|
|
22
|
-
// Runs on the client when ssr gets html script tags
|
|
23
|
-
this.addQueryResult = (queryKey, value) => {
|
|
24
|
-
this.resultMap.set(queryKey, {
|
|
25
|
-
type: value.type,
|
|
26
|
-
status: 'success',
|
|
27
|
-
data: value,
|
|
28
|
-
promise: null,
|
|
29
|
-
error: null,
|
|
30
|
-
});
|
|
31
|
-
// send the result to the client
|
|
32
|
-
if (!isServer) {
|
|
33
|
-
// make sure the attrs are there to create stores
|
|
34
|
-
if (!this.db._reactor.attrs) {
|
|
35
|
-
this.db._reactor._setAttrs(value.attrs);
|
|
36
|
-
}
|
|
37
|
-
this.db._reactor._addQueryData(value.query, value, !!this.db._reactor.config.schema);
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
this.query = (_query, opts) => {
|
|
41
|
-
const { hash, query } = this.hashQuery(_query, opts);
|
|
42
|
-
if (this.db._reactor.status === 'authenticated') {
|
|
43
|
-
const promise = this.db.queryOnce(_query, opts);
|
|
44
|
-
let entry = {
|
|
45
|
-
status: 'pending',
|
|
46
|
-
type: 'session',
|
|
47
|
-
data: undefined,
|
|
48
|
-
error: undefined,
|
|
49
|
-
promise: promise,
|
|
50
|
-
};
|
|
51
|
-
promise.then((result) => {
|
|
52
|
-
entry.status = 'success';
|
|
53
|
-
entry.data = result;
|
|
54
|
-
entry.promise = null;
|
|
55
|
-
});
|
|
56
|
-
promise.catch((error) => {
|
|
57
|
-
entry.status = 'error';
|
|
58
|
-
entry.error = error;
|
|
59
|
-
entry.promise = null;
|
|
60
|
-
});
|
|
61
|
-
return entry;
|
|
62
|
-
}
|
|
63
|
-
const promise = this.getTriplesAndAttrsForQuery(query);
|
|
64
|
-
let entry = {
|
|
65
|
-
status: 'pending',
|
|
66
|
-
type: 'http',
|
|
67
|
-
data: undefined,
|
|
68
|
-
error: undefined,
|
|
69
|
-
promise: promise,
|
|
70
|
-
};
|
|
71
|
-
promise.then((result) => {
|
|
72
|
-
entry.status = 'success';
|
|
73
|
-
entry.data = result;
|
|
74
|
-
entry.promise = null;
|
|
75
|
-
});
|
|
76
|
-
promise.catch((error) => {
|
|
77
|
-
entry.status = 'error';
|
|
78
|
-
entry.error = error;
|
|
79
|
-
entry.promise = null;
|
|
80
|
-
});
|
|
81
|
-
promise.then((result) => {
|
|
82
|
-
this.queryResolvedCallbacks.forEach((callback) => {
|
|
83
|
-
callback({
|
|
84
|
-
queryHash: hash,
|
|
85
|
-
query: query,
|
|
86
|
-
attrs: result.attrs,
|
|
87
|
-
triples: result.triples,
|
|
88
|
-
pageInfo: result.pageInfo,
|
|
89
|
-
});
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
this.resultMap.set(hash, entry);
|
|
93
|
-
return entry;
|
|
94
|
-
};
|
|
95
|
-
this.getExistingResultForQuery = (_query, opts) => {
|
|
96
|
-
const { hash } = this.hashQuery(_query, opts);
|
|
97
|
-
return this.resultMap.get(hash);
|
|
98
|
-
};
|
|
99
|
-
this.completeIsomorphic = (query, triples, attrs, pageInfo) => {
|
|
100
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
101
|
-
const attrMap = {};
|
|
102
|
-
attrs.forEach((attr) => {
|
|
103
|
-
attrMap[attr.id] = attr;
|
|
104
|
-
});
|
|
105
|
-
const enableCardinalityInference = Boolean((_c = (_b = (_a = this.db) === null || _a === void 0 ? void 0 : _a._reactor) === null || _b === void 0 ? void 0 : _b.config) === null || _c === void 0 ? void 0 : _c.schema) &&
|
|
106
|
-
('cardinalityInference' in ((_e = (_d = this.db) === null || _d === void 0 ? void 0 : _d._reactor) === null || _e === void 0 ? void 0 : _e.config)
|
|
107
|
-
? Boolean((_g = (_f = this.db) === null || _f === void 0 ? void 0 : _f._reactor.config) === null || _g === void 0 ? void 0 : _g.cardinalityInference)
|
|
108
|
-
: true);
|
|
109
|
-
const attrsStore = new s.AttrsStoreClass(attrs.reduce((acc, attr) => {
|
|
110
|
-
acc[attr.id] = attr;
|
|
111
|
-
return acc;
|
|
112
|
-
}, {}), createLinkIndex((_h = this.db) === null || _h === void 0 ? void 0 : _h._reactor.config.schema));
|
|
113
|
-
const store = s.createStore(attrsStore, triples, enableCardinalityInference, this.params.db._reactor.config.useDateObjects || false);
|
|
114
|
-
const resp = instaql({
|
|
115
|
-
store: store,
|
|
116
|
-
attrsStore: attrsStore,
|
|
117
|
-
pageInfo: pageInfo,
|
|
118
|
-
aggregate: undefined,
|
|
119
|
-
}, query);
|
|
120
|
-
return resp;
|
|
121
|
-
};
|
|
122
|
-
this.hashQuery = (_query, opts) => {
|
|
123
|
-
if (_query && opts && 'ruleParams' in opts) {
|
|
124
|
-
_query = Object.assign({ $$ruleParams: opts['ruleParams'] }, _query);
|
|
125
|
-
}
|
|
126
|
-
const query = _query ? coerceQuery(_query) : null;
|
|
127
|
-
return { hash: weakHash(query), query: query };
|
|
128
|
-
};
|
|
129
|
-
this.getTriplesAndAttrsForQuery = (query) => __awaiter(this, void 0, void 0, function* () {
|
|
130
|
-
var _a, _b, _c, _d, _e, _f;
|
|
131
|
-
const response = yield fetch(`${this.db._reactor.config.apiURI}/runtime/framework/query`, {
|
|
132
|
-
method: 'POST',
|
|
133
|
-
headers: {
|
|
134
|
-
'app-id': this.params.db._reactor.config.appId,
|
|
135
|
-
'Content-Type': 'application/json',
|
|
136
|
-
Authorization: this.params.token
|
|
137
|
-
? `Bearer ${this.params.token}`
|
|
138
|
-
: undefined,
|
|
139
|
-
},
|
|
140
|
-
body: JSON.stringify({
|
|
141
|
-
query: query,
|
|
142
|
-
}),
|
|
143
|
-
});
|
|
144
|
-
if (!response.ok) {
|
|
145
|
-
throw new Error('Error getting triples from server');
|
|
146
|
-
}
|
|
147
|
-
const data = yield response.json();
|
|
148
|
-
const attrs = data === null || data === void 0 ? void 0 : data.attrs;
|
|
149
|
-
if (!attrs) {
|
|
150
|
-
throw new Error('No attrs');
|
|
151
|
-
}
|
|
152
|
-
// TODO: make safer
|
|
153
|
-
const triples = (_c = (_b = (_a = data.result) === null || _a === void 0 ? void 0 : _a[0].data) === null || _b === void 0 ? void 0 : _b['datalog-result']) === null || _c === void 0 ? void 0 : _c['join-rows'][0];
|
|
154
|
-
const pageInfo = (_f = (_e = (_d = data.result) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.data) === null || _f === void 0 ? void 0 : _f['page-info'];
|
|
155
|
-
return {
|
|
156
|
-
attrs,
|
|
157
|
-
triples,
|
|
158
|
-
type: 'http',
|
|
159
|
-
queryHash: this.hashQuery(query).hash,
|
|
160
|
-
query,
|
|
161
|
-
pageInfo,
|
|
162
|
-
};
|
|
163
|
-
});
|
|
164
|
-
this.params = params;
|
|
165
|
-
this.db = params.db;
|
|
166
|
-
this.resultMap = new Map();
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
//# sourceMappingURL=framework.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"framework.js","sourceRoot":"","sources":["../../src/framework.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EACL,WAAW,EAGX,QAAQ,GACT,MAAM,YAAY,CAAC;AACpB,OAAO,KAAK,CAAC,MAAM,YAAY,CAAC;AAChC,OAAO,OAAO,MAAM,cAAc,CAAC;AAEnC,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAEvD,MAAM,CAAC,MAAM,QAAQ,GAAG,OAAO,MAAM,KAAK,WAAW,IAAI,MAAM,IAAI,UAAU,CAAC;AAqB9E,MAAM,OAAO,eAAe;IAsB1B,YAAY,MAAuB;QAnB5B,cAAS,GASZ,IAAI,GAAG,EAAE,CAAC;QAEN,2BAAsB,GAMd,EAAE,CAAC;QAiBZ,cAAS,GAAG,CACjB,QAKU,EACV,EAAE;YAC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,CAAC,KAAK,CAAC,CAAC,IAAI;gBACrC,KAAK;gBACL,QAAQ;aACT,CAAC;QACJ,CAAC,CAAA,CAAC;QA7OA,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,EAAE,GAAG,MAAM,CAAC,EAAE,CAAC;QACpB,IAAI,CAAC,SAAS,GAAG,IAAI,GAAG,EASrB,CAAC;IACN,CAAC;CAkOF","sourcesContent":["import {\n coerceQuery,\n InstantCoreDatabase,\n InstantDBAttr,\n weakHash,\n} from './index.ts';\nimport * as s from './store.js';\nimport instaql from './instaql.js';\nimport { RuleParams } from './schemaTypes.ts';\nimport { createLinkIndex } from './utils/linkIndex.ts';\n\nexport const isServer = typeof window === 'undefined' || 'Deno' in globalThis;\n\nexport type FrameworkConfig = {\n token?: string | null;\n db: InstantCoreDatabase<any, any>;\n};\n\ntype QueryPromise =\n | {\n type: 'http';\n triples: any;\n attrs: any;\n queryHash: any;\n query: any;\n pageInfo?: any;\n }\n | {\n type: 'session';\n queryResult: any;\n };\n\nexport class FrameworkClient {\n private params: FrameworkConfig;\n private db: InstantCoreDatabase<any, any>;\n public resultMap: Map<\n string,\n {\n status: 'pending' | 'success' | 'error';\n type: 'http' | 'session';\n promise?: Promise<QueryPromise> | null;\n data?: any;\n error?: any;\n }\n > = new Map();\n\n private queryResolvedCallbacks: ((result: {\n triples: any;\n attrs: any;\n queryHash: any;\n query: any;\n pageInfo?: any;\n }) => void)[] = [];\n\n constructor(params: FrameworkConfig) {\n this.params = params;\n this.db = params.db;\n this.resultMap = new Map<\n string,\n {\n type: 'http' | 'session';\n status: 'pending' | 'success' | 'error';\n promise?: Promise<QueryPromise>;\n data?: any;\n error?: any;\n }\n >();\n }\n\n public subscribe = (\n callback: (result: {\n triples: any;\n attrs: any;\n queryHash: string;\n pageInfo?: any;\n }) => void,\n ) => {\n this.queryResolvedCallbacks.push(callback);\n };\n\n // Runs on the client when ssr gets html script tags\n public addQueryResult = (queryKey: string, value: any) => {\n this.resultMap.set(queryKey, {\n type: value.type,\n status: 'success',\n data: value,\n promise: null,\n error: null,\n });\n // send the result to the client\n if (!isServer) {\n // make sure the attrs are there to create stores\n if (!this.db._reactor.attrs) {\n this.db._reactor._setAttrs(value.attrs);\n }\n this.db._reactor._addQueryData(\n value.query,\n value,\n !!this.db._reactor.config.schema,\n );\n }\n };\n\n public query = (\n _query: any,\n opts?: {\n ruleParams: RuleParams;\n },\n ): {\n type: 'http' | 'session';\n status: 'pending' | 'success' | 'error';\n promise?: Promise<QueryPromise>;\n data?: any;\n error?: any;\n } => {\n const { hash, query } = this.hashQuery(_query, opts);\n\n if (this.db._reactor.status === 'authenticated') {\n const promise = this.db.queryOnce(_query, opts);\n let entry = {\n status: 'pending' as 'pending' | 'success' | 'error',\n type: 'session' as 'http' | 'session',\n data: undefined as any,\n error: undefined as any,\n promise: promise as any,\n };\n promise.then((result) => {\n entry.status = 'success';\n entry.data = result;\n entry.promise = null;\n });\n promise.catch((error) => {\n entry.status = 'error';\n entry.error = error;\n entry.promise = null;\n });\n return entry as any;\n }\n\n const promise = this.getTriplesAndAttrsForQuery(query);\n let entry = {\n status: 'pending' as 'pending' | 'success' | 'error',\n type: 'http' as 'http' | 'session',\n data: undefined as any,\n error: undefined as any,\n promise: promise as any,\n };\n\n promise.then((result) => {\n entry.status = 'success';\n entry.data = result;\n entry.promise = null;\n });\n promise.catch((error) => {\n entry.status = 'error';\n entry.error = error;\n entry.promise = null;\n });\n\n promise.then((result) => {\n this.queryResolvedCallbacks.forEach((callback) => {\n callback({\n queryHash: hash,\n query: query,\n attrs: result.attrs,\n triples: result.triples,\n pageInfo: result.pageInfo,\n });\n });\n });\n\n this.resultMap.set(hash, entry);\n return entry;\n };\n\n public getExistingResultForQuery = (\n _query: any,\n opts?: {\n ruleParams: RuleParams;\n },\n ) => {\n const { hash } = this.hashQuery(_query, opts);\n return this.resultMap.get(hash);\n };\n\n public completeIsomorphic = (\n query: any,\n triples: any[],\n attrs: InstantDBAttr[],\n pageInfo?: any,\n ) => {\n const attrMap = {};\n attrs.forEach((attr) => {\n attrMap[attr.id] = attr;\n });\n\n const enableCardinalityInference =\n Boolean(this.db?._reactor?.config?.schema) &&\n ('cardinalityInference' in this.db?._reactor?.config\n ? Boolean(this.db?._reactor.config?.cardinalityInference)\n : true);\n\n const attrsStore = new s.AttrsStoreClass(\n attrs.reduce((acc, attr) => {\n acc[attr.id] = attr;\n return acc;\n }, {}),\n createLinkIndex(this.db?._reactor.config.schema),\n );\n\n const store = s.createStore(\n attrsStore,\n triples,\n enableCardinalityInference,\n this.params.db._reactor.config.useDateObjects || false,\n );\n const resp = instaql(\n {\n store: store,\n attrsStore: attrsStore,\n pageInfo: pageInfo,\n aggregate: undefined,\n },\n query,\n );\n return resp;\n };\n\n public hashQuery = (\n _query: any,\n opts?: {\n ruleParams: RuleParams;\n },\n ): { hash: string; query: any } => {\n if (_query && opts && 'ruleParams' in opts) {\n _query = { $$ruleParams: opts['ruleParams'], ..._query };\n }\n const query = _query ? coerceQuery(_query) : null;\n return { hash: weakHash(query), query: query };\n };\n\n public getTriplesAndAttrsForQuery = async (\n query: any,\n ): Promise<{\n triples: any[];\n attrs: InstantDBAttr[];\n query: any;\n queryHash: string;\n type: 'http';\n pageInfo?: any;\n }> => {\n const response = await fetch(\n `${this.db._reactor.config.apiURI}/runtime/framework/query`,\n {\n method: 'POST',\n headers: {\n 'app-id': this.params.db._reactor.config.appId,\n 'Content-Type': 'application/json',\n Authorization: this.params.token\n ? `Bearer ${this.params.token}`\n : undefined,\n } as Record<string, string>,\n body: JSON.stringify({\n query: query,\n }),\n },\n );\n\n if (!response.ok) {\n throw new Error('Error getting triples from server');\n }\n\n const data = await response.json();\n\n const attrs = data?.attrs;\n if (!attrs) {\n throw new Error('No attrs');\n }\n\n // TODO: make safer\n const triples = data.result?.[0].data?.['datalog-result']?.['join-rows'][0];\n\n const pageInfo = data.result?.[0]?.data?.['page-info'];\n\n return {\n attrs,\n triples,\n type: 'http',\n queryHash: this.hashQuery(query).hash,\n query,\n pageInfo,\n };\n };\n}\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"parseSchemaFromJSON.d.ts","sourceRoot":"","sources":["../../src/parseSchemaFromJSON.ts"],"names":[],"mappings":"AACA,OAAO,EAAe,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAEjE,eAAO,MAAM,mBAAmB,GAC9B,GAAG,GAAG,KACL,gBAAgB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CA0KhC,CAAC"}
|
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
import { i } from "./schema.js";
|
|
2
|
-
export const parseSchemaFromJSON = (s) => {
|
|
3
|
-
var _a, _b, _c, _d, _e, _f;
|
|
4
|
-
// Parse entities
|
|
5
|
-
const entities = {};
|
|
6
|
-
for (const [entityName, entityInfo] of Object.entries(s.entities)) {
|
|
7
|
-
const entityDef = entityInfo;
|
|
8
|
-
const attrs = {};
|
|
9
|
-
// Parse attributes
|
|
10
|
-
for (const [attrName, attrInfo] of Object.entries(entityDef.attrs)) {
|
|
11
|
-
const attrDef = attrInfo;
|
|
12
|
-
let attr;
|
|
13
|
-
// Create the appropriate attribute type
|
|
14
|
-
switch (attrDef.valueType) {
|
|
15
|
-
case 'string':
|
|
16
|
-
attr = i.string();
|
|
17
|
-
break;
|
|
18
|
-
case 'number':
|
|
19
|
-
attr = i.number();
|
|
20
|
-
break;
|
|
21
|
-
case 'boolean':
|
|
22
|
-
attr = i.boolean();
|
|
23
|
-
break;
|
|
24
|
-
case 'date':
|
|
25
|
-
attr = i.date();
|
|
26
|
-
break;
|
|
27
|
-
case 'json':
|
|
28
|
-
attr = i.json();
|
|
29
|
-
break;
|
|
30
|
-
default:
|
|
31
|
-
attr = i.json();
|
|
32
|
-
}
|
|
33
|
-
// Apply modifiers
|
|
34
|
-
if (!attrDef.required) {
|
|
35
|
-
attr = attr.optional();
|
|
36
|
-
}
|
|
37
|
-
if ((_a = attrDef.config) === null || _a === void 0 ? void 0 : _a.indexed) {
|
|
38
|
-
attr = attr.indexed();
|
|
39
|
-
}
|
|
40
|
-
if ((_b = attrDef.config) === null || _b === void 0 ? void 0 : _b.unique) {
|
|
41
|
-
attr = attr.unique();
|
|
42
|
-
}
|
|
43
|
-
attrs[attrName] = attr;
|
|
44
|
-
}
|
|
45
|
-
entities[entityName] = i.entity(attrs);
|
|
46
|
-
}
|
|
47
|
-
// Parse links
|
|
48
|
-
const links = s.links || {};
|
|
49
|
-
// Parse rooms
|
|
50
|
-
const rooms = {};
|
|
51
|
-
if (s.rooms) {
|
|
52
|
-
for (const [roomName, roomInfo] of Object.entries(s.rooms)) {
|
|
53
|
-
const roomDef = roomInfo;
|
|
54
|
-
// Parse presence
|
|
55
|
-
const presenceAttrs = {};
|
|
56
|
-
for (const [attrName, attrInfo] of Object.entries(roomDef.presence.attrs)) {
|
|
57
|
-
const attrDef = attrInfo;
|
|
58
|
-
let attr;
|
|
59
|
-
switch (attrDef.valueType) {
|
|
60
|
-
case 'string':
|
|
61
|
-
attr = i.string();
|
|
62
|
-
break;
|
|
63
|
-
case 'number':
|
|
64
|
-
attr = i.number();
|
|
65
|
-
break;
|
|
66
|
-
case 'boolean':
|
|
67
|
-
attr = i.boolean();
|
|
68
|
-
break;
|
|
69
|
-
case 'date':
|
|
70
|
-
attr = i.date();
|
|
71
|
-
break;
|
|
72
|
-
case 'json':
|
|
73
|
-
attr = i.json();
|
|
74
|
-
break;
|
|
75
|
-
default:
|
|
76
|
-
attr = i.json();
|
|
77
|
-
}
|
|
78
|
-
if (!attrDef.required) {
|
|
79
|
-
attr = attr.optional();
|
|
80
|
-
}
|
|
81
|
-
if ((_c = attrDef.config) === null || _c === void 0 ? void 0 : _c.indexed) {
|
|
82
|
-
attr = attr.indexed();
|
|
83
|
-
}
|
|
84
|
-
if ((_d = attrDef.config) === null || _d === void 0 ? void 0 : _d.unique) {
|
|
85
|
-
attr = attr.unique();
|
|
86
|
-
}
|
|
87
|
-
presenceAttrs[attrName] = attr;
|
|
88
|
-
}
|
|
89
|
-
// Parse topics
|
|
90
|
-
const topics = {};
|
|
91
|
-
if (roomDef.topics) {
|
|
92
|
-
for (const [topicName, topicInfo] of Object.entries(roomDef.topics)) {
|
|
93
|
-
const topicDef = topicInfo;
|
|
94
|
-
const topicAttrs = {};
|
|
95
|
-
for (const [attrName, attrInfo] of Object.entries(topicDef.attrs)) {
|
|
96
|
-
const attrDef = attrInfo;
|
|
97
|
-
let attr;
|
|
98
|
-
switch (attrDef.valueType) {
|
|
99
|
-
case 'string':
|
|
100
|
-
attr = i.string();
|
|
101
|
-
break;
|
|
102
|
-
case 'number':
|
|
103
|
-
attr = i.number();
|
|
104
|
-
break;
|
|
105
|
-
case 'boolean':
|
|
106
|
-
attr = i.boolean();
|
|
107
|
-
break;
|
|
108
|
-
case 'date':
|
|
109
|
-
attr = i.date();
|
|
110
|
-
break;
|
|
111
|
-
case 'json':
|
|
112
|
-
attr = i.json();
|
|
113
|
-
break;
|
|
114
|
-
default:
|
|
115
|
-
attr = i.json();
|
|
116
|
-
}
|
|
117
|
-
if (!attrDef.required) {
|
|
118
|
-
attr = attr.optional();
|
|
119
|
-
}
|
|
120
|
-
if ((_e = attrDef.config) === null || _e === void 0 ? void 0 : _e.indexed) {
|
|
121
|
-
attr = attr.indexed();
|
|
122
|
-
}
|
|
123
|
-
if ((_f = attrDef.config) === null || _f === void 0 ? void 0 : _f.unique) {
|
|
124
|
-
attr = attr.unique();
|
|
125
|
-
}
|
|
126
|
-
topicAttrs[attrName] = attr;
|
|
127
|
-
}
|
|
128
|
-
topics[topicName] = i.entity(topicAttrs);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
rooms[roomName] = {
|
|
132
|
-
presence: i.entity(presenceAttrs),
|
|
133
|
-
topics,
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
const resultingSchema = i.schema({
|
|
138
|
-
entities,
|
|
139
|
-
links,
|
|
140
|
-
rooms,
|
|
141
|
-
});
|
|
142
|
-
return resultingSchema;
|
|
143
|
-
};
|
|
144
|
-
//# sourceMappingURL=parseSchemaFromJSON.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"parseSchemaFromJSON.js","sourceRoot":"","sources":["../../src/parseSchemaFromJSON.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,aAAa,CAAC;AAGhC,MAAM,CAAC,MAAM,mBAAmB,GAAG,CACjC,CAAM,EAC2B,EAAE;;IACnC,iBAAiB;IACjB,MAAM,QAAQ,GAAwB,EAAE,CAAC;IAEzC,KAAK,MAAM,CAAC,UAAU,EAAE,UAAU,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC;QAClE,MAAM,SAAS,GAAG,UAAiB,CAAC;QACpC,MAAM,KAAK,GAAwB,EAAE,CAAC;QAEtC,mBAAmB;QACnB,KAAK,MAAM,CAAC,QAAQ,EAAE,QAAQ,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;YACnE,MAAM,OAAO,GAAG,QAAe,CAAC;YAChC,IAAI,IAAgC,CAAC;YAErC,wCAAwC;YACxC,QAAQ,OAAO,CAAC,SAAS,EAAE,CAAC;gBAC1B,KAAK,QAAQ;oBACX,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC;oBAClB,MAAM;gBACR,KAAK,QAAQ;oBACX,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC;oBAClB,MAAM;gBACR,KAAK,SAAS;oBACZ,IAAI,GAAG,CAAC,CAAC,OAAO,EAAE,CAAC;oBACnB,MAAM;gBACR,KAAK,MAAM;oBACT,IAAI,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;oBAChB,MAAM;gBACR,KAAK,MAAM;oBACT,IAAI,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;oBAChB,MAAM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C;gBACjC,MAAM;aACP,CAAC;QACJ,CAAC;IACH,CAAC;IAED,MAAM,eAAe,GAAG,CAAC,CAAC,MAAM,CAAC;QAC/B,QAAQ;QACR,KAAK;QACL,KAAK;KACN,CAAC,CAAC;IAEH,OAAO,eAAe,CAAC;AACzB,CAAC,CAAC","sourcesContent":["import { i } from './schema.ts';\nimport { DataAttrDef, InstantSchemaDef } from './schemaTypes.ts';\n\nexport const parseSchemaFromJSON = (\n s: any,\n): InstantSchemaDef<any, any, any> => {\n // Parse entities\n const entities: Record<string, any> = {};\n\n for (const [entityName, entityInfo] of Object.entries(s.entities)) {\n const entityDef = entityInfo as any;\n const attrs: Record<string, any> = {};\n\n // Parse attributes\n for (const [attrName, attrInfo] of Object.entries(entityDef.attrs)) {\n const attrDef = attrInfo as any;\n let attr: DataAttrDef<any, any, any>;\n\n // Create the appropriate attribute type\n switch (attrDef.valueType) {\n case 'string':\n attr = i.string();\n break;\n case 'number':\n attr = i.number();\n break;\n case 'boolean':\n attr = i.boolean();\n break;\n case 'date':\n attr = i.date();\n break;\n case 'json':\n attr = i.json();\n break;\n default:\n attr = i.json();\n }\n\n // Apply modifiers\n if (!attrDef.required) {\n attr = attr.optional();\n }\n\n if (attrDef.config?.indexed) {\n attr = attr.indexed();\n }\n\n if (attrDef.config?.unique) {\n attr = attr.unique();\n }\n\n attrs[attrName] = attr;\n }\n\n entities[entityName] = i.entity(attrs);\n }\n\n // Parse links\n const links: Record<string, any> = s.links || {};\n\n // Parse rooms\n const rooms: Record<string, any> = {};\n\n if (s.rooms) {\n for (const [roomName, roomInfo] of Object.entries(s.rooms)) {\n const roomDef = roomInfo as any;\n\n // Parse presence\n const presenceAttrs: Record<string, any> = {};\n for (const [attrName, attrInfo] of Object.entries(\n roomDef.presence.attrs,\n )) {\n const attrDef = attrInfo as any;\n let attr: DataAttrDef<any, any, any>;\n\n switch (attrDef.valueType) {\n case 'string':\n attr = i.string();\n break;\n case 'number':\n attr = i.number();\n break;\n case 'boolean':\n attr = i.boolean();\n break;\n case 'date':\n attr = i.date();\n break;\n case 'json':\n attr = i.json();\n break;\n default:\n attr = i.json();\n }\n\n if (!attrDef.required) {\n attr = attr.optional();\n }\n\n if (attrDef.config?.indexed) {\n attr = attr.indexed();\n }\n\n if (attrDef.config?.unique) {\n attr = attr.unique();\n }\n\n presenceAttrs[attrName] = attr;\n }\n\n // Parse topics\n const topics: Record<string, any> = {};\n if (roomDef.topics) {\n for (const [topicName, topicInfo] of Object.entries(roomDef.topics)) {\n const topicDef = topicInfo as any;\n const topicAttrs: Record<string, any> = {};\n\n for (const [attrName, attrInfo] of Object.entries(topicDef.attrs)) {\n const attrDef = attrInfo as any;\n let attr: DataAttrDef<any, any, any>;\n\n switch (attrDef.valueType) {\n case 'string':\n attr = i.string();\n break;\n case 'number':\n attr = i.number();\n break;\n case 'boolean':\n attr = i.boolean();\n break;\n case 'date':\n attr = i.date();\n break;\n case 'json':\n attr = i.json();\n break;\n default:\n attr = i.json();\n }\n\n if (!attrDef.required) {\n attr = attr.optional();\n }\n\n if (attrDef.config?.indexed) {\n attr = attr.indexed();\n }\n\n if (attrDef.config?.unique) {\n attr = attr.unique();\n }\n\n topicAttrs[attrName] = attr;\n }\n\n topics[topicName] = i.entity(topicAttrs);\n }\n }\n\n rooms[roomName] = {\n presence: i.entity(presenceAttrs),\n topics,\n };\n }\n }\n\n const resultingSchema = i.schema({\n entities,\n links,\n rooms,\n });\n\n return resultingSchema;\n};\n"]}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
export const createInstantRouteHandler = (config: {
|
|
2
|
-
appId: string;
|
|
3
|
-
apiURI?: string;
|
|
4
|
-
}) => {
|
|
5
|
-
async function handleUserSync(req: Request) {
|
|
6
|
-
const body = await req.json();
|
|
7
|
-
if (body.user && body.user.refresh_token) {
|
|
8
|
-
return new Response('sync', {
|
|
9
|
-
headers: {
|
|
10
|
-
// 7 day expiry
|
|
11
|
-
'Set-Cookie': `instant_user=${JSON.stringify(body.user)}; Path=/; HttpOnly; Secure; SameSite=Strict; Max-Age=604800`,
|
|
12
|
-
},
|
|
13
|
-
});
|
|
14
|
-
} else {
|
|
15
|
-
return new Response('sync', {
|
|
16
|
-
headers: {
|
|
17
|
-
// remove the cookie (some browsers)
|
|
18
|
-
'Set-Cookie': `instant_user=; Path=/; HttpOnly; Secure; SameSite=Strict; Max-Age=-1`,
|
|
19
|
-
},
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return {
|
|
25
|
-
GET: async (_req: Request) => {
|
|
26
|
-
return new Response('Method not allowed', {
|
|
27
|
-
status: 405,
|
|
28
|
-
statusText: 'Method Not Allowed',
|
|
29
|
-
});
|
|
30
|
-
},
|
|
31
|
-
POST: async (req: Request) => {
|
|
32
|
-
const url = new URL(req.url);
|
|
33
|
-
const pathname = url.pathname;
|
|
34
|
-
const route = pathname.split('/')[pathname.split('/').length - 1];
|
|
35
|
-
switch (route) {
|
|
36
|
-
case 'sync-auth':
|
|
37
|
-
return await handleUserSync(req);
|
|
38
|
-
}
|
|
39
|
-
return new Response('Route not found', {
|
|
40
|
-
status: 404,
|
|
41
|
-
});
|
|
42
|
-
},
|
|
43
|
-
};
|
|
44
|
-
};
|