From 679f11633ff698c8fc80f4518d19e915831e3d51 Mon Sep 17 00:00:00 2001 From: tecc Date: Tue, 3 Oct 2023 05:08:35 +0200 Subject: [PATCH 1/4] change(rewrite): Use `detail` field instead for second line of `UNKNOWN_PRONOUN` message field --- new/backend/src/server/v1.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/new/backend/src/server/v1.ts b/new/backend/src/server/v1.ts index befa2636b..45cba5538 100644 --- a/new/backend/src/server/v1.ts +++ b/new/backend/src/server/v1.ts @@ -49,7 +49,8 @@ export const ApiError = { UNKNOWN_PRONOUN: { code: 404, message: - "We aren't aware of any such pronoun. Perhaps there's a typo?\nNote that for custom pronouns, you need to specify all necessary forms: https://en.pronouns.page/faq#custom-pronouns", + "We aren't aware of any such pronoun. Perhaps there's a typo, or perhaps you specified an incorrect number of morphemes?", + detail: "Note that for custom pronouns, you need to specify all necessary forms: https://en.pronouns.page/faq#custom-pronouns", }, } satisfies Record; From 581d6e7da3911c78cf6db09f34aab3dfb81ee7d7 Mon Sep 17 00:00:00 2001 From: tecc Date: Tue, 3 Oct 2023 05:24:49 +0200 Subject: [PATCH 2/4] change(rewrite): Small changes in rewrite history-generator: The `historyDefaultsToWeirdGeneratorThing` has been renamed to `isUserGenerated`, and the expression for setting `history` to `__generator__` has been changed. See https://gitlab.com/PronounsPage/PronounsPage/-/merge_requests/373#note_1577573480. cleaning: Formatted a few files and cleaned them up. --- new/backend/src/config.ts | 2 +- new/backend/src/global.d.ts | 2 -- new/backend/src/server/v1.ts | 10 +++------- new/backend/src/server/v1/pronouns.ts | 2 +- new/common/src/suml.ts | 1 - 5 files changed, 5 insertions(+), 12 deletions(-) diff --git a/new/backend/src/config.ts b/new/backend/src/config.ts index cabd5ab76..c0cf2f6c2 100644 --- a/new/backend/src/config.ts +++ b/new/backend/src/config.ts @@ -24,7 +24,7 @@ export interface Config { }; database: { url: string; - } + }; security: { secret: string; }; diff --git a/new/backend/src/global.d.ts b/new/backend/src/global.d.ts index b6fb58942..5649183aa 100644 --- a/new/backend/src/global.d.ts +++ b/new/backend/src/global.d.ts @@ -1,9 +1,7 @@ import type { FastifyInstance, FastifyPluginAsync, - // FastifyPluginCallback, FastifyPluginOptions, - FastifyRequest, FastifyReply, RawReplyDefaultExpression, RawRequestDefaultExpression, diff --git a/new/backend/src/server/v1.ts b/new/backend/src/server/v1.ts index 45cba5538..ef3cdcd1f 100644 --- a/new/backend/src/server/v1.ts +++ b/new/backend/src/server/v1.ts @@ -60,8 +60,7 @@ export const localeSpecific = Type.Object({ export interface TransformPronounOptions { processName: boolean; - // I know it's a long name but it's very descriptive. - historyDefaultsToWeirdGeneratorThing: boolean; + isUserGenerated: boolean; } export function transformPronoun( pronoun: Pronoun, @@ -71,8 +70,7 @@ export function transformPronoun( ) { const opts: TransformPronounOptions = { processName: options?.processName ?? false, - historyDefaultsToWeirdGeneratorThing: - options?.historyDefaultsToWeirdGeneratorThing ?? false, + isUserGenerated: options?.isUserGenerated ?? false, }; const morphemes: Record = {}; const pronunciations: Record = {}; @@ -91,9 +89,7 @@ export function transformPronoun( plural: [pronoun.isPlural], pluralHonorific: [pronoun.isPluralHonorific], aliases: pronoun.keys.slice(1), - history: - pronoun.history ?? - (opts.historyDefaultsToWeirdGeneratorThing ? "__generator__" : ""), + history: opts.isUserGenerated ? "__generator__" : pronoun.history ?? "", pronounceable: pronoun.isPronounceable, thirdForm: pronoun.thirdForm ?? null, smallForm: pronoun.smallForm ?? null, diff --git a/new/backend/src/server/v1/pronouns.ts b/new/backend/src/server/v1/pronouns.ts index a76a6cf4a..73afcc029 100644 --- a/new/backend/src/server/v1/pronouns.ts +++ b/new/backend/src/server/v1/pronouns.ts @@ -93,7 +93,7 @@ export const plugin = async function (app: AppInstance) { locale, { processName: true, - historyDefaultsToWeirdGeneratorThing: true, + isUserGenerated: true, } ); } diff --git a/new/common/src/suml.ts b/new/common/src/suml.ts index 8c0775a84..78dd9ee86 100644 --- a/new/common/src/suml.ts +++ b/new/common/src/suml.ts @@ -2,7 +2,6 @@ import * as SumlImpl from "suml"; declare module "suml"; -console.log(SumlImpl); const instance = new SumlImpl.default(); export function parse(value: string): unknown { From 887498daed61c21d3b77f46910091b08517cbee1 Mon Sep 17 00:00:00 2001 From: tecc Date: Tue, 3 Oct 2023 05:29:44 +0200 Subject: [PATCH 3/4] feat(rewrite): Initial database and `/inclusive` endpoints code #prisma: Instead of using `.prisma/client` and `@prisma/client` (which re-exports `.prisma/client`, the rewrite now just uses the generated code directly through `#prisma` (see package.json and tsconfig.json). inclusive: The `/inclusive` endpoint has started to be written. Right now the code is untested as I don't have sample data, but I'll test it soon enough. --- new/backend/package.json | 5 ++-- new/backend/src/db.ts | 4 ++++ new/backend/src/server/v1.ts | 3 ++- new/backend/src/server/v1/inclusive.ts | 33 ++++++++++++++++++++++++++ new/backend/tsconfig.json | 3 ++- 5 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 new/backend/src/db.ts create mode 100644 new/backend/src/server/v1/inclusive.ts diff --git a/new/backend/package.json b/new/backend/package.json index eb7eb5552..0eda20866 100644 --- a/new/backend/package.json +++ b/new/backend/package.json @@ -24,7 +24,6 @@ "keywords": [], "author": "", "dependencies": { - ".prisma/client": "link:prisma/dist", "@fastify/type-provider-typebox": "^3.5.0", "@prisma/client": "5.3.1", "@pronounspage/common": "workspace:*", @@ -44,6 +43,8 @@ "typescript": "^5.2.2" }, "imports": { - "#self/*": "./dist/*.js" + "#self/*": "./dist/*.js", + "#prisma": "./prisma/dist/index.js", + "#prisma/*": "./prisma/dist/*.js" } } diff --git a/new/backend/src/db.ts b/new/backend/src/db.ts new file mode 100644 index 000000000..7828548b2 --- /dev/null +++ b/new/backend/src/db.ts @@ -0,0 +1,4 @@ +import { PrismaClient } from "#prisma"; +export type * from "#prisma"; + +export const db = new PrismaClient(); diff --git a/new/backend/src/server/v1.ts b/new/backend/src/server/v1.ts index ef3cdcd1f..4d1284020 100644 --- a/new/backend/src/server/v1.ts +++ b/new/backend/src/server/v1.ts @@ -2,12 +2,12 @@ import { Type } from "@sinclair/typebox"; import { allPronounVariants, examplesFor, - getLocale, Locale, Pronoun, PronounExample, } from "#self/locales"; import pronouns from "#self/server/v1/pronouns"; +import inclusive from "#self/server/v1/inclusive"; export type V1AppInstance = AppInstance; @@ -107,5 +107,6 @@ export function transformPronoun( export const routes = async function (app: AppInstance) { app.register(pronouns); + app.register(inclusive); } satisfies AppPluginAsync; export default routes; diff --git a/new/backend/src/server/v1/inclusive.ts b/new/backend/src/server/v1/inclusive.ts new file mode 100644 index 000000000..fb61f6492 --- /dev/null +++ b/new/backend/src/server/v1/inclusive.ts @@ -0,0 +1,33 @@ +import { localeSpecific } from "#self/server/v1"; +import { db, Inclusive } from "#self/db"; + +export function transformInclusive(inc: Inclusive) { + return { + id: inc.id, + insteadOf: inc.insteadOf, + say: inc.say, + because: inc.because, + locale: inc.locale, + }; +} + +export const plugin = async function (app) { + app.get( + "/:locale/inclusive", + { + schema: { + params: localeSpecific, + }, + }, + async (req, reply) => { + const values = await db.inclusive.findMany({ + where: { + locale: req.params.locale, + }, + take: 50, + }); + return reply.send(values.map(transformInclusive)); + } + ); +} satisfies AppPluginAsync; +export default plugin; diff --git a/new/backend/tsconfig.json b/new/backend/tsconfig.json index c49a1544f..0058635cd 100644 --- a/new/backend/tsconfig.json +++ b/new/backend/tsconfig.json @@ -9,7 +9,8 @@ "outDir": "./dist", "resolvePackageJsonExports": true, "paths": { - "#self/*": ["./src/*.js"] + "#self/*": ["./src/*.js"], + "#prisma": ["./prisma/dist/index.d.ts"] }, "incremental": true, "strictNullChecks": true, From ee8b8d40431690a60b6e3daf7490208842e2bc9f Mon Sep 17 00:00:00 2001 From: tecc Date: Tue, 3 Oct 2023 05:30:25 +0200 Subject: [PATCH 4/4] feat(rewrite): Load `expectedTranslations` in locales.ts --- new/backend/src/locales.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/new/backend/src/locales.ts b/new/backend/src/locales.ts index b5c22947b..181166f97 100644 --- a/new/backend/src/locales.ts +++ b/new/backend/src/locales.ts @@ -40,6 +40,8 @@ export interface LocaleDescription { family: string; } +let expectedTranslations: Array; + let loadedDescriptions: Array; let indexedDescriptions: Record; let activeLocaleDescriptions: Array | undefined; @@ -57,6 +59,15 @@ export async function loadLocaleDescriptions(): Promise< indexedDescriptions = Object.fromEntries( loadedDescriptions.map((v) => [v.code, v]) ); + expectedTranslations = ( + await import( + "file://" + + path.resolve( + getConfig().localeDataPath, + "expectedTranslations.js" + ) + ) + ).default; activeLocaleDescriptions = undefined; } return loadedDescriptions; @@ -81,6 +92,13 @@ export function getActiveLocaleDescriptions(): Array { return activeLocaleDescriptions; } +export function getExpectedTranslations(): Array { + if (!Array.isArray(expectedTranslations)) { + throw new Error("Expected translations have not been loaded yet"); + } + return expectedTranslations; +} + export function isLocaleActive(localeCode: string): boolean { return getActiveLocaleDescriptions().some((v) => v.code === localeCode); }