PDMLab / http-problem-details

This library implements HTTP Problem details (RFC 7807) for HTTP APIs build with Node.js.
https://www.npmjs.com/package/http-problem-details
MIT License
48 stars 8 forks source link

Is the ProblemDocument class really neessary? #34

Open tpluscode opened 2 years ago

tpluscode commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

Please send a PR.

tpluscode commented 2 years ago

Definitely, will do.