Skip to content

Commit 43875f6

Browse files
authored
feat: warning for h3 and hone utils (#70)
1 parent 81c8f1f commit 43875f6

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

src/h3.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
parseDefaultHeader,
2626
validateLocale
2727
} from './http.ts'
28+
import { warnOnce } from './utils.ts'
2829

2930
import type { H3Event } from 'h3'
3031
import type {
@@ -61,6 +62,10 @@ import type {
6162
* @returns The array of language tags, if you use `accept-language` header and `*` (any language) or empty string is detected, return an empty array.
6263
*/
6364
export function getHeaderLanguages(event: H3Event, options: HeaderOptions = {}): string[] {
65+
warnOnce(
66+
'`getHeaderLanguages` of `@intlify/utils/h3` is deprecated in v2. Use `getHeaderLanguages` of `@intlify/utils` instead.'
67+
)
68+
6469
const { name = ACCEPT_LANGUAGE_HEADER } = options
6570
const getter = () => {
6671
const headers = getHeaders(event)
@@ -95,6 +100,10 @@ export function getHeaderLanguages(event: H3Event, options: HeaderOptions = {}):
95100
* @returns The **first language tag** of header, if header is not exists, or `*` (any language), return empty string.
96101
*/
97102
export function getHeaderLanguage(event: H3Event, options: HeaderOptions = {}): string {
103+
warnOnce(
104+
'`getHeaderLanguage` of `@intlify/utils/h3` is deprecated in v2. Use `getHeaderLanguage` of `@intlify/utils` instead.'
105+
)
106+
98107
return getHeaderLanguages(event, options)[0] || ''
99108
}
100109

@@ -125,6 +134,10 @@ export function getHeaderLanguage(event: H3Event, options: HeaderOptions = {}):
125134
* @returns The locales that wrapped from header, if you use `accept-language` header and `*` (any language) or empty string is detected, return an empty array.
126135
*/
127136
export function getHeaderLocales(event: H3Event, options: HeaderOptions = {}): Intl.Locale[] {
137+
warnOnce(
138+
'`getHeaderLocales` of `@intlify/utils/h3` is deprecated in v2. Use `getHeaderLocales` of `@intlify/utils` instead.'
139+
)
140+
128141
return mapToLocaleFromLanguageTag(getHeaderLanguages, event, options)
129142
}
130143

@@ -142,6 +155,10 @@ export function tryHeaderLocales(
142155
event: H3Event,
143156
options: HeaderOptions = {}
144157
): Intl.Locale[] | null {
158+
warnOnce(
159+
'`tryHeaderLocales` of `@intlify/utils/h3` is deprecated in v2. Use `tryHeaderLocales` of `@intlify/utils` instead.'
160+
)
161+
145162
try {
146163
return getHeaderLocales(event, options)
147164
} catch {
@@ -179,6 +196,10 @@ export function getHeaderLocale(
179196
event: H3Event,
180197
options: HeaderOptions & { lang?: string } = {}
181198
): Intl.Locale {
199+
warnOnce(
200+
'`getHeaderLocale` of `@intlify/utils/h3` is deprecated in v2. Use `getHeaderLocale` of `@intlify/utils` instead.'
201+
)
202+
182203
const {
183204
lang = DEFAULT_LANG_TAG,
184205
name = ACCEPT_LANGUAGE_HEADER,
@@ -201,6 +222,10 @@ export function tryHeaderLocale(
201222
event: H3Event,
202223
options: HeaderOptions & { lang?: string } = {}
203224
): Intl.Locale | null {
225+
warnOnce(
226+
'`tryHeaderLocale` of `@intlify/utils/h3` is deprecated in v2. Use `tryHeaderLocale` of `@intlify/utils` instead.'
227+
)
228+
204229
try {
205230
return getHeaderLocale(event, options)
206231
} catch {
@@ -233,6 +258,10 @@ export function tryHeaderLocale(
233258
* @returns The locale that resolved from cookie
234259
*/
235260
export function getCookieLocale(event: H3Event, options: CookieLocaleOptions = {}): Intl.Locale {
261+
warnOnce(
262+
'`getCookieLocale` of `@intlify/utils/h3` is deprecated in v2. Use `getCookieLocale` of `@intlify/utils` instead.'
263+
)
264+
236265
const { lang = DEFAULT_LANG_TAG, name = DEFAULT_COOKIE_NAME } = options
237266
return getLocaleWithGetter(() => getCookie(event, name) || lang)
238267
}
@@ -251,6 +280,10 @@ export function tryCookieLocale(
251280
event: H3Event,
252281
options: CookieLocaleOptions = {}
253282
): Intl.Locale | null {
283+
warnOnce(
284+
'`tryCookieLocale` of `@intlify/utils/h3` is deprecated in v2. Use `tryCookieLocale` of `@intlify/utils` instead.'
285+
)
286+
254287
try {
255288
return getCookieLocale(event, options)
256289
} catch {
@@ -285,6 +318,10 @@ export function setCookieLocale(
285318
locale: string | Intl.Locale,
286319
options: CookieOptions = {}
287320
): void {
321+
warnOnce(
322+
'`setCookieLocale` of `@intlify/utils/h3` is deprecated in v2. Use `setCookieLocale` of `@intlify/utils` instead.'
323+
)
324+
288325
const { name = DEFAULT_COOKIE_NAME } = options
289326
validateLocale(locale)
290327
setCookie(event, name, locale.toString(), options)
@@ -301,6 +338,10 @@ export function setCookieLocale(
301338
* @returns The locale that resolved from path
302339
*/
303340
export function getPathLocale(event: H3Event, options: PathOptions = {}): Intl.Locale {
341+
warnOnce(
342+
'`getPathLocale` of `@intlify/utils/h3` is deprecated in v2. Use `getPathLocale` of `@intlify/utils` instead.'
343+
)
344+
304345
return _getPathLocale(getRequestURL(event), options)
305346
}
306347

@@ -315,6 +356,10 @@ export function getPathLocale(event: H3Event, options: PathOptions = {}): Intl.L
315356
* @returns The locale that resolved from path. if the language in the path, that is not a well-formed BCP 47 language tag, return `null`.
316357
*/
317358
export function tryPathLocale(event: H3Event, options: PathOptions = {}): Intl.Locale | null {
359+
warnOnce(
360+
'`tryPathLocale` of `@intlify/utils/h3` is deprecated in v2. Use `tryPathLocale` of `@intlify/utils` instead.'
361+
)
362+
318363
try {
319364
return getPathLocale(event, options)
320365
} catch {
@@ -333,6 +378,10 @@ export function tryPathLocale(event: H3Event, options: PathOptions = {}): Intl.L
333378
* @returns The locale that resolved from query
334379
*/
335380
export function getQueryLocale(event: H3Event, options: QueryOptions = {}): Intl.Locale {
381+
warnOnce(
382+
'`getQueryLocale` of `@intlify/utils/h3` is deprecated in v2. Use `getQueryLocale` of `@intlify/utils` instead.'
383+
)
384+
336385
const { lang = DEFAULT_LANG_TAG, name = 'locale' } = options
337386
return _getQueryLocale(getRequestURL(event), { lang, name })
338387
}
@@ -348,6 +397,10 @@ export function getQueryLocale(event: H3Event, options: QueryOptions = {}): Intl
348397
* @returns The locale that resolved from query. if the language in the query, that is not a well-formed BCP 47 language tag, return `null`.
349398
*/
350399
export function tryQueryLocale(event: H3Event, options: QueryOptions = {}): Intl.Locale | null {
400+
warnOnce(
401+
'`tryQueryLocale` of `@intlify/utils/h3` is deprecated in v2. Use `tryQueryLocale` of `@intlify/utils` instead.'
402+
)
403+
351404
const { lang = DEFAULT_LANG_TAG, name = 'locale' } = options
352405
try {
353406
return getQueryLocale(event, { lang, name })

src/hono.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
parseDefaultHeader,
2626
validateLocale
2727
} from './http.ts'
28+
import { warnOnce } from './utils.ts'
2829

2930
import type { Context } from 'hono'
3031
import type { CookieLocaleOptions, HeaderOptions, PathOptions, QueryOptions } from './http.ts'
@@ -57,6 +58,10 @@ type CookieOptions = Parameters<typeof setCookie>[3] & { name?: string }
5758
* @returns An array of language tags, if you use `accept-language` header and `*` (any language) or empty string is detected, return an empty array.
5859
*/
5960
export function getHeaderLanguages(context: Context, options: HeaderOptions = {}): string[] {
61+
warnOnce(
62+
'`getHeaderLanguages` of `@intlify/utils/hono` is deprecated in v2. Use `getHeaderLanguages` of `@intlify/utils` instead.'
63+
)
64+
6065
const { name = ACCEPT_LANGUAGE_HEADER } = options
6166
return getHeaderLanguagesWithGetter(() => context.req.header(name), options)
6267
}
@@ -87,6 +92,10 @@ export function getHeaderLanguages(context: Context, options: HeaderOptions = {}
8792
* @returns A **first language tag** of header, if header is not exists, or `*` (any language), return empty string.
8893
*/
8994
export function getHeaderLanguage(context: Context, options: HeaderOptions = {}): string {
95+
warnOnce(
96+
'`getHeaderLanguage` of `@intlify/utils/hono` is deprecated in v2. Use `getHeaderLanguage` of `@intlify/utils` instead.'
97+
)
98+
9099
return getHeaderLanguages(context, options)[0] || ''
91100
}
92101

@@ -118,6 +127,10 @@ export function getHeaderLanguage(context: Context, options: HeaderOptions = {})
118127
* @returns Some locales that wrapped from header, if you use `accept-language` header and `*` (any language) or empty string is detected, return an empty array.
119128
*/
120129
export function getHeaderLocales(context: Context, options: HeaderOptions = {}): Intl.Locale[] {
130+
warnOnce(
131+
'`getHeaderLocales` of `@intlify/utils/hono` is deprecated in v2. Use `getHeaderLocales` of `@intlify/utils` instead.'
132+
)
133+
121134
return mapToLocaleFromLanguageTag(getHeaderLanguages, context, options)
122135
}
123136

@@ -135,6 +148,9 @@ export function tryHeaderLocales(
135148
context: Context,
136149
options: HeaderOptions = {}
137150
): Intl.Locale[] | null {
151+
warnOnce(
152+
'`tryHeaderLocales` of `@intlify/utils/hono` is deprecated in v2. Use `tryHeaderLocales` of `@intlify/utils` instead.'
153+
)
138154
try {
139155
return getHeaderLocales(context, options)
140156
} catch {
@@ -173,6 +189,10 @@ export function getHeaderLocale(
173189
context: Context,
174190
options: HeaderOptions & { lang?: string } = {}
175191
): Intl.Locale {
192+
warnOnce(
193+
'`getHeaderLocale` of `@intlify/utils/hono` is deprecated in v2. Use `getHeaderLocale` of `@intlify/utils` instead.'
194+
)
195+
176196
const {
177197
lang = DEFAULT_LANG_TAG,
178198
name = ACCEPT_LANGUAGE_HEADER,
@@ -195,6 +215,10 @@ export function tryHeaderLocale(
195215
context: Context,
196216
options: HeaderOptions & { lang?: string } = {}
197217
): Intl.Locale | null {
218+
warnOnce(
219+
'`tryHeaderLocale` of `@intlify/utils/hono` is deprecated in v2. Use `tryHeaderLocale` of `@intlify/utils` instead.'
220+
)
221+
198222
try {
199223
return getHeaderLocale(context, options)
200224
} catch {
@@ -228,6 +252,10 @@ export function tryHeaderLocale(
228252
* @returns The locale that resolved from cookie
229253
*/
230254
export function getCookieLocale(context: Context, options: CookieLocaleOptions = {}): Intl.Locale {
255+
warnOnce(
256+
'`getCookieLocale` of `@intlify/utils/hono` is deprecated in v2. Use `getCookieLocale` of `@intlify/utils` instead.'
257+
)
258+
231259
const { lang = DEFAULT_LANG_TAG, name = DEFAULT_COOKIE_NAME } = options
232260
return getLocaleWithGetter(() => getCookie(context, name) || lang)
233261
}
@@ -246,6 +274,10 @@ export function tryCookieLocale(
246274
context: Context,
247275
options: CookieLocaleOptions = {}
248276
): Intl.Locale | null {
277+
warnOnce(
278+
'`tryCookieLocale` of `@intlify/utils/hono` is deprecated in v2. Use `tryCookieLocale` of `@intlify/utils` instead.'
279+
)
280+
249281
try {
250282
return getCookieLocale(context, options)
251283
} catch {
@@ -281,6 +313,10 @@ export function setCookieLocale(
281313
locale: string | Intl.Locale,
282314
options: CookieOptions = {}
283315
): void {
316+
warnOnce(
317+
'`setCookieLocale` of `@intlify/utils/hono` is deprecated in v2. Use `setCookieLocale` of `@intlify/utils` instead.'
318+
)
319+
284320
const { name = DEFAULT_COOKIE_NAME } = options
285321
validateLocale(locale)
286322
setCookie(context, name, locale.toString(), options)
@@ -297,6 +333,10 @@ export function setCookieLocale(
297333
* @returns The locale that resolved from path
298334
*/
299335
export function getPathLocale(context: Context, options: PathOptions = {}): Intl.Locale {
336+
warnOnce(
337+
'`getPathLocale` of `@intlify/utils/hono` is deprecated in v2. Use `getPathLocale` of `@intlify/utils` instead.'
338+
)
339+
300340
return _getPathLocale(new URL(context.req.url), options)
301341
}
302342

@@ -311,6 +351,10 @@ export function getPathLocale(context: Context, options: PathOptions = {}): Intl
311351
* @returns The locale that resolved from path. if the language in the path, that is not a well-formed BCP 47 language tag, return `null`.
312352
*/
313353
export function tryPathLocale(context: Context, options: PathOptions = {}): Intl.Locale | null {
354+
warnOnce(
355+
'`tryPathLocale` of `@intlify/utils/hono` is deprecated in v2. Use `tryPathLocale` of `@intlify/utils` instead.'
356+
)
357+
314358
try {
315359
return getPathLocale(context, options)
316360
} catch {
@@ -329,6 +373,10 @@ export function tryPathLocale(context: Context, options: PathOptions = {}): Intl
329373
* @returns The locale that resolved from query
330374
*/
331375
export function getQueryLocale(context: Context, options: QueryOptions = {}): Intl.Locale {
376+
warnOnce(
377+
'`getQueryLocale` of `@intlify/utils/hono` is deprecated in v2. Use `getQueryLocale` of `@intlify/utils` instead.'
378+
)
379+
332380
const { lang = DEFAULT_LANG_TAG, name = 'locale' } = options
333381
return _getQueryLocale(new URL(context.req.url), { lang, name })
334382
}
@@ -344,6 +392,10 @@ export function getQueryLocale(context: Context, options: QueryOptions = {}): In
344392
* @returns The locale that resolved from query. if the language in the query, that is not a well-formed BCP 47 language tag, return `null`.
345393
*/
346394
export function tryQueryLocale(context: Context, options: QueryOptions = {}): Intl.Locale | null {
395+
warnOnce(
396+
'`tryQueryLocale` of `@intlify/utils/hono` is deprecated in v2. Use `tryQueryLocale` of `@intlify/utils` instead.'
397+
)
398+
347399
const { lang = DEFAULT_LANG_TAG, name = 'locale' } = options
348400
try {
349401
return getQueryLocale(context, { lang, name })

src/utils.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @author kazuya kawaguchi (a.k.a. kazupon)
3+
* @license MIT
4+
*/
5+
6+
function warn(msg: string, err?: Error): void {
7+
if (typeof console !== 'undefined') {
8+
console.warn(`[intlify/utils] ` + msg)
9+
/* istanbul ignore if */
10+
if (err) {
11+
console.warn(err.stack)
12+
}
13+
}
14+
}
15+
16+
const hasWarned: Record<string, boolean> = {}
17+
18+
/**
19+
* Warns a message only once.
20+
*
21+
* @param msg - The warning message
22+
*/
23+
export function warnOnce(msg: string): void {
24+
if (!hasWarned[msg]) {
25+
hasWarned[msg] = true
26+
warn(msg)
27+
}
28+
}

0 commit comments

Comments
 (0)