Skip to content

Commit

Permalink
feat(user-api): #89 add support for new register options
Browse files Browse the repository at this point in the history
  • Loading branch information
chfoidl committed Jun 25, 2024
1 parent c073b9c commit e268594
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-bikes-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@drupal-kit/user-api": minor
---

Support latest `user_api` registration options.
23 changes: 21 additions & 2 deletions packages/user-api/src/DrupalkitUserApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
Expand Down Expand Up @@ -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<Result<RegisterResponse, DrupalkitError>> => {
const url = drupalkit.buildUrl(registrationEndpoint);

const localHeaders: Record<string, string> = { ...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<RegisterResponse>(
url,
{
method: "POST",
body: payload,
headers,
headers: localHeaders,
},
requestOptions,
);
Expand Down
8 changes: 8 additions & 0 deletions packages/user-api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
74 changes: 73 additions & 1 deletion packages/user-api/tests/DrupalkitUserApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,86 @@ 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",
},
});
});

test.serial("Register with email notification disabled", async (t) => {
t.plan(1);

const drupalkit = createDrupalkit();

const payload = {
name: { value: "john-doe-1" },
mail: { value: "[email protected]" },
};

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: "[email protected]" },
};

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: "[email protected]" },
};

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,
Expand Down

0 comments on commit e268594

Please sign in to comment.