wavespeed 0.0.1 → 0.0.3
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 +48 -0
- package/dist/index.js +22 -6
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -73,6 +73,54 @@ async function generateImage(): Promise<void> {
|
|
|
73
73
|
generateImage().catch(console.error);
|
|
74
74
|
```
|
|
75
75
|
|
|
76
|
+
### Manual Status Polling Example
|
|
77
|
+
|
|
78
|
+
If you need more control over the polling process, you can use the `create` method and manually poll for status updates:
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
import WaveSpeed from 'wavespeed';
|
|
82
|
+
|
|
83
|
+
// Initialize the client with your API key
|
|
84
|
+
const client = new WaveSpeed('YOUR_API_KEY');
|
|
85
|
+
|
|
86
|
+
async function generateWithManualPolling(): Promise<void> {
|
|
87
|
+
// Create a prediction without waiting
|
|
88
|
+
const prediction = await client.create('wavespeed-ai/flux-dev', {
|
|
89
|
+
prompt: 'A beautiful mountain landscape at sunset',
|
|
90
|
+
size: '1024*1024',
|
|
91
|
+
num_inference_steps: 28,
|
|
92
|
+
guidance_scale: 5.0,
|
|
93
|
+
num_images: 1
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
console.log(`Prediction created with ID: ${prediction.id}`);
|
|
97
|
+
console.log(`Initial status: ${prediction.status}`);
|
|
98
|
+
|
|
99
|
+
// Manually poll for status updates
|
|
100
|
+
let currentPrediction = prediction;
|
|
101
|
+
|
|
102
|
+
while (currentPrediction.status !== 'completed' && currentPrediction.status !== 'failed') {
|
|
103
|
+
console.log('Prediction still processing, checking again in 2 seconds...');
|
|
104
|
+
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
105
|
+
|
|
106
|
+
// Reload the prediction to get the latest status
|
|
107
|
+
currentPrediction = await currentPrediction.reload();
|
|
108
|
+
console.log(`Updated status: ${currentPrediction.status}`);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (currentPrediction.status === 'completed') {
|
|
112
|
+
console.log('Prediction completed successfully!');
|
|
113
|
+
currentPrediction.outputs.forEach((imgUrl, i) => {
|
|
114
|
+
console.log(`Image ${i+1}: ${imgUrl}`);
|
|
115
|
+
});
|
|
116
|
+
} else {
|
|
117
|
+
console.error(`Prediction failed: ${currentPrediction.error}`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
generateWithManualPolling().catch(console.error);
|
|
122
|
+
```
|
|
123
|
+
|
|
76
124
|
## API Reference
|
|
77
125
|
|
|
78
126
|
### WaveSpeed Client
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Input parameters for image generation
|
|
4
|
+
*/
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.WaveSpeed = exports.Prediction = void 0;
|
|
4
|
-
const url_1 = require("url");
|
|
5
7
|
/**
|
|
6
8
|
* Prediction model representing an image generation job
|
|
7
9
|
*/
|
|
@@ -73,15 +75,23 @@ class WaveSpeed {
|
|
|
73
75
|
*/
|
|
74
76
|
constructor(apiKey, options = {}) {
|
|
75
77
|
this.baseUrl = 'https://api.wavespeed.ai/api/v2/';
|
|
76
|
-
|
|
78
|
+
// Browser-friendly environment variable handling
|
|
79
|
+
const getEnvVar = (name) => {
|
|
80
|
+
// Try to get from process.env for Node.js environments
|
|
81
|
+
if (typeof process !== 'undefined' && process.env && process.env[name]) {
|
|
82
|
+
return process.env[name];
|
|
83
|
+
}
|
|
84
|
+
return undefined;
|
|
85
|
+
};
|
|
86
|
+
this.apiKey = apiKey || getEnvVar('WAVESPEED_API_KEY') || '';
|
|
77
87
|
if (!this.apiKey) {
|
|
78
88
|
throw new Error('API key is required. Provide it as a parameter or set the WAVESPEED_API_KEY environment variable.');
|
|
79
89
|
}
|
|
80
90
|
if (options.baseUrl) {
|
|
81
91
|
this.baseUrl = options.baseUrl;
|
|
82
92
|
}
|
|
83
|
-
this.pollInterval = options.pollInterval || Number(
|
|
84
|
-
this.timeout = options.timeout || Number(
|
|
93
|
+
this.pollInterval = options.pollInterval || Number(getEnvVar('WAVESPEED_POLL_INTERVAL')) || 1;
|
|
94
|
+
this.timeout = options.timeout || Number(getEnvVar('WAVESPEED_TIMEOUT')) || 60;
|
|
85
95
|
}
|
|
86
96
|
/**
|
|
87
97
|
* Fetch with timeout support
|
|
@@ -97,11 +107,13 @@ class WaveSpeed {
|
|
|
97
107
|
'Authorization': `Bearer ${this.apiKey}`,
|
|
98
108
|
'Content-Type': 'application/json'
|
|
99
109
|
};
|
|
110
|
+
// Use AbortController for timeout (supported in modern browsers)
|
|
100
111
|
const controller = new AbortController();
|
|
101
112
|
const id = setTimeout(() => controller.abort(), timeout);
|
|
102
113
|
try {
|
|
103
|
-
//
|
|
104
|
-
const url = new
|
|
114
|
+
// Use browser's built-in URL API
|
|
115
|
+
const url = new URL(path, this.baseUrl).toString();
|
|
116
|
+
// Use the global fetch API available in browsers
|
|
105
117
|
const response = await fetch(url, {
|
|
106
118
|
...fetchOptions,
|
|
107
119
|
signal: controller.signal
|
|
@@ -147,3 +159,7 @@ class WaveSpeed {
|
|
|
147
159
|
exports.WaveSpeed = WaveSpeed;
|
|
148
160
|
// Export default and named exports for different import styles
|
|
149
161
|
exports.default = WaveSpeed;
|
|
162
|
+
// Add browser global for UMD-style usage
|
|
163
|
+
if (typeof window !== 'undefined') {
|
|
164
|
+
window.WaveSpeed = WaveSpeed;
|
|
165
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wavespeed",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "Client SDK for Wavespeed API",
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"description": "WaveSpeed Client SDK for Wavespeed API",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
@@ -34,4 +34,4 @@
|
|
|
34
34
|
"files": [
|
|
35
35
|
"dist"
|
|
36
36
|
]
|
|
37
|
-
}
|
|
37
|
+
}
|