@proxylayer/cli 0.0.0-stage → 0.1.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/README.md +70 -2
- package/dist/cli.d.ts +10 -0
- package/dist/cli.js +640 -0
- package/dist/client.d.ts +264 -0
- package/dist/client.js +173 -0
- package/dist/config.d.ts +21 -0
- package/dist/config.js +69 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/mcp.d.ts +9 -0
- package/dist/mcp.js +127 -0
- package/package.json +40 -4
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
export type FixedProductSlug = "isp" | "datacenter" | "mobile";
|
|
2
|
+
export type ProductSlug = FixedProductSlug | "residential";
|
|
3
|
+
export type PaymentMethod = "stripe" | "crypto" | "balance";
|
|
4
|
+
export type ProxyStringFormat = "http://login:password@ip:port" | "https://login:password@ip:port" | "socks5://login:password@ip:port" | "ip:port:login:password";
|
|
5
|
+
export type FixedPurchaseItem = {
|
|
6
|
+
countryCode: string;
|
|
7
|
+
quantity: number;
|
|
8
|
+
targetProxyIds?: string[];
|
|
9
|
+
};
|
|
10
|
+
export type QuotePurchaseInput = {
|
|
11
|
+
productSlug: FixedProductSlug;
|
|
12
|
+
durationDays: number;
|
|
13
|
+
items: FixedPurchaseItem[];
|
|
14
|
+
} | {
|
|
15
|
+
productSlug: "residential";
|
|
16
|
+
trafficGb: number;
|
|
17
|
+
};
|
|
18
|
+
export type CheckoutPurchaseInput = QuotePurchaseInput | {
|
|
19
|
+
productSlug: "balance-topup";
|
|
20
|
+
amountCents: number;
|
|
21
|
+
};
|
|
22
|
+
export type CatalogResponse = {
|
|
23
|
+
currency: "USD";
|
|
24
|
+
products: Array<{
|
|
25
|
+
slug: ProductSlug;
|
|
26
|
+
type: string;
|
|
27
|
+
name: string;
|
|
28
|
+
billingMode: "FIXED" | "TRAFFIC";
|
|
29
|
+
durationsDays: number[];
|
|
30
|
+
countries: Array<{
|
|
31
|
+
code: string;
|
|
32
|
+
name: string;
|
|
33
|
+
}>;
|
|
34
|
+
prices: Array<{
|
|
35
|
+
ruleType: "BASE" | "COUNTRY" | "TRAFFIC_TIER" | "QUANTITY_TIER";
|
|
36
|
+
durationDays: number | null;
|
|
37
|
+
countryCode: string | null;
|
|
38
|
+
minQuantity: number | null;
|
|
39
|
+
maxQuantity: number | null;
|
|
40
|
+
minTrafficGb: number | null;
|
|
41
|
+
maxTrafficGb: number | null;
|
|
42
|
+
unitAmountCents: number;
|
|
43
|
+
packageAmountCents: number | null;
|
|
44
|
+
currency: "USD";
|
|
45
|
+
}>;
|
|
46
|
+
}>;
|
|
47
|
+
limits: {
|
|
48
|
+
maxProxyQuantity: number;
|
|
49
|
+
maxResidentialTrafficGb: number;
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
export type QuoteResponse = {
|
|
53
|
+
productSlug: ProductSlug;
|
|
54
|
+
productType: string;
|
|
55
|
+
productName: string;
|
|
56
|
+
currency: "USD";
|
|
57
|
+
amountCents: number;
|
|
58
|
+
durationDays: number | null;
|
|
59
|
+
quantity: number;
|
|
60
|
+
items: Array<{
|
|
61
|
+
countryCode: string | null;
|
|
62
|
+
quantity: number;
|
|
63
|
+
targetProxyIds: string[];
|
|
64
|
+
trafficGb: number | null;
|
|
65
|
+
unitAmountCents: number;
|
|
66
|
+
totalAmountCents: number;
|
|
67
|
+
}>;
|
|
68
|
+
};
|
|
69
|
+
export type OtpRequestResponse = {
|
|
70
|
+
sent: true;
|
|
71
|
+
email: string;
|
|
72
|
+
expiresInSeconds: number;
|
|
73
|
+
};
|
|
74
|
+
export type OtpVerifyResponse = {
|
|
75
|
+
apiKey: string;
|
|
76
|
+
keyId: string;
|
|
77
|
+
keyPrefix: string;
|
|
78
|
+
scopes: string[];
|
|
79
|
+
user: {
|
|
80
|
+
id: string;
|
|
81
|
+
email: string;
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
export type BalanceResponse = {
|
|
85
|
+
balanceCents: number;
|
|
86
|
+
balanceMajor: number;
|
|
87
|
+
};
|
|
88
|
+
export type CheckoutResponse = {
|
|
89
|
+
checkoutSessionId: string;
|
|
90
|
+
paymentMethod: PaymentMethod;
|
|
91
|
+
status: "pending" | "completed";
|
|
92
|
+
currency: "USD";
|
|
93
|
+
amountCents: number;
|
|
94
|
+
paymentUrl?: string | null;
|
|
95
|
+
expiresAt?: string | null;
|
|
96
|
+
orderId?: string | null;
|
|
97
|
+
balanceCents?: number | null;
|
|
98
|
+
crypto?: {
|
|
99
|
+
address: string;
|
|
100
|
+
payerAmount: string;
|
|
101
|
+
payerCurrency: string;
|
|
102
|
+
network: string;
|
|
103
|
+
expiresAt: number | null;
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
export type CheckoutStatusResponse = {
|
|
107
|
+
checkoutSessionId: string;
|
|
108
|
+
status: "pending" | "completed" | "failed" | "expired" | "cancelled" | "refunded";
|
|
109
|
+
provisioningPending: boolean;
|
|
110
|
+
orderId: string | null;
|
|
111
|
+
fulfillmentStatus: "PENDING" | "FULFILLED" | "FAILED" | "NEEDS_CHECK" | null;
|
|
112
|
+
failureMessage: string | null;
|
|
113
|
+
balanceCents: number | null;
|
|
114
|
+
detail?: "WAITING_CONFIRMATIONS" | "UNDERPAID";
|
|
115
|
+
paymentProvider?: string | null;
|
|
116
|
+
};
|
|
117
|
+
export type OrdersResponse = {
|
|
118
|
+
items: Array<{
|
|
119
|
+
historyKind?: "order" | "balance_top_up";
|
|
120
|
+
occurredAt: string;
|
|
121
|
+
id: string;
|
|
122
|
+
paymentStatus: "PENDING" | "PAID" | "PARTIALLY_REFUNDED" | "REFUNDED" | "CANCELLED";
|
|
123
|
+
fulfillmentStatus: "PENDING" | "FULFILLED" | "FAILED" | "NEEDS_CHECK";
|
|
124
|
+
currency: "USD";
|
|
125
|
+
totalAmount: number;
|
|
126
|
+
subtotalAmount: number;
|
|
127
|
+
discountAmount: number;
|
|
128
|
+
taxAmount: number;
|
|
129
|
+
paymentMethod: {
|
|
130
|
+
provider: string;
|
|
131
|
+
method: "balance" | "card" | "paypal" | "crypto" | "stripe" | "unknown";
|
|
132
|
+
cardBrand?: string | null;
|
|
133
|
+
cardLast4?: string | null;
|
|
134
|
+
} | null;
|
|
135
|
+
items: Array<{
|
|
136
|
+
id: string;
|
|
137
|
+
kind: "balance_top_up" | null;
|
|
138
|
+
durationDays: number | null;
|
|
139
|
+
quantity: number;
|
|
140
|
+
trafficGb: number | null;
|
|
141
|
+
unitAmount: number;
|
|
142
|
+
totalAmount: number;
|
|
143
|
+
countryCode: string | null;
|
|
144
|
+
fulfillmentStatus?: "PENDING" | "FULFILLED" | "FAILED" | "NEEDS_CHECK";
|
|
145
|
+
failureReason?: string | null;
|
|
146
|
+
fulfilledAt?: string | null;
|
|
147
|
+
product: {
|
|
148
|
+
name: string;
|
|
149
|
+
type: string;
|
|
150
|
+
};
|
|
151
|
+
}>;
|
|
152
|
+
}>;
|
|
153
|
+
nextCursor: string | null;
|
|
154
|
+
};
|
|
155
|
+
export type StaticProxiesResponse = {
|
|
156
|
+
data: Array<{
|
|
157
|
+
id: string;
|
|
158
|
+
product: FixedProductSlug;
|
|
159
|
+
countryCode: string;
|
|
160
|
+
country: string;
|
|
161
|
+
host: string;
|
|
162
|
+
port: number;
|
|
163
|
+
username: string;
|
|
164
|
+
password: string;
|
|
165
|
+
protocol: string;
|
|
166
|
+
proxy: string;
|
|
167
|
+
purchasedAt: string;
|
|
168
|
+
expiresAt: string | null;
|
|
169
|
+
daysLeft: number;
|
|
170
|
+
note: string;
|
|
171
|
+
tags: string[];
|
|
172
|
+
}>;
|
|
173
|
+
pagination: {
|
|
174
|
+
nextCursor: string | null;
|
|
175
|
+
};
|
|
176
|
+
pendingCheckout?: {
|
|
177
|
+
checkoutSessionId: string;
|
|
178
|
+
quantity: number;
|
|
179
|
+
checkingPayment?: boolean;
|
|
180
|
+
};
|
|
181
|
+
provisioningOrders?: Array<{
|
|
182
|
+
orderId: string;
|
|
183
|
+
quantity: number;
|
|
184
|
+
fulfillmentStatus: "PENDING" | "FULFILLED" | "FAILED" | "NEEDS_CHECK";
|
|
185
|
+
}>;
|
|
186
|
+
};
|
|
187
|
+
export type ResidentialProxiesResponse = {
|
|
188
|
+
trafficBalance: {
|
|
189
|
+
status: "UNPROVISIONED" | "FRESH" | "ERROR";
|
|
190
|
+
lastSyncedAt: string | null;
|
|
191
|
+
errorMessage: string | null;
|
|
192
|
+
balanceGb: number | null;
|
|
193
|
+
balanceFormatted: string | null;
|
|
194
|
+
totalTrafficGb: number | null;
|
|
195
|
+
totalTrafficFormatted: string | null;
|
|
196
|
+
usedTrafficGb: number | null;
|
|
197
|
+
usedTrafficFormatted: string | null;
|
|
198
|
+
};
|
|
199
|
+
format: ProxyStringFormat;
|
|
200
|
+
proxies: string[];
|
|
201
|
+
};
|
|
202
|
+
export type ProxyLayerOptions = {
|
|
203
|
+
apiKey?: string;
|
|
204
|
+
baseUrl?: string;
|
|
205
|
+
configPath?: string;
|
|
206
|
+
fetch?: typeof globalThis.fetch;
|
|
207
|
+
};
|
|
208
|
+
export declare class ProxyLayerError extends Error {
|
|
209
|
+
readonly status: number;
|
|
210
|
+
readonly code: string;
|
|
211
|
+
readonly issues?: unknown[];
|
|
212
|
+
constructor(message: string, status: number, code: string, issues?: unknown[]);
|
|
213
|
+
}
|
|
214
|
+
export declare class ProxyLayer {
|
|
215
|
+
private apiKeyOverride?;
|
|
216
|
+
private readonly baseUrlOverride?;
|
|
217
|
+
private readonly configPath?;
|
|
218
|
+
private readonly fetchFn;
|
|
219
|
+
constructor(options?: ProxyLayerOptions);
|
|
220
|
+
get baseUrl(): string;
|
|
221
|
+
getResolvedConfig(): import("./config.js").ResolvedConfig;
|
|
222
|
+
private request;
|
|
223
|
+
getCatalog(params?: {
|
|
224
|
+
product?: ProductSlug;
|
|
225
|
+
}): Promise<CatalogResponse>;
|
|
226
|
+
quote(purchase: QuotePurchaseInput): Promise<QuoteResponse>;
|
|
227
|
+
requestOtp(email: string): Promise<OtpRequestResponse>;
|
|
228
|
+
verifyOtp(input: {
|
|
229
|
+
email: string;
|
|
230
|
+
code: string;
|
|
231
|
+
keyName?: string;
|
|
232
|
+
access?: "read" | "full";
|
|
233
|
+
save?: boolean;
|
|
234
|
+
}): Promise<OtpVerifyResponse>;
|
|
235
|
+
getBalance(): Promise<BalanceResponse>;
|
|
236
|
+
createCheckout(input: {
|
|
237
|
+
purchase: CheckoutPurchaseInput;
|
|
238
|
+
paymentMethod?: PaymentMethod;
|
|
239
|
+
cryptoCurrency?: string;
|
|
240
|
+
cryptoNetwork?: string;
|
|
241
|
+
clientOperationId?: string;
|
|
242
|
+
}): Promise<CheckoutResponse>;
|
|
243
|
+
getCheckoutStatus(checkoutSessionId: string): Promise<CheckoutStatusResponse>;
|
|
244
|
+
listOrders(params?: {
|
|
245
|
+
limit?: number;
|
|
246
|
+
cursor?: string;
|
|
247
|
+
}): Promise<OrdersResponse>;
|
|
248
|
+
listStaticProxies(params: {
|
|
249
|
+
product: FixedProductSlug;
|
|
250
|
+
checkoutSessionId?: string;
|
|
251
|
+
limit?: number;
|
|
252
|
+
cursor?: string;
|
|
253
|
+
}): Promise<StaticProxiesResponse>;
|
|
254
|
+
getResidentialProxies(params?: {
|
|
255
|
+
country?: string;
|
|
256
|
+
city?: string;
|
|
257
|
+
region?: string;
|
|
258
|
+
asn?: string;
|
|
259
|
+
session?: "sticky" | "rotating";
|
|
260
|
+
ttl?: number;
|
|
261
|
+
quantity?: number;
|
|
262
|
+
format?: ProxyStringFormat;
|
|
263
|
+
}): Promise<ResidentialProxiesResponse>;
|
|
264
|
+
}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { DEFAULT_BASE_URL, resolveConfig, saveConfig } from "./config.js";
|
|
2
|
+
export class ProxyLayerError extends Error {
|
|
3
|
+
status;
|
|
4
|
+
code;
|
|
5
|
+
issues;
|
|
6
|
+
constructor(message, status, code, issues) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "ProxyLayerError";
|
|
9
|
+
this.status = status;
|
|
10
|
+
this.code = code;
|
|
11
|
+
this.issues = issues;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export class ProxyLayer {
|
|
15
|
+
apiKeyOverride;
|
|
16
|
+
baseUrlOverride;
|
|
17
|
+
configPath;
|
|
18
|
+
fetchFn;
|
|
19
|
+
constructor(options) {
|
|
20
|
+
this.apiKeyOverride = options?.apiKey?.trim() || undefined;
|
|
21
|
+
this.baseUrlOverride = options?.baseUrl?.trim() || undefined;
|
|
22
|
+
this.configPath = options?.configPath;
|
|
23
|
+
this.fetchFn = options?.fetch ?? globalThis.fetch;
|
|
24
|
+
}
|
|
25
|
+
get baseUrl() {
|
|
26
|
+
return resolveConfig({
|
|
27
|
+
apiKey: this.apiKeyOverride,
|
|
28
|
+
baseUrl: this.baseUrlOverride,
|
|
29
|
+
configPath: this.configPath,
|
|
30
|
+
}).baseUrl || DEFAULT_BASE_URL;
|
|
31
|
+
}
|
|
32
|
+
getResolvedConfig() {
|
|
33
|
+
return resolveConfig({
|
|
34
|
+
apiKey: this.apiKeyOverride,
|
|
35
|
+
baseUrl: this.baseUrlOverride,
|
|
36
|
+
configPath: this.configPath,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
async request(method, pathname, options) {
|
|
40
|
+
const config = this.getResolvedConfig();
|
|
41
|
+
const authMode = options?.auth ?? "required";
|
|
42
|
+
if (authMode === "required" && !config.apiKey) {
|
|
43
|
+
throw new ProxyLayerError("Missing ProxyLayer API key. Run `npx @proxylayer/cli login` or set PROXYLAYER_API_KEY.", 401, "UNAUTHORIZED");
|
|
44
|
+
}
|
|
45
|
+
const url = new URL(pathname, `${config.baseUrl}/`);
|
|
46
|
+
if (options?.query) {
|
|
47
|
+
for (const [key, value] of Object.entries(options.query)) {
|
|
48
|
+
if (value !== undefined)
|
|
49
|
+
url.searchParams.set(key, String(value));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const headers = {
|
|
53
|
+
Accept: "application/json",
|
|
54
|
+
...options?.headers,
|
|
55
|
+
};
|
|
56
|
+
if (authMode !== "none" && config.apiKey) {
|
|
57
|
+
headers.Authorization = `Bearer ${config.apiKey}`;
|
|
58
|
+
}
|
|
59
|
+
if (options?.body !== undefined) {
|
|
60
|
+
headers["Content-Type"] = "application/json";
|
|
61
|
+
}
|
|
62
|
+
const response = await this.fetchFn(url.toString(), {
|
|
63
|
+
method,
|
|
64
|
+
headers,
|
|
65
|
+
body: options?.body !== undefined ? JSON.stringify(options.body) : undefined,
|
|
66
|
+
});
|
|
67
|
+
const text = await response.text();
|
|
68
|
+
let parsed = null;
|
|
69
|
+
if (text) {
|
|
70
|
+
try {
|
|
71
|
+
parsed = JSON.parse(text);
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
parsed = null;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (!response.ok) {
|
|
78
|
+
const errorObj = (parsed && typeof parsed === "object" ? parsed.error : undefined);
|
|
79
|
+
const message = typeof errorObj?.message === "string"
|
|
80
|
+
? errorObj.message
|
|
81
|
+
: `ProxyLayer API request failed (${response.status})`;
|
|
82
|
+
const code = typeof errorObj?.code === "string" ? errorObj.code : "API_ERROR";
|
|
83
|
+
const issues = Array.isArray(errorObj?.issues) ? errorObj.issues : undefined;
|
|
84
|
+
throw new ProxyLayerError(message, response.status, code, issues);
|
|
85
|
+
}
|
|
86
|
+
return parsed;
|
|
87
|
+
}
|
|
88
|
+
getCatalog(params) {
|
|
89
|
+
return this.request("GET", "/api/v1/catalog", {
|
|
90
|
+
auth: "none",
|
|
91
|
+
query: { product: params?.product },
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
quote(purchase) {
|
|
95
|
+
return this.request("POST", "/api/v1/quotes", {
|
|
96
|
+
auth: "optional",
|
|
97
|
+
body: { purchase },
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
requestOtp(email) {
|
|
101
|
+
return this.request("POST", "/api/v1/auth/otp/request", {
|
|
102
|
+
auth: "none",
|
|
103
|
+
body: { email },
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
async verifyOtp(input) {
|
|
107
|
+
const result = await this.request("POST", "/api/v1/auth/otp/verify", {
|
|
108
|
+
auth: "none",
|
|
109
|
+
body: {
|
|
110
|
+
email: input.email,
|
|
111
|
+
code: input.code,
|
|
112
|
+
...(input.keyName ? { keyName: input.keyName } : {}),
|
|
113
|
+
...(input.access ? { access: input.access } : {}),
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
this.apiKeyOverride = result.apiKey;
|
|
117
|
+
if (input.save) {
|
|
118
|
+
saveConfig({
|
|
119
|
+
apiKey: result.apiKey,
|
|
120
|
+
email: result.user.email,
|
|
121
|
+
baseUrl: this.baseUrlOverride,
|
|
122
|
+
}, this.configPath);
|
|
123
|
+
}
|
|
124
|
+
return result;
|
|
125
|
+
}
|
|
126
|
+
getBalance() {
|
|
127
|
+
return this.request("GET", "/api/v1/balance");
|
|
128
|
+
}
|
|
129
|
+
createCheckout(input) {
|
|
130
|
+
return this.request("POST", "/api/v1/checkouts", {
|
|
131
|
+
body: {
|
|
132
|
+
purchase: input.purchase,
|
|
133
|
+
...(input.paymentMethod ? { paymentMethod: input.paymentMethod } : {}),
|
|
134
|
+
...(input.cryptoCurrency ? { cryptoCurrency: input.cryptoCurrency } : {}),
|
|
135
|
+
...(input.cryptoNetwork ? { cryptoNetwork: input.cryptoNetwork } : {}),
|
|
136
|
+
...(input.clientOperationId ? { clientOperationId: input.clientOperationId } : {}),
|
|
137
|
+
},
|
|
138
|
+
headers: input.clientOperationId ? { "Idempotency-Key": input.clientOperationId } : undefined,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
getCheckoutStatus(checkoutSessionId) {
|
|
142
|
+
return this.request("GET", `/api/v1/checkouts/${encodeURIComponent(checkoutSessionId)}`);
|
|
143
|
+
}
|
|
144
|
+
listOrders(params) {
|
|
145
|
+
return this.request("GET", "/api/v1/orders", {
|
|
146
|
+
query: { limit: params?.limit, cursor: params?.cursor },
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
listStaticProxies(params) {
|
|
150
|
+
return this.request("GET", "/api/v1/proxies", {
|
|
151
|
+
query: {
|
|
152
|
+
product: params.product,
|
|
153
|
+
checkoutSessionId: params.checkoutSessionId,
|
|
154
|
+
limit: params.limit,
|
|
155
|
+
cursor: params.cursor,
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
getResidentialProxies(params) {
|
|
160
|
+
return this.request("GET", "/api/v1/residential/proxies", {
|
|
161
|
+
query: {
|
|
162
|
+
country: params?.country,
|
|
163
|
+
city: params?.city,
|
|
164
|
+
region: params?.region,
|
|
165
|
+
asn: params?.asn,
|
|
166
|
+
session: params?.session,
|
|
167
|
+
ttl: params?.ttl,
|
|
168
|
+
quantity: params?.quantity,
|
|
169
|
+
format: params?.format,
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare const DEFAULT_BASE_URL = "https://app.proxy-layer.com";
|
|
2
|
+
export type StoredConfig = {
|
|
3
|
+
apiKey?: string;
|
|
4
|
+
email?: string;
|
|
5
|
+
baseUrl?: string;
|
|
6
|
+
};
|
|
7
|
+
export type ResolvedConfig = {
|
|
8
|
+
apiKey?: string;
|
|
9
|
+
email?: string;
|
|
10
|
+
baseUrl: string;
|
|
11
|
+
configPath: string;
|
|
12
|
+
};
|
|
13
|
+
export declare function getConfigPath(override?: string): string;
|
|
14
|
+
export declare function readConfig(configPath?: string): StoredConfig;
|
|
15
|
+
export declare function saveConfig(patch: StoredConfig, configPath?: string): string;
|
|
16
|
+
export declare function clearConfig(configPath?: string): boolean;
|
|
17
|
+
export declare function resolveConfig(overrides?: {
|
|
18
|
+
apiKey?: string;
|
|
19
|
+
baseUrl?: string;
|
|
20
|
+
configPath?: string;
|
|
21
|
+
}): ResolvedConfig;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
export const DEFAULT_BASE_URL = "https://app.proxy-layer.com";
|
|
5
|
+
export function getConfigPath(override) {
|
|
6
|
+
const custom = override?.trim() || process.env.PROXYLAYER_CONFIG_PATH?.trim();
|
|
7
|
+
if (custom)
|
|
8
|
+
return path.resolve(custom);
|
|
9
|
+
const baseDir = process.env.XDG_CONFIG_HOME?.trim() || path.join(os.homedir(), ".config");
|
|
10
|
+
return path.join(baseDir, "proxylayer", "config.json");
|
|
11
|
+
}
|
|
12
|
+
export function readConfig(configPath) {
|
|
13
|
+
const filePath = getConfigPath(configPath);
|
|
14
|
+
if (!existsSync(filePath))
|
|
15
|
+
return {};
|
|
16
|
+
try {
|
|
17
|
+
const raw = JSON.parse(readFileSync(filePath, "utf8"));
|
|
18
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
19
|
+
return {};
|
|
20
|
+
const record = raw;
|
|
21
|
+
return {
|
|
22
|
+
...(typeof record.apiKey === "string" && record.apiKey.trim() ? { apiKey: record.apiKey.trim() } : {}),
|
|
23
|
+
...(typeof record.email === "string" && record.email.trim() ? { email: record.email.trim() } : {}),
|
|
24
|
+
...(typeof record.baseUrl === "string" && record.baseUrl.trim() ? { baseUrl: record.baseUrl.trim() } : {}),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return {};
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export function saveConfig(patch, configPath) {
|
|
32
|
+
const filePath = getConfigPath(configPath);
|
|
33
|
+
const next = { ...readConfig(filePath) };
|
|
34
|
+
for (const key of ["apiKey", "email", "baseUrl"]) {
|
|
35
|
+
if (patch[key] !== undefined) {
|
|
36
|
+
const value = patch[key]?.trim();
|
|
37
|
+
if (value)
|
|
38
|
+
next[key] = value;
|
|
39
|
+
else
|
|
40
|
+
delete next[key];
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
mkdirSync(path.dirname(filePath), { recursive: true, mode: 0o700 });
|
|
44
|
+
writeFileSync(filePath, `${JSON.stringify(next, null, 2)}\n`, { mode: 0o600 });
|
|
45
|
+
chmodSync(filePath, 0o600);
|
|
46
|
+
return filePath;
|
|
47
|
+
}
|
|
48
|
+
export function clearConfig(configPath) {
|
|
49
|
+
const filePath = getConfigPath(configPath);
|
|
50
|
+
if (!existsSync(filePath))
|
|
51
|
+
return false;
|
|
52
|
+
rmSync(filePath, { force: true });
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
export function resolveConfig(overrides) {
|
|
56
|
+
const configPath = getConfigPath(overrides?.configPath);
|
|
57
|
+
const stored = readConfig(configPath);
|
|
58
|
+
const apiKey = overrides?.apiKey?.trim() || process.env.PROXYLAYER_API_KEY?.trim() || stored.apiKey;
|
|
59
|
+
const rawBaseUrl = overrides?.baseUrl?.trim() ||
|
|
60
|
+
process.env.PROXYLAYER_BASE_URL?.trim() ||
|
|
61
|
+
stored.baseUrl ||
|
|
62
|
+
DEFAULT_BASE_URL;
|
|
63
|
+
return {
|
|
64
|
+
apiKey,
|
|
65
|
+
email: stored.email,
|
|
66
|
+
baseUrl: rawBaseUrl.replace(/\/+$/, ""),
|
|
67
|
+
configPath,
|
|
68
|
+
};
|
|
69
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { ProxyLayer, ProxyLayerError, type BalanceResponse, type CatalogResponse, type CheckoutPurchaseInput, type CheckoutResponse, type CheckoutStatusResponse, type FixedProductSlug, type FixedPurchaseItem, type OrdersResponse, type OtpRequestResponse, type OtpVerifyResponse, type PaymentMethod, type ProductSlug, type ProxyLayerOptions, type ProxyStringFormat, type QuotePurchaseInput, type QuoteResponse, type ResidentialProxiesResponse, type StaticProxiesResponse, } from "./client.js";
|
|
2
|
+
export { clearConfig, DEFAULT_BASE_URL, getConfigPath, readConfig, resolveConfig, saveConfig, type ResolvedConfig, type StoredConfig, } from "./config.js";
|
|
3
|
+
export { runStdioMcpServer, type StdioMcpOptions } from "./mcp.js";
|
package/dist/index.js
ADDED
package/dist/mcp.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type StdioMcpOptions = {
|
|
2
|
+
apiKey?: string;
|
|
3
|
+
baseUrl?: string;
|
|
4
|
+
configPath?: string;
|
|
5
|
+
input?: NodeJS.ReadableStream;
|
|
6
|
+
output?: NodeJS.WritableStream;
|
|
7
|
+
fetch?: typeof globalThis.fetch;
|
|
8
|
+
};
|
|
9
|
+
export declare function runStdioMcpServer(options?: StdioMcpOptions): Promise<void>;
|
package/dist/mcp.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import readline from "node:readline";
|
|
2
|
+
import { resolveConfig, saveConfig } from "./config.js";
|
|
3
|
+
const PUBLIC_TOOLS = new Set(["get_catalog", "auth_request_otp", "auth_verify_otp"]);
|
|
4
|
+
function writeJsonLine(output, payload) {
|
|
5
|
+
output.write(`${JSON.stringify(payload)}\n`);
|
|
6
|
+
}
|
|
7
|
+
function extractVerifiedCredentials(result) {
|
|
8
|
+
if (!result || result.isError)
|
|
9
|
+
return null;
|
|
10
|
+
const structured = result.structuredContent;
|
|
11
|
+
let candidate = structured && typeof structured === "object" ? structured : null;
|
|
12
|
+
if (!candidate && Array.isArray(result.content)) {
|
|
13
|
+
const firstText = result.content.find((item) => Boolean(item && typeof item === "object" && item.type === "text"))?.text;
|
|
14
|
+
if (firstText) {
|
|
15
|
+
try {
|
|
16
|
+
const parsed = JSON.parse(firstText);
|
|
17
|
+
if (parsed && typeof parsed === "object")
|
|
18
|
+
candidate = parsed;
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
candidate = null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (!candidate || typeof candidate.apiKey !== "string" || !candidate.apiKey.startsWith("pl_live_")) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
const user = candidate.user;
|
|
29
|
+
const email = user && typeof user === "object" && typeof user.email === "string"
|
|
30
|
+
? user.email
|
|
31
|
+
: undefined;
|
|
32
|
+
return { apiKey: candidate.apiKey, email };
|
|
33
|
+
}
|
|
34
|
+
export async function runStdioMcpServer(options) {
|
|
35
|
+
const input = options?.input ?? process.stdin;
|
|
36
|
+
const output = options?.output ?? process.stdout;
|
|
37
|
+
const fetchFn = options?.fetch ?? globalThis.fetch;
|
|
38
|
+
const initialConfig = resolveConfig({
|
|
39
|
+
apiKey: options?.apiKey,
|
|
40
|
+
baseUrl: options?.baseUrl,
|
|
41
|
+
configPath: options?.configPath,
|
|
42
|
+
});
|
|
43
|
+
const endpointUrl = `${initialConfig.baseUrl}/api/mcp`;
|
|
44
|
+
let sessionApiKey = initialConfig.apiKey;
|
|
45
|
+
const handleLine = async (line) => {
|
|
46
|
+
const trimmed = line.trim();
|
|
47
|
+
if (!trimmed)
|
|
48
|
+
return;
|
|
49
|
+
let requestMessage;
|
|
50
|
+
try {
|
|
51
|
+
requestMessage = JSON.parse(trimmed);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
writeJsonLine(output, {
|
|
55
|
+
jsonrpc: "2.0",
|
|
56
|
+
id: null,
|
|
57
|
+
error: { code: -32700, message: "Parse error: invalid JSON" },
|
|
58
|
+
});
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const hasId = requestMessage.id !== undefined;
|
|
62
|
+
const method = typeof requestMessage.method === "string" ? requestMessage.method : "";
|
|
63
|
+
const toolName = method === "tools/call" && requestMessage.params && typeof requestMessage.params.name === "string"
|
|
64
|
+
? requestMessage.params.name
|
|
65
|
+
: "";
|
|
66
|
+
const sendAuth = Boolean(sessionApiKey) && !PUBLIC_TOOLS.has(toolName);
|
|
67
|
+
try {
|
|
68
|
+
const response = await fetchFn(endpointUrl, {
|
|
69
|
+
method: "POST",
|
|
70
|
+
headers: {
|
|
71
|
+
"Content-Type": "application/json",
|
|
72
|
+
...(sendAuth && sessionApiKey ? { Authorization: `Bearer ${sessionApiKey}` } : {}),
|
|
73
|
+
},
|
|
74
|
+
body: trimmed,
|
|
75
|
+
});
|
|
76
|
+
if (response.status === 202 || !hasId)
|
|
77
|
+
return;
|
|
78
|
+
const raw = await response.text();
|
|
79
|
+
if (!raw)
|
|
80
|
+
return;
|
|
81
|
+
let rpcResponse;
|
|
82
|
+
try {
|
|
83
|
+
rpcResponse = JSON.parse(raw);
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
writeJsonLine(output, {
|
|
87
|
+
jsonrpc: "2.0",
|
|
88
|
+
id: requestMessage.id ?? null,
|
|
89
|
+
error: { code: -32603, message: `Invalid response from ${endpointUrl} (${response.status})` },
|
|
90
|
+
});
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (method === "initialize" && rpcResponse.result && typeof rpcResponse.result === "object") {
|
|
94
|
+
const existing = typeof rpcResponse.result.instructions === "string" ? rpcResponse.result.instructions : "";
|
|
95
|
+
rpcResponse.result.instructions = `${existing} Local stdio bridge: auth_verify_otp automatically saves the returned pl_live_... key locally for immediate use in this session.`.trim();
|
|
96
|
+
}
|
|
97
|
+
if (toolName === "auth_verify_otp") {
|
|
98
|
+
const verified = extractVerifiedCredentials(rpcResponse.result);
|
|
99
|
+
if (verified) {
|
|
100
|
+
sessionApiKey = verified.apiKey;
|
|
101
|
+
saveConfig({
|
|
102
|
+
apiKey: verified.apiKey,
|
|
103
|
+
email: verified.email,
|
|
104
|
+
baseUrl: options?.baseUrl?.trim() || undefined,
|
|
105
|
+
}, options?.configPath);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
writeJsonLine(output, rpcResponse);
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
if (!hasId)
|
|
112
|
+
return;
|
|
113
|
+
writeJsonLine(output, {
|
|
114
|
+
jsonrpc: "2.0",
|
|
115
|
+
id: requestMessage.id ?? null,
|
|
116
|
+
error: {
|
|
117
|
+
code: -32603,
|
|
118
|
+
message: error instanceof Error ? error.message : "Failed to reach ProxyLayer MCP endpoint",
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
const rl = readline.createInterface({ input, crlfDelay: Infinity });
|
|
124
|
+
for await (const line of rl) {
|
|
125
|
+
await handleLine(line);
|
|
126
|
+
}
|
|
127
|
+
}
|