-
-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add "loadPath" options to runtime API and server adapter options
- Loading branch information
Showing
32 changed files
with
482 additions
and
244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export * from './constants'; | ||
export * from './enhancements'; | ||
export * from './error'; | ||
export * from './loader'; | ||
export * from './types'; | ||
export * from './validation'; | ||
export * from './version'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
import path from 'path'; | ||
import { DEFAULT_RUNTIME_LOAD_PATH } from './constants'; | ||
import { ModelMeta, PolicyDef, ZodSchemas } from './enhancements'; | ||
|
||
/** | ||
* Load model metadata. | ||
* | ||
* @param loadPath The path to load model metadata from. If not provided, | ||
* will use default load path. | ||
*/ | ||
export function getDefaultModelMeta(loadPath: string | undefined): ModelMeta { | ||
loadPath = loadPath ? path.resolve(loadPath, 'model-meta') : `${DEFAULT_RUNTIME_LOAD_PATH}/model-meta`; | ||
try { | ||
// normal load | ||
return require(loadPath).default; | ||
} catch { | ||
if (process.env.ZENSTACK_TEST === '1' && !path.isAbsolute(loadPath)) { | ||
try { | ||
// special handling for running as tests, try resolving relative to CWD | ||
return require(path.join(process.cwd(), 'node_modules', loadPath, 'model-meta')).default; | ||
} catch { | ||
throw new Error('Model meta cannot be loaded. Please make sure "zenstack generate" has been run.'); | ||
} | ||
} | ||
throw new Error('Model meta cannot be loaded. Please make sure "zenstack generate" has been run.'); | ||
} | ||
} | ||
|
||
/** | ||
* Load access policies. | ||
* | ||
* @param loadPath The path to load access policies from. If not provided, | ||
* will use default load path. | ||
*/ | ||
export function getDefaultPolicy(loadPath: string | undefined): PolicyDef { | ||
loadPath = loadPath ? path.resolve(loadPath, 'policy') : `${DEFAULT_RUNTIME_LOAD_PATH}/policy`; | ||
try { | ||
return require(loadPath).default; | ||
} catch { | ||
if (process.env.ZENSTACK_TEST === '1' && !path.isAbsolute(loadPath)) { | ||
try { | ||
// special handling for running as tests, try resolving relative to CWD | ||
return require(path.join(process.cwd(), 'node_modules', loadPath, 'policy')).default; | ||
} catch { | ||
throw new Error( | ||
'Policy definition cannot be loaded from default location. Please make sure "zenstack generate" has been run.' | ||
); | ||
} | ||
} | ||
throw new Error( | ||
'Policy definition cannot be loaded from default location. Please make sure "zenstack generate" has been run.' | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Load zod schemas. | ||
* | ||
* @param loadPath The path to load zod schemas from. If not provided, | ||
* will use default load path. | ||
*/ | ||
export function getDefaultZodSchemas(loadPath: string | undefined): ZodSchemas | undefined { | ||
loadPath = loadPath ? path.resolve(loadPath, 'zod') : `${DEFAULT_RUNTIME_LOAD_PATH}/zod`; | ||
try { | ||
return require(loadPath); | ||
} catch { | ||
if (process.env.ZENSTACK_TEST === '1' && !path.isAbsolute(loadPath)) { | ||
try { | ||
// special handling for running as tests, try resolving relative to CWD | ||
return require(path.join(process.cwd(), 'node_modules', loadPath, 'zod')); | ||
} catch { | ||
return undefined; | ||
} | ||
} | ||
return undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.