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

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,
@@ -776,6 +786,8 @@ const unbrandedTypeOf = (schema, collector, hint) => {
776
786
  const side = collector.readsInput() ? def.in : def.out ?? def.in;
777
787
  return side ? typeOf(side, collector, hint) : "unknown";
778
788
  }
789
+ const model = (0, kizunajs_generator.readModelSchema)(schema);
790
+ if (model !== void 0 && model !== schema) return unbrandedTypeOf(model, collector, hint);
779
791
  const modelName = (0, kizunajs_generator.readMetaId)(schema);
780
792
  if (modelName !== void 0) {
781
793
  if (collector.claim(modelName)) collector.add({
@@ -940,7 +952,7 @@ const emitRoute = (routeKey, key, route, collector, namespaceName) => {
940
952
  const argsType = `{\n${indent(args.map((arg) => `${arg};`).join("\n"))}\n}`;
941
953
  const tableEntries = [`method: '${route.method}'`, `path: '${route.path}'`];
942
954
  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}: {}`);
955
+ 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
956
  if (validationBody && !declaredBadRequest) responses.push("400: {}");
945
957
  tableEntries.push(`responses: {\n${indent(responses.map((entry) => `${entry},`).join("\n"))}\n}`);
946
958
  const streams = Object.values(route.responses).some((response) => (0, kizunajs_generator.isStreamResponse)(response));
@@ -951,6 +963,16 @@ const emitRoute = (routeKey, key, route, collector, namespaceName) => {
951
963
  table: `${key}: {\n${indent(tableEntries.map((entry) => `${entry},`).join("\n"))}\n},`
952
964
  };
953
965
  };
966
+ /**
967
+ * How the client reads a body that does not stream: as bytes for a binary schema, as text for any other media type than
968
+ * JSON, as JSON otherwise.
969
+ */
970
+ const readEntry = (response) => {
971
+ const schema = (0, kizunajs_generator.resolveResponseBody)(response);
972
+ if (schema && (0, kizunajs_generator.isBinarySchema)(schema)) return `{ read: 'bytes' }`;
973
+ const contentType = (0, kizunajs_generator.resolveResponseContentType)(response);
974
+ return contentType !== void 0 && !(0, kizunajs_generator.isJsonMediaType)(contentType) ? `{ read: 'text' }` : "{}";
975
+ };
954
976
  const isRoute = (value) => !!value && typeof value === "object" && typeof value.method === "string";
955
977
  const emitTree = (routes, prefix, collector, namespaceName) => {
956
978
  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, readModelSchema, 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,
@@ -775,6 +785,8 @@ const unbrandedTypeOf = (schema, collector, hint) => {
775
785
  const side = collector.readsInput() ? def.in : def.out ?? def.in;
776
786
  return side ? typeOf(side, collector, hint) : "unknown";
777
787
  }
788
+ const model = readModelSchema(schema);
789
+ if (model !== void 0 && model !== schema) return unbrandedTypeOf(model, collector, hint);
778
790
  const modelName = readMetaId(schema);
779
791
  if (modelName !== void 0) {
780
792
  if (collector.claim(modelName)) collector.add({
@@ -939,7 +951,7 @@ const emitRoute = (routeKey, key, route, collector, namespaceName) => {
939
951
  const argsType = `{\n${indent(args.map((arg) => `${arg};`).join("\n"))}\n}`;
940
952
  const tableEntries = [`method: '${route.method}'`, `path: '${route.path}'`];
941
953
  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}: {}`);
954
+ const responses = Object.entries(route.responses).map(([status, response]) => isStreamResponse(response) ? `${status}: { stream: true, contentType: '${streamContentType(response)}' }` : `${status}: ${readEntry(response)}`);
943
955
  if (validationBody && !declaredBadRequest) responses.push("400: {}");
944
956
  tableEntries.push(`responses: {\n${indent(responses.map((entry) => `${entry},`).join("\n"))}\n}`);
945
957
  const streams = Object.values(route.responses).some((response) => isStreamResponse(response));
@@ -950,6 +962,16 @@ const emitRoute = (routeKey, key, route, collector, namespaceName) => {
950
962
  table: `${key}: {\n${indent(tableEntries.map((entry) => `${entry},`).join("\n"))}\n},`
951
963
  };
952
964
  };
965
+ /**
966
+ * How the client reads a body that does not stream: as bytes for a binary schema, as text for any other media type than
967
+ * JSON, as JSON otherwise.
968
+ */
969
+ const readEntry = (response) => {
970
+ const schema = resolveResponseBody(response);
971
+ if (schema && isBinarySchema(schema)) return `{ read: 'bytes' }`;
972
+ const contentType = resolveResponseContentType(response);
973
+ return contentType !== void 0 && !isJsonMediaType(contentType) ? `{ read: 'text' }` : "{}";
974
+ };
953
975
  const isRoute = (value) => !!value && typeof value === "object" && typeof value.method === "string";
954
976
  const emitTree = (routes, prefix, collector, namespaceName) => {
955
977
  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.23",
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.23",
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.23",
64
+ "@kizunajs/express": "2.0.0-beta.23",
65
65
  "@types/express": "^5.0.0",
66
66
  "express": "^5.0.0",
67
67
  "tsdown": "^0.21.0",