import { createConnector, createAction } from "@databite/build";
import { createFlow } from "@databite/flow";
import { z } from "zod";
// Define configuration schemas
const integrationConfig = z.object({
apiKey: z.string(),
baseUrl: z.string().url(),
});
const connectionConfig = z.object({
accessToken: z.string(),
userId: z.string(),
});
// Create authentication flow
const authFlow = createFlow("authenticate")
.form("getCredentials", {
title: "API Authentication",
fields: [
{ name: "apiKey", label: "API Key", type: "password", required: true },
{ name: "baseUrl", label: "Base URL", type: "url", required: true },
],
})
.http("validateCredentials", {
url: (input) => `${input.getCredentials.baseUrl}/auth/validate`,
method: "POST",
headers: { Authorization: `Bearer ${input.getCredentials.apiKey}` },
returnType: { accessToken: "", userId: "" },
})
.returns((context) => ({
accessToken: context.validateCredentials.accessToken,
userId: context.validateCredentials.userId,
}));
// Create the connector
const myConnector = createConnector()
.withIdentity("my-service", "My Service")
.withVersion("1.0.0")
.withAuthor("Your Name")
.withDescription("Connector for My Service API")
.withIntegrationConfig(integrationConfig)
.withConnectionConfig(connectionConfig)
.withAuthenticationFlow(authFlow)
.withActions({
getUser: createAction({
label: "Get User",
description: "Fetch user information by ID",
inputSchema: z.object({ id: z.string() }),
outputSchema: z.object({
id: z.string(),
name: z.string(),
email: z.string(),
}),
handler: async (params, connection) => {
const response = await fetch(
`${connection.config.baseUrl}/users/${params.id}`,
{
headers: {
Authorization: `Bearer ${connection.config.accessToken}`,
},
}
);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
return await response.json();
},
}),
})
.build();