Skip to content

Commit 7dfe817

Browse files
authored
fix: Add Accept-Language to API calls when locale is set (#182)
1 parent 0cde5b7 commit 7dfe817

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

lib/smart-app.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,8 @@ class SmartApp {
420420
const ctx = await this.withContext({
421421
installedAppId: auth.installed_app_id,
422422
authToken: auth.access_token,
423-
refreshToken: auth.refresh_token
423+
refreshToken: auth.refresh_token,
424+
locale: request.headers ? request.headers['Accept-Language'] : undefined
424425
})
425426

426427
const isa = await ctx.api.installedApps.get(auth.installed_app_id)

lib/util/smart-app-context.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ module.exports = class SmartAppContext {
115115
locationId: this.locationId,
116116
installedAppId: this.installedAppId
117117
}
118+
119+
if (this.locale) {
120+
config.headers = {
121+
'Accept-Language': this.locale
122+
}
123+
}
124+
118125
if (app._apiUrl) {
119126
config.urlProvider = {
120127
baseURL: app._apiUrl,
@@ -158,6 +165,13 @@ module.exports = class SmartAppContext {
158165
locationId: this.locationId,
159166
installedAppId: this.installedAppId
160167
}
168+
169+
if (this.locale) {
170+
config.headers = {
171+
'Accept-Language': this.locale
172+
}
173+
}
174+
161175
if (app._apiUrl) {
162176
config.urlProvider = {
163177
baseURL: app._apiUrl,

test/unit/smartapp-context-spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,18 @@ describe('smartapp-context-spec', () => {
122122
assert.equal(params.refreshToken, ctx.refreshToken)
123123
assert.equal(params.locale, ctx.event.locale)
124124
})
125+
126+
it('accept-language header in API calls', async () => {
127+
const params = {
128+
authToken: 'xxx',
129+
installedAppId: 'aaa',
130+
locationId: 'bbb',
131+
locale: 'es',
132+
config: {device: 'ccc'}
133+
}
134+
135+
const ctx = await app.withContext(params)
136+
137+
expect(ctx.api.config.headers['Accept-Language']).toBe('es')
138+
})
125139
})

0 commit comments

Comments
 (0)