-
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
API for accessing public config #796
Merged
Assem-Uber
merged 3 commits into
cadence-workflow:release/4.0.0
from
Assem-Hafez:feature/12016/get-config-api
Jan 21, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { type NextRequest } from 'next/server'; | ||
|
||
import getConfig from '@/route-handlers/get-config/get-config'; | ||
|
||
export async function GET(request: NextRequest) { | ||
return getConfig(request); | ||
} |
85 changes: 85 additions & 0 deletions
85
src/route-handlers/get-config/__tests__/get-config.node.ts
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,85 @@ | ||
import { NextRequest } from 'next/server'; | ||
|
||
import getConfigValue from '@/utils/config/get-config-value'; | ||
|
||
import getConfig from '../get-config'; | ||
import getConfigValueQueryParamsSchema from '../schemas/get-config-query-params-schema'; | ||
|
||
jest.mock('../schemas/get-config-query-params-schema'); | ||
jest.mock('@/utils/config/get-config-value'); | ||
|
||
describe('getConfig', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should return 400 if query parameters are invalid', async () => { | ||
(getConfigValueQueryParamsSchema.safeParse as jest.Mock).mockReturnValue({ | ||
data: null, | ||
error: { errors: ['Invalid query parameters'] }, | ||
}); | ||
|
||
const { res } = await setup({ | ||
configKey: 'testKey', | ||
jsonArgs: '', | ||
}); | ||
const responseJson = await res.json(); | ||
expect(responseJson).toEqual( | ||
expect.objectContaining({ | ||
message: 'Invalid values provided for config key/args', | ||
}) | ||
); | ||
}); | ||
|
||
it('should return config value if query parameters are valid', async () => { | ||
(getConfigValueQueryParamsSchema.safeParse as jest.Mock).mockReturnValue({ | ||
data: { configKey: 'testKey', jsonArgs: '{}' }, | ||
error: null, | ||
}); | ||
(getConfigValue as jest.Mock).mockResolvedValue('value'); | ||
|
||
const { res } = await setup({ | ||
configKey: 'testKey', | ||
jsonArgs: '{}', | ||
}); | ||
|
||
expect(getConfigValue).toHaveBeenCalledWith('testKey', '{}'); | ||
const responseJson = await res.json(); | ||
expect(responseJson).toEqual('value'); | ||
}); | ||
|
||
it('should handle errors from getConfigValue', async () => { | ||
(getConfigValueQueryParamsSchema.safeParse as jest.Mock).mockReturnValue({ | ||
data: { configKey: 'testKey', jsonArgs: '{}' }, | ||
error: null, | ||
}); | ||
(getConfigValue as jest.Mock).mockRejectedValue(new Error('Config error')); | ||
|
||
await expect( | ||
setup({ | ||
configKey: 'testKey', | ||
jsonArgs: '', | ||
}) | ||
).rejects.toThrow('Config error'); | ||
}); | ||
}); | ||
|
||
async function setup({ | ||
configKey, | ||
jsonArgs, | ||
}: { | ||
configKey: string; | ||
jsonArgs: string; | ||
error?: true; | ||
}) { | ||
const res = await getConfig( | ||
new NextRequest( | ||
`http://localhost?configKey=${configKey}&jsonArgs=${jsonArgs}`, | ||
{ | ||
method: 'GET', | ||
} | ||
) | ||
); | ||
|
||
return { res }; | ||
} |
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,29 @@ | ||
import { NextResponse, type NextRequest } from 'next/server'; | ||
|
||
import getConfigValue from '@/utils/config/get-config-value'; | ||
|
||
import getConfigValueQueryParamsSchema from './schemas/get-config-query-params-schema'; | ||
|
||
export default async function getConfig(request: NextRequest) { | ||
const { data: queryParams, error } = | ||
getConfigValueQueryParamsSchema.safeParse( | ||
Object.fromEntries(request.nextUrl.searchParams) | ||
); | ||
|
||
if (error) { | ||
return NextResponse.json( | ||
{ | ||
message: 'Invalid values provided for config key/args', | ||
cause: error.errors, | ||
}, | ||
{ | ||
status: 400, | ||
} | ||
); | ||
} | ||
|
||
const { configKey, jsonArgs } = queryParams; | ||
const res = await getConfigValue(configKey, jsonArgs); | ||
|
||
return NextResponse.json(res); | ||
} |
74 changes: 74 additions & 0 deletions
74
src/route-handlers/get-config/schemas/get-config-query-params-schema.ts
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,74 @@ | ||
import { z } from 'zod'; | ||
|
||
import dynamicConfigs from '@/config/dynamic/dynamic.config'; | ||
import resolverSchemas from '@/config/dynamic/resolvers/schemas/resolver-schemas'; | ||
import { | ||
type PublicDynamicConfigKeys, | ||
type ArgsOfLoadedConfigsResolvers, | ||
} from '@/utils/config/config.types'; | ||
|
||
const publicConfigKeys = Object.entries(dynamicConfigs) | ||
.filter(([_, d]) => d.isPublic) | ||
.map(([k]) => k) as PublicDynamicConfigKeys[]; | ||
|
||
const getConfigValueQueryParamsSchema = z | ||
.object({ | ||
configKey: z.string(), | ||
jsonArgs: z.string().optional(), | ||
}) | ||
.transform((data, ctx) => { | ||
const configKey = data.configKey as PublicDynamicConfigKeys; | ||
|
||
// validate configKey | ||
if (!publicConfigKeys.includes(configKey)) { | ||
ctx.addIssue({ | ||
code: z.ZodIssueCode.invalid_enum_value, | ||
options: publicConfigKeys, | ||
received: configKey, | ||
fatal: true, | ||
}); | ||
|
||
return z.NEVER; | ||
} | ||
|
||
// parse jsonArgs | ||
let parsedArgs; | ||
try { | ||
parsedArgs = data.jsonArgs | ||
? JSON.parse(decodeURIComponent(data.jsonArgs)) | ||
: undefined; | ||
} catch { | ||
ctx.addIssue({ code: 'custom', message: 'Invalid JSON' }); | ||
return z.NEVER; | ||
} | ||
|
||
// validate jsonArgs | ||
const configKeyForSchema = configKey as keyof typeof resolverSchemas; | ||
let validatedArgs = parsedArgs; | ||
if (resolverSchemas[configKeyForSchema]) { | ||
const schema = resolverSchemas[configKey as keyof typeof resolverSchemas]; | ||
const { error, data } = schema.args.safeParse(parsedArgs); | ||
validatedArgs = data; | ||
if (error) { | ||
ctx.addIssue({ | ||
code: z.ZodIssueCode.custom, | ||
message: `Invalid jsonArgs type provided. ${error.errors[0].message}`, | ||
fatal: true, | ||
}); | ||
return z.NEVER; | ||
} | ||
} | ||
const result: { | ||
configKey: PublicDynamicConfigKeys; | ||
jsonArgs: Pick< | ||
ArgsOfLoadedConfigsResolvers, | ||
PublicDynamicConfigKeys | ||
>[PublicDynamicConfigKeys]; | ||
} = { | ||
configKey, | ||
jsonArgs: validatedArgs, | ||
}; | ||
return result; | ||
}); | ||
|
||
export default getConfigValueQueryParamsSchema; |
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,13 @@ | ||
import { type LoadedConfigResolvedValues } from '../config.types'; | ||
|
||
const mockResolvedConfigValues: LoadedConfigResolvedValues = { | ||
DYNAMIC: 2, | ||
ADMIN_SECURITY_TOKEN: 'mock-secret', | ||
CADENCE_WEB_PORT: '3000', | ||
COMPUTED: ['mock-computed'], | ||
COMPUTED_WITH_ARG: ['mock-arg'], | ||
DYNAMIC_WITH_ARG: 5, | ||
GRPC_PROTO_DIR_BASE_PATH: 'mock/path/to/grpc/proto', | ||
GRPC_SERVICES_NAMES: 'mock-grpc-service-name', | ||
}; | ||
export default mockResolvedConfigValues; |
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,8 @@ | ||
import mockResolvedConfigValues from '../__fixtures__/resolved-config-values'; | ||
import { type LoadedConfigResolvedValues } from '../config.types'; | ||
|
||
export default jest.fn(function <K extends keyof LoadedConfigResolvedValues>( | ||
key: K | ||
) { | ||
return Promise.resolve(mockResolvedConfigValues[key]); | ||
}); |
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.
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.
Let's add some auth here so that not everyone can request configs