kayto_ts 0.1.4 → 0.1.6

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.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  import { type Api, type ClientConfig, type EndpointsMap } from "./types.js";
2
- export type { Api, ClientConfig, ClientError, ClientHooks, EndpointsMap, EndpointOf, FetchResult, RequestInput, RequestOptions, } from "./types.js";
2
+ export type { Api, ClientConfig, ClientError, ClientHooks, EndpointsMap, EndpointOf, EndpointResponseMap, EndpointResult, FetchResult, RequestInput, RequestOptions, } from "./types.js";
3
3
  export declare function clientApi<Endpoints extends EndpointsMap>(config?: ClientConfig): Api<Endpoints>;
package/dist/index.js CHANGED
@@ -86,22 +86,27 @@ export function clientApi(config = {}) {
86
86
  };
87
87
  }
88
88
  }
89
- if (!response.ok) {
89
+ const bodyResult = await safeResponseBody(response);
90
+ if (!bodyResult.ok) {
90
91
  return {
91
92
  ok: false,
92
- error: makeClientError("http", `Request failed with status ${response.status}`, undefined, response.status),
93
+ error: makeClientError("parse", "Failed to parse response body", bodyResult.error),
93
94
  response,
94
95
  };
95
96
  }
96
- const bodyResult = await safeResponseBody(response);
97
- if (!bodyResult.ok) {
97
+ const responses = {
98
+ [response.status]: bodyResult.result,
99
+ };
100
+ const result = {
101
+ responses,
102
+ };
103
+ if (!response.ok) {
98
104
  return {
99
105
  ok: false,
100
- error: makeClientError("parse", "Failed to parse response body", bodyResult.error),
106
+ error: makeClientError("http", `Request failed with status ${response.status}`, undefined, response.status),
101
107
  response,
102
108
  };
103
109
  }
104
- const result = bodyResult.result;
105
110
  return { ok: true, result, response };
106
111
  };
107
112
  return {
package/dist/types.d.ts CHANGED
@@ -12,6 +12,13 @@ export type EndpointParams<E> = E extends {
12
12
  export type EndpointBody<E> = E extends {
13
13
  body: infer B;
14
14
  } ? B : never;
15
+ export type EndpointResponses<E> = E extends {
16
+ responses: infer R;
17
+ } ? R extends Record<PropertyKey, unknown> ? R : never : never;
18
+ export type EndpointResponseMap<E> = [EndpointResponses<E>] extends [never] ? Record<number, unknown> : Partial<EndpointResponses<E>>;
19
+ export type EndpointResult<E> = {
20
+ responses: EndpointResponseMap<E>;
21
+ };
15
22
  export type ErrorKind = "network" | "timeout" | "aborted" | "http" | "parse" | "hook";
16
23
  export type ClientError = {
17
24
  kind: ErrorKind;
@@ -48,7 +55,7 @@ export type RequestInput<E> = RequestOptions & ([EndpointParams<E>] extends [nev
48
55
  });
49
56
  export type RequestArgs<E> = [EndpointBody<E>] extends [never] ? [input?: RequestInput<E>] : [input: RequestInput<E>];
50
57
  export type Api<Endpoints extends EndpointsMap> = {
51
- [Method in keyof EndpointsMap]: <Path extends Extract<keyof Endpoints[Method], string>>(path: Path, ...args: RequestArgs<EndpointOf<Endpoints, Method, Path>>) => Promise<FetchResult<EndpointOf<Endpoints, Method, Path>>>;
58
+ [Method in keyof EndpointsMap]: <Path extends Extract<keyof Endpoints[Method], string>>(path: Path, ...args: RequestArgs<EndpointOf<Endpoints, Method, Path>>) => Promise<FetchResult<EndpointResult<EndpointOf<Endpoints, Method, Path>>>>;
52
59
  };
53
60
  export type RequestHookContext = {
54
61
  method: keyof EndpointsMap;
package/package.json CHANGED
@@ -1,34 +1,34 @@
1
1
  {
2
2
  "name": "kayto_ts",
3
- "version": "0.1.4",
4
- "description": "Type-safe HTTP client for working with kayto-generated endpoint schemas.",
3
+ "version": "0.1.6",
5
4
  "repository": "https://github.com/vladislav-yemelyanov/kayto_ts",
6
- "homepage": "https://www.npmjs.com/package/kayto_ts",
7
- "type": "module",
8
5
  "main": "./dist/index.js",
9
6
  "module": "./dist/index.js",
10
- "types": "./dist/index.d.ts",
7
+ "devDependencies": {
8
+ "@types/bun": "latest",
9
+ "typescript": "^5.9.3"
10
+ },
11
+ "peerDependencies": {
12
+ "typescript": "^5"
13
+ },
11
14
  "exports": {
12
15
  ".": {
13
16
  "types": "./dist/index.d.ts",
14
17
  "import": "./dist/index.js"
15
18
  }
16
19
  },
20
+ "description": "Type-safe HTTP client for working with kayto-generated endpoint schemas.",
17
21
  "files": [
18
22
  "dist",
19
23
  "README.md",
20
24
  "LICENSE"
21
25
  ],
26
+ "homepage": "https://www.npmjs.com/package/kayto_ts",
22
27
  "scripts": {
23
28
  "build": "tsc -p tsconfig.build.json",
24
29
  "prepublishOnly": "npm run build",
25
30
  "test": "bun test"
26
31
  },
27
- "devDependencies": {
28
- "@types/bun": "latest",
29
- "typescript": "^5.9.3"
30
- },
31
- "peerDependencies": {
32
- "typescript": "^5"
33
- }
32
+ "type": "module",
33
+ "types": "./dist/index.d.ts"
34
34
  }