@snowcone-app/sdk 0.8.1 → 0.9.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/CHANGELOG.md +33 -0
- package/dist/{chunk-YTOTCNEW.js → chunk-LYLIDU6G.js} +2 -0
- package/dist/index.cjs +2 -0
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/react.cjs +2 -0
- package/dist/react.d.cts +1 -1
- package/dist/react.d.ts +1 -1
- package/dist/react.js +1 -1
- package/dist/server.cjs +43 -2
- package/dist/server.d.cts +46 -1
- package/dist/server.d.ts +46 -1
- package/dist/server.js +42 -2
- package/dist/{websocket-VYgeVFE2.d.cts → websocket-X4tcUm78.d.cts} +8 -0
- package/dist/{websocket-VYgeVFE2.d.ts → websocket-X4tcUm78.d.ts} +8 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,38 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.9.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#577](https://github.com/snowcone-app/snowcone-monorepo/pull/577) [`3bdd003`](https://github.com/snowcone-app/snowcone-monorepo/commit/3bdd0036d1110b3d16af4f240a879342edbfc97e) Thanks [@kevinsproles](https://github.com/kevinsproles)! - Realtime mockup results now carry `costUsd` and `renderCache` ('hit' | 'miss') alongside `renderMs` — the same per-render observability the HTTP path exposes via the `x-render-cost-usd` / `x-render-cache` headers.
|
|
8
|
+
|
|
9
|
+
## 0.9.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- [#540](https://github.com/snowcone-app/snowcone-monorepo/pull/540) [`2dc18c1`](https://github.com/snowcone-app/snowcone-monorepo/commit/2dc18c14f88a547959d54d578d950ff2f06e011a) Thanks [@kevinsproles](https://github.com/kevinsproles)! - Background-removal parity for 3rd-party embedders — no hand-written provider
|
|
14
|
+
required.
|
|
15
|
+
- **canvas:** new `bgRemoveUrl` shorthand on the `services` contract. When
|
|
16
|
+
`services.removeBackground` isn't provided but `bgRemoveUrl` is, the canvas
|
|
17
|
+
synthesizes the provider (mirrors the `imageSearchUrl` / `generateUrl`
|
|
18
|
+
shorthands) — it deproxies a `/api/image-proxy?url=…` source back to its
|
|
19
|
+
original URL, rejects `data:` / `blob:` and non-`http(s)` sources, then
|
|
20
|
+
`POST`s `{ imageUrl }` and reads `{ imageUrl }` back. New
|
|
21
|
+
`createProxyBgRemove(url)` + `BgRemoveProvider` type are exported.
|
|
22
|
+
- **sdk:** new `createBgRemoveHandler` in `@snowcone-app/sdk/server` — a drop-in
|
|
23
|
+
same-origin proxy route for the editor's "remove background" button. Validates
|
|
24
|
+
the `imageUrl` (https only, rejects `data:` / `blob:`, 2048-char cap), proxies
|
|
25
|
+
to the backend `POST /ai-generations/remove-background` with your shop-scoped
|
|
26
|
+
`ai:bg-remove` key, and supports the same optional `gate` hook as the other
|
|
27
|
+
handlers.
|
|
28
|
+
|
|
29
|
+
- [#538](https://github.com/snowcone-app/snowcone-monorepo/pull/538) [`dbcdf66`](https://github.com/snowcone-app/snowcone-monorepo/commit/dbcdf66b25c8f08fd1a878230b7ee1f79252cd41) Thanks [@kevinsproles](https://github.com/kevinsproles)! - `createImageSearchHandler` gains an optional `safesearch` option (default
|
|
30
|
+
`false`). When `true`, the Pixabay source is queried with `safesearch=true` so
|
|
31
|
+
results unsuitable for all ages are filtered out (Unsplash has no equivalent
|
|
32
|
+
flag). This lets a host enforce a family-safe stock-image policy through the
|
|
33
|
+
shared handler instead of forking the vendor logic — www now delegates its
|
|
34
|
+
image-search route to this handler and passes `safesearch: true`.
|
|
35
|
+
|
|
3
36
|
## 0.8.1
|
|
4
37
|
|
|
5
38
|
### Patch Changes
|
|
@@ -259,6 +259,8 @@ var RealtimeMockupService = class {
|
|
|
259
259
|
placement: responsePlacement,
|
|
260
260
|
renderMs: data.renderMs,
|
|
261
261
|
blobToRenderMs: data.blobToRenderMs,
|
|
262
|
+
costUsd: data.costUsd,
|
|
263
|
+
renderCache: data.renderCache,
|
|
262
264
|
canvasRenderTiming: data.canvasRenderTiming
|
|
263
265
|
};
|
|
264
266
|
const existingIndex = this.mockupResults.findIndex((m) => m.mockupId === data.mockupId);
|
package/dist/index.cjs
CHANGED
|
@@ -5137,6 +5137,8 @@ var RealtimeMockupService = class {
|
|
|
5137
5137
|
placement: responsePlacement,
|
|
5138
5138
|
renderMs: data.renderMs,
|
|
5139
5139
|
blobToRenderMs: data.blobToRenderMs,
|
|
5140
|
+
costUsd: data.costUsd,
|
|
5141
|
+
renderCache: data.renderCache,
|
|
5140
5142
|
canvasRenderTiming: data.canvasRenderTiming
|
|
5141
5143
|
};
|
|
5142
5144
|
const existingIndex = this.mockupResults.findIndex((m) => m.mockupId === data.mockupId);
|
package/dist/index.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { createDevFetcher } from './dev-fetcher.cjs';
|
|
2
2
|
import { PublicDesign, PublicFill, PublicOptions } from '@snowcone-app/mockup-url';
|
|
3
|
-
import { O as OptionAttribute, a as OptionSelection, C as Combination, F as FrameworkAdapter, b as ComponentProps, c as ComponentDescriptor, d as ComponentState, e as ComponentContext, f as ComponentLifecycleHooks, E as EventHandler, g as FrameworkUtilities, P as PlacementSettings, M as MockupResult, R as RealtimeMockupService } from './websocket-
|
|
4
|
-
export { A as AdapterRegistry, h as ProductComponentContext, i as RealtimeMockupCallbacks, j as RealtimeMockupState, k as RenderResult, W as WebSocketConfig, l as WebSocketMessage, m as adapterRegistry, n as computeDisabledChoices, o as createComponent, p as defineComponent, q as deriveDefaultSelection, r as findBestCombination, s as getPricePreview, t as isOptionAvailable, u as resolveBestCombination } from './websocket-
|
|
3
|
+
import { O as OptionAttribute, a as OptionSelection, C as Combination, F as FrameworkAdapter, b as ComponentProps, c as ComponentDescriptor, d as ComponentState, e as ComponentContext, f as ComponentLifecycleHooks, E as EventHandler, g as FrameworkUtilities, P as PlacementSettings, M as MockupResult, R as RealtimeMockupService } from './websocket-X4tcUm78.cjs';
|
|
4
|
+
export { A as AdapterRegistry, h as ProductComponentContext, i as RealtimeMockupCallbacks, j as RealtimeMockupState, k as RenderResult, W as WebSocketConfig, l as WebSocketMessage, m as adapterRegistry, n as computeDisabledChoices, o as createComponent, p as defineComponent, q as deriveDefaultSelection, r as findBestCombination, s as getPricePreview, t as isOptionAvailable, u as resolveBestCombination } from './websocket-X4tcUm78.cjs';
|
|
5
5
|
|
|
6
6
|
interface CatalogProduct$2 {
|
|
7
7
|
id: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { createDevFetcher } from './dev-fetcher.js';
|
|
2
2
|
import { PublicDesign, PublicFill, PublicOptions } from '@snowcone-app/mockup-url';
|
|
3
|
-
import { O as OptionAttribute, a as OptionSelection, C as Combination, F as FrameworkAdapter, b as ComponentProps, c as ComponentDescriptor, d as ComponentState, e as ComponentContext, f as ComponentLifecycleHooks, E as EventHandler, g as FrameworkUtilities, P as PlacementSettings, M as MockupResult, R as RealtimeMockupService } from './websocket-
|
|
4
|
-
export { A as AdapterRegistry, h as ProductComponentContext, i as RealtimeMockupCallbacks, j as RealtimeMockupState, k as RenderResult, W as WebSocketConfig, l as WebSocketMessage, m as adapterRegistry, n as computeDisabledChoices, o as createComponent, p as defineComponent, q as deriveDefaultSelection, r as findBestCombination, s as getPricePreview, t as isOptionAvailable, u as resolveBestCombination } from './websocket-
|
|
3
|
+
import { O as OptionAttribute, a as OptionSelection, C as Combination, F as FrameworkAdapter, b as ComponentProps, c as ComponentDescriptor, d as ComponentState, e as ComponentContext, f as ComponentLifecycleHooks, E as EventHandler, g as FrameworkUtilities, P as PlacementSettings, M as MockupResult, R as RealtimeMockupService } from './websocket-X4tcUm78.js';
|
|
4
|
+
export { A as AdapterRegistry, h as ProductComponentContext, i as RealtimeMockupCallbacks, j as RealtimeMockupState, k as RenderResult, W as WebSocketConfig, l as WebSocketMessage, m as adapterRegistry, n as computeDisabledChoices, o as createComponent, p as defineComponent, q as deriveDefaultSelection, r as findBestCombination, s as getPricePreview, t as isOptionAvailable, u as resolveBestCombination } from './websocket-X4tcUm78.js';
|
|
5
5
|
|
|
6
6
|
interface CatalogProduct$2 {
|
|
7
7
|
id: string;
|
package/dist/index.js
CHANGED
package/dist/react.cjs
CHANGED
|
@@ -291,6 +291,8 @@ var RealtimeMockupService = class {
|
|
|
291
291
|
placement: responsePlacement,
|
|
292
292
|
renderMs: data.renderMs,
|
|
293
293
|
blobToRenderMs: data.blobToRenderMs,
|
|
294
|
+
costUsd: data.costUsd,
|
|
295
|
+
renderCache: data.renderCache,
|
|
294
296
|
canvasRenderTiming: data.canvasRenderTiming
|
|
295
297
|
};
|
|
296
298
|
const existingIndex = this.mockupResults.findIndex((m) => m.mockupId === data.mockupId);
|
package/dist/react.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { M as MockupResult, W as WebSocketConfig, P as PlacementSettings, F as FrameworkAdapter, b as ComponentProps, d as ComponentState, e as ComponentContext, f as ComponentLifecycleHooks, E as EventHandler, c as ComponentDescriptor, g as FrameworkUtilities } from './websocket-
|
|
1
|
+
import { M as MockupResult, W as WebSocketConfig, P as PlacementSettings, F as FrameworkAdapter, b as ComponentProps, d as ComponentState, e as ComponentContext, f as ComponentLifecycleHooks, E as EventHandler, c as ComponentDescriptor, g as FrameworkUtilities } from './websocket-X4tcUm78.cjs';
|
|
2
2
|
|
|
3
3
|
interface UseRealtimeMockupOptions {
|
|
4
4
|
wsUrl?: string;
|
package/dist/react.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { M as MockupResult, W as WebSocketConfig, P as PlacementSettings, F as FrameworkAdapter, b as ComponentProps, d as ComponentState, e as ComponentContext, f as ComponentLifecycleHooks, E as EventHandler, c as ComponentDescriptor, g as FrameworkUtilities } from './websocket-
|
|
1
|
+
import { M as MockupResult, W as WebSocketConfig, P as PlacementSettings, F as FrameworkAdapter, b as ComponentProps, d as ComponentState, e as ComponentContext, f as ComponentLifecycleHooks, E as EventHandler, c as ComponentDescriptor, g as FrameworkUtilities } from './websocket-X4tcUm78.js';
|
|
2
2
|
|
|
3
3
|
interface UseRealtimeMockupOptions {
|
|
4
4
|
wsUrl?: string;
|
package/dist/react.js
CHANGED
package/dist/server.cjs
CHANGED
|
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/server.ts
|
|
21
21
|
var server_exports = {};
|
|
22
22
|
__export(server_exports, {
|
|
23
|
+
createBgRemoveHandler: () => createBgRemoveHandler,
|
|
23
24
|
createImageSearchHandler: () => createImageSearchHandler,
|
|
24
25
|
createMockupSignHandler: () => createMockupSignHandler,
|
|
25
26
|
createRealtimeGrantHandler: () => createRealtimeGrantHandler,
|
|
@@ -270,13 +271,14 @@ async function searchUnsplash(accessKey, appName, query, page, perPage) {
|
|
|
270
271
|
hasMore: photos.length === perPage
|
|
271
272
|
};
|
|
272
273
|
}
|
|
273
|
-
async function searchPixabay(apiKey, query, page, perPage) {
|
|
274
|
+
async function searchPixabay(apiKey, query, page, perPage, safesearch) {
|
|
274
275
|
const u = new URL("https://pixabay.com/api/");
|
|
275
276
|
u.searchParams.set("key", apiKey);
|
|
276
277
|
u.searchParams.set("q", query);
|
|
277
278
|
u.searchParams.set("page", String(page));
|
|
278
279
|
u.searchParams.set("per_page", String(perPage));
|
|
279
280
|
u.searchParams.set("image_type", "photo");
|
|
281
|
+
if (safesearch) u.searchParams.set("safesearch", "true");
|
|
280
282
|
if (!query) u.searchParams.set("order", "popular");
|
|
281
283
|
const res = await fetch(u.toString());
|
|
282
284
|
if (!res.ok) throw new Error(`pixabay ${res.status}`);
|
|
@@ -325,7 +327,7 @@ function createImageSearchHandler(opts) {
|
|
|
325
327
|
if (opts.pixabayApiKey) {
|
|
326
328
|
tasks.push({
|
|
327
329
|
source: "pixabay",
|
|
328
|
-
run: searchPixabay(opts.pixabayApiKey, query, page, perPage)
|
|
330
|
+
run: searchPixabay(opts.pixabayApiKey, query, page, perPage, opts.safesearch ?? false)
|
|
329
331
|
});
|
|
330
332
|
}
|
|
331
333
|
if (tasks.length === 0) {
|
|
@@ -386,8 +388,47 @@ function createImageSearchHandler(opts) {
|
|
|
386
388
|
return json({ error: "Method not allowed" }, 405);
|
|
387
389
|
};
|
|
388
390
|
}
|
|
391
|
+
var BG_REMOVE_MAX_URL_LENGTH = 2048;
|
|
392
|
+
function createBgRemoveHandler(opts) {
|
|
393
|
+
const base = opts.base ?? "https://api.snowcone.app";
|
|
394
|
+
return async function POST(req) {
|
|
395
|
+
if (opts.gate) {
|
|
396
|
+
const result = await opts.gate(req);
|
|
397
|
+
if (result instanceof Response) return result;
|
|
398
|
+
if (result === false) return json({ error: "Forbidden" }, 403);
|
|
399
|
+
}
|
|
400
|
+
let body;
|
|
401
|
+
try {
|
|
402
|
+
body = await req.json();
|
|
403
|
+
} catch {
|
|
404
|
+
return json({ error: "Invalid JSON" }, 400);
|
|
405
|
+
}
|
|
406
|
+
const imageUrl = body.imageUrl;
|
|
407
|
+
if (typeof imageUrl !== "string" || !/^https?:\/\//.test(imageUrl) || imageUrl.length > BG_REMOVE_MAX_URL_LENGTH) {
|
|
408
|
+
return json({ error: "imageUrl must be an http(s) URL" }, 400);
|
|
409
|
+
}
|
|
410
|
+
const f = opts.fetch ?? globalThis.fetch;
|
|
411
|
+
try {
|
|
412
|
+
const upstream = await f(`${base}/ai-generations/remove-background`, {
|
|
413
|
+
method: "POST",
|
|
414
|
+
headers: {
|
|
415
|
+
"Content-Type": "application/json",
|
|
416
|
+
"x-api-key": opts.shopKey
|
|
417
|
+
},
|
|
418
|
+
body: JSON.stringify({ imageUrl })
|
|
419
|
+
});
|
|
420
|
+
return new Response(await upstream.text(), {
|
|
421
|
+
status: upstream.status,
|
|
422
|
+
headers: { "Content-Type": "application/json" }
|
|
423
|
+
});
|
|
424
|
+
} catch {
|
|
425
|
+
return json({ error: "Background removal failed" }, 502);
|
|
426
|
+
}
|
|
427
|
+
};
|
|
428
|
+
}
|
|
389
429
|
// Annotate the CommonJS export names for ESM import in node:
|
|
390
430
|
0 && (module.exports = {
|
|
431
|
+
createBgRemoveHandler,
|
|
391
432
|
createImageSearchHandler,
|
|
392
433
|
createMockupSignHandler,
|
|
393
434
|
createRealtimeGrantHandler,
|
package/dist/server.d.cts
CHANGED
|
@@ -28,6 +28,8 @@
|
|
|
28
28
|
*
|
|
29
29
|
* Also home to {@link createImageSearchHandler}, the same-origin proxy route for
|
|
30
30
|
* the canvas stock-image search tab (your Unsplash / Pixabay keys stay
|
|
31
|
+
* server-side), and {@link createBgRemoveHandler}, the proxy for the canvas
|
|
32
|
+
* "remove background" button (your shop-scoped `ai:bg-remove` key stays
|
|
31
33
|
* server-side).
|
|
32
34
|
*/
|
|
33
35
|
/**
|
|
@@ -127,6 +129,11 @@ interface ImageSearchHandlerOptions {
|
|
|
127
129
|
gate?: SignGate;
|
|
128
130
|
/** Default page size (default 30, hard cap 50). */
|
|
129
131
|
perPage?: number;
|
|
132
|
+
/**
|
|
133
|
+
* Ask Pixabay to filter out images unsuitable for all ages
|
|
134
|
+
* (`safesearch=true`). Default `false`. Unsplash has no equivalent flag.
|
|
135
|
+
*/
|
|
136
|
+
safesearch?: boolean;
|
|
130
137
|
}
|
|
131
138
|
/**
|
|
132
139
|
* Build the same-origin proxy route the canvas image-search tab talks to.
|
|
@@ -158,5 +165,43 @@ interface ImageSearchHandlerOptions {
|
|
|
158
165
|
* if you expect volume.
|
|
159
166
|
*/
|
|
160
167
|
declare function createImageSearchHandler(opts: ImageSearchHandlerOptions): (req: Request) => Promise<Response>;
|
|
168
|
+
interface BgRemoveHandlerOptions {
|
|
169
|
+
/**
|
|
170
|
+
* Your Snowcone shop-scoped API key (`shk_…`) with the `ai:bg-remove` scope.
|
|
171
|
+
* Server-only — the browser only ever sees this route. Required.
|
|
172
|
+
*/
|
|
173
|
+
shopKey: string;
|
|
174
|
+
/** Backend base URL hosting `POST /ai-generations/remove-background`. Default api.snowcone.app. */
|
|
175
|
+
base?: string;
|
|
176
|
+
/** Optional auth / rate-limit / bot check, same SignGate type as the other handlers. */
|
|
177
|
+
gate?: SignGate;
|
|
178
|
+
/** Override fetch (tests / non-global-fetch runtimes). */
|
|
179
|
+
fetch?: typeof fetch;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Build the same-origin proxy route the canvas "remove background" button talks
|
|
183
|
+
* to. Your shop-scoped key (`ai:bg-remove` scope) stays server-side; the browser
|
|
184
|
+
* only ever sees this route. Pairs with the canvas `services.bgRemoveUrl`
|
|
185
|
+
* shorthand.
|
|
186
|
+
*
|
|
187
|
+
* - `POST { imageUrl }` (an `http(s)` URL) → proxies to the backend
|
|
188
|
+
* `POST /ai-generations/remove-background` with your key, returning its
|
|
189
|
+
* `{ imageUrl, … }` body verbatim.
|
|
190
|
+
* - Validates `imageUrl` is an `http(s)` URL (rejects `data:` / `blob:`) within
|
|
191
|
+
* the 2048-char cap before spending the key, mirroring the www route.
|
|
192
|
+
*
|
|
193
|
+
* ```ts
|
|
194
|
+
* // app/api/bg-remove/route.ts
|
|
195
|
+
* import { createBgRemoveHandler } from "@snowcone-app/sdk/server";
|
|
196
|
+
* export const POST = createBgRemoveHandler({
|
|
197
|
+
* shopKey: process.env.SNOWCONE_SHOP_API_KEY!, // ai:bg-remove scope, server-only
|
|
198
|
+
* gate: myAuthOrTurnstileCheck, // optional — keep it from being an open quota funnel
|
|
199
|
+
* });
|
|
200
|
+
* ```
|
|
201
|
+
*
|
|
202
|
+
* Background removal is billed, so gate this route the same way you gate signing
|
|
203
|
+
* and AI generation.
|
|
204
|
+
*/
|
|
205
|
+
declare function createBgRemoveHandler(opts: BgRemoveHandlerOptions): (req: Request) => Promise<Response>;
|
|
161
206
|
|
|
162
|
-
export { type ImageSearchHandlerOptions, type ImageSearchResponse, type ImageSearchResult, type MockupSignHandlerOptions, type RealtimeGrantHandlerOptions, type SignGate, createImageSearchHandler, createMockupSignHandler, createRealtimeGrantHandler, signGrantRequest, signMockupUrl };
|
|
207
|
+
export { type BgRemoveHandlerOptions, type ImageSearchHandlerOptions, type ImageSearchResponse, type ImageSearchResult, type MockupSignHandlerOptions, type RealtimeGrantHandlerOptions, type SignGate, createBgRemoveHandler, createImageSearchHandler, createMockupSignHandler, createRealtimeGrantHandler, signGrantRequest, signMockupUrl };
|
package/dist/server.d.ts
CHANGED
|
@@ -28,6 +28,8 @@
|
|
|
28
28
|
*
|
|
29
29
|
* Also home to {@link createImageSearchHandler}, the same-origin proxy route for
|
|
30
30
|
* the canvas stock-image search tab (your Unsplash / Pixabay keys stay
|
|
31
|
+
* server-side), and {@link createBgRemoveHandler}, the proxy for the canvas
|
|
32
|
+
* "remove background" button (your shop-scoped `ai:bg-remove` key stays
|
|
31
33
|
* server-side).
|
|
32
34
|
*/
|
|
33
35
|
/**
|
|
@@ -127,6 +129,11 @@ interface ImageSearchHandlerOptions {
|
|
|
127
129
|
gate?: SignGate;
|
|
128
130
|
/** Default page size (default 30, hard cap 50). */
|
|
129
131
|
perPage?: number;
|
|
132
|
+
/**
|
|
133
|
+
* Ask Pixabay to filter out images unsuitable for all ages
|
|
134
|
+
* (`safesearch=true`). Default `false`. Unsplash has no equivalent flag.
|
|
135
|
+
*/
|
|
136
|
+
safesearch?: boolean;
|
|
130
137
|
}
|
|
131
138
|
/**
|
|
132
139
|
* Build the same-origin proxy route the canvas image-search tab talks to.
|
|
@@ -158,5 +165,43 @@ interface ImageSearchHandlerOptions {
|
|
|
158
165
|
* if you expect volume.
|
|
159
166
|
*/
|
|
160
167
|
declare function createImageSearchHandler(opts: ImageSearchHandlerOptions): (req: Request) => Promise<Response>;
|
|
168
|
+
interface BgRemoveHandlerOptions {
|
|
169
|
+
/**
|
|
170
|
+
* Your Snowcone shop-scoped API key (`shk_…`) with the `ai:bg-remove` scope.
|
|
171
|
+
* Server-only — the browser only ever sees this route. Required.
|
|
172
|
+
*/
|
|
173
|
+
shopKey: string;
|
|
174
|
+
/** Backend base URL hosting `POST /ai-generations/remove-background`. Default api.snowcone.app. */
|
|
175
|
+
base?: string;
|
|
176
|
+
/** Optional auth / rate-limit / bot check, same SignGate type as the other handlers. */
|
|
177
|
+
gate?: SignGate;
|
|
178
|
+
/** Override fetch (tests / non-global-fetch runtimes). */
|
|
179
|
+
fetch?: typeof fetch;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Build the same-origin proxy route the canvas "remove background" button talks
|
|
183
|
+
* to. Your shop-scoped key (`ai:bg-remove` scope) stays server-side; the browser
|
|
184
|
+
* only ever sees this route. Pairs with the canvas `services.bgRemoveUrl`
|
|
185
|
+
* shorthand.
|
|
186
|
+
*
|
|
187
|
+
* - `POST { imageUrl }` (an `http(s)` URL) → proxies to the backend
|
|
188
|
+
* `POST /ai-generations/remove-background` with your key, returning its
|
|
189
|
+
* `{ imageUrl, … }` body verbatim.
|
|
190
|
+
* - Validates `imageUrl` is an `http(s)` URL (rejects `data:` / `blob:`) within
|
|
191
|
+
* the 2048-char cap before spending the key, mirroring the www route.
|
|
192
|
+
*
|
|
193
|
+
* ```ts
|
|
194
|
+
* // app/api/bg-remove/route.ts
|
|
195
|
+
* import { createBgRemoveHandler } from "@snowcone-app/sdk/server";
|
|
196
|
+
* export const POST = createBgRemoveHandler({
|
|
197
|
+
* shopKey: process.env.SNOWCONE_SHOP_API_KEY!, // ai:bg-remove scope, server-only
|
|
198
|
+
* gate: myAuthOrTurnstileCheck, // optional — keep it from being an open quota funnel
|
|
199
|
+
* });
|
|
200
|
+
* ```
|
|
201
|
+
*
|
|
202
|
+
* Background removal is billed, so gate this route the same way you gate signing
|
|
203
|
+
* and AI generation.
|
|
204
|
+
*/
|
|
205
|
+
declare function createBgRemoveHandler(opts: BgRemoveHandlerOptions): (req: Request) => Promise<Response>;
|
|
161
206
|
|
|
162
|
-
export { type ImageSearchHandlerOptions, type ImageSearchResponse, type ImageSearchResult, type MockupSignHandlerOptions, type RealtimeGrantHandlerOptions, type SignGate, createImageSearchHandler, createMockupSignHandler, createRealtimeGrantHandler, signGrantRequest, signMockupUrl };
|
|
207
|
+
export { type BgRemoveHandlerOptions, type ImageSearchHandlerOptions, type ImageSearchResponse, type ImageSearchResult, type MockupSignHandlerOptions, type RealtimeGrantHandlerOptions, type SignGate, createBgRemoveHandler, createImageSearchHandler, createMockupSignHandler, createRealtimeGrantHandler, signGrantRequest, signMockupUrl };
|
package/dist/server.js
CHANGED
|
@@ -160,13 +160,14 @@ async function searchUnsplash(accessKey, appName, query, page, perPage) {
|
|
|
160
160
|
hasMore: photos.length === perPage
|
|
161
161
|
};
|
|
162
162
|
}
|
|
163
|
-
async function searchPixabay(apiKey, query, page, perPage) {
|
|
163
|
+
async function searchPixabay(apiKey, query, page, perPage, safesearch) {
|
|
164
164
|
const u = new URL("https://pixabay.com/api/");
|
|
165
165
|
u.searchParams.set("key", apiKey);
|
|
166
166
|
u.searchParams.set("q", query);
|
|
167
167
|
u.searchParams.set("page", String(page));
|
|
168
168
|
u.searchParams.set("per_page", String(perPage));
|
|
169
169
|
u.searchParams.set("image_type", "photo");
|
|
170
|
+
if (safesearch) u.searchParams.set("safesearch", "true");
|
|
170
171
|
if (!query) u.searchParams.set("order", "popular");
|
|
171
172
|
const res = await fetch(u.toString());
|
|
172
173
|
if (!res.ok) throw new Error(`pixabay ${res.status}`);
|
|
@@ -215,7 +216,7 @@ function createImageSearchHandler(opts) {
|
|
|
215
216
|
if (opts.pixabayApiKey) {
|
|
216
217
|
tasks.push({
|
|
217
218
|
source: "pixabay",
|
|
218
|
-
run: searchPixabay(opts.pixabayApiKey, query, page, perPage)
|
|
219
|
+
run: searchPixabay(opts.pixabayApiKey, query, page, perPage, opts.safesearch ?? false)
|
|
219
220
|
});
|
|
220
221
|
}
|
|
221
222
|
if (tasks.length === 0) {
|
|
@@ -276,7 +277,46 @@ function createImageSearchHandler(opts) {
|
|
|
276
277
|
return json({ error: "Method not allowed" }, 405);
|
|
277
278
|
};
|
|
278
279
|
}
|
|
280
|
+
var BG_REMOVE_MAX_URL_LENGTH = 2048;
|
|
281
|
+
function createBgRemoveHandler(opts) {
|
|
282
|
+
const base = opts.base ?? "https://api.snowcone.app";
|
|
283
|
+
return async function POST(req) {
|
|
284
|
+
if (opts.gate) {
|
|
285
|
+
const result = await opts.gate(req);
|
|
286
|
+
if (result instanceof Response) return result;
|
|
287
|
+
if (result === false) return json({ error: "Forbidden" }, 403);
|
|
288
|
+
}
|
|
289
|
+
let body;
|
|
290
|
+
try {
|
|
291
|
+
body = await req.json();
|
|
292
|
+
} catch {
|
|
293
|
+
return json({ error: "Invalid JSON" }, 400);
|
|
294
|
+
}
|
|
295
|
+
const imageUrl = body.imageUrl;
|
|
296
|
+
if (typeof imageUrl !== "string" || !/^https?:\/\//.test(imageUrl) || imageUrl.length > BG_REMOVE_MAX_URL_LENGTH) {
|
|
297
|
+
return json({ error: "imageUrl must be an http(s) URL" }, 400);
|
|
298
|
+
}
|
|
299
|
+
const f = opts.fetch ?? globalThis.fetch;
|
|
300
|
+
try {
|
|
301
|
+
const upstream = await f(`${base}/ai-generations/remove-background`, {
|
|
302
|
+
method: "POST",
|
|
303
|
+
headers: {
|
|
304
|
+
"Content-Type": "application/json",
|
|
305
|
+
"x-api-key": opts.shopKey
|
|
306
|
+
},
|
|
307
|
+
body: JSON.stringify({ imageUrl })
|
|
308
|
+
});
|
|
309
|
+
return new Response(await upstream.text(), {
|
|
310
|
+
status: upstream.status,
|
|
311
|
+
headers: { "Content-Type": "application/json" }
|
|
312
|
+
});
|
|
313
|
+
} catch {
|
|
314
|
+
return json({ error: "Background removal failed" }, 502);
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
}
|
|
279
318
|
export {
|
|
319
|
+
createBgRemoveHandler,
|
|
280
320
|
createImageSearchHandler,
|
|
281
321
|
createMockupSignHandler,
|
|
282
322
|
createRealtimeGrantHandler,
|
|
@@ -240,6 +240,10 @@ interface MockupResult {
|
|
|
240
240
|
renderMs?: number;
|
|
241
241
|
/** Time from blob received to render start in ms */
|
|
242
242
|
blobToRenderMs?: number;
|
|
243
|
+
/** Cost of this render in USD (same value as the x-render-cost-usd header on HTTP renders) */
|
|
244
|
+
costUsd?: number;
|
|
245
|
+
/** Whether the underlying render was served from cache */
|
|
246
|
+
renderCache?: 'hit' | 'miss';
|
|
243
247
|
/** Server-side canvas render timing breakdown (present when canvas_state was used) */
|
|
244
248
|
canvasRenderTiming?: any;
|
|
245
249
|
}
|
|
@@ -262,6 +266,10 @@ interface WebSocketMessage {
|
|
|
262
266
|
renderMs?: number;
|
|
263
267
|
/** Time from blob received to render start in ms */
|
|
264
268
|
blobToRenderMs?: number;
|
|
269
|
+
/** Cost of this render in USD (same value as the x-render-cost-usd header on HTTP renders) */
|
|
270
|
+
costUsd?: number;
|
|
271
|
+
/** Whether the underlying render was served from cache */
|
|
272
|
+
renderCache?: 'hit' | 'miss';
|
|
265
273
|
/** Server-side canvas render timing (only present when canvas_state was used) */
|
|
266
274
|
canvasRenderTiming?: {
|
|
267
275
|
poolAcquireMs: number;
|
|
@@ -240,6 +240,10 @@ interface MockupResult {
|
|
|
240
240
|
renderMs?: number;
|
|
241
241
|
/** Time from blob received to render start in ms */
|
|
242
242
|
blobToRenderMs?: number;
|
|
243
|
+
/** Cost of this render in USD (same value as the x-render-cost-usd header on HTTP renders) */
|
|
244
|
+
costUsd?: number;
|
|
245
|
+
/** Whether the underlying render was served from cache */
|
|
246
|
+
renderCache?: 'hit' | 'miss';
|
|
243
247
|
/** Server-side canvas render timing breakdown (present when canvas_state was used) */
|
|
244
248
|
canvasRenderTiming?: any;
|
|
245
249
|
}
|
|
@@ -262,6 +266,10 @@ interface WebSocketMessage {
|
|
|
262
266
|
renderMs?: number;
|
|
263
267
|
/** Time from blob received to render start in ms */
|
|
264
268
|
blobToRenderMs?: number;
|
|
269
|
+
/** Cost of this render in USD (same value as the x-render-cost-usd header on HTTP renders) */
|
|
270
|
+
costUsd?: number;
|
|
271
|
+
/** Whether the underlying render was served from cache */
|
|
272
|
+
renderCache?: 'hit' | 'miss';
|
|
265
273
|
/** Server-side canvas render timing (only present when canvas_state was used) */
|
|
266
274
|
canvasRenderTiming?: {
|
|
267
275
|
poolAcquireMs: number;
|