Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
},
"./node": {
"types": "./dist/node.d.ts",
"import": "./dist/node.js"
}
},
"files": [
Expand Down
99 changes: 99 additions & 0 deletions packages/client/src/__tests__/node.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { describe, expect, test } from "bun:test";
import { action, defineConnector, Lobu } from "../node.js";

describe("hosted connector helpers", () => {
test("registers metadata-only connectors", async () => {
let body: Record<string, unknown> | undefined;
const lobu = new Lobu({
baseUrl: "https://lobu.test/lobu",
org: "acme",
token: "token",
fetch: (async (...args) => {
body = JSON.parse(String(args[1]?.body)) as Record<string, unknown>;
return json({ ok: true });
}) as typeof fetch,
});

await lobu.connectors.register(
defineConnector({
key: "app.crm",
name: "CRM",
version: "1.0.0",
actions: {
refund: action({
key: "refund",
name: "Refund",
execute: async () => ({ ok: true }),
}),
},
})
);

expect(body?.action).toBe("install_connector");
expect((body?.connector_definition as { key?: string }).key).toBe(
"app.crm"
);
});

test("serves action runs through worker endpoints", async () => {
const controller = new AbortController();
const completed: Record<string, unknown>[] = [];
const lobu = new Lobu({
baseUrl: "https://lobu.test/lobu",
org: "acme",
token: "token",
fetch: (async (input, init) => {
const url = String(input);
if (url.endsWith("/api/workers/poll")) {
return json({
run_id: 7,
run_type: "action",
connector_key: "app.crm",
action_key: "refund",
action_input: { amount: 10 },
});
}
if (url.endsWith("/api/workers/complete-action")) {
completed.push(
JSON.parse(String(init?.body)) as Record<string, unknown>
);
controller.abort();
return json({ success: true });
}
throw new Error(`unexpected url ${url}`);
}) as typeof fetch,
});

await lobu.connectors.serve(
defineConnector({
key: "app.crm",
name: "CRM",
version: "1.0.0",
actions: {
refund: action<{ amount: number }>({
key: "refund",
name: "Refund",
execute: async (_ctx, input) => ({ refunded: input.amount }),
}),
},
}),
{ workerId: "worker-1", signal: controller.signal }
);

expect(completed).toEqual([
{
run_id: 7,
worker_id: "worker-1",
status: "success",
action_output: { refunded: 10 },
},
]);
});
});

function json(body: unknown): Response {
return new Response(JSON.stringify(body), {
status: 200,
headers: { "content-type": "application/json" },
});
}
2 changes: 2 additions & 0 deletions packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ export class Lobu {
constructor(options: LobuClientOptions) {
this.rest = new LobuRestClient({
baseUrl: options.baseUrl,
apiBaseUrl: options.apiBaseUrl,
token: options.token,
fetch: options.fetch ?? globalThis.fetch.bind(globalThis),
headers: options.headers,
org: options.org,
});
this.sessions = {
create: (input) => this.createSession(input),
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type {
LobuClientOptions,
LobuFetch,
LobuHeaders,
LobuInternalRequestOptions,
LobuSseEvent,
SendMessageOptions,
SendMessageResponse,
Expand Down
Loading