@4mica/sdk-next 1.3.1

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,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 4mica Network
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # @4mica/sdk-next
2
+
3
+ Next.js App Router helpers for gating routes behind a [4Mica](https://4mica.io) x402 payment.
4
+
5
+ These helpers are **Web-standard and edge-safe** — `NextRequest`/`NextResponse` extend the Web
6
+ `Request`/`Response`, so no `next` import is needed and they run on both the Edge and Node runtimes.
7
+
8
+ ## Install
9
+
10
+ ```bash
11
+ pnpm add @4mica/sdk-next @4mica/sdk-node
12
+ ```
13
+
14
+ ## Route handler
15
+
16
+ ```ts
17
+ // app/api/protected/route.ts
18
+ import { withPaywall } from "@4mica/sdk-next";
19
+ import { createClient } from "@4mica/sdk-node";
20
+
21
+ const client = await createClient(); // reads 4MICA_* env
22
+
23
+ export const GET = withPaywall(
24
+ async () => Response.json({ data: "premium content" }),
25
+ client.rpc,
26
+ {
27
+ payTo: "0x…",
28
+ asset: "0x0000000000000000000000000000000000000000",
29
+ network: "base-sepolia",
30
+ amount: "1000",
31
+ tabEndpoint: "https://your-recipient.example/tab",
32
+ },
33
+ );
34
+ ```
35
+
36
+ No `X-PAYMENT` header → `402` with the x402 payment requirements. Valid payment → your handler runs
37
+ and `X-PAYMENT-RESPONSE` is added to the response.
38
+
39
+ ## Middleware
40
+
41
+ ```ts
42
+ // middleware.ts
43
+ import { NextResponse } from "next/server";
44
+ import { paywallMiddleware } from "@4mica/sdk-next";
45
+ import { createClient } from "@4mica/sdk-node";
46
+
47
+ const client = await createClient();
48
+ const gate = paywallMiddleware(client.rpc, {
49
+ payTo: "0x…",
50
+ asset: "0x0000000000000000000000000000000000000000",
51
+ network: "base-sepolia",
52
+ amount: "1000",
53
+ tabEndpoint: "https://your-recipient.example/tab",
54
+ });
55
+
56
+ export const config = { matcher: "/api/protected/:path*" };
57
+
58
+ export async function middleware(request: Request) {
59
+ return (await gate(request)) ?? NextResponse.next();
60
+ }
61
+ ```
62
+
63
+ ## Notes
64
+
65
+ - The paywall only **verifies** payment (issues a guarantee). On-chain settlement (`remunerate`) is
66
+ an out-of-band recipient operation.
67
+ - All heavy lifting lives in `@4mica/sdk/server`'s runtime-neutral `createPaywall`; this package is
68
+ a thin mapping to Next's route/middleware shapes.
package/dist/index.cjs ADDED
@@ -0,0 +1,31 @@
1
+ 'use strict';
2
+
3
+ var server = require('@4mica/sdk/server');
4
+
5
+ // src/index.ts
6
+ function withPaywall(handler, verifier, config) {
7
+ const pw = server.createPaywall(verifier, config);
8
+ return async (request, context) => {
9
+ const result = await pw.handle(request);
10
+ if (result instanceof Response) {
11
+ return result;
12
+ }
13
+ const response = await handler(request, context);
14
+ result.headers.forEach((value, key) => {
15
+ response.headers.set(key, value);
16
+ });
17
+ return response;
18
+ };
19
+ }
20
+ function paywallMiddleware(verifier, config) {
21
+ const pw = server.createPaywall(verifier, config);
22
+ return async (request) => {
23
+ const result = await pw.handle(request);
24
+ return result instanceof Response ? result : void 0;
25
+ };
26
+ }
27
+
28
+ exports.paywallMiddleware = paywallMiddleware;
29
+ exports.withPaywall = withPaywall;
30
+ //# sourceMappingURL=index.cjs.map
31
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":["createPaywall"],"mappings":";;;;;AA+BO,SAAS,WAAA,CACd,OAAA,EACA,QAAA,EACA,MAAA,EACc;AACd,EAAA,MAAM,EAAA,GAAKA,oBAAA,CAAc,QAAA,EAAU,MAAM,CAAA;AACzC,EAAA,OAAO,OAAO,SAAS,OAAA,KAAY;AACjC,IAAA,MAAM,MAAA,GAAS,MAAM,EAAA,CAAG,MAAA,CAAO,OAAO,CAAA;AACtC,IAAA,IAAI,kBAAkB,QAAA,EAAU;AAC9B,MAAA,OAAO,MAAA;AAAA,IACT;AACA,IAAA,MAAM,QAAA,GAAW,MAAM,OAAA,CAAQ,OAAA,EAAS,OAAO,CAAA;AAC/C,IAAA,MAAA,CAAO,OAAA,CAAQ,OAAA,CAAQ,CAAC,KAAA,EAAO,GAAA,KAAQ;AACrC,MAAA,QAAA,CAAS,OAAA,CAAQ,GAAA,CAAI,GAAA,EAAK,KAAK,CAAA;AAAA,IACjC,CAAC,CAAA;AACD,IAAA,OAAO,QAAA;AAAA,EACT,CAAA;AACF;AAgBO,SAAS,iBAAA,CACd,UACA,MAAA,EACqD;AACrD,EAAA,MAAM,EAAA,GAAKA,oBAAA,CAAc,QAAA,EAAU,MAAM,CAAA;AACzC,EAAA,OAAO,OAAO,OAAA,KAAY;AACxB,IAAA,MAAM,MAAA,GAAS,MAAM,EAAA,CAAG,MAAA,CAAO,OAAO,CAAA;AACtC,IAAA,OAAO,MAAA,YAAkB,WAAW,MAAA,GAAS,MAAA;AAAA,EAC/C,CAAA;AACF","file":"index.cjs","sourcesContent":["import type {\n PaywallConfig,\n PaywallGuarantee,\n PaywallVerifier,\n} from \"@4mica/sdk/server\";\nimport { createPaywall } from \"@4mica/sdk/server\";\n\nexport type { PaywallConfig, PaywallGuarantee, PaywallVerifier };\n\n/**\n * A Next.js App Router route handler. `NextRequest` extends the Web `Request`\n * and `NextResponse` extends `Response`, so these helpers are Web-standard and\n * edge-safe — no `next` import required.\n */\nexport type RouteHandler = (\n request: Request,\n context?: unknown,\n) => Response | Promise<Response>;\n\n/**\n * Wrap an App Router route handler so it is gated behind a 4Mica x402 payment.\n *\n * When no valid `X-PAYMENT` header is present, returns a `402` with the x402\n * payment requirements. Otherwise verifies the payment, runs `handler`, and\n * merges the `X-PAYMENT-RESPONSE` header onto its response.\n *\n * @example\n * ```ts\n * export const GET = withPaywall(async () => Response.json({ ok: true }), client.rpc, config);\n * ```\n */\nexport function withPaywall(\n handler: RouteHandler,\n verifier: PaywallVerifier,\n config: PaywallConfig,\n): RouteHandler {\n const pw = createPaywall(verifier, config);\n return async (request, context) => {\n const result = await pw.handle(request);\n if (result instanceof Response) {\n return result;\n }\n const response = await handler(request, context);\n result.headers.forEach((value, key) => {\n response.headers.set(key, value);\n });\n return response;\n };\n}\n\n/**\n * Build a `middleware.ts` handler that gates matched routes behind a payment.\n *\n * Returns a `402` `Response` when payment is required/invalid, or `undefined`\n * to let the request continue. Edge-safe — usable without `export const runtime\n * = \"nodejs\"` (Node runtime remains a supported fallback).\n *\n * @example\n * ```ts\n * // middleware.ts\n * const gate = paywallMiddleware(client.rpc, config);\n * export async function middleware(req: Request) { return (await gate(req)) ?? NextResponse.next(); }\n * ```\n */\nexport function paywallMiddleware(\n verifier: PaywallVerifier,\n config: PaywallConfig,\n): (request: Request) => Promise<Response | undefined> {\n const pw = createPaywall(verifier, config);\n return async (request) => {\n const result = await pw.handle(request);\n return result instanceof Response ? result : undefined;\n };\n}\n"]}
@@ -0,0 +1,39 @@
1
+ import { PaywallVerifier, PaywallConfig } from '@4mica/sdk/server';
2
+ export { PaywallConfig, PaywallGuarantee, PaywallVerifier } from '@4mica/sdk/server';
3
+
4
+ /**
5
+ * A Next.js App Router route handler. `NextRequest` extends the Web `Request`
6
+ * and `NextResponse` extends `Response`, so these helpers are Web-standard and
7
+ * edge-safe — no `next` import required.
8
+ */
9
+ type RouteHandler = (request: Request, context?: unknown) => Response | Promise<Response>;
10
+ /**
11
+ * Wrap an App Router route handler so it is gated behind a 4Mica x402 payment.
12
+ *
13
+ * When no valid `X-PAYMENT` header is present, returns a `402` with the x402
14
+ * payment requirements. Otherwise verifies the payment, runs `handler`, and
15
+ * merges the `X-PAYMENT-RESPONSE` header onto its response.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * export const GET = withPaywall(async () => Response.json({ ok: true }), client.rpc, config);
20
+ * ```
21
+ */
22
+ declare function withPaywall(handler: RouteHandler, verifier: PaywallVerifier, config: PaywallConfig): RouteHandler;
23
+ /**
24
+ * Build a `middleware.ts` handler that gates matched routes behind a payment.
25
+ *
26
+ * Returns a `402` `Response` when payment is required/invalid, or `undefined`
27
+ * to let the request continue. Edge-safe — usable without `export const runtime
28
+ * = "nodejs"` (Node runtime remains a supported fallback).
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * // middleware.ts
33
+ * const gate = paywallMiddleware(client.rpc, config);
34
+ * export async function middleware(req: Request) { return (await gate(req)) ?? NextResponse.next(); }
35
+ * ```
36
+ */
37
+ declare function paywallMiddleware(verifier: PaywallVerifier, config: PaywallConfig): (request: Request) => Promise<Response | undefined>;
38
+
39
+ export { type RouteHandler, paywallMiddleware, withPaywall };
@@ -0,0 +1,39 @@
1
+ import { PaywallVerifier, PaywallConfig } from '@4mica/sdk/server';
2
+ export { PaywallConfig, PaywallGuarantee, PaywallVerifier } from '@4mica/sdk/server';
3
+
4
+ /**
5
+ * A Next.js App Router route handler. `NextRequest` extends the Web `Request`
6
+ * and `NextResponse` extends `Response`, so these helpers are Web-standard and
7
+ * edge-safe — no `next` import required.
8
+ */
9
+ type RouteHandler = (request: Request, context?: unknown) => Response | Promise<Response>;
10
+ /**
11
+ * Wrap an App Router route handler so it is gated behind a 4Mica x402 payment.
12
+ *
13
+ * When no valid `X-PAYMENT` header is present, returns a `402` with the x402
14
+ * payment requirements. Otherwise verifies the payment, runs `handler`, and
15
+ * merges the `X-PAYMENT-RESPONSE` header onto its response.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * export const GET = withPaywall(async () => Response.json({ ok: true }), client.rpc, config);
20
+ * ```
21
+ */
22
+ declare function withPaywall(handler: RouteHandler, verifier: PaywallVerifier, config: PaywallConfig): RouteHandler;
23
+ /**
24
+ * Build a `middleware.ts` handler that gates matched routes behind a payment.
25
+ *
26
+ * Returns a `402` `Response` when payment is required/invalid, or `undefined`
27
+ * to let the request continue. Edge-safe — usable without `export const runtime
28
+ * = "nodejs"` (Node runtime remains a supported fallback).
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * // middleware.ts
33
+ * const gate = paywallMiddleware(client.rpc, config);
34
+ * export async function middleware(req: Request) { return (await gate(req)) ?? NextResponse.next(); }
35
+ * ```
36
+ */
37
+ declare function paywallMiddleware(verifier: PaywallVerifier, config: PaywallConfig): (request: Request) => Promise<Response | undefined>;
38
+
39
+ export { type RouteHandler, paywallMiddleware, withPaywall };
package/dist/index.js ADDED
@@ -0,0 +1,28 @@
1
+ import { createPaywall } from '@4mica/sdk/server';
2
+
3
+ // src/index.ts
4
+ function withPaywall(handler, verifier, config) {
5
+ const pw = createPaywall(verifier, config);
6
+ return async (request, context) => {
7
+ const result = await pw.handle(request);
8
+ if (result instanceof Response) {
9
+ return result;
10
+ }
11
+ const response = await handler(request, context);
12
+ result.headers.forEach((value, key) => {
13
+ response.headers.set(key, value);
14
+ });
15
+ return response;
16
+ };
17
+ }
18
+ function paywallMiddleware(verifier, config) {
19
+ const pw = createPaywall(verifier, config);
20
+ return async (request) => {
21
+ const result = await pw.handle(request);
22
+ return result instanceof Response ? result : void 0;
23
+ };
24
+ }
25
+
26
+ export { paywallMiddleware, withPaywall };
27
+ //# sourceMappingURL=index.js.map
28
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;AA+BO,SAAS,WAAA,CACd,OAAA,EACA,QAAA,EACA,MAAA,EACc;AACd,EAAA,MAAM,EAAA,GAAK,aAAA,CAAc,QAAA,EAAU,MAAM,CAAA;AACzC,EAAA,OAAO,OAAO,SAAS,OAAA,KAAY;AACjC,IAAA,MAAM,MAAA,GAAS,MAAM,EAAA,CAAG,MAAA,CAAO,OAAO,CAAA;AACtC,IAAA,IAAI,kBAAkB,QAAA,EAAU;AAC9B,MAAA,OAAO,MAAA;AAAA,IACT;AACA,IAAA,MAAM,QAAA,GAAW,MAAM,OAAA,CAAQ,OAAA,EAAS,OAAO,CAAA;AAC/C,IAAA,MAAA,CAAO,OAAA,CAAQ,OAAA,CAAQ,CAAC,KAAA,EAAO,GAAA,KAAQ;AACrC,MAAA,QAAA,CAAS,OAAA,CAAQ,GAAA,CAAI,GAAA,EAAK,KAAK,CAAA;AAAA,IACjC,CAAC,CAAA;AACD,IAAA,OAAO,QAAA;AAAA,EACT,CAAA;AACF;AAgBO,SAAS,iBAAA,CACd,UACA,MAAA,EACqD;AACrD,EAAA,MAAM,EAAA,GAAK,aAAA,CAAc,QAAA,EAAU,MAAM,CAAA;AACzC,EAAA,OAAO,OAAO,OAAA,KAAY;AACxB,IAAA,MAAM,MAAA,GAAS,MAAM,EAAA,CAAG,MAAA,CAAO,OAAO,CAAA;AACtC,IAAA,OAAO,MAAA,YAAkB,WAAW,MAAA,GAAS,MAAA;AAAA,EAC/C,CAAA;AACF","file":"index.js","sourcesContent":["import type {\n PaywallConfig,\n PaywallGuarantee,\n PaywallVerifier,\n} from \"@4mica/sdk/server\";\nimport { createPaywall } from \"@4mica/sdk/server\";\n\nexport type { PaywallConfig, PaywallGuarantee, PaywallVerifier };\n\n/**\n * A Next.js App Router route handler. `NextRequest` extends the Web `Request`\n * and `NextResponse` extends `Response`, so these helpers are Web-standard and\n * edge-safe — no `next` import required.\n */\nexport type RouteHandler = (\n request: Request,\n context?: unknown,\n) => Response | Promise<Response>;\n\n/**\n * Wrap an App Router route handler so it is gated behind a 4Mica x402 payment.\n *\n * When no valid `X-PAYMENT` header is present, returns a `402` with the x402\n * payment requirements. Otherwise verifies the payment, runs `handler`, and\n * merges the `X-PAYMENT-RESPONSE` header onto its response.\n *\n * @example\n * ```ts\n * export const GET = withPaywall(async () => Response.json({ ok: true }), client.rpc, config);\n * ```\n */\nexport function withPaywall(\n handler: RouteHandler,\n verifier: PaywallVerifier,\n config: PaywallConfig,\n): RouteHandler {\n const pw = createPaywall(verifier, config);\n return async (request, context) => {\n const result = await pw.handle(request);\n if (result instanceof Response) {\n return result;\n }\n const response = await handler(request, context);\n result.headers.forEach((value, key) => {\n response.headers.set(key, value);\n });\n return response;\n };\n}\n\n/**\n * Build a `middleware.ts` handler that gates matched routes behind a payment.\n *\n * Returns a `402` `Response` when payment is required/invalid, or `undefined`\n * to let the request continue. Edge-safe — usable without `export const runtime\n * = \"nodejs\"` (Node runtime remains a supported fallback).\n *\n * @example\n * ```ts\n * // middleware.ts\n * const gate = paywallMiddleware(client.rpc, config);\n * export async function middleware(req: Request) { return (await gate(req)) ?? NextResponse.next(); }\n * ```\n */\nexport function paywallMiddleware(\n verifier: PaywallVerifier,\n config: PaywallConfig,\n): (request: Request) => Promise<Response | undefined> {\n const pw = createPaywall(verifier, config);\n return async (request) => {\n const result = await pw.handle(request);\n return result instanceof Response ? result : undefined;\n };\n}\n"]}
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@4mica/sdk-next",
3
+ "version": "1.3.1",
4
+ "description": "Next.js App Router helpers for gating routes behind a 4Mica x402 payment",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/4mica-Network/homepage.git",
10
+ "directory": "packages/sdk-next"
11
+ },
12
+ "sideEffects": false,
13
+ "main": "./dist/index.cjs",
14
+ "module": "./dist/index.js",
15
+ "types": "./dist/index.d.ts",
16
+ "exports": {
17
+ ".": {
18
+ "import": {
19
+ "types": "./dist/index.d.ts",
20
+ "default": "./dist/index.js"
21
+ },
22
+ "require": {
23
+ "types": "./dist/index.d.cts",
24
+ "default": "./dist/index.cjs"
25
+ }
26
+ },
27
+ "./package.json": "./package.json"
28
+ },
29
+ "files": [
30
+ "dist"
31
+ ],
32
+ "engines": {
33
+ "node": ">=18"
34
+ },
35
+ "publishConfig": {
36
+ "access": "public"
37
+ },
38
+ "keywords": [
39
+ "4mica",
40
+ "sdk",
41
+ "next",
42
+ "nextjs",
43
+ "x402",
44
+ "payments"
45
+ ],
46
+ "dependencies": {
47
+ "@4mica/sdk": "1.3.1"
48
+ },
49
+ "peerDependencies": {
50
+ "next": ">=14"
51
+ },
52
+ "devDependencies": {
53
+ "@arethetypeswrong/cli": "^0.18.2",
54
+ "@types/node": "^24.10.1",
55
+ "publint": "^0.3.14",
56
+ "tsup": "^8.5.1",
57
+ "typescript": "^5.9.0",
58
+ "vitest": "^4.1.8",
59
+ "@4mica/tsconfig": "0.0.0"
60
+ },
61
+ "scripts": {
62
+ "build": "tsup",
63
+ "dev": "tsup --watch",
64
+ "clean": "rm -rf dist",
65
+ "typecheck": "tsc --noEmit",
66
+ "test": "vitest run --passWithNoTests",
67
+ "check:exports": "attw --pack . --profile node16 && publint --strict"
68
+ }
69
+ }