-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API for accessing public config (#796)
* get config api * fix mock * fix types for transfrom-config test
- Loading branch information
1 parent
443af78
commit 9fc9459
Showing
11 changed files
with
305 additions
and
34 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
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.