Skip to content

Commit

Permalink
refactor(core): rename prefix to account
Browse files Browse the repository at this point in the history
  • Loading branch information
wangsijie committed Nov 15, 2024
1 parent 05f7677 commit 129e714
Show file tree
Hide file tree
Showing 16 changed files with 83 additions and 80 deletions.
1 change: 1 addition & 0 deletions packages/core/src/routes/account/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const accountApiPrefix = '/account';
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { buildVerificationRecordByIdAndType } from '../../libraries/verification
import assertThat from '../../utils/assert-that.js';
import type { UserRouter, RouterInitArgs } from '../types.js';

import { accountApiPrefix } from './constants.js';

export default function emailAndPhoneRoutes<T extends UserRouter>(...args: RouterInitArgs<T>) {
const [router, { queries, libraries }] = args;
const {
Expand All @@ -21,7 +23,7 @@ export default function emailAndPhoneRoutes<T extends UserRouter>(...args: Route
} = libraries;

router.post(
'/profile/primary-email',
`${accountApiPrefix}/primary-email`,
koaGuard({
body: z.object({
email: z.string().regex(emailRegEx),
Expand Down Expand Up @@ -67,7 +69,7 @@ export default function emailAndPhoneRoutes<T extends UserRouter>(...args: Route
);

router.delete(
'/profile/primary-email',
`${accountApiPrefix}/primary-email`,
koaGuard({
status: [204, 400, 401],
}),
Expand Down Expand Up @@ -106,7 +108,7 @@ export default function emailAndPhoneRoutes<T extends UserRouter>(...args: Route
);

router.post(
'/profile/primary-phone',
`${accountApiPrefix}/primary-phone`,
koaGuard({
body: z.object({
phone: z.string().regex(phoneRegEx),
Expand Down Expand Up @@ -152,7 +154,7 @@ export default function emailAndPhoneRoutes<T extends UserRouter>(...args: Route
);

router.delete(
'/profile/primary-phone',
`${accountApiPrefix}/primary-phone`,
koaGuard({
status: [204, 400, 401],
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { buildVerificationRecordByIdAndType } from '../../libraries/verification
import assertThat from '../../utils/assert-that.js';
import type { UserRouter, RouterInitArgs } from '../types.js';

import { accountApiPrefix } from './constants.js';

export default function identitiesRoutes<T extends UserRouter>(
...[router, { queries, libraries }]: RouterInitArgs<T>
) {
Expand All @@ -21,7 +23,7 @@ export default function identitiesRoutes<T extends UserRouter>(
} = libraries;

router.post(
'/profile/identities',
`${accountApiPrefix}/identities`,
koaGuard({
body: z.object({
newIdentifierVerificationRecordId: z.string(),
Expand Down Expand Up @@ -81,7 +83,7 @@ export default function identitiesRoutes<T extends UserRouter>(
);

router.delete(
'/profile/identities/:target',
`${accountApiPrefix}/identities/:target`,
koaGuard({
params: z.object({ target: z.string() }),
status: [204, 400, 401, 404],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"tags": [
{
"name": "Profile",
"description": "Profile routes provide functionality for managing user profiles for the end user to interact directly with access tokens."
"name": "Account",
"description": "Account routes provide functionality for managing user profile for the end user to interact directly with access tokens."
},
{
"name": "Dev feature"
}
],
"paths": {
"/api/profile": {
"/api/account": {
"get": {
"operationId": "GetProfile",
"summary": "Get profile",
Expand Down Expand Up @@ -56,7 +56,7 @@
}
}
},
"/api/profile/profile": {
"/api/account/account": {
"patch": {
"operationId": "UpdateOtherProfile",
"summary": "Update other profile",
Expand Down Expand Up @@ -114,7 +114,7 @@
}
}
},
"/api/profile/password": {
"/api/account/password": {
"post": {
"operationId": "UpdatePassword",
"summary": "Update password",
Expand Down Expand Up @@ -142,7 +142,7 @@
}
}
},
"/api/profile/primary-email": {
"/api/account/primary-email": {
"post": {
"operationId": "UpdatePrimaryEmail",
"summary": "Update primary email",
Expand Down Expand Up @@ -186,7 +186,7 @@
}
}
},
"/api/profile/primary-phone": {
"/api/account/primary-phone": {
"post": {
"operationId": "UpdatePrimaryPhone",
"summary": "Update primary phone",
Expand Down Expand Up @@ -230,7 +230,7 @@
}
}
},
"/api/profile/identities": {
"/api/account/identities": {
"post": {
"operationId": "AddUserIdentities",
"summary": "Add a user identity",
Expand All @@ -255,7 +255,7 @@
}
}
},
"/api/profile/identities/{target}": {
"/api/account/identities/{target}": {
"delete": {
"operationId": "DeleteIdentity",
"summary": "Delete a user identity",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ import assertThat from '../../utils/assert-that.js';
import { PasswordValidator } from '../experience/classes/libraries/password-validator.js';
import type { UserRouter, RouterInitArgs } from '../types.js';

import { accountApiPrefix } from './constants.js';
import emailAndPhoneRoutes from './email-and-phone.js';
import identitiesRoutes from './identities.js';
import koaAccountCenter from './middlewares/koa-account-center.js';
import { getAccountCenterFilteredProfile, getScopedProfile } from './utils/get-scoped-profile.js';

export default function profileRoutes<T extends UserRouter>(...args: RouterInitArgs<T>) {
export default function accountRoutes<T extends UserRouter>(...args: RouterInitArgs<T>) {
const [router, { queries, libraries }] = args;
const {
users: { updateUserById, findUserById },
Expand All @@ -39,7 +40,7 @@ export default function profileRoutes<T extends UserRouter>(...args: RouterInitA
}

router.get(
'/profile',
`${accountApiPrefix}`,
koaGuard({
response: userProfileResponseGuard.partial(),
status: [200],
Expand All @@ -53,7 +54,7 @@ export default function profileRoutes<T extends UserRouter>(...args: RouterInitA
);

router.patch(
'/profile',
`${accountApiPrefix}`,
koaGuard({
body: z.object({
name: z.string().nullable().optional(),
Expand Down Expand Up @@ -111,7 +112,7 @@ export default function profileRoutes<T extends UserRouter>(...args: RouterInitA
);

router.patch(
'/profile/profile',
`${accountApiPrefix}/profile`,
koaGuard({
body: userProfileGuard,
response: userProfileGuard,
Expand Down Expand Up @@ -146,7 +147,7 @@ export default function profileRoutes<T extends UserRouter>(...args: RouterInitA
);

router.post(
'/profile/password',
`${accountApiPrefix}/password`,
koaGuard({
body: z.object({ password: z.string().min(1) }),
status: [204, 400, 401, 422],
Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/routes/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import koaAuth from '../middleware/koa-auth/index.js';
import koaOidcAuth from '../middleware/koa-auth/koa-oidc-auth.js';
import koaCors from '../middleware/koa-cors.js';

import { accountApiPrefix } from './account/constants.js';
import accountRoutes from './account/index.js';
import accountCentersRoutes from './account-center/index.js';
import adminUserRoutes from './admin-user/index.js';
import applicationOrganizationRoutes from './applications/application-organization.js';
Expand All @@ -34,7 +36,6 @@ import interactionRoutes from './interaction/index.js';
import logRoutes from './log.js';
import logtoConfigRoutes from './logto-config/index.js';
import organizationRoutes from './organization/index.js';
import profileRoutes from './profile/index.js';
import resourceRoutes from './resource.js';
import resourceScopeRoutes from './resource.scope.js';
import roleRoutes from './role.js';
Expand All @@ -47,7 +48,7 @@ import swaggerRoutes from './swagger/index.js';
import systemRoutes from './system.js';
import type { AnonymousRouter, ManagementApiRouter, UserRouter } from './types.js';
import userAssetsRoutes from './user-assets.js';
import verificationRoutes from './verification/index.js';
import verificationRoutes, { verificationApiPrefix } from './verification/index.js';
import verificationCodeRoutes from './verification-code.js';
import wellKnownRoutes from './well-known/index.js';
import wellKnownOpenApiRoutes from './well-known/well-known.openapi.js';
Expand Down Expand Up @@ -107,7 +108,7 @@ const createRouters = (tenant: TenantContext) => {
userRouter.use(koaOidcAuth(tenant));
// TODO(LOG-10147): Rename to koaApiHooks, this middleware is used for both management API and user API

Check warning on line 109 in packages/core/src/routes/init.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/core/src/routes/init.ts#L109

[no-warning-comments] Unexpected 'todo' comment: 'TODO(LOG-10147): Rename to koaApiHooks,...'.
userRouter.use(koaManagementApiHooks(tenant.libraries.hooks));
profileRoutes(userRouter, tenant);
accountRoutes(userRouter, tenant);
verificationRoutes(userRouter, tenant);

wellKnownRoutes(anonymousRouter, tenant);
Expand Down Expand Up @@ -136,7 +137,7 @@ const createRouters = (tenant: TenantContext) => {
export default function initApis(tenant: TenantContext): Koa {
const apisApp = new Koa();
const { adminUrlSet, cloudUrlSet } = EnvSet.values;
apisApp.use(koaCors([adminUrlSet, cloudUrlSet], ['/profile', '/verifications']));
apisApp.use(koaCors([adminUrlSet, cloudUrlSet], [accountApiPrefix, verificationApiPrefix]));
apisApp.use(koaBodyEtag());

for (const router of createRouters(tenant)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/routes/swagger/utils/operation-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ const exceptionPrefixes = Object.freeze([
'/interaction',
'/experience',
'/sign-in-exp/default/check-password',
'/profile',
'/account',
'/verifications',
]);

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/routes/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { WithI18nContext } from '#src/middleware/koa-i18next.js';
import { type WithHookContext } from '#src/middleware/koa-management-api-hooks.js';
import type TenantContext from '#src/tenants/TenantContext.js';

import { type WithAccountCenterContext } from './profile/middlewares/koa-account-center.js';
import { type WithAccountCenterContext } from './account/middlewares/koa-account-center.js';

Check warning on line 10 in packages/core/src/routes/types.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/routes/types.ts#L10

Added line #L10 was not covered by tests

export type AnonymousRouter = Router<unknown, WithLogContext & WithI18nContext>;

Expand Down
12 changes: 7 additions & 5 deletions packages/core/src/routes/verification/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { PasswordVerification } from '../experience/classes/verifications/passwo
import { SocialVerification } from '../experience/classes/verifications/social-verification.js';
import type { UserRouter, RouterInitArgs } from '../types.js';

export const verificationApiPrefix = '/verifications';

export default function verificationRoutes<T extends UserRouter>(
...[router, tenantContext]: RouterInitArgs<T>
) {
Expand All @@ -34,7 +36,7 @@ export default function verificationRoutes<T extends UserRouter>(
}

router.post(
'/verifications/password',
`${verificationApiPrefix}/password`,
koaGuard({
body: z.object({ password: z.string().min(1) }),
response: z.object({ verificationRecordId: z.string(), expiresAt: z.string() }),
Expand Down Expand Up @@ -76,7 +78,7 @@ export default function verificationRoutes<T extends UserRouter>(
);

router.post(
'/verifications/verification-code',
`${verificationApiPrefix}/verification-code`,
koaGuard({
body: z.object({
identifier: verificationCodeIdentifierGuard,
Expand Down Expand Up @@ -119,7 +121,7 @@ export default function verificationRoutes<T extends UserRouter>(
);

router.post(
'/verifications/verification-code/verify',
`${verificationApiPrefix}/verification-code/verify`,
koaGuard({
body: z.object({
identifier: verificationCodeIdentifierGuard,
Expand Down Expand Up @@ -164,7 +166,7 @@ export default function verificationRoutes<T extends UserRouter>(
);

router.post(
'/verifications/social',
`${verificationApiPrefix}/social`,
koaGuard({
body: socialAuthorizationUrlPayloadGuard.extend({
connectorId: z.string(),
Expand Down Expand Up @@ -202,7 +204,7 @@ export default function verificationRoutes<T extends UserRouter>(
);

router.post(
'/verifications/social/verify',
`${verificationApiPrefix}/social/verify`,
koaGuard({
body: socialVerificationCallbackPayloadGuard
.pick({
Expand Down
16 changes: 8 additions & 8 deletions packages/integration-tests/src/api/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const updatePassword = async (
verificationRecordId: string,
password: string
) =>
api.post('api/profile/password', {
api.post('api/account/password', {
json: { password },
headers: { [verificationRecordIdHeader]: verificationRecordId },
});
Expand All @@ -19,7 +19,7 @@ export const updatePrimaryEmail = async (
verificationRecordId: string,
newIdentifierVerificationRecordId: string
) =>
api.post('api/profile/primary-email', {
api.post('api/account/primary-email', {
json: { email, newIdentifierVerificationRecordId },
headers: { [verificationRecordIdHeader]: verificationRecordId },
});
Expand All @@ -35,7 +35,7 @@ export const updatePrimaryPhone = async (
verificationRecordId: string,
newIdentifierVerificationRecordId: string
) =>
api.post('api/profile/primary-phone', {
api.post('api/account/primary-phone', {
json: { phone, newIdentifierVerificationRecordId },
headers: { [verificationRecordIdHeader]: verificationRecordId },
});
Expand All @@ -50,7 +50,7 @@ export const updateIdentities = async (
verificationRecordId: string,
newIdentifierVerificationRecordId: string
) =>
api.post('api/profile/identities', {
api.post('api/account/identities', {
json: { newIdentifierVerificationRecordId },
headers: { [verificationRecordIdHeader]: verificationRecordId },
});
Expand All @@ -60,15 +60,15 @@ export const deleteIdentity = async (
target: string,
verificationRecordId: string
) =>
api.delete(`api/profile/identities/${target}`, {
api.delete(`api/account/identities/${target}`, {
headers: { [verificationRecordIdHeader]: verificationRecordId },
});

export const updateUser = async (api: KyInstance, body: Record<string, unknown>) =>
api.patch('api/profile', { json: body }).json<Partial<UserProfileResponse>>();
api.patch('api/account', { json: body }).json<Partial<UserProfileResponse>>();

export const updateOtherProfile = async (api: KyInstance, body: Record<string, unknown>) =>
api.patch('api/profile/profile', { json: body }).json<Partial<UserProfileResponse['profile']>>();
api.patch('api/account/profile', { json: body }).json<Partial<UserProfileResponse['profile']>>();

export const getUserInfo = async (api: KyInstance) =>
api.get('api/profile').json<Partial<UserProfileResponse>>();
api.get('api/account').json<Partial<UserProfileResponse>>();
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const expectedError = {
status: 400,
};

describe('profile, account center fields disabled', () => {
describe('account center fields disabled', () => {
beforeAll(async () => {
await enableAllPasswordSignInMethods();
await updateAccountCenter({
Expand All @@ -42,7 +42,7 @@ describe('profile, account center fields disabled', () => {
});
});

it('should return only name in GET /profile', async () => {
it('should return only name in GET /account', async () => {
const { user, username, password } = await createDefaultTenantUserWithPassword();
const api = await signInAndGetUserApi(username, password, {
scopes: [UserScope.Email],
Expand Down
Loading

0 comments on commit 129e714

Please sign in to comment.