msw 0.42.0 → 0.42.1

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/lib/index.d.ts CHANGED
@@ -183,6 +183,14 @@ declare const rest: {
183
183
  options: <RequestBodyType_7 extends DefaultBodyType = DefaultBodyType, Params_7 extends PathParams<keyof Params_7> = PathParams<string>, ResponseBody_7 extends DefaultBodyType = DefaultBodyType>(path: Path, resolver: ResponseResolver<RestRequest<RequestBodyType_7, Params_7>, RestContext, ResponseBody_7>) => RestHandler<MockedRequest<DefaultBodyType>>;
184
184
  };
185
185
 
186
+ declare type ForbiddenFieldNames = '' | 'data' | 'errors' | 'extensions';
187
+ /**
188
+ * Set a custom field on the GraphQL mocked response.
189
+ * @example res(ctx.fields('customField', value))
190
+ * @see {@link https://mswjs.io/docs/api/context/field}
191
+ */
192
+ declare const field: <FieldNameType extends string, FieldValueType>(fieldName: FieldNameType extends ForbiddenFieldNames ? never : FieldNameType, fieldValue: FieldValueType) => ResponseTransformer<string>;
193
+
186
194
  interface ParsedGraphQLQuery {
187
195
  operationType: OperationTypeNode;
188
196
  operationName?: string;
@@ -204,6 +212,7 @@ declare type GraphQLContext<QueryType extends Record<string, unknown>> = Default
204
212
  extensions: GraphQLPayloadContext<QueryType>;
205
213
  errors: typeof errors;
206
214
  cookie: typeof cookie;
215
+ field: typeof field;
207
216
  };
208
217
  declare const graphqlContext: GraphQLContext<any>;
209
218
  declare type GraphQLVariables = Record<string, any>;
package/lib/index.js CHANGED
@@ -685,8 +685,8 @@ function parseMultipartData(data2, headers) {
685
685
  }
686
686
  const parsedBody = {};
687
687
  try {
688
- for (const field of fields) {
689
- const [contentHeaders, ...rest2] = field.split("\r\n\r\n");
688
+ for (const field2 of fields) {
689
+ const [contentHeaders, ...rest2] = field2.split("\r\n\r\n");
690
690
  const contentBody = rest2.join("\r\n\r\n");
691
691
  const { contentType: contentType2, filename, name } = parseContentHeaders(contentHeaders);
692
692
  const value = filename === void 0 ? contentBody : new File([contentBody], filename, { type: contentType2 });
@@ -1088,6 +1088,23 @@ var RestHandler = class extends RequestHandler {
1088
1088
  }
1089
1089
  };
1090
1090
 
1091
+ // src/context/field.ts
1092
+ var import_outvariant2 = require("outvariant");
1093
+ var field = (fieldName, fieldValue) => {
1094
+ return (res) => {
1095
+ validateFieldName(fieldName);
1096
+ const prevBody = jsonParse(res.body) || {};
1097
+ const nextBody = mergeRight(prevBody, { [fieldName]: fieldValue });
1098
+ return json(nextBody)(res);
1099
+ };
1100
+ };
1101
+ function validateFieldName(fieldName) {
1102
+ (0, import_outvariant2.invariant)(fieldName.trim() !== "", devUtils.formatMessage("Failed to set a custom field on a GraphQL response: field name cannot be empty."));
1103
+ (0, import_outvariant2.invariant)(fieldName !== "data", devUtils.formatMessage('Failed to set a custom "%s" field on a mocked GraphQL response: forbidden field name. Did you mean to call "ctx.data()" instead?', fieldName));
1104
+ (0, import_outvariant2.invariant)(fieldName !== "errors", devUtils.formatMessage('Failed to set a custom "%s" field on a mocked GraphQL response: forbidden field name. Did you mean to call "ctx.errors()" instead?', fieldName));
1105
+ (0, import_outvariant2.invariant)(fieldName !== "extensions", devUtils.formatMessage('Failed to set a custom "%s" field on a mocked GraphQL response: forbidden field name. Did you mean to call "ctx.extensions()" instead?', fieldName));
1106
+ }
1107
+
1091
1108
  // src/utils/internal/tryCatch.ts
1092
1109
  function tryCatch(fn, onException) {
1093
1110
  try {
@@ -1103,7 +1120,8 @@ var graphqlContext = __spreadProps(__spreadValues({}, defaultContext), {
1103
1120
  data,
1104
1121
  extensions,
1105
1122
  errors,
1106
- cookie
1123
+ cookie,
1124
+ field
1107
1125
  });
1108
1126
  function isDocumentNode(value) {
1109
1127
  if (value == null) {