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

Is the ProblemDocument class really neessary? #34

Open
tpluscode opened this issue Oct 27, 2022 · 3 comments
Open

Is the ProblemDocument class really neessary? #34

tpluscode opened this issue Oct 27, 2022 · 3 comments

Comments

@tpluscode
Copy link
Contributor

Working some more with this and related libraries I'm repeatedly thinking whether the ProblemDocument really needs to be a class?

Wouldn't it be enough to simply define a contract for a problem document and have mapper return plain objects?

interface ProblemDocument extends Record<string, unknown> {
    detail?: string;
    instance?: string;
    status: number;
    title: string;
    type?: string;
}

class Mapper {
  mapError(error): ProblemDocument {
    return {
      status: 500,
      title: 'Something happened'
    }
  }
}

This would simplify how extensions are implemented and make it easier to work with derived or decorated mappers

@tpluscode
Copy link
Contributor Author

And as alternative to the Record<> type, we could keep a closed interface and have consumers extend with module augmentation

declare module 'http-problem-details' {
  interface ProblemDocument {
    extendingField?: string[]
  }
}

@AlexZeitler
Copy link
Contributor

Please send a PR.

@tpluscode
Copy link
Contributor Author

Definitely, will do.

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

No branches or pull requests

2 participants