Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/remove-base-url.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@formrelay/core": patch
"@formrelay/vue": patch
"@formrelay/nuxt": patch
---

Remove `baseUrl` from public API. The FormRelay API URL is now hardcoded. Override via `FORMRELAY_API_URL` env var at build time for local development.

Nuxt module now supports both Nuxt 3 and Nuxt 4.
18 changes: 1 addition & 17 deletions packages/core/src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe("createForm", () => {
);
});

test("uses default baseUrl when not provided", async () => {
test("uses hardcoded API base URL", async () => {
const client = createMockHttpClient();
const form = createForm("01abc", {
publicKey: "pk_fr_test",
Expand All @@ -122,20 +122,4 @@ describe("createForm", () => {
expect.any(Object),
);
});

test("uses custom baseUrl when provided", async () => {
const client = createMockHttpClient();
const form = createForm("01abc", {
publicKey: "pk_fr_test",
baseUrl: "https://custom.api.com",
httpClient: client,
});

await form.getSchema();

expect(client.get).toHaveBeenCalledWith(
"https://custom.api.com/api/v1/form/01abc/schema",
expect.any(Object),
);
});
});
5 changes: 2 additions & 3 deletions packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import type { SubmitResult } from "./submit";
import { createFetchAdapter } from "./http/fetch";
import { createSchemaFetcher } from "./schema";
import { submitForm } from "./submit";
import { API_BASE_URL } from "./constants";

export interface FormClientOptions {
publicKey: string;
baseUrl?: string;
httpClient?: HttpAdapter;
}

Expand All @@ -17,9 +17,8 @@ export interface FormClient {
}

export function createForm(formId: string, options: FormClientOptions): FormClient {
const baseUrl = options.baseUrl ?? "https://formrelay.app";
const httpClient = options.httpClient ?? createFetchAdapter();
const fetchSchema = createSchemaFetcher(formId, baseUrl, options.publicKey, httpClient);
const fetchSchema = createSchemaFetcher(formId, API_BASE_URL, options.publicKey, httpClient);

let cachedSchema: FormSchema | null = null;

Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const API_BASE_URL: string =
(typeof process !== "undefined" && process.env?.FORMRELAY_API_URL) || "https://formrelay.app";
2 changes: 1 addition & 1 deletion packages/nuxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
"vite-plus": "latest"
},
"peerDependencies": {
"nuxt": "^3.0.0"
"nuxt": "^3.0.0 || ^4.0.0"
}
}
6 changes: 1 addition & 5 deletions packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@ import { defineNuxtModule, addImports, addComponent, createResolver } from "@nux
export interface ModuleOptions {
publicKey?: string;
secretKey?: string;
baseUrl?: string;
}

export default defineNuxtModule<ModuleOptions>({
meta: {
name: "@formrelay/nuxt",
configKey: "formrelay",
},
defaults: {
baseUrl: "https://formrelay.app",
},
defaults: {},
setup(options, nuxt) {
const resolver = createResolver(import.meta.url);

nuxt.options.runtimeConfig.public.formrelay = {
publicKey: options.publicKey ?? "",
baseUrl: options.baseUrl ?? "https://formrelay.app",
};

if (options.secretKey) {
Expand Down
7 changes: 1 addition & 6 deletions packages/nuxt/src/runtime/composables/useFormRelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,21 @@ export async function useFormRelay(options: Partial<UseFormRelayOptions> & { for
const runtimeConfig = useRuntimeConfig();
const config = runtimeConfig.public.formrelay as {
publicKey: string;
baseUrl: string;
};

const secretKey = (runtimeConfig as Record<string, unknown>).formrelaySecretKey as
| string
| undefined;

const publicKey = options.publicKey ?? config.publicKey;
const baseUrl = options.baseUrl ?? config.baseUrl;

// For SSR schema fetch: use secret key adapter if configured (server-only)
const schemaClient =
import.meta.server && secretKey
? createForm(options.formId, {
publicKey,
baseUrl,
httpClient: createSecretKeyAdapter(secretKey),
})
: createForm(options.formId, { publicKey, baseUrl });
: createForm(options.formId, { publicKey });

const { data: initialSchema } = await useAsyncData(`formrelay-schema-${options.formId}`, () =>
schemaClient.getSchema(),
Expand All @@ -63,7 +59,6 @@ export async function useFormRelay(options: Partial<UseFormRelayOptions> & { for
return useVueFormRelay({
formId: options.formId,
publicKey,
baseUrl,
initialSchema: initialSchema.value ?? undefined,
validate: options.validate,
onSuccess: options.onSuccess,
Expand Down
2 changes: 0 additions & 2 deletions packages/vue/src/components/FormRelay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,12 @@ describe("FormRelay", () => {
props: {
formId: "form-123",
publicKey: "pk_fr_abc",
baseUrl: "https://custom.api.com",
},
slots: { default: () => h("div") },
});

expect(createForm).toHaveBeenCalledWith("form-123", {
publicKey: "pk_fr_abc",
baseUrl: "https://custom.api.com",
});
});

Expand Down
1 change: 0 additions & 1 deletion packages/vue/src/components/FormRelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export default defineComponent({
props: {
formId: { type: String, required: true },
publicKey: { type: String, required: true },
baseUrl: { type: String, default: undefined },
validate: { type: Function, default: undefined },
onSuccess: { type: Function, default: undefined },
onError: { type: Function, default: undefined },
Expand Down
15 changes: 1 addition & 14 deletions packages/vue/src/composables/useFormRelay.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, test, vi, beforeEach } from "vitest";
import { nextTick, isRef, isReactive } from "vue";
import { createForm, ValidationError } from "@formrelay/core";
import { ValidationError } from "@formrelay/core";
import { useFormRelay } from "./useFormRelay";

const mockFields = [
Expand Down Expand Up @@ -306,19 +306,6 @@ describe("useFormRelay", () => {
expect(mockSubmit).not.toHaveBeenCalled();
});

test("passes baseUrl to createForm", () => {
useFormRelay({
formId: "01abc",
publicKey: "pk_fr_test",
baseUrl: "https://custom.api.com",
});

expect(createForm).toHaveBeenCalledWith("01abc", {
publicKey: "pk_fr_test",
baseUrl: "https://custom.api.com",
});
});

test("canSubmit is false while bot protection token is missing", () => {
const { canSubmit, setBotToken } = useFormRelay({
formId: "01abc",
Expand Down
1 change: 0 additions & 1 deletion packages/vue/src/composables/useFormRelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import type { UseFormRelayOptions, UseFormRelayReturn } from "../types";
export function useFormRelay(options: UseFormRelayOptions): UseFormRelayReturn {
const client = createForm(options.formId, {
publicKey: options.publicKey,
baseUrl: options.baseUrl,
});

const schema = ref<FormSchema | null>(null);
Expand Down
1 change: 0 additions & 1 deletion packages/vue/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type { ComputedRef, Ref } from "vue";
export interface UseFormRelayOptions {
formId: string;
publicKey: string;
baseUrl?: string;
initialSchema?: FormSchema;
validate?: (data: Record<string, unknown>, schema: JsonSchema) => Record<string, string[]>;
onSuccess?: (result: { message: string }) => void;
Expand Down
Loading