@sidekickdb/platform 0.0.0 → 2.0.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/.tshy/build.json +8 -0
- package/.tshy/commonjs.json +16 -0
- package/.tshy/esm.json +15 -0
- package/.turbo/turbo-build.log +50 -0
- package/LICENSE.md +202 -0
- package/NOTICE +27 -0
- package/README.md +444 -2
- package/__tests__/src/apiURI.test.ts +58 -0
- package/__tests__/src/backupDownload.test.ts +427 -0
- package/__tests__/src/backups.test.ts +168 -0
- package/__tests__/src/clerk.test.ts +13 -0
- package/__tests__/src/consts.test.ts +11 -0
- package/__tests__/src/migrations.test.ts +885 -0
- package/__tests__/src/perms.test.ts +102 -0
- package/__tests__/src/rename.test.ts +126 -0
- package/__tests__/src/schema.push.e2e.test.ts +186 -0
- package/__tests__/src/schema.test.ts +644 -0
- package/__tests__/src/typescript-schema.test.ts +117 -0
- package/dist/commonjs/ProgressPromise.d.ts +18 -0
- package/dist/commonjs/ProgressPromise.d.ts.map +1 -0
- package/dist/commonjs/ProgressPromise.js +77 -0
- package/dist/commonjs/ProgressPromise.js.map +1 -0
- package/dist/commonjs/api.d.ts +480 -0
- package/dist/commonjs/api.d.ts.map +1 -0
- package/dist/commonjs/api.js +1132 -0
- package/dist/commonjs/api.js.map +1 -0
- package/dist/commonjs/backupDownload.d.ts +89 -0
- package/dist/commonjs/backupDownload.d.ts.map +1 -0
- package/dist/commonjs/backupDownload.js +357 -0
- package/dist/commonjs/backupDownload.js.map +1 -0
- package/dist/commonjs/backups.d.ts +146 -0
- package/dist/commonjs/backups.d.ts.map +1 -0
- package/dist/commonjs/backups.js +248 -0
- package/dist/commonjs/backups.js.map +1 -0
- package/dist/commonjs/clerk.d.ts +2 -0
- package/dist/commonjs/clerk.d.ts.map +1 -0
- package/dist/commonjs/clerk.js +29 -0
- package/dist/commonjs/clerk.js.map +1 -0
- package/dist/commonjs/consts.d.ts +11 -0
- package/dist/commonjs/consts.d.ts.map +1 -0
- package/dist/commonjs/consts.js +17 -0
- package/dist/commonjs/consts.js.map +1 -0
- package/dist/commonjs/crypto.d.ts +4 -0
- package/dist/commonjs/crypto.d.ts.map +1 -0
- package/dist/commonjs/crypto.js +26 -0
- package/dist/commonjs/crypto.js.map +1 -0
- package/dist/commonjs/index.d.ts +18 -0
- package/dist/commonjs/index.d.ts.map +1 -0
- package/dist/commonjs/index.js +61 -0
- package/dist/commonjs/index.js.map +1 -0
- package/dist/commonjs/migrationUtils.d.ts +4 -0
- package/dist/commonjs/migrationUtils.d.ts.map +1 -0
- package/dist/commonjs/migrationUtils.js +52 -0
- package/dist/commonjs/migrationUtils.js.map +1 -0
- package/dist/commonjs/migrations.d.ts +99 -0
- package/dist/commonjs/migrations.d.ts.map +1 -0
- package/dist/commonjs/migrations.js +597 -0
- package/dist/commonjs/migrations.js.map +1 -0
- package/dist/commonjs/oauth.d.ts +98 -0
- package/dist/commonjs/oauth.d.ts.map +1 -0
- package/dist/commonjs/oauth.js +277 -0
- package/dist/commonjs/oauth.js.map +1 -0
- package/dist/commonjs/oauthCommon.d.ts +12 -0
- package/dist/commonjs/oauthCommon.d.ts.map +1 -0
- package/dist/commonjs/oauthCommon.js +26 -0
- package/dist/commonjs/oauthCommon.js.map +1 -0
- package/dist/commonjs/package.json +3 -0
- package/dist/commonjs/perms.d.ts +3 -0
- package/dist/commonjs/perms.d.ts.map +1 -0
- package/dist/commonjs/perms.js +66 -0
- package/dist/commonjs/perms.js.map +1 -0
- package/dist/commonjs/relationships.d.ts +7 -0
- package/dist/commonjs/relationships.d.ts.map +1 -0
- package/dist/commonjs/relationships.js +46 -0
- package/dist/commonjs/relationships.js.map +1 -0
- package/dist/commonjs/schema.d.ts +161 -0
- package/dist/commonjs/schema.d.ts.map +1 -0
- package/dist/commonjs/schema.js +174 -0
- package/dist/commonjs/schema.js.map +1 -0
- package/dist/commonjs/serverOAuth.d.ts +25 -0
- package/dist/commonjs/serverOAuth.d.ts.map +1 -0
- package/dist/commonjs/serverOAuth.js +72 -0
- package/dist/commonjs/serverOAuth.js.map +1 -0
- package/dist/commonjs/typescript-schema.d.ts +17 -0
- package/dist/commonjs/typescript-schema.d.ts.map +1 -0
- package/dist/commonjs/typescript-schema.js +314 -0
- package/dist/commonjs/typescript-schema.js.map +1 -0
- package/dist/commonjs/util.d.ts +13 -0
- package/dist/commonjs/util.d.ts.map +1 -0
- package/dist/commonjs/util.js +55 -0
- package/dist/commonjs/util.js.map +1 -0
- package/dist/commonjs/version.d.ts +3 -0
- package/dist/commonjs/version.d.ts.map +1 -0
- package/dist/commonjs/version.js +5 -0
- package/dist/commonjs/version.js.map +1 -0
- package/dist/esm/ProgressPromise.d.ts +18 -0
- package/dist/esm/ProgressPromise.d.ts.map +1 -0
- package/dist/esm/ProgressPromise.js +73 -0
- package/dist/esm/ProgressPromise.js.map +1 -0
- package/dist/esm/api.d.ts +480 -0
- package/dist/esm/api.d.ts.map +1 -0
- package/dist/esm/api.js +1121 -0
- package/dist/esm/api.js.map +1 -0
- package/dist/esm/backupDownload.d.ts +89 -0
- package/dist/esm/backupDownload.d.ts.map +1 -0
- package/dist/esm/backupDownload.js +354 -0
- package/dist/esm/backupDownload.js.map +1 -0
- package/dist/esm/backups.d.ts +146 -0
- package/dist/esm/backups.d.ts.map +1 -0
- package/dist/esm/backups.js +237 -0
- package/dist/esm/backups.js.map +1 -0
- package/dist/esm/clerk.d.ts +2 -0
- package/dist/esm/clerk.d.ts.map +1 -0
- package/dist/esm/clerk.js +26 -0
- package/dist/esm/clerk.js.map +1 -0
- package/dist/esm/consts.d.ts +11 -0
- package/dist/esm/consts.d.ts.map +1 -0
- package/dist/esm/consts.js +13 -0
- package/dist/esm/consts.js.map +1 -0
- package/dist/esm/crypto.d.ts +4 -0
- package/dist/esm/crypto.d.ts.map +1 -0
- package/dist/esm/crypto.js +21 -0
- package/dist/esm/crypto.js.map +1 -0
- package/dist/esm/index.d.ts +18 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +17 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/migrationUtils.d.ts +4 -0
- package/dist/esm/migrationUtils.d.ts.map +1 -0
- package/dist/esm/migrationUtils.js +47 -0
- package/dist/esm/migrationUtils.js.map +1 -0
- package/dist/esm/migrations.d.ts +99 -0
- package/dist/esm/migrations.d.ts.map +1 -0
- package/dist/esm/migrations.js +588 -0
- package/dist/esm/migrations.js.map +1 -0
- package/dist/esm/oauth.d.ts +98 -0
- package/dist/esm/oauth.d.ts.map +1 -0
- package/dist/esm/oauth.js +268 -0
- package/dist/esm/oauth.js.map +1 -0
- package/dist/esm/oauthCommon.d.ts +12 -0
- package/dist/esm/oauthCommon.d.ts.map +1 -0
- package/dist/esm/oauthCommon.js +22 -0
- package/dist/esm/oauthCommon.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/perms.d.ts +3 -0
- package/dist/esm/perms.d.ts.map +1 -0
- package/dist/esm/perms.js +62 -0
- package/dist/esm/perms.js.map +1 -0
- package/dist/esm/relationships.d.ts +7 -0
- package/dist/esm/relationships.d.ts.map +1 -0
- package/dist/esm/relationships.js +43 -0
- package/dist/esm/relationships.js.map +1 -0
- package/dist/esm/schema.d.ts +161 -0
- package/dist/esm/schema.d.ts.map +1 -0
- package/dist/esm/schema.js +158 -0
- package/dist/esm/schema.js.map +1 -0
- package/dist/esm/serverOAuth.d.ts +25 -0
- package/dist/esm/serverOAuth.d.ts.map +1 -0
- package/dist/esm/serverOAuth.js +68 -0
- package/dist/esm/serverOAuth.js.map +1 -0
- package/dist/esm/typescript-schema.d.ts +17 -0
- package/dist/esm/typescript-schema.d.ts.map +1 -0
- package/dist/esm/typescript-schema.js +310 -0
- package/dist/esm/typescript-schema.js.map +1 -0
- package/dist/esm/util.d.ts +13 -0
- package/dist/esm/util.d.ts.map +1 -0
- package/dist/esm/util.js +46 -0
- package/dist/esm/util.js.map +1 -0
- package/dist/esm/version.d.ts +3 -0
- package/dist/esm/version.d.ts.map +1 -0
- package/dist/esm/version.js +3 -0
- package/dist/esm/version.js.map +1 -0
- package/dist/standalone/index.js +13747 -0
- package/dist/standalone/index.umd.cjs +72 -0
- package/package.json +69 -3
- package/src/ProgressPromise.ts +97 -0
- package/src/api.ts +1903 -0
- package/src/backupDownload.ts +519 -0
- package/src/backups.ts +342 -0
- package/src/clerk.ts +24 -0
- package/src/consts.ts +22 -0
- package/src/crypto.ts +28 -0
- package/src/index.ts +147 -0
- package/src/migrationUtils.ts +56 -0
- package/src/migrations.ts +920 -0
- package/src/oauth.ts +390 -0
- package/src/oauthCommon.ts +38 -0
- package/src/perms.ts +81 -0
- package/src/relationships.ts +57 -0
- package/src/schema.ts +497 -0
- package/src/serverOAuth.ts +119 -0
- package/src/typescript-schema.ts +439 -0
- package/src/util.ts +77 -0
- package/src/version.ts +3 -0
- package/tsconfig.dev.json +14 -0
- package/tsconfig.json +14 -0
- package/vite.config.ts +19 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generatePermsTypescriptFile = generatePermsTypescriptFile;
|
|
4
|
+
exports.permsTypescriptFileToCode = permsTypescriptFileToCode;
|
|
5
|
+
const parser_1 = require("@babel/parser");
|
|
6
|
+
const typescript_schema_ts_1 = require("./typescript-schema.js");
|
|
7
|
+
function generatePermsTypescriptFile(permsCode, instantModuleName) {
|
|
8
|
+
const rulesTxt = permsCode && Object.keys(permsCode).length
|
|
9
|
+
? JSON.stringify(permsCode, null, 2)
|
|
10
|
+
: `{
|
|
11
|
+
/**
|
|
12
|
+
* Welcome to SidekickDB's permission system!
|
|
13
|
+
* Right now your rules are empty. To start filling them in, check out the docs:
|
|
14
|
+
* https://sidekickdb.co/docs/permissions
|
|
15
|
+
*
|
|
16
|
+
* Here's an example to give you a feel:
|
|
17
|
+
* posts: {
|
|
18
|
+
* allow: {
|
|
19
|
+
* view: "true",
|
|
20
|
+
* create: "isOwner",
|
|
21
|
+
* update: "isOwner",
|
|
22
|
+
* delete: "isOwner",
|
|
23
|
+
* },
|
|
24
|
+
* bind: {"isOwner": "auth.id != null && auth.id == data.ownerId"},
|
|
25
|
+
* },
|
|
26
|
+
*/
|
|
27
|
+
}
|
|
28
|
+
`.trim();
|
|
29
|
+
return `// Docs: https://sidekickdb.co/docs/permissions
|
|
30
|
+
|
|
31
|
+
import type { InstantRules } from "${instantModuleName ?? '@sidekickdb/core'}";
|
|
32
|
+
|
|
33
|
+
const rules = ${rulesTxt} satisfies InstantRules;
|
|
34
|
+
|
|
35
|
+
export default rules;
|
|
36
|
+
`;
|
|
37
|
+
}
|
|
38
|
+
function astToPermsCode(ast) {
|
|
39
|
+
for (const n of ast.program.body) {
|
|
40
|
+
if (n.type !== 'VariableDeclaration' ||
|
|
41
|
+
n.declarations.length !== 1 ||
|
|
42
|
+
n.declarations[0].id.type !== 'Identifier' ||
|
|
43
|
+
n.declarations[0].id.name !== 'rules') {
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const node = n.declarations[0];
|
|
47
|
+
if (!node.init || node.init.type !== 'TSSatisfiesExpression') {
|
|
48
|
+
throw new Error(`Could not extract rules`);
|
|
49
|
+
}
|
|
50
|
+
return (0, typescript_schema_ts_1.astToJson)(node.init.expression);
|
|
51
|
+
}
|
|
52
|
+
throw new Error('Could not extract rules, did not find the rules variable declaration.');
|
|
53
|
+
}
|
|
54
|
+
function permsTypescriptFileToCode(content, sourceFilename) {
|
|
55
|
+
const ast = (0, parser_1.parse)(content, {
|
|
56
|
+
sourceFilename,
|
|
57
|
+
sourceType: 'module',
|
|
58
|
+
errorRecovery: false,
|
|
59
|
+
plugins: ['typescript'],
|
|
60
|
+
});
|
|
61
|
+
if (ast.errors?.length) {
|
|
62
|
+
throw new Error(`Could not parse perms file. ${ast.errors[0].reasonCode}.`);
|
|
63
|
+
}
|
|
64
|
+
return astToPermsCode(ast);
|
|
65
|
+
}
|
|
66
|
+
//# sourceMapping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IAChE,CAAC;IAED,MAAM,IAAI,KAAK,CACb,uEAAuE,CACxE,CAAC;AACJ,CAAC;AAED,SAAgB,yBAAyB,CACvC,OAAe,EACf,cAAsB;IAEtB,MAAM,GAAG,GAAG,IAAA,cAAK,EAAC,OAAO,EAAE;QACzB,cAAc;QACd,UAAU,EAAE,QAAQ;QACpB,aAAa,EAAE,KAAK;QACpB,OAAO,EAAE,CAAC,YAAY,CAAC;KACxB,CAAC,CAAC;IACH,IAAI,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CAAC,+BAA+B,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,UAAU,GAAG,CAAC,CAAC;IAC9E,CAAC;IAED,OAAO,cAAc,CAAC,GAAG,CAAC,CAAC;AAC7B,CAAC","sourcesContent":["import { parse, type ParseResult, type File } from '@babel/parser';\nimport { astToJson } from './typescript-schema.ts';\n\nexport function generatePermsTypescriptFile(\n permsCode: null | Record<string, any>,\n instantModuleName: string,\n) {\n const rulesTxt =\n permsCode && Object.keys(permsCode).length\n ? JSON.stringify(permsCode, null, 2)\n : `{\n /**\n * Welcome to SidekickDB's permission system!\n * Right now your rules are empty. To start filling them in, check out the docs:\n * https://sidekickdb.co/docs/permissions\n *\n * Here's an example to give you a feel:\n * posts: {\n * allow: {\n * view: \"true\",\n * create: \"isOwner\",\n * update: \"isOwner\",\n * delete: \"isOwner\",\n * },\n * bind: {\"isOwner\": \"auth.id != null && auth.id == data.ownerId\"},\n * },\n */\n}\n`.trim();\n\n return `// Docs: https://sidekickdb.co/docs/permissions\n\nimport type { InstantRules } from \"${instantModuleName ?? '@sidekickdb/core'}\";\n\nconst rules = ${rulesTxt} satisfies InstantRules;\n\nexport default rules;\n`;\n}\n\nfunction astToPermsCode(ast: ParseResult<File>): Record<string, any> {\n for (const n of ast.program.body) {\n if (\n n.type !== 'VariableDeclaration' ||\n n.declarations.length !== 1 ||\n n.declarations[0].id.type !== 'Identifier' ||\n n.declarations[0].id.name !== 'rules'\n ) {\n continue;\n }\n\n const node = n.declarations[0];\n\n if (!node.init || node.init.type !== 'TSSatisfiesExpression') {\n throw new Error(`Could not extract rules`);\n }\n\n return astToJson(node.init.expression) as Record<string, any>;\n }\n\n throw new Error(\n 'Could not extract rules, did not find the rules variable declaration.',\n );\n}\n\nexport function permsTypescriptFileToCode(\n content: string,\n sourceFilename: string,\n) {\n const ast = parse(content, {\n sourceFilename,\n sourceType: 'module',\n errorRecovery: false,\n plugins: ['typescript'],\n });\n if (ast.errors?.length) {\n throw new Error(`Could not parse perms file. ${ast.errors[0].reasonCode}.`);\n }\n\n return astToPermsCode(ast);\n}\n"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type RelationshipKinds = `many-many` | 'one-one' | 'one-many' | 'many-one';
|
|
2
|
+
export declare const relationshipConstraints: Record<RelationshipKinds, {
|
|
3
|
+
cardinality: 'one' | 'many';
|
|
4
|
+
'unique?': boolean;
|
|
5
|
+
}>;
|
|
6
|
+
export declare const relationshipConstraintsInverse: Record<string, RelationshipKinds>;
|
|
7
|
+
//# sourceMappingURL=relationships.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"relationships.d.ts","sourceRoot":"","sources":["../../src/relationships.ts"],"names":[],"mappings":"AACA,MAAM,MAAM,iBAAiB,GACzB,WAAW,GACX,SAAS,GACT,UAAU,GACV,UAAU,CAAC;AAEf,eAAO,MAAM,uBAAuB,EAAE,MAAM,CAC1C,iBAAiB,EACjB;IAAE,WAAW,EAAE,KAAK,GAAG,MAAM,CAAC;IAAC,SAAS,EAAE,OAAO,CAAA;CAAE,CAuCpD,CAAC;AAEF,eAAO,MAAM,8BAA8B,EAAE,MAAM,CAAC,MAAM,EAAE,iBAAiB,CAM1E,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.relationshipConstraintsInverse = exports.relationshipConstraints = void 0;
|
|
4
|
+
exports.relationshipConstraints = {
|
|
5
|
+
/**
|
|
6
|
+
* users has_many tags
|
|
7
|
+
* tags has_many users
|
|
8
|
+
*/
|
|
9
|
+
'many-many': {
|
|
10
|
+
cardinality: 'many',
|
|
11
|
+
'unique?': false,
|
|
12
|
+
},
|
|
13
|
+
/**
|
|
14
|
+
* users has_one profile
|
|
15
|
+
* profiles has_one owner
|
|
16
|
+
*/
|
|
17
|
+
'one-one': {
|
|
18
|
+
cardinality: 'one',
|
|
19
|
+
'unique?': true,
|
|
20
|
+
},
|
|
21
|
+
/**
|
|
22
|
+
* users has_many posts
|
|
23
|
+
* posts has_one author
|
|
24
|
+
* [?users :users/posts ?posts]
|
|
25
|
+
* <---------------> unique!
|
|
26
|
+
*/
|
|
27
|
+
'many-one': {
|
|
28
|
+
cardinality: 'many',
|
|
29
|
+
'unique?': true,
|
|
30
|
+
},
|
|
31
|
+
/**
|
|
32
|
+
* posts has_one owner
|
|
33
|
+
* users has_many owned_posts
|
|
34
|
+
* [?posts :posts/user ?users]
|
|
35
|
+
* <---------------> unique!
|
|
36
|
+
*/
|
|
37
|
+
'one-many': {
|
|
38
|
+
cardinality: 'one',
|
|
39
|
+
'unique?': false,
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
exports.relationshipConstraintsInverse = Object.fromEntries(Object.entries(exports.relationshipConstraints).map(([k, v]) => [
|
|
43
|
+
`${v.cardinality}-${v['unique?']}`,
|
|
44
|
+
k,
|
|
45
|
+
]));
|
|
46
|
+
//# sourceMappingURL=relationships.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"relationships.js","sourceRoot":"","sources":["../../src/relationships.ts"],"names":[],"mappings":";;;AAOa,QAAA,uBAAuB,GAGhC;IACF;;;OAGG;IACH,WAAW,EAAE;QACX,WAAW,EAAE,MAAM;QACnB,SAAS,EAAE,KAAK;KACjB;IACD;;;OAGG;IACH,SAAS,EAAE;QACT,WAAW,EAAE,KAAK;QAClB,SAAS,EAAE,IAAI;KAChB;IACD;;;;;OAKG;IACH,UAAU,EAAE;QACV,WAAW,EAAE,MAAM;QACnB,SAAS,EAAE,IAAI;KAChB;IAED;;;;;OAKG;IACH,UAAU,EAAE;QACV,WAAW,EAAE,KAAK;QAClB,SAAS,EAAE,KAAK;KACjB;CACF,CAAC;AAEW,QAAA,8BAA8B,GACzC,MAAM,CAAC,WAAW,CAChB,MAAM,CAAC,OAAO,CAAC,+BAAuB,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC;IACtD,GAAG,CAAC,CAAC,WAAW,IAAI,CAAC,CAAC,SAAS,CAAC,EAAE;IAClC,CAAsB;CACvB,CAAC,CACH,CAAC","sourcesContent":["// Copied from www/lib/relationships.ts\nexport type RelationshipKinds =\n | `many-many`\n | 'one-one'\n | 'one-many'\n | 'many-one';\n\nexport const relationshipConstraints: Record<\n RelationshipKinds,\n { cardinality: 'one' | 'many'; 'unique?': boolean }\n> = {\n /**\n * users has_many tags\n * tags has_many users\n */\n 'many-many': {\n cardinality: 'many',\n 'unique?': false,\n },\n /**\n * users has_one profile\n * profiles has_one owner\n */\n 'one-one': {\n cardinality: 'one',\n 'unique?': true,\n },\n /**\n * users has_many posts\n * posts has_one author\n * [?users :users/posts ?posts]\n * <---------------> unique!\n */\n 'many-one': {\n cardinality: 'many',\n 'unique?': true,\n },\n\n /**\n * posts has_one owner\n * users has_many owned_posts\n * [?posts :posts/user ?users]\n * <---------------> unique!\n */\n 'one-many': {\n cardinality: 'one',\n 'unique?': false,\n },\n};\n\nexport const relationshipConstraintsInverse: Record<string, RelationshipKinds> =\n Object.fromEntries(\n Object.entries(relationshipConstraints).map(([k, v]) => [\n `${v.cardinality}-${v['unique?']}`,\n k as RelationshipKinds,\n ]),\n );\n"]}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { InstantDBAttr, InstantDBCheckedDataType, InstantDBIdent } from '@sidekickdb/core';
|
|
2
|
+
import { GenericSchemaDef } from './util.ts';
|
|
3
|
+
export type InstantAPIPlatformSchema = {
|
|
4
|
+
refs: Record<string, InstantDBAttr>;
|
|
5
|
+
blobs: Record<string, Record<string, InstantDBAttr>>;
|
|
6
|
+
};
|
|
7
|
+
export type InstantAPISchemaPlanAddAttrStep = {
|
|
8
|
+
type: 'add-attr';
|
|
9
|
+
friendlyDescription: string;
|
|
10
|
+
attr: InstantDBAttr;
|
|
11
|
+
};
|
|
12
|
+
export type InstantAPISchemaPlanDeleteAttrStep = {
|
|
13
|
+
type: 'delete-attr';
|
|
14
|
+
friendlyDescription: string;
|
|
15
|
+
attrId: string;
|
|
16
|
+
};
|
|
17
|
+
export type InstantAPISchemaPlanUpdateAttrStep = {
|
|
18
|
+
type: 'update-attr';
|
|
19
|
+
friendlyDescription: string;
|
|
20
|
+
attr: InstantDBAttr;
|
|
21
|
+
};
|
|
22
|
+
export type InstantAPISchemaPlanIndexStep = {
|
|
23
|
+
type: 'index';
|
|
24
|
+
friendlyDescription: string;
|
|
25
|
+
attrId: string;
|
|
26
|
+
forwardIdentity: InstantDBIdent;
|
|
27
|
+
};
|
|
28
|
+
export type InstantAPISchemaPlanRemoveIndexStep = {
|
|
29
|
+
type: 'remove-index';
|
|
30
|
+
friendlyDescription: string;
|
|
31
|
+
attrId: string;
|
|
32
|
+
forwardIdentity: InstantDBIdent;
|
|
33
|
+
};
|
|
34
|
+
export type InstantAPISchemaPlanUniqueStep = {
|
|
35
|
+
type: 'unique';
|
|
36
|
+
friendlyDescription: string;
|
|
37
|
+
attrId: string;
|
|
38
|
+
forwardIdentity: InstantDBIdent;
|
|
39
|
+
};
|
|
40
|
+
export type InstantAPISchemaPlanRemoveUniqueStep = {
|
|
41
|
+
type: 'remove-unique';
|
|
42
|
+
friendlyDescription: string;
|
|
43
|
+
attrId: string;
|
|
44
|
+
forwardIdentity: InstantDBIdent;
|
|
45
|
+
};
|
|
46
|
+
export type InstantAPISchemaPlanRequiredStep = {
|
|
47
|
+
type: 'required';
|
|
48
|
+
friendlyDescription: string;
|
|
49
|
+
attrId: string;
|
|
50
|
+
forwardIdentity: InstantDBIdent;
|
|
51
|
+
};
|
|
52
|
+
export type InstantAPISchemaPlanRemoveRequiredStep = {
|
|
53
|
+
type: 'remove-required';
|
|
54
|
+
friendlyDescription: string;
|
|
55
|
+
attrId: string;
|
|
56
|
+
forwardIdentity: InstantDBIdent;
|
|
57
|
+
};
|
|
58
|
+
export type InstantAPISchemaPlanCheckDataTypeStep = {
|
|
59
|
+
type: 'check-data-type';
|
|
60
|
+
friendlyDescription: string;
|
|
61
|
+
attrId: string;
|
|
62
|
+
forwardIdentity: InstantDBIdent;
|
|
63
|
+
checkedDataType: InstantDBCheckedDataType;
|
|
64
|
+
};
|
|
65
|
+
export type InstantAPISchemaPlanRemoveDataTypeStep = {
|
|
66
|
+
type: 'remove-data-type';
|
|
67
|
+
friendlyDescription: string;
|
|
68
|
+
attrId: string;
|
|
69
|
+
forwardIdentity: InstantDBIdent;
|
|
70
|
+
};
|
|
71
|
+
type InstantBackgroundSchemaBaseJob = {
|
|
72
|
+
id: string;
|
|
73
|
+
createdAt: Date;
|
|
74
|
+
updatedAt: Date;
|
|
75
|
+
status: 'completed' | 'waiting' | 'processing' | 'errored';
|
|
76
|
+
workEstimate: number | null;
|
|
77
|
+
workCompleted: number | null;
|
|
78
|
+
error?: 'invalid-triple-error' | 'invalid-attr-state-error' | 'triple-not-unique-error' | 'triple-too-large-error' | 'missing-required-error' | 'unexpected-error';
|
|
79
|
+
invalidTriplesSample?: {
|
|
80
|
+
entityId: string;
|
|
81
|
+
value: any;
|
|
82
|
+
jsonType: 'string' | 'number' | 'boolean' | 'null' | 'object' | 'array' | 'date';
|
|
83
|
+
}[];
|
|
84
|
+
};
|
|
85
|
+
type InstantBackgroundSchemaBaseJobWithInvalidTriples = InstantBackgroundSchemaBaseJob & {
|
|
86
|
+
invalidTriplesSample?: {
|
|
87
|
+
entityId: string;
|
|
88
|
+
value: any;
|
|
89
|
+
jsonType: 'string' | 'number' | 'boolean' | 'null' | 'object' | 'array' | 'date';
|
|
90
|
+
}[];
|
|
91
|
+
};
|
|
92
|
+
export interface InstantBackgroundSchemaRemoveDataTypeJob extends InstantBackgroundSchemaBaseJob {
|
|
93
|
+
type: 'remove-data-type';
|
|
94
|
+
}
|
|
95
|
+
export interface InstantBackgroundSchemaCheckDataTypeJob extends InstantBackgroundSchemaBaseJobWithInvalidTriples {
|
|
96
|
+
type: 'check-data-type';
|
|
97
|
+
checkedDataType: InstantDBCheckedDataType;
|
|
98
|
+
}
|
|
99
|
+
export interface InstantBackgroundSchemaAddIndexJob extends InstantBackgroundSchemaBaseJobWithInvalidTriples {
|
|
100
|
+
type: 'index';
|
|
101
|
+
}
|
|
102
|
+
export interface InstantBackgroundSchemaRemoveIndexJob extends InstantBackgroundSchemaBaseJob {
|
|
103
|
+
type: 'remove-index';
|
|
104
|
+
}
|
|
105
|
+
export interface InstantBackgroundSchemaAddUniqueJob extends InstantBackgroundSchemaBaseJobWithInvalidTriples {
|
|
106
|
+
type: 'unique';
|
|
107
|
+
invalidUniqueValue?: any;
|
|
108
|
+
}
|
|
109
|
+
export interface InstantBackgroundSchemaRemoveUniqueJob extends InstantBackgroundSchemaBaseJob {
|
|
110
|
+
type: 'remove-unique';
|
|
111
|
+
}
|
|
112
|
+
export interface InstantBackgroundSchemaAddRequiredJob extends InstantBackgroundSchemaBaseJobWithInvalidTriples {
|
|
113
|
+
type: 'required';
|
|
114
|
+
}
|
|
115
|
+
export interface InstantBackgroundSchemaRemoveRequiredJob extends InstantBackgroundSchemaBaseJob {
|
|
116
|
+
type: 'remove-required';
|
|
117
|
+
}
|
|
118
|
+
export type InstantAPISchemaPlanStep = InstantAPISchemaPlanAddAttrStep | InstantAPISchemaPlanDeleteAttrStep | InstantAPISchemaPlanUpdateAttrStep | InstantAPISchemaPlanIndexStep | InstantAPISchemaPlanRemoveIndexStep | InstantAPISchemaPlanUniqueStep | InstantAPISchemaPlanRemoveUniqueStep | InstantAPISchemaPlanRequiredStep | InstantAPISchemaPlanRemoveRequiredStep | InstantAPISchemaPlanCheckDataTypeStep | InstantAPISchemaPlanRemoveDataTypeStep;
|
|
119
|
+
type BackgroundJobByStep = {
|
|
120
|
+
index: InstantBackgroundSchemaAddIndexJob;
|
|
121
|
+
'remove-index': InstantBackgroundSchemaRemoveIndexJob;
|
|
122
|
+
unique: InstantBackgroundSchemaAddUniqueJob;
|
|
123
|
+
'remove-unique': InstantBackgroundSchemaRemoveUniqueJob;
|
|
124
|
+
required: InstantBackgroundSchemaAddRequiredJob;
|
|
125
|
+
'remove-required': InstantBackgroundSchemaRemoveRequiredJob;
|
|
126
|
+
'check-data-type': InstantBackgroundSchemaCheckDataTypeJob;
|
|
127
|
+
'remove-data-type': InstantBackgroundSchemaRemoveDataTypeJob;
|
|
128
|
+
};
|
|
129
|
+
export type WithBackgroundJob<P extends {
|
|
130
|
+
type: string;
|
|
131
|
+
}> = P['type'] extends keyof BackgroundJobByStep ? P & {
|
|
132
|
+
backgroundJob: BackgroundJobByStep[P['type']];
|
|
133
|
+
} : P;
|
|
134
|
+
export type InstantAPISchemaPushAddAttrStep = WithBackgroundJob<InstantAPISchemaPlanAddAttrStep>;
|
|
135
|
+
export type InstantAPISchemaPushUpdateAttrStep = WithBackgroundJob<InstantAPISchemaPlanUpdateAttrStep>;
|
|
136
|
+
export type InstantAPISchemaPushIndexStep = WithBackgroundJob<InstantAPISchemaPlanIndexStep>;
|
|
137
|
+
export type InstantAPISchemaPushRemoveIndexStep = WithBackgroundJob<InstantAPISchemaPlanRemoveIndexStep>;
|
|
138
|
+
export type InstantAPISchemaPushUniqueStep = WithBackgroundJob<InstantAPISchemaPlanUniqueStep>;
|
|
139
|
+
export type InstantAPISchemaPushRemoveUniqueStep = WithBackgroundJob<InstantAPISchemaPlanRemoveUniqueStep>;
|
|
140
|
+
export type InstantAPISchemaPushRequiredStep = WithBackgroundJob<InstantAPISchemaPlanRequiredStep>;
|
|
141
|
+
export type InstantAPISchemaPushRemoveRequiredStep = WithBackgroundJob<InstantAPISchemaPlanRemoveRequiredStep>;
|
|
142
|
+
export type InstantAPISchemaPushCheckDataTypeStep = WithBackgroundJob<InstantAPISchemaPlanCheckDataTypeStep>;
|
|
143
|
+
export type InstantAPISchemaPushRemoveDataTypeStep = WithBackgroundJob<InstantAPISchemaPlanRemoveDataTypeStep>;
|
|
144
|
+
export type InstantAPISchemaPushStep = InstantAPISchemaPushAddAttrStep | InstantAPISchemaPushUpdateAttrStep | InstantAPISchemaPushIndexStep | InstantAPISchemaPushRemoveIndexStep | InstantAPISchemaPushUniqueStep | InstantAPISchemaPushRemoveUniqueStep | InstantAPISchemaPushRequiredStep | InstantAPISchemaPushRemoveRequiredStep | InstantAPISchemaPushCheckDataTypeStep | InstantAPISchemaPushRemoveDataTypeStep;
|
|
145
|
+
export declare function identEtype(ident: InstantDBIdent): string;
|
|
146
|
+
export declare function identLabel(ident: InstantDBIdent): string;
|
|
147
|
+
export declare function identName(ident: InstantDBIdent): string;
|
|
148
|
+
export declare function attrFwdLabel(attr: InstantDBAttr): string;
|
|
149
|
+
export declare function attrFwdEtype(attr: InstantDBAttr): string;
|
|
150
|
+
export declare function attrRevLabel(attr: InstantDBAttr): string | undefined;
|
|
151
|
+
export declare function attrRevEtype(attr: InstantDBAttr): string | undefined;
|
|
152
|
+
export declare function attrFwdName(attr: InstantDBAttr): string;
|
|
153
|
+
export declare function attrRevName(attr: InstantDBAttr): string | undefined;
|
|
154
|
+
export declare function generateSchemaTypescriptFile(prevSchema: GenericSchemaDef | null | undefined, newSchema: GenericSchemaDef, instantModuleName: string): string;
|
|
155
|
+
export declare class SchemaValidationError extends Error {
|
|
156
|
+
constructor(message: string);
|
|
157
|
+
}
|
|
158
|
+
export declare function collectSystemCatalogIdentNames(currentAttrs: InstantDBAttr[]): Record<string, Set<string>>;
|
|
159
|
+
export declare const validateSchema: (schema: GenericSchemaDef, systemCatalogIdentNames: Record<string, Set<string>>) => void;
|
|
160
|
+
export {};
|
|
161
|
+
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,aAAa,EACb,wBAAwB,EACxB,cAAc,EAIf,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAKL,gBAAgB,EACjB,MAAM,WAAW,CAAC;AAEnB,MAAM,MAAM,wBAAwB,GAAG;IACrC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;IACpC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC,CAAC;CACtD,CAAC;AAEF,MAAM,MAAM,+BAA+B,GAAG;IAC5C,IAAI,EAAE,UAAU,CAAC;IACjB,mBAAmB,EAAE,MAAM,CAAC;IAC5B,IAAI,EAAE,aAAa,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,kCAAkC,GAAG;IAC/C,IAAI,EAAE,aAAa,CAAC;IACpB,mBAAmB,EAAE,MAAM,CAAC;IAC5B,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,kCAAkC,GAAG;IAC/C,IAAI,EAAE,aAAa,CAAC;IACpB,mBAAmB,EAAE,MAAM,CAAC;IAC5B,IAAI,EAAE,aAAa,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,6BAA6B,GAAG;IAC1C,IAAI,EAAE,OAAO,CAAC;IACd,mBAAmB,EAAE,MAAM,CAAC;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,eAAe,EAAE,cAAc,CAAC;CACjC,CAAC;AAEF,MAAM,MAAM,mCAAmC,GAAG;IAChD,IAAI,EAAE,cAAc,CAAC;IACrB,mBAAmB,EAAE,MAAM,CAAC;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,e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A8B,CAAC,YAAY,EAAE,aAAa,EAAE,+BAgB3E;AAED,eAAO,MAAM,cAAc,GACzB,QAAQ,gBAAgB,EACxB,yBAAyB,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,SA8DrD,CAAC"}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateSchema = exports.SchemaValidationError = void 0;
|
|
4
|
+
exports.identEtype = identEtype;
|
|
5
|
+
exports.identLabel = identLabel;
|
|
6
|
+
exports.identName = identName;
|
|
7
|
+
exports.attrFwdLabel = attrFwdLabel;
|
|
8
|
+
exports.attrFwdEtype = attrFwdEtype;
|
|
9
|
+
exports.attrRevLabel = attrRevLabel;
|
|
10
|
+
exports.attrRevEtype = attrRevEtype;
|
|
11
|
+
exports.attrFwdName = attrFwdName;
|
|
12
|
+
exports.attrRevName = attrRevName;
|
|
13
|
+
exports.generateSchemaTypescriptFile = generateSchemaTypescriptFile;
|
|
14
|
+
exports.collectSystemCatalogIdentNames = collectSystemCatalogIdentNames;
|
|
15
|
+
const util_ts_1 = require("./util.js");
|
|
16
|
+
function attrDefToCodeString([name, attr]) {
|
|
17
|
+
const type = attr.metadata.derivedType?.type || attr.valueType || 'any';
|
|
18
|
+
const unique = attr.config.unique ? '.unique()' : '';
|
|
19
|
+
const index = attr.config.indexed ? '.indexed()' : '';
|
|
20
|
+
const required = attr.required ? '' : '.optional()';
|
|
21
|
+
return `${(0, util_ts_1.formatKey)(name)}: i.${type}()${unique}${index}${required}`;
|
|
22
|
+
}
|
|
23
|
+
function entityDefToCodeStr(name, edef) {
|
|
24
|
+
const attrBlock = (0, util_ts_1.joinWithTrailingSep)((0, util_ts_1.sortedEntries)(edef.attrs).map(attrDefToCodeString), ',\n', ',');
|
|
25
|
+
// a block of code for each entity
|
|
26
|
+
return `${(0, util_ts_1.formatKey)(name)}: i.entity({${attrBlock.length ? '\n' : ''}${(0, util_ts_1.indentLines)(attrBlock, 2)}${attrBlock.length ? '\n' : ''}})`;
|
|
27
|
+
}
|
|
28
|
+
function identEtype(ident) {
|
|
29
|
+
return ident[1];
|
|
30
|
+
}
|
|
31
|
+
function identLabel(ident) {
|
|
32
|
+
return ident[2];
|
|
33
|
+
}
|
|
34
|
+
function identName(ident) {
|
|
35
|
+
return `${identEtype(ident)}.${identLabel(ident)}`;
|
|
36
|
+
}
|
|
37
|
+
function attrFwdLabel(attr) {
|
|
38
|
+
return attr['forward-identity']?.[2];
|
|
39
|
+
}
|
|
40
|
+
function attrFwdEtype(attr) {
|
|
41
|
+
return attr['forward-identity']?.[1];
|
|
42
|
+
}
|
|
43
|
+
function attrRevLabel(attr) {
|
|
44
|
+
return attr['reverse-identity']?.[2];
|
|
45
|
+
}
|
|
46
|
+
function attrRevEtype(attr) {
|
|
47
|
+
return attr['reverse-identity']?.[1];
|
|
48
|
+
}
|
|
49
|
+
function attrFwdName(attr) {
|
|
50
|
+
return `${attrFwdEtype(attr)}.${attrFwdLabel(attr)}`;
|
|
51
|
+
}
|
|
52
|
+
function attrRevName(attr) {
|
|
53
|
+
if (attr['reverse-identity']) {
|
|
54
|
+
return `${attrRevEtype(attr)}.${attrRevLabel(attr)}`;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function roomDefToCodeStr(room) {
|
|
58
|
+
let ret = '{';
|
|
59
|
+
if (room.presence) {
|
|
60
|
+
ret += `\n${(0, util_ts_1.indentLines)(entityDefToCodeStr('presence', room.presence), 4)},`;
|
|
61
|
+
}
|
|
62
|
+
if (room.topics) {
|
|
63
|
+
ret += `\n "topics": {`;
|
|
64
|
+
for (const [topicName, topicConfig] of Object.entries(room.topics)) {
|
|
65
|
+
ret += `\n${(0, util_ts_1.indentLines)(entityDefToCodeStr(topicName, topicConfig), 6)},`;
|
|
66
|
+
}
|
|
67
|
+
ret += `\n }`;
|
|
68
|
+
}
|
|
69
|
+
ret += ret === '{' ? '}' : '\n }';
|
|
70
|
+
return ret;
|
|
71
|
+
}
|
|
72
|
+
function roomsCodeStr(rooms) {
|
|
73
|
+
let ret = '{';
|
|
74
|
+
for (const [roomType, roomDef] of Object.entries(rooms)) {
|
|
75
|
+
ret += `\n ${(0, util_ts_1.formatKey)(roomType)}: ${roomDefToCodeStr(roomDef)},`;
|
|
76
|
+
}
|
|
77
|
+
ret += ret === '{' ? '}' : '\n}';
|
|
78
|
+
return ret;
|
|
79
|
+
}
|
|
80
|
+
function generateSchemaTypescriptFile(prevSchema, newSchema, instantModuleName) {
|
|
81
|
+
// entities
|
|
82
|
+
const entitiesEntriesCode = (0, util_ts_1.joinWithTrailingSep)((0, util_ts_1.sortedEntries)(newSchema.entities).map(([etype, entityDef]) => entityDefToCodeStr(etype, entityDef)), ',\n', ',');
|
|
83
|
+
const entitiesObjCode = `{\n${(0, util_ts_1.indentLines)(entitiesEntriesCode, 2)}\n}`;
|
|
84
|
+
const linksEntriesCode = JSON.stringify(newSchema.links, null, 2).trim();
|
|
85
|
+
// rooms
|
|
86
|
+
const rooms = prevSchema?.rooms || {};
|
|
87
|
+
const roomsCode = roomsCodeStr(rooms);
|
|
88
|
+
const kv = (k, v) => {
|
|
89
|
+
return (0, util_ts_1.indentLines)(`${k}: ${v}`, 2);
|
|
90
|
+
};
|
|
91
|
+
const code = `// Docs: https://sidekickdb.co/docs/modeling-data
|
|
92
|
+
|
|
93
|
+
import { i } from "${instantModuleName ?? '@sidekickdb/core'}";
|
|
94
|
+
|
|
95
|
+
const _schema = i.schema({
|
|
96
|
+
${kv('entities', entitiesObjCode)},
|
|
97
|
+
${kv('links', linksEntriesCode)},
|
|
98
|
+
${kv('rooms', roomsCode)}
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// This helps TypeScript display nicer intellisense
|
|
102
|
+
type _AppSchema = typeof _schema;
|
|
103
|
+
interface AppSchema extends _AppSchema {}
|
|
104
|
+
const schema: AppSchema = _schema;
|
|
105
|
+
|
|
106
|
+
export type { AppSchema }
|
|
107
|
+
export default schema;
|
|
108
|
+
`;
|
|
109
|
+
return code;
|
|
110
|
+
}
|
|
111
|
+
class SchemaValidationError extends Error {
|
|
112
|
+
constructor(message) {
|
|
113
|
+
super(message);
|
|
114
|
+
this.name = 'SchemaValidationError';
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
exports.SchemaValidationError = SchemaValidationError;
|
|
118
|
+
function collectSystemCatalogIdentNames(currentAttrs) {
|
|
119
|
+
const allSystemIdents = currentAttrs
|
|
120
|
+
.filter((attr) => attr.catalog === 'system')
|
|
121
|
+
.flatMap((attr) => [attr['forward-identity'], attr['reverse-identity']].filter(Boolean))
|
|
122
|
+
.filter((x) => Boolean(x));
|
|
123
|
+
let res = {};
|
|
124
|
+
for (const [_, etype, label] of allSystemIdents) {
|
|
125
|
+
res[etype] = res[etype] || new Set();
|
|
126
|
+
res[etype].add(label);
|
|
127
|
+
}
|
|
128
|
+
return res;
|
|
129
|
+
}
|
|
130
|
+
const validateSchema = (schema, systemCatalogIdentNames) => {
|
|
131
|
+
const entityNames = Object.keys(schema.entities);
|
|
132
|
+
for (const [etype, entityDef] of Object.entries(schema.entities)) {
|
|
133
|
+
if (!etype.startsWith('$'))
|
|
134
|
+
continue;
|
|
135
|
+
if (!systemCatalogIdentNames[etype]) {
|
|
136
|
+
throw new SchemaValidationError('The $ keyword is reserved for system namespaces.' +
|
|
137
|
+
`\nYou can't create '${etype}'. Perhaps call it '${etype.slice(1)}' instead?`);
|
|
138
|
+
}
|
|
139
|
+
for (const [attrName, attrDef] of Object.entries(entityDef.attrs)) {
|
|
140
|
+
if (systemCatalogIdentNames[etype].has(attrName)) {
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
if (attrDef.required) {
|
|
144
|
+
throw new SchemaValidationError(`The '${etype}' namespace is managed by the system and can't modify required constraints yet.` +
|
|
145
|
+
`\nMake sure to set ${etype}.${attrName} as optional.` +
|
|
146
|
+
`\n i.e { ${attrName}: i.${attrDef.valueType}().optional() }`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
for (const link of Object.values(schema.links)) {
|
|
151
|
+
if (systemCatalogIdentNames[link.forward.on] &&
|
|
152
|
+
!systemCatalogIdentNames[link.forward.on].has(link.forward.label) &&
|
|
153
|
+
link.forward.required) {
|
|
154
|
+
throw new SchemaValidationError(`The ${link.forward.on} namespace is managed by the system and can't modify required constraints yet.` +
|
|
155
|
+
`\nMake sure to set ${link.forward.on}${link.forward.label} as 'optional'.` +
|
|
156
|
+
`\n i.e { 'on': '${link.forward.on}', 'label': ${link.forward.label}, 'required': false }`);
|
|
157
|
+
}
|
|
158
|
+
if (link.forward.has === 'many' && link.forward.onDelete === 'cascade') {
|
|
159
|
+
throw new SchemaValidationError(`${link.forward.on}${link.forward.label} -> ${link.reverse.on}${link.reverse.label} has onDelete: "cascade" with has: "many"`);
|
|
160
|
+
}
|
|
161
|
+
if (link.reverse.has === 'many' && link.reverse.onDelete === 'cascade') {
|
|
162
|
+
throw new SchemaValidationError(`${link.forward.on}${link.forward.label} -> ${link.reverse.on}${link.reverse.label} has onDelete: "cascade" with has: "many"`);
|
|
163
|
+
}
|
|
164
|
+
const linkDisplay = `${link.forward.on}${link.forward.label} -> ${link.reverse.on}${link.reverse.label}`;
|
|
165
|
+
if (!entityNames.includes(link.forward.on)) {
|
|
166
|
+
throw new SchemaValidationError(`${linkDisplay} connects to non existing entity "${link.forward.on}".`);
|
|
167
|
+
}
|
|
168
|
+
if (!entityNames.includes(link.reverse.on)) {
|
|
169
|
+
throw new SchemaValidationError(`${linkDisplay} connects to non existing entity "${link.forward.on}".`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
exports.validateSchema = validateSchema;
|
|
174
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/schema.ts"],"names":[],"mappings":";;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AC,QAAQ,KAAK,SAAS,EAAE,CAAC;YACvE,MAAM,IAAI,qBAAqB,CAC7B,GAAG,IAAI,CAAC,OAAO,CAAC,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,OAAO,IAAI,CAAC,OAAO,CAAC,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,2CAA2C,CAC9H,CAAC;QACJ,CAAC;QAED,MAAM,WAAW,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,OAAO,IAAI,CAAC,OAAO,CAAC,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QACzG,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE,CAAC;YAC3C,MAAM,IAAI,qBAAqB,CAC7B,GAAG,WAAW,qCAAqC,IAAI,CAAC,OAAO,CAAC,EAAE,IAAI,CACvE,CAAC;QACJ,CAAC;QACD,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,EAAE,CAAC;YAC3C,MAAM,IAAI,qBAAqB,CAC7B,GAAG,WAAW,qCAAqC,IAAI,CAAC,OAAO,CAAC,EAAE,IAAI,CACvE,CAAC;QACJ,CAAC;IACH,CAAC;AACH,CAAC,CAAC;AAhEW,QAAA,cAAc,kBAgEzB","sourcesContent":["import {\n AttrsDefs,\n CardinalityKind,\n DataAttrDef,\n EntityDef,\n InstantDBAttr,\n InstantDBCheckedDataType,\n InstantDBIdent,\n InstantSchemaDef,\n LinkAttrDef,\n RoomsDef,\n} from '@sidekickdb/core';\n\nimport {\n indentLines,\n joinWithTrailingSep,\n sortedEntries,\n formatKey,\n GenericSchemaDef,\n} from './util.ts';\n\nexport type InstantAPIPlatformSchema = {\n refs: Record<string, InstantDBAttr>;\n blobs: Record<string, Record<string, InstantDBAttr>>;\n};\n\nexport type InstantAPISchemaPlanAddAttrStep = {\n type: 'add-attr';\n friendlyDescription: string;\n attr: InstantDBAttr;\n};\n\nexport type InstantAPISchemaPlanDeleteAttrStep = {\n type: 'delete-attr';\n friendlyDescription: string;\n attrId: string;\n};\n\nexport type InstantAPISchemaPlanUpdateAttrStep = {\n type: 'update-attr';\n friendlyDescription: string;\n attr: InstantDBAttr;\n};\n\nexport type InstantAPISchemaPlanIndexStep = {\n type: 'index';\n friendlyDescription: string;\n attrId: string;\n forwardIdentity: InstantDBIdent;\n};\n\nexport type InstantAPISchemaPlanRemoveIndexStep = {\n type: 'remove-index';\n friendlyDescription: string;\n attrId: string;\n forwardIdentity: InstantDBIdent;\n};\n\nexport type InstantAPISchemaPlanUniqueStep = {\n type: 'unique';\n friendlyDescription: string;\n attrId: string;\n forwardIdentity: InstantDBIdent;\n};\n\nexport type InstantAPISchemaPlanRemoveUniqueStep = {\n type: 'remove-unique';\n friendlyDescription: string;\n attrId: string;\n forwardIdentity: InstantDBIdent;\n};\n\nexport type InstantAPISchemaPlanRequiredStep = {\n type: 'required';\n friendlyDescription: string;\n attrId: string;\n forwardIdentity: InstantDBIdent;\n};\n\nexport type InstantAPISchemaPlanRemoveRequiredStep = {\n type: 'remove-required';\n friendlyDescription: string;\n attrId: string;\n forwardIdentity: InstantDBIdent;\n};\n\nexport type InstantAPISchemaPlanCheckDataTypeStep = {\n type: 'check-data-type';\n friendlyDescription: string;\n attrId: string;\n forwardIdentity: InstantDBIdent;\n checkedDataType: InstantDBCheckedDataType;\n};\n\nexport type InstantAPISchemaPlanRemoveDataTypeStep = {\n type: 'remove-data-type';\n friendlyDescription: string;\n attrId: string;\n forwardIdentity: InstantDBIdent;\n};\n\ntype InstantBackgroundSchemaBaseJob = {\n id: string;\n createdAt: Date;\n updatedAt: Date;\n status: 'completed' | 'waiting' | 'processing' | 'errored';\n workEstimate: number | null;\n workCompleted: number | null;\n error?:\n | 'invalid-triple-error'\n | 'invalid-attr-state-error'\n | 'triple-not-unique-error'\n | 'triple-too-large-error'\n | 'missing-required-error'\n | 'unexpected-error';\n invalidTriplesSample?: {\n entityId: string;\n value: any;\n jsonType:\n | 'string'\n | 'number'\n | 'boolean'\n | 'null'\n | 'object'\n | 'array'\n | 'date';\n }[];\n};\n\ntype InstantBackgroundSchemaBaseJobWithInvalidTriples =\n InstantBackgroundSchemaBaseJob & {\n invalidTriplesSample?: {\n entityId: string;\n value: any;\n jsonType:\n | 'string'\n | 'number'\n | 'boolean'\n | 'null'\n | 'object'\n | 'array'\n | 'date';\n }[];\n };\n\nexport interface InstantBackgroundSchemaRemoveDataTypeJob\n extends InstantBackgroundSchemaBaseJob {\n type: 'remove-data-type';\n}\n\nexport interface InstantBackgroundSchemaCheckDataTypeJob\n extends InstantBackgroundSchemaBaseJobWithInvalidTriples {\n type: 'check-data-type';\n checkedDataType: InstantDBCheckedDataType;\n}\n\nexport interface InstantBackgroundSchemaAddIndexJob\n extends InstantBackgroundSchemaBaseJobWithInvalidTriples {\n type: 'index';\n}\n\nexport interface InstantBackgroundSchemaRemoveIndexJob\n extends InstantBackgroundSchemaBaseJob {\n type: 'remove-index';\n}\n\nexport interface InstantBackgroundSchemaAddUniqueJob\n extends InstantBackgroundSchemaBaseJobWithInvalidTriples {\n type: 'unique';\n invalidUniqueValue?: any;\n}\n\nexport interface InstantBackgroundSchemaRemoveUniqueJob\n extends InstantBackgroundSchemaBaseJob {\n type: 'remove-unique';\n}\n\nexport interface InstantBackgroundSchemaAddRequiredJob\n extends InstantBackgroundSchemaBaseJobWithInvalidTriples {\n type: 'required';\n}\n\nexport interface InstantBackgroundSchemaRemoveRequiredJob\n extends InstantBackgroundSchemaBaseJob {\n type: 'remove-required';\n}\n\nexport type InstantAPISchemaPlanStep =\n | InstantAPISchemaPlanAddAttrStep\n | InstantAPISchemaPlanDeleteAttrStep\n | InstantAPISchemaPlanUpdateAttrStep\n | InstantAPISchemaPlanIndexStep\n | InstantAPISchemaPlanRemoveIndexStep\n | InstantAPISchemaPlanUniqueStep\n | InstantAPISchemaPlanRemoveUniqueStep\n | InstantAPISchemaPlanRequiredStep\n | InstantAPISchemaPlanRemoveRequiredStep\n | InstantAPISchemaPlanCheckDataTypeStep\n | InstantAPISchemaPlanRemoveDataTypeStep;\n\ntype BackgroundJobByStep = {\n index: InstantBackgroundSchemaAddIndexJob;\n 'remove-index': InstantBackgroundSchemaRemoveIndexJob;\n unique: InstantBackgroundSchemaAddUniqueJob;\n 'remove-unique': InstantBackgroundSchemaRemoveUniqueJob;\n required: InstantBackgroundSchemaAddRequiredJob;\n 'remove-required': InstantBackgroundSchemaRemoveRequiredJob;\n 'check-data-type': InstantBackgroundSchemaCheckDataTypeJob;\n 'remove-data-type': InstantBackgroundSchemaRemoveDataTypeJob;\n};\n\n// Adds the proper flavor of the backgroundJob type to the steps\n// that run in the background\nexport type WithBackgroundJob<P extends { type: string }> =\n P['type'] extends keyof BackgroundJobByStep\n ? P & { backgroundJob: BackgroundJobByStep[P['type']] }\n : P;\n\nexport type InstantAPISchemaPushAddAttrStep =\n WithBackgroundJob<InstantAPISchemaPlanAddAttrStep>;\n\nexport type InstantAPISchemaPushUpdateAttrStep =\n WithBackgroundJob<InstantAPISchemaPlanUpdateAttrStep>;\n\nexport type InstantAPISchemaPushIndexStep =\n WithBackgroundJob<InstantAPISchemaPlanIndexStep>;\n\nexport type InstantAPISchemaPushRemoveIndexStep =\n WithBackgroundJob<InstantAPISchemaPlanRemoveIndexStep>;\n\nexport type InstantAPISchemaPushUniqueStep =\n WithBackgroundJob<InstantAPISchemaPlanUniqueStep>;\n\nexport type InstantAPISchemaPushRemoveUniqueStep =\n WithBackgroundJob<InstantAPISchemaPlanRemoveUniqueStep>;\n\nexport type InstantAPISchemaPushRequiredStep =\n WithBackgroundJob<InstantAPISchemaPlanRequiredStep>;\n\nexport type InstantAPISchemaPushRemoveRequiredStep =\n WithBackgroundJob<InstantAPISchemaPlanRemoveRequiredStep>;\n\nexport type InstantAPISchemaPushCheckDataTypeStep =\n WithBackgroundJob<InstantAPISchemaPlanCheckDataTypeStep>;\n\nexport type InstantAPISchemaPushRemoveDataTypeStep =\n WithBackgroundJob<InstantAPISchemaPlanRemoveDataTypeStep>;\n\nexport type InstantAPISchemaPushStep =\n | InstantAPISchemaPushAddAttrStep\n | InstantAPISchemaPushUpdateAttrStep\n | InstantAPISchemaPushIndexStep\n | InstantAPISchemaPushRemoveIndexStep\n | InstantAPISchemaPushUniqueStep\n | InstantAPISchemaPushRemoveUniqueStep\n | InstantAPISchemaPushRequiredStep\n | InstantAPISchemaPushRemoveRequiredStep\n | InstantAPISchemaPushCheckDataTypeStep\n | InstantAPISchemaPushRemoveDataTypeStep;\n\nfunction attrDefToCodeString([name, attr]: [\n string,\n DataAttrDef<string, boolean, boolean>,\n]) {\n const type =\n (attr.metadata.derivedType as any)?.type || attr.valueType || 'any';\n const unique = attr.config.unique ? '.unique()' : '';\n const index = attr.config.indexed ? '.indexed()' : '';\n const required = attr.required ? '' : '.optional()';\n return `${formatKey(name)}: i.${type}()${unique}${index}${required}`;\n}\n\nfunction entityDefToCodeStr(\n name: string,\n edef: EntityDef<\n AttrsDefs,\n Record<string, LinkAttrDef<CardinalityKind, string>>,\n any\n >,\n) {\n const attrBlock = joinWithTrailingSep(\n sortedEntries(edef.attrs).map(attrDefToCodeString),\n ',\\n',\n ',',\n );\n\n // a block of code for each entity\n return `${formatKey(name)}: i.entity({${attrBlock.length ? '\\n' : ''}${indentLines(attrBlock, 2)}${attrBlock.length ? '\\n' : ''}})`;\n}\n\nexport function identEtype(ident: InstantDBIdent) {\n return ident[1];\n}\n\nexport function identLabel(ident: InstantDBIdent) {\n return ident[2];\n}\n\nexport function identName(ident: InstantDBIdent) {\n return `${identEtype(ident)}.${identLabel(ident)}`;\n}\n\nexport function attrFwdLabel(attr: InstantDBAttr) {\n return attr['forward-identity']?.[2];\n}\n\nexport function attrFwdEtype(attr: InstantDBAttr) {\n return attr['forward-identity']?.[1];\n}\n\nexport function attrRevLabel(attr: InstantDBAttr) {\n return attr['reverse-identity']?.[2];\n}\n\nexport function attrRevEtype(attr: InstantDBAttr) {\n return attr['reverse-identity']?.[1];\n}\n\nexport function attrFwdName(attr: InstantDBAttr) {\n return `${attrFwdEtype(attr)}.${attrFwdLabel(attr)}`;\n}\n\nexport function attrRevName(attr: InstantDBAttr) {\n if (attr['reverse-identity']) {\n return `${attrRevEtype(attr)}.${attrRevLabel(attr)}`;\n }\n}\n\nfunction roomDefToCodeStr(room: RoomsDef[string]) {\n let ret = '{';\n\n if (room.presence) {\n ret += `\\n${indentLines(entityDefToCodeStr('presence', room.presence), 4)},`;\n }\n\n if (room.topics) {\n ret += `\\n \"topics\": {`;\n\n for (const [topicName, topicConfig] of Object.entries(room.topics)) {\n ret += `\\n${indentLines(entityDefToCodeStr(topicName, topicConfig), 6)},`;\n }\n ret += `\\n }`;\n }\n\n ret += ret === '{' ? '}' : '\\n }';\n\n return ret;\n}\n\nfunction roomsCodeStr(rooms: RoomsDef) {\n let ret = '{';\n\n for (const [roomType, roomDef] of Object.entries(rooms)) {\n ret += `\\n ${formatKey(roomType)}: ${roomDefToCodeStr(roomDef)},`;\n }\n ret += ret === '{' ? '}' : '\\n}';\n\n return ret;\n}\n\nexport function generateSchemaTypescriptFile(\n prevSchema: GenericSchemaDef | null | undefined,\n newSchema: GenericSchemaDef,\n instantModuleName: string,\n): string {\n // entities\n const entitiesEntriesCode = joinWithTrailingSep(\n sortedEntries(newSchema.entities).map(([etype, entityDef]) =>\n entityDefToCodeStr(etype, entityDef),\n ),\n ',\\n',\n ',',\n );\n\n const entitiesObjCode = `{\\n${indentLines(entitiesEntriesCode, 2)}\\n}`;\n\n const linksEntriesCode = JSON.stringify(newSchema.links, null, 2).trim();\n\n // rooms\n const rooms = prevSchema?.rooms || {};\n const roomsCode = roomsCodeStr(rooms);\n const kv = (k: string, v: string) => {\n return indentLines(`${k}: ${v}`, 2);\n };\n\n const code = `// Docs: https://sidekickdb.co/docs/modeling-data\n\nimport { i } from \"${instantModuleName ?? '@sidekickdb/core'}\";\n\nconst _schema = i.schema({\n${kv('entities', entitiesObjCode)},\n${kv('links', linksEntriesCode)},\n${kv('rooms', roomsCode)}\n});\n\n// This helps TypeScript display nicer intellisense\ntype _AppSchema = typeof _schema;\ninterface AppSchema extends _AppSchema {}\nconst schema: AppSchema = _schema;\n\nexport type { AppSchema }\nexport default schema;\n`;\n\n return code;\n}\n\nexport class SchemaValidationError extends Error {\n constructor(message: string) {\n super(message);\n this.name = 'SchemaValidationError';\n }\n}\n\nexport function collectSystemCatalogIdentNames(currentAttrs: InstantDBAttr[]) {\n const allSystemIdents = currentAttrs\n .filter((attr) => attr.catalog === 'system')\n .flatMap((attr) =>\n [attr['forward-identity'], attr['reverse-identity']].filter(Boolean),\n )\n .filter((x): x is NonNullable<typeof x> => Boolean(x));\n\n let res: Record<string, Set<string>> = {};\n\n for (const [_, etype, label] of allSystemIdents) {\n res[etype] = res[etype] || new Set();\n res[etype].add(label);\n }\n\n return res;\n}\n\nexport const validateSchema = (\n schema: GenericSchemaDef,\n systemCatalogIdentNames: Record<string, Set<string>>,\n) => {\n const entityNames = Object.keys(schema.entities);\n\n for (const [etype, entityDef] of Object.entries(schema.entities)) {\n if (!etype.startsWith('$')) continue;\n if (!systemCatalogIdentNames[etype]) {\n throw new SchemaValidationError(\n 'The $ keyword is reserved for system namespaces.' +\n `\\nYou can't create '${etype}'. Perhaps call it '${etype.slice(1)}' instead?`,\n );\n }\n for (const [attrName, attrDef] of Object.entries(entityDef.attrs)) {\n if (systemCatalogIdentNames[etype].has(attrName)) {\n continue;\n }\n if (attrDef.required) {\n throw new SchemaValidationError(\n `The '${etype}' namespace is managed by the system and can't modify required constraints yet.` +\n `\\nMake sure to set ${etype}.${attrName} as optional.` +\n `\\n i.e { ${attrName}: i.${attrDef.valueType}().optional() }`,\n );\n }\n }\n }\n\n for (const link of Object.values(schema.links)) {\n if (\n systemCatalogIdentNames[link.forward.on] &&\n !systemCatalogIdentNames[link.forward.on].has(link.forward.label) &&\n link.forward.required\n ) {\n throw new SchemaValidationError(\n `The ${link.forward.on} namespace is managed by the system and can't modify required constraints yet.` +\n `\\nMake sure to set ${link.forward.on}${link.forward.label} as 'optional'.` +\n `\\n i.e { 'on': '${link.forward.on}', 'label': ${link.forward.label}, 'required': false }`,\n );\n }\n\n if (link.forward.has === 'many' && link.forward.onDelete === 'cascade') {\n throw new SchemaValidationError(\n `${link.forward.on}${link.forward.label} -> ${link.reverse.on}${link.reverse.label} has onDelete: \"cascade\" with has: \"many\"`,\n );\n }\n if (link.reverse.has === 'many' && link.reverse.onDelete === 'cascade') {\n throw new SchemaValidationError(\n `${link.forward.on}${link.forward.label} -> ${link.reverse.on}${link.reverse.label} has onDelete: \"cascade\" with has: \"many\"`,\n );\n }\n\n const linkDisplay = `${link.forward.on}${link.forward.label} -> ${link.reverse.on}${link.reverse.label}`;\n if (!entityNames.includes(link.forward.on)) {\n throw new SchemaValidationError(\n `${linkDisplay} connects to non existing entity \"${link.forward.on}\".`,\n );\n }\n if (!entityNames.includes(link.reverse.on)) {\n throw new SchemaValidationError(\n `${linkDisplay} connects to non existing entity \"${link.forward.on}\".`,\n );\n }\n }\n};\n"]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export declare function exchangeCodeForToken({ code, clientId, clientSecret, redirectUri, apiURI: apiURIOpt, }: {
|
|
2
|
+
code: string;
|
|
3
|
+
clientId: string;
|
|
4
|
+
clientSecret: string;
|
|
5
|
+
redirectUri: string;
|
|
6
|
+
apiURI?: string;
|
|
7
|
+
}): Promise<{
|
|
8
|
+
accessToken: string;
|
|
9
|
+
refreshToken: string;
|
|
10
|
+
expiresAt: Date;
|
|
11
|
+
scopes: string;
|
|
12
|
+
tokenType: 'Bearer';
|
|
13
|
+
}>;
|
|
14
|
+
export declare function exchangeRefreshToken({ clientId, clientSecret, refreshToken, apiURI: apiURIOpt, }: {
|
|
15
|
+
clientId: string;
|
|
16
|
+
clientSecret: string;
|
|
17
|
+
refreshToken: string;
|
|
18
|
+
apiURI?: string;
|
|
19
|
+
}): Promise<{
|
|
20
|
+
accessToken: string;
|
|
21
|
+
expiresAt: Date;
|
|
22
|
+
scopes: string;
|
|
23
|
+
tokenType: 'Bearer';
|
|
24
|
+
}>;
|
|
25
|
+
//# sourceMappingURL=serverOAuth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serverOAuth.d.ts","sourceRoot":"","sources":["../../src/serverOAuth.ts"],"names":[],"mappings":"AAGA,wBAAsB,oBAAoB,CAAC,EACzC,IAAI,EACJ,QAAQ,EACR,YAAY,EACZ,WAAW,EACX,MAAM,EAAE,SAAS,GAClB,EAAE;IACD,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,GAAG,OAAO,CAAC;IACV,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,IAAI,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,QAAQ,CAAC;CACrB,CAAC,CAoDD;AAED,wBAAsB,oBAAoB,CAAC,EACzC,QAAQ,EACR,YAAY,EACZ,YAAY,EACZ,MAAM,EAAE,SAAS,GAClB,EAAE;IACD,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE,MAAM,CAAC;IACrB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,GAAG,OAAO,CAAC;IACV,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,IAAI,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,QAAQ,CAAC;CACrB,CAAC,CA4BD"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.exchangeCodeForToken = exchangeCodeForToken;
|
|
4
|
+
exports.exchangeRefreshToken = exchangeRefreshToken;
|
|
5
|
+
const core_1 = require("@sidekickdb/core");
|
|
6
|
+
const oauthCommon_ts_1 = require("./oauthCommon.js");
|
|
7
|
+
async function exchangeCodeForToken({ code, clientId, clientSecret, redirectUri, apiURI: apiURIOpt, }) {
|
|
8
|
+
const apiURI = (0, core_1.requireApiURI)(apiURIOpt, {
|
|
9
|
+
target: 'exchangeCodeForToken()',
|
|
10
|
+
});
|
|
11
|
+
const res = await fetch(`${apiURI}/platform/oauth/token`, {
|
|
12
|
+
method: 'POST',
|
|
13
|
+
headers: { 'Content-type': 'application/json' },
|
|
14
|
+
body: JSON.stringify({
|
|
15
|
+
grant_type: 'authorization_code',
|
|
16
|
+
code,
|
|
17
|
+
redirect_uri: redirectUri,
|
|
18
|
+
client_id: clientId,
|
|
19
|
+
client_secret: clientSecret,
|
|
20
|
+
}),
|
|
21
|
+
});
|
|
22
|
+
if (!res.ok) {
|
|
23
|
+
const text = await res.text();
|
|
24
|
+
let error;
|
|
25
|
+
try {
|
|
26
|
+
const json = JSON.parse(text);
|
|
27
|
+
error = {
|
|
28
|
+
error: json.error,
|
|
29
|
+
errorDescription: json.error_description,
|
|
30
|
+
message: `OAuth error: ${json.error || 'server_error'}`,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
catch (e) {
|
|
34
|
+
error = {
|
|
35
|
+
error: 'server_error',
|
|
36
|
+
message: 'OAuth error: server_error',
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
throw new oauthCommon_ts_1.InstantOAuthError(error);
|
|
40
|
+
}
|
|
41
|
+
const json = (await res.json());
|
|
42
|
+
return {
|
|
43
|
+
accessToken: json.access_token,
|
|
44
|
+
expiresAt: new Date(Date.now() + (json.expires_in - 30) * 1000),
|
|
45
|
+
refreshToken: json.refresh_token,
|
|
46
|
+
scopes: json.scopes,
|
|
47
|
+
tokenType: json.token_type,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
async function exchangeRefreshToken({ clientId, clientSecret, refreshToken, apiURI: apiURIOpt, }) {
|
|
51
|
+
const apiURI = (0, core_1.requireApiURI)(apiURIOpt, {
|
|
52
|
+
target: 'exchangeRefreshToken()',
|
|
53
|
+
});
|
|
54
|
+
const res = await fetch(`${apiURI}/platform/oauth/token`, {
|
|
55
|
+
method: 'POST',
|
|
56
|
+
headers: { 'Content-Type': 'application/json' },
|
|
57
|
+
body: JSON.stringify({
|
|
58
|
+
grant_type: 'refresh_token',
|
|
59
|
+
refresh_token: refreshToken,
|
|
60
|
+
client_id: clientId,
|
|
61
|
+
client_secret: clientSecret,
|
|
62
|
+
}),
|
|
63
|
+
});
|
|
64
|
+
const json = (await res.json());
|
|
65
|
+
return {
|
|
66
|
+
accessToken: json.access_token,
|
|
67
|
+
expiresAt: new Date((json.expires_in - 30) * 1000),
|
|
68
|
+
scopes: json.scopes,
|
|
69
|
+
tokenType: json.token_type,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=serverOAuth.js.map
|