Skip to content

Commit

Permalink
refactor(user-api): rename methods to reflect rest resource name; dep…
Browse files Browse the repository at this point in the history
…recate old methods
  • Loading branch information
chfoidl committed Jan 18, 2024
1 parent 4cd6be2 commit 9aa506c
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 115 deletions.
4 changes: 3 additions & 1 deletion packages/user-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
"types": "./dist/index.d.ts",
"dependencies": {
"@drupal-kit/core": "workspace:0.9.2",
"@wunderwerk/ts-functional": "1.0.0-beta.2"
"@wunderwerk/ts-functional": "1.0.0-beta.2",
"util-deprecate": "^1.0.2"
},
"devDependencies": {
"@drupal-kit/config-typescript": "workspace:0.9.2",
"@drupal-kit/eslint-config": "workspace:0.9.2",
"@drupal-kit/types": "workspace:0.9.2",
"@rollup/plugin-typescript": "^11.1.1",
"@swc/core": "^1.3.58",
"@types/util-deprecate": "^1.0.3",
"ava": "^5.2.0",
"esbuild": "^0.17.19",
"msw": "^1.2.1",
Expand Down
144 changes: 90 additions & 54 deletions packages/user-api/src/DrupalkitUserApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Drupalkit, DrupalkitError, DrupalkitOptions } from "@drupal-kit/core";
import { OverrideableRequestOptions } from "@drupal-kit/types";

import { RegisterPayload, RegisterResponse, SuccessResponse } from "./types.js";
import { deprecate } from "./utils.js";

/**
* DrupalkitUserApi plugin for Drupalkit.
Expand All @@ -17,22 +18,28 @@ export const DrupalkitUserApi = (
drupalkitOptions: DrupalkitOptions,
) => {
if (drupalkitOptions.userApiResendMailEndpoint) {
drupalkitOptions.userApiRegisterResendEmailEndpoint = drupalkitOptions.userApiResendMailEndpoint;
drupalkitOptions.userApiRegisterResendEmailEndpoint =
drupalkitOptions.userApiResendMailEndpoint;
}
if (drupalkitOptions.userApiInitAccountCancelEndpoint) {
drupalkitOptions.userApiInitCancelAccountEndpoint = drupalkitOptions.userApiInitAccountCancelEndpoint;
drupalkitOptions.userApiInitCancelAccountEndpoint =
drupalkitOptions.userApiInitAccountCancelEndpoint;
}
if (drupalkitOptions.userApiResetPasswordEndpoint) {
drupalkitOptions.userApiInitSetPasswordEndpoint = drupalkitOptions.userApiResetPasswordEndpoint;
drupalkitOptions.userApiInitSetPasswordEndpoint =
drupalkitOptions.userApiResetPasswordEndpoint;
}
if (drupalkitOptions.userApiUpdatePasswordEndpoint) {
drupalkitOptions.userApiSetPasswordEndpoint = drupalkitOptions.userApiUpdatePasswordEndpoint;
drupalkitOptions.userApiSetPasswordEndpoint =
drupalkitOptions.userApiUpdatePasswordEndpoint;
}
if (drupalkitOptions.userApiVerifyEmailEndpoint) {
drupalkitOptions.userApiInitSetEmailEndpoint = drupalkitOptions.userApiVerifyEmailEndpoint;
drupalkitOptions.userApiInitSetEmailEndpoint =
drupalkitOptions.userApiVerifyEmailEndpoint;
}
if (drupalkitOptions.userApiUpdateEmailEndpoint) {
drupalkitOptions.userApiSetEmailEndpoint = drupalkitOptions.userApiUpdateEmailEndpoint;
drupalkitOptions.userApiSetEmailEndpoint =
drupalkitOptions.userApiUpdateEmailEndpoint;
}

const registrationEndpoint =
Expand Down Expand Up @@ -99,6 +106,41 @@ export const DrupalkitUserApi = (
return Result.Ok(result.val.data);
};

/**
* Resend verification email.
*
* Resend the verification email for the given operation.
*
* @param email - E-Mail address to resend to.
* @param operation - Operation of which to resend email.
* @param requestOptions - Optional request options.
*/
const resendRegisterEmail = async (
email: string,
operation: string,
requestOptions?: OverrideableRequestOptions,
) => {
const url = drupalkit.buildUrl(registerResendEmailEndpoint);

const result = await drupalkit.request<{ status: "success" }>(
url,
{
method: "POST",
body: { email, operation },
headers: {
"content-type": "application/json",
},
},
requestOptions,
);

if (result.err) {
return result;
}

return Result.Ok(result.val.data);
};

/**
* Initialize account cancellation.
*
Expand All @@ -109,7 +151,7 @@ export const DrupalkitUserApi = (
*
* @param requestOptions - Optional request options.
*/
const initAccountCancel = async (
const initCancelAccount = async (
requestOptions?: OverrideableRequestOptions,
): Promise<Result<SuccessResponse, DrupalkitError>> => {
const url = drupalkit.buildUrl(initCancelAccountEndpoint);
Expand Down Expand Up @@ -163,7 +205,7 @@ export const DrupalkitUserApi = (
};

/**
* Initialize password reset.
* Initialize set password.
*
* The request MUST be authorized!
*
Expand All @@ -173,7 +215,7 @@ export const DrupalkitUserApi = (
* @param email - E-Mail address of the user.
* @param requestOptions - Optional request options.
*/
const resetPassword = async (
const initSetPassword = async (
email: string,
requestOptions?: OverrideableRequestOptions,
): Promise<Result<SuccessResponse, DrupalkitError>> => {
Expand Down Expand Up @@ -206,7 +248,7 @@ export const DrupalkitUserApi = (
* @param currentPassword - The current password for the user.
* @param requestOptions - Optional request options.
*/
const updatePassword = async (
const setPassword = async (
newPassword: string,
currentPassword?: string,
requestOptions?: OverrideableRequestOptions,
Expand Down Expand Up @@ -273,7 +315,7 @@ export const DrupalkitUserApi = (
};

/**
* Verify new email change.
* Initialize email update.
*
* The request MUST be authorized!
*
Expand All @@ -283,7 +325,7 @@ export const DrupalkitUserApi = (
* @param email - New E-Mail address of the user.
* @param requestOptions - Optional request options.
*/
const verifyEmail = async (
const initSetEmail = async (
email: string,
requestOptions?: OverrideableRequestOptions,
): Promise<Result<SuccessResponse, DrupalkitError>> => {
Expand Down Expand Up @@ -315,7 +357,7 @@ export const DrupalkitUserApi = (
* @param email - New E-Mail address of the user.
* @param requestOptions - Optional request options.
*/
const updateEmail = async (
const setEmail = async (
email: string,
requestOptions?: OverrideableRequestOptions,
): Promise<Result<SuccessResponse, DrupalkitError>> => {
Expand All @@ -338,55 +380,49 @@ export const DrupalkitUserApi = (
return Result.Ok(result.val.data);
};

/**
* Resend verification email.
*
* Resend the verification email for the given operation.
*
* @param email - E-Mail address to resend to.
* @param operation - Operation of which to resend email.
* @param requestOptions - Optional request options.
*/
const resendVerificationEmail = async (
email: string,
operation: string,
requestOptions?: OverrideableRequestOptions,
) => {
const url = drupalkit.buildUrl(registerResendEmailEndpoint);

const result = await drupalkit.request<{ status: "success" }>(
url,
{
method: "POST",
body: { email, operation },
headers: {
"content-type": "application/json",
},
},
requestOptions,
);

if (result.err) {
return result;
}

return Result.Ok(result.val.data);
};

/**
* Extend the Drupalkit instance.
*/
return {
userApi: {
register,
initAccountCancel,
resendRegisterEmail,
initCancelAccount,
cancelAccount,
resetPassword,
updatePassword,
initSetPassword,
setPassword,
passwordlessLogin,
verifyEmail,
updateEmail,
resendVerificationEmail,
initSetEmail,
setEmail,
/* eslint-disable */
/**
* @deprecated Deprecated in `0.9.3` will be removed in `1.0.0`. Use `resendRegisterEmail` instead.
*/
resendVerificationEmail: deprecate(
resendRegisterEmail,
"resendVerificationEmail",
),
/**
* @deprecated Deprecated in `0.9.3` will be removed in `1.0.0`. Use `initCancelAccount` instead.
*/
initAccountCancel: deprecate(initCancelAccount, "initAccountCancel"),
/**
* @deprecated Deprecated in `0.9.3` will be removed in `1.0.0`. Use `initSetPassword` instead.
*/
resetPassword: deprecate(initSetPassword, "resetPassword"),
/**
* @deprecated Deprecated in `0.9.3` will be removed in `1.0.0`. Use `setPassword` instead.
*/
updatePassword: deprecate(setPassword, "updatePassword"),
/**
* @deprecated Deprecated in `0.9.3` will be removed in `1.0.0`. Use `initSetEmail` instead.
*/
verifyEmail: deprecate(initSetEmail, "verifyEmail"),
/**
* @deprecated Deprecated in `0.9.3` will be removed in `1.0.0`. Use `setEmail` instead.
*/
updateEmail: deprecate(setEmail, "updateEmail"),
/* eslint-enable */
},
};
};
2 changes: 1 addition & 1 deletion packages/user-api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ declare module "@drupal-kit/core" {

//
// Deprecated properties.
//
//

/**
* Endpoint path for the User API ResendRegisterEmailResource.
Expand Down
17 changes: 17 additions & 0 deletions packages/user-api/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import coreDeprecate from "util-deprecate";

/**
* Wrapper arround the util-deprecate deprecate function.
*
* Generates the deprecation proper message.
*
* @param fn - The function to deprecate.
* @param deprecatedFuncName - The name of the deprecated function.
*/
// eslint-disable-next-line
export function deprecate(fn: Function, deprecatedFuncName: string) {
return coreDeprecate(
fn,
`drupalkit.userApi.${deprecatedFuncName}() is deprecated, use drupalkit.userApi.${fn.name}() instead.`,
);
}
Loading

0 comments on commit 9aa506c

Please sign in to comment.