Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

1.5 KB
Raw Blame
import FormData from 'form-data';

import {
	AuthenticationType,
	httpClient,
	HttpMethod,
	QueryParams,
} from '@activepieces/pieces-common';

import type { ImageRouterAuthValue } from './types';

function baseUrl(): string {
	return BASE_URL;
}

async function request<T>({ auth, method, path, query, body }: RequestParams): Promise<T> {
	const response = await httpClient.sendRequest<T>({
		method,
		url: `${BASE_URL}${path}`,
		headers: {
			Authorization: `Bearer ${auth.secret_text}`,
			'Content-Type': 'application/json',
		},
		queryParams: query,
		body,
	});
	return response.body;
}

async function upload<T>({ auth, path, form }: UploadParams): Promise<T> {
	const response = await httpClient.sendRequest<T>({
		method: HttpMethod.POST,
		url: `${BASE_URL}${path}`,
		authentication: {
			type: AuthenticationType.BEARER_TOKEN,
			token: auth.secret_text,
		},
		headers: {
			...form.getHeaders(),
		},
		body: form,
	});
	return response.body;
}

async function download({ url }: { url: string }): Promise<Buffer> {
	const response = await httpClient.sendRequest({
		method: HttpMethod.GET,
		url,
		responseType: 'arraybuffer',
	});
	return Buffer.from(response.body);
}

export const imageRouterClient = { baseUrl, request, upload, download };

const BASE_URL = 'https://api.imagerouter.io';

type RequestParams = {
	auth: ImageRouterAuthValue;
	method: HttpMethod;
	path: string;
	query?: QueryParams;
	body?: unknown;
};

type UploadParams = {
	auth: ImageRouterAuthValue;
	path: string;
	form: FormData;
};