diff --git a/.changeset/friendly-bikes-listen.md b/.changeset/friendly-bikes-listen.md new file mode 100644 index 0000000..ad9210b --- /dev/null +++ b/.changeset/friendly-bikes-listen.md @@ -0,0 +1,5 @@ +--- +"@drupal-kit/user-api": minor +--- + +Support latest `user_api` registration options. diff --git a/packages/user-api/src/DrupalkitUserApi.ts b/packages/user-api/src/DrupalkitUserApi.ts index b8b48ff..0e3b175 100644 --- a/packages/user-api/src/DrupalkitUserApi.ts +++ b/packages/user-api/src/DrupalkitUserApi.ts @@ -2,7 +2,12 @@ import { Result } from "@wunderwerk/ts-functional/results"; import { Drupalkit, DrupalkitError, DrupalkitOptions } from "@drupal-kit/core"; import { OverrideableRequestOptions } from "@drupal-kit/types"; -import { RegisterPayload, RegisterResponse, SuccessResponse } from "./types.js"; +import { + RegisterOptions, + RegisterPayload, + RegisterResponse, + SuccessResponse, +} from "./types.js"; import { deprecate } from "./utils.js"; /** @@ -89,20 +94,34 @@ export const DrupalkitUserApi = ( * interfaces to suit your needs! * * @param payload - The registration payload. + * @param options - Registration options. * @param requestOptions - Optional request options. */ const register = async ( payload: RegisterPayload, + options?: RegisterOptions, requestOptions?: OverrideableRequestOptions, ): Promise> => { const url = drupalkit.buildUrl(registrationEndpoint); + const localHeaders: Record = { ...headers }; + + // Handle options. + if (options?.disableEmailNotification) { + localHeaders["X-Disable-Email-Notification"] = "1"; + } + + if (options?.disableAccountActivation) { + localHeaders["X-Disable-Account-Activation"] = "1"; + } + + // Make request. const result = await drupalkit.request( url, { method: "POST", body: payload, - headers, + headers: localHeaders, }, requestOptions, ); diff --git a/packages/user-api/src/types.ts b/packages/user-api/src/types.ts index 6dd367c..e5bcccd 100644 --- a/packages/user-api/src/types.ts +++ b/packages/user-api/src/types.ts @@ -121,6 +121,14 @@ export interface RegisterPayload { mail: EntityField; } +/** + * Register options. + */ +export interface RegisterOptions { + disableEmailNotification?: boolean; + disableAccountActivation?: boolean; +} + /** * Augment this interface to include additional * fields for the user entity. diff --git a/packages/user-api/tests/DrupalkitUserApi.test.ts b/packages/user-api/tests/DrupalkitUserApi.test.ts index 72b0dec..4762743 100644 --- a/packages/user-api/tests/DrupalkitUserApi.test.ts +++ b/packages/user-api/tests/DrupalkitUserApi.test.ts @@ -91,7 +91,7 @@ test.serial("Register with custom request options", async (t) => { }), ); - await drupalkit.userApi.register(payload, { + await drupalkit.userApi.register(payload, undefined, { cache: "no-cache", headers: { "X-Custom": "1", @@ -99,6 +99,78 @@ test.serial("Register with custom request options", async (t) => { }); }); +test.serial("Register with email notification disabled", async (t) => { + t.plan(1); + + const drupalkit = createDrupalkit(); + + const payload = { + name: { value: "john-doe-1" }, + mail: { value: "JzWZg@example.com" }, + }; + + server.use( + http.post("*/user-api/register", async ({ request }) => { + t.is(request.headers.get("X-Disable-Email-Notification"), "1"); + + return HttpResponse.json(UserResponse); + }), + ); + + await drupalkit.userApi.register(payload, { + disableEmailNotification: true, + }); +}); + +test.serial("Register with account activation disabled", async (t) => { + t.plan(1); + + const drupalkit = createDrupalkit(); + + const payload = { + name: { value: "john-doe-1" }, + mail: { value: "JzWZg@example.com" }, + }; + + server.use( + http.post("*/user-api/register", async ({ request }) => { + t.is(request.headers.get("X-Disable-Account-Activation"), "1"); + + return HttpResponse.json(UserResponse); + }), + ); + + await drupalkit.userApi.register(payload, { + disableEmailNotification: false, + disableAccountActivation: true, + }); +}); + +test.serial("Register with all options", async (t) => { + t.plan(2); + + const drupalkit = createDrupalkit(); + + const payload = { + name: { value: "john-doe-1" }, + mail: { value: "JzWZg@example.com" }, + }; + + server.use( + http.post("*/user-api/register", async ({ request }) => { + t.is(request.headers.get("X-Disable-Email-Notification"), "1"); + t.is(request.headers.get("X-Disable-Account-Activation"), "1"); + + return HttpResponse.json(UserResponse); + }), + ); + + await drupalkit.userApi.register(payload, { + disableEmailNotification: true, + disableAccountActivation: true, + }); +}); + test.serial("Register with custom endpoint", async (t) => { const drupalkit = createDrupalkit({ baseUrl: BASE_URL,