Skip to content

Commit

Permalink
レベル8正規化に使われるデータへのリクエスト URL にタイポがあったので修正 (#219)
Browse files Browse the repository at this point in the history
* Fix data fething bug

* Add a test to assert transforRequest function argument is valid
  • Loading branch information
Kamata, Ryo committed Aug 1, 2023
1 parent 4d778a9 commit 1bf1a3d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/lib/cacheRegexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export const getAddrs = async (pref: string, city: string, town: string) => {
}

const addrsResp = await __internals.fetch(
['', encodeURI(pref), encodeURI(city), encodeURI(town) + 'json'].join('/'),
['', encodeURI(pref), encodeURI(city), encodeURI(town) + '.json'].join('/'),
{ level: 8, pref, city, town },
)
let addrs: AddrList
Expand Down
35 changes: 30 additions & 5 deletions test/advanced/interface_v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@ import { normalize, config } from '../../src/main-node'

config.interfaceVersion = 2
config.transformRequest = async (url, query) => {
const { level, pref, city, town } = query
const { level, pref = '', city = '', town = '' } = query

switch (level) {
case 1:
case 1: {
const actual = url.toString()
const expected = config.japaneseAddressesApi + '.json'
if (actual !== expected) {
throw new Error(`Invalid URL: ${actual} should be ${expected}`)
}
return {
A県: ['X市', 'Y市', 'Z町'],
}
case 3:
}
case 3: {
const actual = url.toString()
const expected =
config.japaneseAddressesApi +
`/${encodeURIComponent(pref)}/${encodeURIComponent(city)}.json`
if (actual !== expected) {
throw new Error(`Invalid URL: ${actual} should be ${expected}`)
}
if (pref === 'A県' && city === 'X市') {
return [
{ town: 'あああ', koaza: '', lat: '30', lng: '135' },
Expand All @@ -17,15 +31,26 @@ config.transformRequest = async (url, query) => {
} else {
return null
}
case 8:
}
case 8: {
const actual = url.toString()
const expected =
config.japaneseAddressesApi +
`/${encodeURIComponent(pref)}/${encodeURIComponent(
city,
)}/${encodeURIComponent(town)}.json`
if (actual !== expected) {
throw new Error(`Invalid URL: ${actual} should be ${expected}`)
}
if (pref === 'A県' && city === 'X市' && town === 'あああ') {
return [
{ addr: '1-1', lat: '30.1', lng: '135.1' },
{ addr: '1-2', lat: '30.2', lng: '135.2' },
]
}
}
default:
return null
throw new Error(`Invalid required normalization level: ${level}`)
}
}

Expand Down

0 comments on commit 1bf1a3d

Please sign in to comment.