lumen-framework 3.0.1 → 3.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -3694,10 +3694,11 @@ var MIME_TYPE = "application/vnd.api+json";
3694
3694
 
3695
3695
  // src/packages/jsonapi/errors/not-acceptable-error.ts
3696
3696
  var NotAcceptableError = class extends TypeError {
3697
- constructor(contentType) {
3697
+ constructor(accept) {
3698
3698
  super(line`
3699
- Media type parameters is not supported. Try your request again
3700
- without specifying '${contentType.replace(MIME_TYPE, "")}'.
3699
+ Every '${MIME_TYPE}' in Accept: '${accept}' has media type parameters,
3700
+ which are not supported. Try your request again with '${MIME_TYPE}'
3701
+ without parameters.
3701
3702
  `);
3702
3703
  }
3703
3704
  };
@@ -3707,8 +3708,9 @@ var not_acceptable_error_default = createServerError(NotAcceptableError, 406);
3707
3708
  var UnsupportedMediaTypeError = class extends TypeError {
3708
3709
  constructor(contentType) {
3709
3710
  super(line`
3710
- Media type parameters is not supported. Try your request again
3711
- without specifying '${contentType.replace(MIME_TYPE, "")}'.
3711
+ Media type parameters are not supported in Content-Type:
3712
+ '${contentType}'. Try your request again with Content-Type:
3713
+ '${MIME_TYPE}' without parameters.
3712
3714
  `);
3713
3715
  }
3714
3716
  };
@@ -3723,17 +3725,59 @@ var InvalidContentTypeError = class extends TypeError {
3723
3725
  `);
3724
3726
  }
3725
3727
  };
3726
- var invalid_content_type_error_default = createServerError(InvalidContentTypeError, 400);
3728
+ var invalid_content_type_error_default = createServerError(InvalidContentTypeError, 415);
3729
+
3730
+ // src/packages/jsonapi/utils/media-type.ts
3731
+ var QUALITY = /^q\s*=/i;
3732
+ function splitUnquoted(value, delimiter) {
3733
+ const parts = [];
3734
+ let current = "";
3735
+ let quoted = false;
3736
+ let escaped = false;
3737
+ for (const char of value) {
3738
+ if (escaped) {
3739
+ escaped = false;
3740
+ } else if (quoted && char === "\\") {
3741
+ escaped = true;
3742
+ } else if (char === '"') {
3743
+ quoted = !quoted;
3744
+ } else if (!quoted && char === delimiter) {
3745
+ parts.push(current);
3746
+ current = "";
3747
+ continue;
3748
+ }
3749
+ current += char;
3750
+ }
3751
+ return [...parts, current];
3752
+ }
3753
+ function parseMediaType(value) {
3754
+ const [type = "", ...params] = splitUnquoted(value, ";").map(
3755
+ (part) => part.trim()
3756
+ );
3757
+ return {
3758
+ type: type.toLowerCase(),
3759
+ params: params.filter(Boolean)
3760
+ };
3761
+ }
3762
+ function parseAccept(value) {
3763
+ return splitUnquoted(value, ",").map((range) => {
3764
+ const { type, params } = parseMediaType(range);
3765
+ const quality = params.findIndex((param) => QUALITY.test(param));
3766
+ return {
3767
+ type,
3768
+ params: quality === -1 ? params : params.slice(0, quality)
3769
+ };
3770
+ }).filter(({ type }) => type.length > 0);
3771
+ }
3727
3772
 
3728
3773
  // src/packages/jsonapi/utils/is-jsonapi.ts
3729
3774
  function isJSONAPI(value) {
3730
- return value.startsWith(MIME_TYPE);
3775
+ return parseMediaType(value).type === MIME_TYPE;
3731
3776
  }
3732
3777
 
3733
- // src/packages/jsonapi/utils/has-media-type.ts
3734
- var PATTERN2 = /^application\/vnd.api\+json;charset=.+$/;
3735
- function hasMediaType(value) {
3736
- return PATTERN2.test(value);
3778
+ // src/packages/jsonapi/utils/has-media-type-params.ts
3779
+ function hasMediaTypeParams(value) {
3780
+ return parseMediaType(value).params.length > 0;
3737
3781
  }
3738
3782
 
3739
3783
  // src/utils/env.ts
@@ -3834,9 +3878,12 @@ function createResponder(req, res) {
3834
3878
  }
3835
3879
 
3836
3880
  // src/packages/server/utils/validate-accept.ts
3837
- function validateAccept(contentType) {
3838
- if (contentType && hasMediaType(contentType)) {
3839
- throw new not_acceptable_error_default(contentType);
3881
+ function validateAccept(accept) {
3882
+ if (accept) {
3883
+ const ranges = parseAccept(accept).filter(({ type }) => type === MIME_TYPE);
3884
+ if (ranges.length && ranges.every(({ params }) => params.length)) {
3885
+ throw new not_acceptable_error_default(accept);
3886
+ }
3840
3887
  }
3841
3888
  return true;
3842
3889
  }
@@ -3845,7 +3892,7 @@ function validateAccept(contentType) {
3845
3892
  function validateContentType(contentType) {
3846
3893
  if (!contentType || !isJSONAPI(contentType)) {
3847
3894
  throw new invalid_content_type_error_default(contentType);
3848
- } else if (hasMediaType(contentType)) {
3895
+ } else if (hasMediaTypeParams(contentType)) {
3849
3896
  throw new unsupported_media_type_error_default(contentType);
3850
3897
  }
3851
3898
  return true;