@kizunajs/fetch 2.0.0-beta.21 → 2.0.0-beta.22

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
@@ -10,10 +10,13 @@ let zod = require("zod");
10
10
  */
11
11
  const runtimeSource = String.raw`/**
12
12
  * What the client knows about one response: nothing for a body it reads as
13
- * JSON, the media type for one that streams.
13
+ * JSON, how to read one that is not, the media type for one that streams.
14
14
  */
15
15
  export type GeneratedResponse =
16
16
  | Record<string, never>
17
+ | {
18
+ read: 'bytes' | 'text';
19
+ }
17
20
  | {
18
21
  stream: true;
19
22
  contentType: string;
@@ -488,6 +491,13 @@ const buildMethod = (route: GeneratedRoute, config: ClientConfig) => {
488
491
  headers: responseHeaders,
489
492
  };
490
493
  }
494
+ if (declared !== undefined && 'read' in declared) {
495
+ return {
496
+ status: response.status,
497
+ body: declared.read === 'bytes' ? new Uint8Array(await response.arrayBuffer()) : await response.text(),
498
+ headers: responseHeaders,
499
+ };
500
+ }
491
501
  const text = await response.text();
492
502
  return {
493
503
  status: response.status,
@@ -940,7 +950,7 @@ const emitRoute = (routeKey, key, route, collector, namespaceName) => {
940
950
  const argsType = `{\n${indent(args.map((arg) => `${arg};`).join("\n"))}\n}`;
941
951
  const tableEntries = [`method: '${route.method}'`, `path: '${route.path}'`];
942
952
  if (route.contentType) tableEntries.push(`contentType: '${route.contentType}'`);
943
- const responses = Object.entries(route.responses).map(([status, response]) => (0, kizunajs_generator.isStreamResponse)(response) ? `${status}: { stream: true, contentType: '${(0, kizunajs_generator.streamContentType)(response)}' }` : `${status}: {}`);
953
+ const responses = Object.entries(route.responses).map(([status, response]) => (0, kizunajs_generator.isStreamResponse)(response) ? `${status}: { stream: true, contentType: '${(0, kizunajs_generator.streamContentType)(response)}' }` : `${status}: ${readEntry(response)}`);
944
954
  if (validationBody && !declaredBadRequest) responses.push("400: {}");
945
955
  tableEntries.push(`responses: {\n${indent(responses.map((entry) => `${entry},`).join("\n"))}\n}`);
946
956
  const streams = Object.values(route.responses).some((response) => (0, kizunajs_generator.isStreamResponse)(response));
@@ -951,6 +961,16 @@ const emitRoute = (routeKey, key, route, collector, namespaceName) => {
951
961
  table: `${key}: {\n${indent(tableEntries.map((entry) => `${entry},`).join("\n"))}\n},`
952
962
  };
953
963
  };
964
+ /**
965
+ * How the client reads a body that does not stream: as bytes for a binary schema, as text for any other media type than
966
+ * JSON, as JSON otherwise.
967
+ */
968
+ const readEntry = (response) => {
969
+ const schema = (0, kizunajs_generator.resolveResponseBody)(response);
970
+ if (schema && (0, kizunajs_generator.isBinarySchema)(schema)) return `{ read: 'bytes' }`;
971
+ const contentType = (0, kizunajs_generator.resolveResponseContentType)(response);
972
+ return contentType !== void 0 && !(0, kizunajs_generator.isJsonMediaType)(contentType) ? `{ read: 'text' }` : "{}";
973
+ };
954
974
  const isRoute = (value) => !!value && typeof value === "object" && typeof value.method === "string";
955
975
  const emitTree = (routes, prefix, collector, namespaceName) => {
956
976
  const signatures = [];
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { ValidationErrorSchema } from "kizunajs/schemas";
2
- import { defineClient, isBinarySchema, isFileSchema, isStreamResponse, isVoidSchema, listedRoutes, readDef, readDeprecation, readDiscriminatedUnion, readMetaBrand, readMetaDescription, readMetaExamples, readMetaId, readObjectShape, resolveResponseBody, resolveResponseHeaders, sanitizeIdentifier, streamContentType, streamMode, toPascalCase, unwrapOptionalWrappers } from "kizunajs/generator";
2
+ import { defineClient, isBinarySchema, isFileSchema, isJsonMediaType, isStreamResponse, isVoidSchema, listedRoutes, readDef, readDeprecation, readDiscriminatedUnion, readMetaBrand, readMetaDescription, readMetaExamples, readMetaId, readObjectShape, resolveResponseBody, resolveResponseContentType, resolveResponseHeaders, sanitizeIdentifier, streamContentType, streamMode, toPascalCase, unwrapOptionalWrappers } from "kizunajs/generator";
3
3
  import { z } from "zod";
4
4
  //#region src/runtime.ts
5
5
  /**
@@ -9,10 +9,13 @@ import { z } from "zod";
9
9
  */
10
10
  const runtimeSource = String.raw`/**
11
11
  * What the client knows about one response: nothing for a body it reads as
12
- * JSON, the media type for one that streams.
12
+ * JSON, how to read one that is not, the media type for one that streams.
13
13
  */
14
14
  export type GeneratedResponse =
15
15
  | Record<string, never>
16
+ | {
17
+ read: 'bytes' | 'text';
18
+ }
16
19
  | {
17
20
  stream: true;
18
21
  contentType: string;
@@ -487,6 +490,13 @@ const buildMethod = (route: GeneratedRoute, config: ClientConfig) => {
487
490
  headers: responseHeaders,
488
491
  };
489
492
  }
493
+ if (declared !== undefined && 'read' in declared) {
494
+ return {
495
+ status: response.status,
496
+ body: declared.read === 'bytes' ? new Uint8Array(await response.arrayBuffer()) : await response.text(),
497
+ headers: responseHeaders,
498
+ };
499
+ }
490
500
  const text = await response.text();
491
501
  return {
492
502
  status: response.status,
@@ -939,7 +949,7 @@ const emitRoute = (routeKey, key, route, collector, namespaceName) => {
939
949
  const argsType = `{\n${indent(args.map((arg) => `${arg};`).join("\n"))}\n}`;
940
950
  const tableEntries = [`method: '${route.method}'`, `path: '${route.path}'`];
941
951
  if (route.contentType) tableEntries.push(`contentType: '${route.contentType}'`);
942
- const responses = Object.entries(route.responses).map(([status, response]) => isStreamResponse(response) ? `${status}: { stream: true, contentType: '${streamContentType(response)}' }` : `${status}: {}`);
952
+ const responses = Object.entries(route.responses).map(([status, response]) => isStreamResponse(response) ? `${status}: { stream: true, contentType: '${streamContentType(response)}' }` : `${status}: ${readEntry(response)}`);
943
953
  if (validationBody && !declaredBadRequest) responses.push("400: {}");
944
954
  tableEntries.push(`responses: {\n${indent(responses.map((entry) => `${entry},`).join("\n"))}\n}`);
945
955
  const streams = Object.values(route.responses).some((response) => isStreamResponse(response));
@@ -950,6 +960,16 @@ const emitRoute = (routeKey, key, route, collector, namespaceName) => {
950
960
  table: `${key}: {\n${indent(tableEntries.map((entry) => `${entry},`).join("\n"))}\n},`
951
961
  };
952
962
  };
963
+ /**
964
+ * How the client reads a body that does not stream: as bytes for a binary schema, as text for any other media type than
965
+ * JSON, as JSON otherwise.
966
+ */
967
+ const readEntry = (response) => {
968
+ const schema = resolveResponseBody(response);
969
+ if (schema && isBinarySchema(schema)) return `{ read: 'bytes' }`;
970
+ const contentType = resolveResponseContentType(response);
971
+ return contentType !== void 0 && !isJsonMediaType(contentType) ? `{ read: 'text' }` : "{}";
972
+ };
953
973
  const isRoute = (value) => !!value && typeof value === "object" && typeof value.method === "string";
954
974
  const emitTree = (routes, prefix, collector, namespaceName) => {
955
975
  const signatures = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kizunajs/fetch",
3
- "version": "2.0.0-beta.21",
3
+ "version": "2.0.0-beta.22",
4
4
  "description": "Fetch client generation for Kizuna.js APIs",
5
5
  "keywords": [
6
6
  "kizunajs",
@@ -56,12 +56,12 @@
56
56
  "access": "public"
57
57
  },
58
58
  "peerDependencies": {
59
- "kizunajs": "2.0.0-beta.21",
59
+ "kizunajs": "2.0.0-beta.22",
60
60
  "zod": "^4.0.0"
61
61
  },
62
62
  "devDependencies": {
63
- "kizunajs": "2.0.0-beta.21",
64
- "@kizunajs/express": "2.0.0-beta.21",
63
+ "kizunajs": "2.0.0-beta.22",
64
+ "@kizunajs/express": "2.0.0-beta.22",
65
65
  "@types/express": "^5.0.0",
66
66
  "express": "^5.0.0",
67
67
  "tsdown": "^0.21.0",