-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (63 loc) · 2.2 KB
/
Copy pathindex.js
File metadata and controls
69 lines (63 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const fetch = (...args) => import('node-fetch').then(({default: f}) => f(...args));
const BASE = 'https://orbisapi.com';
/**
* Search the Orbis marketplace for APIs
*/
async function search(query, options = {}) {
const { limit = 10, category } = options;
const params = new URLSearchParams({ search: query, limit });
if (category) params.set('category', category);
const res = await fetch(`${BASE}/api/marketplace/apis?${params}`);
const data = await res.json();
return data.apis || data;
}
/**
* Get full x402 discovery catalog (paginated)
*/
async function discover(options = {}) {
const { offset = 0, limit = 20 } = options;
const res = await fetch(`${BASE}/api/v2/x402/discovery/resources?offset=${offset}&limit=${limit}`);
return res.json();
}
/**
* Get payment info for a specific API by slug
*/
async function getPaymentInfo(apiSlug) {
const res = await fetch(`${BASE}/api/marketplace/apis/${apiSlug}`);
if (!res.ok) throw new Error(`API not found: ${apiSlug}`);
const api = await res.json();
return {
slug: api.slug,
name: api.name,
description: api.description,
proxyUrl: `${BASE}/proxy/${api.slug}`,
priceUsd: api.price,
category: api.category,
endpoints: api.endpoints || []
};
}
/**
* Call an Orbis API with x402 payment.
* Requires a pre-built x402 payment header — use with x402-fetch or coinbase/x402 SDK.
*
* For automatic payment handling, use x402-fetch:
* import { wrapFetchWithPayment } from 'x402-fetch';
* const fetch = wrapFetchWithPayment(globalThis.fetch, wallet);
*/
async function call(url, options = {}) {
const { method = 'GET', headers = {}, body, paymentHeader } = options;
const reqHeaders = { ...headers };
if (paymentHeader) reqHeaders['X-PAYMENT'] = paymentHeader;
const res = await fetch(url, {
method,
headers: reqHeaders,
body: body ? JSON.stringify(body) : undefined
});
if (res.status === 402) {
const payment = await res.json();
throw Object.assign(new Error('Payment required'), { payment402: payment });
}
if (!res.ok) throw new Error(`HTTP ${res.status}: ${await res.text()}`);
return res.json().catch(() => res.text());
}
module.exports = { search, discover, getPaymentInfo, call };