From 9aa506c7a0d0a6c83a12073e0f40104db52c78ea Mon Sep 17 00:00:00 2001 From: Christian Foidl Date: Thu, 18 Jan 2024 12:15:58 +0100 Subject: [PATCH] refactor(user-api): rename methods to reflect rest resource name; deprecate old methods --- packages/user-api/package.json | 4 +- packages/user-api/src/DrupalkitUserApi.ts | 144 +++++++++++------- packages/user-api/src/types.ts | 2 +- packages/user-api/src/utils.ts | 17 +++ .../user-api/tests/DrupalkitUserApi.test.ts | 103 ++++++------- pnpm-lock.yaml | 11 +- 6 files changed, 166 insertions(+), 115 deletions(-) create mode 100644 packages/user-api/src/utils.ts diff --git a/packages/user-api/package.json b/packages/user-api/package.json index 4b47d55..f280cc7 100644 --- a/packages/user-api/package.json +++ b/packages/user-api/package.json @@ -5,7 +5,8 @@ "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", @@ -13,6 +14,7 @@ "@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", diff --git a/packages/user-api/src/DrupalkitUserApi.ts b/packages/user-api/src/DrupalkitUserApi.ts index 63b6518..071d454 100644 --- a/packages/user-api/src/DrupalkitUserApi.ts +++ b/packages/user-api/src/DrupalkitUserApi.ts @@ -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. @@ -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 = @@ -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. * @@ -109,7 +151,7 @@ export const DrupalkitUserApi = ( * * @param requestOptions - Optional request options. */ - const initAccountCancel = async ( + const initCancelAccount = async ( requestOptions?: OverrideableRequestOptions, ): Promise> => { const url = drupalkit.buildUrl(initCancelAccountEndpoint); @@ -163,7 +205,7 @@ export const DrupalkitUserApi = ( }; /** - * Initialize password reset. + * Initialize set password. * * The request MUST be authorized! * @@ -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> => { @@ -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, @@ -273,7 +315,7 @@ export const DrupalkitUserApi = ( }; /** - * Verify new email change. + * Initialize email update. * * The request MUST be authorized! * @@ -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> => { @@ -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> => { @@ -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 */ }, }; }; diff --git a/packages/user-api/src/types.ts b/packages/user-api/src/types.ts index 963f021..ee233b2 100644 --- a/packages/user-api/src/types.ts +++ b/packages/user-api/src/types.ts @@ -60,7 +60,7 @@ declare module "@drupal-kit/core" { // // Deprecated properties. - // + // /** * Endpoint path for the User API ResendRegisterEmailResource. diff --git a/packages/user-api/src/utils.ts b/packages/user-api/src/utils.ts new file mode 100644 index 0000000..db33658 --- /dev/null +++ b/packages/user-api/src/utils.ts @@ -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.`, + ); +} diff --git a/packages/user-api/tests/DrupalkitUserApi.test.ts b/packages/user-api/tests/DrupalkitUserApi.test.ts index 0fac416..692ab94 100644 --- a/packages/user-api/tests/DrupalkitUserApi.test.ts +++ b/packages/user-api/tests/DrupalkitUserApi.test.ts @@ -140,7 +140,6 @@ test.serial("Handle register error", async (t) => { t.assert(result.err); }); - /** * Resend register email. */ @@ -162,45 +161,39 @@ test.serial("Resend register email", async (t) => { }), ); - const result = await drupalkit.userApi.resendVerificationEmail( - email, - operation, - ); + const result = await drupalkit.userApi.resendRegisterEmail(email, operation); const res = result.unwrap(); t.deepEqual(res, successResponse); }); -test.serial( - "Resend register email with custom request options", - async (t) => { - t.plan(2); +test.serial("Resend register email with custom request options", async (t) => { + t.plan(2); - const drupalkit = createDrupalkit(); - const email = "JzWZg@example.com"; - const operation = "register"; + const drupalkit = createDrupalkit(); + const email = "JzWZg@example.com"; + const operation = "register"; - drupalkit.hook.before("request", (options) => { - t.is(options.cache, "no-cache"); - }); + drupalkit.hook.before("request", (options) => { + t.is(options.cache, "no-cache"); + }); - server.use( - rest.post("*/user-api/register/resend-email", async (req, res, ctx) => { - t.is(req.headers.get("X-Custom"), "1"); + server.use( + rest.post("*/user-api/register/resend-email", async (req, res, ctx) => { + t.is(req.headers.get("X-Custom"), "1"); - return res(ctx.json(successResponse)); - }), - ); + return res(ctx.json(successResponse)); + }), + ); - await drupalkit.userApi.resendVerificationEmail(email, operation, { - cache: "no-cache", - headers: { - "X-Custom": "1", - }, - }); - }, -); + await drupalkit.userApi.resendRegisterEmail(email, operation, { + cache: "no-cache", + headers: { + "X-Custom": "1", + }, + }); +}); test.serial("Resend register email with custom endpoint", async (t) => { const drupalkit = createDrupalkit({ @@ -216,10 +209,7 @@ test.serial("Resend register email with custom endpoint", async (t) => { ), ); - const result = await drupalkit.userApi.resendVerificationEmail( - email, - operation, - ); + const result = await drupalkit.userApi.resendRegisterEmail(email, operation); t.assert(result.ok); }); @@ -257,16 +247,13 @@ test.serial("Handle error while resend register email", async (t) => { ), ); - const result = await drupalkit.userApi.resendVerificationEmail( - email, - operation, - ); + const result = await drupalkit.userApi.resendRegisterEmail(email, operation); t.assert(result.err); }); /** - * initAccountCancel(). + * initCancelAccount(). */ test.serial("Init cancel account", async (t) => { @@ -282,7 +269,7 @@ test.serial("Init cancel account", async (t) => { }), ); - const result = await drupalkit.userApi.initAccountCancel(); + const result = await drupalkit.userApi.initCancelAccount(); const res = result.unwrap(); @@ -306,7 +293,7 @@ test.serial("Init cancel account with custom request options", async (t) => { }), ); - await drupalkit.userApi.initAccountCancel({ + await drupalkit.userApi.initCancelAccount({ cache: "no-cache", headers: { "X-Custom": "1", @@ -326,7 +313,7 @@ test.serial("Init cancel account with custom endpoint", async (t) => { ), ); - const result = await drupalkit.userApi.initAccountCancel(); + const result = await drupalkit.userApi.initCancelAccount(); t.assert(result.ok); }); @@ -357,7 +344,7 @@ test.serial("Handle error while init cancel account", async (t) => { ), ); - const result = await drupalkit.userApi.initAccountCancel(); + const result = await drupalkit.userApi.initCancelAccount(); t.assert(result.err); }); @@ -462,7 +449,7 @@ test.serial("Init set password", async (t) => { }), ); - const result = await drupalkit.userApi.resetPassword(email); + const result = await drupalkit.userApi.initSetPassword(email); const res = result.unwrap(); @@ -487,7 +474,7 @@ test.serial("Init set password with custom request options", async (t) => { }), ); - await drupalkit.userApi.resetPassword(email, { + await drupalkit.userApi.initSetPassword(email, { cache: "no-cache", headers: { "X-Custom": "1", @@ -508,7 +495,7 @@ test.serial("Init set password with custom endpoint", async (t) => { ), ); - const result = await drupalkit.userApi.resetPassword(email); + const result = await drupalkit.userApi.initSetPassword(email); t.assert(result.ok); }); @@ -541,7 +528,7 @@ test.serial("Handle error while init set password", async (t) => { ), ); - const result = await drupalkit.userApi.resetPassword(email); + const result = await drupalkit.userApi.initSetPassword(email); t.assert(result.err); }); @@ -566,7 +553,7 @@ test.serial("Set password", async (t) => { }), ); - const result = await drupalkit.userApi.updatePassword(newPassword); + const result = await drupalkit.userApi.setPassword(newPassword); const res = result.unwrap(); @@ -591,7 +578,7 @@ test.serial("Set password with custom request options", async (t) => { }), ); - await drupalkit.userApi.updatePassword(newPassword, undefined, { + await drupalkit.userApi.setPassword(newPassword, undefined, { cache: "no-cache", headers: { "X-Custom": "1", @@ -612,7 +599,7 @@ test.serial("Set password with custom endpoint", async (t) => { ), ); - const result = await drupalkit.userApi.updatePassword(newPassword); + const result = await drupalkit.userApi.setPassword(newPassword); t.assert(result.ok); }); @@ -645,7 +632,7 @@ test.serial("Handle error while set password", async (t) => { ), ); - const result = await drupalkit.userApi.updatePassword(newPassword); + const result = await drupalkit.userApi.setPassword(newPassword); t.assert(result.err); }); @@ -756,7 +743,7 @@ test.serial("Init set email", async (t) => { }), ); - const result = await drupalkit.userApi.verifyEmail(email); + const result = await drupalkit.userApi.initSetEmail(email); const res = result.unwrap(); @@ -781,7 +768,7 @@ test.serial("Init set email with custom request options", async (t) => { }), ); - await drupalkit.userApi.verifyEmail(email, { + await drupalkit.userApi.initSetEmail(email, { cache: "no-cache", headers: { "X-Custom": "1", @@ -802,7 +789,7 @@ test.serial("Init set email with custom endpoint", async (t) => { ), ); - const result = await drupalkit.userApi.verifyEmail(email); + const result = await drupalkit.userApi.initSetEmail(email); t.assert(result.ok); }); @@ -835,7 +822,7 @@ test.serial("Handle error while init set email", async (t) => { ), ); - const result = await drupalkit.userApi.verifyEmail(email); + const result = await drupalkit.userApi.initSetEmail(email); t.assert(result.err); }); @@ -860,7 +847,7 @@ test.serial("Set email", async (t) => { }), ); - const result = await drupalkit.userApi.updateEmail(email); + const result = await drupalkit.userApi.setEmail(email); const res = result.unwrap(); @@ -885,7 +872,7 @@ test.serial("Set email with custom request options", async (t) => { }), ); - await drupalkit.userApi.updateEmail(email, { + await drupalkit.userApi.setEmail(email, { cache: "no-cache", headers: { "X-Custom": "1", @@ -906,7 +893,7 @@ test.serial("Set email with custom endpoint", async (t) => { ), ); - const result = await drupalkit.userApi.updateEmail(email); + const result = await drupalkit.userApi.setEmail(email); t.assert(result.ok); }); @@ -939,7 +926,7 @@ test.serial("Handle error while set email", async (t) => { ), ); - const result = await drupalkit.userApi.updateEmail(email); + const result = await drupalkit.userApi.setEmail(email); t.assert(result.err); }); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index eaa8aaa..46f71b8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -363,6 +363,9 @@ importers: '@wunderwerk/ts-functional': specifier: 1.0.0-beta.2 version: 1.0.0-beta.2 + util-deprecate: + specifier: ^1.0.2 + version: 1.0.2 devDependencies: '@drupal-kit/config-typescript': specifier: workspace:0.9.2 @@ -379,6 +382,9 @@ importers: '@swc/core': specifier: ^1.3.58 version: 1.3.58 + '@types/util-deprecate': + specifier: ^1.0.3 + version: 1.0.3 ava: specifier: ^5.2.0 version: 5.2.0 @@ -1674,6 +1680,10 @@ packages: '@types/node': 20.1.7 dev: true + /@types/util-deprecate@1.0.3: + resolution: {integrity: sha512-tS4Vw2qY1XJnS+ZL4wgzyJprahEFBgg72NlC8lyY7/RWmS9cqz5gnxta9Mv9/Zla/wXBDrcNpiLLMv/4+5hokw==} + dev: true + /@typescript-eslint/eslint-plugin@5.59.6(@typescript-eslint/parser@5.59.6)(eslint@8.40.0)(typescript@5.0.4): resolution: {integrity: sha512-sXtOgJNEuRU5RLwPUb1jxtToZbgvq3M6FPpY4QENxoOggK+UpTxUBpj6tD8+Qh2g46Pi9We87E+eHnUw8YcGsw==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} @@ -5679,7 +5689,6 @@ packages: /util-deprecate@1.0.2: resolution: {integrity: sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==} - dev: true /util@0.12.5: resolution: {integrity: sha512-kZf/K6hEIrWHI6XqOFUiiMa+79wE/D8Q+NCNAWclkyg3b4d2k7s0QGepNjiABc+aR3N1PAyHL7p6UcLY6LmrnA==}