Skip to content

Commit

Permalink
Merge pull request #45 from FlatFilers/feat/add-version-to-api-url
Browse files Browse the repository at this point in the history
feat: add version to apiUrl
  • Loading branch information
carlbrugger authored Jun 4, 2024
2 parents f68eafc + cb15797 commit b88ed88
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/wrapper/FlatfileClient.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { CrossEnvConfig } from "@flatfile/cross-env-config";
import urlJoin from "url-join";
import { FlatfileClient as FernClient } from "../Client";
import * as environments from "../environments";
import * as core from "../core";
import urlJoin from "url-join";
import { CrossEnvConfig } from "@flatfile/cross-env-config";
import * as environments from "../environments";
import { Records } from "./RecordsClient";

CrossEnvConfig.alias("FLATFILE_API_URL", "AGENT_INTERNAL_URL");
Expand All @@ -24,7 +24,7 @@ export class FlatfileClient extends FernClient {

constructor(options: FlatfileClient.Options = {}) {
super({
environment: (options.environment || options.apiUrl) ?? environmentSupplier,
environment: resolveEnvironment(options),
token: options.token ?? tokenSupplier,
});
}
Expand All @@ -36,6 +36,13 @@ export class FlatfileClient extends FernClient {
}
}

const resolveEnvironment = (options: FlatfileClient.Options) => {
if (options.apiUrl && !options.apiUrl.endsWith("/v1")) {
return urlJoin(options.apiUrl, "v1");
}
return options.environment || options.apiUrl || environmentSupplier();
};

const environmentSupplier = () => {
const url = CrossEnvConfig.get("FLATFILE_API_URL");
if (!url) {
Expand All @@ -47,7 +54,7 @@ const environmentSupplier = () => {
const tokenSupplier = () => {
const token = CrossEnvConfig.get("FLATFILE_BEARER_TOKEN");
if (token == undefined) {
throw new Error("FLATFILE_BEARER_TOKEN is not undefined");
throw new Error("FLATFILE_BEARER_TOKEN is undefined");
}
return token;
};

0 comments on commit b88ed88

Please sign in to comment.