From fd41ed92fed755b32cf4960a1275b1b6a869c8c2 Mon Sep 17 00:00:00 2001 From: Felix Haeberle Date: Mon, 29 Jul 2024 16:35:57 +0200 Subject: [PATCH] refactor types to handle multiple importer / exporter --- inlang/source-code/sdk-v2/src/types/module.ts | 18 ++----- inlang/source-code/sdk-v2/src/types/plugin.ts | 53 ++++++++++--------- 2 files changed, 33 insertions(+), 38 deletions(-) diff --git a/inlang/source-code/sdk-v2/src/types/module.ts b/inlang/source-code/sdk-v2/src/types/module.ts index 21a731ac4b..d6224a2a44 100644 --- a/inlang/source-code/sdk-v2/src/types/module.ts +++ b/inlang/source-code/sdk-v2/src/types/module.ts @@ -1,22 +1,20 @@ import type { ProjectSettings2 } from "./project-settings.js" import type { ImportFunction } from "../resolve-modules/import.js" import { Plugin2, type ResolvePlugins2Function, type ResolvedPlugin2Api } from "./plugin.js" -import { MessageBundleLintRule } from "./lint.js" import type { ModuleHasNoExportsError, ModuleImportError } from "./module-errors.js" -import type { resolveMessageBundleLintRules } from "../resolveMessageBundleLintRules.js" import { Type } from "@sinclair/typebox" /** - * An inlang module has a default export that is either a plugin or a message lint rule. + * An inlang plugin module has a default export that is a plugin. * * @example * export default myPlugin */ // not using Static here because the type is not inferred correctly // due to type overwrites in modules. -export type InlangModule = { default: Plugin2 | MessageBundleLintRule } -export const InlangModule = Type.Object({ - default: Type.Union([Plugin2, MessageBundleLintRule]), +export type InlangPlugin = { default: Plugin2 } +export const InlangPlugin = Type.Object({ + default: Plugin2, }) /** @@ -47,16 +45,12 @@ export type ResolveModule2Function = (args: { /** * The resolved item id of the module. */ - id: Plugin2["id"] | MessageBundleLintRule["id"] + id: Plugin2["id"] }> /** * The resolved plugins. */ plugins: Array - /** - * The resolved message lint rules. - */ - messageBundleLintRules: Array /** * The resolved api provided by plugins. */ @@ -67,13 +61,11 @@ export type ResolveModule2Function = (args: { * This includes errors from: * - importing module * - resolving plugins - * - resolving lint rules * - resolving the runtime plugin api */ errors: Array< | ModuleHasNoExportsError | ModuleImportError | Awaited>["errors"][number] - | Awaited>["errors"][number] > }> diff --git a/inlang/source-code/sdk-v2/src/types/plugin.ts b/inlang/source-code/sdk-v2/src/types/plugin.ts index 98aded75b1..692027a91d 100644 --- a/inlang/source-code/sdk-v2/src/types/plugin.ts +++ b/inlang/source-code/sdk-v2/src/types/plugin.ts @@ -25,6 +25,12 @@ import type { import { NestedBundle } from "./schema.js" import { TranslationFile, type TranslationFile as TranslationFileType } from "./translation-file.js" +export const PluginId = Type.String({ + pattern: "^plugin\\.([a-z][a-zA-Z0-9]*)\\.([a-z][a-zA-Z0-9]*(?:[A-Z][a-z0-9]*)*)$", + examples: ["plugin.namespace.id"], +}) as unknown as TTemplateLiteral<[TLiteral<`plugin.${string}.${string}`>]> +export type PluginId = Static + // ---------------------------- RUNTIME VALIDATION TYPES --------------------------------------------- /** @@ -33,13 +39,13 @@ import { TranslationFile, type TranslationFile as TranslationFileType } from "./ * You can use your own settings by extending the plugin with a generic: * * ```ts - * type PluginSettings = { - * filePath: string - * } + * type PluginSettings = { + * filePath: string + * } * - * const plugin: Plugin<{ - * "plugin.your.id": PluginSettings - * }> + * const plugin: Plugin<{ + * "plugin.your.id": PluginSettings + * }> * ``` */ export type Plugin2< @@ -67,9 +73,9 @@ export type Plugin2< * * @example * addCustomApi: () => ({ - * "app.inlang.ide-extension": { - * messageReferenceMatcher: () => {} - * } + * "app.inlang.ide-extension": { + * messageReferenceMatcher: () => {} + * } * }) */ addCustomApi?: (args: { @@ -80,14 +86,11 @@ export type Plugin2< } export const Plugin2 = Type.Object({ - id: Type.String({ - pattern: "^plugin\\.([a-z][a-zA-Z0-9]*)\\.([a-z][a-zA-Z0-9]*(?:[A-Z][a-z0-9]*)*)$", - examples: ["plugin.namespace.id"], - }) as unknown as TTemplateLiteral<[TLiteral<`plugin.${string}.${string}`>]>, + id: PluginId, displayName: Translatable(Type.String()), description: Translatable(Type.String()), /** - * Tyepbox must be used to validate the Json Schema. + * Typebox must be used to validate the Json Schema. * Github discussion to upvote a plain Json Schema validator and read the benefits of Typebox * https://github.com/opral/monorepo/discussions/1503 */ @@ -144,25 +147,25 @@ export type ResolvePlugins2Function = (args: { */ export type ResolvedPlugin2Api = { /** - * Importer / Exporter functions. + * * Importer / Exporter functions. * see https://linear.app/opral/issue/MESDK-157/sdk-v2-release-on-sqlite */ - toBeImportedFiles: Array - importFiles: Array - exportFiles: Array + toBeImportedFiles: Record + importFiles: Record + exportFiles: Record /** * App specific APIs. * * @example * // define * customApi: ({ settings }) => ({ - * "app.inlang.ide-extension": { - * messageReferenceMatcher: () => { - * // use settings - * settings.pathPattern - * return - * } - * } + * "app.inlang.ide-extension": { + * messageReferenceMatcher: () => { + * // use settings + * settings.pathPattern + * return + * } + * } * }) * // use * customApi['app.inlang.ide-extension'].messageReferenceMatcher()