11/* eslint-disable @typescript-eslint/no-var-requires */
22/* eslint-disable @typescript-eslint/no-explicit-any */
33
4- import path from 'path' ;
54import semver from 'semver' ;
65import { PRISMA_MINIMUM_VERSION } from '../../constants' ;
6+ import { getDefaultModelMeta , getDefaultPolicy , getDefaultZodSchemas } from '../../loader' ;
77import { AuthUser , DbClientContract } from '../../types' ;
88import { hasAllFields } from '../../validation' ;
9- import { getDefaultModelMeta } from '../model-meta' ;
109import { makeProxy } from '../proxy' ;
11- import type { ModelMeta , PolicyDef , ZodSchemas } from '../types' ;
10+ import type { CommonEnhancementOptions , ModelMeta , PolicyDef , ZodSchemas } from '../types' ;
1211import { getIdFields } from '../utils' ;
1312import { PolicyProxyHandler } from './handler' ;
1413
@@ -22,7 +21,7 @@ export type WithPolicyContext = {
2221/**
2322 * Options for @see withPolicy
2423 */
25- export type WithPolicyOptions = {
24+ export interface WithPolicyOptions extends CommonEnhancementOptions {
2625 /**
2726 * Policy definition
2827 */
@@ -42,7 +41,7 @@ export type WithPolicyOptions = {
4241 * Whether to log Prisma query
4342 */
4443 logPrismaQuery ?: boolean ;
45- } ;
44+ }
4645
4746/**
4847 * Gets an enhanced Prisma client with access policy check.
@@ -68,9 +67,9 @@ export function withPolicy<DbClient extends object>(
6867 ) ;
6968 }
7069
71- const _policy = options ?. policy ?? getDefaultPolicy ( ) ;
72- const _modelMeta = options ?. modelMeta ?? getDefaultModelMeta ( ) ;
73- const _zodSchemas = options ?. zodSchemas ?? getDefaultZodSchemas ( ) ;
70+ const _policy = options ?. policy ?? getDefaultPolicy ( options ?. loadPath ) ;
71+ const _modelMeta = options ?. modelMeta ?? getDefaultModelMeta ( options ?. loadPath ) ;
72+ const _zodSchemas = options ?. zodSchemas ?? getDefaultZodSchemas ( options ?. loadPath ) ;
7473
7574 // validate user context
7675 if ( context ?. user ) {
@@ -103,40 +102,3 @@ export function withPolicy<DbClient extends object>(
103102 'policy'
104103 ) ;
105104}
106-
107- function getDefaultPolicy ( ) : PolicyDef {
108- try {
109- return require ( '.zenstack/policy' ) . default ;
110- } catch {
111- if ( process . env . ZENSTACK_TEST === '1' ) {
112- try {
113- // special handling for running as tests, try resolving relative to CWD
114- return require ( path . join ( process . cwd ( ) , 'node_modules' , '.zenstack' , 'policy' ) ) . default ;
115- } catch {
116- throw new Error (
117- 'Policy definition cannot be loaded from default location. Please make sure "zenstack generate" has been run.'
118- ) ;
119- }
120- }
121- throw new Error (
122- 'Policy definition cannot be loaded from default location. Please make sure "zenstack generate" has been run.'
123- ) ;
124- }
125- }
126-
127- function getDefaultZodSchemas ( ) : ZodSchemas | undefined {
128- try {
129- // eslint-disable-next-line @typescript-eslint/no-var-requires
130- return require ( '.zenstack/zod' ) ;
131- } catch {
132- if ( process . env . ZENSTACK_TEST === '1' ) {
133- try {
134- // special handling for running as tests, try resolving relative to CWD
135- return require ( path . join ( process . cwd ( ) , 'node_modules' , '.zenstack' , 'zod' ) ) ;
136- } catch {
137- return undefined ;
138- }
139- }
140- return undefined ;
141- }
142- }
0 commit comments