wavespeed 0.0.1
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 +145 -0
- package/dist/index.d.ts +88 -0
- package/dist/index.js +149 -0
- package/package.json +37 -0
package/README.md
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# WaveSpeed JavaScript/TypeScript Client
|
|
2
|
+
|
|
3
|
+
A JavaScript/TypeScript client for the WaveSpeed AI image generation API. This client is compatible with both JavaScript and TypeScript projects.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install wavespeed
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### JavaScript
|
|
14
|
+
|
|
15
|
+
```javascript
|
|
16
|
+
const { WaveSpeed } = require('wavespeed');
|
|
17
|
+
|
|
18
|
+
// Initialize the client with your API key
|
|
19
|
+
const client = new WaveSpeed('YOUR_API_KEY');
|
|
20
|
+
|
|
21
|
+
// Generate an image and wait for the result
|
|
22
|
+
async function generateImage() {
|
|
23
|
+
const prediction = await client.run(
|
|
24
|
+
'wavespeed-ai/flux-dev',
|
|
25
|
+
{
|
|
26
|
+
prompt: 'A futuristic cityscape with flying cars and neon lights',
|
|
27
|
+
size: '1024*1024',
|
|
28
|
+
num_inference_steps: 28,
|
|
29
|
+
guidance_scale: 5.0,
|
|
30
|
+
num_images: 1,
|
|
31
|
+
seed: -1,
|
|
32
|
+
enable_safety_checker: true
|
|
33
|
+
}
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
// Print the generated image URLs
|
|
37
|
+
prediction.outputs.forEach((imgUrl, i) => {
|
|
38
|
+
console.log(`Image ${i+1}: ${imgUrl}`);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
generateImage().catch(console.error);
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### TypeScript
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import WaveSpeed from 'wavespeed';
|
|
49
|
+
|
|
50
|
+
// Initialize the client with your API key
|
|
51
|
+
const client = new WaveSpeed('YOUR_API_KEY');
|
|
52
|
+
|
|
53
|
+
// Generate an image and wait for the result
|
|
54
|
+
async function generateImage(): Promise<void> {
|
|
55
|
+
const input: Record<string, any> = {
|
|
56
|
+
prompt: 'A futuristic cityscape with flying cars and neon lights',
|
|
57
|
+
size: '1024*1024',
|
|
58
|
+
num_inference_steps: 28,
|
|
59
|
+
guidance_scale: 5.0,
|
|
60
|
+
num_images: 1,
|
|
61
|
+
seed: -1,
|
|
62
|
+
enable_safety_checker: true
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const prediction = await client.run('wavespeed-ai/flux-dev', input);
|
|
66
|
+
|
|
67
|
+
// Print the generated image URLs
|
|
68
|
+
prediction.outputs.forEach((imgUrl, i) => {
|
|
69
|
+
console.log(`Image ${i+1}: ${imgUrl}`);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
generateImage().catch(console.error);
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## API Reference
|
|
77
|
+
|
|
78
|
+
### WaveSpeed Client
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
new WaveSpeed(apiKey?: string, options?: {
|
|
82
|
+
baseUrl?: string,
|
|
83
|
+
pollInterval?: number,
|
|
84
|
+
timeout?: number
|
|
85
|
+
})
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
#### Parameters:
|
|
89
|
+
- `apiKey` (string): Your WaveSpeed API key
|
|
90
|
+
- `options` (object, optional):
|
|
91
|
+
- `baseUrl` (string): API base URL (default: 'https://api.wavespeed.ai/api/v2/')
|
|
92
|
+
- `pollInterval` (number): Interval in seconds for polling prediction status (default: 1)
|
|
93
|
+
- `timeout` (number): Timeout in seconds for API requests (default: 60)
|
|
94
|
+
|
|
95
|
+
### Methods
|
|
96
|
+
|
|
97
|
+
#### run
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
run(modelId: string, input: Record<string, any>, options?: RequestOptions): Promise<Prediction>
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Generate an image and wait for the result.
|
|
104
|
+
|
|
105
|
+
#### create
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
create(modelId: string, input: Record<string, any>, options?: RequestOptions): Promise<Prediction>
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Create a prediction without waiting for it to complete.
|
|
112
|
+
|
|
113
|
+
### Prediction Model
|
|
114
|
+
|
|
115
|
+
The Prediction object contains information about an image generation job:
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
prediction.id // Unique ID of the prediction
|
|
119
|
+
prediction.model // Model ID used for the prediction
|
|
120
|
+
prediction.status // Status of the prediction (processing, completed, failed)
|
|
121
|
+
prediction.input // Input parameters used for the prediction
|
|
122
|
+
prediction.outputs // List of output image URLs
|
|
123
|
+
prediction.urls.get // URL to get the prediction status
|
|
124
|
+
prediction.has_nsfw_contents // List of booleans indicating if each image has NSFW content
|
|
125
|
+
prediction.created_at // Creation timestamp
|
|
126
|
+
prediction.error // Error message (if any)
|
|
127
|
+
prediction.executionTime // Time taken to execute the prediction in milliseconds
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
#### Methods
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
prediction.wait(): Promise<Prediction> // Wait for the prediction to complete
|
|
134
|
+
prediction.reload(): Promise<Prediction> // Reload the prediction status
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Environment Variables
|
|
138
|
+
|
|
139
|
+
- `WAVESPEED_API_KEY`: Your WaveSpeed API key
|
|
140
|
+
- `WAVESPEED_POLL_INTERVAL`: Interval in seconds for polling prediction status (default: 1)
|
|
141
|
+
- `WAVESPEED_TIMEOUT`: Timeout in seconds for API requests (default: 60)
|
|
142
|
+
|
|
143
|
+
## License
|
|
144
|
+
|
|
145
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Input parameters for image generation
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Prediction status
|
|
6
|
+
*/
|
|
7
|
+
export type PredictionStatus = 'created' | 'processing' | 'completed' | 'failed';
|
|
8
|
+
/**
|
|
9
|
+
* Prediction URLs
|
|
10
|
+
*/
|
|
11
|
+
export interface PredictionUrls {
|
|
12
|
+
get: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Request options for fetch
|
|
16
|
+
*/
|
|
17
|
+
export interface RequestOptions extends RequestInit {
|
|
18
|
+
timeout?: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Prediction model representing an image generation job
|
|
22
|
+
*/
|
|
23
|
+
export declare class Prediction {
|
|
24
|
+
id: string;
|
|
25
|
+
model: string;
|
|
26
|
+
status: PredictionStatus;
|
|
27
|
+
input: Record<string, any>;
|
|
28
|
+
outputs: string[];
|
|
29
|
+
urls: PredictionUrls;
|
|
30
|
+
has_nsfw_contents: boolean[];
|
|
31
|
+
created_at: string;
|
|
32
|
+
error?: string;
|
|
33
|
+
executionTime?: number;
|
|
34
|
+
private client;
|
|
35
|
+
constructor(data: any, client: WaveSpeed);
|
|
36
|
+
/**
|
|
37
|
+
* Wait for the prediction to complete
|
|
38
|
+
*/
|
|
39
|
+
wait(): Promise<Prediction>;
|
|
40
|
+
/**
|
|
41
|
+
* Reload the prediction status
|
|
42
|
+
*/
|
|
43
|
+
reload(): Promise<Prediction>;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* WaveSpeed client for generating images
|
|
47
|
+
*/
|
|
48
|
+
export declare class WaveSpeed {
|
|
49
|
+
private apiKey;
|
|
50
|
+
private baseUrl;
|
|
51
|
+
readonly pollInterval: number;
|
|
52
|
+
readonly timeout: number;
|
|
53
|
+
/**
|
|
54
|
+
* Create a new WaveSpeed client
|
|
55
|
+
*
|
|
56
|
+
* @param apiKey Your WaveSpeed API key (or set WAVESPEED_API_KEY environment variable)
|
|
57
|
+
* @param options Additional client options
|
|
58
|
+
*/
|
|
59
|
+
constructor(apiKey?: string, options?: {
|
|
60
|
+
baseUrl?: string;
|
|
61
|
+
pollInterval?: number;
|
|
62
|
+
timeout?: number;
|
|
63
|
+
});
|
|
64
|
+
/**
|
|
65
|
+
* Fetch with timeout support
|
|
66
|
+
*
|
|
67
|
+
* @param path API path
|
|
68
|
+
* @param options Fetch options
|
|
69
|
+
*/
|
|
70
|
+
fetchWithTimeout(path: string, options?: RequestOptions): Promise<Response>;
|
|
71
|
+
/**
|
|
72
|
+
* Generate an image and wait for the result
|
|
73
|
+
*
|
|
74
|
+
* @param modelId Model ID to use for prediction
|
|
75
|
+
* @param input Input parameters for the prediction
|
|
76
|
+
* @param options Additional fetch options
|
|
77
|
+
*/
|
|
78
|
+
run(modelId: string, input: Record<string, any>, options?: RequestOptions): Promise<Prediction>;
|
|
79
|
+
/**
|
|
80
|
+
* Create a prediction without waiting for it to complete
|
|
81
|
+
*
|
|
82
|
+
* @param modelId Model ID to use for prediction
|
|
83
|
+
* @param input Input parameters for the prediction
|
|
84
|
+
* @param options Additional fetch options
|
|
85
|
+
*/
|
|
86
|
+
create(modelId: string, input: Record<string, any>, options?: RequestOptions): Promise<Prediction>;
|
|
87
|
+
}
|
|
88
|
+
export default WaveSpeed;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WaveSpeed = exports.Prediction = void 0;
|
|
4
|
+
const url_1 = require("url");
|
|
5
|
+
/**
|
|
6
|
+
* Prediction model representing an image generation job
|
|
7
|
+
*/
|
|
8
|
+
class Prediction {
|
|
9
|
+
constructor(data, client) {
|
|
10
|
+
this.id = data.id;
|
|
11
|
+
this.model = data.model;
|
|
12
|
+
this.status = data.status;
|
|
13
|
+
this.input = data.input;
|
|
14
|
+
this.outputs = data.outputs || [];
|
|
15
|
+
this.urls = data.urls;
|
|
16
|
+
this.has_nsfw_contents = data.has_nsfw_contents || [];
|
|
17
|
+
this.created_at = data.created_at;
|
|
18
|
+
this.error = data.error;
|
|
19
|
+
this.executionTime = data.executionTime;
|
|
20
|
+
this.client = client;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Wait for the prediction to complete
|
|
24
|
+
*/
|
|
25
|
+
async wait() {
|
|
26
|
+
if (this.status === 'completed' || this.status === 'failed') {
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
const checkStatus = async () => {
|
|
31
|
+
try {
|
|
32
|
+
const updated = await this.reload();
|
|
33
|
+
if (updated.status === 'completed' || updated.status === 'failed') {
|
|
34
|
+
resolve(updated);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
setTimeout(checkStatus, this.client.pollInterval * 1000);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
reject(error);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
checkStatus();
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Reload the prediction status
|
|
49
|
+
*/
|
|
50
|
+
async reload() {
|
|
51
|
+
const response = await this.client.fetchWithTimeout(`predictions/${this.id}/result`);
|
|
52
|
+
if (!response.ok) {
|
|
53
|
+
const errorText = await response.text();
|
|
54
|
+
throw new Error(`Failed to reload prediction: ${response.status} ${errorText}`);
|
|
55
|
+
}
|
|
56
|
+
const data = await response.json();
|
|
57
|
+
const updatedPrediction = new Prediction(data.data, this.client);
|
|
58
|
+
// Update this instance with new data
|
|
59
|
+
Object.assign(this, updatedPrediction);
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
exports.Prediction = Prediction;
|
|
64
|
+
/**
|
|
65
|
+
* WaveSpeed client for generating images
|
|
66
|
+
*/
|
|
67
|
+
class WaveSpeed {
|
|
68
|
+
/**
|
|
69
|
+
* Create a new WaveSpeed client
|
|
70
|
+
*
|
|
71
|
+
* @param apiKey Your WaveSpeed API key (or set WAVESPEED_API_KEY environment variable)
|
|
72
|
+
* @param options Additional client options
|
|
73
|
+
*/
|
|
74
|
+
constructor(apiKey, options = {}) {
|
|
75
|
+
this.baseUrl = 'https://api.wavespeed.ai/api/v2/';
|
|
76
|
+
this.apiKey = apiKey || process.env.WAVESPEED_API_KEY || '';
|
|
77
|
+
if (!this.apiKey) {
|
|
78
|
+
throw new Error('API key is required. Provide it as a parameter or set the WAVESPEED_API_KEY environment variable.');
|
|
79
|
+
}
|
|
80
|
+
if (options.baseUrl) {
|
|
81
|
+
this.baseUrl = options.baseUrl;
|
|
82
|
+
}
|
|
83
|
+
this.pollInterval = options.pollInterval || Number(process.env.WAVESPEED_POLL_INTERVAL) || 1;
|
|
84
|
+
this.timeout = options.timeout || Number(process.env.WAVESPEED_TIMEOUT) || 60;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Fetch with timeout support
|
|
88
|
+
*
|
|
89
|
+
* @param path API path
|
|
90
|
+
* @param options Fetch options
|
|
91
|
+
*/
|
|
92
|
+
async fetchWithTimeout(path, options = {}) {
|
|
93
|
+
const { timeout = this.timeout * 1000, ...fetchOptions } = options;
|
|
94
|
+
// Ensure headers exist
|
|
95
|
+
fetchOptions.headers = {
|
|
96
|
+
...fetchOptions.headers,
|
|
97
|
+
'Authorization': `Bearer ${this.apiKey}`,
|
|
98
|
+
'Content-Type': 'application/json'
|
|
99
|
+
};
|
|
100
|
+
const controller = new AbortController();
|
|
101
|
+
const id = setTimeout(() => controller.abort(), timeout);
|
|
102
|
+
try {
|
|
103
|
+
// Construct the full URL by joining baseUrl and path
|
|
104
|
+
const url = new url_1.URL(path, this.baseUrl).toString();
|
|
105
|
+
const response = await fetch(url, {
|
|
106
|
+
...fetchOptions,
|
|
107
|
+
signal: controller.signal
|
|
108
|
+
});
|
|
109
|
+
return response;
|
|
110
|
+
}
|
|
111
|
+
finally {
|
|
112
|
+
clearTimeout(id);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Generate an image and wait for the result
|
|
117
|
+
*
|
|
118
|
+
* @param modelId Model ID to use for prediction
|
|
119
|
+
* @param input Input parameters for the prediction
|
|
120
|
+
* @param options Additional fetch options
|
|
121
|
+
*/
|
|
122
|
+
async run(modelId, input, options) {
|
|
123
|
+
const prediction = await this.create(modelId, input, options);
|
|
124
|
+
return prediction.wait();
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Create a prediction without waiting for it to complete
|
|
128
|
+
*
|
|
129
|
+
* @param modelId Model ID to use for prediction
|
|
130
|
+
* @param input Input parameters for the prediction
|
|
131
|
+
* @param options Additional fetch options
|
|
132
|
+
*/
|
|
133
|
+
async create(modelId, input, options) {
|
|
134
|
+
const response = await this.fetchWithTimeout(`${modelId}`, {
|
|
135
|
+
method: 'POST',
|
|
136
|
+
body: JSON.stringify(input),
|
|
137
|
+
...options
|
|
138
|
+
});
|
|
139
|
+
if (!response.ok) {
|
|
140
|
+
const errorText = await response.text();
|
|
141
|
+
throw new Error(`Failed to create prediction: ${response.status} ${errorText}`);
|
|
142
|
+
}
|
|
143
|
+
const data = await response.json();
|
|
144
|
+
return new Prediction(data.data, this);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
exports.WaveSpeed = WaveSpeed;
|
|
148
|
+
// Export default and named exports for different import styles
|
|
149
|
+
exports.default = WaveSpeed;
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "wavespeed",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Client SDK for Wavespeed API",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"test": "jest",
|
|
10
|
+
"test:watch": "jest --watch",
|
|
11
|
+
"test:coverage": "jest --coverage",
|
|
12
|
+
"lint": "eslint src --ext .ts",
|
|
13
|
+
"prepare": "npm run build"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"api",
|
|
17
|
+
"client",
|
|
18
|
+
"sdk",
|
|
19
|
+
"wavespeed"
|
|
20
|
+
],
|
|
21
|
+
"author": "",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@types/jest": "^29.5.5",
|
|
25
|
+
"@types/node": "^20.8.2",
|
|
26
|
+
"@typescript-eslint/eslint-plugin": "^6.7.4",
|
|
27
|
+
"@typescript-eslint/parser": "^6.7.4",
|
|
28
|
+
"eslint": "^8.50.0",
|
|
29
|
+
"jest": "^29.7.0",
|
|
30
|
+
"ts-jest": "^29.1.1",
|
|
31
|
+
"typescript": "^5.2.2"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist"
|
|
36
|
+
]
|
|
37
|
+
}
|