From 1f65ec8868afca5a2e2b0ea7b76e681f6e50e58c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Marie=20De=20Mey?= Date: Thu, 16 May 2024 12:13:52 +0300 Subject: [PATCH] export fix + restruct --- README.md | 3 +- docs/README.md | 20 ----------- docs/interpolation.md | 4 +-- package.json | 30 ++++++++-------- src/client.ts | 29 ++------------- src/db/index.ts | 1 + src/db/memDb.ts | 3 +- src/db/types.ts | 75 +++++++++++++++++++++++++++++++++++++++ src/index.ts | 37 ++----------------- src/server/interactive.ts | 2 +- src/server/server.ts | 3 +- src/types.ts | 73 ------------------------------------- 12 files changed, 102 insertions(+), 178 deletions(-) create mode 100644 src/db/types.ts diff --git a/README.md b/README.md index 2a71e7d..f64b1d6 100644 --- a/README.md +++ b/README.md @@ -34,8 +34,6 @@ The server: The client part is a [`I18nClient`](./docs/client.md) that will remember a locale and manage the queries to a server and language changes. This client will produce `Translator`s who are described in typescript by the type `any`, or you can specify yours for your dictionary structure. -> :warning: The library has 2 entry points: `omni18n` and `omni18n/client`. Only load the latter in the browser. - ### Server side ```ts @@ -158,6 +156,7 @@ Or the [object-oriented way](./docs/client.md#oo-reporting) by overriding these missing(key: string, fallback: Translation | undefined, zones: Zone[]): string error(key: string, error: string, spec: object, zones: Zone[]): string ``` + ## Integrations - [Svelte4](https://github.com/eddow/omni18n-svelte4) diff --git a/docs/README.md b/docs/README.md index b110635..bca41d1 100644 --- a/docs/README.md +++ b/docs/README.md @@ -9,26 +9,6 @@ Projects using OmnI18n use it in 4 layers 3. [The `server`](./server.md): The server exposes functions to interact with the languages 4. [The `database`](./db.md): A class implementing some interface that interacts directly with a database -## Entry points - -The library has 2x2 entry points: - -client/server: The server functionalities are not needed **and** harmful on client-side (try to ask chrome to import `node:fs` ...) - -- The complete library `omni18n` -- The client part `omni18n/client` - -bundled/source: The sources (TypeScript) are provided so that you can use your favorite bundler/debugger - -- The bundled `omni18n` -- The source `omni18n/src` - -And of course `omni18n/src/client` for the 2x2... - -### umd - -On the client side, it is also possible to reference the file `lib/omni18n.js` statically in the HTML code, every functionality will be in the `OmnI18n` global variable. - ## Bonus ### Flags diff --git a/docs/interpolation.md b/docs/interpolation.md index 6e0be04..7791d7d 100644 --- a/docs/interpolation.md +++ b/docs/interpolation.md @@ -62,7 +62,7 @@ The syntax `{other.text.key | arg1 | arg2}` can be used to do such. The syntax also allow some processing specification, when a processor name (with no `.` in it) is used instead of a first element. The available processors can be extended : ```ts -import { processors, type TContext } from 'omni18n/client'; +import { processors, type TContext } from 'omni18n'; Object.assign(processors, { myProc(this: TContext, arg1: any, ...args: any[]) { @@ -96,7 +96,7 @@ example: `{upper | $1}` will render the first argument in upper-case A list of predefined options can be set in exported variables ```ts -import { formats } from 'omni18n/client' +import { formats } from 'omni18n' formats.date.year = { year: 'numeric' } formats.number.arabic = { numberingSystem: 'arab' } diff --git a/package.json b/package.json index da26c61..302754c 100644 --- a/package.json +++ b/package.json @@ -1,24 +1,22 @@ { "name": "omni18n", - "version": "1.1.6", + "version": "1.1.7", "exports": { - "./client": { - "import": "./lib/esm/client.js", - "require": "./lib/cjs/client.js", - "types": "./lib/src/client.d.ts" - }, ".": { - "import": "./lib/esm/index.js", - "require": "./lib/cjs/index.js", - "types": "./lib/src/index.d.ts" - }, - "./src/client": { - "import": "./src/client.ts", - "types": "./src/client.ts" + "browser": { + "import": "./lib/esm/client.js", + "require": "./lib/cjs/client.js", + "types": "./lib/src/client.d.ts" + }, + "default": { + "import": "./lib/esm/index.js", + "require": "./lib/cjs/index.js", + "types": "./lib/src/index.d.ts" + } }, - "./src": { - "import": "./src/index.ts", - "types": "./src/index.ts" + "./ts": { + "browser": "./src/client.ts", + "default": "./src/index.ts" } }, "description": "", diff --git a/src/client.ts b/src/client.ts index 4bdc1c1..9998e97 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,29 +1,6 @@ -export { - type CondensedDictionary, - type Condense, - type Locale, - type OnModification, - type TextKey, - type Translation, - type Zone, - type WorkDictionary, - type WorkDictionaryEntry, - type WorkDictionaryText -} from './types' -export { - I18nClient, - type TContext, - getContext, - TranslationError, - type ClientDictionary, - type Translator, - reports, - bulkObject, - bulkDictionary, - formats, - processors -} from './client/index' -export { localeFlags, flagCodeExceptions } from './flags' +export * from './types' +export * from './client/index' +export * from './flags' declare global { interface Set { diff --git a/src/db/index.ts b/src/db/index.ts index 3f92b35..ab7897c 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -1,2 +1,3 @@ export { default as MemDB, type MemDBDictionary, type MemDBDictionaryEntry } from './memDb' export { default as FileDB } from './fileDb' +export * from './types' diff --git a/src/db/memDb.ts b/src/db/memDb.ts index 1db0d01..511bce9 100644 --- a/src/db/memDb.ts +++ b/src/db/memDb.ts @@ -2,13 +2,12 @@ import { WorkDictionary, WorkDictionaryEntry, WorkDictionaryText, - type InteractiveDB, type Locale, - type RawDictionary, type TextKey, type Translation, type Zone } from '../types' +import { type InteractiveDB, type RawDictionary } from './types' interface SystemEntry { '.zone'?: Zone diff --git a/src/db/types.ts b/src/db/types.ts new file mode 100644 index 0000000..7a8245c --- /dev/null +++ b/src/db/types.ts @@ -0,0 +1,75 @@ +import { Locale, TextKey, Translation, WorkDictionary, Zone } from '../types' + +/** + * Dictionary used between the server and the DB + * key => [locale, text] + */ +export type RawDictionary = Record + +export interface DB { + /** + * Retrieves all the values for a certain zone + * @param locales A list of locales to search for + * @param zone The zone to search in + * @returns A dictionary of key => [locale, text], where locale is the first on the list that has a translation + */ + list(locales: Locale[], zone: Zone): Promise +} + +export interface TranslatableDB extends DB { + /** + * Retrieves all the values for certain locales, in order for translators to work on it + * @param locales + */ + workList(locales: Locale[]): Promise + + /** + * Modifies/add the value for the key [key, locale] + * Note: checks that the key exists + * @param key text key + * @param locale A language and perhaps a country + * @param text A text value + * @returns The zone where the key is stored or false if no change were brought + */ + modify( + key: TextKey, + locale: Locale, + text: Translation, + textInfos?: Partial + ): Promise +} + +export interface EditableDB + extends TranslatableDB { + /** + * Creates or modifies a key + * @param key The key to manipulate + * @param zone The zone to create the key in, or undefined to delete it + * @returns Wether the zone was changed + */ + key(key: TextKey, zone: Zone, keyInfos?: Partial): Promise + + /** + * Renames a key - or removes it (and its translation) if newKey is undefined + * @param key + * @param newKey + * @returns All the information about that key (actually useful on deletion) + */ + reKey(key: TextKey, newKey?: TextKey): Promise<{ zone: Zone; texts: Record }> +} + +export interface InteractiveDB + extends EditableDB { + /** + * Retrieves all the translations given for a certain key + * @param key The key to search for + */ + get(key: TextKey): Promise> + + /** + * Checks if a key is specified in a certain locale + * @param key The key to search for + * @param locales The locales to search in + */ + getZone(key: TextKey, locales?: Locale[]): Promise +} diff --git a/src/index.ts b/src/index.ts index 1ef3157..43dc079 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,34 +1,3 @@ -export { - type CondensedDictionary, - type Condense, - type Locale, - type OnModification, - type TextKey, - type Translation, - type Zone, - type WorkDictionary, - type WorkDictionaryEntry, - type WorkDictionaryText, - I18nClient, - type TContext, - getContext, - TranslationError, - type ClientDictionary, - type Translator, - reports, - bulkObject, - bulkDictionary, - formats, - processors, - localeFlags, - flagCodeExceptions -} from './client' -export { type RawDictionary, type InteractiveDB, type DB } from './types' -export { - I18nServer, - InteractiveServer, - type Modification, - specs2url, - url2specs -} from './server/index' -export { FileDB, MemDB, type MemDBDictionary, type MemDBDictionaryEntry } from './db/index' +export * from './client' +export * from './server' +export * from './db' diff --git a/src/server/interactive.ts b/src/server/interactive.ts index d00ba95..adddcea 100644 --- a/src/server/interactive.ts +++ b/src/server/interactive.ts @@ -1,12 +1,12 @@ import { WorkDictionary, type CondensedDictionary, - type InteractiveDB, type Locale, type TextKey, type Translation, type Zone } from '../types' +import { type InteractiveDB } from '../db' import I18nServer, { localeTree } from './server' const subscriptions = new Map< diff --git a/src/server/server.ts b/src/server/server.ts index 232edcf..d54a86b 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -1,8 +1,7 @@ +import { DB, RawDictionary } from '../db' import { - DB, type CondensedDictionary, type Locale, - type RawDictionary, type TextKey, type Translation, type Zone diff --git a/src/types.ts b/src/types.ts index 26c86cf..6be902c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -14,11 +14,6 @@ export type CondensedDictionary = { export type Condense = (locales: Locale[], zones: Zone[]) => Promise export type OnModification = (keys?: TextKey[]) => void -/** - * Dictionary used between the server and the DB - * key => [locale, text] - */ -export type RawDictionary = Record export type WorkDictionaryText = { text?: Translation infos?: TextInfos @@ -32,71 +27,3 @@ export type WorkDictionaryEntry - -export interface DB { - /** - * Retrieves all the values for a certain zone - * @param locales A list of locales to search for - * @param zone The zone to search in - * @returns A dictionary of key => [locale, text], where locale is the first on the list that has a translation - */ - list(locales: Locale[], zone: Zone): Promise -} - -export interface TranslatableDB extends DB { - /** - * Retrieves all the values for certain locales, in order for translators to work on it - * @param locales - */ - workList(locales: Locale[]): Promise - - /** - * Modifies/add the value for the key [key, locale] - * Note: checks that the key exists - * @param key text key - * @param locale A language and perhaps a country - * @param text A text value - * @returns The zone where the key is stored or false if no change were brought - */ - modify( - key: TextKey, - locale: Locale, - text: Translation, - textInfos?: Partial - ): Promise -} - -export interface EditableDB - extends TranslatableDB { - /** - * Creates or modifies a key - * @param key The key to manipulate - * @param zone The zone to create the key in, or undefined to delete it - * @returns Wether the zone was changed - */ - key(key: TextKey, zone: Zone, keyInfos?: Partial): Promise - - /** - * Renames a key - or removes it (and its translation) if newKey is undefined - * @param key - * @param newKey - * @returns All the information about that key (actually useful on deletion) - */ - reKey(key: TextKey, newKey?: TextKey): Promise<{ zone: Zone; texts: Record }> -} - -export interface InteractiveDB - extends EditableDB { - /** - * Retrieves all the translations given for a certain key - * @param key The key to search for - */ - get(key: TextKey): Promise> - - /** - * Checks if a key is specified in a certain locale - * @param key The key to search for - * @param locales The locales to search in - */ - getZone(key: TextKey, locales?: Locale[]): Promise -}