@kubb/plugin-fetch 5.13.0 → 5.13.2

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.cjs CHANGED
@@ -133,22 +133,65 @@ function getEventStream(node) {
133
133
  const content = getEventStreamContent(node);
134
134
  return content ? content.stream?.shape ?? "data" : null;
135
135
  }
136
+ function baseContentType(contentType) {
137
+ return contentType.split(";")[0].trim().toLowerCase();
138
+ }
139
+ function isJsonContentType(baseType) {
140
+ return baseType === "application/json" || baseType.endsWith("+json") || baseType === "text/json";
141
+ }
136
142
  /**
137
- * Derives the default `responseType` for an operation from its primary success response.
143
+ * Parser for one response content entry.
138
144
  *
139
- * Returns a value only when that response declares a single non-JSON content type. `text/event-stream` maps to
140
- * `'stream'`, `application/octet-stream`, `application/pdf`, `image/*`, `audio/*` and `video/*` map to `'blob'`, and
141
- * other `text/*` to `'text'`. Otherwise `undefined`, leaving the runtime client's `Content-Type` auto-detection in charge.
145
+ * JSON stays unset so the runtime detects it. `text/event-stream` is a stream. A binary schema
146
+ * (`format: binary` / `contentMediaType`) is a blob even when the media type is not a well-known
147
+ * binary type. Other `text/*` is text, and `application/octet-stream`, `application/pdf`,
148
+ * `image/*`, `audio/*`, and `video/*` are blobs.
142
149
  */
143
- function getResponseType(node) {
144
- const baseType = getPrimarySuccessContentType(node);
150
+ function responseTypeForContent(contentType, schema) {
151
+ const baseType = baseContentType(contentType);
145
152
  if (!baseType) return void 0;
146
- if (baseType === "application/json" || baseType.endsWith("+json") || baseType === "text/json") return void 0;
153
+ if (isJsonContentType(baseType)) return void 0;
147
154
  if (baseType === "text/event-stream") return "stream";
155
+ if (schema?.type === "blob") return "blob";
148
156
  if (baseType.startsWith("text/")) return "text";
149
157
  if (baseType === "application/octet-stream" || baseType === "application/pdf" || /^(image|audio|video)\//.test(baseType)) return "blob";
150
158
  }
151
159
  /**
160
+ * Derives the default `responseType` string for an operation from its primary success response.
161
+ *
162
+ * One content type uses {@link responseTypeForContent}. Several content types that all use the same
163
+ * parser collapse to that string, so a call can keep `responseType: 'blob'`. Mixed parsers return
164
+ * `undefined`; {@link getResponseTypeMap} carries the per-type choice.
165
+ */
166
+ function getResponseType(node) {
167
+ const entries = getPrimarySuccessResponse(node)?.content ?? [];
168
+ if (entries.length === 0) return void 0;
169
+ const responseTypes = entries.map((entry) => responseTypeForContent(entry.contentType, entry.schema));
170
+ const first = responseTypes[0];
171
+ if (!first) return void 0;
172
+ if (responseTypes.every((responseType) => responseType === first)) return first;
173
+ }
174
+ /**
175
+ * Maps each base content type of a primary success response to its parser when those parsers differ.
176
+ *
177
+ * Keys are lowercased and stripped of parameters. JSON entries are kept as `'json'` so a catch-all
178
+ * entry cannot claim a JSON response. Returns `undefined` when {@link getResponseType} already
179
+ * covers the response with one string, or when no content type needs a parser other than JSON.
180
+ * The runtime matches the response `Content-Type` against this map instead of guessing from the media type.
181
+ */
182
+ function getResponseTypeMap(node) {
183
+ if (getResponseType(node)) return void 0;
184
+ const entries = getPrimarySuccessResponse(node)?.content ?? [];
185
+ if (entries.length <= 1) return void 0;
186
+ const map = {};
187
+ for (const entry of entries) {
188
+ const baseType = baseContentType(entry.contentType);
189
+ const responseType = responseTypeForContent(entry.contentType, entry.schema) ?? (isJsonContentType(baseType) ? "json" : void 0);
190
+ if (baseType && responseType) map[baseType] = responseType;
191
+ }
192
+ return Object.values(map).some((responseType) => responseType !== "json") ? map : void 0;
193
+ }
194
+ /**
152
195
  * Which of the grouped request options an operation carries.
153
196
  */
154
197
  function getRequestGroups(node) {
@@ -176,7 +219,7 @@ function getRequestGroupOptionality(node) {
176
219
  hasRequiredPath,
177
220
  hasRequiredQuery,
178
221
  hasRequiredHeader,
179
- isOptional: !hasRequiredPath && !hasRequiredQuery && !hasRequiredHeader && !groups.body
222
+ isOptional: !hasRequiredPath && !hasRequiredQuery && !hasRequiredHeader && !(groups.body && node.requestBody?.required === true)
180
223
  };
181
224
  }
182
225
  function buildOperationComments(node, options = {}) {
@@ -835,6 +878,17 @@ function buildStyles({ node }) {
835
878
  }
836
879
  // ../../internals/client/src/builders/callConfig.ts
837
880
  /**
881
+ * `responseType` baked into a call. One parser is a string. Several parsers become a map from content
882
+ * type to parser so the runtime can choose from the response `Content-Type`.
883
+ */
884
+ function buildResponseTypeLiteral(node) {
885
+ const responseType = getResponseType(node);
886
+ if (responseType) return `responseType: '${responseType}'`;
887
+ const responseTypes = getResponseTypeMap(node);
888
+ if (!responseTypes) return null;
889
+ return `responseType: { ${Object.entries(responseTypes).map(([contentType, type]) => `${JSON.stringify(contentType)}: '${type}'`).join(", ")} }`;
890
+ }
891
+ /**
838
892
  * Builds the call config literal a generated operation forwards to the client, shared by the standalone
839
893
  * functions and the SDK methods. The `throwOnError` fallback is applied after the `...config` spread.
840
894
  *
@@ -861,7 +915,6 @@ function buildCallConfig({ node, validatorHooks, security, throwOnErrorDefault,
861
915
  "error"
862
916
  ]);
863
917
  const { literal: contentTypeLiteral, merge: mergeContentType } = buildContentType({ node });
864
- const responseType = getResponseType(node);
865
918
  return {
866
919
  callConfig: `{ ${[
867
920
  `method: '${node.method.toUpperCase()}'`,
@@ -870,7 +923,7 @@ function buildCallConfig({ node, validatorHooks, security, throwOnErrorDefault,
870
923
  stylesLiteral ? `styles: ${stylesLiteral}` : null,
871
924
  validatorLiteral,
872
925
  contentTypeLiteral,
873
- responseType ? `responseType: '${responseType}'` : null,
926
+ buildResponseTypeLiteral(node),
874
927
  "...config",
875
928
  `throwOnError: config.throwOnError ?? ${throwOnErrorDefault}`
876
929
  ].filter(Boolean).join(", ")} }`,
@@ -1676,7 +1729,7 @@ var compatibility = {
1676
1729
  "node": ">=22.22"
1677
1730
  };
1678
1731
  // package.json
1679
- var version = "5.13.0";
1732
+ var version = "5.13.2";
1680
1733
  // src/plugin.ts
1681
1734
  /**
1682
1735
  * Canonical plugin name for `@kubb/plugin-fetch`. Used for driver lookups and cross-plugin