@proofkit/fmdapi
Version:
FileMaker Data API client
257 lines (256 loc) • 8.53 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 { FileMakerError } from "../client-types.js";
class BaseFetchAdapter {
constructor(options) {
__publicField(this, "server");
__publicField(this, "db");
__publicField(this, "refreshToken");
__publicField(this, "baseUrl");
// eslint-disable-next-line @typescript-eslint/no-unused-vars
__publicField(this, "getToken", async (args) => {
throw new Error("getToken method not implemented by Fetch Adapter");
});
__publicField(this, "request", async (params) => {
var _a, _b;
const {
query,
body,
method = "GET",
retry = false,
fetchOptions = {}
} = params;
const url = new URL(`${this.baseUrl}${params.url}`);
if (query) {
const { _sort, ...rest } = query;
const searchParams = new URLSearchParams(rest);
if (query.portalRanges && typeof query.portalRanges === "object") {
for (const [portalName, value] of Object.entries(
query.portalRanges
)) {
if (value) {
if (value.offset && value.offset > 0) {
searchParams.set(
`_offset.${portalName}`,
value.offset.toString()
);
}
if (value.limit) {
searchParams.set(`_limit.${portalName}`, value.limit.toString());
}
}
}
}
if (_sort) {
searchParams.set("_sort", JSON.stringify(_sort));
}
searchParams.delete("portalRanges");
url.search = searchParams.toString();
}
if (body && "portalRanges" in body) {
for (const [portalName, value] of Object.entries(
body.portalRanges
)) {
if (value) {
if (value.offset && value.offset > 0) {
url.searchParams.set(
`_offset.${portalName}`,
value.offset.toString()
);
}
if (value.limit) {
url.searchParams.set(
`_limit.${portalName}`,
value.limit.toString()
);
}
}
}
delete body.portalRanges;
}
const controller = new AbortController();
let timeout = null;
if (params.timeout)
timeout = setTimeout(() => controller.abort(), params.timeout);
const token = await this.getToken({ refresh: retry });
const headers = new Headers(fetchOptions == null ? void 0 : fetchOptions.headers);
headers.set("Authorization", `Bearer ${token}`);
if (!(body instanceof FormData)) {
headers.set("Content-Type", "application/json");
}
const res = await fetch(url.toString(), {
...fetchOptions,
method,
body: body instanceof FormData ? body : body ? JSON.stringify(body) : void 0,
headers,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
signal: controller.signal
});
if (timeout) clearTimeout(timeout);
let respData;
try {
respData = await res.json();
} catch {
respData = {};
}
if (!res.ok) {
if (((_a = respData == null ? void 0 : respData.messages) == null ? void 0 : _a[0].code) === "952" && !retry && this.refreshToken) {
return this.request({ ...params, retry: true });
} else {
throw new FileMakerError(
((_b = respData == null ? void 0 : respData.messages) == null ? void 0 : _b[0].code) ?? "500",
`Filemaker Data API failed with (${res.status}): ${JSON.stringify(
respData,
null,
2
)}`
);
}
}
return respData.response;
});
__publicField(this, "list", async (opts) => {
const { data, layout } = opts;
const resp = await this.request({
url: `/layouts/${layout}/records`,
query: data,
fetchOptions: opts.fetch,
timeout: opts.timeout
});
return resp;
});
__publicField(this, "get", async (opts) => {
const { data, layout } = opts;
const resp = await this.request({
url: `/layouts/${layout}/records/${data.recordId}`,
fetchOptions: opts.fetch,
timeout: opts.timeout
});
return resp;
});
__publicField(this, "find", async (opts) => {
const { data, layout } = opts;
const resp = await this.request({
url: `/layouts/${layout}/_find`,
body: data,
method: "POST",
fetchOptions: opts.fetch,
timeout: opts.timeout
});
return resp;
});
__publicField(this, "create", async (opts) => {
const { data, layout } = opts;
const resp = await this.request({
url: `/layouts/${layout}/records`,
body: data,
method: "POST",
fetchOptions: opts.fetch,
timeout: opts.timeout
});
return resp;
});
__publicField(this, "update", async (opts) => {
const {
data: { recordId, ...data },
layout
} = opts;
const resp = await this.request({
url: `/layouts/${layout}/records/${recordId}`,
body: data,
method: "PATCH",
fetchOptions: opts.fetch,
timeout: opts.timeout
});
return resp;
});
__publicField(this, "delete", async (opts) => {
const { data, layout } = opts;
const resp = await this.request({
url: `/layouts/${layout}/records/${data.recordId}`,
method: "DELETE",
fetchOptions: opts.fetch,
timeout: opts.timeout
});
return resp;
});
__publicField(this, "layoutMetadata", async (opts) => {
return await this.request({
url: `/layouts/${opts.layout}`,
fetchOptions: opts.fetch,
timeout: opts.timeout
});
});
/**
* Execute a script within the database
*/
__publicField(this, "executeScript", async (opts) => {
const { script, scriptParam, layout } = opts;
const resp = await this.request({
url: `/layouts/${layout}/script/${script}`,
query: scriptParam ? { "script.param": scriptParam } : void 0,
fetchOptions: opts.fetch,
timeout: opts.timeout
});
return resp;
});
/**
* Returns a list of available layouts on the database.
*/
__publicField(this, "layouts", async (opts) => {
return await this.request({
url: "/layouts",
fetchOptions: opts == null ? void 0 : opts.fetch,
timeout: opts == null ? void 0 : opts.timeout
});
});
/**
* Returns a list of available scripts on the database.
*/
__publicField(this, "scripts", async (opts) => {
return await this.request({
url: "/scripts",
fetchOptions: opts == null ? void 0 : opts.fetch,
timeout: opts == null ? void 0 : opts.timeout
});
});
__publicField(this, "containerUpload", async (opts) => {
let url = `/layouts/${opts.layout}/records/${opts.data.recordId}/containers/${opts.data.containerFieldName}`;
if (opts.data.repetition) url += `/${opts.data.repetition}`;
const formData = new FormData();
formData.append("upload", opts.data.file);
await this.request({
url,
method: "POST",
body: formData,
timeout: opts.timeout,
fetchOptions: opts.fetch
});
});
/**
* Set global fields for the current session
*/
__publicField(this, "globals", async (opts) => {
return await this.request({
url: "/globals",
method: "PATCH",
body: { globalFields: opts.globalFields },
fetchOptions: opts == null ? void 0 : opts.fetch,
timeout: opts == null ? void 0 : opts.timeout
});
});
this.server = options.server;
this.db = options.db;
this.refreshToken = options.refreshToken ?? false;
this.baseUrl = new URL(
`${this.server}/fmi/data/vLatest/databases/${this.db}`
);
if (this.db === "") throw new Error("Database name is required");
}
}
export {
BaseFetchAdapter
};
//# sourceMappingURL=fetch-base.js.map