-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic config resolver schemas and types #793
Open
Assem-Hafez
wants to merge
1
commit into
cadence-workflow:release/4.0.0
Choose a base branch
from
Assem-Hafez:feature/12015/dynamic-config-resolver-schemas
base: release/4.0.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,15 @@ const dynamicConfigs: { | |
CADENCE_WEB_PORT: ConfigEnvDefinition; | ||
ADMIN_SECURITY_TOKEN: ConfigEnvDefinition; | ||
GRPC_PROTO_DIR_BASE_PATH: ConfigEnvDefinition; | ||
GRPC_SERVICES_NAMES: ConfigEnvDefinition; | ||
COMPUTED: ConfigSyncResolverDefinition<[string], [string]>; | ||
DYNAMIC: ConfigAsyncResolverDefinition<undefined, number>; | ||
GRPC_SERVICES_NAMES: ConfigEnvDefinition<true>; | ||
DYNAMIC: ConfigAsyncResolverDefinition<undefined, number, 'serverStart'>; | ||
DYNAMIC_WITH_ARG: ConfigAsyncResolverDefinition<number, number, 'request'>; | ||
COMPUTED: ConfigSyncResolverDefinition<undefined, [string], 'request'>; | ||
COMPUTED_WITH_ARG: ConfigSyncResolverDefinition< | ||
[string], | ||
[string], | ||
'request' | ||
>; | ||
} = { | ||
CADENCE_WEB_PORT: { | ||
env: 'CADENCE_WEB_PORT', | ||
|
@@ -30,17 +36,32 @@ const dynamicConfigs: { | |
GRPC_SERVICES_NAMES: { | ||
env: 'NEXT_PUBLIC_CADENCE_GRPC_SERVICES_NAMES', | ||
default: 'cadence-frontend', | ||
isPublic: true, | ||
}, | ||
// For testing purposes | ||
DYNAMIC: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since these are test configs anyway, "ASYNC" and "SYNC" would be more meaningful than "DYNAMIC" and "COMPUTED" |
||
resolver: async () => { | ||
return 1; | ||
}, | ||
evaluateOn: 'serverStart', | ||
}, | ||
DYNAMIC_WITH_ARG: { | ||
resolver: async (value: number) => { | ||
return value; | ||
}, | ||
evaluateOn: 'request', | ||
}, | ||
COMPUTED: { | ||
resolver: () => { | ||
return ['value']; | ||
}, | ||
evaluateOn: 'request', | ||
}, | ||
COMPUTED_WITH_ARG: { | ||
resolver: (value: [string]) => { | ||
return value; | ||
}, | ||
evaluateOn: 'request', | ||
}, | ||
} as const; | ||
|
||
|
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,25 @@ | ||
import { z } from 'zod'; | ||
|
||
import { type ResolverSchemas } from '../../../../utils/config/config.types'; | ||
|
||
// Example usage: | ||
const resolverSchemas: ResolverSchemas = { | ||
COMPUTED: { | ||
args: z.undefined(), | ||
returnType: z.tuple([z.string()]), | ||
}, | ||
COMPUTED_WITH_ARG: { | ||
args: z.tuple([z.string()]), | ||
returnType: z.tuple([z.string()]), | ||
}, | ||
DYNAMIC: { | ||
args: z.undefined(), | ||
returnType: z.number(), | ||
}, | ||
DYNAMIC_WITH_ARG: { | ||
args: z.number(), | ||
returnType: z.number(), | ||
}, | ||
}; | ||
|
||
export default resolverSchemas; |
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,10 +1,21 @@ | ||
import getTransformedConfigs from './utils/config/get-transformed-configs'; | ||
import { setLoadedGlobalConfigs } from './utils/config/global-configs-ref'; | ||
import { registerLoggers } from './utils/logger'; | ||
import logger, { registerLoggers } from './utils/logger'; | ||
|
||
export async function register() { | ||
registerLoggers(); | ||
if (process.env.NEXT_RUNTIME === 'nodejs') { | ||
setLoadedGlobalConfigs(getTransformedConfigs()); | ||
try { | ||
const configs = await getTransformedConfigs(); | ||
setLoadedGlobalConfigs(configs); | ||
} catch (e) { | ||
// manually catching and logging the error to prevent the error being replaced | ||
// by "Cannot set property message of [object Object] which has only a getter" | ||
logger.error({ | ||
message: 'Failed to load configs', | ||
cause: String(e), | ||
}); | ||
process.exit(1); // use process.exit to exit without an extra error log from instrumentation | ||
} | ||
} | ||
} |
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,23 +1,17 @@ | ||||
import { type ConfigEnvDefinition, type LoadedConfigs } from '../config.types'; | ||||
import { default as getTransformedConfigs } from '../get-transformed-configs'; | ||||
|
||||
type MockConfigDefinitions = { | ||||
config1: ConfigEnvDefinition; | ||||
config2: ConfigEnvDefinition; | ||||
}; | ||||
jest.mock( | ||||
'@/config/dynamic/dynamic.config', | ||||
() => | ||||
({ | ||||
config1: { env: '$$$_MOCK_ENV_CONFIG1', default: 'default1' }, | ||||
config2: { env: '$$$_MOCK_ENV_CONFIG2', default: 'default2' }, | ||||
}) satisfies MockConfigDefinitions | ||||
); | ||||
import { z } from 'zod'; | ||||
|
||||
import { | ||||
type InferResolverSchema, | ||||
type ConfigEnvDefinition, | ||||
type LoadedConfigs, | ||||
type ConfigSyncResolverDefinition, | ||||
type ConfigAsyncResolverDefinition, | ||||
} from '../config.types'; | ||||
import transformConfigs from '../transform-configs'; | ||||
|
||||
describe('getTransformedConfigs', () => { | ||||
const originalEnv = process.env; | ||||
beforeEach(() => { | ||||
jest.resetModules(); | ||||
process.env = { | ||||
...originalEnv, | ||||
$$$_MOCK_ENV_CONFIG1: 'envValue1', | ||||
|
@@ -29,11 +23,95 @@ describe('getTransformedConfigs', () => { | |||
process.env = originalEnv; | ||||
}); | ||||
|
||||
it('should return transformed dynamic configs', () => { | ||||
const result = getTransformedConfigs(); | ||||
it('should get value for existing non empty environment variables', async () => { | ||||
const configs = { | ||||
config1: { env: '$$$_MOCK_ENV_CONFIG1', default: 'default1' }, | ||||
} satisfies { config1: ConfigEnvDefinition }; | ||||
|
||||
const resolversSchemas = {} as InferResolverSchema<typeof configs>; | ||||
|
||||
const result = await transformConfigs(configs, resolversSchemas); | ||||
expect(result).toEqual({ | ||||
config1: 'envValue1', | ||||
} satisfies LoadedConfigs<typeof configs>); | ||||
}); | ||||
|
||||
it('should get default value for unset environment variables', async () => { | ||||
const configs = { | ||||
config2: { env: '$$$_MOCK_ENV_CONFIG2', default: 'default2' }, | ||||
} satisfies { config2: ConfigEnvDefinition }; | ||||
|
||||
const resolversSchemas: InferResolverSchema<typeof configs> = {}; | ||||
|
||||
const result = await transformConfigs(configs, resolversSchemas); | ||||
expect(result).toEqual({ | ||||
config2: 'default2', | ||||
} satisfies LoadedConfigs<MockConfigDefinitions>); | ||||
} satisfies LoadedConfigs<typeof configs>); | ||||
}); | ||||
|
||||
it('should get resolved value for configuration that is evaluated on server start', async () => { | ||||
const configs = { | ||||
config3: { evaluateOn: 'serverStart', resolver: jest.fn(() => 3) }, | ||||
} satisfies { | ||||
config3: ConfigSyncResolverDefinition<undefined, number, 'serverStart'>; | ||||
}; | ||||
|
||||
const resolversSchemas: InferResolverSchema<typeof configs> = { | ||||
config3: { | ||||
args: z.undefined(), | ||||
returnType: z.number(), | ||||
}, | ||||
}; | ||||
|
||||
const result = await transformConfigs(configs, resolversSchemas); | ||||
expect(configs.config3.resolver).toHaveBeenCalledWith(undefined); | ||||
expect(result).toEqual({ | ||||
config3: 3, | ||||
} satisfies LoadedConfigs<typeof configs>); | ||||
}); | ||||
|
||||
it('should get the resolver for configuration that is evaluated on request', async () => { | ||||
const configs = { | ||||
config3: { evaluateOn: 'request', resolver: (n) => n }, | ||||
} satisfies { | ||||
config3: ConfigSyncResolverDefinition<number, number, 'request'>; | ||||
}; | ||||
|
||||
const resolversSchemas: InferResolverSchema<typeof configs> = { | ||||
config3: { | ||||
args: z.number(), | ||||
returnType: z.number(), | ||||
}, | ||||
}; | ||||
|
||||
const result = await transformConfigs(configs, resolversSchemas); | ||||
expect(result).toEqual({ | ||||
config3: configs.config3.resolver, | ||||
} satisfies LoadedConfigs<typeof configs>); | ||||
}); | ||||
|
||||
// should throw an error if the resolved value does not match the schema | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
it('should throw an error if the resolved value does not match the schema', async () => { | ||||
const configs = { | ||||
config3: { | ||||
evaluateOn: 'serverStart', | ||||
// @ts-expect-error - intentionally testing invalid return type | ||||
resolver: async () => '3', | ||||
}, | ||||
} satisfies { | ||||
config3: ConfigAsyncResolverDefinition<undefined, number, 'serverStart'>; | ||||
}; | ||||
|
||||
const resolversSchemas: InferResolverSchema<typeof configs> = { | ||||
config3: { | ||||
args: z.undefined(), | ||||
// @ts-expect-error - intentionally testing invalid return type | ||||
returnType: z.number(), | ||||
}, | ||||
}; | ||||
|
||||
await expect(transformConfigs(configs, resolversSchemas)).rejects.toThrow( | ||||
/Failed to parse config 'config3' resolved value/ | ||||
); | ||||
}); | ||||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since these are test configs, it would be good to add a comment above them like you did on Line 41.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO the best option would be to have a separate config file specifically for tests