From 3da3309cbf27c176e38f952772d4de662da76527 Mon Sep 17 00:00:00 2001 From: "IceHe.xyz" Date: Mon, 18 Apr 2022 13:35:48 +0800 Subject: [PATCH] refactor(core,schemas): pascalcase enum keys (#558) * refactor(schemas): pascalcase enum keys * chore(core): pascalcase enum keys --- .../core/src/__mocks__/sign-in-experience.ts | 24 +++---- .../core/src/lib/sign-in-experience.test.ts | 24 +++---- packages/core/src/lib/sign-in-experience.ts | 4 +- .../src/middleware/koa-role-guard.test.ts | 4 +- .../routes/sign-in-experience.guard.test.ts | 68 +++++++++---------- .../src/routes/sign-in-experience.test.ts | 8 +-- .../schemas/src/foundations/jsonb-types.ts | 10 +-- .../schemas/src/seeds/sign-in-experience.ts | 12 ++-- packages/schemas/src/types/user.ts | 2 +- 9 files changed, 78 insertions(+), 78 deletions(-) diff --git a/packages/core/src/__mocks__/sign-in-experience.ts b/packages/core/src/__mocks__/sign-in-experience.ts index 69ecbe7f1..0efc501e2 100644 --- a/packages/core/src/__mocks__/sign-in-experience.ts +++ b/packages/core/src/__mocks__/sign-in-experience.ts @@ -24,14 +24,14 @@ export const mockSignInExperience: SignInExperience = { }, languageInfo: { autoDetect: true, - fallbackLanguage: Language.english, - fixedLanguage: Language.chinese, + fallbackLanguage: Language.English, + fixedLanguage: Language.Chinese, }, signInMethods: { - username: SignInMethodState.primary, - email: SignInMethodState.disabled, - sms: SignInMethodState.disabled, - social: SignInMethodState.secondary, + username: SignInMethodState.Primary, + email: SignInMethodState.Disabled, + sms: SignInMethodState.Disabled, + social: SignInMethodState.Secondary, }, socialSignInConnectorIds: ['github', 'facebook'], }; @@ -52,13 +52,13 @@ export const mockTermsOfUse: TermsOfUse = { export const mockLanguageInfo: LanguageInfo = { autoDetect: true, - fallbackLanguage: Language.english, - fixedLanguage: Language.chinese, + fallbackLanguage: Language.English, + fixedLanguage: Language.Chinese, }; export const mockSignInMethods: SignInMethods = { - username: SignInMethodState.primary, - email: SignInMethodState.disabled, - sms: SignInMethodState.disabled, - social: SignInMethodState.disabled, + username: SignInMethodState.Primary, + email: SignInMethodState.Disabled, + sms: SignInMethodState.Disabled, + social: SignInMethodState.Disabled, }; diff --git a/packages/core/src/lib/sign-in-experience.test.ts b/packages/core/src/lib/sign-in-experience.test.ts index b777be41d..b9e5c30d1 100644 --- a/packages/core/src/lib/sign-in-experience.test.ts +++ b/packages/core/src/lib/sign-in-experience.test.ts @@ -61,15 +61,15 @@ describe('validate terms of use', () => { describe('check whether the social sign in method state is enabled', () => { it('should be truthy when sign-in method state is primary', () => { - expect(isEnabled(SignInMethodState.primary)).toBeTruthy(); + expect(isEnabled(SignInMethodState.Primary)).toBeTruthy(); }); it('should be truthy when sign-in method state is secondary', () => { - expect(isEnabled(SignInMethodState.secondary)).toBeTruthy(); + expect(isEnabled(SignInMethodState.Secondary)).toBeTruthy(); }); it('should be falsy when sign-in method state is disabled', () => { - expect(isEnabled(SignInMethodState.disabled)).toBeFalsy(); + expect(isEnabled(SignInMethodState.Disabled)).toBeFalsy(); }); }); @@ -78,7 +78,7 @@ describe('validate sign-in methods', () => { test('should throw when there is no primary sign-in method', () => { expect(() => { validateSignInMethods( - { ...mockSignInMethods, username: SignInMethodState.disabled }, + { ...mockSignInMethods, username: SignInMethodState.Disabled }, [], [] ); @@ -89,7 +89,7 @@ describe('validate sign-in methods', () => { test('should throw when there are more than one primary sign-in methods', () => { expect(() => { - validateSignInMethods({ ...mockSignInMethods, social: SignInMethodState.primary }, [], []); + validateSignInMethods({ ...mockSignInMethods, social: SignInMethodState.Primary }, [], []); }).toMatchError( new RequestError('sign_in_experiences.not_one_and_only_one_primary_sign_in_method') ); @@ -100,7 +100,7 @@ describe('validate sign-in methods', () => { test('should throw when there is no enabled email connector and email sign-in method is enabled', async () => { expect(() => { validateSignInMethods( - { ...mockSignInMethods, email: SignInMethodState.secondary }, + { ...mockSignInMethods, email: SignInMethodState.Secondary }, [], enabledConnectorInstances as ConnectorInstance[] ); @@ -115,7 +115,7 @@ describe('validate sign-in methods', () => { test('should throw when there is no enabled SMS connector and SMS sign-in method is enabled', () => { expect(() => { validateSignInMethods( - { ...mockSignInMethods, sms: SignInMethodState.secondary }, + { ...mockSignInMethods, sms: SignInMethodState.Secondary }, [], enabledConnectorInstances as ConnectorInstance[] ); @@ -129,7 +129,7 @@ describe('validate sign-in methods', () => { test('should throw when there is no enabled social connector and social sign-in method is enabled', () => { expect(() => { - validateSignInMethods({ ...mockSignInMethods, social: SignInMethodState.secondary }, [], [ + validateSignInMethods({ ...mockSignInMethods, social: SignInMethodState.Secondary }, [], [ mockAliyunDmConnectorInstance, ] as ConnectorInstance[]); }).toMatchError( @@ -144,7 +144,7 @@ describe('validate sign-in methods', () => { test('should throw when the social connector IDs are empty and social sign-in method is enabled', () => { expect(() => { validateSignInMethods( - { ...mockSignInMethods, social: SignInMethodState.secondary }, + { ...mockSignInMethods, social: SignInMethodState.Secondary }, [], enabledConnectorInstances as ConnectorInstance[] ); @@ -155,7 +155,7 @@ describe('validate sign-in methods', () => { test('should not validate selected social connectors when social sign-in method is disabled', () => { expect(() => { validateSignInMethods( - { ...mockSignInMethods, social: SignInMethodState.disabled }, + { ...mockSignInMethods, social: SignInMethodState.Disabled }, ['google', 'facebook'], enabledConnectorInstances as ConnectorInstance[] ); @@ -165,7 +165,7 @@ describe('validate sign-in methods', () => { test('should throw when some selected social connector are disabled and social sign-in method is enabled', () => { expect(() => { validateSignInMethods( - { ...mockSignInMethods, social: SignInMethodState.secondary }, + { ...mockSignInMethods, social: SignInMethodState.Secondary }, ['google', 'facebook'], enabledConnectorInstances as ConnectorInstance[] ); @@ -175,7 +175,7 @@ describe('validate sign-in methods', () => { test('should not throw when all selected social connectors are enabled and social sign-in method is enabled', () => { expect(() => { validateSignInMethods( - { ...mockSignInMethods, social: SignInMethodState.secondary }, + { ...mockSignInMethods, social: SignInMethodState.Secondary }, ['facebook', 'github'], enabledConnectorInstances as ConnectorInstance[] ); diff --git a/packages/core/src/lib/sign-in-experience.ts b/packages/core/src/lib/sign-in-experience.ts index 2a6cdd570..817e7af15 100644 --- a/packages/core/src/lib/sign-in-experience.ts +++ b/packages/core/src/lib/sign-in-experience.ts @@ -23,7 +23,7 @@ export const validateTermsOfUse = (termsOfUse: TermsOfUse) => { ); }; -export const isEnabled = (state: SignInMethodState) => state !== SignInMethodState.disabled; +export const isEnabled = (state: SignInMethodState) => state !== SignInMethodState.Disabled; export const validateSignInMethods = ( signInMethods: SignInMethods, @@ -32,7 +32,7 @@ export const validateSignInMethods = ( ) => { const signInMethodStates = Object.values(signInMethods); assertThat( - signInMethodStates.filter((state) => state === SignInMethodState.primary).length === 1, + signInMethodStates.filter((state) => state === SignInMethodState.Primary).length === 1, 'sign_in_experiences.not_one_and_only_one_primary_sign_in_method' ); diff --git a/packages/core/src/middleware/koa-role-guard.test.ts b/packages/core/src/middleware/koa-role-guard.test.ts index b815008fa..2dbc2dc07 100644 --- a/packages/core/src/middleware/koa-role-guard.test.ts +++ b/packages/core/src/middleware/koa-role-guard.test.ts @@ -24,11 +24,11 @@ describe('koaRoleGuard middleware', () => { it('should throw if user dose not have admin role', async () => { ctx.userInfo.roleNames = ['guest']; - await expect(koaRoleGuard(UserRole.admin)(ctx, next)).rejects.toMatchError(unauthorizedError); + await expect(koaRoleGuard(UserRole.Admin)(ctx, next)).rejects.toMatchError(unauthorizedError); }); it('should not throw for admin user', async () => { ctx.userInfo.roleNames = ['admin']; - await expect(koaRoleGuard(UserRole.admin)(ctx, next)).resolves.not.toThrow(); + await expect(koaRoleGuard(UserRole.Admin)(ctx, next)).resolves.not.toThrow(); }); }); diff --git a/packages/core/src/routes/sign-in-experience.guard.test.ts b/packages/core/src/routes/sign-in-experience.guard.test.ts index 83a0d8d66..4ab7765ae 100644 --- a/packages/core/src/routes/sign-in-experience.guard.test.ts +++ b/packages/core/src/routes/sign-in-experience.guard.test.ts @@ -125,30 +125,30 @@ describe('signInMethods', () => { describe('username', () => { test.each(validSignInMethodStates)('%p should success', async (state) => { - if (state === SignInMethodState.primary) { + if (state === SignInMethodState.Primary) { return; } const signInExperience = { signInMethods: { username: state, - email: SignInMethodState.primary, - sms: SignInMethodState.disabled, - social: SignInMethodState.disabled, + email: SignInMethodState.Primary, + sms: SignInMethodState.Disabled, + social: SignInMethodState.Disabled, }, }; await expectPatchResponseStatus(signInExperience, 200); }); test.each(invalidSignInMethodStates)('%p should fail', async (state) => { - if (state === SignInMethodState.primary) { + if (state === SignInMethodState.Primary) { return; } const signInExperience = { signInMethods: { username: state, - email: SignInMethodState.primary, - sms: SignInMethodState.disabled, - social: SignInMethodState.disabled, + email: SignInMethodState.Primary, + sms: SignInMethodState.Disabled, + social: SignInMethodState.Disabled, }, }; await expectPatchResponseStatus(signInExperience, 400); @@ -157,30 +157,30 @@ describe('signInMethods', () => { describe('email', () => { test.each(validSignInMethodStates)('%p should success', async (state) => { - if (state === SignInMethodState.primary) { + if (state === SignInMethodState.Primary) { return; } const signInExperience = { signInMethods: { - username: SignInMethodState.disabled, + username: SignInMethodState.Disabled, email: state, - sms: SignInMethodState.primary, - social: SignInMethodState.disabled, + sms: SignInMethodState.Primary, + social: SignInMethodState.Disabled, }, }; await expectPatchResponseStatus(signInExperience, 200); }); test.each(invalidSignInMethodStates)('%p should fail', async (state) => { - if (state === SignInMethodState.primary) { + if (state === SignInMethodState.Primary) { return; } const signInExperience = { signInMethods: { - username: SignInMethodState.disabled, + username: SignInMethodState.Disabled, email: state, - sms: SignInMethodState.primary, - social: SignInMethodState.disabled, + sms: SignInMethodState.Primary, + social: SignInMethodState.Disabled, }, }; await expectPatchResponseStatus(signInExperience, 400); @@ -189,15 +189,15 @@ describe('signInMethods', () => { describe('sms', () => { test.each(validSignInMethodStates)('%p should success', async (state) => { - if (state === SignInMethodState.primary) { + if (state === SignInMethodState.Primary) { return; } const signInExperience = { signInMethods: { - username: SignInMethodState.disabled, - email: SignInMethodState.disabled, + username: SignInMethodState.Disabled, + email: SignInMethodState.Disabled, sms: state, - social: SignInMethodState.primary, + social: SignInMethodState.Primary, }, socialSignInConnectorIds: ['github'], }; @@ -205,15 +205,15 @@ describe('signInMethods', () => { }); test.each(invalidSignInMethodStates)('%p should fail', async (state) => { - if (state === SignInMethodState.primary) { + if (state === SignInMethodState.Primary) { return; } const signInExperience = { signInMethods: { - username: SignInMethodState.disabled, - email: SignInMethodState.disabled, + username: SignInMethodState.Disabled, + email: SignInMethodState.Disabled, sms: state, - social: SignInMethodState.primary, + social: SignInMethodState.Primary, }, socialSignInConnectorIds: ['github'], }; @@ -223,14 +223,14 @@ describe('signInMethods', () => { describe('social', () => { test.each(validSignInMethodStates)('%p should success', async (state) => { - if (state === SignInMethodState.primary) { + if (state === SignInMethodState.Primary) { return; } const signInExperience = { signInMethods: { - username: SignInMethodState.primary, - email: SignInMethodState.disabled, - sms: SignInMethodState.disabled, + username: SignInMethodState.Primary, + email: SignInMethodState.Disabled, + sms: SignInMethodState.Disabled, social: state, }, socialSignInConnectorIds: ['github'], @@ -239,14 +239,14 @@ describe('signInMethods', () => { }); test.each(invalidSignInMethodStates)('%p should fail', async (state) => { - if (state === SignInMethodState.primary) { + if (state === SignInMethodState.Primary) { return; } const signInExperience = { signInMethods: { - username: SignInMethodState.primary, - email: SignInMethodState.disabled, - sms: SignInMethodState.disabled, + username: SignInMethodState.Primary, + email: SignInMethodState.Disabled, + sms: SignInMethodState.Disabled, social: state, }, socialSignInConnectorIds: ['github'], @@ -262,7 +262,7 @@ describe('socialSignInConnectorIds', () => { async (socialSignInConnectorIds) => { await expectPatchResponseStatus( { - signInMethods: { ...mockSignInMethods, social: SignInMethodState.secondary }, + signInMethods: { ...mockSignInMethods, social: SignInMethodState.Secondary }, socialSignInConnectorIds, }, 200 @@ -275,7 +275,7 @@ describe('socialSignInConnectorIds', () => { async (socialSignInConnectorIds: any[]) => { await expectPatchResponseStatus( { - signInMethods: { ...mockSignInMethods, social: SignInMethodState.secondary }, + signInMethods: { ...mockSignInMethods, social: SignInMethodState.Secondary }, socialSignInConnectorIds, }, 400 diff --git a/packages/core/src/routes/sign-in-experience.test.ts b/packages/core/src/routes/sign-in-experience.test.ts index f8cec1fc4..00904c664 100644 --- a/packages/core/src/routes/sign-in-experience.test.ts +++ b/packages/core/src/routes/sign-in-experience.test.ts @@ -63,7 +63,7 @@ describe('GET /sign-in-exp', () => { it('should filter enabled social connectors', async () => { const signInExperience = { ...mockSignInExperience, - signInMethods: { ...mockSignInMethods, social: SignInMethodState.secondary }, + signInMethods: { ...mockSignInMethods, social: SignInMethodState.Secondary }, socialSignInConnectorIds: ['facebook', 'github', 'google'], }; @@ -93,7 +93,7 @@ describe('GET /sign-in-settings', () => { describe('PATCH /sign-in-exp', () => { it('should not update social connector ids when social sign-in is disabled', async () => { - const signInMethods = { ...mockSignInMethods, social: SignInMethodState.disabled }; + const signInMethods = { ...mockSignInMethods, social: SignInMethodState.Disabled }; const response = await signInExperienceRequester.patch('/sign-in-exp').send({ signInMethods, socialSignInConnectorIds: ['facebook'], @@ -108,7 +108,7 @@ describe('PATCH /sign-in-exp', () => { }); it('should update enabled social connector IDs only when social sign-in is enabled', async () => { - const signInMethods = { ...mockSignInMethods, social: SignInMethodState.secondary }; + const signInMethods = { ...mockSignInMethods, social: SignInMethodState.Secondary }; const socialSignInConnectorIds = ['facebook']; const signInExperience = { signInMethods, @@ -126,7 +126,7 @@ describe('PATCH /sign-in-exp', () => { }); it('should update social connector IDs in correct sorting order', async () => { - const signInMethods = { ...mockSignInMethods, social: SignInMethodState.secondary }; + const signInMethods = { ...mockSignInMethods, social: SignInMethodState.Secondary }; const socialSignInConnectorIds = ['github', 'facebook']; const signInExperience = { signInMethods, diff --git a/packages/schemas/src/foundations/jsonb-types.ts b/packages/schemas/src/foundations/jsonb-types.ts index 4e11f47ae..9af04d3ea 100644 --- a/packages/schemas/src/foundations/jsonb-types.ts +++ b/packages/schemas/src/foundations/jsonb-types.ts @@ -110,8 +110,8 @@ export const termsOfUseGuard = z.object({ export type TermsOfUse = z.infer; export enum Language { - english = 'en', - chinese = 'zh-cn', + English = 'en', + Chinese = 'zh-cn', } export const languageInfoGuard = z.object({ @@ -130,9 +130,9 @@ export enum SignInMethodKey { } export enum SignInMethodState { - primary = 'primary', - secondary = 'secondary', - disabled = 'disabled', + Primary = 'primary', + Secondary = 'secondary', + Disabled = 'disabled', } export const signInMethodsGuard = z.object({ diff --git a/packages/schemas/src/seeds/sign-in-experience.ts b/packages/schemas/src/seeds/sign-in-experience.ts index e0ce94c0d..b7600f700 100644 --- a/packages/schemas/src/seeds/sign-in-experience.ts +++ b/packages/schemas/src/seeds/sign-in-experience.ts @@ -12,17 +12,17 @@ export const defaultSignInExperience: Readonly = { }, languageInfo: { autoDetect: true, - fallbackLanguage: Language.english, - fixedLanguage: Language.english, + fallbackLanguage: Language.English, + fixedLanguage: Language.English, }, termsOfUse: { enabled: false, }, signInMethods: { - username: SignInMethodState.primary, - email: SignInMethodState.disabled, - sms: SignInMethodState.disabled, - social: SignInMethodState.disabled, + username: SignInMethodState.Primary, + email: SignInMethodState.Disabled, + sms: SignInMethodState.Disabled, + social: SignInMethodState.Disabled, }, socialSignInConnectorIds: [], }; diff --git a/packages/schemas/src/types/user.ts b/packages/schemas/src/types/user.ts index 368c155d3..1d3622a90 100644 --- a/packages/schemas/src/types/user.ts +++ b/packages/schemas/src/types/user.ts @@ -18,5 +18,5 @@ export type UserInfo; export enum UserRole { - admin = 'admin', + Admin = 'admin', }