From fc5ad7a2e78d7cdbcc969447396e708aa7a2bcfb Mon Sep 17 00:00:00 2001 From: Mirko Schaefer Date: Tue, 10 Dec 2024 12:01:16 +0100 Subject: [PATCH 1/2] feat: add typings for cds module. --- apis/facade.d.ts | 1 + apis/i18n.d.ts | 46 ++++++++++++++++++++++++ test/typescript/apis/project/cds-i18n.ts | 35 ++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 apis/i18n.d.ts create mode 100644 test/typescript/apis/project/cds-i18n.ts diff --git a/apis/facade.d.ts b/apis/facade.d.ts index 366cebd5..3eaa72a1 100644 --- a/apis/facade.d.ts +++ b/apis/facade.d.ts @@ -7,6 +7,7 @@ export * from './events' export * from './utils' export * from './cqn' export * from './global' +export * from './i18n' export { log, debug } from './log' export { test } from './test' diff --git a/apis/i18n.d.ts b/apis/i18n.d.ts new file mode 100644 index 00000000..e4e560e1 --- /dev/null +++ b/apis/i18n.d.ts @@ -0,0 +1,46 @@ +import { csn, entity } from './cds' + + +declare class I18nBundle { + constructor(options: I18nFilesOptions) + for(key: number | string | object, locale?: string | object, args?: object): string | undefined + at(key: number | string | object, locale?: string | object, args?: object): string | undefined + file: string + files: I18nFiles + get defaults(): Record + get fallback(): Record + key4(definition: entity): string + texts4 (locale: string): Texts + translations4 (...locales : 'all' | string[]) : { [locale]: Texts } +} + +declare class I18nFacade { + Bundle: typeof I18nBundle + Facade: typeof I18nFacade + Files: typeof I18nFiles + get file(): string + get folders(): string[] + get labels(): I18nBundle + get messages(): I18nBundle + + bundle4 (file: string, options?: I18nFilesOptions): I18nBundle + bundle4 (model: csn.CSN): I18nBundle + +} + +interface I18nFilesOptions { + file? : string + model? : csn.CSN + roots? : string[] + leafs? : string[] + folders? : string[] +} + +declare class I18nFiles { + constructor (options: I18nFilesOptions) + get options(): I18nFilesOptions + get basename(): string + content4(locale: string, suffix: string): Array +} + +export declare const i18n: I18nFacade \ No newline at end of file diff --git a/test/typescript/apis/project/cds-i18n.ts b/test/typescript/apis/project/cds-i18n.ts new file mode 100644 index 00000000..0761d9f3 --- /dev/null +++ b/test/typescript/apis/project/cds-i18n.ts @@ -0,0 +1,35 @@ +import cds, { i18n } from "@sap/cds"; +import { testType } from "./dummy"; +const model = cds.reflect({}); +const { Book: Books } = model.entities; + +testType(i18n.labels.for("foo")); +testType(i18n.labels.at("foo")); +i18n.labels.at("foo", "bar"); +i18n.labels.at("foo", "bar", [123, "baz"]); +i18n.labels.at("foo", [123, "baz"]); +i18n.labels.file = ""; +// @ts-expect-error +i18n.labels.defaults = { + foo: "bar", +}; +// @ts-expect-error +i18n.labels.fallback = { + foo: "bar", +}; + +testType(i18n.messages.for("foo")); +testType(i18n.messages.at("foo")); +i18n.messages.at("foo", "bar"); +i18n.messages.at("foo", "bar", [123, "baz"]); +i18n.messages.at("foo", [123, "baz"]); + +i18n.bundle4("foo", { file: "bar" }); +i18n.labels.texts4("de"); +i18n.labels.translations4("de"); +i18n.labels.key4(Books); +i18n.messages.texts4("de"); +i18n.messages.translations4("de"); +i18n.messages.key4(Books); +cds.i18n.labels.for("foo"); +cds.i18n === i18n; From 3e41d5daa71e46ddcb112457b81fb45faa7315fa Mon Sep 17 00:00:00 2001 From: Mirko Schaefer Date: Sun, 15 Dec 2024 14:39:59 +0100 Subject: [PATCH 2/2] fix: implemented suggestions. --- apis/i18n.d.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/apis/i18n.d.ts b/apis/i18n.d.ts index e4e560e1..3ccd0299 100644 --- a/apis/i18n.d.ts +++ b/apis/i18n.d.ts @@ -1,20 +1,22 @@ import { csn, entity } from './cds' +type Texts = Record declare class I18nBundle { constructor(options: I18nFilesOptions) for(key: number | string | object, locale?: string | object, args?: object): string | undefined at(key: number | string | object, locale?: string | object, args?: object): string | undefined - file: string files: I18nFiles get defaults(): Record get fallback(): Record key4(definition: entity): string texts4 (locale: string): Texts - translations4 (...locales : 'all' | string[]) : { [locale]: Texts } + translations4 (...locales : string[]) : { [locale: string]: Texts } + translations4 (locale : 'all' ) : { [locale: string]: Texts } + all () : Record } -declare class I18nFacade { +declare interface I18nFacade { Bundle: typeof I18nBundle Facade: typeof I18nFacade Files: typeof I18nFiles @@ -25,7 +27,6 @@ declare class I18nFacade { bundle4 (file: string, options?: I18nFilesOptions): I18nBundle bundle4 (model: csn.CSN): I18nBundle - } interface I18nFilesOptions {