Skip to content

Commit

Permalink
allow custom store
Browse files Browse the repository at this point in the history
  • Loading branch information
eluce2 committed Apr 21, 2023
1 parent 39a65ec commit 5c2f0d2
Show file tree
Hide file tree
Showing 10 changed files with 1,032 additions and 16 deletions.
8 changes: 8 additions & 0 deletions .changeset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changesets

Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
with multi-package repos, or single-package repos to help you version and publish your code. You can
find the full documentation for it [in our repository](https://github.com/changesets/changesets)

We have a quick list of common questions to get you started engaging with this project in
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
11 changes: 11 additions & 0 deletions .changeset/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
"changelog": "@changesets/cli/changelog",
"commit": false,
"fixed": [],
"linked": [],
"access": "restricted",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": []
}
5 changes: 5 additions & 0 deletions .changeset/eleven-icons-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@proofgeist/fmdapi": minor
---

Custom functions to override where the temporary access token is stored
8 changes: 8 additions & 0 deletions .changeset/fast-months-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@proofgeist/fmdapi": major
---

Use native fetch (Node 18+).

This package now requires Node 18+ and no longer relys on the `node-fetch` package.
Each method supports passing additional options to the `fetch` function via the `fetch` parameter. This is useful if used within a framework that overrides the global `fetch` function (such as Next.js).
4 changes: 3 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ module.exports = {
sourceType: "module",
},
plugins: ["react", "@typescript-eslint"],
rules: {},
rules: {
"@typescript-eslint/no-var-requires": "off",
},
};
Empty file added CHANGELOG.md
Empty file.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ This change was made to take advantage of caching if used in a Next 13 app. You
client.list({ fetch: { next: { revalidate: 10 } } });
```

You can also now pass custom functions to override where the access token is stored when using username/password authentication. This can be used to improve performance if running in a serverless function or even the browser.

## FAQ

### I don't like the way the code is generated. Can I edit the generated files?
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@proofgeist/fmdapi",
"version": "3.0.0",
"version": "2.2.14",
"description": "FileMaker Data API client",
"main": "dist/index.js",
"repository": "[email protected]:proofgeist/fm-dapi.git",
Expand All @@ -13,9 +13,11 @@
"scripts": {
"prepublishOnly": "tsc",
"build": "tsc",
"test": "jest"
"test": "jest",
"changeset": "changeset"
},
"dependencies": {
"@changesets/cli": "^2.26.1",
"chalk": "4.1.2",
"commander": "^9.2.0",
"dayjs": "^1.11.2",
Expand Down
42 changes: 38 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from "zod";
import { getSharedData, setSharedData } from "./shared";
import type { getSharedData, setSharedData } from "./shared";
import {
CreateParams,
CreateResponse,
Expand Down Expand Up @@ -43,6 +43,12 @@ export type ClientObjectProps = {
* The layout to use by default for all requests. Can be overrridden on each request.
*/
layout?: string;
tokenStore?: {
getKey?: () => string;
getToken: (key: string) => string | null;
setToken: (key: string, value: string) => void;
clearToken: (key: string) => void;
};
};
const ZodOptions = z.object({
server: z
Expand All @@ -60,6 +66,14 @@ const ZodOptions = z.object({
}),
]),
layout: z.string().optional(),
tokenStore: z
.object({
getKey: z.function().args().returns(z.string()).optional(),
getToken: z.function().args(z.string()).returns(z.string().nullable()),
setToken: z.function().args(z.string(), z.string()).returns(z.void()),
clearToken: z.function().args(z.string()).returns(z.void()),
})
.optional(),
});

class FileMakerError extends Error {
Expand All @@ -83,7 +97,25 @@ function DataApi<
}
) {
const options = ZodOptions.strict().parse(input); // validate options
const sharedDataKey = `${options.server}/${options.db}`; // used for storing and re-using token

let tokenStore = options.tokenStore;

if (!tokenStore) {
const defaultStore: {
getSharedData: typeof getSharedData;
setSharedData: typeof setSharedData;
} = require("./shared.ts");

tokenStore = {
getToken: (key) => defaultStore.getSharedData(key),
setToken: (key, value) => defaultStore.setSharedData(key, value),
clearToken: () => null,
};
}

if (!tokenStore.getKey) {
tokenStore.getKey = () => `${options.server}/${options.db}`;
}

const baseUrl = new URL(
`${options.server}/fmi/data/vLatest/databases/${options.db}`
Expand All @@ -97,8 +129,10 @@ function DataApi<
fetchOptions?: Omit<RequestInit, "method">
): Promise<string> {
if ("apiKey" in options.auth) return options.auth.apiKey;
if (tokenStore === undefined) throw new Error("No token store provided");
if (!tokenStore.getKey) throw new Error("No token store key provided");

let token = getSharedData(sharedDataKey);
let token = tokenStore.getToken(tokenStore.getKey());

if (refresh) token = null; // clear token so are forced to get a new one

Expand Down Expand Up @@ -126,7 +160,7 @@ function DataApi<
if (!token) throw new Error("Could not get token");
}

setSharedData(sharedDataKey, token);
tokenStore.setToken(tokenStore.getKey(), token);
return token;
}

Expand Down
Loading

0 comments on commit 5c2f0d2

Please sign in to comment.