Skip to content

Commit

Permalink
feat: allow custom locale store cookie domain (#33)
Browse files Browse the repository at this point in the history
* test: use mocha@4
  • Loading branch information
fengmk2 authored Apr 17, 2019
1 parent b3c8ab1 commit 08037ee
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 12 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ language: node_js
node_js:
- '4'
- '6'
- '7'
- '8'
- '10'
- '11'
install:
- npm i npminstall && npminstall
script:
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ Patch locales functions to koa app.
- {String} defaultLocale: default locale. Optional, default is `en-US`.
- {String} queryField: locale field name on query. Optional, default is `locale`.
- {String} cookieField: locale field name on cookie. Optional, default is `locale`.
- {Object} localeAlias: locale value map. Optional, default is {}.
- {String} cookieDomain: domain on cookie. Optional, default is `''`.
- {Object} localeAlias: locale value map. Optional, default is `{}`.
- {String|Number} cookieMaxAge: set locale cookie value max age. Optional, default is `1y`, expired after one year.

```js
Expand Down
6 changes: 4 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ environment:
matrix:
- nodejs_version: '4'
- nodejs_version: '6'
- nodejs_version: '7'
- nodejs_version: '8'
- nodejs_version: '10'
- nodejs_version: '11'

install:
- ps: Install-Product node $env:nodejs_version
Expand All @@ -11,6 +13,6 @@ install:
test_script:
- node --version
- npm --version
- npm run ci
- npm run test

build: off
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = function (app, options) {
const defaultLocale = formatLocale(options.defaultLocale);
const queryField = options.queryField;
const cookieField = options.cookieField;
const cookieDomain = options.cookieDomain;
const localeAlias = options.localeAlias;
const writeCookie = options.writeCookie;
const cookieMaxAge = ms(options.cookieMaxAge);
Expand Down Expand Up @@ -194,12 +195,14 @@ module.exports = function (app, options) {
// if header not send, set the locale cookie
if (writeCookie && cookieLocale !== locale && !this.headerSent) {
// locale change, need to set cookie
this.cookies.set(cookieField, locale, {
const cookieOptions = {
// make sure brower javascript can read the cookie
httpOnly: false,
maxAge: cookieMaxAge,
signed: false,
});
domain: cookieDomain,
};
this.cookies.set(cookieField, locale, cookieOptions);
debugSilly('Saved cookie with locale %s', locale);
}
debug('Locale: %s from %s', locale, localeOrigin);
Expand Down
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"istanbul": "*",
"koa": "^1.2.4",
"mm": "^2.0.0",
"mocha": "*",
"mocha": "4",
"pedding": "^1.1.0",
"supertest": "^2.0.1"
},
Expand All @@ -41,8 +41,7 @@
"web": "https://github.com/koajs/locales"
},
"bugs": {
"url": "https://github.com/koajs/locales/issues",
"email": "[email protected]"
"url": "https://github.com/koajs/locales/issues"
},
"keywords": [
"koa-locales",
Expand All @@ -55,8 +54,8 @@
"node": ">=4.0.0"
},
"ci": {
"version": "4, 6, 7"
"version": "4, 6, 8, 10, 11"
},
"author": "fengmk2 <m@fengmk2.com> (https://fengmk2.com)",
"author": "fengmk2 <fengmk2@gmail.com> (https://fengmk2.com)",
"license": "MIT"
}
33 changes: 32 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('koa-locales.test.js', function () {
'gender': 'model.user.fields.gender',
'name': 'model.user.fields.name',
})
.expect('Set-Cookie', /^locale=en\-us; path=\/; expires=\w+/)
.expect('Set-Cookie', /^locale=en\-us; path=\/; expires=[^;]+ GMT$/)
.expect(200, done);
});

Expand All @@ -50,6 +50,37 @@ describe('koa-locales.test.js', function () {
});
});

describe('options.cookieDomain', function () {
const app = createApp({
cookieDomain: '.foo.com',
});

it('should use default locale: en-US', function (done) {
request(app.callback())
.get('/')
.expect({
email: 'Email',
hello: 'Hello fengmk2, how are you today?',
message: 'Hello fengmk2, how are you today? How was your 18.',
empty: '',
notexists_key: 'key not exists',
empty_string: '',
empty_value: 'emptyValue',
novalue: 'key %s ok',
arguments3: '1 2 3',
arguments4: '1 2 3 4',
arguments5: '1 2 3 4 5',
arguments6: '1 2 3 4 5. 6',
values: 'foo bar foo bar {2} {100}',
object: 'foo bar foo bar {z}',
'gender': 'model.user.fields.gender',
'name': 'model.user.fields.name',
})
.expect('Set-Cookie', /^locale=en\-us; path=\/; expires=[^;]+; domain=.foo.com$/)
.expect(200, done);
});
});

describe('custom options', function () {
const app = createApp({
dirs: [__dirname + '/locales', __dirname + '/other-locales'],
Expand Down

0 comments on commit 08037ee

Please sign in to comment.