Skip to content

Commit 1663f5e

Browse files
committed
refactor: shared modules
1 parent 1d07768 commit 1663f5e

File tree

11 files changed

+206
-259
lines changed

11 files changed

+206
-259
lines changed

packages/h3/spec/e2e.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { spawn } from 'node:child_process'
22
import path from 'node:path'
33
import { afterEach, describe, expect, test } from 'vitest'
4-
import { delay, runCommand } from '../../shared/helper.ts'
4+
import { delay, runCommand } from '../../shared/src/index.ts'
55

66
let serve: ReturnType<typeof spawn> | null = null
77

packages/h3/spec/integration.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { eventHandler, H3 } from 'h3'
22
import { afterEach, describe, expect, test, vi } from 'vitest'
3-
import { delay as sleep } from '../../shared/helper.ts'
3+
import { delay as sleep } from '../../shared/src/index.ts'
44

55
import {
66
detectLocaleFromAcceptLanguageHeader,

packages/h3/src/index.ts

Lines changed: 2 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,11 @@ import type {
4646
LocaleDetector,
4747
LocaleMessage,
4848
LocaleParams,
49-
NamedValue,
50-
PickupPaths,
51-
RemovedIndexResources,
5249
RemoveIndexSignature,
53-
SchemaParams,
54-
TranslateOptions
50+
SchemaParams
5551
} from '@intlify/core'
5652
import type { H3Event, H3EventContext, Middleware } from 'h3'
53+
import type { TranslationFunction } from '../../shared/src/types.ts'
5754

5855
declare module 'h3' {
5956
interface H3EventContext {
@@ -264,124 +261,6 @@ export const detectLocaleFromAcceptLanguageHeader = (event: H3Event): Locale =>
264261
*/
265262
export interface DefineLocaleMessage extends LocaleMessage<string> {}
266263

267-
type ResolveResourceKeys<
268-
Schema extends Record<string, any> = {}, // eslint-disable-line @typescript-eslint/no-explicit-any -- NOTE(kazupon): generic type
269-
DefineLocaleMessageSchema extends Record<string, any> = {}, // eslint-disable-line @typescript-eslint/no-explicit-any -- NOTE(kazupon): generic type
270-
DefinedLocaleMessage extends
271-
RemovedIndexResources<DefineLocaleMessageSchema> = RemovedIndexResources<DefineLocaleMessageSchema>,
272-
SchemaPaths = IsEmptyObject<Schema> extends false
273-
? PickupPaths<{ [K in keyof Schema]: Schema[K] }>
274-
: never,
275-
DefineMessagesPaths = IsEmptyObject<DefinedLocaleMessage> extends false
276-
? PickupPaths<{
277-
[K in keyof DefinedLocaleMessage]: DefinedLocaleMessage[K]
278-
}>
279-
: never
280-
> = SchemaPaths | DefineMessagesPaths
281-
282-
/**
283-
* The translation function, which will be defined by {@link useTranslation}.
284-
*/
285-
interface TranslationFunction<
286-
Schema extends Record<string, any> = {}, // eslint-disable-line @typescript-eslint/no-explicit-any -- NOTE(kazupon): generic type
287-
DefineLocaleMessageSchema extends Record<string, any> = {}, // eslint-disable-line @typescript-eslint/no-explicit-any -- NOTE(kazupon): generic type
288-
ResourceKeys = ResolveResourceKeys<Schema, DefineLocaleMessageSchema>
289-
> {
290-
/**
291-
* @param {Key | ResourceKeys} key - A translation key
292-
* @returns {string} A translated message, if the key is not found, return the key
293-
*/
294-
<Key extends string>(key: Key | ResourceKeys): string
295-
/**
296-
* @param {Key | ResourceKeys} key - A translation key
297-
* @param {number} plural - A plural choice number
298-
* @returns {string} A translated message, if the key is not found, return the key
299-
*/
300-
<Key extends string>(key: Key | ResourceKeys, plural: number): string
301-
/**
302-
* @param {Key | ResourceKeys} key - A translation key
303-
* @param {number} plural - A plural choice number
304-
* @param {TranslateOptions} options - A translate options, about details see {@link TranslateOptions}
305-
* @returns {string} A translated message, if the key is not found, return the key
306-
*/
307-
<Key extends string>(key: Key | ResourceKeys, plural: number, options: TranslateOptions): string
308-
/**
309-
* @param {Key | ResourceKeys} key - A translation key
310-
* @param {string} defaultMsg - A default message, if the key is not found
311-
* @returns {string} A translated message, if the key is not found, return the `defaultMsg` argument
312-
*/
313-
<Key extends string>(key: Key | ResourceKeys, defaultMsg: string): string
314-
/**
315-
* @param {Key | ResourceKeys} key - A translation key
316-
* @param {string} defaultMsg - A default message, if the key is not found
317-
* @param {TranslateOptions} options - A translate options, about details see {@link TranslateOptions}
318-
* @returns {string} A translated message, if the key is not found, return the `defaultMsg` argument
319-
*/
320-
<Key extends string>(
321-
key: Key | ResourceKeys,
322-
defaultMsg: string,
323-
options: TranslateOptions
324-
): string
325-
/**
326-
* @param {Key | ResourceKeys} key - A translation key
327-
* @param {unknown[]} list - A list for list interpolation
328-
* @returns {string} A translated message, if the key is not found, return the key
329-
*/
330-
<Key extends string>(key: Key | ResourceKeys, list: unknown[]): string
331-
/**
332-
* @param {Key | ResourceKeys} key - A translation key
333-
* @param {unknown[]} list - A list for list interpolation
334-
* @param {number} plural - A plural choice number
335-
* @returns {string} A translated message, if the key is not found, return the key
336-
*/
337-
<Key extends string>(key: Key | ResourceKeys, list: unknown[], plural: number): string
338-
/**
339-
* @param {Key | ResourceKeys} key - A translation key
340-
* @param {unknown[]} list - A list for list interpolation
341-
* @param {string} defaultMsg - A default message, if the key is not found
342-
* @returns {string} A translated message, if the key is not found, return the `defaultMsg` argument
343-
*/
344-
<Key extends string>(key: Key | ResourceKeys, list: unknown[], defaultMsg: string): string
345-
/**
346-
* @param {Key | ResourceKeys} key - A translation key
347-
* @param {unknown[]} list - A list for list interpolation
348-
* @param {TranslateOptions} options - A translate options, about details see {@link TranslateOptions}
349-
* @returns {string} A translated message, if the key is not found, return the key
350-
*/
351-
<Key extends string>(key: Key | ResourceKeys, list: unknown[], options: TranslateOptions): string
352-
/**
353-
* @param {Key | ResourceKeys} key - A translation key
354-
* @param {NamedValue} named - A named value for named interpolation
355-
* @returns {string} A translated message, if the key is not found, return the key
356-
*/
357-
<Key extends string>(key: Key | ResourceKeys, named: NamedValue): string
358-
/**
359-
* @param {Key | ResourceKeys} key - A translation key
360-
* @param {NamedValue} named - A named value for named interpolation
361-
* @param {number} plural - A plural choice number
362-
* @returns {string} A translated message, if the key is not found, return the key
363-
*/
364-
<Key extends string>(key: Key | ResourceKeys, named: NamedValue, plural: number): string
365-
/**
366-
* @param {Key | ResourceKeys} key - A translation key
367-
* @param {NamedValue} named - A named value for named interpolation
368-
* @param {string} defaultMsg - A default message, if the key is not found
369-
* @returns {string} A translated message, if the key is not found, return the `defaultMsg` argument
370-
*/
371-
<Key extends string>(key: Key | ResourceKeys, named: NamedValue, defaultMsg: string): string
372-
/**
373-
* @param {Key | ResourceKeys} key - A translation key
374-
* @param {NamedValue} named - A named value for named interpolation
375-
* @param {TranslateOptions} options - A translate options, about details see {@link TranslateOptions}
376-
* @returns {string} A translated message, if the key is not found, return the key
377-
*/
378-
<Key extends string>(
379-
key: Key | ResourceKeys,
380-
named: NamedValue,
381-
options: TranslateOptions
382-
): string
383-
}
384-
385264
/**
386265
* Use translation function in event handler
387266
*

packages/hono/spec/e2e.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { spawn } from 'node:child_process'
22
import path from 'node:path'
33
import { afterEach, describe, expect, test } from 'vitest'
4-
import { delay, runCommand } from '../../shared/helper.ts'
4+
import { delay, runCommand } from '../../shared/src/index.ts'
55

66
let serve: ReturnType<typeof spawn> | null = null
77

packages/hono/spec/integration.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Hono } from 'hono'
22
import { afterEach, describe, expect, test, vi } from 'vitest'
3-
import { delay as sleep } from '../../shared/helper.ts'
3+
import { delay as sleep } from '../../shared/src/index.ts'
44
import {
55
defineI18nMiddleware,
66
detectLocaleFromAcceptLanguageHeader,

packages/hono/src/index.ts

Lines changed: 2 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,11 @@ import type {
4343
LocaleDetector,
4444
LocaleMessage,
4545
LocaleParams,
46-
NamedValue,
47-
PickupPaths,
48-
RemovedIndexResources,
4946
RemoveIndexSignature,
50-
SchemaParams,
51-
TranslateOptions
47+
SchemaParams
5248
} from '@intlify/core'
5349
import type { Context, MiddlewareHandler, Next } from 'hono'
50+
import type { TranslationFunction } from '../../shared/src/types.ts'
5451

5552
declare module 'hono' {
5653
interface ContextVariableMap {
@@ -196,122 +193,6 @@ export function defineI18nMiddleware<
196193
export const detectLocaleFromAcceptLanguageHeader = (ctx: Context): Locale =>
197194
getHeaderLocale(ctx.req.raw).toString()
198195

199-
type ResolveResourceKeys<
200-
Schema extends Record<string, any> = {}, // eslint-disable-line @typescript-eslint/no-explicit-any -- NOTE(kazupon): generic type
201-
DefineLocaleMessageSchema extends Record<string, any> = {}, // eslint-disable-line @typescript-eslint/no-explicit-any -- NOTE(kazupon): generic type
202-
DefinedLocaleMessage extends
203-
RemovedIndexResources<DefineLocaleMessageSchema> = RemovedIndexResources<DefineLocaleMessageSchema>,
204-
SchemaPaths = IsEmptyObject<Schema> extends false
205-
? PickupPaths<{ [K in keyof Schema]: Schema[K] }>
206-
: never,
207-
DefineMessagesPaths = IsEmptyObject<DefinedLocaleMessage> extends false
208-
? PickupPaths<{ [K in keyof DefinedLocaleMessage]: DefinedLocaleMessage[K] }>
209-
: never
210-
> = SchemaPaths | DefineMessagesPaths
211-
212-
/**
213-
* The translation function, which will be defined by {@link useTranslation}.
214-
*/
215-
interface TranslationFunction<
216-
Schema extends Record<string, any> = {}, // eslint-disable-line @typescript-eslint/no-explicit-any -- NOTE(kazupon): generic type
217-
DefineLocaleMessageSchema extends Record<string, any> = {}, // eslint-disable-line @typescript-eslint/no-explicit-any -- NOTE(kazupon): generic type
218-
ResourceKeys = ResolveResourceKeys<Schema, DefineLocaleMessageSchema>
219-
> {
220-
/**
221-
* @param {Key | ResourceKeys} key - A translation key
222-
* @returns {string} A translated message, if the key is not found, return the key
223-
*/
224-
<Key extends string>(key: Key | ResourceKeys): string
225-
/**
226-
* @param {Key | ResourceKeys} key - A translation key
227-
* @param {number} plural - A plural choice number
228-
* @returns {string} A translated message, if the key is not found, return the key
229-
*/
230-
<Key extends string>(key: Key | ResourceKeys, plural: number): string
231-
/**
232-
* @param {Key | ResourceKeys} key - A translation key
233-
* @param {number} plural - A plural choice number
234-
* @param {TranslateOptions} options - A translate options, about details see {@link TranslateOptions}
235-
* @returns {string} A translated message, if the key is not found, return the key
236-
*/
237-
<Key extends string>(key: Key | ResourceKeys, plural: number, options: TranslateOptions): string
238-
/**
239-
* @param {Key | ResourceKeys} key - A translation key
240-
* @param {string} defaultMsg - A default message, if the key is not found
241-
* @returns {string} A translated message, if the key is not found, return the `defaultMsg` argument
242-
*/
243-
<Key extends string>(key: Key | ResourceKeys, defaultMsg: string): string
244-
/**
245-
* @param {Key | ResourceKeys} key - A translation key
246-
* @param {string} defaultMsg - A default message, if the key is not found
247-
* @param {TranslateOptions} options - A translate options, about details see {@link TranslateOptions}
248-
* @returns {string} A translated message, if the key is not found, return the `defaultMsg` argument
249-
*/
250-
<Key extends string>(
251-
key: Key | ResourceKeys,
252-
defaultMsg: string,
253-
options: TranslateOptions
254-
): string
255-
/**
256-
* @param {Key | ResourceKeys} key - A translation key
257-
* @param {unknown[]} list - A list for list interpolation
258-
* @returns {string} A translated message, if the key is not found, return the key
259-
*/
260-
<Key extends string>(key: Key | ResourceKeys, list: unknown[]): string
261-
/**
262-
* @param {Key | ResourceKeys} key - A translation key
263-
* @param {unknown[]} list - A list for list interpolation
264-
* @param {number} plural - A plural choice number
265-
* @returns {string} A translated message, if the key is not found, return the key
266-
*/
267-
<Key extends string>(key: Key | ResourceKeys, list: unknown[], plural: number): string
268-
/**
269-
* @param {Key | ResourceKeys} key - A translation key
270-
* @param {unknown[]} list - A list for list interpolation
271-
* @param {string} defaultMsg - A default message, if the key is not found
272-
* @returns {string} A translated message, if the key is not found, return the `defaultMsg` argument
273-
*/
274-
<Key extends string>(key: Key | ResourceKeys, list: unknown[], defaultMsg: string): string
275-
/**
276-
* @param {Key | ResourceKeys} key - A translation key
277-
* @param {unknown[]} list - A list for list interpolation
278-
* @param {TranslateOptions} options - A translate options, about details see {@link TranslateOptions}
279-
* @returns {string} A translated message, if the key is not found, return the key
280-
*/
281-
<Key extends string>(key: Key | ResourceKeys, list: unknown[], options: TranslateOptions): string
282-
/**
283-
* @param {Key | ResourceKeys} key - A translation key
284-
* @param {NamedValue} named - A named value for named interpolation
285-
* @returns {string} A translated message, if the key is not found, return the key
286-
*/
287-
<Key extends string>(key: Key | ResourceKeys, named: NamedValue): string
288-
/**
289-
* @param {Key | ResourceKeys} key - A translation key
290-
* @param {NamedValue} named - A named value for named interpolation
291-
* @param {number} plural - A plural choice number
292-
* @returns {string} A translated message, if the key is not found, return the key
293-
*/
294-
<Key extends string>(key: Key | ResourceKeys, named: NamedValue, plural: number): string
295-
/**
296-
* @param {Key | ResourceKeys} key - A translation key
297-
* @param {NamedValue} named - A named value for named interpolation
298-
* @param {string} defaultMsg - A default message, if the key is not found
299-
* @returns {string} A translated message, if the key is not found, return the `defaultMsg` argument
300-
*/
301-
<Key extends string>(key: Key | ResourceKeys, named: NamedValue, defaultMsg: string): string
302-
/**
303-
* @param {Key | ResourceKeys} key - A translation key
304-
* @param {NamedValue} named - A named value for named interpolation
305-
* @param {TranslateOptions} options - A translate options, about details see {@link TranslateOptions}
306-
* @returns {string} A translated message, if the key is not found, return the key
307-
*/
308-
<Key extends string>(
309-
key: Key | ResourceKeys,
310-
named: NamedValue,
311-
options: TranslateOptions
312-
): string
313-
}
314-
315196
/**
316197
* use translation function in event handler
317198
*

packages/shared/package.json

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"name": "@intlify/srvmid-shared",
3+
"private": true,
4+
"version": "2.0.0-alpha.3",
5+
"author": {
6+
"name": "kazuya kawaguchi",
7+
"email": "kawakazu80@gmail.com"
8+
},
9+
"license": "MIT",
10+
"funding": "https://github.com/sponsors/kazupon",
11+
"bugs": {
12+
"url": "https://github.com/intlify/srvmid/issues"
13+
},
14+
"repository": {
15+
"type": "git",
16+
"url": "git+https://github.com/intlify/srvmid.git",
17+
"directory": "packages/shared"
18+
},
19+
"engines": {
20+
"node": ">= 20"
21+
},
22+
"type": "module",
23+
"sideEffects": false,
24+
"files": [
25+
"dist"
26+
],
27+
"main": "dist/index.mjs",
28+
"module": "dist/index.mjs",
29+
"exports": {
30+
".": {
31+
"types": "./dist/index.d.mts",
32+
"import": "./dist/index.mjs"
33+
},
34+
"./dist/*": "./dist/*",
35+
"./package.json": "./package.json"
36+
},
37+
"types": "dist/index.d.ts",
38+
"typesVersions": {
39+
"*": {
40+
"*": [
41+
"./dist/*",
42+
"./*"
43+
]
44+
}
45+
},
46+
"dependencies": {
47+
"@intlify/core": "catalog:"
48+
},
49+
"devDependencies": {
50+
"@types/node": "catalog:"
51+
}
52+
}

packages/shared/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './types.ts'
2+
export * from './utils.ts'

0 commit comments

Comments
 (0)