v0-sdk 0.0.5 → 0.0.7

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/dist/index.js CHANGED
@@ -1,105 +1,273 @@
1
- /**
2
- * v0 Chats API SDK
3
- * A client library for interacting with the v0 Chats API
4
- */ class V0ChatsError extends Error {
5
- constructor(message, status, data){
6
- super(message);
7
- this.name = 'V0ChatsError';
8
- this.status = status;
9
- this.data = data;
1
+ let BASE_URL = 'https://api.v0.dev/v1';
2
+ async function fetcher(url, method, params = {}) {
3
+ const queryString = params.query ? '?' + new URLSearchParams(params.query).toString() : '';
4
+ const finalUrl = BASE_URL + url + queryString;
5
+ const apiKey = process.env.V0_API_KEY;
6
+ if (!apiKey) {
7
+ throw new Error('V0_API_KEY environment variable is required');
10
8
  }
11
- }
12
- /**
13
- * Client for the v0 Chats API
14
- */ class V0Client {
15
- /**
16
- * Create a new v0 Chats API client
17
- *
18
- * @param options Client configuration options
19
- */ constructor(options){
20
- this.apiKey = options.apiKey;
21
- this.baseUrl = options.baseUrl || 'https://api.v0.dev/v1';
22
- this.fetchFn = options.fetch || fetch;
23
- }
24
- /**
25
- * Create a new chat
26
- *
27
- * @param options Options for creating a new chat
28
- * @returns Promise with the created chat details
29
- */ async createChat(options) {
30
- const response = await this.request('POST', '/chats', options);
31
- return response;
32
- }
33
- /**
34
- * Add a message to an existing chat
35
- *
36
- * @param chatId The ID of the chat to add a message to
37
- * @param options Options for adding a message
38
- * @returns Promise with the updated chat details
39
- */ async addMessage(chatId, options) {
40
- const response = await this.request('POST', `/chats/${chatId}`, options);
41
- return response;
42
- }
43
- /**
44
- * Get chat details and message history
45
- *
46
- * @param chatId The ID of the chat to retrieve
47
- * @returns Promise with the chat details and messages
48
- */ async getChat(chatId) {
49
- const response = await this.request('GET', `/chats/${chatId}`);
50
- return response;
9
+ const hasBody = method !== 'GET' && params.body;
10
+ const headers = {
11
+ Authorization: `Bearer ${apiKey}`,
12
+ ...params.headers
13
+ };
14
+ if (hasBody) {
15
+ headers['Content-Type'] = 'application/json';
51
16
  }
52
- /**
53
- * Continue an existing chat (deprecated)
54
- *
55
- * This method is deprecated. Use addMessage() instead.
56
- *
57
- * @deprecated Use addMessage() instead
58
- * @param chatId The ID of the chat to continue
59
- * @param options Options for continuing the chat
60
- * @returns Promise with the updated chat details
61
- */ async continueChat(chatId, options) {
62
- console.warn('continueChat() is deprecated. Use addMessage() instead.');
63
- const payload = {
64
- ...options,
65
- chatId
66
- };
67
- const response = await this.request('POST', '/chats', payload);
68
- return response;
17
+ const res = await fetch(finalUrl, {
18
+ method,
19
+ headers,
20
+ body: hasBody ? JSON.stringify(params.body) : undefined
21
+ });
22
+ if (!res.ok) {
23
+ const text = await res.text();
24
+ throw new Error(`HTTP ${res.status}: ${text}`);
69
25
  }
70
- /**
71
- * Make a request to the v0 Chats API
72
- *
73
- * @param method HTTP method
74
- * @param path API path
75
- * @param data Request data
76
- * @returns Promise with the response data
77
- */ async request(method, path, data) {
78
- const url = `${this.baseUrl}${path}`;
79
- const headers = {
80
- Authorization: `Bearer ${this.apiKey}`,
81
- 'Content-Type': 'application/json'
82
- };
83
- const options = {
84
- method,
85
- headers,
86
- body: data && method !== 'GET' ? JSON.stringify(data) : undefined
87
- };
88
- try {
89
- const response = await this.fetchFn(url, options);
90
- const responseData = await response.json();
91
- if (!response.ok) {
92
- throw new V0ChatsError(responseData.error || `API request failed with status ${response.status}`, response.status, responseData);
93
- }
94
- return responseData;
95
- } catch (error) {
96
- if (error instanceof V0ChatsError) {
97
- throw error;
26
+ return res.json();
27
+ }
28
+
29
+ const v0 = {
30
+ chats: {
31
+ async create (params) {
32
+ const body = {
33
+ message: params.message,
34
+ attachments: params.attachments,
35
+ system: params.system,
36
+ chatPrivacy: params.chatPrivacy,
37
+ projectId: params.projectId,
38
+ modelConfiguration: params.modelConfiguration
39
+ };
40
+ return fetcher(`/chats`, 'POST', {
41
+ body
42
+ });
43
+ },
44
+ async find (params) {
45
+ const query = params ? Object.fromEntries(Object.entries({
46
+ limit: params.limit,
47
+ offset: params.offset,
48
+ isFavorite: params.isFavorite
49
+ }).filter(([_, value])=>value !== undefined)) : {};
50
+ const hasQuery = Object.keys(query).length > 0;
51
+ return fetcher(`/chats`, 'GET', {
52
+ ...hasQuery ? {
53
+ query
54
+ } : {}
55
+ });
56
+ },
57
+ async delete (params) {
58
+ const pathParams = {
59
+ chatId: params.chatId
60
+ };
61
+ return fetcher(`/chats/${pathParams.chatId}`, 'DELETE', {
62
+ });
63
+ },
64
+ async getById (params) {
65
+ const pathParams = {
66
+ chatId: params.chatId
67
+ };
68
+ return fetcher(`/chats/${pathParams.chatId}`, 'GET', {
69
+ });
70
+ },
71
+ async update (params) {
72
+ const pathParams = {
73
+ chatId: params.chatId
74
+ };
75
+ const body = {
76
+ privacy: params.privacy
77
+ };
78
+ return fetcher(`/chats/${pathParams.chatId}`, 'PATCH', {
79
+ body
80
+ });
81
+ },
82
+ async favorite (params) {
83
+ const pathParams = {
84
+ chatId: params.chatId
85
+ };
86
+ const body = {
87
+ isFavorite: params.isFavorite
88
+ };
89
+ return fetcher(`/chats/${pathParams.chatId}/favorite`, 'PUT', {
90
+ body
91
+ });
92
+ },
93
+ async fork (params) {
94
+ const pathParams = {
95
+ chatId: params.chatId
96
+ };
97
+ const body = {
98
+ versionId: params.versionId
99
+ };
100
+ return fetcher(`/chats/${pathParams.chatId}/fork`, 'POST', {
101
+ body
102
+ });
103
+ },
104
+ async createMessage (params) {
105
+ const pathParams = {
106
+ chatId: params.chatId
107
+ };
108
+ const body = {
109
+ message: params.message,
110
+ attachments: params.attachments,
111
+ modelConfiguration: params.modelConfiguration
112
+ };
113
+ return fetcher(`/chats/${pathParams.chatId}/messages`, 'POST', {
114
+ body
115
+ });
116
+ },
117
+ async findIframe (params) {
118
+ const pathParams = {
119
+ chatId: params.chatId,
120
+ versionId: params.versionId
121
+ };
122
+ return fetcher(`/chats/${pathParams.chatId}/versions/${pathParams.versionId}/iframe`, 'GET', {
123
+ });
124
+ },
125
+ async getVersionFrameToken (params) {
126
+ const pathParams = {
127
+ chatId: params.chatId,
128
+ versionId: params.versionId
129
+ };
130
+ return fetcher(`/chats/${pathParams.chatId}/versions/${pathParams.versionId}/frame-token`, 'GET', {
131
+ });
132
+ },
133
+ async getMetadata (params) {
134
+ const pathParams = {
135
+ chatId: params.chatId
136
+ };
137
+ return fetcher(`/chats/${pathParams.chatId}/metadata`, 'GET', {
138
+ });
139
+ },
140
+ async upload (params) {
141
+ const pathParams = {
142
+ chatId: params.chatId
143
+ };
144
+ const body = {
145
+ file: params.file
146
+ };
147
+ return fetcher(`/chats/${pathParams.chatId}/upload`, 'POST', {
148
+ body
149
+ });
150
+ },
151
+ async resume (params) {
152
+ const pathParams = {
153
+ chatId: params.chatId,
154
+ messageId: params.messageId
155
+ };
156
+ return fetcher(`/chats/${pathParams.chatId}/messages/${pathParams.messageId}/resume`, 'POST', {
157
+ });
158
+ }
159
+ },
160
+ projects: {
161
+ async getByChatId (params) {
162
+ const pathParams = {
163
+ chatId: params.chatId
164
+ };
165
+ return fetcher(`/chats/${pathParams.chatId}/project`, 'GET', {
166
+ });
167
+ },
168
+ async find () {
169
+ return fetcher(`/projects`, 'GET', {});
170
+ },
171
+ async create (params) {
172
+ const body = {
173
+ name: params.name,
174
+ description: params.description,
175
+ icon: params.icon,
176
+ environmentVariables: params.environmentVariables,
177
+ instructions: params.instructions
178
+ };
179
+ return fetcher(`/projects`, 'POST', {
180
+ body
181
+ });
182
+ },
183
+ async assign (params) {
184
+ const pathParams = {
185
+ projectId: params.projectId
186
+ };
187
+ const body = {
188
+ chatId: params.chatId
189
+ };
190
+ return fetcher(`/projects/${pathParams.projectId}/assign`, 'POST', {
191
+ body
192
+ });
193
+ }
194
+ },
195
+ deployments: {
196
+ async findLogs (params) {
197
+ const pathParams = {
198
+ deploymentId: params.deploymentId
199
+ };
200
+ const query = Object.fromEntries(Object.entries({
201
+ since: params.since
202
+ }).filter(([_, value])=>value !== undefined));
203
+ const hasQuery = Object.keys(query).length > 0;
204
+ return fetcher(`/deployments/${pathParams.deploymentId}/logs`, 'GET', {
205
+ ...hasQuery ? {
206
+ query
207
+ } : {}
208
+ });
209
+ },
210
+ async findErrors (params) {
211
+ const pathParams = {
212
+ deploymentId: params.deploymentId
213
+ };
214
+ return fetcher(`/deployments/${pathParams.deploymentId}/errors`, 'GET', {
215
+ });
216
+ }
217
+ },
218
+ integrations: {
219
+ vercel: {
220
+ projects: {
221
+ async find () {
222
+ return fetcher(`/integrations/vercel/projects`, 'GET', {});
223
+ },
224
+ async create (params) {
225
+ const body = {
226
+ projectId: params.projectId,
227
+ name: params.name
228
+ };
229
+ return fetcher(`/integrations/vercel/projects`, 'POST', {
230
+ body
231
+ });
232
+ }
98
233
  }
99
- throw new V0ChatsError(error instanceof Error ? error.message : 'Unknown error occurred', 500);
234
+ }
235
+ },
236
+ rateLimits: {
237
+ async find (params) {
238
+ const query = params ? Object.fromEntries(Object.entries({
239
+ scope: params.scope
240
+ }).filter(([_, value])=>value !== undefined)) : {};
241
+ const hasQuery = Object.keys(query).length > 0;
242
+ return fetcher(`/rate-limits`, 'GET', {
243
+ ...hasQuery ? {
244
+ query
245
+ } : {}
246
+ });
247
+ }
248
+ },
249
+ user: {
250
+ async get () {
251
+ return fetcher(`/user`, 'GET', {});
252
+ },
253
+ async getBilling (params) {
254
+ const query = params ? Object.fromEntries(Object.entries({
255
+ scope: params.scope
256
+ }).filter(([_, value])=>value !== undefined)) : {};
257
+ const hasQuery = Object.keys(query).length > 0;
258
+ return fetcher(`/user/billing`, 'GET', {
259
+ ...hasQuery ? {
260
+ query
261
+ } : {}
262
+ });
263
+ },
264
+ async getPlan () {
265
+ return fetcher(`/user/plan`, 'GET', {});
266
+ },
267
+ async getScopes () {
268
+ return fetcher(`/user/scopes`, 'GET', {});
100
269
  }
101
270
  }
102
- }
271
+ };
103
272
 
104
- exports.V0ChatsError = V0ChatsError;
105
- exports.V0Client = V0Client;
273
+ export { v0 };
package/package.json CHANGED
@@ -1,11 +1,28 @@
1
1
  {
2
2
  "name": "v0-sdk",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "TypeScript SDK for the v0 Chats API",
5
+ "type": "module",
5
6
  "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js",
12
+ "require": "./dist/index.cjs"
13
+ }
14
+ },
6
15
  "files": [
7
16
  "dist"
8
17
  ],
18
+ "scripts": {
19
+ "type-check": "tsc --noEmit",
20
+ "type-check:go": "tsgo --noEmit",
21
+ "build": "bunchee",
22
+ "generate": "tsx src/scripts/generate.ts && pnpm format",
23
+ "format": "prettier --write \"**/*.{ts,tsx,md}\"",
24
+ "test": "vitest"
25
+ },
9
26
  "keywords": [
10
27
  "v0",
11
28
  "vercel",
@@ -16,10 +33,11 @@
16
33
  "author": "",
17
34
  "license": "MIT",
18
35
  "devDependencies": {
36
+ "@types/node": "22.5.5",
37
+ "@typescript/native-preview": "7.0.0-dev.20250613.1",
19
38
  "bunchee": "^6.5.2",
39
+ "prettier": "^3.3.3",
40
+ "tsx": "^4.19.2",
20
41
  "typescript": "5.7.3"
21
- },
22
- "scripts": {
23
- "build": "bunchee"
24
42
  }
25
- }
43
+ }