Merge remote-tracking branch 'origin/main'

This commit is contained in:
Andrea Vos 2023-10-04 19:44:19 +02:00
commit c3cab91c8c
10 changed files with 69 additions and 17 deletions

View File

@ -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"
}
}

View File

@ -24,7 +24,7 @@ export interface Config {
};
database: {
url: string;
}
};
security: {
secret: string;
};

4
new/backend/src/db.ts Normal file
View File

@ -0,0 +1,4 @@
import { PrismaClient } from "#prisma";
export type * from "#prisma";
export const db = new PrismaClient();

View File

@ -1,9 +1,7 @@
import type {
FastifyInstance,
FastifyPluginAsync,
// FastifyPluginCallback,
FastifyPluginOptions,
FastifyRequest,
FastifyReply,
RawReplyDefaultExpression,
RawRequestDefaultExpression,

View File

@ -40,6 +40,8 @@ export interface LocaleDescription {
family: string;
}
let expectedTranslations: Array<string>;
let loadedDescriptions: Array<LocaleDescription>;
let indexedDescriptions: Record<string, LocaleDescription>;
let activeLocaleDescriptions: Array<LocaleDescription> | 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<LocaleDescription> {
return activeLocaleDescriptions;
}
export function getExpectedTranslations(): Array<string> {
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);
}

View File

@ -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;
@ -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<string, ApiErrorOptions>;
@ -59,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,
@ -70,8 +70,7 @@ export function transformPronoun(
) {
const opts: TransformPronounOptions = {
processName: options?.processName ?? false,
historyDefaultsToWeirdGeneratorThing:
options?.historyDefaultsToWeirdGeneratorThing ?? false,
isUserGenerated: options?.isUserGenerated ?? false,
};
const morphemes: Record<string, string> = {};
const pronunciations: Record<string, string> = {};
@ -90,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,
@ -110,5 +107,6 @@ export function transformPronoun(
export const routes = async function (app: AppInstance) {
app.register(pronouns);
app.register(inclusive);
} satisfies AppPluginAsync;
export default routes;

View File

@ -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;

View File

@ -93,7 +93,7 @@ export const plugin = async function (app: AppInstance) {
locale,
{
processName: true,
historyDefaultsToWeirdGeneratorThing: true,
isUserGenerated: true,
}
);
}

View File

@ -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,

View File

@ -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 {