@alphatique/hono-fetch 0.0.0-stage → 1.0.4

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/LICENSE ADDED
@@ -0,0 +1,15 @@
1
+ ISC License
2
+
3
+ Copyright (c) 2026 Alphatique
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any
6
+ purpose with or without fee is hereby granted, provided that the above
7
+ copyright notice and this permission notice appear in all copies.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
@@ -0,0 +1,33 @@
1
+ import { Schema } from "hono";
2
+ import { HonoBase } from "hono/hono-base";
3
+ import { Endpoint, ExtractSchema } from "hono/types";
4
+ import "hono/utils/headers";
5
+ //#region src/config.d.ts
6
+ interface HonoFetchConfig {
7
+ baseUrl: string;
8
+ onError?: (err: unknown) => any;
9
+ }
10
+ //#endregion
11
+ //#region src/types.d.ts
12
+ type Method = 'get' | 'post' | 'put' | 'delete' | 'patch';
13
+ type ExtractPath<S extends Schema, M extends Method> = { [P in keyof S]: P extends string ? InferEndpoint<S, M, P> extends Endpoint ? P : never : never; }[keyof S];
14
+ type InferEndpoint<S extends Schema, M extends Method, P extends keyof S> = S[P][`$${M}`];
15
+ type InferInput<S extends Schema, M extends Method, P extends keyof S> = InferEndpoint<S, M, P>['input'];
16
+ type InferOutput<S extends Schema, M extends Method, P extends keyof S> = InferEndpoint<S, M, P>['output'];
17
+ type InferError<C extends HonoFetchConfig> = C['onError'] extends ((err: unknown) => infer E) ? Awaited<E> : never;
18
+ //#endregion
19
+ //#region src/index.d.ts
20
+ export declare class HonoFetch<S extends Schema, C extends HonoFetchConfig> {
21
+ private config;
22
+ constructor(config: C);
23
+ private fetch;
24
+ get<P extends ExtractPath<S, 'get'>>(path: P, args: InferInput<S, 'get', P>): Promise<InferOutput<S, "get", P> | InferError<C>>;
25
+ post<P extends ExtractPath<S, 'post'>>(path: P, args: InferInput<S, 'post', P>): Promise<InferError<C> | InferOutput<S, "post", P>>;
26
+ put<P extends ExtractPath<S, 'put'>>(path: P, args: InferInput<S, 'put', P>): Promise<InferError<C> | InferOutput<S, "put", P>>;
27
+ delete<P extends ExtractPath<S, 'delete'>>(path: P, args: InferInput<S, 'delete', P>): Promise<InferError<C> | InferOutput<S, "delete", P>>;
28
+ patch<P extends ExtractPath<S, 'patch'>>(path: P, args: InferInput<S, 'patch', P>): Promise<InferError<C> | InferOutput<S, "patch", P>>;
29
+ }
30
+ type InferSchema<T extends HonoBase<any, any, any>> = T extends HonoBase<any, any, any> ? ExtractSchema<T> : never;
31
+ export declare function honoFetch<T extends HonoBase<any, any, any>, C extends HonoFetchConfig = HonoFetchConfig>(config: C): HonoFetch<InferSchema<T>, C>;
32
+ //#endregion
33
+ export {};
package/dist/index.mjs ADDED
@@ -0,0 +1,71 @@
1
+ //#region src/utils.ts
2
+ function replaceUrlParam(urlString, params) {
3
+ for (const [k, v] of Object.entries(params)) {
4
+ const reg = new RegExp("/:" + k + "(?:{[^/]+})?\\??");
5
+ urlString = urlString.replace(reg, v ? `/${v}` : "");
6
+ }
7
+ return urlString;
8
+ }
9
+ //#endregion
10
+ //#region src/index.ts
11
+ var HonoFetch = class {
12
+ config;
13
+ constructor(config) {
14
+ this.config = config;
15
+ }
16
+ async fetch(method, path, args) {
17
+ const url = new URL(args.param ? replaceUrlParam(path, args.param) : path, this.config.baseUrl);
18
+ if (args.query) for (const [k, v] of Object.entries(args.query)) {
19
+ if (v === void 0) continue;
20
+ if (Array.isArray(v)) for (const v2 of v) url.searchParams.append(k, v2);
21
+ else url.searchParams.set(k, v);
22
+ }
23
+ let contentType = null;
24
+ let body = null;
25
+ if (args.json) {
26
+ contentType = "application/json";
27
+ body = JSON.stringify(args.json);
28
+ }
29
+ if (args.form) {
30
+ const form = new FormData();
31
+ for (const [k, v] of Object.entries(args.form)) {
32
+ if (v === void 0) continue;
33
+ if (Array.isArray(v)) for (const v2 of v) form.append(k, v2);
34
+ else form.set(k, v);
35
+ }
36
+ body = form;
37
+ }
38
+ const headers = new Headers(args.header);
39
+ if (contentType) headers.set("Content-Type", contentType);
40
+ try {
41
+ return (await fetch(url, {
42
+ method: method.toUpperCase(),
43
+ headers,
44
+ body
45
+ })).json();
46
+ } catch (err) {
47
+ if (this.config.onError) return this.config.onError(err);
48
+ throw err;
49
+ }
50
+ }
51
+ get(path, args) {
52
+ return this.fetch("get", path, args);
53
+ }
54
+ post(path, args) {
55
+ return this.fetch("post", path, args);
56
+ }
57
+ put(path, args) {
58
+ return this.fetch("put", path, args);
59
+ }
60
+ delete(path, args) {
61
+ return this.fetch("delete", path, args);
62
+ }
63
+ patch(path, args) {
64
+ return this.fetch("patch", path, args);
65
+ }
66
+ };
67
+ function honoFetch(config) {
68
+ return new HonoFetch(config);
69
+ }
70
+ //#endregion
71
+ export { HonoFetch, honoFetch };
package/package.json CHANGED
@@ -1,6 +1,36 @@
1
1
  {
2
- "name": "@alphatique/hono-fetch",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "@alphatique/hono-fetch",
3
+ "version": "1.0.4",
4
+ "license": "ISC",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/alphatique/hono-fetch.git"
8
+ },
9
+ "files": [
10
+ "dist"
11
+ ],
12
+ "type": "module",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.mts",
16
+ "default": "./dist/index.mjs"
17
+ }
18
+ },
19
+ "publishConfig": {
20
+ "access": "public"
21
+ },
22
+ "scripts": {
23
+ "prepare": "vp config --no-agent",
24
+ "build": "vp pack"
25
+ },
26
+ "devDependencies": {
27
+ "hono": "^4.12.10",
28
+ "typescript": "^5.9.3",
29
+ "vite": "catalog:",
30
+ "vite-plus": "catalog:"
31
+ },
32
+ "peerDependencies": {
33
+ "hono": "^4.12.10"
34
+ },
35
+ "packageManager": "pnpm@12.11.2"
36
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.