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