Skip to content

Commit

Permalink
refactor: move vscode-ally key mapping to satisfies I18nKey
Browse files Browse the repository at this point in the history
this allows to remove the runtime implication of calling a functtion that simply returns the provided key
  • Loading branch information
V-ed committed Feb 27, 2024
1 parent 5ea21f0 commit cbdc413
Show file tree
Hide file tree
Showing 23 changed files with 61 additions and 55 deletions.
4 changes: 2 additions & 2 deletions api/.vscode/i18n-ally-custom-framework.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ usageMatchRegex:
- "[^\\w\\d]new I18nException\\(['\"`]({key})['\"`]"
- "[^\\w\\d]i18n\\??.t\\(['\"`]({key})['\"`]"
- "(?:{{ |\\()t ['\"`]({key})['\"`]"
- "[^\\w\\d]k\\(['\"`]({key})['\"`]"
- "[^\\w\\d]['\"`]({key})['\"`] satisfies I18nKey"

# A RegEx to set a custom scope range. This scope will be used as a prefix when detecting keys
# and works like how the i18next framework identifies the namespace scope from the
Expand All @@ -28,6 +28,6 @@ refactorTemplates:
- i18n.t('$1')
- this.i18n.t('$1')
- new I18nException('$1')
- "k('$1')"
- "'$1' satisfies I18nKey"
# If set to true, only enables this custom framework (will disable all built-in frameworks)
# monopoly: true
10 changes: 5 additions & 5 deletions api/src/@shared/schemas/auth.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { z } from 'zod';
import { k } from '../utils/i18n';
import type { I18nKey } from '../utils/i18n';

export const PASSWORD_MIN_LENGTH = 4;

export const passwordSchema = z
.string({ required_error: k('common.errors.validation.required') })
.min(PASSWORD_MIN_LENGTH, k('common.errors.validation.password.minlength'));
.string({ required_error: 'common.errors.validation.required' satisfies I18nKey })
.min(PASSWORD_MIN_LENGTH, 'common.errors.validation.password.minlength' satisfies I18nKey);

export const emailSchema = z
.string({ required_error: k('common.errors.validation.required') })
.email(k('common.errors.validation.email-schema'));
.string({ required_error: 'common.errors.validation.required' satisfies I18nKey })
.email('common.errors.validation.email-schema' satisfies I18nKey);
4 changes: 1 addition & 3 deletions api/src/@shared/utils/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export function k(key: string) {
return key;
}
export type I18nKey = string;
3 changes: 2 additions & 1 deletion client/.vscode/i18n-ally-custom-framework.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ usageMatchRegex:
# the `{key}` will be placed by a proper keypath matching regex,
# you can ignore it and use your own matching rules as well
- "[^\\w\\d]k\\(['\"`]({key})['\"`]"
- "[^\\w\\d]['\"`]({key})['\"`] satisfies I18nKey"

# A RegEx to set a custom scope range. This scope will be used as a prefix when detecting keys
# and works like how the i18next framework identifies the namespace scope from the
Expand All @@ -24,7 +25,7 @@ usageMatchRegex:
refactorTemplates:
- "{$t('$1')}"
- "t.get('$1')"
- "k('$1')"
- "'$1' satisfies I18nKey"

# If set to true, only enables this custom framework (will disable all built-in frameworks)
# monopoly: true
4 changes: 2 additions & 2 deletions client/src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createTsRestClient } from '$lib/ts-rest/client';
import type { Handle, HandleFetch } from '@sveltejs/kit';
import { StatusCodes } from 'http-status-codes';
import { parseString } from 'set-cookie-parser';
import { SESSION_COOKIE_NAME, k } from '~shared';
import { SESSION_COOKIE_NAME, type I18nKey } from '~shared';
import { getAuthUser } from './auth/auth-handler';
import { themeCookieName, themes, type Theme } from './lib/stores';
import { HASJS_COOKIE_NAME } from './lib/utils/js-handling';
Expand Down Expand Up @@ -64,7 +64,7 @@ export const handleFetch: HandleFetch = async ({ event, request, fetch }) => {
}

const response = await fetch(request).catch(() => {
const fakeResponse = { message: k('common.errors.server.down') };
const fakeResponse = { message: 'common.errors.server.down' satisfies I18nKey };

const blob = new Blob([JSON.stringify(fakeResponse)], {
type: 'application/json',
Expand Down
3 changes: 2 additions & 1 deletion client/src/i18n/fr/shared/userform.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"new-email": "Nouveau Courriel"
},
"roles": {
"admin": "Administrateur"
"admin": "Administrateur",
"chat": "Chat"
},
"placeholders": {
"email": "[email protected]"
Expand Down
4 changes: 2 additions & 2 deletions client/src/lib/utils/assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { StatusCodes } from 'http-status-codes';
import { redirect } from 'sveltekit-flash-message/server';
import type { Infer, SuperValidated } from 'sveltekit-superforms';
import type { AnyZodObject } from 'zod';
import { k } from '~shared';
import type { I18nKey } from '~shared';

export type ValidResult<T extends { status: number }> = T extends { status: StatusCodes.OK } ? T : never;
export type InvalidResult<T extends { status: number }> = Exclude<T, { status: StatusCodes.OK }>;
Expand Down Expand Up @@ -59,7 +59,7 @@ export function assertTsRestActionResultOK<T extends { status: number; body: unk
const errorMessage =
result.body && typeof result.body === 'object' && 'message' in result.body
? String(result.body.message)
: k('common.errorpage.types.server.details.500');
: ('common.errorpage.types.server.details.500' satisfies I18nKey);

const pageData = (() => {
if ('layoutAlert' in args) {
Expand Down
10 changes: 5 additions & 5 deletions client/src/navigation/routes.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import type { NavElement } from '$lib/components/nav/nav-elements';
import { Roles, k } from '~shared';
import { Roles, type I18nKey } from '~shared';

export const navElements: NavElement[] = [
{
title: k('navbar.navigation.about'),
title: 'navbar.navigation.about' satisfies I18nKey,
url: '/about',
isPublic: true,
drawerIconPath: 'i-mdi-information',
},
{
title: k('navbar.navigation.chat'),
title: 'navbar.navigation.chat' satisfies I18nKey,
url: '/chat',
drawerIconPath: 'i-mdi-chat',
roles: [Roles.CHAT],
},
{
title: k('navbar.navigation.admin'),
title: 'navbar.navigation.admin' satisfies I18nKey,
id: 'admin',
drawerIconPath: 'i-mdi-shield-crown',
roles: [Roles.ADMIN],
elements: [
{
title: k('navbar.navigation.admin-users'),
title: 'navbar.navigation.admin-users' satisfies I18nKey,
url: '/admin/users',
drawerIconPath: 'i-mdi-account-details',
matches: [/\/admin\/users\/.+/],
Expand Down
6 changes: 3 additions & 3 deletions client/src/routes/(auth)/forgot_password/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { error } from '@sveltejs/kit';
import { StatusCodes } from 'http-status-codes';
import { superValidate } from 'sveltekit-superforms';
import { zod } from 'sveltekit-superforms/adapters';
import { k } from '~shared';
import type { I18nKey } from '~shared';
import type { Actions, PageServerLoad } from './$types';
import { requestPasswordSchema, resetPasswordSchema } from './schemas';

Expand Down Expand Up @@ -59,7 +59,7 @@ export const actions = {
result: () => tsrest.auth.forgotPasswordRequest({ query: form.data }),
onValid: () => ({
layoutAlert: createLayoutAlert({
text: k('(auth).forgot_password.request.alert'),
text: '(auth).forgot_password.request.alert' satisfies I18nKey,
}),
}),
});
Expand All @@ -74,7 +74,7 @@ export const actions = {
onValid: () => ({
redirectTo: '/login',
layoutAlert: createLayoutAlert({
text: k('(auth).forgot_password.reset.action.success'),
text: '(auth).forgot_password.reset.action.success' satisfies I18nKey,
}),
}),
});
Expand Down
4 changes: 2 additions & 2 deletions client/src/routes/(auth)/forgot_password/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { z } from 'zod';
import { emailSchema, k, passwordSchema } from '~shared';
import { emailSchema, passwordSchema, type I18nKey } from '~shared';

export const requestPasswordSchema = z.object({
email: emailSchema,
});

export const resetPasswordSchema = z.object({
password: passwordSchema,
resetToken: z.string({ required_error: k('(auth).forgot_password.reset.action.no-token') }),
resetToken: z.string({ required_error: '(auth).forgot_password.reset.action.no-token' satisfies I18nKey }),
});
4 changes: 2 additions & 2 deletions client/src/routes/(auth)/login/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { StatusCodes } from 'http-status-codes';
import { superValidate } from 'sveltekit-superforms';
import { zod } from 'sveltekit-superforms/adapters';
import { z } from 'zod';
import { emailSchema, k, passwordSchema } from '~shared';
import { emailSchema, passwordSchema, type I18nKey } from '~shared';
import type { Actions, PageServerLoad } from './$types';

const schema = z.object({
Expand All @@ -29,7 +29,7 @@ export const load = (async ({ url, locals: { sessionUser } }) => {
}

return createLayoutAlert({
text: k('(auth).login.errors.access'),
text: '(auth).login.errors.access' satisfies I18nKey,
type: 'warning',
});
})();
Expand Down
4 changes: 2 additions & 2 deletions client/src/routes/(auth)/register/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { error, type Actions } from '@sveltejs/kit';
import { StatusCodes } from 'http-status-codes';
import { superValidate } from 'sveltekit-superforms';
import { zod } from 'sveltekit-superforms/adapters';
import { k } from '~shared';
import type { I18nKey } from '~shared';
import type { PageServerLoad } from './$types';
import { registerSchema } from './schema';

export const load = (async ({ url, locals: { tsrest } }) => {
const registerToken = url.searchParams.get('token');

if (!registerToken) {
error(StatusCodes.BAD_REQUEST, { message: k('(auth).register.errors.missing-token') });
error(StatusCodes.BAD_REQUEST, { message: '(auth).register.errors.missing-token' satisfies I18nKey });
}

const result = await tsrest.auth.initRegistration({ query: { registerToken } });
Expand Down
6 changes: 4 additions & 2 deletions client/src/routes/(user)/settings/experience/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { assertTsRestActionResultOK } from '$lib/utils/assertions';
import { superValidate } from 'sveltekit-superforms';
import { zod, type Infer } from 'sveltekit-superforms/adapters';
import { z } from 'zod';
import { k } from '~shared';
import type { I18nKey } from '~shared';
import type { Actions, PageServerLoad } from './$types';

const langSchema = z.object({
Expand Down Expand Up @@ -35,7 +35,9 @@ export const actions = {
},
onValid: () => ({
toasts: createToasts({
text: lang ? k('settings.experience.lang.toast.targetted') : k('settings.experience.lang.toast.automatic'),
text: lang
? ('settings.experience.lang.toast.targetted' satisfies I18nKey)
: ('settings.experience.lang.toast.automatic' satisfies I18nKey),
timeout: 3000,
}),
}),
Expand Down
4 changes: 2 additions & 2 deletions client/src/routes/(user)/settings/profile/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { fail, type Actions } from '@sveltejs/kit';
import { StatusCodes } from 'http-status-codes';
import { superValidate } from 'sveltekit-superforms';
import { zod } from 'sveltekit-superforms/adapters';
import { k } from '~shared';
import type { I18nKey } from '~shared';
import type { PageServerLoad } from './$types';

export const load = (async ({ locals: { sessionUser } }) => {
Expand All @@ -31,7 +31,7 @@ export const actions = {

if (!(profilePictureFile instanceof File)) {
const toasts = createToasts({
text: k('settings.profile.picture.errors.missing'),
text: 'settings.profile.picture.errors.missing' satisfies I18nKey,
});

return fail(StatusCodes.BAD_REQUEST, { toasts });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { redirect } from 'sveltekit-flash-message/server';
import { setError, superValidate } from 'sveltekit-superforms';
import { zod } from 'sveltekit-superforms/adapters';
import { z } from 'zod';
import { emailSchema, k } from '~shared';
import { emailSchema, type I18nKey } from '~shared';
import type { PageServerLoad } from './$types';

const emailChangeSchema = z.object({
Expand Down Expand Up @@ -60,7 +60,7 @@ export const actions = {
},
onValid: () => ({
toasts: createToasts({
text: k('settings.profile.email.request.success'),
text: 'settings.profile.email.request.success' satisfies I18nKey,
i18nPayload: { email: form.data.email },
}),
}),
Expand Down
6 changes: 3 additions & 3 deletions client/src/routes/(user)/settings/security/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Actions } from '@sveltejs/kit';
import { superValidate } from 'sveltekit-superforms';
import { zod } from 'sveltekit-superforms/adapters';
import { z } from 'zod';
import { k, passwordSchema } from '~shared';
import { passwordSchema, type I18nKey } from '~shared';
import type { PageServerLoad } from './$types';

const newPasswordFormSchema = z
Expand All @@ -13,7 +13,7 @@ const newPasswordFormSchema = z
confirm: z.string(),
})
.refine(({ password, confirm }) => password === confirm, {
message: k('settings.security.actions.password.errors.not-matching'),
message: 'settings.security.actions.password.errors.not-matching' satisfies I18nKey,
path: ['confirm'],
});

Expand All @@ -34,7 +34,7 @@ export const actions = {
onValid: () => ({
form,
toasts: createToasts({
text: k('settings.security.actions.password.updated.success'),
text: 'settings.security.actions.password.updated.success' satisfies I18nKey,
}),
}),
});
Expand Down
6 changes: 3 additions & 3 deletions client/src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { AppPageData } from '$app-types';
import { createLayoutAlert } from '$lib/components/LayoutAlert/helper';
import { HASJS_COOKIE_NAME } from '$lib/utils/js-handling';
import { loadFlash } from 'sveltekit-flash-message/server';
import { k } from '~shared';
import type { I18nKey } from '~shared';

export const load = loadFlash(async (event) => {
const {
Expand All @@ -28,13 +28,13 @@ export const load = loadFlash(async (event) => {

if (sessionUser === undefined) {
layoutAlert = createLayoutAlert({
text: k('common.errors.server.down'),
text: 'common.errors.server.down' satisfies I18nKey,
i18nPayload: { userHasJs },
type: 'error',
});
} else if (url.searchParams.has('forbidden')) {
layoutAlert = createLayoutAlert({
text: k('common.errors.access.forbidden'),
text: 'common.errors.access.forbidden' satisfies I18nKey,
type: 'error',
});
}
Expand Down
8 changes: 4 additions & 4 deletions client/src/routes/admin/users/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { assertTsRestActionResultOK } from '$lib/utils/assertions';
import { streamed } from '$lib/utils/streaming';
import { StatusCodes } from 'http-status-codes';
import { redirect } from 'sveltekit-flash-message/server';
import { emailSchema, k } from '~shared';
import { emailSchema, type I18nKey } from '~shared';
import type { Actions, PageServerLoad } from './$types';

export const load = (async ({ locals: { tsrest } }) => {
Expand Down Expand Up @@ -42,9 +42,9 @@ export const actions = {
'/admin/users',
{
toasts: createToasts({
text: k('admin.users.actions.delete.errors.missing-email.error'),
text: 'admin.users.actions.delete.errors.missing-email.error' satisfies I18nKey,
type: 'warning',
extraData: k('admin.users.actions.delete.errors.missing-email.details'),
extraData: 'admin.users.actions.delete.errors.missing-email.details' satisfies I18nKey,
}),
},
cookies,
Expand All @@ -56,7 +56,7 @@ export const actions = {
result: () => tsrest.users.admin.deleteUser({ body: { email } }),
onValid: () => ({
toasts: createToasts({
text: k('admin.users.actions.delete.success'),
text: 'admin.users.actions.delete.success' satisfies I18nKey,
i18nPayload: { email },
}),
}),
Expand Down
4 changes: 2 additions & 2 deletions client/src/routes/admin/users/[email]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createToasts } from '$lib/components/ToastManager/helper';
import { assertTsRestActionResultOK, assertTsRestResultOK } from '$lib/utils/assertions';
import { superValidate } from 'sveltekit-superforms';
import { zod } from 'sveltekit-superforms/adapters';
import { k } from '~shared';
import type { I18nKey } from '~shared';
import { adminUserFormSchema } from '../schema/schema';
import type { Actions, PageServerLoad } from './$types';

Expand Down Expand Up @@ -50,7 +50,7 @@ export const actions = {
onValid: () => ({
redirectTo: '/admin/users',
toasts: createToasts({
text: k('admin.users.actions.edit.success'),
text: 'admin.users.actions.edit.success' satisfies I18nKey,
i18nPayload: { email: editableUserEmail },
}),
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import type { TsRestClient } from '$lib/ts-rest/client';
import { Button } from 'flowbite-svelte';
import { StatusCodes } from 'http-status-codes';
import { k } from '~shared';
import type { I18nKey } from '~shared';
import type { BaseUser } from '../../types';
let i18n = getI18n();
$: ({ t } = $i18n);
Expand Down Expand Up @@ -37,14 +37,14 @@
});
} else {
toasts = createToasts({
text: k('admin.users.tables.actions.resend-invite.toasts.error-server'),
text: 'admin.users.tables.actions.resend-invite.toasts.error-server' satisfies I18nKey,
timeout: showInviteResentTextDuration,
// extraData: `<div class="mt-3 italic">${errors}</div>`,
});
}
} catch (error) {
toasts = createToasts({
text: k('admin.users.tables.actions.resend-invite.toasts.error-local'),
text: 'admin.users.tables.actions.resend-invite.toasts.error-local' satisfies I18nKey,
type: 'warning',
timeout: showInviteResentTextDuration,
extraData: `<div class="mt-3 italic">${error}</div>`,
Expand Down
Loading

1 comment on commit cbdc413

@ved-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 This commit is now deployed to Vercel!

fullstacked

https://fullstacked-4wzp2d1id-v-ed.vercel.app

Please sign in to comment.