Skip to content

Commit 59a01d1

Browse files
committed
add setCookieLocale
1 parent 9e6d58c commit 59a01d1

File tree

4 files changed

+103
-5
lines changed

4 files changed

+103
-5
lines changed

bun.lockb

10.1 KB
Binary file not shown.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,13 @@
5959
},
6060
"devDependencies": {
6161
"@types/node": "^20.6.0",
62+
"@types/supertest": "^2.0.12",
6263
"@vitest/coverage-v8": "^0.34.4",
6364
"bumpp": "^9.2.0",
6465
"gh-changelogen": "^0.2.8",
6566
"h3": "^1.8.1",
6667
"lint-staged": "^14.0.0",
68+
"supertest": "^6.3.3",
6769
"typescript": "^5.2.2",
6870
"unbuild": "^2.0.0",
6971
"vitest": "^0.34.4"

src/index.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { getCookie, getHeaders } from 'h3'
2-
import { parseAcceptLanguage } from './utils.ts'
1+
import { getCookie, getHeaders, setCookie } from 'h3'
2+
import { isLocale, parseAcceptLanguage, validateLanguageTag } from './utils.ts'
33

44
import type { H3Event } from 'h3'
55

@@ -50,3 +50,24 @@ export function getCookieLocale(
5050
): Intl.Locale {
5151
return new Intl.Locale(getCookie(event, name) || lang)
5252
}
53+
54+
/**
55+
* @param {H3Event} event The {@link H3Event | H3} event
56+
* @param {string | Intl.Locale} locale The locale value
57+
* @param options.name The cookie name, default is `i18n_locale`
58+
*
59+
* @throws {SyntaxError} Throws a {@link SyntaxError} if `locale` is invalid.
60+
*/
61+
export function setCookieLocale(
62+
event: H3Event,
63+
locale: string | Intl.Locale,
64+
{ name = 'i18n_locale' } = {},
65+
): void {
66+
if (
67+
!(isLocale(locale) ||
68+
typeof locale === 'string' && validateLanguageTag(locale))
69+
) {
70+
throw new SyntaxError(`locale is invalid: ${locale.toString()}`)
71+
}
72+
setCookie(event, name, locale.toString())
73+
}

test/index.test.ts

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
import { describe, expect, test } from 'vitest'
2-
import { getAcceptLanguages, getCookieLocale, getLocale } from '../src/index.ts'
1+
import { beforeEach, describe, expect, test } from 'vitest'
2+
import { createApp, eventHandler, toNodeListener } from 'h3'
3+
import supertest from 'supertest'
4+
import {
5+
getAcceptLanguages,
6+
getCookieLocale,
7+
getLocale,
8+
setCookieLocale,
9+
} from '../src/index.ts'
310

4-
import type { H3Event } from 'h3'
11+
import type { App, H3Event } from 'h3'
12+
import type { SuperTest, Test } from 'supertest'
513

614
describe('getAcceptLanguages', () => {
715
test('basic', () => {
@@ -191,3 +199,70 @@ describe('getCookieLocale', () => {
191199
.toThrowError(RangeError)
192200
})
193201
})
202+
203+
describe('setCookieLocale', () => {
204+
let app: App
205+
let request: SuperTest<Test>
206+
207+
beforeEach(() => {
208+
app = createApp({ debug: false })
209+
request = supertest(toNodeListener(app))
210+
})
211+
212+
test('specify Locale instance', async () => {
213+
app.use(
214+
'/',
215+
eventHandler((event) => {
216+
const locale = new Intl.Locale('ja-JP')
217+
setCookieLocale(event, locale)
218+
return '200'
219+
}),
220+
)
221+
const result = await request.get('/')
222+
expect(result.headers['set-cookie']).toEqual([
223+
'i18n_locale=ja-JP; Path=/',
224+
])
225+
})
226+
227+
test('specify language tag', async () => {
228+
app.use(
229+
'/',
230+
eventHandler((event) => {
231+
setCookieLocale(event, 'ja-JP')
232+
return '200'
233+
}),
234+
)
235+
const result = await request.get('/')
236+
expect(result.headers['set-cookie']).toEqual([
237+
'i18n_locale=ja-JP; Path=/',
238+
])
239+
})
240+
241+
test('specify cookie name', async () => {
242+
app.use(
243+
'/',
244+
eventHandler((event) => {
245+
setCookieLocale(event, 'ja-JP', { name: 'intlify_locale' })
246+
return '200'
247+
}),
248+
)
249+
const result = await request.get('/')
250+
expect(result.headers['set-cookie']).toEqual([
251+
'intlify_locale=ja-JP; Path=/',
252+
])
253+
})
254+
255+
test('Syntax Error', () => {
256+
const eventMock = {
257+
node: {
258+
req: {
259+
method: 'GET',
260+
headers: {},
261+
},
262+
},
263+
} as H3Event
264+
265+
expect(() => setCookieLocale(eventMock, 'j'))
266+
.toThrowError(/locale is invalid: j/)
267+
})
268+
})

0 commit comments

Comments
 (0)