@foldset/hono 0.1.0
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/adapter.d.ts +19 -0
- package/dist/adapter.d.ts.map +1 -0
- package/dist/adapter.js +44 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/dist/middleware.d.ts +4 -0
- package/dist/middleware.d.ts.map +1 -0
- package/dist/middleware.js +55 -0
- package/package.json +49 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Context } from "hono";
|
|
2
|
+
import type { RequestAdapter } from "@foldset/core";
|
|
3
|
+
export declare class HonoAdapter implements RequestAdapter {
|
|
4
|
+
private c;
|
|
5
|
+
private bodyPromise;
|
|
6
|
+
constructor(c: Context);
|
|
7
|
+
getIpAddress(): string | null;
|
|
8
|
+
getHeader(name: string): string | undefined;
|
|
9
|
+
getMethod(): string;
|
|
10
|
+
getPath(): string;
|
|
11
|
+
getUrl(): string;
|
|
12
|
+
getHost(): string;
|
|
13
|
+
getAcceptHeader(): string;
|
|
14
|
+
getUserAgent(): string;
|
|
15
|
+
getQueryParams(): Record<string, string | string[]>;
|
|
16
|
+
getQueryParam(name: string): string | string[] | undefined;
|
|
17
|
+
getBody(): Promise<unknown>;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAEpD,qBAAa,WAAY,YAAW,cAAc;IAGpC,OAAO,CAAC,CAAC;IAFrB,OAAO,CAAC,WAAW,CAAiC;gBAEhC,CAAC,EAAE,OAAO;IAE9B,YAAY,IAAI,MAAM,GAAG,IAAI;IAM7B,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAI3C,SAAS,IAAI,MAAM;IAInB,OAAO,IAAI,MAAM;IAIjB,MAAM,IAAI,MAAM;IAIhB,OAAO,IAAI,MAAM;IAIjB,eAAe,IAAI,MAAM;IAIzB,YAAY,IAAI,MAAM;IAItB,cAAc,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAInD,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS;IAI1D,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC;CAI5B"}
|
package/dist/adapter.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export class HonoAdapter {
|
|
2
|
+
c;
|
|
3
|
+
bodyPromise = null;
|
|
4
|
+
constructor(c) {
|
|
5
|
+
this.c = c;
|
|
6
|
+
}
|
|
7
|
+
getIpAddress() {
|
|
8
|
+
const forwarded = this.getHeader("x-forwarded-for");
|
|
9
|
+
if (forwarded)
|
|
10
|
+
return forwarded.split(",")[0].trim();
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
getHeader(name) {
|
|
14
|
+
return this.c.req.header(name);
|
|
15
|
+
}
|
|
16
|
+
getMethod() {
|
|
17
|
+
return this.c.req.method;
|
|
18
|
+
}
|
|
19
|
+
getPath() {
|
|
20
|
+
return this.c.req.path;
|
|
21
|
+
}
|
|
22
|
+
getUrl() {
|
|
23
|
+
return this.c.req.url;
|
|
24
|
+
}
|
|
25
|
+
getHost() {
|
|
26
|
+
return new URL(this.c.req.url).hostname;
|
|
27
|
+
}
|
|
28
|
+
getAcceptHeader() {
|
|
29
|
+
return this.getHeader("Accept") ?? "";
|
|
30
|
+
}
|
|
31
|
+
getUserAgent() {
|
|
32
|
+
return this.getHeader("User-Agent") ?? "";
|
|
33
|
+
}
|
|
34
|
+
getQueryParams() {
|
|
35
|
+
return this.c.req.query();
|
|
36
|
+
}
|
|
37
|
+
getQueryParam(name) {
|
|
38
|
+
return this.c.req.query(name);
|
|
39
|
+
}
|
|
40
|
+
getBody() {
|
|
41
|
+
this.bodyPromise ??= this.c.req.json().catch(() => undefined);
|
|
42
|
+
return this.bodyPromise;
|
|
43
|
+
}
|
|
44
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,YAAY,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { foldset } from "./middleware";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAW,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAGvD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAYpD,wBAAgB,OAAO,CAAC,OAAO,EAAE,cAAc,GAAG,iBAAiB,CA8DlE"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { createMiddleware } from "hono/factory";
|
|
2
|
+
import { WorkerCore, reportError } from "@foldset/core";
|
|
3
|
+
import packageJson from "../package.json" with { type: "json" };
|
|
4
|
+
import { HonoAdapter } from "./adapter";
|
|
5
|
+
function setHeaders(c, headers) {
|
|
6
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
7
|
+
c.header(key, value);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export function foldset(options) {
|
|
11
|
+
if (!options.apiKey) {
|
|
12
|
+
console.warn("[foldset] No API key provided, middleware disabled");
|
|
13
|
+
return createMiddleware(async (_c, next) => {
|
|
14
|
+
await next();
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
const opts = { ...options, platform: "hono", sdkVersion: packageJson.version };
|
|
18
|
+
return createMiddleware(async (c, next) => {
|
|
19
|
+
try {
|
|
20
|
+
const core = await WorkerCore.fromOptions(opts);
|
|
21
|
+
const adapter = new HonoAdapter(c);
|
|
22
|
+
const result = await core.processRequest(adapter);
|
|
23
|
+
switch (result.type) {
|
|
24
|
+
case "health-check":
|
|
25
|
+
return c.newResponse(result.response.body, result.response.status, result.response.headers);
|
|
26
|
+
case "no-payment-required":
|
|
27
|
+
if (result.headers) {
|
|
28
|
+
setHeaders(c, result.headers);
|
|
29
|
+
}
|
|
30
|
+
await next();
|
|
31
|
+
return;
|
|
32
|
+
case "payment-error": {
|
|
33
|
+
const { body, status, headers } = result.response;
|
|
34
|
+
return c.newResponse(body, status, headers);
|
|
35
|
+
}
|
|
36
|
+
case "payment-verified": {
|
|
37
|
+
await next();
|
|
38
|
+
const settlement = await core.processSettlement(adapter, result.paymentPayload, result.paymentRequirements, c.res.status, result.metadata.request_id);
|
|
39
|
+
if (settlement.success) {
|
|
40
|
+
setHeaders(c, settlement.headers);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
c.res = c.newResponse(JSON.stringify({ error: "Settlement failed", details: settlement.errorReason }), 402, { "Content-Type": "application/json" });
|
|
44
|
+
}
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
// On any error, allow the request through rather than blocking the user.
|
|
51
|
+
reportError(opts.apiKey, error, new HonoAdapter(c));
|
|
52
|
+
await next();
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@foldset/hono",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Hono middleware for Foldset payment protection",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "https://github.com/foldset/sdks.git",
|
|
24
|
+
"directory": "typescript/hono"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://docs.foldset.com",
|
|
27
|
+
"keywords": [
|
|
28
|
+
"foldset",
|
|
29
|
+
"hono",
|
|
30
|
+
"middleware",
|
|
31
|
+
"micropayments",
|
|
32
|
+
"ai",
|
|
33
|
+
"agents"
|
|
34
|
+
],
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@foldset/core": "0.1.0"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"hono": ">=4"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"hono": "^4.11.8",
|
|
43
|
+
"typescript": "^5.9.3"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "tsc",
|
|
47
|
+
"typecheck": "tsc --noEmit"
|
|
48
|
+
}
|
|
49
|
+
}
|