@proofkit/better-auth
Version:
FileMaker adapter for Better Auth
84 lines (83 loc) • 2.39 kB
JavaScript
import { createSchema, createFetch } from "@better-fetch/fetch";
import { logger } from "@better-fetch/logger";
import { logger as logger$1 } from "better-auth";
import { ok, err } from "neverthrow";
import { z } from "zod/v4";
const schema = createSchema({
/**
* Create a new table
*/
"@post/FileMaker_Tables": {
input: z.object({ tableName: z.string(), fields: z.array(z.any()) })
},
/**
* Add fields to a table
*/
"@patch/FileMaker_Tables/:tableName": {
params: z.object({ tableName: z.string() }),
input: z.object({ fields: z.array(z.any()) })
},
/**
* Delete a table
*/
"@delete/FileMaker_Tables/:tableName": {
params: z.object({ tableName: z.string() })
},
/**
* Delete a field from a table
*/
"@delete/FileMaker_Tables/:tableName/:fieldName": {
params: z.object({ tableName: z.string(), fieldName: z.string() })
}
});
function createFmOdataFetch(args) {
const result = validateUrl(args.serverUrl);
if (result.isErr()) {
throw new Error("Invalid server URL");
}
let baseURL = result.value.origin;
if ("apiKey" in args.auth) {
baseURL += `/otto`;
}
baseURL += `/fmi/odata/v4/${args.database}`;
return createFetch({
baseURL,
auth: "apiKey" in args.auth ? { type: "Bearer", token: args.auth.apiKey } : {
type: "Basic",
username: args.auth.username,
password: args.auth.password
},
onError: (error) => {
console.error("url", error.request.url.toString());
console.log(error.error);
console.log("error.request.body", JSON.stringify(error.request.body));
},
schema,
plugins: [
logger({
verbose: args.logging === "verbose",
enabled: args.logging === "verbose" || !!args.logging,
console: {
fail: (...args2) => logger$1.error("better-fetch", ...args2),
success: (...args2) => logger$1.info("better-fetch", ...args2),
log: (...args2) => logger$1.info("better-fetch", ...args2),
error: (...args2) => logger$1.error("better-fetch", ...args2),
warn: (...args2) => logger$1.warn("better-fetch", ...args2)
}
})
]
});
}
function validateUrl(input) {
try {
const url = new URL(input);
return ok(url);
} catch (error) {
return err(error);
}
}
export {
createFmOdataFetch,
validateUrl
};
//# sourceMappingURL=index.js.map