//native
type Recraft = {
apiKey: string;
};
type Base64 = string;
/**
* Replace Background
*
*/
export async function main(
auth: Recraft,
body: {
block_nsfw?: false | true;
calculate_features?: false | true;
image: {
base64: Base64;
type:
| "image/png"
| "image/jpeg"
| "image/gif"
| "application/pdf"
| "appication/json"
| "text/csv"
| "text/plain"
| "audio/mpeg"
| "audio/wav"
| "video/mp4";
name: string;
};
mask: {
base64: Base64;
type:
| "image/png"
| "image/jpeg"
| "image/gif"
| "application/pdf"
| "appication/json"
| "text/csv"
| "text/plain"
| "audio/mpeg"
| "audio/wav"
| "video/mp4";
name: string;
};
model?:
| "refm1"
| "recraft20b"
| "recraftv2"
| "recraftv3"
| "flux1_1pro"
| "flux1dev";
n?: number;
prompt: string;
random_seed?: number;
response_format?: "url" | "b64_json";
style?:
| "digital_illustration"
| "icon"
| "realistic_image"
| "vector_illustration";
style_id?: string;
substyle?:
| "2d_art_poster"
| "3d"
| "80s"
| "glow"
| "grain"
| "hand_drawn"
| "infantile_sketch"
| "kawaii"
| "pixel_art"
| "psychedelic"
| "seamless"
| "voxel"
| "watercolor"
| "broken_line"
| "colored_outline"
| "colored_shapes"
| "colored_shapes_gradient"
| "doodle_fill"
| "doodle_offset_fill"
| "offset_fill"
| "outline"
| "outline_gradient"
| "uneven_fill"
| "70s"
| "cartoon"
| "doodle_line_art"
| "engraving"
| "flat_2"
| "line_art"
| "linocut"
| "b_and_w"
| "enterprise"
| "hard_flash"
| "hdr"
| "motion_blur"
| "natural_light"
| "studio_portrait"
| "line_circuit"
| "2d_art_poster_2"
| "engraving_color"
| "flat_air_art"
| "hand_drawn_outline"
| "handmade_3d"
| "stickers_drawings";
text_layout?: { bbox: number[][]; text: string }[];
},
) {
const url = new URL(
`https://external.api.recraft.ai/v1/images/replaceBackground`,
);
const formData = new FormData();
for (const [k, v] of Object.entries(body)) {
if (v !== undefined && v !== "") {
if (["image", "mask"].includes(k)) {
const { base64, type, name } = v as {
base64: Base64;
type: string;
name: string;
};
formData.append(
k,
new Blob([Uint8Array.from(atob(base64), (m) => m.codePointAt(0)!)], {
type,
}),
name,
);
} else {
formData.append(k, String(v));
}
}
}
const response = await fetch(url, {
method: "POST",
headers: {
Authorization: "Bearer " + auth.apiKey,
},
body: formData,
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 428 days ago