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

Fix CreateTeam forms UI problems and add field error parsing to Cyberstorm forms #917

Merged
merged 4 commits into from
Nov 24, 2023

Conversation

Oksamies
Copy link
Contributor

No description provided.

@Oksamies Oksamies changed the title Continuing 908 Fix CreateTeam forms UI problems and add field error parsing to Cyberstorm forms Nov 22, 2023
return new ApiError(`${response.status}: ${response.statusText}`);
return new ApiError(
`${response.status}: ${response.statusText}`,
await response.json()
Copy link
Member

Choose a reason for hiding this comment

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

You need to try-catch this, it might not be json

}

getFieldErrors(): { [key: string]: string[] } {
// TODO: Implement
return {};
return { ...this.response };
Copy link
Member

Choose a reason for hiding this comment

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

Not guaranteed to exist if parsing is done properly

@@ -3,17 +3,22 @@ export function isApiError(e: Error | ApiError | unknown): e is ApiError {
}

export class ApiError extends Error {
constructor(message?: string) {
response: { [key: string]: string[] } | undefined;
Copy link
Member

Choose a reason for hiding this comment

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

Calling this responseData or such would be more appropriate. response would be appropriate if you stored the actual response object itself.

Comment on lines 24 to 26
...(args.method === "GET" || args.method === "HEAD" || args.method === null
? {}
: { body: args.body }),
Copy link
Member

Choose a reason for hiding this comment

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

This is wrong, whether a request body should be sent has nothing to do with the request method. It's just more common for that to be done with POST requests.

If having undefined in the body was causing issues, check if null works instead:

Suggested change
...(args.method === "GET" || args.method === "HEAD" || args.method === null
? {}
: { body: args.body }),
body: body ?? null,

According to the typescript type hints at least, that should be valid.

Copy link
Member

Choose a reason for hiding this comment

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

This suggestion isn't necessary if you follow the larger change suggestion, leaving it as a note anyway.

@@ -16,15 +16,18 @@ export async function apiFetch2(args: apiFetchArgs) {
const url = getUrl(args.config, args.path, args.query);

const response = await fetch(url, {
method: args.method ? args.method : "GET",
Copy link
Member

Choose a reason for hiding this comment

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

Don't do this, change the type hint for apiFetchArgs to require the type to be provided instead if you want to enforce it.

The best option would probably be to move both the method and body arguments under the same object and let the caller control any of the normal fetch parameters like so:

export type apiFetchArgs = {
  config: RequestConfig;
  path: string;
  query?: string;
  request?: Omit<RequestInit, "headers">;
};

Then you can just merge it to the fetch call as-is:

  const response = await fetch(url, {
    ...(args.request ?? {}),
    headers: {
      ...BASE_HEADERS,
      ...getAuthHeaders(args.config),
    },
  });

It should be the caller's responsibility to call the function correctly, and typescript's is used to make sure the caller knows what "correctly" means.

Copy link
Member

@MythicManiac MythicManiac left a comment

Choose a reason for hiding this comment

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

Good to go now as far as I'm concerned, but do check that it works for the use case you tested it for

@Oksamies Oksamies merged commit 1c15173 into master Nov 24, 2023
@Oksamies Oksamies deleted the continuing-908 branch November 24, 2023 02:09
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