@dyrected/core 2.5.25 → 2.5.27
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/__tests__/app.test.d.ts +2 -0
- package/dist/__tests__/app.test.d.ts.map +1 -0
- package/dist/__tests__/app.test.js +27 -0
- package/dist/__tests__/app.test.js.map +1 -0
- package/dist/__tests__/config.test.d.ts +2 -0
- package/dist/__tests__/config.test.d.ts.map +1 -0
- package/dist/__tests__/config.test.js +34 -0
- package/dist/__tests__/config.test.js.map +1 -0
- package/dist/__tests__/deleteMany.test.d.ts +2 -0
- package/dist/__tests__/deleteMany.test.d.ts.map +1 -0
- package/dist/__tests__/deleteMany.test.js +75 -0
- package/dist/__tests__/deleteMany.test.js.map +1 -0
- package/dist/__tests__/depth.test.d.ts +2 -0
- package/dist/__tests__/depth.test.d.ts.map +1 -0
- package/dist/__tests__/depth.test.js +81 -0
- package/dist/__tests__/depth.test.js.map +1 -0
- package/dist/__tests__/dynamic-options.test.d.ts +2 -0
- package/dist/__tests__/dynamic-options.test.d.ts.map +1 -0
- package/dist/__tests__/dynamic-options.test.js +132 -0
- package/dist/__tests__/dynamic-options.test.js.map +1 -0
- package/dist/__tests__/field-inference.test-types.d.ts +24 -0
- package/dist/__tests__/field-inference.test-types.d.ts.map +1 -0
- package/dist/__tests__/field-inference.test-types.js +87 -0
- package/dist/__tests__/field-inference.test-types.js.map +1 -0
- package/dist/__tests__/hooks.test.d.ts +2 -0
- package/dist/__tests__/hooks.test.d.ts.map +1 -0
- package/dist/__tests__/hooks.test.js +320 -0
- package/dist/__tests__/hooks.test.js.map +1 -0
- package/dist/__tests__/mocks.d.ts +68 -0
- package/dist/__tests__/mocks.d.ts.map +1 -0
- package/dist/__tests__/mocks.js +151 -0
- package/dist/__tests__/mocks.js.map +1 -0
- package/dist/__tests__/router.test.d.ts +2 -0
- package/dist/__tests__/router.test.d.ts.map +1 -0
- package/dist/__tests__/router.test.js +48 -0
- package/dist/__tests__/router.test.js.map +1 -0
- package/dist/__tests__/where.test.d.ts +2 -0
- package/dist/__tests__/where.test.d.ts.map +1 -0
- package/dist/__tests__/where.test.js +97 -0
- package/dist/__tests__/where.test.js.map +1 -0
- package/dist/app-BOrsS7Tz.d.cts +1771 -0
- package/dist/app-BOrsS7Tz.d.ts +1771 -0
- package/dist/app.d.ts +21 -0
- package/dist/app.d.ts.map +1 -0
- package/dist/app.js +56 -0
- package/dist/app.js.map +1 -0
- package/dist/auth/jexl.d.ts +10 -0
- package/dist/auth/jexl.d.ts.map +1 -0
- package/dist/auth/jexl.js +22 -0
- package/dist/auth/jexl.js.map +1 -0
- package/dist/auth/password.d.ts +10 -0
- package/dist/auth/password.d.ts.map +1 -0
- package/dist/auth/password.js +28 -0
- package/dist/auth/password.js.map +1 -0
- package/dist/auth/token.d.ts +20 -0
- package/dist/auth/token.d.ts.map +1 -0
- package/dist/auth/token.js +40 -0
- package/dist/auth/token.js.map +1 -0
- package/dist/chunk-SUGK7UYL.js +311 -0
- package/dist/chunk-ZFAOBRHT.js +2709 -0
- package/dist/controllers/auth.controller.d.ts +125 -0
- package/dist/controllers/auth.controller.d.ts.map +1 -0
- package/dist/controllers/auth.controller.js +323 -0
- package/dist/controllers/auth.controller.js.map +1 -0
- package/dist/controllers/collection.controller.d.ts +88 -0
- package/dist/controllers/collection.controller.d.ts.map +1 -0
- package/dist/controllers/collection.controller.js +554 -0
- package/dist/controllers/collection.controller.js.map +1 -0
- package/dist/controllers/global.controller.d.ts +17 -0
- package/dist/controllers/global.controller.d.ts.map +1 -0
- package/dist/controllers/global.controller.js +116 -0
- package/dist/controllers/global.controller.js.map +1 -0
- package/dist/controllers/media.controller.d.ts +36 -0
- package/dist/controllers/media.controller.d.ts.map +1 -0
- package/dist/controllers/media.controller.js +155 -0
- package/dist/controllers/media.controller.js.map +1 -0
- package/dist/controllers/preview.controller.d.ts +37 -0
- package/dist/controllers/preview.controller.d.ts.map +1 -0
- package/dist/controllers/preview.controller.js +48 -0
- package/dist/controllers/preview.controller.js.map +1 -0
- package/dist/index-Bp7PDOYG.d.cts +1750 -0
- package/dist/index-Bp7PDOYG.d.ts +1750 -0
- package/dist/index-DfAmTZXk.d.cts +1749 -0
- package/dist/index-DfAmTZXk.d.ts +1749 -0
- package/dist/index.cjs +2 -2396
- package/dist/index.d.cts +2 -4
- package/dist/index.d.ts +2 -4
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -5
- package/dist/index.js.map +1 -0
- package/dist/middleware/auth.d.ts +18 -0
- package/dist/middleware/auth.d.ts.map +1 -0
- package/dist/middleware/auth.js +45 -0
- package/dist/middleware/auth.js.map +1 -0
- package/dist/router.d.ts +8 -0
- package/dist/router.d.ts.map +1 -0
- package/dist/router.js +463 -0
- package/dist/router.js.map +1 -0
- package/dist/server.cjs +103 -0
- package/dist/server.d.cts +22 -4
- package/dist/server.d.ts +22 -4
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +2429 -8
- package/dist/server.js.map +1 -0
- package/dist/services/audit.service.d.ts +23 -0
- package/dist/services/audit.service.d.ts.map +1 -0
- package/dist/services/audit.service.js +28 -0
- package/dist/services/audit.service.js.map +1 -0
- package/dist/services/defaults.service.d.ts +8 -0
- package/dist/services/defaults.service.d.ts.map +1 -0
- package/dist/services/defaults.service.js +55 -0
- package/dist/services/defaults.service.js.map +1 -0
- package/dist/services/email.service.d.ts +33 -0
- package/dist/services/email.service.d.ts.map +1 -0
- package/dist/services/email.service.js +219 -0
- package/dist/services/email.service.js.map +1 -0
- package/dist/services/media.service.d.ts +20 -0
- package/dist/services/media.service.d.ts.map +1 -0
- package/dist/services/media.service.js +49 -0
- package/dist/services/media.service.js.map +1 -0
- package/dist/services/population.service.d.ts +20 -0
- package/dist/services/population.service.d.ts.map +1 -0
- package/dist/services/population.service.js +168 -0
- package/dist/services/population.service.js.map +1 -0
- package/dist/types/index.d.ts +1749 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +3 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/config.d.ts +8 -0
- package/dist/utils/config.d.ts.map +1 -0
- package/dist/utils/config.js +153 -0
- package/dist/utils/config.js.map +1 -0
- package/dist/utils/hooks.d.ts +41 -0
- package/dist/utils/hooks.d.ts.map +1 -0
- package/dist/utils/hooks.js +169 -0
- package/dist/utils/hooks.js.map +1 -0
- package/dist/utils/openapi.d.ts +6 -0
- package/dist/utils/openapi.d.ts.map +1 -0
- package/dist/utils/openapi.js +331 -0
- package/dist/utils/openapi.js.map +1 -0
- package/dist/utils/parse-where.d.ts +63 -0
- package/dist/utils/parse-where.d.ts.map +1 -0
- package/dist/utils/parse-where.js +196 -0
- package/dist/utils/parse-where.js.map +1 -0
- package/dist/utils/readonly-db.d.ts +9 -0
- package/dist/utils/readonly-db.d.ts.map +1 -0
- package/dist/utils/readonly-db.js +21 -0
- package/dist/utils/readonly-db.js.map +1 -0
- package/dist/utils/setup-prompt.d.ts +11 -0
- package/dist/utils/setup-prompt.d.ts.map +1 -0
- package/dist/utils/setup-prompt.js +863 -0
- package/dist/utils/setup-prompt.js.map +1 -0
- package/dist/utils/swagger.d.ts +5 -0
- package/dist/utils/swagger.d.ts.map +1 -0
- package/dist/utils/swagger.js +51 -0
- package/dist/utils/swagger.js.map +1 -0
- package/dist/utils/where-sanitizer.d.ts +10 -0
- package/dist/utils/where-sanitizer.d.ts.map +1 -0
- package/dist/utils/where-sanitizer.js +63 -0
- package/dist/utils/where-sanitizer.js.map +1 -0
- package/dist/where-sanitizer-DQIWTQZW.js +50 -0
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { D as DyrectedConfig, F as Field, C as CollectionConfig, P as Prettify, I as InferDocShape, S as SystemDocFields, A as AuthDocFields, U as UploadDocFields, G as GlobalConfig } from './
|
|
2
|
-
export { a as AccessFunction, b as AdminConfig, c as AuthenticatedUser, B as BaseDocument, d as Block, e as CollectionAfterChangeHook, f as CollectionAfterDeleteHook, g as CollectionAfterReadHook, h as CollectionBeforeChangeHook, i as CollectionBeforeDeleteHook, j as CollectionBeforeReadHook, k as DatabaseAdapter, l as DynamicOptionItem, m as DynamicOptionsConfig, n as DynamicOptionsResolver, o as DynamicOptionsResolverArgs, p as
|
|
3
|
-
import 'hono/types';
|
|
4
|
-
import 'hono';
|
|
1
|
+
import { D as DyrectedConfig, F as Field, C as CollectionConfig, P as Prettify, I as InferDocShape, S as SystemDocFields, A as AuthDocFields, U as UploadDocFields, G as GlobalConfig } from './index-Bp7PDOYG.cjs';
|
|
2
|
+
export { a as AccessFunction, b as AdminConfig, c as AuthenticatedUser, B as BaseDocument, d as Block, e as CollectionAfterChangeHook, f as CollectionAfterDeleteHook, g as CollectionAfterReadHook, h as CollectionBeforeChangeHook, i as CollectionBeforeDeleteHook, j as CollectionBeforeReadHook, k as DatabaseAdapter, l as DynamicOptionItem, m as DynamicOptionsConfig, n as DynamicOptionsResolver, o as DynamicOptionsResolverArgs, p as FieldAfterReadHook, q as FieldBeforeChangeHook, r as FieldHook, s as FieldType, t as FileData, u as GlobalAfterChangeHook, v as GlobalAfterReadHook, w as GlobalBeforeChangeHook, x as GlobalBeforeReadHook, H as HookFunction, y as HookRequestContext, z as ImageService, E as PaginatedResult, R as ReadonlyDatabaseAdapter, J as StorageAdapter, K as UploadConfig } from './index-Bp7PDOYG.cjs';
|
|
5
3
|
|
|
6
4
|
interface SetupPromptConfig {
|
|
7
5
|
siteName?: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { D as DyrectedConfig, F as Field, C as CollectionConfig, P as Prettify, I as InferDocShape, S as SystemDocFields, A as AuthDocFields, U as UploadDocFields, G as GlobalConfig } from './
|
|
2
|
-
export { a as AccessFunction, b as AdminConfig, c as AuthenticatedUser, B as BaseDocument, d as Block, e as CollectionAfterChangeHook, f as CollectionAfterDeleteHook, g as CollectionAfterReadHook, h as CollectionBeforeChangeHook, i as CollectionBeforeDeleteHook, j as CollectionBeforeReadHook, k as DatabaseAdapter, l as DynamicOptionItem, m as DynamicOptionsConfig, n as DynamicOptionsResolver, o as DynamicOptionsResolverArgs, p as
|
|
3
|
-
import 'hono/types';
|
|
4
|
-
import 'hono';
|
|
1
|
+
import { D as DyrectedConfig, F as Field, C as CollectionConfig, P as Prettify, I as InferDocShape, S as SystemDocFields, A as AuthDocFields, U as UploadDocFields, G as GlobalConfig } from './index-Bp7PDOYG.js';
|
|
2
|
+
export { a as AccessFunction, b as AdminConfig, c as AuthenticatedUser, B as BaseDocument, d as Block, e as CollectionAfterChangeHook, f as CollectionAfterDeleteHook, g as CollectionAfterReadHook, h as CollectionBeforeChangeHook, i as CollectionBeforeDeleteHook, j as CollectionBeforeReadHook, k as DatabaseAdapter, l as DynamicOptionItem, m as DynamicOptionsConfig, n as DynamicOptionsResolver, o as DynamicOptionsResolverArgs, p as FieldAfterReadHook, q as FieldBeforeChangeHook, r as FieldHook, s as FieldType, t as FileData, u as GlobalAfterChangeHook, v as GlobalAfterReadHook, w as GlobalBeforeChangeHook, x as GlobalBeforeReadHook, H as HookFunction, y as HookRequestContext, z as ImageService, E as PaginatedResult, R as ReadonlyDatabaseAdapter, J as StorageAdapter, K as UploadConfig } from './index-Bp7PDOYG.js';
|
|
5
3
|
|
|
6
4
|
interface SetupPromptConfig {
|
|
7
5
|
siteName?: string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,cAAc,EAAE,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,QAAQ,EAAE,eAAe,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAExK;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,wBAAgB,gBAAgB,CAAC,KAAK,CAAC,OAAO,SAAS,KAAK,EAAE,EAC5D,MAAM,EAAE,IAAI,CAAC,gBAAgB,CAAC,QAAQ,CAAC;IAAE,EAAE,EAAE,MAAM,CAAA;CAAE,GAAG,aAAa,CAAC,OAAO,CAAC,GAAG,eAAe,GAAG,aAAa,CAAC,CAAC,EAAE,QAAQ,GAAG,MAAM,CAAC,GAAG;IAAE,MAAM,EAAE,OAAO,CAAC;IAAC,IAAI,EAAE,IAAI,CAAA;CAAE,GACvK,gBAAgB,CAAC,QAAQ,CAAC;IAAE,EAAE,EAAE,MAAM,CAAA;CAAE,GAAG,aAAa,CAAC,OAAO,CAAC,GAAG,eAAe,GAAG,aAAa,CAAC,CAAC,CAAC;AAEzG,wBAAgB,gBAAgB,CAAC,KAAK,CAAC,OAAO,SAAS,KAAK,EAAE,EAC5D,MAAM,EAAE,IAAI,CAAC,gBAAgB,CAAC,QAAQ,CAAC;IAAE,EAAE,EAAE,MAAM,CAAA;CAAE,GAAG,aAAa,CAAC,OAAO,CAAC,GAAG,eAAe,GAAG,eAAe,CAAC,CAAC,EAAE,QAAQ,GAAG,QAAQ,CAAC,GAAG;IAAE,MAAM,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,IAAI,CAAA;CAAE,GAC7K,gBAAgB,CAAC,QAAQ,CAAC;IAAE,EAAE,EAAE,MAAM,CAAA;CAAE,GAAG,aAAa,CAAC,OAAO,CAAC,GAAG,eAAe,GAAG,eAAe,CAAC,CAAC,CAAC;AAE3G,wBAAgB,gBAAgB,CAAC,KAAK,CAAC,OAAO,SAAS,KAAK,EAAE,EAC5D,MAAM,EAAE,IAAI,CAAC,gBAAgB,CAAC,QAAQ,CAAC;IAAE,EAAE,EAAE,MAAM,CAAA;CAAE,GAAG,aAAa,CAAC,OAAO,CAAC,GAAG,eAAe,CAAC,CAAC,EAAE,QAAQ,CAAC,GAAG;IAAE,MAAM,EAAE,OAAO,CAAA;CAAE,GAClI,gBAAgB,CAAC,QAAQ,CAAC;IAAE,EAAE,EAAE,MAAM,CAAA;CAAE,GAAG,aAAa,CAAC,OAAO,CAAC,GAAG,eAAe,CAAC,CAAC,CAAC;AAEzF,wBAAgB,gBAAgB,CAAC,IAAI,SAAS,MAAM,EAClD,MAAM,EAAE,gBAAgB,CAAC,IAAI,CAAC,GAC7B,gBAAgB,CAAC,IAAI,CAAC,CAAC;AAM1B;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,wBAAgB,YAAY,CAAC,KAAK,CAAC,OAAO,SAAS,KAAK,EAAE,EACxD,MAAM,EAAE,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,GAAG;IAAE,MAAM,EAAE,OAAO,CAAA;CAAE,GAC3F,YAAY,CAAC,QAAQ,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;AAElD,wBAAgB,YAAY,CAAC,IAAI,SAAS,MAAM,EAC9C,MAAM,EAAE,YAAY,CAAC,IAAI,CAAC,GACzB,YAAY,CAAC,IAAI,CAAC,CAAC;AAMtB;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,cAAc,GAAG,cAAc,CAEnE;AAED,cAAc,kBAAkB,CAAC;AACjC,cAAc,yBAAyB,CAAC;AACxC,cAAc,mBAAmB,CAAC;AAClC,cAAc,wBAAwB,CAAC;AACvC,cAAc,kBAAkB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
|
-
createDyrectedApp,
|
|
3
2
|
executeFieldAfterRead,
|
|
4
3
|
executeFieldBeforeChange,
|
|
5
4
|
normalizeConfig,
|
|
6
5
|
runCollectionHooks
|
|
7
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-SUGK7UYL.js";
|
|
8
7
|
|
|
9
8
|
// src/utils/setup-prompt.ts
|
|
10
9
|
function buildEnvironmentSection(frameworkLabel, isSelfHosted, config) {
|
|
@@ -855,10 +854,10 @@ function parseSqlWhere(where, getJsonField, placeholder = "?") {
|
|
|
855
854
|
function buildSingleOp(c, op, operand) {
|
|
856
855
|
switch (op) {
|
|
857
856
|
case "equals":
|
|
858
|
-
params.push(operand);
|
|
857
|
+
params.push(typeof operand === "boolean" ? String(operand) : operand);
|
|
859
858
|
return `${c} = ${next()}`;
|
|
860
859
|
case "not_equals":
|
|
861
|
-
params.push(operand);
|
|
860
|
+
params.push(typeof operand === "boolean" ? String(operand) : operand);
|
|
862
861
|
return `${c} != ${next()}`;
|
|
863
862
|
case "in": {
|
|
864
863
|
const vals = Array.isArray(operand) ? operand : [operand];
|
|
@@ -995,7 +994,6 @@ function defineConfig(config) {
|
|
|
995
994
|
return config;
|
|
996
995
|
}
|
|
997
996
|
export {
|
|
998
|
-
createDyrectedApp,
|
|
999
997
|
defineCollection,
|
|
1000
998
|
defineConfig,
|
|
1001
999
|
defineGlobal,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA+CA,iBAAiB;AACjB,MAAM,UAAU,gBAAgB,CAAC,MAAe;IAC9C,OAAO,MAAM,CAAC;AAChB,CAAC;AAmCD,iBAAiB;AACjB,MAAM,UAAU,YAAY,CAAC,MAAe;IAC1C,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,YAAY,CAAC,MAAsB;IACjD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,cAAc,kBAAkB,CAAC;AACjC,cAAc,yBAAyB,CAAC;AACxC,cAAc,mBAAmB,CAAC;AAClC,cAAc,wBAAwB,CAAC;AACvC,cAAc,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Context, Next } from 'hono';
|
|
2
|
+
import type { DyrectedContext } from '../app.js';
|
|
3
|
+
/**
|
|
4
|
+
* Middleware that requires a valid Bearer JWT.
|
|
5
|
+
* On success: sets `c.get('user')` to the decoded token payload.
|
|
6
|
+
* On failure: returns 401.
|
|
7
|
+
*/
|
|
8
|
+
export declare function requireAuth(): (c: Context<DyrectedContext>, next: Next) => Promise<(Response & import("hono").TypedResponse<{
|
|
9
|
+
error: true;
|
|
10
|
+
message: string;
|
|
11
|
+
}, 401, "json">) | undefined>;
|
|
12
|
+
/**
|
|
13
|
+
* Middleware that optionally decodes a Bearer JWT.
|
|
14
|
+
* Does NOT block the request if the token is missing or invalid — it just won't set `user`.
|
|
15
|
+
* Use this for routes that behave differently when authenticated.
|
|
16
|
+
*/
|
|
17
|
+
export declare function optionalAuth(): (c: Context<DyrectedContext>, next: Next) => Promise<void>;
|
|
18
|
+
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/middleware/auth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC1C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAGjD;;;;GAIG;AACH,wBAAgB,WAAW,KACX,GAAG,OAAO,CAAC,eAAe,CAAC,EAAE,MAAM,IAAI;;;8BAgBtD;AAED;;;;GAIG;AACH,wBAAgB,YAAY,KACZ,GAAG,OAAO,CAAC,eAAe,CAAC,EAAE,MAAM,IAAI,mBAetD"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { verifyCollectionToken } from '../auth/token.js';
|
|
2
|
+
/**
|
|
3
|
+
* Middleware that requires a valid Bearer JWT.
|
|
4
|
+
* On success: sets `c.get('user')` to the decoded token payload.
|
|
5
|
+
* On failure: returns 401.
|
|
6
|
+
*/
|
|
7
|
+
export function requireAuth() {
|
|
8
|
+
return async (c, next) => {
|
|
9
|
+
const authHeader = c.req.header('Authorization');
|
|
10
|
+
const token = authHeader?.replace(/^Bearer\s+/i, '');
|
|
11
|
+
if (!token) {
|
|
12
|
+
return c.json({ error: true, message: 'Authentication required.' }, 401);
|
|
13
|
+
}
|
|
14
|
+
try {
|
|
15
|
+
const user = await verifyCollectionToken(token);
|
|
16
|
+
c.set('user', user);
|
|
17
|
+
await next();
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return c.json({ error: true, message: 'Invalid or expired token.' }, 401);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Middleware that optionally decodes a Bearer JWT.
|
|
26
|
+
* Does NOT block the request if the token is missing or invalid — it just won't set `user`.
|
|
27
|
+
* Use this for routes that behave differently when authenticated.
|
|
28
|
+
*/
|
|
29
|
+
export function optionalAuth() {
|
|
30
|
+
return async (c, next) => {
|
|
31
|
+
const authHeader = c.req.header('Authorization');
|
|
32
|
+
const token = authHeader?.replace(/^Bearer\s+/i, '');
|
|
33
|
+
if (token) {
|
|
34
|
+
try {
|
|
35
|
+
const user = await verifyCollectionToken(token);
|
|
36
|
+
c.set('user', user);
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
// Invalid token — proceed without user
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
await next();
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=auth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../../src/middleware/auth.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AAEzD;;;;GAIG;AACH,MAAM,UAAU,WAAW;IACzB,OAAO,KAAK,EAAE,CAA2B,EAAE,IAAU,EAAE,EAAE;QACvD,MAAM,UAAU,GAAG,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC;QACjD,MAAM,KAAK,GAAG,UAAU,EAAE,OAAO,CAAC,aAAa,EAAE,EAAE,CAAC,CAAC;QAErD,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,OAAO,CAAC,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,0BAA0B,EAAE,EAAE,GAAG,CAAC,CAAC;QAC3E,CAAC;QAED,IAAI,CAAC;YACH,MAAM,IAAI,GAAG,MAAM,qBAAqB,CAAC,KAAK,CAAC,CAAC;YAChD,CAAC,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;YACpB,MAAM,IAAI,EAAE,CAAC;QACf,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,CAAC,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,2BAA2B,EAAE,EAAE,GAAG,CAAC,CAAC;QAC5E,CAAC;IACH,CAAC,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,YAAY;IAC1B,OAAO,KAAK,EAAE,CAA2B,EAAE,IAAU,EAAE,EAAE;QACvD,MAAM,UAAU,GAAG,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC;QACjD,MAAM,KAAK,GAAG,UAAU,EAAE,OAAO,CAAC,aAAa,EAAE,EAAE,CAAC,CAAC;QAErD,IAAI,KAAK,EAAE,CAAC;YACV,IAAI,CAAC;gBACH,MAAM,IAAI,GAAG,MAAM,qBAAqB,CAAC,KAAK,CAAC,CAAC;gBAChD,CAAC,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;YACtB,CAAC;YAAC,MAAM,CAAC;gBACP,uCAAuC;YACzC,CAAC;QACH,CAAC;QAED,MAAM,IAAI,EAAE,CAAC;IACf,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/router.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Hono } from "hono";
|
|
2
|
+
import type { DyrectedContext } from "./app.js";
|
|
3
|
+
import type { DyrectedConfig } from "./types/index.js";
|
|
4
|
+
/**
|
|
5
|
+
* Register dynamic routes based on the provided configuration.
|
|
6
|
+
*/
|
|
7
|
+
export declare function registerRoutes(app: Hono<DyrectedContext>, config: DyrectedConfig): void;
|
|
8
|
+
//# sourceMappingURL=router.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../src/router.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAChD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAiFvD;;GAEG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,IAAI,CAAC,eAAe,CAAC,EAAE,MAAM,EAAE,cAAc,QAsZhF"}
|
package/dist/router.js
ADDED
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
import { CollectionController } from "./controllers/collection.controller.js";
|
|
2
|
+
import { GlobalController } from "./controllers/global.controller.js";
|
|
3
|
+
import { MediaController } from "./controllers/media.controller.js";
|
|
4
|
+
import { AuthController } from "./controllers/auth.controller.js";
|
|
5
|
+
import { PreviewController } from "./controllers/preview.controller.js";
|
|
6
|
+
import { requireAuth, optionalAuth } from "./middleware/auth.js";
|
|
7
|
+
import { generateOpenApi } from "./utils/openapi.js";
|
|
8
|
+
import { getSwaggerHtml } from "./utils/swagger.js";
|
|
9
|
+
import { evaluateAccess } from "./auth/jexl.js";
|
|
10
|
+
/**
|
|
11
|
+
* Access gate middleware for granular permissions using Jexl.
|
|
12
|
+
*/
|
|
13
|
+
function accessGate(target, action) {
|
|
14
|
+
return async (c, next) => {
|
|
15
|
+
const user = c.get('user');
|
|
16
|
+
const accessExpr = target.access?.[action];
|
|
17
|
+
// If no access expression, default to public (true) for now to maintain parity with old behavior.
|
|
18
|
+
// However, if we want to be secure by default, we could change this to false.
|
|
19
|
+
if (accessExpr === undefined || accessExpr === null) {
|
|
20
|
+
return await next();
|
|
21
|
+
}
|
|
22
|
+
const accessArgs = { user, req: c.req, doc: null };
|
|
23
|
+
const allowed = await evaluateAccess(accessExpr, accessArgs);
|
|
24
|
+
if (!allowed) {
|
|
25
|
+
return c.json({ error: true, message: `Access denied: ${action} on ${target.slug}` }, 403);
|
|
26
|
+
}
|
|
27
|
+
await next();
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
async function checkAccess(access, accessArgs) {
|
|
31
|
+
if (access === undefined || access === null)
|
|
32
|
+
return true;
|
|
33
|
+
if (typeof access === 'function') {
|
|
34
|
+
try {
|
|
35
|
+
const result = await access(accessArgs);
|
|
36
|
+
return typeof result === 'boolean' ? result : !!result;
|
|
37
|
+
}
|
|
38
|
+
catch (err) {
|
|
39
|
+
console.error('[dyrected/core] Functional access check failed:', err);
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (typeof access === 'string' || typeof access === 'boolean') {
|
|
44
|
+
return evaluateAccess(access, accessArgs);
|
|
45
|
+
}
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
function serializeFieldForApi(f) {
|
|
49
|
+
if (!f)
|
|
50
|
+
return f;
|
|
51
|
+
const serialized = { ...f };
|
|
52
|
+
if (serialized.admin?.hooks) {
|
|
53
|
+
const hooks = { ...serialized.admin.hooks };
|
|
54
|
+
if (typeof hooks.onChange === "function") {
|
|
55
|
+
hooks.onChange = hooks.onChange.toString();
|
|
56
|
+
}
|
|
57
|
+
if (typeof hooks.options === "function") {
|
|
58
|
+
hooks.options = hooks.options.toString();
|
|
59
|
+
}
|
|
60
|
+
serialized.admin = { ...serialized.admin, hooks };
|
|
61
|
+
}
|
|
62
|
+
if (typeof serialized.options === "function" || (serialized.options && typeof serialized.options === "object" && "resolve" in serialized.options)) {
|
|
63
|
+
serialized.options = { _dynamic: true };
|
|
64
|
+
}
|
|
65
|
+
if (serialized.fields) {
|
|
66
|
+
serialized.fields = serialized.fields.map(serializeFieldForApi);
|
|
67
|
+
}
|
|
68
|
+
if (serialized.blocks) {
|
|
69
|
+
serialized.blocks = serialized.blocks.map((b) => ({
|
|
70
|
+
...b,
|
|
71
|
+
fields: b.fields?.map(serializeFieldForApi),
|
|
72
|
+
}));
|
|
73
|
+
}
|
|
74
|
+
return serialized;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Register dynamic routes based on the provided configuration.
|
|
78
|
+
*/
|
|
79
|
+
export function registerRoutes(app, config) {
|
|
80
|
+
// 1. Schema Endpoints
|
|
81
|
+
// Used by the SDK and Admin to understand the content structure
|
|
82
|
+
app.get("/api/schemas", optionalAuth(), async (c) => {
|
|
83
|
+
const siteId = c.req.header("X-Site-Id");
|
|
84
|
+
let collections = [...config.collections];
|
|
85
|
+
let globals = [...config.globals];
|
|
86
|
+
if (siteId && config.onSchemaFetch) {
|
|
87
|
+
const dynamic = await config.onSchemaFetch(siteId);
|
|
88
|
+
if (dynamic.collections)
|
|
89
|
+
collections = [...collections, ...dynamic.collections];
|
|
90
|
+
if (dynamic.globals)
|
|
91
|
+
globals = [...globals, ...dynamic.globals];
|
|
92
|
+
// Merge dynamic admin config if provided
|
|
93
|
+
if (dynamic.admin) {
|
|
94
|
+
config.admin = { ...config.admin, ...dynamic.admin };
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const user = c.get('user');
|
|
98
|
+
const accessArgs = { user, req: c.req, doc: null };
|
|
99
|
+
const serializeAccess = async (access) => {
|
|
100
|
+
if (typeof access === 'string')
|
|
101
|
+
return access;
|
|
102
|
+
if (typeof access === 'boolean')
|
|
103
|
+
return access;
|
|
104
|
+
return checkAccess(access, accessArgs);
|
|
105
|
+
};
|
|
106
|
+
const filteredCollections = await Promise.all(collections
|
|
107
|
+
.filter((col) => !siteId || col.shared || !col.siteId || col.siteId === siteId)
|
|
108
|
+
.map(async (col) => ({
|
|
109
|
+
slug: col.slug,
|
|
110
|
+
labels: col.labels,
|
|
111
|
+
access: {
|
|
112
|
+
read: await serializeAccess(col.access?.read),
|
|
113
|
+
create: await serializeAccess(col.access?.create),
|
|
114
|
+
update: await serializeAccess(col.access?.update),
|
|
115
|
+
delete: await serializeAccess(col.access?.delete),
|
|
116
|
+
},
|
|
117
|
+
fields: await Promise.all(col.fields.map(serializeFieldForApi).map(async (f) => ({
|
|
118
|
+
name: f.name,
|
|
119
|
+
type: f.type,
|
|
120
|
+
label: f.label,
|
|
121
|
+
required: f.required,
|
|
122
|
+
defaultValue: f.defaultValue,
|
|
123
|
+
options: f.options,
|
|
124
|
+
relationTo: f.relationTo,
|
|
125
|
+
hasMany: f.hasMany,
|
|
126
|
+
fields: f.fields,
|
|
127
|
+
blocks: f.blocks,
|
|
128
|
+
collection: f.collection,
|
|
129
|
+
on: f.on,
|
|
130
|
+
limit: f.limit,
|
|
131
|
+
admin: f.admin,
|
|
132
|
+
access: {
|
|
133
|
+
read: await serializeAccess(f.access?.read),
|
|
134
|
+
update: await serializeAccess(f.access?.update),
|
|
135
|
+
},
|
|
136
|
+
}))),
|
|
137
|
+
upload: !!col.upload,
|
|
138
|
+
auth: !!col.auth,
|
|
139
|
+
admin: col.admin,
|
|
140
|
+
})));
|
|
141
|
+
const filteredGlobals = await Promise.all(globals
|
|
142
|
+
.filter((glb) => !siteId || glb.shared || !glb.siteId || glb.siteId === siteId)
|
|
143
|
+
.map(async (glb) => ({
|
|
144
|
+
slug: glb.slug,
|
|
145
|
+
label: glb.label,
|
|
146
|
+
access: {
|
|
147
|
+
read: await serializeAccess(glb.access?.read),
|
|
148
|
+
update: await serializeAccess(glb.access?.update),
|
|
149
|
+
},
|
|
150
|
+
fields: await Promise.all(glb.fields.map(serializeFieldForApi).map(async (f) => ({
|
|
151
|
+
name: f.name,
|
|
152
|
+
type: f.type,
|
|
153
|
+
label: f.label,
|
|
154
|
+
required: f.required,
|
|
155
|
+
defaultValue: f.defaultValue,
|
|
156
|
+
options: f.options,
|
|
157
|
+
relationTo: f.relationTo,
|
|
158
|
+
hasMany: f.hasMany,
|
|
159
|
+
fields: f.fields,
|
|
160
|
+
blocks: f.blocks,
|
|
161
|
+
collection: f.collection,
|
|
162
|
+
on: f.on,
|
|
163
|
+
limit: f.limit,
|
|
164
|
+
admin: f.admin,
|
|
165
|
+
access: {
|
|
166
|
+
read: await serializeAccess(f.access?.read),
|
|
167
|
+
update: await serializeAccess(f.access?.update),
|
|
168
|
+
},
|
|
169
|
+
}))),
|
|
170
|
+
admin: glb.admin,
|
|
171
|
+
})));
|
|
172
|
+
return c.json({
|
|
173
|
+
collections: filteredCollections,
|
|
174
|
+
globals: filteredGlobals,
|
|
175
|
+
admin: config.admin || {},
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
app.get("/api/dyrected/options/:collection/:field", optionalAuth(), async (c) => {
|
|
179
|
+
const { collection: colSlug, field: fieldName } = c.req.param();
|
|
180
|
+
const siteId = c.req.header("X-Site-Id");
|
|
181
|
+
// Resolve collections
|
|
182
|
+
let collections = [...config.collections];
|
|
183
|
+
if (siteId && config.onSchemaFetch) {
|
|
184
|
+
const dynamic = await config.onSchemaFetch(siteId);
|
|
185
|
+
if (dynamic.collections)
|
|
186
|
+
collections = [...collections, ...dynamic.collections];
|
|
187
|
+
}
|
|
188
|
+
const user = c.get('user');
|
|
189
|
+
let collection = collections.find((col) => col.slug === colSlug);
|
|
190
|
+
let field;
|
|
191
|
+
if (collection) {
|
|
192
|
+
// Check read access on collection
|
|
193
|
+
const accessExpr = collection.access?.read;
|
|
194
|
+
if (accessExpr !== undefined && accessExpr !== null) {
|
|
195
|
+
const accessArgs = { user, req: c.req, doc: null };
|
|
196
|
+
const allowed = await checkAccess(accessExpr, accessArgs);
|
|
197
|
+
if (!allowed) {
|
|
198
|
+
return c.json({ error: true, message: `Access denied: read on ${colSlug}` }, 403);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
field = collection.fields.find((f) => f.name === fieldName);
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
let globals = [...config.globals];
|
|
205
|
+
if (siteId && config.onSchemaFetch) {
|
|
206
|
+
const dynamic = await config.onSchemaFetch(siteId);
|
|
207
|
+
if (dynamic.globals)
|
|
208
|
+
globals = [...globals, ...dynamic.globals];
|
|
209
|
+
}
|
|
210
|
+
const glb = globals.find((g) => g.slug === colSlug);
|
|
211
|
+
if (!glb) {
|
|
212
|
+
return c.json({ error: true, message: `${colSlug} not found as collection or global` }, 404);
|
|
213
|
+
}
|
|
214
|
+
// Check read access on global
|
|
215
|
+
const accessExpr = glb.access?.read;
|
|
216
|
+
if (accessExpr !== undefined && accessExpr !== null) {
|
|
217
|
+
const accessArgs = { user, req: c.req, doc: null };
|
|
218
|
+
const allowed = await checkAccess(accessExpr, accessArgs);
|
|
219
|
+
if (!allowed) {
|
|
220
|
+
return c.json({ error: true, message: `Access denied: read on global ${colSlug}` }, 403);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
field = glb.fields.find((f) => f.name === fieldName);
|
|
224
|
+
}
|
|
225
|
+
if (!field) {
|
|
226
|
+
return c.json({ error: true, message: `Field ${fieldName} not found in ${colSlug}` }, 404);
|
|
227
|
+
}
|
|
228
|
+
// Get the resolver
|
|
229
|
+
let resolver;
|
|
230
|
+
if (typeof field.options === "function") {
|
|
231
|
+
resolver = field.options;
|
|
232
|
+
}
|
|
233
|
+
else if (field.options && typeof field.options === "object" && "resolve" in field.options) {
|
|
234
|
+
resolver = field.options.resolve;
|
|
235
|
+
}
|
|
236
|
+
if (!resolver) {
|
|
237
|
+
return c.json({ error: true, message: `Field ${fieldName} in ${colSlug} is not dynamic` }, 400);
|
|
238
|
+
}
|
|
239
|
+
try {
|
|
240
|
+
const db = c.get("db") || config.db;
|
|
241
|
+
// Construct a request query helper
|
|
242
|
+
const queryParams = c.req.query();
|
|
243
|
+
const reqContext = {
|
|
244
|
+
query: queryParams,
|
|
245
|
+
headers: c.req.header(),
|
|
246
|
+
raw: c.req.raw,
|
|
247
|
+
};
|
|
248
|
+
const result = await resolver({
|
|
249
|
+
db,
|
|
250
|
+
user,
|
|
251
|
+
req: reqContext,
|
|
252
|
+
});
|
|
253
|
+
return c.json(result);
|
|
254
|
+
}
|
|
255
|
+
catch (err) {
|
|
256
|
+
console.error(`[dyrected/core] Failed to resolve dynamic options for field ${fieldName}:`, err);
|
|
257
|
+
return c.json({ error: true, message: err.message || "Failed to resolve dynamic options" }, 500);
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
app.get("/api/openapi.json", (c) => {
|
|
261
|
+
return c.json(generateOpenApi(config));
|
|
262
|
+
});
|
|
263
|
+
app.get("/api/docs", (c) => {
|
|
264
|
+
return c.html(getSwaggerHtml());
|
|
265
|
+
});
|
|
266
|
+
app.get("/api/preferences/:key", requireAuth(), async (c) => {
|
|
267
|
+
const db = config.db;
|
|
268
|
+
const user = c.get("user");
|
|
269
|
+
const key = c.req.param("key");
|
|
270
|
+
if (!db)
|
|
271
|
+
return c.json({ message: "Database not configured" }, 500);
|
|
272
|
+
if (!user?.collection || !user.sub)
|
|
273
|
+
return c.json({ error: true, message: "Authentication required." }, 401);
|
|
274
|
+
const doc = await db.findOne({ collection: user.collection, id: user.sub });
|
|
275
|
+
if (!doc)
|
|
276
|
+
return c.json({ error: true, message: "User not found." }, 404);
|
|
277
|
+
const preferences = typeof doc.__preferences === "object" && doc.__preferences !== null
|
|
278
|
+
? doc.__preferences
|
|
279
|
+
: {};
|
|
280
|
+
return c.json({ key, value: preferences[key] ?? null });
|
|
281
|
+
});
|
|
282
|
+
app.put("/api/preferences/:key", requireAuth(), async (c) => {
|
|
283
|
+
const db = config.db;
|
|
284
|
+
const user = c.get("user");
|
|
285
|
+
const key = c.req.param("key");
|
|
286
|
+
if (!db)
|
|
287
|
+
return c.json({ message: "Database not configured" }, 500);
|
|
288
|
+
if (!user?.collection || !user.sub)
|
|
289
|
+
return c.json({ error: true, message: "Authentication required." }, 401);
|
|
290
|
+
const body = await c.req.json().catch(() => ({}));
|
|
291
|
+
const doc = await db.findOne({ collection: user.collection, id: user.sub });
|
|
292
|
+
if (!doc)
|
|
293
|
+
return c.json({ error: true, message: "User not found." }, 404);
|
|
294
|
+
const preferences = typeof doc.__preferences === "object" && doc.__preferences !== null
|
|
295
|
+
? doc.__preferences
|
|
296
|
+
: {};
|
|
297
|
+
const nextPreferences = { ...preferences, [key]: body.value };
|
|
298
|
+
await db.update({
|
|
299
|
+
collection: user.collection,
|
|
300
|
+
id: user.sub,
|
|
301
|
+
data: { __preferences: nextPreferences },
|
|
302
|
+
});
|
|
303
|
+
return c.json({ key, value: body.value });
|
|
304
|
+
});
|
|
305
|
+
// Global Media Fallback (Proxies to the 'media' collection)
|
|
306
|
+
app.get("/api/media/:filename{.+$}", async (c) => {
|
|
307
|
+
const mediaController = new MediaController("media");
|
|
308
|
+
return mediaController.serve(c);
|
|
309
|
+
});
|
|
310
|
+
app.get("/media/:filename{.+$}", async (c) => {
|
|
311
|
+
const mediaController = new MediaController("media");
|
|
312
|
+
return mediaController.serve(c);
|
|
313
|
+
});
|
|
314
|
+
// 2. Media Routes (Conditional & Dynamic)
|
|
315
|
+
if (config.storage) {
|
|
316
|
+
const uploadCollections = config.collections.filter((c) => c.upload);
|
|
317
|
+
// Register routes for each upload-enabled collection
|
|
318
|
+
for (const col of uploadCollections) {
|
|
319
|
+
const mediaController = new MediaController(col.slug);
|
|
320
|
+
const prefix = `/api/collections/${col.slug}`;
|
|
321
|
+
app.get(`${prefix}/media`, accessGate(col, 'read'), (c) => mediaController.find(c));
|
|
322
|
+
app.get(`${prefix}/media/:filename{.+$}`, (c) => mediaController.serve(c));
|
|
323
|
+
app.post(`${prefix}/media`, accessGate(col, 'create'), (c) => mediaController.upload(c));
|
|
324
|
+
app.delete(`${prefix}/media/:id`, accessGate(col, 'delete'), (c) => mediaController.delete(c));
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
// 3. Auth Routes — for collections with auth: true
|
|
328
|
+
for (const collection of config.collections) {
|
|
329
|
+
if (!collection.auth)
|
|
330
|
+
continue;
|
|
331
|
+
const path = `/api/collections/${collection.slug}`;
|
|
332
|
+
const authController = new AuthController(collection);
|
|
333
|
+
app.post(`${path}/login`, (c) => authController.login(c));
|
|
334
|
+
app.post(`${path}/logout`, (c) => authController.logout(c));
|
|
335
|
+
app.get(`${path}/init`, (c) => authController.init(c));
|
|
336
|
+
app.post(`${path}/first-user`, (c) => authController.registerFirstUser(c));
|
|
337
|
+
// /me and /refresh-token require a valid token
|
|
338
|
+
app.get(`${path}/me`, requireAuth(), (c) => authController.me(c));
|
|
339
|
+
app.post(`${path}/refresh-token`, requireAuth(), (c) => authController.refreshToken(c));
|
|
340
|
+
app.post(`${path}/forgot-password`, (c) => authController.forgotPassword(c));
|
|
341
|
+
app.post(`${path}/reset-password`, (c) => authController.resetPassword(c));
|
|
342
|
+
app.post(`${path}/invite`, requireAuth(), (c) => authController.invite(c));
|
|
343
|
+
app.post(`${path}/accept-invite`, (c) => authController.acceptInvite(c));
|
|
344
|
+
}
|
|
345
|
+
// 4. Collection Routes (Static)
|
|
346
|
+
for (const collection of config.collections) {
|
|
347
|
+
const path = `/api/collections/${collection.slug}`;
|
|
348
|
+
const controller = new CollectionController(collection);
|
|
349
|
+
app.get(path, accessGate(collection, 'read'), (c) => controller.find(c));
|
|
350
|
+
app.post(path, accessGate(collection, 'create'), (c) => controller.create(c));
|
|
351
|
+
app.post(`${path}/media`, accessGate(collection, 'create'), (c) => controller.create(c));
|
|
352
|
+
// delete-many must be registered before /:id to avoid the wildcard swallowing it
|
|
353
|
+
app.delete(`${path}/delete-many`, accessGate(collection, 'delete'), (c) => controller.deleteMany(c));
|
|
354
|
+
app.get(`${path}/:id`, accessGate(collection, 'read'), (c) => controller.findOne(c));
|
|
355
|
+
app.patch(`${path}/:id`, accessGate(collection, 'update'), (c) => controller.update(c));
|
|
356
|
+
app.delete(`${path}/:id`, accessGate(collection, 'delete'), (c) => controller.delete(c));
|
|
357
|
+
app.post(`${path}/seed`, (c) => controller.seed(c));
|
|
358
|
+
// Dedicated password-change endpoint (auth collections only)
|
|
359
|
+
if (collection.auth) {
|
|
360
|
+
app.post(`${path}/:id/change-password`, requireAuth(), (c) => controller.changePassword(c));
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
// 5. Global Routes (Static)
|
|
364
|
+
for (const global of config.globals) {
|
|
365
|
+
const path = `/api/globals/${global.slug}`;
|
|
366
|
+
const controller = new GlobalController(global);
|
|
367
|
+
app.get(path, accessGate(global, 'read'), (c) => controller.get(c));
|
|
368
|
+
app.patch(path, accessGate(global, 'update'), (c) => controller.update(c));
|
|
369
|
+
app.post(`${path}/seed`, (c) => controller.seed(c));
|
|
370
|
+
}
|
|
371
|
+
// 6. Preview Routes
|
|
372
|
+
const previewController = new PreviewController();
|
|
373
|
+
app.post("/api/preview-token", requireAuth(), (c) => previewController.createToken(c));
|
|
374
|
+
app.get("/api/preview-data", (c) => previewController.getData(c));
|
|
375
|
+
// 7. Dynamic Routes (Tenant-specific)
|
|
376
|
+
// This handles collections/globals defined via sync:schema and fetched via onSchemaFetch
|
|
377
|
+
app.all("/api/collections/:slug/:id?", async (c) => {
|
|
378
|
+
const slug = c.req.param("slug");
|
|
379
|
+
const id = c.req.param("id");
|
|
380
|
+
const siteId = c.req.header("X-Site-Id") || c.get("siteId");
|
|
381
|
+
const config = c.get("config");
|
|
382
|
+
// Skip if static (already handled by routes above)
|
|
383
|
+
if (config.collections.some((col) => col.slug === slug)) {
|
|
384
|
+
return c.json({ message: "Method Not Allowed" }, 405);
|
|
385
|
+
}
|
|
386
|
+
if (config.onSchemaFetch && siteId) {
|
|
387
|
+
const dynamic = await config.onSchemaFetch(siteId);
|
|
388
|
+
let collection = dynamic.collections?.find((col) => col.slug === slug);
|
|
389
|
+
if (!collection && slug === "media") {
|
|
390
|
+
collection = {
|
|
391
|
+
slug: "media",
|
|
392
|
+
labels: { singular: "Media", plural: "Media" },
|
|
393
|
+
upload: true,
|
|
394
|
+
fields: [],
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
if (collection) {
|
|
398
|
+
// Handle auth sub-routes for dynamic auth collections
|
|
399
|
+
if (collection.auth && id) {
|
|
400
|
+
const authController = new AuthController(collection);
|
|
401
|
+
const method = c.req.method;
|
|
402
|
+
if (method === "POST" && id === "login")
|
|
403
|
+
return authController.login(c);
|
|
404
|
+
if (method === "POST" && id === "logout")
|
|
405
|
+
return authController.logout(c);
|
|
406
|
+
if (method === "GET" && id === "me")
|
|
407
|
+
return authController.me(c);
|
|
408
|
+
if (method === "POST" && id === "refresh-token")
|
|
409
|
+
return authController.refreshToken(c);
|
|
410
|
+
if (method === "POST" && id === "forgot-password")
|
|
411
|
+
return authController.forgotPassword(c);
|
|
412
|
+
if (method === "POST" && id === "reset-password")
|
|
413
|
+
return authController.resetPassword(c);
|
|
414
|
+
}
|
|
415
|
+
const controller = new CollectionController(collection);
|
|
416
|
+
const method = c.req.method;
|
|
417
|
+
if (id) {
|
|
418
|
+
if (method === "GET")
|
|
419
|
+
return controller.findOne(c);
|
|
420
|
+
if (method === "PATCH")
|
|
421
|
+
return controller.update(c);
|
|
422
|
+
if (method === "DELETE" && id === "delete-many")
|
|
423
|
+
return controller.deleteMany(c);
|
|
424
|
+
if (method === "DELETE")
|
|
425
|
+
return controller.delete(c);
|
|
426
|
+
if (method === "POST" && id === "media")
|
|
427
|
+
return controller.create(c);
|
|
428
|
+
if (method === "POST" && id === "seed")
|
|
429
|
+
return controller.seed(c);
|
|
430
|
+
}
|
|
431
|
+
else {
|
|
432
|
+
if (method === "GET")
|
|
433
|
+
return controller.find(c);
|
|
434
|
+
if (method === "POST")
|
|
435
|
+
return controller.create(c);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
return c.json({ message: `Collection "${slug}" not found` }, 404);
|
|
440
|
+
});
|
|
441
|
+
app.all("/api/globals/:slug", async (c) => {
|
|
442
|
+
const slug = c.req.param("slug");
|
|
443
|
+
const siteId = c.req.header("X-Site-Id") || c.get("siteId");
|
|
444
|
+
const config = c.get("config");
|
|
445
|
+
// Skip if static
|
|
446
|
+
if (config.globals.some((glb) => glb.slug === slug)) {
|
|
447
|
+
return c.json({ message: "Method Not Allowed" }, 405);
|
|
448
|
+
}
|
|
449
|
+
if (config.onSchemaFetch && siteId) {
|
|
450
|
+
const dynamic = await config.onSchemaFetch(siteId);
|
|
451
|
+
const global = dynamic.globals?.find((glb) => glb.slug === slug);
|
|
452
|
+
if (global) {
|
|
453
|
+
const controller = new GlobalController(global);
|
|
454
|
+
if (c.req.method === "GET")
|
|
455
|
+
return controller.get(c);
|
|
456
|
+
if (c.req.method === "PATCH")
|
|
457
|
+
return controller.update(c);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
return c.json({ message: `Global "${slug}" not found` }, 404);
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
//# sourceMappingURL=router.js.map
|