Skip to content

Commit

Permalink
add write operations to wv client
Browse files Browse the repository at this point in the history
  • Loading branch information
eluce2 committed Jun 7, 2024
1 parent 1b8b8dc commit f7777c1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/five-toes-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@proofgeist/fmdapi": minor
---

Support for write operations in FileMaker 2024
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ const result = await client.list<TContact>({ layout: "Contacts" });

A (nearly) identical client designed to be used with the [fm-webviewer-fetch](https://github.com/proofgeist/fm-webviewer-fetch) library when integrating within a FileMaker WebViewer instead of the browser. Using this client requires a bit extra configuration within your FileMaker file, but provides great developer experience, especially when using TypeScript and the codegen features.

(v3.5+) Support for write operations in FileMaker 2024

Install the [fm-webviewer-fetch](https://github.com/proofgeist/fm-webviewer-fetch) library to your project:

```sh
Expand Down Expand Up @@ -257,14 +259,15 @@ import { fileTokenStore } from "@proofgeist/fmdapi/dist/tokenStore/file.js"; //
| valueLists | `strict` `allowEmpty` `ignore` | `ignore` | If `strict`, will add enum types based on the value list defined for the field. If `allowEmpty`, will append `""` to the value list. Otherwise, fields are typed as normal. |
| strictNumbers | `boolean` | `false` | (v2.2.11+) If true, the zod schema will apply a transformer to force all number fields to be either `number` or `null`. <br>**WARNING:** If you are not using Zod or the auto-generated layout specific client, enabling this option may result in false types! |
| generateClient | `boolean` | none | If present, override the `generateClient` option for this schema only. |

#### Codegen CLI options
| Option | Default | Description |
| --- | --- | --- |
| --init | false | Run with this flag to add the config file to your project |
| --config <path> | - | path to a non-default config file |
| --env-path <path> | .env.local | path to your .env file |
| --skip-env-check | - | Ignore loading environment variables from a file. |

| Option | Default | Description |
| ----------------- | ---------- | --------------------------------------------------------- |
| --init | false | Run with this flag to add the config file to your project |
| --config <path> | - | path to a non-default config file |
| --env-path <path> | .env.local | path to your .env file |
| --skip-env-check | - | Ignore loading environment variables from a file. |

## FAQ

Expand Down
38 changes: 25 additions & 13 deletions src/wv.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { z } from "zod";
import {
CreateParams,
Expand Down Expand Up @@ -60,7 +61,7 @@ function DataApi<
async function request(params: {
layout: string;
body: object;
action?: "read" | "metaData";
action?: "read" | "metaData" | "create" | "update" | "delete" | "duplicate";
}): Promise<unknown> {
const { action = "read", layout, body } = params;
const { fmFetch } = await import("@proofgeist/fm-webviewer-fetch").catch(
Expand Down Expand Up @@ -142,6 +143,7 @@ function DataApi<
? ListParams<T, U> & Partial<WithLayout> & FetchOptions
: ListParams<T, U> & WithLayout & FetchOptions
): Promise<GetResponse<T, U>> {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { layout = options.layout, fetch, ...params } = args ?? {};
if (layout === undefined) throw new Error("Must specify layout");

Expand Down Expand Up @@ -197,8 +199,7 @@ function DataApi<
}
/**
* Create a new record in a given layout
* @deprecated Not supported by Execute Data API script step
* @throws {Error} Always
* @since FileMaker 2024
*/
async function create<T extends Td = Td, U extends Ud = Ud>(
args: Opts["layout"] extends string
Expand All @@ -218,6 +219,7 @@ function DataApi<
: GetArgs<U> & WithLayout & FetchOptions
): Promise<GetResponse<T, U>> {
args.recordId = asNumber(args.recordId);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { recordId, layout = options.layout, fetch, ...params } = args;
if (!layout) throw new Error("Must specify layout");
const data = await request({
Expand All @@ -230,8 +232,7 @@ function DataApi<
}
/**
* Update a single record by internal RecordId
* @deprecated Not supported by Execute Data API script step
* @throws {Error} Always
* @since FileMaker 2024
*/
async function update<T extends Td = Td, U extends Ud = Ud>(
args: Opts["layout"] extends string
Expand All @@ -241,12 +242,16 @@ function DataApi<
args.recordId = asNumber(args.recordId);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { recordId, fieldData, layout = options.layout, ...params } = args;
throw new Error("Not supported by Execute Data API script step");
if (!layout) throw new Error("Must specify layout");
return (await request({
action: "update",
layout,
body: { recordId, fieldData, ...params },
})) as UpdateResponse;
}
/**
* Delete a single record by internal RecordId
* @deprecated Not supported by Execute Data API script step
* @throws {Error} Always
* @since FileMaker 2024
*/
async function deleteRecord(
args: Opts["layout"] extends string
Expand All @@ -256,7 +261,12 @@ function DataApi<
args.recordId = asNumber(args.recordId);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { recordId, layout = options.layout, fetch, ...params } = args;
throw new Error("Not supported by Execute Data API script step");
if (!layout) throw new Error("Must specify layout");
return (await request({
action: "update",
layout,
body: { recordId, ...params },
})) as DeleteResponse;
}

/**
Expand Down Expand Up @@ -288,7 +298,9 @@ function DataApi<
query: queryInput,
layout = options.layout,
ignoreEmptyResult = false,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
timeout,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
fetch,
...params
} = args;
Expand Down Expand Up @@ -371,11 +383,11 @@ function DataApi<
return {
list,
listAll,
// create,
create,
get,
// update,
// delete: deleteRecord,
// metadata,
update,
delete: deleteRecord,
metadata,
find,
findOne,
findFirst,
Expand Down

0 comments on commit f7777c1

Please sign in to comment.