@proofkit/fmodata
Version:
FileMaker OData API client
188 lines (187 loc) • 6.84 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
import { Effect } from "effect";
import { requestFromService, runLayerOrThrow } from "../effect.js";
import { isColumn } from "../orm/column.js";
import { FilterExpression } from "../orm/operators.js";
import { getTableName } from "../orm/table.js";
import { formatSelectFields } from "./builders/select-utils.js";
import { createClientRuntime } from "./runtime.js";
class WebhookManager {
constructor(layer) {
__publicField(this, "layer");
__publicField(this, "config");
const runtime = createClientRuntime(layer);
this.layer = runtime.layer;
this.config = runtime.config;
}
/**
* Adds a new webhook to the database.
* @param webhook - The webhook configuration object
* @param webhook.webhook - The webhook URL to call
* @param webhook.tableName - The FMTable instance for the table to monitor
* @param webhook.headers - Optional custom headers to include in webhook requests
* @param webhook.notifySchemaChanges - Whether to notify on schema changes
* @param webhook.select - Optional field selection (string or array of Column references)
* @param webhook.filter - Optional filter (string or FilterExpression)
* @returns Promise resolving to the created webhook data with ID
* @example
* ```ts
* const result = await db.webhook.add({
* webhook: "https://example.com/webhook",
* tableName: contactsTable,
* headers: { "X-Custom-Header": "value" },
* });
* // result.webhookResult.webhookID contains the new webhook ID
* ```
* @example
* ```ts
* // Using filter expressions and column arrays (same DX as query builder)
* const result = await db.webhook.add({
* webhook: "https://example.com/webhook",
* tableName: contacts,
* filter: eq(contacts.name, "John"),
* select: [contacts.name, contacts.PrimaryKey],
* });
* ```
*/
add(webhook, options) {
const tableName = getTableName(webhook.tableName);
const useEntityIds = (options == null ? void 0 : options.useEntityIds) ?? this.config.useEntityIds ?? false;
let filter;
if (webhook.filter !== void 0) {
if (webhook.filter instanceof FilterExpression) {
filter = webhook.filter.toODataFilter(useEntityIds);
} else {
filter = webhook.filter;
}
}
let select;
if (webhook.select !== void 0) {
if (Array.isArray(webhook.select)) {
const fieldNames = webhook.select.map((item) => {
if (isColumn(item)) {
return item.getFieldIdentifier(useEntityIds);
}
return String(item);
});
select = formatSelectFields(fieldNames, webhook.tableName, useEntityIds);
} else {
select = webhook.select;
}
}
const requestBody = {
webhook: webhook.webhook,
tableName
};
if (webhook.headers !== void 0) {
requestBody.headers = webhook.headers;
}
if (webhook.notifySchemaChanges !== void 0) {
requestBody.notifySchemaChanges = webhook.notifySchemaChanges;
}
if (select !== void 0) {
requestBody.select = select;
}
if (filter !== void 0) {
requestBody.filter = filter;
}
const pipeline = Effect.gen(this, function* () {
return yield* requestFromService(`/${this.config.databaseName}/Webhook.Add`, {
...options,
method: "POST",
body: JSON.stringify(requestBody),
databaseNameNormalizationMode: "ensureExtension"
});
});
return runLayerOrThrow(this.layer, pipeline, "fmodata.webhook.add");
}
/**
* Deletes a webhook by ID.
* @param webhookId - The ID of the webhook to delete
* @returns Promise that resolves when the webhook is deleted
* @example
* ```ts
* await db.webhook.remove(1);
* ```
*/
async remove(webhookId, options) {
const pipeline = Effect.gen(this, function* () {
return yield* requestFromService(`/${this.config.databaseName}/Webhook.Delete(${webhookId})`, {
...options,
method: "POST",
databaseNameNormalizationMode: "ensureExtension"
});
});
await runLayerOrThrow(this.layer, pipeline, "fmodata.webhook.remove");
}
/**
* Gets a webhook by ID.
* @param webhookId - The ID of the webhook to retrieve
* @returns Promise resolving to the webhook data
* @example
* ```ts
* const webhook = await db.webhook.get(1);
* // webhook.webhookID, webhook.tableName, webhook.webhook, etc.
* ```
*/
get(webhookId, options) {
const pipeline = Effect.gen(this, function* () {
return yield* requestFromService(`/${this.config.databaseName}/Webhook.Get(${webhookId})`, options);
});
return runLayerOrThrow(this.layer, pipeline, "fmodata.webhook.get");
}
/**
* Lists all webhooks.
* @returns Promise resolving to webhook list response with status and webhooks array
* @example
* ```ts
* const result = await db.webhook.list();
* // result.status contains the status
* // result.webhooks contains the array of webhooks
* ```
*/
list(options) {
const pipeline = Effect.gen(this, function* () {
return yield* requestFromService(`/${this.config.databaseName}/Webhook.GetAll`, {
...options,
databaseNameNormalizationMode: "ensureExtension"
});
});
return runLayerOrThrow(this.layer, pipeline, "fmodata.webhook.list");
}
/**
* Invokes a webhook by ID, optionally for specific row IDs.
* @param webhookId - The ID of the webhook to invoke
* @param options - Optional configuration
* @param options.rowIDs - Array of row IDs to trigger the webhook for
* @returns Promise resolving to the invocation result (type unknown until API behavior is confirmed)
* @example
* ```ts
* // Invoke for all rows
* await db.webhook.invoke(1);
*
* // Invoke for specific rows
* await db.webhook.invoke(1, { rowIDs: [63, 61] });
* ```
*/
invoke(webhookId, options, executeOptions) {
const body = {};
if ((options == null ? void 0 : options.rowIDs) !== void 0) {
body.rowIDs = options.rowIDs;
}
const pipeline = Effect.gen(this, function* () {
return yield* requestFromService(`/${this.config.databaseName}/Webhook.Invoke(${webhookId})`, {
method: "POST",
body: Object.keys(body).length > 0 ? JSON.stringify(body) : void 0,
...executeOptions
});
});
return runLayerOrThrow(this.layer, pipeline, "fmodata.webhook.invoke");
}
}
export {
WebhookManager
};
//# sourceMappingURL=webhook-builder.js.map