@blinkdotnew/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 +343 -0
- package/dist/index.d.mts +563 -0
- package/dist/index.d.ts +563 -0
- package/dist/index.js +2230 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2224 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Blink Team
|
|
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,343 @@
|
|
|
1
|
+
# @blinkdotnew/sdk
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/js/%40blinkdotnew%2Fsdk)
|
|
4
|
+
[](https://www.typescriptlang.org/)
|
|
5
|
+
|
|
6
|
+
**Zero-boilerplate CRUD + auth for modern SaaS/AI apps**
|
|
7
|
+
|
|
8
|
+
Blink SDK provides a Firebase/Supabase-like experience with built-in authentication, database operations, AI capabilities, and file storage. Works seamlessly on both client-side (React, Vue, etc.) and server-side (Node.js, Deno, Edge functions).
|
|
9
|
+
|
|
10
|
+
## 🚀 Quick Start
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install @blinkdotnew/sdk
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
import { createClient } from '@blinkdotnew/sdk'
|
|
18
|
+
|
|
19
|
+
const blink = createClient({
|
|
20
|
+
projectId: 'your-project-id',
|
|
21
|
+
authRequired: true
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
// Authentication
|
|
25
|
+
const user = await blink.auth.me()
|
|
26
|
+
|
|
27
|
+
// Database operations
|
|
28
|
+
const todos = await blink.db.todos.list({
|
|
29
|
+
where: { user_id: user.id },
|
|
30
|
+
orderBy: { created_at: 'desc' },
|
|
31
|
+
limit: 20
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
// AI operations
|
|
35
|
+
const { text } = await blink.ai.generateText({
|
|
36
|
+
prompt: "Write a summary of the user's todos"
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
// Storage operations
|
|
40
|
+
const { publicUrl } = await blink.storage.upload(
|
|
41
|
+
file,
|
|
42
|
+
`avatars/${user.id}.png`,
|
|
43
|
+
{ upsert: true }
|
|
44
|
+
)
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## 📚 Features
|
|
48
|
+
|
|
49
|
+
- **🔐 Authentication**: JWT-based auth with automatic token management
|
|
50
|
+
- **🗄️ Database**: PostgREST-compatible CRUD operations with advanced filtering
|
|
51
|
+
- **🤖 AI**: Text generation, object generation, image creation, speech synthesis, and transcription
|
|
52
|
+
- **📁 Storage**: File upload, download, and management
|
|
53
|
+
- **🌐 Universal**: Works on client-side and server-side
|
|
54
|
+
- **📱 Framework Agnostic**: React, Vue, Svelte, vanilla JS, Node.js, Deno
|
|
55
|
+
- **🔄 Real-time**: Built-in auth state management and token refresh
|
|
56
|
+
|
|
57
|
+
## 🛠️ Installation & Setup
|
|
58
|
+
|
|
59
|
+
### Client-side (React, Vue, etc.)
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
import { createClient } from '@blinkdotnew/sdk'
|
|
63
|
+
|
|
64
|
+
const blink = createClient({
|
|
65
|
+
projectId: 'your-project-id',
|
|
66
|
+
authRequired: true // Automatic auth redirect
|
|
67
|
+
})
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Server-side (Node.js, Deno, Edge functions)
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
import { createClient } from '@blinkdotnew/sdk'
|
|
74
|
+
|
|
75
|
+
const blink = createClient({
|
|
76
|
+
projectId: 'your-project-id',
|
|
77
|
+
authRequired: false // Manual token management
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
// Set JWT manually
|
|
81
|
+
blink.auth.setToken(jwtFromHeader)
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## 📖 API Reference
|
|
85
|
+
|
|
86
|
+
### Authentication
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
// Login/logout
|
|
90
|
+
blink.auth.login(nextUrl?)
|
|
91
|
+
blink.auth.logout(redirectUrl?)
|
|
92
|
+
|
|
93
|
+
// User management
|
|
94
|
+
const user = await blink.auth.me()
|
|
95
|
+
await blink.auth.updateMe({ displayName: 'New Name' })
|
|
96
|
+
|
|
97
|
+
// Token management
|
|
98
|
+
blink.auth.setToken(jwt, persist?)
|
|
99
|
+
const isAuth = blink.auth.isAuthenticated()
|
|
100
|
+
|
|
101
|
+
// Auth state listener
|
|
102
|
+
const unsubscribe = blink.auth.onAuthStateChanged((state) => {
|
|
103
|
+
console.log('Auth state:', state)
|
|
104
|
+
})
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Database Operations
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
// Create
|
|
111
|
+
const todo = await blink.db.todos.create({
|
|
112
|
+
title: 'Learn Blink SDK',
|
|
113
|
+
user_id: user.id
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
// Read with filtering
|
|
117
|
+
const todos = await blink.db.todos.list({
|
|
118
|
+
where: {
|
|
119
|
+
AND: [
|
|
120
|
+
{ user_id: user.id },
|
|
121
|
+
{ OR: [{ status: 'open' }, { priority: 'high' }] }
|
|
122
|
+
]
|
|
123
|
+
},
|
|
124
|
+
orderBy: { created_at: 'desc' },
|
|
125
|
+
limit: 20
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
// Update
|
|
129
|
+
await blink.db.todos.update(todo.id, { completed: true })
|
|
130
|
+
|
|
131
|
+
// Delete
|
|
132
|
+
await blink.db.todos.delete(todo.id)
|
|
133
|
+
|
|
134
|
+
// Bulk operations
|
|
135
|
+
await blink.db.todos.createMany([...])
|
|
136
|
+
await blink.db.todos.upsertMany([...])
|
|
137
|
+
|
|
138
|
+
// Advanced queries
|
|
139
|
+
const count = await blink.db.todos.count({ where: { user_id: user.id } })
|
|
140
|
+
const exists = await blink.db.todos.exists({ where: { urgent: true } })
|
|
141
|
+
|
|
142
|
+
// Raw SQL
|
|
143
|
+
const result = await blink.db.sql('SELECT * FROM todos WHERE user_id = ?', [user.id])
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### AI Operations
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
// Text generation
|
|
150
|
+
const { text } = await blink.ai.generateText({
|
|
151
|
+
prompt: 'Write a poem about coding',
|
|
152
|
+
model: 'gpt-4o-mini',
|
|
153
|
+
maxTokens: 150
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
// Structured object generation
|
|
157
|
+
const { object } = await blink.ai.generateObject({
|
|
158
|
+
prompt: 'Generate a user profile',
|
|
159
|
+
schema: {
|
|
160
|
+
type: 'object',
|
|
161
|
+
properties: {
|
|
162
|
+
name: { type: 'string' },
|
|
163
|
+
age: { type: 'number' }
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
// Image generation
|
|
169
|
+
const { data } = await blink.ai.generateImage({
|
|
170
|
+
prompt: 'A serene landscape',
|
|
171
|
+
size: '1024x1024',
|
|
172
|
+
quality: 'hd'
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
// Speech synthesis
|
|
176
|
+
const { url } = await blink.ai.generateSpeech({
|
|
177
|
+
text: 'Hello, world!',
|
|
178
|
+
voice: 'nova'
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
// Audio transcription
|
|
182
|
+
const { text } = await blink.ai.transcribeAudio({
|
|
183
|
+
audio: audioFile,
|
|
184
|
+
language: 'en'
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
// Streaming support
|
|
188
|
+
await blink.ai.streamText(
|
|
189
|
+
{ prompt: 'Write a story...' },
|
|
190
|
+
(chunk) => console.log(chunk)
|
|
191
|
+
)
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Storage Operations
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
// Upload files
|
|
198
|
+
const { publicUrl } = await blink.storage.upload(
|
|
199
|
+
file,
|
|
200
|
+
'path/to/file.jpg',
|
|
201
|
+
{
|
|
202
|
+
upsert: true,
|
|
203
|
+
onProgress: (percent) => console.log(`${percent}%`)
|
|
204
|
+
}
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
// List files
|
|
208
|
+
const files = await blink.storage.list('folder/')
|
|
209
|
+
|
|
210
|
+
// Get public URL
|
|
211
|
+
const url = await blink.storage.getPublicUrl('path/to/file.jpg')
|
|
212
|
+
|
|
213
|
+
// Remove files
|
|
214
|
+
await blink.storage.remove('file1.jpg', 'file2.jpg')
|
|
215
|
+
|
|
216
|
+
// Copy/move files
|
|
217
|
+
await blink.storage.copy('source.jpg', 'destination.jpg')
|
|
218
|
+
await blink.storage.move('old-path.jpg', 'new-path.jpg')
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## 🔧 Advanced Usage
|
|
222
|
+
|
|
223
|
+
### Error Handling
|
|
224
|
+
|
|
225
|
+
```typescript
|
|
226
|
+
import { BlinkAuthError, BlinkAIError, BlinkStorageError } from '@blinkdotnew/sdk'
|
|
227
|
+
|
|
228
|
+
try {
|
|
229
|
+
const user = await blink.auth.me()
|
|
230
|
+
} catch (error) {
|
|
231
|
+
if (error instanceof BlinkAuthError) {
|
|
232
|
+
// Handle auth errors
|
|
233
|
+
console.error('Auth error:', error.message)
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
### Custom Configuration
|
|
239
|
+
|
|
240
|
+
```typescript
|
|
241
|
+
const blink = createClient({
|
|
242
|
+
projectId: 'your-project',
|
|
243
|
+
baseUrl: 'https://custom-api.example.com',
|
|
244
|
+
authRequired: true,
|
|
245
|
+
httpClient: {
|
|
246
|
+
timeout: 30000,
|
|
247
|
+
retries: 3
|
|
248
|
+
}
|
|
249
|
+
})
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
### TypeScript Support
|
|
253
|
+
|
|
254
|
+
The SDK is written in TypeScript and provides full type safety:
|
|
255
|
+
|
|
256
|
+
```typescript
|
|
257
|
+
interface Todo {
|
|
258
|
+
id: string
|
|
259
|
+
title: string
|
|
260
|
+
completed: boolean
|
|
261
|
+
user_id: string
|
|
262
|
+
created_at: string
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const todos = await blink.db.todos.list<Todo>({
|
|
266
|
+
where: { completed: false }
|
|
267
|
+
})
|
|
268
|
+
// todos is fully typed as ListResponse<Todo>
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## 🌍 Framework Examples
|
|
272
|
+
|
|
273
|
+
### React
|
|
274
|
+
|
|
275
|
+
```typescript
|
|
276
|
+
import { createClient } from '@blinkdotnew/sdk'
|
|
277
|
+
import { useState, useEffect } from 'react'
|
|
278
|
+
|
|
279
|
+
const blink = createClient({ projectId: 'your-project', authRequired: true })
|
|
280
|
+
|
|
281
|
+
function App() {
|
|
282
|
+
const [user, setUser] = useState(null)
|
|
283
|
+
|
|
284
|
+
useEffect(() => {
|
|
285
|
+
const unsubscribe = blink.auth.onAuthStateChanged((state) => {
|
|
286
|
+
setUser(state.user)
|
|
287
|
+
})
|
|
288
|
+
return unsubscribe
|
|
289
|
+
}, [])
|
|
290
|
+
|
|
291
|
+
if (!user) return <div>Please log in</div>
|
|
292
|
+
|
|
293
|
+
return <div>Welcome, {user.email}!</div>
|
|
294
|
+
}
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
### Next.js API Routes
|
|
298
|
+
|
|
299
|
+
```typescript
|
|
300
|
+
// pages/api/todos.ts
|
|
301
|
+
import { createClient } from '@blinkdotnew/sdk'
|
|
302
|
+
|
|
303
|
+
const blink = createClient({ projectId: 'your-project', authRequired: false })
|
|
304
|
+
|
|
305
|
+
export default async function handler(req, res) {
|
|
306
|
+
const jwt = req.headers.authorization?.replace('Bearer ', '')
|
|
307
|
+
blink.auth.setToken(jwt)
|
|
308
|
+
|
|
309
|
+
const todos = await blink.db.todos.list()
|
|
310
|
+
res.json(todos)
|
|
311
|
+
}
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
### Deno Edge Function
|
|
315
|
+
|
|
316
|
+
```typescript
|
|
317
|
+
import { createClient } from '@blinkdotnew/sdk'
|
|
318
|
+
|
|
319
|
+
const blink = createClient({ projectId: 'your-project', authRequired: false })
|
|
320
|
+
|
|
321
|
+
Deno.serve(async (req) => {
|
|
322
|
+
const jwt = req.headers.get('authorization')?.replace('Bearer ', '')
|
|
323
|
+
blink.auth.setToken(jwt)
|
|
324
|
+
|
|
325
|
+
const todos = await blink.db.todos.list()
|
|
326
|
+
return Response.json(todos)
|
|
327
|
+
})
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
## 🔗 Links
|
|
331
|
+
|
|
332
|
+
- **Documentation**: [https://docs.blink.new](https://docs.blink.new)
|
|
333
|
+
- **GitHub**: [https://github.com/ShadowWalker2014/blink-sdk](https://github.com/ShadowWalker2014/blink-sdk)
|
|
334
|
+
- **Website**: [https://blink.new](https://blink.new)
|
|
335
|
+
- **Discord**: [Join our community](https://discord.gg/blink)
|
|
336
|
+
|
|
337
|
+
## 📄 License
|
|
338
|
+
|
|
339
|
+
MIT © Blink Team
|
|
340
|
+
|
|
341
|
+
---
|
|
342
|
+
|
|
343
|
+
**Made with ❤️ by the Blink team**
|