accepts.js 649 B

1234567891011121314151617181920
  1. // src/helper/accepts/accepts.ts
  2. import { parseAccept } from "../../utils/accept.js";
  3. var defaultMatch = (accepts2, config) => {
  4. const { supports, default: defaultSupport } = config;
  5. const accept = accepts2.sort((a, b) => b.q - a.q).find((accept2) => supports.includes(accept2.type));
  6. return accept ? accept.type : defaultSupport;
  7. };
  8. var accepts = (c, options) => {
  9. const acceptHeader = c.req.header(options.header);
  10. if (!acceptHeader) {
  11. return options.default;
  12. }
  13. const accepts2 = parseAccept(acceptHeader);
  14. const match = options.match || defaultMatch;
  15. return match(accepts2, options);
  16. };
  17. export {
  18. accepts,
  19. defaultMatch
  20. };