@closeloop/sdk 0.1.7 → 0.1.9
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/README.md +18 -234
- package/dist/index.d.mts +843 -2
- package/dist/index.d.ts +843 -2
- package/dist/index.js +105 -103
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +105 -103
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -19
- package/dist/errors-CNnLzjDZ.d.mts +0 -901
- package/dist/errors-CNnLzjDZ.d.ts +0 -901
- package/dist/nextjs.d.mts +0 -135
- package/dist/nextjs.d.ts +0 -135
- package/dist/nextjs.js +0 -952
- package/dist/nextjs.js.map +0 -1
- package/dist/nextjs.mjs +0 -917
- package/dist/nextjs.mjs.map +0 -1
package/dist/nextjs.d.mts
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
-
import { C as CloseLoop } from './errors-CNnLzjDZ.mjs';
|
|
3
|
-
export { A as AuthenticationError, c as CloseLoopError, a as CloseLoopOptions, d as CreditsExpiredError, I as InsufficientCreditsError, N as NetworkError, e as NotFoundError, R as RateLimitError, f as ValidationError } from './errors-CNnLzjDZ.mjs';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Options for the creditGate middleware
|
|
7
|
-
*/
|
|
8
|
-
interface CreditGateOptions {
|
|
9
|
-
/**
|
|
10
|
-
* CloseLoop SDK instance
|
|
11
|
-
*/
|
|
12
|
-
client: CloseLoop;
|
|
13
|
-
/**
|
|
14
|
-
* Plan ID to check credits for
|
|
15
|
-
*/
|
|
16
|
-
planId: string;
|
|
17
|
-
/**
|
|
18
|
-
* Number of credits to consume per request
|
|
19
|
-
* @default 1
|
|
20
|
-
*/
|
|
21
|
-
creditsPerRequest?: number;
|
|
22
|
-
/**
|
|
23
|
-
* Function to extract wallet address from request
|
|
24
|
-
*/
|
|
25
|
-
getWalletAddress: (request: NextRequest) => string | null | Promise<string | null>;
|
|
26
|
-
/**
|
|
27
|
-
* Optional: Service identifier for consumption tracking
|
|
28
|
-
*/
|
|
29
|
-
consumedBy?: string;
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Next.js middleware to gate routes by credit balance.
|
|
33
|
-
*
|
|
34
|
-
* **IMPORTANT: Rate Limiting**
|
|
35
|
-
*
|
|
36
|
-
* This middleware does NOT include rate limiting. You should combine it with
|
|
37
|
-
* a rate limiter to prevent abuse. We recommend using `@upstash/ratelimit`
|
|
38
|
-
* or a similar solution.
|
|
39
|
-
*
|
|
40
|
-
* @example
|
|
41
|
-
* ```typescript
|
|
42
|
-
* // middleware.ts - Basic usage
|
|
43
|
-
* import { CloseLoop, creditGate } from "@closeloop/sdk/nextjs"
|
|
44
|
-
*
|
|
45
|
-
* const client = new CloseLoop({ apiKey: process.env.CLOSELOOP_API_KEY! })
|
|
46
|
-
*
|
|
47
|
-
* export default creditGate({
|
|
48
|
-
* client,
|
|
49
|
-
* planId: "plan_abc123",
|
|
50
|
-
* creditsPerRequest: 1,
|
|
51
|
-
* getWalletAddress: (req) => req.headers.get("x-wallet-address")
|
|
52
|
-
* })
|
|
53
|
-
*
|
|
54
|
-
* export const config = {
|
|
55
|
-
* matcher: ["/api/ai/:path*"]
|
|
56
|
-
* }
|
|
57
|
-
* ```
|
|
58
|
-
*
|
|
59
|
-
* @example
|
|
60
|
-
* ```typescript
|
|
61
|
-
* // middleware.ts - With rate limiting (RECOMMENDED)
|
|
62
|
-
* import { CloseLoop, creditGate } from "@closeloop/sdk/nextjs"
|
|
63
|
-
* import { Ratelimit } from "@upstash/ratelimit"
|
|
64
|
-
* import { Redis } from "@upstash/redis"
|
|
65
|
-
*
|
|
66
|
-
* const client = new CloseLoop({ apiKey: process.env.CLOSELOOP_API_KEY! })
|
|
67
|
-
*
|
|
68
|
-
* const ratelimit = new Ratelimit({
|
|
69
|
-
* redis: Redis.fromEnv(),
|
|
70
|
-
* limiter: Ratelimit.slidingWindow(10, "10 s"),
|
|
71
|
-
* })
|
|
72
|
-
*
|
|
73
|
-
* const creditGateMiddleware = creditGate({
|
|
74
|
-
* client,
|
|
75
|
-
* planId: "plan_abc123",
|
|
76
|
-
* creditsPerRequest: 1,
|
|
77
|
-
* getWalletAddress: (req) => req.headers.get("x-wallet-address")
|
|
78
|
-
* })
|
|
79
|
-
*
|
|
80
|
-
* export default async function middleware(request: NextRequest) {
|
|
81
|
-
* const ip = request.ip ?? "127.0.0.1"
|
|
82
|
-
* const { success } = await ratelimit.limit(ip)
|
|
83
|
-
*
|
|
84
|
-
* if (!success) {
|
|
85
|
-
* return NextResponse.json({ error: "Rate limit exceeded" }, { status: 429 })
|
|
86
|
-
* }
|
|
87
|
-
*
|
|
88
|
-
* return creditGateMiddleware(request)
|
|
89
|
-
* }
|
|
90
|
-
*
|
|
91
|
-
* export const config = {
|
|
92
|
-
* matcher: ["/api/protected/:path*"]
|
|
93
|
-
* }
|
|
94
|
-
* ```
|
|
95
|
-
*/
|
|
96
|
-
declare function creditGate(options: CreditGateOptions): (request: NextRequest) => Promise<NextResponse>;
|
|
97
|
-
/**
|
|
98
|
-
* Utility to consume credits after a request is processed.
|
|
99
|
-
* Use this in your API route handler after successful processing.
|
|
100
|
-
*
|
|
101
|
-
* **Security Note**: Always validate the wallet address and ensure the user
|
|
102
|
-
* is authenticated before consuming credits.
|
|
103
|
-
*
|
|
104
|
-
* @example
|
|
105
|
-
* ```typescript
|
|
106
|
-
* // app/api/ai/route.ts
|
|
107
|
-
* import { consumeCreditsAfterRequest } from "@closeloop/sdk/nextjs"
|
|
108
|
-
*
|
|
109
|
-
* export async function POST(request: NextRequest) {
|
|
110
|
-
* const wallet = request.headers.get("x-wallet-address")!
|
|
111
|
-
*
|
|
112
|
-
* // Process request...
|
|
113
|
-
* const result = await processAIRequest()
|
|
114
|
-
*
|
|
115
|
-
* // Consume credit after successful processing
|
|
116
|
-
* await consumeCreditsAfterRequest(client, {
|
|
117
|
-
* walletAddress: wallet,
|
|
118
|
-
* planId: "plan_abc123",
|
|
119
|
-
* amount: 1,
|
|
120
|
-
* consumedBy: "ai-text-generation"
|
|
121
|
-
* })
|
|
122
|
-
*
|
|
123
|
-
* return NextResponse.json(result)
|
|
124
|
-
* }
|
|
125
|
-
* ```
|
|
126
|
-
*/
|
|
127
|
-
declare function consumeCreditsAfterRequest(client: CloseLoop, params: {
|
|
128
|
-
walletAddress: string;
|
|
129
|
-
planId: string;
|
|
130
|
-
amount: number;
|
|
131
|
-
consumedBy?: string;
|
|
132
|
-
metadata?: Record<string, unknown>;
|
|
133
|
-
}): Promise<void>;
|
|
134
|
-
|
|
135
|
-
export { CloseLoop, type CreditGateOptions, consumeCreditsAfterRequest, creditGate };
|
package/dist/nextjs.d.ts
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
-
import { C as CloseLoop } from './errors-CNnLzjDZ.js';
|
|
3
|
-
export { A as AuthenticationError, c as CloseLoopError, a as CloseLoopOptions, d as CreditsExpiredError, I as InsufficientCreditsError, N as NetworkError, e as NotFoundError, R as RateLimitError, f as ValidationError } from './errors-CNnLzjDZ.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Options for the creditGate middleware
|
|
7
|
-
*/
|
|
8
|
-
interface CreditGateOptions {
|
|
9
|
-
/**
|
|
10
|
-
* CloseLoop SDK instance
|
|
11
|
-
*/
|
|
12
|
-
client: CloseLoop;
|
|
13
|
-
/**
|
|
14
|
-
* Plan ID to check credits for
|
|
15
|
-
*/
|
|
16
|
-
planId: string;
|
|
17
|
-
/**
|
|
18
|
-
* Number of credits to consume per request
|
|
19
|
-
* @default 1
|
|
20
|
-
*/
|
|
21
|
-
creditsPerRequest?: number;
|
|
22
|
-
/**
|
|
23
|
-
* Function to extract wallet address from request
|
|
24
|
-
*/
|
|
25
|
-
getWalletAddress: (request: NextRequest) => string | null | Promise<string | null>;
|
|
26
|
-
/**
|
|
27
|
-
* Optional: Service identifier for consumption tracking
|
|
28
|
-
*/
|
|
29
|
-
consumedBy?: string;
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Next.js middleware to gate routes by credit balance.
|
|
33
|
-
*
|
|
34
|
-
* **IMPORTANT: Rate Limiting**
|
|
35
|
-
*
|
|
36
|
-
* This middleware does NOT include rate limiting. You should combine it with
|
|
37
|
-
* a rate limiter to prevent abuse. We recommend using `@upstash/ratelimit`
|
|
38
|
-
* or a similar solution.
|
|
39
|
-
*
|
|
40
|
-
* @example
|
|
41
|
-
* ```typescript
|
|
42
|
-
* // middleware.ts - Basic usage
|
|
43
|
-
* import { CloseLoop, creditGate } from "@closeloop/sdk/nextjs"
|
|
44
|
-
*
|
|
45
|
-
* const client = new CloseLoop({ apiKey: process.env.CLOSELOOP_API_KEY! })
|
|
46
|
-
*
|
|
47
|
-
* export default creditGate({
|
|
48
|
-
* client,
|
|
49
|
-
* planId: "plan_abc123",
|
|
50
|
-
* creditsPerRequest: 1,
|
|
51
|
-
* getWalletAddress: (req) => req.headers.get("x-wallet-address")
|
|
52
|
-
* })
|
|
53
|
-
*
|
|
54
|
-
* export const config = {
|
|
55
|
-
* matcher: ["/api/ai/:path*"]
|
|
56
|
-
* }
|
|
57
|
-
* ```
|
|
58
|
-
*
|
|
59
|
-
* @example
|
|
60
|
-
* ```typescript
|
|
61
|
-
* // middleware.ts - With rate limiting (RECOMMENDED)
|
|
62
|
-
* import { CloseLoop, creditGate } from "@closeloop/sdk/nextjs"
|
|
63
|
-
* import { Ratelimit } from "@upstash/ratelimit"
|
|
64
|
-
* import { Redis } from "@upstash/redis"
|
|
65
|
-
*
|
|
66
|
-
* const client = new CloseLoop({ apiKey: process.env.CLOSELOOP_API_KEY! })
|
|
67
|
-
*
|
|
68
|
-
* const ratelimit = new Ratelimit({
|
|
69
|
-
* redis: Redis.fromEnv(),
|
|
70
|
-
* limiter: Ratelimit.slidingWindow(10, "10 s"),
|
|
71
|
-
* })
|
|
72
|
-
*
|
|
73
|
-
* const creditGateMiddleware = creditGate({
|
|
74
|
-
* client,
|
|
75
|
-
* planId: "plan_abc123",
|
|
76
|
-
* creditsPerRequest: 1,
|
|
77
|
-
* getWalletAddress: (req) => req.headers.get("x-wallet-address")
|
|
78
|
-
* })
|
|
79
|
-
*
|
|
80
|
-
* export default async function middleware(request: NextRequest) {
|
|
81
|
-
* const ip = request.ip ?? "127.0.0.1"
|
|
82
|
-
* const { success } = await ratelimit.limit(ip)
|
|
83
|
-
*
|
|
84
|
-
* if (!success) {
|
|
85
|
-
* return NextResponse.json({ error: "Rate limit exceeded" }, { status: 429 })
|
|
86
|
-
* }
|
|
87
|
-
*
|
|
88
|
-
* return creditGateMiddleware(request)
|
|
89
|
-
* }
|
|
90
|
-
*
|
|
91
|
-
* export const config = {
|
|
92
|
-
* matcher: ["/api/protected/:path*"]
|
|
93
|
-
* }
|
|
94
|
-
* ```
|
|
95
|
-
*/
|
|
96
|
-
declare function creditGate(options: CreditGateOptions): (request: NextRequest) => Promise<NextResponse>;
|
|
97
|
-
/**
|
|
98
|
-
* Utility to consume credits after a request is processed.
|
|
99
|
-
* Use this in your API route handler after successful processing.
|
|
100
|
-
*
|
|
101
|
-
* **Security Note**: Always validate the wallet address and ensure the user
|
|
102
|
-
* is authenticated before consuming credits.
|
|
103
|
-
*
|
|
104
|
-
* @example
|
|
105
|
-
* ```typescript
|
|
106
|
-
* // app/api/ai/route.ts
|
|
107
|
-
* import { consumeCreditsAfterRequest } from "@closeloop/sdk/nextjs"
|
|
108
|
-
*
|
|
109
|
-
* export async function POST(request: NextRequest) {
|
|
110
|
-
* const wallet = request.headers.get("x-wallet-address")!
|
|
111
|
-
*
|
|
112
|
-
* // Process request...
|
|
113
|
-
* const result = await processAIRequest()
|
|
114
|
-
*
|
|
115
|
-
* // Consume credit after successful processing
|
|
116
|
-
* await consumeCreditsAfterRequest(client, {
|
|
117
|
-
* walletAddress: wallet,
|
|
118
|
-
* planId: "plan_abc123",
|
|
119
|
-
* amount: 1,
|
|
120
|
-
* consumedBy: "ai-text-generation"
|
|
121
|
-
* })
|
|
122
|
-
*
|
|
123
|
-
* return NextResponse.json(result)
|
|
124
|
-
* }
|
|
125
|
-
* ```
|
|
126
|
-
*/
|
|
127
|
-
declare function consumeCreditsAfterRequest(client: CloseLoop, params: {
|
|
128
|
-
walletAddress: string;
|
|
129
|
-
planId: string;
|
|
130
|
-
amount: number;
|
|
131
|
-
consumedBy?: string;
|
|
132
|
-
metadata?: Record<string, unknown>;
|
|
133
|
-
}): Promise<void>;
|
|
134
|
-
|
|
135
|
-
export { CloseLoop, type CreditGateOptions, consumeCreditsAfterRequest, creditGate };
|