Skip to content

Commit

Permalink
add solutions for api-helper and apply.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromehardaway committed Feb 9, 2024
1 parent 04d4b93 commit ba33006
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
15 changes: 6 additions & 9 deletions src/pages/api/api-helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
type EventBody = {
[key: string]: unknown;
};

export function checkParams(eventBody: EventBody, params: string[]): boolean {
return params.some((k) => {
const value = eventBody[k];
return typeof value === "undefined" || value === null || value === "";
// Further specific checks can be added based on the expected types of values.
// Adjusting the EventBody type to be generic
export function checkParams<T>(eventBody: T, params: (keyof T)[]): boolean {
return params.some((key) => {
// Explicitly handling the indexing with keyof T to ensure type safety
const value = eventBody[key as keyof T];
return value === undefined || value === null || value === "";
});
}

Expand Down
4 changes: 3 additions & 1 deletion src/pages/api/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ export default async function handler(req: Request, res: Response) {
"preworkLink",
"preworkRepo",
];
const hasErrors = checkParams<ParsedBody>(parsedBody, requiredParams);

// Note: Type argument removed since TypeScript infers <ParsedBody> from the argument types
const hasErrors = checkParams(parsedBody, requiredParams);

if (hasErrors) {
return res.status(422).json({
Expand Down

0 comments on commit ba33006

Please sign in to comment.