@onlist/sdk 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/LICENSE +21 -0
- package/README.md +206 -0
- package/dist/index.cjs +252 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +178 -0
- package/dist/index.d.ts +178 -0
- package/dist/index.js +205 -0
- package/dist/index.js.map +1 -0
- package/package.json +77 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Onlist (onlist.io)
|
|
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,206 @@
|
|
|
1
|
+
# Onlist JavaScript/TypeScript SDK
|
|
2
|
+
|
|
3
|
+
The official JavaScript/TypeScript SDK for [Onlist](https://onlist.io), the AI API marketplace. Access 200+ AI models through a unified, OpenAI-compatible API with intelligent provider routing and competitive pricing.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install onlist
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { Onlist } from "onlist";
|
|
15
|
+
|
|
16
|
+
const client = new Onlist({ apiKey: "sk-..." });
|
|
17
|
+
|
|
18
|
+
const response = await client.chat.completions.create({
|
|
19
|
+
model: "anthropic/claude-sonnet-4",
|
|
20
|
+
messages: [{ role: "user", content: "Hello!" }],
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
console.log(response.choices[0].message.content);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Authentication
|
|
27
|
+
|
|
28
|
+
The SDK looks for API keys in this order:
|
|
29
|
+
|
|
30
|
+
1. `apiKey` constructor parameter
|
|
31
|
+
2. `ONLIST_API_KEY` environment variable
|
|
32
|
+
3. `OPENAI_API_KEY` environment variable (OpenAI SDK fallback)
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
// Explicit key
|
|
36
|
+
const client = new Onlist({ apiKey: "sk-..." });
|
|
37
|
+
|
|
38
|
+
// From environment variable
|
|
39
|
+
// export ONLIST_API_KEY=sk-...
|
|
40
|
+
const client = new Onlist();
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Provider Routing
|
|
44
|
+
|
|
45
|
+
Route requests to specific providers on the Onlist marketplace:
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
// Pin to a specific provider
|
|
49
|
+
const response = await client.chat.completions.create({
|
|
50
|
+
model: "anthropic/claude-sonnet-4",
|
|
51
|
+
messages: [{ role: "user", content: "Hello!" }],
|
|
52
|
+
// @ts-expect-error -- extra body fields
|
|
53
|
+
provider: { only: ["alice-shop"] },
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// Sort by price
|
|
57
|
+
const response = await client.chat.completions.create({
|
|
58
|
+
model: "openai/gpt-4o",
|
|
59
|
+
messages: [{ role: "user", content: "Hello!" }],
|
|
60
|
+
// @ts-expect-error
|
|
61
|
+
provider: { sort: "price" },
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// Prioritize specific providers with fallback
|
|
65
|
+
const response = await client.chat.completions.create({
|
|
66
|
+
model: "openai/gpt-4o",
|
|
67
|
+
messages: [{ role: "user", content: "Hello!" }],
|
|
68
|
+
// @ts-expect-error
|
|
69
|
+
provider: {
|
|
70
|
+
order: ["alice-shop", "bob-ai"],
|
|
71
|
+
allow_fallbacks: true,
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Streaming
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
const stream = await client.chat.completions.create({
|
|
80
|
+
model: "anthropic/claude-sonnet-4",
|
|
81
|
+
messages: [{ role: "user", content: "Tell me a story" }],
|
|
82
|
+
stream: true,
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
for await (const chunk of stream) {
|
|
86
|
+
const content = chunk.choices[0]?.delta?.content;
|
|
87
|
+
if (content) process.stdout.write(content);
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Marketplace API
|
|
92
|
+
|
|
93
|
+
Browse models and providers on the Onlist marketplace:
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
// List models with pricing
|
|
97
|
+
const models = await client.marketplace.models.list({ limit: 10 });
|
|
98
|
+
console.log(`Found ${models.total} models`);
|
|
99
|
+
for (const model of models.data) {
|
|
100
|
+
console.log(`${model.id}: $${model.pricing?.prompt}/M tokens`);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Search models
|
|
104
|
+
const results = await client.marketplace.models.list({ q: "claude" });
|
|
105
|
+
|
|
106
|
+
// Get detailed model info with all provider offers
|
|
107
|
+
const detail = await client.marketplace.models.get("anthropic/claude-sonnet-4");
|
|
108
|
+
for (const offer of detail.providers) {
|
|
109
|
+
console.log(`${offer.name}: $${offer.price_input_usd}/M input`);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// List providers
|
|
113
|
+
const providers = await client.marketplace.providers.list();
|
|
114
|
+
for (const provider of providers.items) {
|
|
115
|
+
console.log(`${provider.name} (${provider.listing_count} models)`);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Get provider profile
|
|
119
|
+
const profile = await client.marketplace.providers.get("alice-shop");
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Error Handling
|
|
123
|
+
|
|
124
|
+
OpenAI-compatible calls (`chat.completions`, `embeddings`, etc.) throw standard `openai` errors. Marketplace calls throw `onlist` errors:
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
import OpenAI from "openai";
|
|
128
|
+
import { AuthenticationError } from "onlist";
|
|
129
|
+
|
|
130
|
+
// OpenAI-compatible endpoints throw openai errors
|
|
131
|
+
try {
|
|
132
|
+
await client.chat.completions.create({ model: "gpt-4o", messages: [] });
|
|
133
|
+
} catch (e) {
|
|
134
|
+
if (e instanceof OpenAI.AuthenticationError) {
|
|
135
|
+
console.log("Invalid API key");
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Marketplace endpoints throw onlist errors
|
|
140
|
+
try {
|
|
141
|
+
await client.marketplace.models.list();
|
|
142
|
+
} catch (e) {
|
|
143
|
+
if (e instanceof AuthenticationError) {
|
|
144
|
+
console.log("Invalid API key for marketplace");
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Migration from OpenAI
|
|
150
|
+
|
|
151
|
+
Replace the `openai` import with `onlist`:
|
|
152
|
+
|
|
153
|
+
```diff
|
|
154
|
+
- import OpenAI from "openai";
|
|
155
|
+
+ import { Onlist } from "onlist";
|
|
156
|
+
|
|
157
|
+
- const client = new OpenAI({ apiKey: "sk-..." });
|
|
158
|
+
+ const client = new Onlist({ apiKey: "sk-..." });
|
|
159
|
+
|
|
160
|
+
// All existing code works unchanged
|
|
161
|
+
const response = await client.chat.completions.create({
|
|
162
|
+
model: "gpt-4o",
|
|
163
|
+
messages: [{ role: "user", content: "Hello!" }],
|
|
164
|
+
});
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Migration from OpenRouter
|
|
168
|
+
|
|
169
|
+
```diff
|
|
170
|
+
- import OpenAI from "openai";
|
|
171
|
+
+ import { Onlist } from "onlist";
|
|
172
|
+
|
|
173
|
+
- const client = new OpenAI({
|
|
174
|
+
- baseURL: "https://openrouter.ai/api/v1",
|
|
175
|
+
- apiKey: process.env.OPENROUTER_API_KEY,
|
|
176
|
+
- });
|
|
177
|
+
+ const client = new Onlist();
|
|
178
|
+
|
|
179
|
+
// Provider routing syntax is compatible
|
|
180
|
+
const response = await client.chat.completions.create({
|
|
181
|
+
model: "anthropic/claude-sonnet-4",
|
|
182
|
+
messages: [{ role: "user", content: "Hello!" }],
|
|
183
|
+
});
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## TypeScript
|
|
187
|
+
|
|
188
|
+
The SDK is written in TypeScript and ships with full type definitions. All marketplace response types are exported:
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
191
|
+
import type { Model, Provider, ProviderRouting } from "onlist";
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
## Links
|
|
195
|
+
|
|
196
|
+
- [Website](https://onlist.io)
|
|
197
|
+
- [Documentation](https://onlist.io/docs)
|
|
198
|
+
- [Model Catalog](https://onlist.io/models)
|
|
199
|
+
- [Provider Directory](https://onlist.io/providers)
|
|
200
|
+
- [API Reference](https://onlist.io/docs/api)
|
|
201
|
+
- [GitHub](https://github.com/OnlistTeam/onlist-js)
|
|
202
|
+
- [Python SDK](https://pypi.org/project/onlist/)
|
|
203
|
+
|
|
204
|
+
## License
|
|
205
|
+
|
|
206
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
APIError: () => APIError,
|
|
34
|
+
AuthenticationError: () => AuthenticationError,
|
|
35
|
+
InsufficientBalanceError: () => InsufficientBalanceError,
|
|
36
|
+
Marketplace: () => Marketplace,
|
|
37
|
+
MarketplaceModels: () => MarketplaceModels,
|
|
38
|
+
MarketplaceProviders: () => MarketplaceProviders,
|
|
39
|
+
Onlist: () => Onlist,
|
|
40
|
+
OnlistError: () => OnlistError,
|
|
41
|
+
ProviderError: () => ProviderError,
|
|
42
|
+
RateLimitError: () => RateLimitError,
|
|
43
|
+
VERSION: () => VERSION
|
|
44
|
+
});
|
|
45
|
+
module.exports = __toCommonJS(index_exports);
|
|
46
|
+
|
|
47
|
+
// src/client.ts
|
|
48
|
+
var import_openai = __toESM(require("openai"), 1);
|
|
49
|
+
|
|
50
|
+
// src/errors.ts
|
|
51
|
+
var OnlistError = class extends Error {
|
|
52
|
+
constructor(message) {
|
|
53
|
+
super(message);
|
|
54
|
+
this.name = "OnlistError";
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
var APIError = class extends OnlistError {
|
|
58
|
+
status;
|
|
59
|
+
type;
|
|
60
|
+
code;
|
|
61
|
+
param;
|
|
62
|
+
body;
|
|
63
|
+
constructor(message, opts) {
|
|
64
|
+
super(message);
|
|
65
|
+
this.name = "APIError";
|
|
66
|
+
this.status = opts.status;
|
|
67
|
+
this.type = opts.type ?? null;
|
|
68
|
+
this.code = opts.code ?? null;
|
|
69
|
+
this.param = opts.param ?? null;
|
|
70
|
+
this.body = opts.body ?? null;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
var AuthenticationError = class extends APIError {
|
|
74
|
+
constructor(message = "Invalid API key", opts) {
|
|
75
|
+
super(message, { status: 401, ...opts });
|
|
76
|
+
this.name = "AuthenticationError";
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
var InsufficientBalanceError = class extends APIError {
|
|
80
|
+
constructor(message = "Insufficient balance", opts) {
|
|
81
|
+
super(message, { status: 402, ...opts });
|
|
82
|
+
this.name = "InsufficientBalanceError";
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
var RateLimitError = class extends APIError {
|
|
86
|
+
constructor(message = "Rate limited", opts) {
|
|
87
|
+
super(message, { status: 429, ...opts });
|
|
88
|
+
this.name = "RateLimitError";
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
var ProviderError = class extends APIError {
|
|
92
|
+
constructor(message, opts) {
|
|
93
|
+
super(message, opts);
|
|
94
|
+
this.name = "ProviderError";
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
function raiseForStatus(status, body) {
|
|
98
|
+
let error = {};
|
|
99
|
+
if (body && typeof body === "object" && "error" in body) {
|
|
100
|
+
const e = body.error;
|
|
101
|
+
error = typeof e === "object" && e !== null ? e : {};
|
|
102
|
+
} else if (body && typeof body === "object") {
|
|
103
|
+
error = body;
|
|
104
|
+
}
|
|
105
|
+
const message = (typeof error.message === "string" ? error.message : String(body)) || "Unknown error";
|
|
106
|
+
const type = typeof error.type === "string" ? error.type : null;
|
|
107
|
+
const code = typeof error.code === "string" ? error.code : null;
|
|
108
|
+
const param = typeof error.param === "string" ? error.param : null;
|
|
109
|
+
const opts = { status, type, code, param, body };
|
|
110
|
+
if (status === 401) throw new AuthenticationError(message, opts);
|
|
111
|
+
if (status === 402) throw new InsufficientBalanceError(message, opts);
|
|
112
|
+
if (status === 429) throw new RateLimitError(message, opts);
|
|
113
|
+
if (code && code.startsWith("no_provider")) throw new ProviderError(message, opts);
|
|
114
|
+
throw new APIError(message, opts);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// src/version.ts
|
|
118
|
+
var VERSION = "0.1.0";
|
|
119
|
+
|
|
120
|
+
// src/marketplace.ts
|
|
121
|
+
var DEFAULT_TIMEOUT = 3e4;
|
|
122
|
+
function encodePath(segment) {
|
|
123
|
+
return segment.split("/").map((s) => encodeURIComponent(s)).join("/");
|
|
124
|
+
}
|
|
125
|
+
async function parseResponse(response) {
|
|
126
|
+
let body;
|
|
127
|
+
try {
|
|
128
|
+
body = await response.json();
|
|
129
|
+
} catch {
|
|
130
|
+
body = await response.text().catch(() => "");
|
|
131
|
+
}
|
|
132
|
+
if (!response.ok) {
|
|
133
|
+
raiseForStatus(response.status, body);
|
|
134
|
+
}
|
|
135
|
+
if (body && typeof body === "object" && "data" in body && "success" in body) {
|
|
136
|
+
return body.data;
|
|
137
|
+
}
|
|
138
|
+
return body;
|
|
139
|
+
}
|
|
140
|
+
var MarketplaceModels = class {
|
|
141
|
+
constructor(_opts) {
|
|
142
|
+
this._opts = _opts;
|
|
143
|
+
}
|
|
144
|
+
_opts;
|
|
145
|
+
async list(params) {
|
|
146
|
+
const search = new URLSearchParams();
|
|
147
|
+
search.set("limit", String(params?.limit ?? 20));
|
|
148
|
+
search.set("offset", String(params?.offset ?? 0));
|
|
149
|
+
if (params?.q) search.set("q", params.q);
|
|
150
|
+
const resp = await this._fetch(`/api/mkt/models?${search}`);
|
|
151
|
+
return await parseResponse(resp);
|
|
152
|
+
}
|
|
153
|
+
async get(modelId) {
|
|
154
|
+
const resp = await this._fetch(`/api/mkt/models/${encodePath(modelId)}`);
|
|
155
|
+
let data = await parseResponse(resp);
|
|
156
|
+
if (data && typeof data === "object" && "data" in data) {
|
|
157
|
+
data = data.data;
|
|
158
|
+
}
|
|
159
|
+
return data;
|
|
160
|
+
}
|
|
161
|
+
_fetch(path, init) {
|
|
162
|
+
return fetchWithOpts(this._opts, path, init);
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
var MarketplaceProviders = class {
|
|
166
|
+
constructor(_opts) {
|
|
167
|
+
this._opts = _opts;
|
|
168
|
+
}
|
|
169
|
+
_opts;
|
|
170
|
+
async list(params) {
|
|
171
|
+
const search = new URLSearchParams();
|
|
172
|
+
if (params?.sort) search.set("sort", params.sort);
|
|
173
|
+
if (params?.q) search.set("q", params.q);
|
|
174
|
+
const qs = search.toString();
|
|
175
|
+
const resp = await this._fetch(`/api/mkt/providers${qs ? `?${qs}` : ""}`);
|
|
176
|
+
return await parseResponse(resp);
|
|
177
|
+
}
|
|
178
|
+
async get(slug) {
|
|
179
|
+
const resp = await this._fetch(`/api/mkt/provider/${encodePath(slug)}`);
|
|
180
|
+
let data = await parseResponse(resp);
|
|
181
|
+
if (data && typeof data === "object" && "data" in data) {
|
|
182
|
+
data = data.data;
|
|
183
|
+
}
|
|
184
|
+
return data;
|
|
185
|
+
}
|
|
186
|
+
_fetch(path, init) {
|
|
187
|
+
return fetchWithOpts(this._opts, path, init);
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
var Marketplace = class {
|
|
191
|
+
models;
|
|
192
|
+
providers;
|
|
193
|
+
constructor(opts) {
|
|
194
|
+
this.models = new MarketplaceModels(opts);
|
|
195
|
+
this.providers = new MarketplaceProviders(opts);
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
function fetchWithOpts(opts, path, init) {
|
|
199
|
+
const url = `${opts.baseURL.replace(/\/$/, "")}${path}`;
|
|
200
|
+
const headers = {
|
|
201
|
+
"User-Agent": `onlist-js/${VERSION}`,
|
|
202
|
+
Accept: "application/json",
|
|
203
|
+
...init?.headers
|
|
204
|
+
};
|
|
205
|
+
if (opts.apiKey) {
|
|
206
|
+
headers["Authorization"] = `Bearer ${opts.apiKey}`;
|
|
207
|
+
}
|
|
208
|
+
const signal = AbortSignal.timeout(opts.timeout ?? DEFAULT_TIMEOUT);
|
|
209
|
+
return fetch(url, { ...init, headers, signal });
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// src/client.ts
|
|
213
|
+
var BASE_URL = "https://onlist.io/v1";
|
|
214
|
+
var MARKETPLACE_BASE_URL = "https://onlist.io";
|
|
215
|
+
var ENV_API_KEY = "ONLIST_API_KEY";
|
|
216
|
+
var Onlist = class extends import_openai.default {
|
|
217
|
+
marketplace;
|
|
218
|
+
constructor(opts) {
|
|
219
|
+
const apiKey = opts?.apiKey ?? (typeof process !== "undefined" ? process.env?.[ENV_API_KEY] : void 0) ?? void 0;
|
|
220
|
+
const baseURL = opts?.baseURL ?? BASE_URL;
|
|
221
|
+
super({
|
|
222
|
+
...opts,
|
|
223
|
+
apiKey: apiKey ?? void 0,
|
|
224
|
+
baseURL,
|
|
225
|
+
defaultHeaders: {
|
|
226
|
+
"User-Agent": `onlist-js/${VERSION}`,
|
|
227
|
+
"HTTP-Referer": "https://onlist.io",
|
|
228
|
+
...opts?.defaultHeaders
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
const marketplaceBase = String(baseURL).split("/v1")[0] || MARKETPLACE_BASE_URL;
|
|
232
|
+
this.marketplace = new Marketplace({
|
|
233
|
+
apiKey: this.apiKey,
|
|
234
|
+
baseURL: marketplaceBase
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
239
|
+
0 && (module.exports = {
|
|
240
|
+
APIError,
|
|
241
|
+
AuthenticationError,
|
|
242
|
+
InsufficientBalanceError,
|
|
243
|
+
Marketplace,
|
|
244
|
+
MarketplaceModels,
|
|
245
|
+
MarketplaceProviders,
|
|
246
|
+
Onlist,
|
|
247
|
+
OnlistError,
|
|
248
|
+
ProviderError,
|
|
249
|
+
RateLimitError,
|
|
250
|
+
VERSION
|
|
251
|
+
});
|
|
252
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/client.ts","../src/errors.ts","../src/version.ts","../src/marketplace.ts"],"sourcesContent":["export { Onlist } from \"./client.js\";\nexport type { OnlistOptions } from \"./client.js\";\n\nexport { Marketplace, MarketplaceModels, MarketplaceProviders } from \"./marketplace.js\";\nexport type { MarketplaceOptions } from \"./marketplace.js\";\n\nexport {\n OnlistError,\n APIError,\n AuthenticationError,\n InsufficientBalanceError,\n RateLimitError,\n ProviderError,\n} from \"./errors.js\";\n\nexport type {\n Pricing,\n Architecture,\n TopProvider,\n Model,\n ProviderOffer,\n ModelDetail,\n ModelListResponse,\n Provider,\n ProviderDetail,\n ProviderListResponse,\n MaxPrice,\n ProviderRouting,\n} from \"./types/index.js\";\n\nexport { VERSION } from \"./version.js\";\n","import OpenAI from \"openai\";\nimport type { ClientOptions } from \"openai\";\nimport { Marketplace } from \"./marketplace.js\";\nimport { VERSION } from \"./version.js\";\n\nconst BASE_URL = \"https://onlist.io/v1\";\nconst MARKETPLACE_BASE_URL = \"https://onlist.io\";\nconst ENV_API_KEY = \"ONLIST_API_KEY\";\n\nexport interface OnlistOptions extends Omit<ClientOptions, \"apiKey\" | \"baseURL\"> {\n apiKey?: string | null;\n baseURL?: string | null;\n}\n\nexport class Onlist extends OpenAI {\n readonly marketplace: Marketplace;\n\n constructor(opts?: OnlistOptions) {\n const apiKey =\n opts?.apiKey ??\n (typeof process !== \"undefined\" ? process.env?.[ENV_API_KEY] : undefined) ??\n undefined;\n\n const baseURL = opts?.baseURL ?? BASE_URL;\n\n super({\n ...opts,\n apiKey: apiKey ?? undefined,\n baseURL,\n defaultHeaders: {\n \"User-Agent\": `onlist-js/${VERSION}`,\n \"HTTP-Referer\": \"https://onlist.io\",\n ...opts?.defaultHeaders,\n },\n });\n\n const marketplaceBase = String(baseURL).split(\"/v1\")[0] || MARKETPLACE_BASE_URL;\n\n this.marketplace = new Marketplace({\n apiKey: this.apiKey,\n baseURL: marketplaceBase,\n });\n }\n}\n","export class OnlistError extends Error {\n constructor(message: string) {\n super(message);\n this.name = \"OnlistError\";\n }\n}\n\nexport class APIError extends OnlistError {\n readonly status: number;\n readonly type: string | null;\n readonly code: string | null;\n readonly param: string | null;\n readonly body: unknown;\n\n constructor(\n message: string,\n opts: {\n status: number;\n type?: string | null;\n code?: string | null;\n param?: string | null;\n body?: unknown;\n },\n ) {\n super(message);\n this.name = \"APIError\";\n this.status = opts.status;\n this.type = opts.type ?? null;\n this.code = opts.code ?? null;\n this.param = opts.param ?? null;\n this.body = opts.body ?? null;\n }\n}\n\nexport class AuthenticationError extends APIError {\n constructor(message = \"Invalid API key\", opts?: Partial<ConstructorParameters<typeof APIError>[1]>) {\n super(message, { status: 401, ...opts });\n this.name = \"AuthenticationError\";\n }\n}\n\nexport class InsufficientBalanceError extends APIError {\n constructor(message = \"Insufficient balance\", opts?: Partial<ConstructorParameters<typeof APIError>[1]>) {\n super(message, { status: 402, ...opts });\n this.name = \"InsufficientBalanceError\";\n }\n}\n\nexport class RateLimitError extends APIError {\n constructor(message = \"Rate limited\", opts?: Partial<ConstructorParameters<typeof APIError>[1]>) {\n super(message, { status: 429, ...opts });\n this.name = \"RateLimitError\";\n }\n}\n\nexport class ProviderError extends APIError {\n constructor(message: string, opts: ConstructorParameters<typeof APIError>[1]) {\n super(message, opts);\n this.name = \"ProviderError\";\n }\n}\n\nexport function raiseForStatus(status: number, body: unknown): never {\n let error: Record<string, unknown> = {};\n if (body && typeof body === \"object\" && \"error\" in body) {\n const e = (body as Record<string, unknown>).error;\n error = typeof e === \"object\" && e !== null ? (e as Record<string, unknown>) : {};\n } else if (body && typeof body === \"object\") {\n error = body as Record<string, unknown>;\n }\n\n const message = (typeof error.message === \"string\" ? error.message : String(body)) || \"Unknown error\";\n const type = typeof error.type === \"string\" ? error.type : null;\n const code = typeof error.code === \"string\" ? error.code : null;\n const param = typeof error.param === \"string\" ? error.param : null;\n const opts = { status, type, code, param, body };\n\n if (status === 401) throw new AuthenticationError(message, opts);\n if (status === 402) throw new InsufficientBalanceError(message, opts);\n if (status === 429) throw new RateLimitError(message, opts);\n if (code && code.startsWith(\"no_provider\")) throw new ProviderError(message, opts);\n\n throw new APIError(message, opts);\n}\n","export const VERSION = \"0.1.0\";\n","import { raiseForStatus } from \"./errors.js\";\nimport { VERSION } from \"./version.js\";\nimport type { ModelDetail, ModelListResponse } from \"./types/model.js\";\nimport type { ProviderDetail, ProviderListResponse } from \"./types/provider.js\";\n\nconst DEFAULT_TIMEOUT = 30_000;\n\nfunction encodePath(segment: string): string {\n return segment\n .split(\"/\")\n .map((s) => encodeURIComponent(s))\n .join(\"/\");\n}\n\nasync function parseResponse(response: Response): Promise<unknown> {\n let body: unknown;\n try {\n body = await response.json();\n } catch {\n body = await response.text().catch(() => \"\");\n }\n\n if (!response.ok) {\n raiseForStatus(response.status, body);\n }\n\n if (body && typeof body === \"object\" && \"data\" in body && \"success\" in body) {\n return (body as Record<string, unknown>).data;\n }\n return body;\n}\n\nexport interface MarketplaceOptions {\n apiKey?: string | null;\n baseURL: string;\n timeout?: number;\n}\n\nexport class MarketplaceModels {\n constructor(private readonly _opts: MarketplaceOptions) {}\n\n async list(params?: { limit?: number; offset?: number; q?: string }): Promise<ModelListResponse> {\n const search = new URLSearchParams();\n search.set(\"limit\", String(params?.limit ?? 20));\n search.set(\"offset\", String(params?.offset ?? 0));\n if (params?.q) search.set(\"q\", params.q);\n\n const resp = await this._fetch(`/api/mkt/models?${search}`);\n return (await parseResponse(resp)) as ModelListResponse;\n }\n\n async get(modelId: string): Promise<ModelDetail> {\n const resp = await this._fetch(`/api/mkt/models/${encodePath(modelId)}`);\n let data = (await parseResponse(resp)) as Record<string, unknown>;\n if (data && typeof data === \"object\" && \"data\" in data) {\n data = data.data as Record<string, unknown>;\n }\n return data as unknown as ModelDetail;\n }\n\n private _fetch(path: string, init?: RequestInit): Promise<Response> {\n return fetchWithOpts(this._opts, path, init);\n }\n}\n\nexport class MarketplaceProviders {\n constructor(private readonly _opts: MarketplaceOptions) {}\n\n async list(params?: { sort?: string; q?: string }): Promise<ProviderListResponse> {\n const search = new URLSearchParams();\n if (params?.sort) search.set(\"sort\", params.sort);\n if (params?.q) search.set(\"q\", params.q);\n\n const qs = search.toString();\n const resp = await this._fetch(`/api/mkt/providers${qs ? `?${qs}` : \"\"}`);\n return (await parseResponse(resp)) as ProviderListResponse;\n }\n\n async get(slug: string): Promise<ProviderDetail> {\n const resp = await this._fetch(`/api/mkt/provider/${encodePath(slug)}`);\n let data = (await parseResponse(resp)) as Record<string, unknown>;\n if (data && typeof data === \"object\" && \"data\" in data) {\n data = data.data as Record<string, unknown>;\n }\n return data as unknown as ProviderDetail;\n }\n\n private _fetch(path: string, init?: RequestInit): Promise<Response> {\n return fetchWithOpts(this._opts, path, init);\n }\n}\n\nexport class Marketplace {\n readonly models: MarketplaceModels;\n readonly providers: MarketplaceProviders;\n\n constructor(opts: MarketplaceOptions) {\n this.models = new MarketplaceModels(opts);\n this.providers = new MarketplaceProviders(opts);\n }\n}\n\nfunction fetchWithOpts(opts: MarketplaceOptions, path: string, init?: RequestInit): Promise<Response> {\n const url = `${opts.baseURL.replace(/\\/$/, \"\")}${path}`;\n const headers: Record<string, string> = {\n \"User-Agent\": `onlist-js/${VERSION}`,\n Accept: \"application/json\",\n ...(init?.headers as Record<string, string>),\n };\n if (opts.apiKey) {\n headers[\"Authorization\"] = `Bearer ${opts.apiKey}`;\n }\n const signal = AbortSignal.timeout(opts.timeout ?? DEFAULT_TIMEOUT);\n return fetch(url, { ...init, headers, signal });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAmB;;;ACAZ,IAAM,cAAN,cAA0B,MAAM;AAAA,EACrC,YAAY,SAAiB;AAC3B,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EACd;AACF;AAEO,IAAM,WAAN,cAAuB,YAAY;AAAA,EAC/B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAET,YACE,SACA,MAOA;AACA,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,SAAK,SAAS,KAAK;AACnB,SAAK,OAAO,KAAK,QAAQ;AACzB,SAAK,OAAO,KAAK,QAAQ;AACzB,SAAK,QAAQ,KAAK,SAAS;AAC3B,SAAK,OAAO,KAAK,QAAQ;AAAA,EAC3B;AACF;AAEO,IAAM,sBAAN,cAAkC,SAAS;AAAA,EAChD,YAAY,UAA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G,IAAI;AACrD,QAAM,UAAkC;AAAA,IACtC,cAAc,aAAa,OAAO;AAAA,IAClC,QAAQ;AAAA,IACR,GAAI,MAAM;AAAA,EACZ;AACA,MAAI,KAAK,QAAQ;AACf,YAAQ,eAAe,IAAI,UAAU,KAAK,MAAM;AAAA,EAClD;AACA,QAAM,SAAS,YAAY,QAAQ,KAAK,WAAW,eAAe;AAClE,SAAO,MAAM,KAAK,EAAE,GAAG,MAAM,SAAS,OAAO,CAAC;AAChD;;;AH7GA,IAAM,WAAW;AACjB,IAAM,uBAAuB;AAC7B,IAAM,cAAc;AAOb,IAAM,SAAN,cAAqB,cAAAA,QAAO;AAAA,EACxB;AAAA,EAET,YAAY,MAAsB;AAChC,UAAM,SACJ,MAAM,WACL,OAAO,YAAY,cAAc,QAAQ,MAAM,WAAW,IAAI,WAC/D;AAEF,UAAM,UAAU,MAAM,WAAW;AAEjC,UAAM;AAAA,MACJ,GAAG;AAAA,MACH,QAAQ,UAAU;AAAA,MAClB;AAAA,MACA,gBAAgB;AAAA,QACd,cAAc,aAAa,OAAO;AAAA,QAClC,gBAAgB;AAAA,QAChB,GAAG,MAAM;AAAA,MACX;AAAA,IACF,CAAC;AAED,UAAM,kBAAkB,OAAO,OAAO,EAAE,MAAM,KAAK,EAAE,CAAC,KAAK;AAE3D,SAAK,cAAc,IAAI,YAAY;AAAA,MACjC,QAAQ,KAAK;AAAA,MACb,SAAS;AAAA,IACX,CAAC;AAAA,EACH;AACF;","names":["OpenAI"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import OpenAI, { ClientOptions } from 'openai';
|
|
2
|
+
|
|
3
|
+
interface Pricing {
|
|
4
|
+
prompt: string;
|
|
5
|
+
completion: string;
|
|
6
|
+
request?: string | null;
|
|
7
|
+
}
|
|
8
|
+
interface Architecture {
|
|
9
|
+
modality?: string | null;
|
|
10
|
+
input_modalities?: string[] | null;
|
|
11
|
+
output_modalities?: string[] | null;
|
|
12
|
+
tokenizer?: string | null;
|
|
13
|
+
}
|
|
14
|
+
interface TopProvider {
|
|
15
|
+
context_length?: number | null;
|
|
16
|
+
max_completion_tokens?: number | null;
|
|
17
|
+
is_moderated?: boolean | null;
|
|
18
|
+
}
|
|
19
|
+
interface Model {
|
|
20
|
+
id: string;
|
|
21
|
+
name?: string | null;
|
|
22
|
+
author?: string | null;
|
|
23
|
+
owned_by?: string | null;
|
|
24
|
+
canonical_slug?: string | null;
|
|
25
|
+
created?: number | null;
|
|
26
|
+
description?: string | null;
|
|
27
|
+
context_length?: number | null;
|
|
28
|
+
max_output_length?: number | null;
|
|
29
|
+
architecture?: Architecture | null;
|
|
30
|
+
pricing?: Pricing | null;
|
|
31
|
+
supported_parameters?: string[] | null;
|
|
32
|
+
quantization?: string | null;
|
|
33
|
+
top_provider?: TopProvider | null;
|
|
34
|
+
is_ready?: boolean | null;
|
|
35
|
+
[key: string]: unknown;
|
|
36
|
+
}
|
|
37
|
+
interface ProviderOffer {
|
|
38
|
+
listing_id?: number | null;
|
|
39
|
+
provider_id?: number | null;
|
|
40
|
+
slug?: string | null;
|
|
41
|
+
name?: string | null;
|
|
42
|
+
logo_url?: string | null;
|
|
43
|
+
score?: number | null;
|
|
44
|
+
price_input_usd?: string | null;
|
|
45
|
+
price_output_usd?: string | null;
|
|
46
|
+
availability_7d?: number | null;
|
|
47
|
+
[key: string]: unknown;
|
|
48
|
+
}
|
|
49
|
+
interface ModelDetail {
|
|
50
|
+
id: string;
|
|
51
|
+
name?: string | null;
|
|
52
|
+
author?: string | null;
|
|
53
|
+
owned_by?: string | null;
|
|
54
|
+
context_length?: number | null;
|
|
55
|
+
max_output_length?: number | null;
|
|
56
|
+
architecture?: Architecture | null;
|
|
57
|
+
pricing?: Pricing | null;
|
|
58
|
+
description?: string | null;
|
|
59
|
+
providers: ProviderOffer[];
|
|
60
|
+
[key: string]: unknown;
|
|
61
|
+
}
|
|
62
|
+
interface ModelListResponse {
|
|
63
|
+
data: Model[];
|
|
64
|
+
total?: number | null;
|
|
65
|
+
offset?: number | null;
|
|
66
|
+
limit?: number | null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
interface Provider {
|
|
70
|
+
id?: number | null;
|
|
71
|
+
slug: string;
|
|
72
|
+
name?: string | null;
|
|
73
|
+
display_name?: string | null;
|
|
74
|
+
description?: string | null;
|
|
75
|
+
logo_url?: string | null;
|
|
76
|
+
listing_count?: number | null;
|
|
77
|
+
follower_count?: number | null;
|
|
78
|
+
weighted_score?: number | null;
|
|
79
|
+
sample_count?: number | null;
|
|
80
|
+
max_rpm?: number | null;
|
|
81
|
+
availability_7d?: number | null;
|
|
82
|
+
[key: string]: unknown;
|
|
83
|
+
}
|
|
84
|
+
interface ProviderDetail extends Provider {
|
|
85
|
+
listings: Record<string, unknown>[];
|
|
86
|
+
}
|
|
87
|
+
interface ProviderListResponse {
|
|
88
|
+
items: Provider[];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
interface MarketplaceOptions {
|
|
92
|
+
apiKey?: string | null;
|
|
93
|
+
baseURL: string;
|
|
94
|
+
timeout?: number;
|
|
95
|
+
}
|
|
96
|
+
declare class MarketplaceModels {
|
|
97
|
+
private readonly _opts;
|
|
98
|
+
constructor(_opts: MarketplaceOptions);
|
|
99
|
+
list(params?: {
|
|
100
|
+
limit?: number;
|
|
101
|
+
offset?: number;
|
|
102
|
+
q?: string;
|
|
103
|
+
}): Promise<ModelListResponse>;
|
|
104
|
+
get(modelId: string): Promise<ModelDetail>;
|
|
105
|
+
private _fetch;
|
|
106
|
+
}
|
|
107
|
+
declare class MarketplaceProviders {
|
|
108
|
+
private readonly _opts;
|
|
109
|
+
constructor(_opts: MarketplaceOptions);
|
|
110
|
+
list(params?: {
|
|
111
|
+
sort?: string;
|
|
112
|
+
q?: string;
|
|
113
|
+
}): Promise<ProviderListResponse>;
|
|
114
|
+
get(slug: string): Promise<ProviderDetail>;
|
|
115
|
+
private _fetch;
|
|
116
|
+
}
|
|
117
|
+
declare class Marketplace {
|
|
118
|
+
readonly models: MarketplaceModels;
|
|
119
|
+
readonly providers: MarketplaceProviders;
|
|
120
|
+
constructor(opts: MarketplaceOptions);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
interface OnlistOptions extends Omit<ClientOptions, "apiKey" | "baseURL"> {
|
|
124
|
+
apiKey?: string | null;
|
|
125
|
+
baseURL?: string | null;
|
|
126
|
+
}
|
|
127
|
+
declare class Onlist extends OpenAI {
|
|
128
|
+
readonly marketplace: Marketplace;
|
|
129
|
+
constructor(opts?: OnlistOptions);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
declare class OnlistError extends Error {
|
|
133
|
+
constructor(message: string);
|
|
134
|
+
}
|
|
135
|
+
declare class APIError extends OnlistError {
|
|
136
|
+
readonly status: number;
|
|
137
|
+
readonly type: string | null;
|
|
138
|
+
readonly code: string | null;
|
|
139
|
+
readonly param: string | null;
|
|
140
|
+
readonly body: unknown;
|
|
141
|
+
constructor(message: string, opts: {
|
|
142
|
+
status: number;
|
|
143
|
+
type?: string | null;
|
|
144
|
+
code?: string | null;
|
|
145
|
+
param?: string | null;
|
|
146
|
+
body?: unknown;
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
declare class AuthenticationError extends APIError {
|
|
150
|
+
constructor(message?: string, opts?: Partial<ConstructorParameters<typeof APIError>[1]>);
|
|
151
|
+
}
|
|
152
|
+
declare class InsufficientBalanceError extends APIError {
|
|
153
|
+
constructor(message?: string, opts?: Partial<ConstructorParameters<typeof APIError>[1]>);
|
|
154
|
+
}
|
|
155
|
+
declare class RateLimitError extends APIError {
|
|
156
|
+
constructor(message?: string, opts?: Partial<ConstructorParameters<typeof APIError>[1]>);
|
|
157
|
+
}
|
|
158
|
+
declare class ProviderError extends APIError {
|
|
159
|
+
constructor(message: string, opts: ConstructorParameters<typeof APIError>[1]);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
interface MaxPrice {
|
|
163
|
+
prompt?: number;
|
|
164
|
+
completion?: number;
|
|
165
|
+
}
|
|
166
|
+
interface ProviderRouting {
|
|
167
|
+
only?: string[];
|
|
168
|
+
sort?: "price" | "throughput";
|
|
169
|
+
order?: string[];
|
|
170
|
+
allow?: string[];
|
|
171
|
+
ignore?: string[];
|
|
172
|
+
allow_fallbacks?: boolean;
|
|
173
|
+
max_price?: MaxPrice;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
declare const VERSION = "0.1.0";
|
|
177
|
+
|
|
178
|
+
export { APIError, type Architecture, AuthenticationError, InsufficientBalanceError, Marketplace, MarketplaceModels, type MarketplaceOptions, MarketplaceProviders, type MaxPrice, type Model, type ModelDetail, type ModelListResponse, Onlist, OnlistError, type OnlistOptions, type Pricing, type Provider, type ProviderDetail, ProviderError, type ProviderListResponse, type ProviderOffer, type ProviderRouting, RateLimitError, type TopProvider, VERSION };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import OpenAI, { ClientOptions } from 'openai';
|
|
2
|
+
|
|
3
|
+
interface Pricing {
|
|
4
|
+
prompt: string;
|
|
5
|
+
completion: string;
|
|
6
|
+
request?: string | null;
|
|
7
|
+
}
|
|
8
|
+
interface Architecture {
|
|
9
|
+
modality?: string | null;
|
|
10
|
+
input_modalities?: string[] | null;
|
|
11
|
+
output_modalities?: string[] | null;
|
|
12
|
+
tokenizer?: string | null;
|
|
13
|
+
}
|
|
14
|
+
interface TopProvider {
|
|
15
|
+
context_length?: number | null;
|
|
16
|
+
max_completion_tokens?: number | null;
|
|
17
|
+
is_moderated?: boolean | null;
|
|
18
|
+
}
|
|
19
|
+
interface Model {
|
|
20
|
+
id: string;
|
|
21
|
+
name?: string | null;
|
|
22
|
+
author?: string | null;
|
|
23
|
+
owned_by?: string | null;
|
|
24
|
+
canonical_slug?: string | null;
|
|
25
|
+
created?: number | null;
|
|
26
|
+
description?: string | null;
|
|
27
|
+
context_length?: number | null;
|
|
28
|
+
max_output_length?: number | null;
|
|
29
|
+
architecture?: Architecture | null;
|
|
30
|
+
pricing?: Pricing | null;
|
|
31
|
+
supported_parameters?: string[] | null;
|
|
32
|
+
quantization?: string | null;
|
|
33
|
+
top_provider?: TopProvider | null;
|
|
34
|
+
is_ready?: boolean | null;
|
|
35
|
+
[key: string]: unknown;
|
|
36
|
+
}
|
|
37
|
+
interface ProviderOffer {
|
|
38
|
+
listing_id?: number | null;
|
|
39
|
+
provider_id?: number | null;
|
|
40
|
+
slug?: string | null;
|
|
41
|
+
name?: string | null;
|
|
42
|
+
logo_url?: string | null;
|
|
43
|
+
score?: number | null;
|
|
44
|
+
price_input_usd?: string | null;
|
|
45
|
+
price_output_usd?: string | null;
|
|
46
|
+
availability_7d?: number | null;
|
|
47
|
+
[key: string]: unknown;
|
|
48
|
+
}
|
|
49
|
+
interface ModelDetail {
|
|
50
|
+
id: string;
|
|
51
|
+
name?: string | null;
|
|
52
|
+
author?: string | null;
|
|
53
|
+
owned_by?: string | null;
|
|
54
|
+
context_length?: number | null;
|
|
55
|
+
max_output_length?: number | null;
|
|
56
|
+
architecture?: Architecture | null;
|
|
57
|
+
pricing?: Pricing | null;
|
|
58
|
+
description?: string | null;
|
|
59
|
+
providers: ProviderOffer[];
|
|
60
|
+
[key: string]: unknown;
|
|
61
|
+
}
|
|
62
|
+
interface ModelListResponse {
|
|
63
|
+
data: Model[];
|
|
64
|
+
total?: number | null;
|
|
65
|
+
offset?: number | null;
|
|
66
|
+
limit?: number | null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
interface Provider {
|
|
70
|
+
id?: number | null;
|
|
71
|
+
slug: string;
|
|
72
|
+
name?: string | null;
|
|
73
|
+
display_name?: string | null;
|
|
74
|
+
description?: string | null;
|
|
75
|
+
logo_url?: string | null;
|
|
76
|
+
listing_count?: number | null;
|
|
77
|
+
follower_count?: number | null;
|
|
78
|
+
weighted_score?: number | null;
|
|
79
|
+
sample_count?: number | null;
|
|
80
|
+
max_rpm?: number | null;
|
|
81
|
+
availability_7d?: number | null;
|
|
82
|
+
[key: string]: unknown;
|
|
83
|
+
}
|
|
84
|
+
interface ProviderDetail extends Provider {
|
|
85
|
+
listings: Record<string, unknown>[];
|
|
86
|
+
}
|
|
87
|
+
interface ProviderListResponse {
|
|
88
|
+
items: Provider[];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
interface MarketplaceOptions {
|
|
92
|
+
apiKey?: string | null;
|
|
93
|
+
baseURL: string;
|
|
94
|
+
timeout?: number;
|
|
95
|
+
}
|
|
96
|
+
declare class MarketplaceModels {
|
|
97
|
+
private readonly _opts;
|
|
98
|
+
constructor(_opts: MarketplaceOptions);
|
|
99
|
+
list(params?: {
|
|
100
|
+
limit?: number;
|
|
101
|
+
offset?: number;
|
|
102
|
+
q?: string;
|
|
103
|
+
}): Promise<ModelListResponse>;
|
|
104
|
+
get(modelId: string): Promise<ModelDetail>;
|
|
105
|
+
private _fetch;
|
|
106
|
+
}
|
|
107
|
+
declare class MarketplaceProviders {
|
|
108
|
+
private readonly _opts;
|
|
109
|
+
constructor(_opts: MarketplaceOptions);
|
|
110
|
+
list(params?: {
|
|
111
|
+
sort?: string;
|
|
112
|
+
q?: string;
|
|
113
|
+
}): Promise<ProviderListResponse>;
|
|
114
|
+
get(slug: string): Promise<ProviderDetail>;
|
|
115
|
+
private _fetch;
|
|
116
|
+
}
|
|
117
|
+
declare class Marketplace {
|
|
118
|
+
readonly models: MarketplaceModels;
|
|
119
|
+
readonly providers: MarketplaceProviders;
|
|
120
|
+
constructor(opts: MarketplaceOptions);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
interface OnlistOptions extends Omit<ClientOptions, "apiKey" | "baseURL"> {
|
|
124
|
+
apiKey?: string | null;
|
|
125
|
+
baseURL?: string | null;
|
|
126
|
+
}
|
|
127
|
+
declare class Onlist extends OpenAI {
|
|
128
|
+
readonly marketplace: Marketplace;
|
|
129
|
+
constructor(opts?: OnlistOptions);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
declare class OnlistError extends Error {
|
|
133
|
+
constructor(message: string);
|
|
134
|
+
}
|
|
135
|
+
declare class APIError extends OnlistError {
|
|
136
|
+
readonly status: number;
|
|
137
|
+
readonly type: string | null;
|
|
138
|
+
readonly code: string | null;
|
|
139
|
+
readonly param: string | null;
|
|
140
|
+
readonly body: unknown;
|
|
141
|
+
constructor(message: string, opts: {
|
|
142
|
+
status: number;
|
|
143
|
+
type?: string | null;
|
|
144
|
+
code?: string | null;
|
|
145
|
+
param?: string | null;
|
|
146
|
+
body?: unknown;
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
declare class AuthenticationError extends APIError {
|
|
150
|
+
constructor(message?: string, opts?: Partial<ConstructorParameters<typeof APIError>[1]>);
|
|
151
|
+
}
|
|
152
|
+
declare class InsufficientBalanceError extends APIError {
|
|
153
|
+
constructor(message?: string, opts?: Partial<ConstructorParameters<typeof APIError>[1]>);
|
|
154
|
+
}
|
|
155
|
+
declare class RateLimitError extends APIError {
|
|
156
|
+
constructor(message?: string, opts?: Partial<ConstructorParameters<typeof APIError>[1]>);
|
|
157
|
+
}
|
|
158
|
+
declare class ProviderError extends APIError {
|
|
159
|
+
constructor(message: string, opts: ConstructorParameters<typeof APIError>[1]);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
interface MaxPrice {
|
|
163
|
+
prompt?: number;
|
|
164
|
+
completion?: number;
|
|
165
|
+
}
|
|
166
|
+
interface ProviderRouting {
|
|
167
|
+
only?: string[];
|
|
168
|
+
sort?: "price" | "throughput";
|
|
169
|
+
order?: string[];
|
|
170
|
+
allow?: string[];
|
|
171
|
+
ignore?: string[];
|
|
172
|
+
allow_fallbacks?: boolean;
|
|
173
|
+
max_price?: MaxPrice;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
declare const VERSION = "0.1.0";
|
|
177
|
+
|
|
178
|
+
export { APIError, type Architecture, AuthenticationError, InsufficientBalanceError, Marketplace, MarketplaceModels, type MarketplaceOptions, MarketplaceProviders, type MaxPrice, type Model, type ModelDetail, type ModelListResponse, Onlist, OnlistError, type OnlistOptions, type Pricing, type Provider, type ProviderDetail, ProviderError, type ProviderListResponse, type ProviderOffer, type ProviderRouting, RateLimitError, type TopProvider, VERSION };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
// src/client.ts
|
|
2
|
+
import OpenAI from "openai";
|
|
3
|
+
|
|
4
|
+
// src/errors.ts
|
|
5
|
+
var OnlistError = class extends Error {
|
|
6
|
+
constructor(message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "OnlistError";
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
var APIError = class extends OnlistError {
|
|
12
|
+
status;
|
|
13
|
+
type;
|
|
14
|
+
code;
|
|
15
|
+
param;
|
|
16
|
+
body;
|
|
17
|
+
constructor(message, opts) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.name = "APIError";
|
|
20
|
+
this.status = opts.status;
|
|
21
|
+
this.type = opts.type ?? null;
|
|
22
|
+
this.code = opts.code ?? null;
|
|
23
|
+
this.param = opts.param ?? null;
|
|
24
|
+
this.body = opts.body ?? null;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var AuthenticationError = class extends APIError {
|
|
28
|
+
constructor(message = "Invalid API key", opts) {
|
|
29
|
+
super(message, { status: 401, ...opts });
|
|
30
|
+
this.name = "AuthenticationError";
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var InsufficientBalanceError = class extends APIError {
|
|
34
|
+
constructor(message = "Insufficient balance", opts) {
|
|
35
|
+
super(message, { status: 402, ...opts });
|
|
36
|
+
this.name = "InsufficientBalanceError";
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
var RateLimitError = class extends APIError {
|
|
40
|
+
constructor(message = "Rate limited", opts) {
|
|
41
|
+
super(message, { status: 429, ...opts });
|
|
42
|
+
this.name = "RateLimitError";
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var ProviderError = class extends APIError {
|
|
46
|
+
constructor(message, opts) {
|
|
47
|
+
super(message, opts);
|
|
48
|
+
this.name = "ProviderError";
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
function raiseForStatus(status, body) {
|
|
52
|
+
let error = {};
|
|
53
|
+
if (body && typeof body === "object" && "error" in body) {
|
|
54
|
+
const e = body.error;
|
|
55
|
+
error = typeof e === "object" && e !== null ? e : {};
|
|
56
|
+
} else if (body && typeof body === "object") {
|
|
57
|
+
error = body;
|
|
58
|
+
}
|
|
59
|
+
const message = (typeof error.message === "string" ? error.message : String(body)) || "Unknown error";
|
|
60
|
+
const type = typeof error.type === "string" ? error.type : null;
|
|
61
|
+
const code = typeof error.code === "string" ? error.code : null;
|
|
62
|
+
const param = typeof error.param === "string" ? error.param : null;
|
|
63
|
+
const opts = { status, type, code, param, body };
|
|
64
|
+
if (status === 401) throw new AuthenticationError(message, opts);
|
|
65
|
+
if (status === 402) throw new InsufficientBalanceError(message, opts);
|
|
66
|
+
if (status === 429) throw new RateLimitError(message, opts);
|
|
67
|
+
if (code && code.startsWith("no_provider")) throw new ProviderError(message, opts);
|
|
68
|
+
throw new APIError(message, opts);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// src/version.ts
|
|
72
|
+
var VERSION = "0.1.0";
|
|
73
|
+
|
|
74
|
+
// src/marketplace.ts
|
|
75
|
+
var DEFAULT_TIMEOUT = 3e4;
|
|
76
|
+
function encodePath(segment) {
|
|
77
|
+
return segment.split("/").map((s) => encodeURIComponent(s)).join("/");
|
|
78
|
+
}
|
|
79
|
+
async function parseResponse(response) {
|
|
80
|
+
let body;
|
|
81
|
+
try {
|
|
82
|
+
body = await response.json();
|
|
83
|
+
} catch {
|
|
84
|
+
body = await response.text().catch(() => "");
|
|
85
|
+
}
|
|
86
|
+
if (!response.ok) {
|
|
87
|
+
raiseForStatus(response.status, body);
|
|
88
|
+
}
|
|
89
|
+
if (body && typeof body === "object" && "data" in body && "success" in body) {
|
|
90
|
+
return body.data;
|
|
91
|
+
}
|
|
92
|
+
return body;
|
|
93
|
+
}
|
|
94
|
+
var MarketplaceModels = class {
|
|
95
|
+
constructor(_opts) {
|
|
96
|
+
this._opts = _opts;
|
|
97
|
+
}
|
|
98
|
+
_opts;
|
|
99
|
+
async list(params) {
|
|
100
|
+
const search = new URLSearchParams();
|
|
101
|
+
search.set("limit", String(params?.limit ?? 20));
|
|
102
|
+
search.set("offset", String(params?.offset ?? 0));
|
|
103
|
+
if (params?.q) search.set("q", params.q);
|
|
104
|
+
const resp = await this._fetch(`/api/mkt/models?${search}`);
|
|
105
|
+
return await parseResponse(resp);
|
|
106
|
+
}
|
|
107
|
+
async get(modelId) {
|
|
108
|
+
const resp = await this._fetch(`/api/mkt/models/${encodePath(modelId)}`);
|
|
109
|
+
let data = await parseResponse(resp);
|
|
110
|
+
if (data && typeof data === "object" && "data" in data) {
|
|
111
|
+
data = data.data;
|
|
112
|
+
}
|
|
113
|
+
return data;
|
|
114
|
+
}
|
|
115
|
+
_fetch(path, init) {
|
|
116
|
+
return fetchWithOpts(this._opts, path, init);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
var MarketplaceProviders = class {
|
|
120
|
+
constructor(_opts) {
|
|
121
|
+
this._opts = _opts;
|
|
122
|
+
}
|
|
123
|
+
_opts;
|
|
124
|
+
async list(params) {
|
|
125
|
+
const search = new URLSearchParams();
|
|
126
|
+
if (params?.sort) search.set("sort", params.sort);
|
|
127
|
+
if (params?.q) search.set("q", params.q);
|
|
128
|
+
const qs = search.toString();
|
|
129
|
+
const resp = await this._fetch(`/api/mkt/providers${qs ? `?${qs}` : ""}`);
|
|
130
|
+
return await parseResponse(resp);
|
|
131
|
+
}
|
|
132
|
+
async get(slug) {
|
|
133
|
+
const resp = await this._fetch(`/api/mkt/provider/${encodePath(slug)}`);
|
|
134
|
+
let data = await parseResponse(resp);
|
|
135
|
+
if (data && typeof data === "object" && "data" in data) {
|
|
136
|
+
data = data.data;
|
|
137
|
+
}
|
|
138
|
+
return data;
|
|
139
|
+
}
|
|
140
|
+
_fetch(path, init) {
|
|
141
|
+
return fetchWithOpts(this._opts, path, init);
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
var Marketplace = class {
|
|
145
|
+
models;
|
|
146
|
+
providers;
|
|
147
|
+
constructor(opts) {
|
|
148
|
+
this.models = new MarketplaceModels(opts);
|
|
149
|
+
this.providers = new MarketplaceProviders(opts);
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
function fetchWithOpts(opts, path, init) {
|
|
153
|
+
const url = `${opts.baseURL.replace(/\/$/, "")}${path}`;
|
|
154
|
+
const headers = {
|
|
155
|
+
"User-Agent": `onlist-js/${VERSION}`,
|
|
156
|
+
Accept: "application/json",
|
|
157
|
+
...init?.headers
|
|
158
|
+
};
|
|
159
|
+
if (opts.apiKey) {
|
|
160
|
+
headers["Authorization"] = `Bearer ${opts.apiKey}`;
|
|
161
|
+
}
|
|
162
|
+
const signal = AbortSignal.timeout(opts.timeout ?? DEFAULT_TIMEOUT);
|
|
163
|
+
return fetch(url, { ...init, headers, signal });
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// src/client.ts
|
|
167
|
+
var BASE_URL = "https://onlist.io/v1";
|
|
168
|
+
var MARKETPLACE_BASE_URL = "https://onlist.io";
|
|
169
|
+
var ENV_API_KEY = "ONLIST_API_KEY";
|
|
170
|
+
var Onlist = class extends OpenAI {
|
|
171
|
+
marketplace;
|
|
172
|
+
constructor(opts) {
|
|
173
|
+
const apiKey = opts?.apiKey ?? (typeof process !== "undefined" ? process.env?.[ENV_API_KEY] : void 0) ?? void 0;
|
|
174
|
+
const baseURL = opts?.baseURL ?? BASE_URL;
|
|
175
|
+
super({
|
|
176
|
+
...opts,
|
|
177
|
+
apiKey: apiKey ?? void 0,
|
|
178
|
+
baseURL,
|
|
179
|
+
defaultHeaders: {
|
|
180
|
+
"User-Agent": `onlist-js/${VERSION}`,
|
|
181
|
+
"HTTP-Referer": "https://onlist.io",
|
|
182
|
+
...opts?.defaultHeaders
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
const marketplaceBase = String(baseURL).split("/v1")[0] || MARKETPLACE_BASE_URL;
|
|
186
|
+
this.marketplace = new Marketplace({
|
|
187
|
+
apiKey: this.apiKey,
|
|
188
|
+
baseURL: marketplaceBase
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
export {
|
|
193
|
+
APIError,
|
|
194
|
+
AuthenticationError,
|
|
195
|
+
InsufficientBalanceError,
|
|
196
|
+
Marketplace,
|
|
197
|
+
MarketplaceModels,
|
|
198
|
+
MarketplaceProviders,
|
|
199
|
+
Onlist,
|
|
200
|
+
OnlistError,
|
|
201
|
+
ProviderError,
|
|
202
|
+
RateLimitError,
|
|
203
|
+
VERSION
|
|
204
|
+
};
|
|
205
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/client.ts","../src/errors.ts","../src/version.ts","../src/marketplace.ts"],"sourcesContent":["import OpenAI from \"openai\";\nimport type { ClientOptions } from \"openai\";\nimport { Marketplace } from \"./marketplace.js\";\nimport { VERSION } from \"./version.js\";\n\nconst BASE_URL = \"https://onlist.io/v1\";\nconst MARKETPLACE_BASE_URL = \"https://onlist.io\";\nconst ENV_API_KEY = \"ONLIST_API_KEY\";\n\nexport interface OnlistOptions extends Omit<ClientOptions, \"apiKey\" | \"baseURL\"> {\n apiKey?: string | null;\n baseURL?: string | null;\n}\n\nexport class Onlist extends OpenAI {\n readonly marketplace: Marketplace;\n\n constructor(opts?: OnlistOptions) {\n const apiKey =\n opts?.apiKey ??\n (typeof process !== \"undefined\" ? process.env?.[ENV_API_KEY] : undefined) ??\n undefined;\n\n const baseURL = opts?.baseURL ?? BASE_URL;\n\n super({\n ...opts,\n apiKey: apiKey ?? undefined,\n baseURL,\n defaultHeaders: {\n \"User-Agent\": `onlist-js/${VERSION}`,\n \"HTTP-Referer\": \"https://onlist.io\",\n ...opts?.defaultHeaders,\n },\n });\n\n const marketplaceBase = String(baseURL).split(\"/v1\")[0] || MARKETPLACE_BASE_URL;\n\n this.marketplace = new Marketplace({\n apiKey: this.apiKey,\n baseURL: marketplaceBase,\n });\n }\n}\n","export class OnlistError extends Error {\n constructor(message: string) {\n super(message);\n this.name = \"OnlistError\";\n }\n}\n\nexport class APIError extends OnlistError {\n readonly status: number;\n readonly type: string | null;\n readonly code: string | null;\n readonly param: string | null;\n readonly body: unknown;\n\n constructor(\n message: string,\n opts: {\n status: number;\n type?: string | null;\n code?: string | null;\n param?: string | null;\n body?: unknown;\n },\n ) {\n super(message);\n this.name = \"APIError\";\n this.status = opts.status;\n this.type = opts.type ?? null;\n this.code = opts.code ?? null;\n this.param = opts.param ?? null;\n this.body = opts.body ?? null;\n }\n}\n\nexport class AuthenticationError extends APIError {\n constructor(message = \"Invalid API key\", opts?: Partial<ConstructorParameters<typeof APIError>[1]>) {\n super(message, { status: 401, ...opts });\n this.name = \"AuthenticationError\";\n }\n}\n\nexport class InsufficientBalanceError extends APIError {\n constructor(message = \"Insufficient balance\", opts?: Partial<ConstructorParameters<typeof APIError>[1]>) {\n super(message, { status: 402, ...opts });\n this.name = \"InsufficientBalanceError\";\n }\n}\n\nexport class RateLimitError extends APIError {\n constructor(message = \"Rate limited\", opts?: Partial<ConstructorParameters<typeof APIError>[1]>) {\n super(message, { status: 429, ...opts });\n this.name = \"RateLimitError\";\n }\n}\n\nexport class ProviderError extends APIError {\n constructor(message: string, opts: ConstructorParameters<typeof APIError>[1]) {\n super(message, opts);\n this.name = \"ProviderError\";\n }\n}\n\nexport function raiseForStatus(status: number, body: unknown): never {\n let error: Record<string, unknown> = {};\n if (body && typeof body === \"object\" && \"error\" in body) {\n const e = (body as Record<string, unknown>).error;\n error = typeof e === \"object\" && e !== null ? (e as Record<string, unknown>) : {};\n } else if (body && typeof body === \"object\") {\n error = body as Record<string, unknown>;\n }\n\n const message = (typeof error.message === \"string\" ? error.message : String(body)) || \"Unknown error\";\n const type = typeof error.type === \"string\" ? error.type : null;\n const code = typeof error.code === \"string\" ? error.code : null;\n const param = typeof error.param === \"string\" ? error.param : null;\n const opts = { status, type, code, param, body };\n\n if (status === 401) throw new AuthenticationError(message, opts);\n if (status === 402) throw new InsufficientBalanceError(message, opts);\n if (status === 429) throw new RateLimitError(message, opts);\n if (code && code.startsWith(\"no_provider\")) throw new ProviderError(message, opts);\n\n throw new APIError(message, opts);\n}\n","export const VERSION = \"0.1.0\";\n","import { raiseForStatus } from \"./errors.js\";\nimport { VERSION } from \"./version.js\";\nimport type { ModelDetail, ModelListResponse } from \"./types/model.js\";\nimport type { ProviderDetail, ProviderListResponse } from \"./types/provider.js\";\n\nconst DEFAULT_TIMEOUT = 30_000;\n\nfunction encodePath(segment: string): string {\n return segment\n .split(\"/\")\n .map((s) => encodeURIComponent(s))\n .join(\"/\");\n}\n\nasync function parseResponse(response: Response): Promise<unknown> {\n let body: unknown;\n try {\n body = await response.json();\n } catch {\n body = await response.text().catch(() => \"\");\n }\n\n if (!response.ok) {\n raiseForStatus(response.status, body);\n }\n\n if (body && typeof body === \"object\" && \"data\" in body && \"success\" in body) {\n return (body as Record<string, unknown>).data;\n }\n return body;\n}\n\nexport interface MarketplaceOptions {\n apiKey?: string | null;\n baseURL: string;\n timeout?: number;\n}\n\nexport class MarketplaceModels {\n constructor(private readonly _opts: MarketplaceOptions) {}\n\n async list(params?: { limit?: number; offset?: number; q?: string }): Promise<ModelListResponse> {\n const search = new URLSearchParams();\n search.set(\"limit\", String(params?.limit ?? 20));\n search.set(\"offset\", String(params?.offset ?? 0));\n if (params?.q) search.set(\"q\", params.q);\n\n const resp = await this._fetch(`/api/mkt/models?${search}`);\n return (await parseResponse(resp)) as ModelListResponse;\n }\n\n async get(modelId: string): Promise<ModelDetail> {\n const resp = await this._fetch(`/api/mkt/models/${encodePath(modelId)}`);\n let data = (await parseResponse(resp)) as Record<string, unknown>;\n if (data && typeof data === \"object\" && \"data\" in data) {\n data = data.data as Record<string, unknown>;\n }\n return data as unknown as ModelDetail;\n }\n\n private _fetch(path: string, init?: RequestInit): Promise<Response> {\n return fetchWithOpts(this._opts, path, init);\n }\n}\n\nexport class MarketplaceProviders {\n constructor(private readonly _opts: MarketplaceOptions) {}\n\n async list(params?: { sort?: string; q?: string }): Promise<ProviderListResponse> {\n const search = new URLSearchParams();\n if (params?.sort) search.set(\"sort\", params.sort);\n if (params?.q) search.set(\"q\", params.q);\n\n const qs = search.toString();\n const resp = await this._fetch(`/api/mkt/providers${qs ? `?${qs}` : \"\"}`);\n return (await parseResponse(resp)) as ProviderListResponse;\n }\n\n async get(slug: string): Promise<ProviderDetail> {\n const resp = await this._fetch(`/api/mkt/provider/${encodePath(slug)}`);\n let data = (await parseResponse(resp)) as Record<string, unknown>;\n if (data && typeof data === \"object\" && \"data\" in data) {\n data = data.data as Record<string, unknown>;\n }\n return data as unknown as ProviderDetail;\n }\n\n private _fetch(path: string, init?: RequestInit): Promise<Response> {\n return fetchWithOpts(this._opts, path, init);\n }\n}\n\nexport class Marketplace {\n readonly models: MarketplaceModels;\n readonly providers: MarketplaceProviders;\n\n constructor(opts: MarketplaceOptions) {\n this.models = new MarketplaceModels(opts);\n this.providers = new MarketplaceProviders(opts);\n }\n}\n\nfunction fetchWithOpts(opts: MarketplaceOptions, path: string, init?: RequestInit): Promise<Response> {\n const url = `${opts.baseURL.replace(/\\/$/, \"\")}${path}`;\n const headers: Record<string, string> = {\n \"User-Agent\": `onlist-js/${VERSION}`,\n Accept: \"application/json\",\n ...(init?.headers as Record<string, string>),\n };\n if (opts.apiKey) {\n headers[\"Authorization\"] = `Bearer ${opts.apiKey}`;\n }\n const signal = AbortSignal.timeout(opts.timeout ?? DEFAULT_TIMEOUT);\n return fetch(url, { ...init, headers, signal });\n}\n"],"mappings":";AAAA,OAAO,YAAY;;;ACAZ,IAAM,cAAN,cAA0B,MAAM;AAAA,EACrC,YAAY,SAAiB;AAC3B,UA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AI,YAAY;AAAA,MACjC,QAAQ,KAAK;AAAA,MACb,SAAS;AAAA,IACX,CAAC;AAAA,EACH;AACF;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@onlist/sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Official JavaScript/TypeScript SDK for Onlist, the AI API marketplace. Access 200+ AI models (GPT, Claude, Gemini, DeepSeek, Llama) through a unified OpenAI-compatible API with provider routing, marketplace data, and competitive pricing.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"onlist",
|
|
7
|
+
"ai",
|
|
8
|
+
"api",
|
|
9
|
+
"llm",
|
|
10
|
+
"openai",
|
|
11
|
+
"claude",
|
|
12
|
+
"gpt",
|
|
13
|
+
"gemini",
|
|
14
|
+
"deepseek",
|
|
15
|
+
"llama",
|
|
16
|
+
"marketplace",
|
|
17
|
+
"sdk",
|
|
18
|
+
"openrouter",
|
|
19
|
+
"ai-gateway",
|
|
20
|
+
"provider-routing",
|
|
21
|
+
"typescript",
|
|
22
|
+
"machine-learning",
|
|
23
|
+
"artificial-intelligence"
|
|
24
|
+
],
|
|
25
|
+
"homepage": "https://onlist.io",
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "https://github.com/OnlistTeam/onlist-js.git"
|
|
29
|
+
},
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/OnlistTeam/onlist-js/issues"
|
|
32
|
+
},
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"author": "Onlist <dev@onlist.io>",
|
|
35
|
+
"type": "module",
|
|
36
|
+
"exports": {
|
|
37
|
+
".": {
|
|
38
|
+
"import": {
|
|
39
|
+
"types": "./dist/index.d.ts",
|
|
40
|
+
"default": "./dist/index.js"
|
|
41
|
+
},
|
|
42
|
+
"require": {
|
|
43
|
+
"types": "./dist/index.d.cts",
|
|
44
|
+
"default": "./dist/index.cjs"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
"main": "./dist/index.cjs",
|
|
49
|
+
"module": "./dist/index.js",
|
|
50
|
+
"types": "./dist/index.d.ts",
|
|
51
|
+
"files": [
|
|
52
|
+
"dist",
|
|
53
|
+
"LICENSE",
|
|
54
|
+
"README.md"
|
|
55
|
+
],
|
|
56
|
+
"scripts": {
|
|
57
|
+
"build": "tsup",
|
|
58
|
+
"test": "vitest run",
|
|
59
|
+
"test:watch": "vitest",
|
|
60
|
+
"typecheck": "tsc --noEmit",
|
|
61
|
+
"lint": "eslint src/ tests/",
|
|
62
|
+
"prepublishOnly": "npm run build"
|
|
63
|
+
},
|
|
64
|
+
"dependencies": {
|
|
65
|
+
"openai": ">=4.0.0"
|
|
66
|
+
},
|
|
67
|
+
"devDependencies": {
|
|
68
|
+
"@types/node": "^20.0.0",
|
|
69
|
+
"eslint": "^9.0.0",
|
|
70
|
+
"tsup": "^8.0.0",
|
|
71
|
+
"typescript": "^5.5.0",
|
|
72
|
+
"vitest": "^3.0.0"
|
|
73
|
+
},
|
|
74
|
+
"engines": {
|
|
75
|
+
"node": ">=18.0.0"
|
|
76
|
+
}
|
|
77
|
+
}
|