Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ new FitDaysClient(opts?: ClientOptions)
| ------------- | -------------------------------- | ---------------------------------------------------------- |
| `region` | `'us'` | `'us'` \| `'eu'` \| `'cn'` — picks the API host |
| `baseUrl` | derived from `region` | overrides the host (useful for tests or new hosts) |
| `country` | `'US'` | sent as the `country` query param |
| `language` | `'en'` | sent as the `language` query param |
| `country` | `'CN'` for `cn`, otherwise `'US'` | sent as the `country` query param |
| `language` | `'zh'` for `cn`, otherwise `'en'` | sent as the `language` query param |
| `deviceModel` | `'AndroidSDKbuiltforarm64-6.0'` | sent as the `device_model` query param |
| `clientId` | generated (uppercase MD5 of UUID) | identifies the client "device" |
| `fetchImpl` | `globalThis.fetch` | inject for tests or to use `undici` / `node-fetch` |
Expand Down
9 changes: 6 additions & 3 deletions src/client/fitdays-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
DEFAULT_DEVICE_MODEL,
FULL_SYNC_WINDOW_SECONDS,
REGION_HOSTS,
REGION_LOCALES,
USER_AGENT,
} from '../constants/api.js'
import { FitDaysApiError } from '../errors/api-error.js'
Expand All @@ -26,9 +27,11 @@ export class FitDaysClient {
private readonly fetchImpl: typeof fetch

constructor(opts: ClientOptions = {}) {
this.baseUrl = (opts.baseUrl ?? REGION_HOSTS[opts.region ?? 'us']).replace(/\/$/, '')
this.country = opts.country ?? 'US'
this.language = opts.language ?? 'en'
const region = opts.region ?? 'us'
const locale = REGION_LOCALES[region]
this.baseUrl = (opts.baseUrl ?? REGION_HOSTS[region]).replace(/\/$/, '')
this.country = opts.country ?? locale.country
this.language = opts.language ?? locale.language
this.deviceModel = opts.deviceModel ?? DEFAULT_DEVICE_MODEL
this.clientId = opts.clientId ?? newClientId()
this.fetchImpl = opts.fetchImpl ?? fetch
Expand Down
8 changes: 7 additions & 1 deletion src/constants/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Region } from '../types/client.js'
import type { Region, RegionLocale } from '../types/client.js'

export const APP_VER = '1.21.0'
export const DEFAULT_DEVICE_MODEL = 'AndroidSDKbuiltforarm64-6.0'
Expand All @@ -10,5 +10,11 @@ export const REGION_HOSTS: Record<Region, string> = {
us: 'https://online-us.fitdays.cn',
}

export const REGION_LOCALES: Record<Region, RegionLocale> = {
cn: { country: 'CN', language: 'zh' },
eu: { country: 'US', language: 'en' },
us: { country: 'US', language: 'en' },
}

/** Default sync window (~6 years / 2160 days) used by `syncAll()`. */
export const FULL_SYNC_WINDOW_SECONDS = 186624000
2 changes: 1 addition & 1 deletion src/fitdays-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export {
export { PASSWORD_SALT, SIGN_SECRET } from './constants/sign.js'
export { FitDaysApiError } from './errors/api-error.js'
export type { ApiResponse, LoginData } from './types/api.js'
export type { ClientOptions, Region, Session } from './types/client.js'
export type { ClientOptions, Region, RegionLocale, Session } from './types/client.js'
export type { SignParams } from './types/sign.js'
export type {
AccountInfo,
Expand Down
13 changes: 13 additions & 0 deletions src/tests/fitdays-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ describe('FitDaysClient — constructor', () => {
assert.equal(c.session, null)
})

it('defaults country and language to the China locale for the cn region', () => {
const c = new FitDaysClient({ region: 'cn' })
assert.equal(c.country, 'CN')
assert.equal(c.language, 'zh')
assert.equal(c.baseUrl, 'https://online.fitdays.cn')
})

it('keeps the US locale for the eu region', () => {
const c = new FitDaysClient({ region: 'eu' })
assert.equal(c.country, 'US')
assert.equal(c.language, 'en')
})

it('honours overrides', () => {
const c = new FitDaysClient({
clientId: 'AAAA',
Expand Down
5 changes: 5 additions & 0 deletions src/types/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export type ClientOptions = {

export type Region = 'cn' | 'eu' | 'us'

export type RegionLocale = {
country: string
language: string
}

export type Session = {
refreshToken?: string
token: string
Expand Down