feat: 🎸 add runtime typings

 Closes: Closes #43
This commit is contained in:
Christian Kaisermann 2020-01-23 10:11:04 -03:00
parent 107eec0e89
commit dadeaa2e7f
26 changed files with 49 additions and 101 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
node_modules node_modules
*.log *.log
dist/ dist/
coverage/ coverage/
types/

View File

@ -1,8 +1,9 @@
{ {
"name": "svelte-i18n", "name": "svelte-i18n",
"version": "2.2.4", "version": "2.2.4",
"main": "dist/i18n.cjs.js", "main": "dist/runtime.cjs.js",
"module": "dist/i18n.esm.js", "module": "dist/runtime.esm.js",
"types": "types/runtime/index.d.ts",
"bin": { "bin": {
"svelte-i18n": "dist/cli.js" "svelte-i18n": "dist/cli.js"
}, },
@ -21,21 +22,26 @@
"node": ">= 11.15.0" "node": ">= 11.15.0"
}, },
"scripts": { "scripts": {
"clean": "rm -rf dist/ types/",
"build": "rollup -c", "build": "rollup -c",
"build:types": "tsc -p src/runtime --emitDeclarationOnly",
"dev": "rollup -c -w", "dev": "rollup -c -w",
"pretest": "npm run build",
"test": "jest", "test": "jest",
"test:ci": "jest --silent", "test:ci": "jest --silent",
"test:watch": "jest --verbose --watchAll", "test:watch": "jest --verbose --watchAll",
"lint": "eslint \"src/**/*.ts\"", "lint": "eslint \"src/**/*.ts\"",
"format": "prettier --loglevel silent --write \"src/**/*.ts\" && eslint --fix \"src/**/*.ts\"", "format": "prettier --loglevel silent --write \"src/**/*.ts\" && eslint --fix \"src/**/*.ts\"",
"prerelease": "conventional-changelog -p angular -i CHANGELOG.md -s -r 1 && git add CHANGELOG.md",
"release": " git add package.json && git commit -m \"chore(release): v$npm_package_version :tada:\"", "release": " git add package.json && git commit -m \"chore(release): v$npm_package_version :tada:\"",
"pretest": "npm run build",
"prebuild": "yarn clean",
"postbuild": "yarn build:types",
"prerelease": "conventional-changelog -p angular -i CHANGELOG.md -s -r 1 && git add CHANGELOG.md",
"postrelease": "git tag -a v$npm_package_version -m 'Release v$npm_package_version'", "postrelease": "git tag -a v$npm_package_version -m 'Release v$npm_package_version'",
"prepublishOnly": "run-s test:ci build release" "prepublishOnly": "run-s test:ci build release"
}, },
"files": [ "files": [
"dist/" "dist/",
"types/"
], ],
"jest": { "jest": {
"collectCoverage": true, "collectCoverage": true,

View File

@ -9,7 +9,7 @@ const PROD = !process.env.ROLLUP_WATCH
export default [ export default [
{ {
input: 'src/client/index.ts', input: 'src/runtime/index.ts',
external: [ external: [
...Object.keys(pkg.dependencies), ...Object.keys(pkg.dependencies),
...Object.keys(pkg.peerDependencies), ...Object.keys(pkg.peerDependencies),

View File

@ -1,66 +0,0 @@
import { Formats } from 'intl-messageformat'
export interface DeepDictionary {
[key: string]: DeepDictionary | string | string[]
}
export type LocaleDictionary = Record<string, string>
export type Dictionary = Record<string, LocaleDictionary>
export interface MessageObject {
id?: string
locale?: string
format?: string
default?: string
values?: Record<string, string | number | Date>
}
interface FormatterFn {
(id: string | MessageObject, options?: MessageObject): string
}
type IntlFormatterOptions<T> = T & {
format?: string
locale?: string
}
export interface MemoizedIntlFormatter<T, U> {
(options?: IntlFormatterOptions<U>): T
}
export interface Formatter extends FormatterFn {
time: (
d: Date | number,
options?: IntlFormatterOptions<Intl.DateTimeFormatOptions>
) => string
date: (
d: Date | number,
options?: IntlFormatterOptions<Intl.DateTimeFormatOptions>
) => string
number: (
d: number,
options?: IntlFormatterOptions<Intl.NumberFormatOptions>
) => string
capital: FormatterFn
title: FormatterFn
upper: FormatterFn
lower: FormatterFn
}
export interface MessagesLoader {
(): Promise<any>
}
export interface GetClientLocaleOptions {
navigator?: boolean
hash?: string
search?: string
pathname?: RegExp
hostname?: RegExp
}
export interface ConfigureOptions {
fallbackLocale: string
initialLocale?: string | GetClientLocaleOptions
formats?: Partial<Formats>
loadingDelay?: number
}

View File

@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"declaration": true,
"declarationDir": "../../types/runtime"
},
"include": ["."]
}

View File

@ -5,8 +5,8 @@ import {
getOptions, getOptions,
defaultOptions, defaultOptions,
defaultFormats, defaultFormats,
} from '../../src/client/configs' } from '../../src/runtime/configs'
import { $locale } from '../../src/client/stores/locale' import { $locale } from '../../src/runtime/stores/locale'
beforeEach(() => { beforeEach(() => {
init(defaultOptions) init(defaultOptions)

View File

@ -4,7 +4,7 @@ import {
getTimeFormatter, getTimeFormatter,
getMessageFormatter, getMessageFormatter,
init init
} from '../../../src/client' } from '../../../src/runtime'
beforeEach(() => { beforeEach(() => {
init({ fallbackLocale: undefined }) init({ fallbackLocale: undefined })

View File

@ -5,10 +5,10 @@ import {
flush, flush,
registerLocaleLoader, registerLocaleLoader,
resetQueues, resetQueues,
} from '../../../src/client/includes/loaderQueue' } from '../../../src/runtime/includes/loaderQueue'
import { getMessageFromDictionary } from '../../../src/client/stores/dictionary' import { getMessageFromDictionary } from '../../../src/runtime/stores/dictionary'
import { $isLoading } from '../../../src/client/stores/loading' import { $isLoading } from '../../../src/runtime/stores/loading'
import { getOptions } from '../../../src/client/configs' import { getOptions } from '../../../src/runtime/configs'
beforeEach(() => { beforeEach(() => {
resetQueues() resetQueues()

View File

@ -1,5 +1,5 @@
import { lookup, lookupCache } from '../../../src/client/includes/lookup' import { lookup, lookupCache } from '../../../src/runtime/includes/lookup'
import { $dictionary, addMessages } from '../../../src/client/stores/dictionary' import { $dictionary, addMessages } from '../../../src/runtime/stores/dictionary'
beforeEach(() => { beforeEach(() => {
$dictionary.set({}) $dictionary.set({})

View File

@ -5,7 +5,7 @@ import {
upper, upper,
lower, lower,
flatObj, flatObj,
} from '../../../src/client/includes/utils' } from '../../../src/runtime/includes/utils'
describe('getting client locale', () => { describe('getting client locale', () => {
beforeEach(() => { beforeEach(() => {

View File

@ -1,11 +1,11 @@
import { defineMessages, waitLocale, register, init } from '../../src/client' import { defineMessages, waitLocale, register, init } from '../../src/runtime'
import { $locale } from '../../src/client/stores/locale' import { $locale } from '../../src/runtime/stores/locale'
import { hasLocaleQueue } from '../../src/client/includes/loaderQueue' import { hasLocaleQueue } from '../../src/runtime/includes/loaderQueue'
import { import {
getLocaleDictionary, getLocaleDictionary,
$dictionary, $dictionary,
} from '../../src/client/stores/dictionary' } from '../../src/runtime/stores/dictionary'
import { $format } from '../../src/client/stores/format' import { $format } from '../../src/runtime/stores/format'
test('defineMessages returns the identity of its first argument', () => { test('defineMessages returns the identity of its first argument', () => {
const obj = {} const obj = {}

View File

@ -9,7 +9,7 @@ import {
$dictionary, $dictionary,
$locales, $locales,
getLocaleDictionary, getLocaleDictionary,
} from '../../../src/client/stores/dictionary' } from '../../../src/runtime/stores/dictionary'
beforeEach(() => { beforeEach(() => {
$dictionary.set({}) $dictionary.set({})

View File

@ -1,8 +1,8 @@
import { Formatter } from '../../../src/client/types/index' import { Formatter } from '../../../src/runtime/types/index'
import { $format } from '../../../src/client/stores/format' import { $format } from '../../../src/runtime/stores/format'
import { init } from '../../../src/client/configs' import { init } from '../../../src/runtime/configs'
import { addMessages } from '../../../src/client/stores/dictionary' import { addMessages } from '../../../src/runtime/stores/dictionary'
import { $locale } from '../../../src/client/stores/locale' import { $locale } from '../../../src/runtime/stores/locale'
let format: Formatter let format: Formatter
$format.subscribe(f => (format = f)) $format.subscribe(f => (format = f))

View File

@ -1,6 +1,6 @@
import { get } from 'svelte/store' import { get } from 'svelte/store'
import { lookup } from '../../../src/client/includes/lookup' import { lookup } from '../../../src/runtime/includes/lookup'
import { import {
isFallbackLocaleOf, isFallbackLocaleOf,
getFallbackOf, getFallbackOf,
@ -8,10 +8,10 @@ import {
getCurrentLocale, getCurrentLocale,
$locale, $locale,
isRelatedLocale, isRelatedLocale,
} from '../../../src/client/stores/locale' } from '../../../src/runtime/stores/locale'
import { getOptions, init } from '../../../src/client/configs' import { getOptions, init } from '../../../src/runtime/configs'
import { register } from '../../../src/client' import { register } from '../../../src/runtime'
import { hasLocaleQueue } from '../../../src/client/includes/loaderQueue' import { hasLocaleQueue } from '../../../src/runtime/includes/loaderQueue'
beforeEach(() => { beforeEach(() => {
init({ fallbackLocale: undefined }) init({ fallbackLocale: undefined })

View File

@ -11,6 +11,5 @@
"lib": ["es2018", "dom", "esnext"], "lib": ["es2018", "dom", "esnext"],
"outDir": "dist", "outDir": "dist",
"types": ["svelte", "jest"] "types": ["svelte", "jest"]
}, }
"exclude": ["node_modules/**/*", "dist"]
} }