@poof-bg/js 1.1.0 → 1.3.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/README.md +82 -1
- package/dist/index.cjs +168 -21
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +148 -21
- package/dist/index.d.ts +148 -21
- package/dist/index.js +168 -22
- package/dist/index.js.map +1 -1
- package/package.json +7 -3
package/dist/index.d.cts
CHANGED
|
@@ -12,9 +12,15 @@ interface PoofOptions {
|
|
|
12
12
|
}
|
|
13
13
|
/** Output image format */
|
|
14
14
|
type ImageFormat = 'png' | 'jpg' | 'webp';
|
|
15
|
+
/** Output video format: VP9 WebM (alpha or opaque), ProRes MOV (alpha or opaque) or H.264 MP4 (opaque) */
|
|
16
|
+
type VideoFormat = 'webm' | 'mov' | 'mp4' | 'gif';
|
|
15
17
|
/** Output color channels */
|
|
16
18
|
type Channels = 'rgba' | 'rgb' | 'alpha';
|
|
17
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* Output size preset. Images: megapixel cap. Videos: longest side capped at
|
|
21
|
+
* 640 (preview), 1280 (medium) or 1920 (hd); 'full' keeps the source
|
|
22
|
+
* resolution up to 4096 px (4K).
|
|
23
|
+
*/
|
|
18
24
|
type ImageSize = 'full' | 'preview' | 'medium' | 'hd';
|
|
19
25
|
/**
|
|
20
26
|
* How the image is fitted into a fixed width/height (never stretched):
|
|
@@ -23,10 +29,19 @@ type ImageSize = 'full' | 'preview' | 'medium' | 'hd';
|
|
|
23
29
|
* enlarging a smaller image.
|
|
24
30
|
*/
|
|
25
31
|
type FitMode = 'contain' | 'cover' | 'scale-down';
|
|
32
|
+
/** Options for waiting on a video job */
|
|
33
|
+
interface WaitForJobOptions {
|
|
34
|
+
/** Delay between status polls in milliseconds (default: 3000) */
|
|
35
|
+
pollIntervalMs?: number;
|
|
36
|
+
/** Give up after this many milliseconds (default: 600000, i.e. 10 minutes) */
|
|
37
|
+
timeoutMs?: number;
|
|
38
|
+
/** Called after every poll with the latest job state */
|
|
39
|
+
onProgress?: (job: Job) => void;
|
|
40
|
+
}
|
|
26
41
|
/** Options for background removal */
|
|
27
42
|
interface RemoveBackgroundOptions {
|
|
28
|
-
/** Output format (default: png) */
|
|
29
|
-
format?: ImageFormat;
|
|
43
|
+
/** Output format: png, jpg or webp for images (default: png); webm, mov or mp4 for videos (default: webm) */
|
|
44
|
+
format?: ImageFormat | VideoFormat;
|
|
30
45
|
/** Color channels - rgba for transparency, rgb for opaque, alpha for the grayscale mask only (default: rgba) */
|
|
31
46
|
channels?: Channels;
|
|
32
47
|
/** Background color (hex, rgb, or color name). Applies when channels is 'rgb' or 'rgba' */
|
|
@@ -43,6 +58,11 @@ interface RemoveBackgroundOptions {
|
|
|
43
58
|
height?: number;
|
|
44
59
|
/** How to fit into width x height without stretching (default: contain) */
|
|
45
60
|
fit?: FitMode;
|
|
61
|
+
/**
|
|
62
|
+
* Videos only: wait for the job to finish and return the completed Job
|
|
63
|
+
* instead of the queued one. Pass an object to tune polling.
|
|
64
|
+
*/
|
|
65
|
+
wait?: boolean | WaitForJobOptions;
|
|
46
66
|
}
|
|
47
67
|
/** Metadata returned with processed images */
|
|
48
68
|
interface ProcessingMetadata {
|
|
@@ -74,6 +94,59 @@ interface RemoveBackgroundResult {
|
|
|
74
94
|
/** Processing metadata */
|
|
75
95
|
metadata: ProcessingMetadata;
|
|
76
96
|
}
|
|
97
|
+
/** State of a video job */
|
|
98
|
+
type JobStatus = 'queued' | 'processing' | 'completed' | 'failed';
|
|
99
|
+
/** Why a video job failed */
|
|
100
|
+
interface JobError {
|
|
101
|
+
/** Machine-readable error code, e.g. processing_failed */
|
|
102
|
+
code: string;
|
|
103
|
+
/** Human-readable description */
|
|
104
|
+
message: string;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* A video background removal job. Returned with status 'queued' when a video
|
|
108
|
+
* is submitted; poll with getJob() or waitForJob() until it is 'completed'.
|
|
109
|
+
*/
|
|
110
|
+
interface Job {
|
|
111
|
+
/** Job ID, prefixed with job_ */
|
|
112
|
+
id: string;
|
|
113
|
+
/** queued and processing are transient; completed and failed are terminal */
|
|
114
|
+
status: JobStatus;
|
|
115
|
+
/** Fraction of the video processed so far, 0 to 1 */
|
|
116
|
+
progress: number;
|
|
117
|
+
/** When the job was accepted (ISO 8601) */
|
|
118
|
+
createdAt: string;
|
|
119
|
+
/** When processing began; null while queued */
|
|
120
|
+
startedAt: string | null;
|
|
121
|
+
/** When the job reached completed or failed; null until then */
|
|
122
|
+
completedAt: string | null;
|
|
123
|
+
/** Source video duration in seconds */
|
|
124
|
+
durationSeconds: number;
|
|
125
|
+
/** Source frame rate */
|
|
126
|
+
fps: number;
|
|
127
|
+
/** Source video width in pixels */
|
|
128
|
+
width: number;
|
|
129
|
+
/** Source video height in pixels */
|
|
130
|
+
height: number;
|
|
131
|
+
/** Number of frames in the source video */
|
|
132
|
+
frames: number;
|
|
133
|
+
/** Credits charged for this job (5 per second of video, 10 above 30 fps); refunded if it fails */
|
|
134
|
+
credits: number;
|
|
135
|
+
/** Requested output format */
|
|
136
|
+
format: VideoFormat;
|
|
137
|
+
/** Requested output channels */
|
|
138
|
+
channels: Channels;
|
|
139
|
+
/** URL of the job; GET it to poll for progress */
|
|
140
|
+
statusUrl: string;
|
|
141
|
+
/** Direct download URL of the result on the Poof CDN once completed, null before that. No API key needed */
|
|
142
|
+
resultUrl: string | null;
|
|
143
|
+
/** When the result is deleted, 24 hours after completion; null until completed */
|
|
144
|
+
expiresAt: string | null;
|
|
145
|
+
/** Set when status is failed, null otherwise */
|
|
146
|
+
error: JobError | null;
|
|
147
|
+
/** Processing statistics once completed (output_width, output_height, processing_seconds, ...), null before */
|
|
148
|
+
stats: Record<string, unknown> | null;
|
|
149
|
+
}
|
|
77
150
|
/** Account information */
|
|
78
151
|
interface AccountInfo {
|
|
79
152
|
/** Your organization ID */
|
|
@@ -88,7 +161,7 @@ interface AccountInfo {
|
|
|
88
161
|
autoRechargeThreshold: number | null;
|
|
89
162
|
}
|
|
90
163
|
/** Error codes returned by the API */
|
|
91
|
-
type ErrorCode = 'authentication_error' | 'permission_denied' | 'payment_required' | 'rate_limit_exceeded' | 'validation_error' | 'missing_image' | 'image_too_large' | 'upstream_error' | 'internal_server_error';
|
|
164
|
+
type ErrorCode = 'authentication_error' | 'permission_denied' | 'payment_required' | 'rate_limit_exceeded' | 'validation_error' | 'missing_image' | 'image_too_large' | 'invalid_image' | 'video_too_large' | 'video_too_long' | 'invalid_video' | 'job_not_found' | 'job_failed' | 'upstream_error' | 'internal_server_error';
|
|
92
165
|
/** API error response structure */
|
|
93
166
|
interface ApiErrorResponse {
|
|
94
167
|
code: ErrorCode;
|
|
@@ -96,8 +169,14 @@ interface ApiErrorResponse {
|
|
|
96
169
|
details?: string;
|
|
97
170
|
request_id: string;
|
|
98
171
|
}
|
|
99
|
-
/** Valid input types for image processing */
|
|
172
|
+
/** Valid input types for image and video processing: file contents, or a file path in Node.js */
|
|
100
173
|
type ImageInput = File | Blob | ArrayBuffer | Buffer | Uint8Array | string;
|
|
174
|
+
/** A publicly reachable https URL that Poof downloads instead of an upload (images up to 20 MB, videos up to 500 MB) */
|
|
175
|
+
interface UrlInput {
|
|
176
|
+
url: string;
|
|
177
|
+
}
|
|
178
|
+
/** Anything removeBackground() accepts: file contents, a file path (Node.js) or `{ url }` */
|
|
179
|
+
type MediaInput = ImageInput | UrlInput;
|
|
101
180
|
|
|
102
181
|
/**
|
|
103
182
|
* Poof API client for background removal
|
|
@@ -116,37 +195,71 @@ declare class Poof {
|
|
|
116
195
|
private readonly timeout;
|
|
117
196
|
constructor(options: PoofOptions);
|
|
118
197
|
/**
|
|
119
|
-
* Remove background from an image
|
|
198
|
+
* Remove the background from an image or a video.
|
|
199
|
+
*
|
|
200
|
+
* Images are processed synchronously and come back as bytes. Videos are
|
|
201
|
+
* processed as a job: the call returns a queued {@link Job}; poll it with
|
|
202
|
+
* {@link getJob} / {@link waitForJob}, or pass `wait: true` to get the
|
|
203
|
+
* completed job (with `resultUrl`) directly. Use {@link isJob} to tell the
|
|
204
|
+
* two apart.
|
|
120
205
|
*
|
|
121
|
-
* @param input -
|
|
206
|
+
* @param input - File, Blob, Buffer, ArrayBuffer, Uint8Array, file path (Node.js), or `{ url }`
|
|
122
207
|
* @param options - Processing options
|
|
123
|
-
* @returns Processed image data and metadata
|
|
208
|
+
* @returns Processed image data and metadata, or a video Job
|
|
124
209
|
*
|
|
125
210
|
* @example
|
|
126
211
|
* ```typescript
|
|
127
|
-
* //
|
|
212
|
+
* // Image
|
|
128
213
|
* const result = await poof.removeBackground(file);
|
|
129
214
|
* const imageBlob = new Blob([result.data], { type: result.metadata.contentType });
|
|
130
215
|
*
|
|
131
216
|
* // With options
|
|
132
|
-
* const result = await poof.removeBackground(file, {
|
|
133
|
-
* format: 'webp',
|
|
134
|
-
* crop: true,
|
|
135
|
-
* size: 'medium'
|
|
136
|
-
* });
|
|
217
|
+
* const result = await poof.removeBackground(file, { format: 'webp', crop: true, size: 'medium' });
|
|
137
218
|
*
|
|
138
219
|
* // Exact 500x500 output, subject cropped and centred, never stretched
|
|
139
220
|
* const result = await poof.removeBackground(file, { crop: true, width: 500, height: 500 });
|
|
140
221
|
*
|
|
141
|
-
* //
|
|
142
|
-
* const
|
|
222
|
+
* // Video: submit, then wait for the result
|
|
223
|
+
* const job = await poof.removeBackground('./clip.mp4', { format: 'webm' });
|
|
224
|
+
* const done = await poof.waitForJob(job);
|
|
225
|
+
* console.log(done.resultUrl);
|
|
226
|
+
*
|
|
227
|
+
* // Video from a public URL, waiting inline
|
|
228
|
+
* const done = await poof.removeBackground({ url: 'https://example.com/clip.mp4' }, { wait: true });
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
231
|
+
removeBackground(input: MediaInput, options?: RemoveBackgroundOptions): Promise<RemoveBackgroundResult | Job>;
|
|
232
|
+
/**
|
|
233
|
+
* Get the current state of a video job. Polling is free and does not count
|
|
234
|
+
* against your rate limit.
|
|
235
|
+
*
|
|
236
|
+
* @param jobId - The job ID returned by removeBackground()
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* ```typescript
|
|
240
|
+
* const job = await poof.getJob('job_8f3a1c2e9b7d4a10');
|
|
241
|
+
* console.log(job.status, job.progress);
|
|
242
|
+
* ```
|
|
243
|
+
*/
|
|
244
|
+
getJob(jobId: string): Promise<Job>;
|
|
245
|
+
/**
|
|
246
|
+
* Poll a video job until it is completed.
|
|
247
|
+
*
|
|
248
|
+
* @param job - A Job or a job ID
|
|
249
|
+
* @param options - Poll interval, timeout and progress callback
|
|
250
|
+
* @returns The completed job; `resultUrl` is a direct download link valid for 24 hours
|
|
251
|
+
* @throws PoofError with code `job_failed` if the job fails (credits are refunded),
|
|
252
|
+
* or code `timeout` if `timeoutMs` elapses first
|
|
143
253
|
*
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
*
|
|
254
|
+
* @example
|
|
255
|
+
* ```typescript
|
|
256
|
+
* const done = await poof.waitForJob(job, {
|
|
257
|
+
* onProgress: (j) => console.log(`${Math.round(j.progress * 100)}%`),
|
|
258
|
+
* });
|
|
259
|
+
* const video = await fetch(done.resultUrl!);
|
|
147
260
|
* ```
|
|
148
261
|
*/
|
|
149
|
-
|
|
262
|
+
waitForJob(job: Job | string, options?: WaitForJobOptions): Promise<Job>;
|
|
150
263
|
/**
|
|
151
264
|
* Get account information
|
|
152
265
|
*
|
|
@@ -165,6 +278,20 @@ declare class Poof {
|
|
|
165
278
|
/** Handle error response */
|
|
166
279
|
private handleError;
|
|
167
280
|
}
|
|
281
|
+
/**
|
|
282
|
+
* Type guard: did removeBackground() return a video Job rather than image bytes?
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* ```typescript
|
|
286
|
+
* const result = await poof.removeBackground(input);
|
|
287
|
+
* if (isJob(result)) {
|
|
288
|
+
* const done = await poof.waitForJob(result);
|
|
289
|
+
* } else {
|
|
290
|
+
* fs.writeFileSync('output.png', Buffer.from(result.data));
|
|
291
|
+
* }
|
|
292
|
+
* ```
|
|
293
|
+
*/
|
|
294
|
+
declare function isJob(value: RemoveBackgroundResult | Job): value is Job;
|
|
168
295
|
|
|
169
296
|
/**
|
|
170
297
|
* Base error class for all Poof SDK errors
|
|
@@ -247,4 +374,4 @@ declare class ServerError extends PoofError {
|
|
|
247
374
|
});
|
|
248
375
|
}
|
|
249
376
|
|
|
250
|
-
export { type AccountInfo, type ApiErrorResponse, AuthenticationError, type Channels, type ErrorCode, type FitMode, type ImageFormat, type ImageInput, type ImageSize, PaymentRequiredError, PermissionError, Poof, PoofError, type PoofOptions, type ProcessingMetadata, RateLimitError, type RemoveBackgroundOptions, type RemoveBackgroundResult, ServerError, ValidationError };
|
|
377
|
+
export { type AccountInfo, type ApiErrorResponse, AuthenticationError, type Channels, type ErrorCode, type FitMode, type ImageFormat, type ImageInput, type ImageSize, type Job, type JobError, type JobStatus, type MediaInput, PaymentRequiredError, PermissionError, Poof, PoofError, type PoofOptions, type ProcessingMetadata, RateLimitError, type RemoveBackgroundOptions, type RemoveBackgroundResult, ServerError, type UrlInput, ValidationError, type VideoFormat, type WaitForJobOptions, isJob };
|
package/dist/index.d.ts
CHANGED
|
@@ -12,9 +12,15 @@ interface PoofOptions {
|
|
|
12
12
|
}
|
|
13
13
|
/** Output image format */
|
|
14
14
|
type ImageFormat = 'png' | 'jpg' | 'webp';
|
|
15
|
+
/** Output video format: VP9 WebM (alpha or opaque), ProRes MOV (alpha or opaque) or H.264 MP4 (opaque) */
|
|
16
|
+
type VideoFormat = 'webm' | 'mov' | 'mp4' | 'gif';
|
|
15
17
|
/** Output color channels */
|
|
16
18
|
type Channels = 'rgba' | 'rgb' | 'alpha';
|
|
17
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* Output size preset. Images: megapixel cap. Videos: longest side capped at
|
|
21
|
+
* 640 (preview), 1280 (medium) or 1920 (hd); 'full' keeps the source
|
|
22
|
+
* resolution up to 4096 px (4K).
|
|
23
|
+
*/
|
|
18
24
|
type ImageSize = 'full' | 'preview' | 'medium' | 'hd';
|
|
19
25
|
/**
|
|
20
26
|
* How the image is fitted into a fixed width/height (never stretched):
|
|
@@ -23,10 +29,19 @@ type ImageSize = 'full' | 'preview' | 'medium' | 'hd';
|
|
|
23
29
|
* enlarging a smaller image.
|
|
24
30
|
*/
|
|
25
31
|
type FitMode = 'contain' | 'cover' | 'scale-down';
|
|
32
|
+
/** Options for waiting on a video job */
|
|
33
|
+
interface WaitForJobOptions {
|
|
34
|
+
/** Delay between status polls in milliseconds (default: 3000) */
|
|
35
|
+
pollIntervalMs?: number;
|
|
36
|
+
/** Give up after this many milliseconds (default: 600000, i.e. 10 minutes) */
|
|
37
|
+
timeoutMs?: number;
|
|
38
|
+
/** Called after every poll with the latest job state */
|
|
39
|
+
onProgress?: (job: Job) => void;
|
|
40
|
+
}
|
|
26
41
|
/** Options for background removal */
|
|
27
42
|
interface RemoveBackgroundOptions {
|
|
28
|
-
/** Output format (default: png) */
|
|
29
|
-
format?: ImageFormat;
|
|
43
|
+
/** Output format: png, jpg or webp for images (default: png); webm, mov or mp4 for videos (default: webm) */
|
|
44
|
+
format?: ImageFormat | VideoFormat;
|
|
30
45
|
/** Color channels - rgba for transparency, rgb for opaque, alpha for the grayscale mask only (default: rgba) */
|
|
31
46
|
channels?: Channels;
|
|
32
47
|
/** Background color (hex, rgb, or color name). Applies when channels is 'rgb' or 'rgba' */
|
|
@@ -43,6 +58,11 @@ interface RemoveBackgroundOptions {
|
|
|
43
58
|
height?: number;
|
|
44
59
|
/** How to fit into width x height without stretching (default: contain) */
|
|
45
60
|
fit?: FitMode;
|
|
61
|
+
/**
|
|
62
|
+
* Videos only: wait for the job to finish and return the completed Job
|
|
63
|
+
* instead of the queued one. Pass an object to tune polling.
|
|
64
|
+
*/
|
|
65
|
+
wait?: boolean | WaitForJobOptions;
|
|
46
66
|
}
|
|
47
67
|
/** Metadata returned with processed images */
|
|
48
68
|
interface ProcessingMetadata {
|
|
@@ -74,6 +94,59 @@ interface RemoveBackgroundResult {
|
|
|
74
94
|
/** Processing metadata */
|
|
75
95
|
metadata: ProcessingMetadata;
|
|
76
96
|
}
|
|
97
|
+
/** State of a video job */
|
|
98
|
+
type JobStatus = 'queued' | 'processing' | 'completed' | 'failed';
|
|
99
|
+
/** Why a video job failed */
|
|
100
|
+
interface JobError {
|
|
101
|
+
/** Machine-readable error code, e.g. processing_failed */
|
|
102
|
+
code: string;
|
|
103
|
+
/** Human-readable description */
|
|
104
|
+
message: string;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* A video background removal job. Returned with status 'queued' when a video
|
|
108
|
+
* is submitted; poll with getJob() or waitForJob() until it is 'completed'.
|
|
109
|
+
*/
|
|
110
|
+
interface Job {
|
|
111
|
+
/** Job ID, prefixed with job_ */
|
|
112
|
+
id: string;
|
|
113
|
+
/** queued and processing are transient; completed and failed are terminal */
|
|
114
|
+
status: JobStatus;
|
|
115
|
+
/** Fraction of the video processed so far, 0 to 1 */
|
|
116
|
+
progress: number;
|
|
117
|
+
/** When the job was accepted (ISO 8601) */
|
|
118
|
+
createdAt: string;
|
|
119
|
+
/** When processing began; null while queued */
|
|
120
|
+
startedAt: string | null;
|
|
121
|
+
/** When the job reached completed or failed; null until then */
|
|
122
|
+
completedAt: string | null;
|
|
123
|
+
/** Source video duration in seconds */
|
|
124
|
+
durationSeconds: number;
|
|
125
|
+
/** Source frame rate */
|
|
126
|
+
fps: number;
|
|
127
|
+
/** Source video width in pixels */
|
|
128
|
+
width: number;
|
|
129
|
+
/** Source video height in pixels */
|
|
130
|
+
height: number;
|
|
131
|
+
/** Number of frames in the source video */
|
|
132
|
+
frames: number;
|
|
133
|
+
/** Credits charged for this job (5 per second of video, 10 above 30 fps); refunded if it fails */
|
|
134
|
+
credits: number;
|
|
135
|
+
/** Requested output format */
|
|
136
|
+
format: VideoFormat;
|
|
137
|
+
/** Requested output channels */
|
|
138
|
+
channels: Channels;
|
|
139
|
+
/** URL of the job; GET it to poll for progress */
|
|
140
|
+
statusUrl: string;
|
|
141
|
+
/** Direct download URL of the result on the Poof CDN once completed, null before that. No API key needed */
|
|
142
|
+
resultUrl: string | null;
|
|
143
|
+
/** When the result is deleted, 24 hours after completion; null until completed */
|
|
144
|
+
expiresAt: string | null;
|
|
145
|
+
/** Set when status is failed, null otherwise */
|
|
146
|
+
error: JobError | null;
|
|
147
|
+
/** Processing statistics once completed (output_width, output_height, processing_seconds, ...), null before */
|
|
148
|
+
stats: Record<string, unknown> | null;
|
|
149
|
+
}
|
|
77
150
|
/** Account information */
|
|
78
151
|
interface AccountInfo {
|
|
79
152
|
/** Your organization ID */
|
|
@@ -88,7 +161,7 @@ interface AccountInfo {
|
|
|
88
161
|
autoRechargeThreshold: number | null;
|
|
89
162
|
}
|
|
90
163
|
/** Error codes returned by the API */
|
|
91
|
-
type ErrorCode = 'authentication_error' | 'permission_denied' | 'payment_required' | 'rate_limit_exceeded' | 'validation_error' | 'missing_image' | 'image_too_large' | 'upstream_error' | 'internal_server_error';
|
|
164
|
+
type ErrorCode = 'authentication_error' | 'permission_denied' | 'payment_required' | 'rate_limit_exceeded' | 'validation_error' | 'missing_image' | 'image_too_large' | 'invalid_image' | 'video_too_large' | 'video_too_long' | 'invalid_video' | 'job_not_found' | 'job_failed' | 'upstream_error' | 'internal_server_error';
|
|
92
165
|
/** API error response structure */
|
|
93
166
|
interface ApiErrorResponse {
|
|
94
167
|
code: ErrorCode;
|
|
@@ -96,8 +169,14 @@ interface ApiErrorResponse {
|
|
|
96
169
|
details?: string;
|
|
97
170
|
request_id: string;
|
|
98
171
|
}
|
|
99
|
-
/** Valid input types for image processing */
|
|
172
|
+
/** Valid input types for image and video processing: file contents, or a file path in Node.js */
|
|
100
173
|
type ImageInput = File | Blob | ArrayBuffer | Buffer | Uint8Array | string;
|
|
174
|
+
/** A publicly reachable https URL that Poof downloads instead of an upload (images up to 20 MB, videos up to 500 MB) */
|
|
175
|
+
interface UrlInput {
|
|
176
|
+
url: string;
|
|
177
|
+
}
|
|
178
|
+
/** Anything removeBackground() accepts: file contents, a file path (Node.js) or `{ url }` */
|
|
179
|
+
type MediaInput = ImageInput | UrlInput;
|
|
101
180
|
|
|
102
181
|
/**
|
|
103
182
|
* Poof API client for background removal
|
|
@@ -116,37 +195,71 @@ declare class Poof {
|
|
|
116
195
|
private readonly timeout;
|
|
117
196
|
constructor(options: PoofOptions);
|
|
118
197
|
/**
|
|
119
|
-
* Remove background from an image
|
|
198
|
+
* Remove the background from an image or a video.
|
|
199
|
+
*
|
|
200
|
+
* Images are processed synchronously and come back as bytes. Videos are
|
|
201
|
+
* processed as a job: the call returns a queued {@link Job}; poll it with
|
|
202
|
+
* {@link getJob} / {@link waitForJob}, or pass `wait: true` to get the
|
|
203
|
+
* completed job (with `resultUrl`) directly. Use {@link isJob} to tell the
|
|
204
|
+
* two apart.
|
|
120
205
|
*
|
|
121
|
-
* @param input -
|
|
206
|
+
* @param input - File, Blob, Buffer, ArrayBuffer, Uint8Array, file path (Node.js), or `{ url }`
|
|
122
207
|
* @param options - Processing options
|
|
123
|
-
* @returns Processed image data and metadata
|
|
208
|
+
* @returns Processed image data and metadata, or a video Job
|
|
124
209
|
*
|
|
125
210
|
* @example
|
|
126
211
|
* ```typescript
|
|
127
|
-
* //
|
|
212
|
+
* // Image
|
|
128
213
|
* const result = await poof.removeBackground(file);
|
|
129
214
|
* const imageBlob = new Blob([result.data], { type: result.metadata.contentType });
|
|
130
215
|
*
|
|
131
216
|
* // With options
|
|
132
|
-
* const result = await poof.removeBackground(file, {
|
|
133
|
-
* format: 'webp',
|
|
134
|
-
* crop: true,
|
|
135
|
-
* size: 'medium'
|
|
136
|
-
* });
|
|
217
|
+
* const result = await poof.removeBackground(file, { format: 'webp', crop: true, size: 'medium' });
|
|
137
218
|
*
|
|
138
219
|
* // Exact 500x500 output, subject cropped and centred, never stretched
|
|
139
220
|
* const result = await poof.removeBackground(file, { crop: true, width: 500, height: 500 });
|
|
140
221
|
*
|
|
141
|
-
* //
|
|
142
|
-
* const
|
|
222
|
+
* // Video: submit, then wait for the result
|
|
223
|
+
* const job = await poof.removeBackground('./clip.mp4', { format: 'webm' });
|
|
224
|
+
* const done = await poof.waitForJob(job);
|
|
225
|
+
* console.log(done.resultUrl);
|
|
226
|
+
*
|
|
227
|
+
* // Video from a public URL, waiting inline
|
|
228
|
+
* const done = await poof.removeBackground({ url: 'https://example.com/clip.mp4' }, { wait: true });
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
231
|
+
removeBackground(input: MediaInput, options?: RemoveBackgroundOptions): Promise<RemoveBackgroundResult | Job>;
|
|
232
|
+
/**
|
|
233
|
+
* Get the current state of a video job. Polling is free and does not count
|
|
234
|
+
* against your rate limit.
|
|
235
|
+
*
|
|
236
|
+
* @param jobId - The job ID returned by removeBackground()
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* ```typescript
|
|
240
|
+
* const job = await poof.getJob('job_8f3a1c2e9b7d4a10');
|
|
241
|
+
* console.log(job.status, job.progress);
|
|
242
|
+
* ```
|
|
243
|
+
*/
|
|
244
|
+
getJob(jobId: string): Promise<Job>;
|
|
245
|
+
/**
|
|
246
|
+
* Poll a video job until it is completed.
|
|
247
|
+
*
|
|
248
|
+
* @param job - A Job or a job ID
|
|
249
|
+
* @param options - Poll interval, timeout and progress callback
|
|
250
|
+
* @returns The completed job; `resultUrl` is a direct download link valid for 24 hours
|
|
251
|
+
* @throws PoofError with code `job_failed` if the job fails (credits are refunded),
|
|
252
|
+
* or code `timeout` if `timeoutMs` elapses first
|
|
143
253
|
*
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
*
|
|
254
|
+
* @example
|
|
255
|
+
* ```typescript
|
|
256
|
+
* const done = await poof.waitForJob(job, {
|
|
257
|
+
* onProgress: (j) => console.log(`${Math.round(j.progress * 100)}%`),
|
|
258
|
+
* });
|
|
259
|
+
* const video = await fetch(done.resultUrl!);
|
|
147
260
|
* ```
|
|
148
261
|
*/
|
|
149
|
-
|
|
262
|
+
waitForJob(job: Job | string, options?: WaitForJobOptions): Promise<Job>;
|
|
150
263
|
/**
|
|
151
264
|
* Get account information
|
|
152
265
|
*
|
|
@@ -165,6 +278,20 @@ declare class Poof {
|
|
|
165
278
|
/** Handle error response */
|
|
166
279
|
private handleError;
|
|
167
280
|
}
|
|
281
|
+
/**
|
|
282
|
+
* Type guard: did removeBackground() return a video Job rather than image bytes?
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* ```typescript
|
|
286
|
+
* const result = await poof.removeBackground(input);
|
|
287
|
+
* if (isJob(result)) {
|
|
288
|
+
* const done = await poof.waitForJob(result);
|
|
289
|
+
* } else {
|
|
290
|
+
* fs.writeFileSync('output.png', Buffer.from(result.data));
|
|
291
|
+
* }
|
|
292
|
+
* ```
|
|
293
|
+
*/
|
|
294
|
+
declare function isJob(value: RemoveBackgroundResult | Job): value is Job;
|
|
168
295
|
|
|
169
296
|
/**
|
|
170
297
|
* Base error class for all Poof SDK errors
|
|
@@ -247,4 +374,4 @@ declare class ServerError extends PoofError {
|
|
|
247
374
|
});
|
|
248
375
|
}
|
|
249
376
|
|
|
250
|
-
export { type AccountInfo, type ApiErrorResponse, AuthenticationError, type Channels, type ErrorCode, type FitMode, type ImageFormat, type ImageInput, type ImageSize, PaymentRequiredError, PermissionError, Poof, PoofError, type PoofOptions, type ProcessingMetadata, RateLimitError, type RemoveBackgroundOptions, type RemoveBackgroundResult, ServerError, ValidationError };
|
|
377
|
+
export { type AccountInfo, type ApiErrorResponse, AuthenticationError, type Channels, type ErrorCode, type FitMode, type ImageFormat, type ImageInput, type ImageSize, type Job, type JobError, type JobStatus, type MediaInput, PaymentRequiredError, PermissionError, Poof, PoofError, type PoofOptions, type ProcessingMetadata, RateLimitError, type RemoveBackgroundOptions, type RemoveBackgroundResult, ServerError, type UrlInput, ValidationError, type VideoFormat, type WaitForJobOptions, isJob };
|