Skip to content
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
wants to merge 1 commit into
base: release/4.0.0
Choose a base branch
from

Conversation

Assem-Hafez
Copy link
Contributor

@Assem-Hafez Assem-Hafez commented Jan 17, 2025

Summary

Adding validation schemas for config resolvers (args/return types) & use those schemas to validate the input/output of resolvers on runtime.

Add evaluateOn flag to configs to define when resolvers are being executed. It supports request & serverStart.

Add isPublic flag to configs to identify which configs are accessible from client code.

Changes

  • Extract logic from getTransformedConfigs to transformConfigs for better testing (doing the same for get-config-value.ts in upcomming PR)
  • Add schemas directory under configs as they can also be modified
  • Add evaluateOn & isPublic flags
  • Refactored types

Upcoming plans

  • Revisit naming for Types and functions to make them more understandable
  • better testing for get-config-value.ts

COMPUTED: ConfigSyncResolverDefinition<[string], [string]>;
DYNAMIC: ConfigAsyncResolverDefinition<undefined, number>;
GRPC_SERVICES_NAMES: ConfigEnvDefinition<true>;
DYNAMIC: ConfigAsyncResolverDefinition<undefined, number, 'serverStart'>;
Copy link
Member

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.

Copy link
Member

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

@@ -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: {
Copy link
Member

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 anyway, "ASYNC" and "SYNC" would be more meaningful than "DYNAMIC" and "COMPUTED"

} satisfies LoadedConfigs<typeof configs>);
});

// should throw an error if the resolved value does not match the schema
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// should throw an error if the resolved value does not match the schema

};

export type ConfigEnvDefinition = {
export type ConfigEnvDefinition<IsPublic extends boolean = false> = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit (as discussed): a union type would be nicer here, just so that we can write ConfigEnvDefinition<'public'> instead of ConfigEnvDefinition<true>

@@ -21,7 +24,7 @@ export default async function getConfigValue<K extends ConfigKeysWithoutArgs>(

export default async function getConfigValue<K extends keyof LoadedConfigs>(
key: K,
arg?: ArgOfConfigResolver<K>
arg?: ArgsOfLoadedConfigResolver<K>
): Promise<LoadedConfigValue<K>> {
if (typeof window !== 'undefined') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this line different from using 'server-only'?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants