jp-postal-lookup 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ var u="https://geoapi.heartrails.com/api/json";function f(n){return n==null?"":String(n).replace(/[ー--]/g,"").replace(/[\s\u3000]/g,"").trim()}async function a(n,t={}){let e=typeof globalThis.fetch=="function"?globalThis.fetch:null;if(!e)throw new Error("jp-postal-lookup: Fetch API is not available in this environment.");try{let r=await e(n,t);return r.ok?(await r.json())?.response??null:null}catch{return null}}async function p(n,t,e=u){let r=`${e}?method=getPrefectures&area=${encodeURIComponent(n||"")}`,s=await a(r);return typeof t=="function"&&t(s),s}async function y(n,t,e,r=u){let s=n?`&area=${encodeURIComponent(n)}`:"",o=`${r}?method=getCities${s}&prefecture=${encodeURIComponent(t||"")}`,i=await a(o);return typeof e=="function"&&e(i),i}async function d(n,t,e,r=u){let s=`${r}?method=getTowns&city=${encodeURIComponent(n||"")}&prefecture=${encodeURIComponent(t||"")}`,o=await a(s);return typeof e=="function"&&e(o),o}async function h(n,t=!1,e=u){let r=f(n);if(!r)return t?null:"";let s=`${e}?method=searchByPostal&postal=${encodeURIComponent(r)}`,o=await a(s);if(!o)return t?null:"";let i=o?.location;if(Array.isArray(i)&&i.length>0){let c=i[0];return t?c:`${c.prefecture||""}${c.city||""}${c.town||""}`}return t?null:""}async function g(n,t=!1,e="suffix",r=u){if(!n||typeof n!="string"||!n.trim())return t?null:"";let s=`${r}?method=suggest&matching=${encodeURIComponent(e)}&keyword=${encodeURIComponent(n.trim())}`,o=await a(s);if(!o)return t?null:"";let i=o?.location;if(Array.isArray(i)&&i.length>0){let c=i[0];return t?c:c?.postal||""}return t?null:""}async function m(n,{raw:t=!1,all:e=!1,baseUrl:r=u}={}){let s=f(n);if(!s)return e?[]:t?null:"";let o=`${r}?method=searchByPostal&postal=${encodeURIComponent(s)}`,c=(await a(o))?.location||[];if(!c.length)return e?[]:t?null:"";if(e)return c;let l=c[0];return t?l:`${l.prefecture||""}${l.city||""}${l.town||""}`}async function $(n,{raw:t=!1,all:e=!1,matching:r="suffix",baseUrl:s=u}={}){if(!n||typeof n!="string"||!n.trim())return e?[]:t?null:"";let o=`${s}?method=suggest&matching=${encodeURIComponent(r)}&keyword=${encodeURIComponent(n.trim())}`,c=(await a(o))?.location||[];if(!c.length)return e?[]:t?null:"";if(e)return c;let l=c[0];return t?l:l.postal||""}async function w(n=u){let t=`${n}?method=getAreas`;return(await a(t))?.area||[]}async function A(n,t=u){return(await p(n,null,t))?.prefecture||[]}async function C(n,t="",e=u){return((await y(t,n,null,e))?.location||[]).map(o=>typeof o=="string"?o:o.city).filter(Boolean)}async function I(n,t="",e=u){return((await d(n,t,null,e))?.location||[]).map(o=>typeof o=="string"?o:o.town).filter(Boolean)}module.exports={DEFAULT_API_BASE_URL:u,normalizePostal:f,getPrefecturesByArea:p,onGetCitiesByPrefecture:y,onGetTownsByCity:d,onSearchAddressByPostcode:h,onSearchPostcodeByAddress:g,searchAddressByPostal:m,searchPostalByAddress:$,getAreas:w,getPrefectures:A,getCities:C,getTowns:I};
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ var u="https://geoapi.heartrails.com/api/json";function f(t){return t==null?"":String(t).replace(/[ー--]/g,"").replace(/[\s\u3000]/g,"").trim()}async function a(t,n={}){let e=typeof globalThis.fetch=="function"?globalThis.fetch:null;if(!e)throw new Error("jp-postal-lookup: Fetch API is not available in this environment.");try{let r=await e(t,n);return r.ok?(await r.json())?.response??null:null}catch{return null}}async function p(t,n,e=u){let r=`${e}?method=getPrefectures&area=${encodeURIComponent(t||"")}`,s=await a(r);return typeof n=="function"&&n(s),s}async function y(t,n,e,r=u){let s=t?`&area=${encodeURIComponent(t)}`:"",o=`${r}?method=getCities${s}&prefecture=${encodeURIComponent(n||"")}`,i=await a(o);return typeof e=="function"&&e(i),i}async function d(t,n,e,r=u){let s=`${r}?method=getTowns&city=${encodeURIComponent(t||"")}&prefecture=${encodeURIComponent(n||"")}`,o=await a(s);return typeof e=="function"&&e(o),o}async function h(t,n=!1,e=u){let r=f(t);if(!r)return n?null:"";let s=`${e}?method=searchByPostal&postal=${encodeURIComponent(r)}`,o=await a(s);if(!o)return n?null:"";let i=o?.location;if(Array.isArray(i)&&i.length>0){let c=i[0];return n?c:`${c.prefecture||""}${c.city||""}${c.town||""}`}return n?null:""}async function g(t,n=!1,e="suffix",r=u){if(!t||typeof t!="string"||!t.trim())return n?null:"";let s=`${r}?method=suggest&matching=${encodeURIComponent(e)}&keyword=${encodeURIComponent(t.trim())}`,o=await a(s);if(!o)return n?null:"";let i=o?.location;if(Array.isArray(i)&&i.length>0){let c=i[0];return n?c:c?.postal||""}return n?null:""}async function m(t,{raw:n=!1,all:e=!1,baseUrl:r=u}={}){let s=f(t);if(!s)return e?[]:n?null:"";let o=`${r}?method=searchByPostal&postal=${encodeURIComponent(s)}`,c=(await a(o))?.location||[];if(!c.length)return e?[]:n?null:"";if(e)return c;let l=c[0];return n?l:`${l.prefecture||""}${l.city||""}${l.town||""}`}async function $(t,{raw:n=!1,all:e=!1,matching:r="suffix",baseUrl:s=u}={}){if(!t||typeof t!="string"||!t.trim())return e?[]:n?null:"";let o=`${s}?method=suggest&matching=${encodeURIComponent(r)}&keyword=${encodeURIComponent(t.trim())}`,c=(await a(o))?.location||[];if(!c.length)return e?[]:n?null:"";if(e)return c;let l=c[0];return n?l:l.postal||""}async function w(t=u){let n=`${t}?method=getAreas`;return(await a(n))?.area||[]}async function A(t,n=u){return(await p(t,null,n))?.prefecture||[]}async function x(t,n="",e=u){return((await y(n,t,null,e))?.location||[]).map(o=>typeof o=="string"?o:o.city).filter(Boolean)}async function C(t,n="",e=u){return((await d(t,n,null,e))?.location||[]).map(o=>typeof o=="string"?o:o.town).filter(Boolean)}var I={DEFAULT_API_BASE_URL:u,normalizePostal:f,getPrefecturesByArea:p,onGetCitiesByPrefecture:y,onGetTownsByCity:d,onSearchAddressByPostcode:h,onSearchPostcodeByAddress:g,searchAddressByPostal:m,searchPostalByAddress:$,getAreas:w,getPrefectures:A,getCities:x,getTowns:C};export{u as DEFAULT_API_BASE_URL,I as default,w as getAreas,x as getCities,A as getPrefectures,p as getPrefecturesByArea,C as getTowns,f as normalizePostal,y as onGetCitiesByPrefecture,d as onGetTownsByCity,h as onSearchAddressByPostcode,g as onSearchPostcodeByAddress,m as searchAddressByPostal,$ as searchPostalByAddress};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jp-postal-lookup",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Zero-dependency Japanese address and postal code lookup library using HeartRails Geo API. Supports ESM, CommonJS, and TypeScript. 日本の郵便番号・住所検索ライブラリ。",
5
5
  "keywords": [
6
6
  "japan",
@@ -34,26 +34,32 @@
34
34
  "author": "dungdao",
35
35
  "license": "MIT",
36
36
  "type": "module",
37
- "main": "./src/index.cjs",
38
- "module": "./src/index.js",
37
+ "main": "./dist/index.cjs",
38
+ "module": "./dist/index.js",
39
39
  "types": "./index.d.ts",
40
40
  "exports": {
41
41
  ".": {
42
42
  "types": "./index.d.ts",
43
- "import": "./src/index.js",
44
- "require": "./src/index.cjs"
43
+ "import": "./dist/index.js",
44
+ "require": "./dist/index.cjs"
45
45
  }
46
46
  },
47
47
  "files": [
48
- "src",
48
+ "dist",
49
49
  "index.d.ts",
50
50
  "README.md",
51
51
  "LICENSE"
52
52
  ],
53
53
  "scripts": {
54
- "test": "node --test tests/index.test.js"
54
+ "build": "node scripts/build.mjs",
55
+ "test": "node --test tests/index.test.js",
56
+ "test:dist": "npm run build && node --test tests/dist.test.js",
57
+ "prepublishOnly": "npm test && npm run test:dist"
55
58
  },
56
59
  "engines": {
57
60
  "node": ">=18.0.0"
61
+ },
62
+ "devDependencies": {
63
+ "esbuild": "^0.28.2"
58
64
  }
59
65
  }
package/src/index.cjs DELETED
@@ -1,191 +0,0 @@
1
- const DEFAULT_API_BASE_URL = 'https://geoapi.heartrails.com/api/json';
2
-
3
- function normalizePostal(postal) {
4
- if (postal === null || postal === undefined) return '';
5
- return String(postal)
6
- .replace(/[ー--]/g, '')
7
- .replace(/[\s\u3000]/g, '')
8
- .trim();
9
- }
10
-
11
- async function fetchJson(url, options = {}) {
12
- const fetchFn = typeof globalThis.fetch === 'function' ? globalThis.fetch : null;
13
- if (!fetchFn) {
14
- throw new Error('jp-postal-lookup: Fetch API is not available in this environment.');
15
- }
16
-
17
- try {
18
- const response = await fetchFn(url, options);
19
- if (!response.ok) {
20
- return null;
21
- }
22
- const data = await response.json();
23
- return data?.response ?? null;
24
- } catch {
25
- return null;
26
- }
27
- }
28
-
29
- async function getPrefecturesByArea(area, callback, baseUrl = DEFAULT_API_BASE_URL) {
30
- const url = `${baseUrl}?method=getPrefectures&area=${encodeURIComponent(area || '')}`;
31
- const result = await fetchJson(url);
32
- if (typeof callback === 'function') {
33
- callback(result);
34
- }
35
- return result;
36
- }
37
-
38
- async function onGetCitiesByPrefecture(area, prefecture, callback, baseUrl = DEFAULT_API_BASE_URL) {
39
- const queryArea = area ? `&area=${encodeURIComponent(area)}` : '';
40
- const url = `${baseUrl}?method=getCities${queryArea}&prefecture=${encodeURIComponent(prefecture || '')}`;
41
- const result = await fetchJson(url);
42
- if (typeof callback === 'function') {
43
- callback(result);
44
- }
45
- return result;
46
- }
47
-
48
- async function onGetTownsByCity(city, prefecture, callback, baseUrl = DEFAULT_API_BASE_URL) {
49
- const url = `${baseUrl}?method=getTowns&city=${encodeURIComponent(city || '')}&prefecture=${encodeURIComponent(prefecture || '')}`;
50
- const result = await fetchJson(url);
51
- if (typeof callback === 'function') {
52
- callback(result);
53
- }
54
- return result;
55
- }
56
-
57
- async function onSearchAddressByPostcode(postcode, isResponse = false, baseUrl = DEFAULT_API_BASE_URL) {
58
- const cleanCode = normalizePostal(postcode);
59
- if (!cleanCode) return isResponse ? null : '';
60
-
61
- const url = `${baseUrl}?method=searchByPostal&postal=${encodeURIComponent(cleanCode)}`;
62
- const response = await fetchJson(url);
63
-
64
- if (!response) {
65
- return isResponse ? null : '';
66
- }
67
-
68
- const dataAddress = response?.location;
69
- if (Array.isArray(dataAddress) && dataAddress.length > 0) {
70
- const address = dataAddress[0];
71
- if (isResponse) {
72
- return address;
73
- }
74
- return `${address.prefecture || ''}${address.city || ''}${address.town || ''}`;
75
- }
76
-
77
- return isResponse ? null : '';
78
- }
79
-
80
- async function onSearchPostcodeByAddress(
81
- address,
82
- isResponse = false,
83
- matching = 'suffix',
84
- baseUrl = DEFAULT_API_BASE_URL
85
- ) {
86
- if (!address || typeof address !== 'string' || !address.trim()) {
87
- return isResponse ? null : '';
88
- }
89
-
90
- const url = `${baseUrl}?method=suggest&matching=${encodeURIComponent(matching)}&keyword=${encodeURIComponent(address.trim())}`;
91
- const response = await fetchJson(url);
92
-
93
- if (!response) {
94
- return isResponse ? null : '';
95
- }
96
-
97
- const dataAddress = response?.location;
98
- if (Array.isArray(dataAddress) && dataAddress.length > 0) {
99
- const item = dataAddress[0];
100
- return isResponse ? item : item?.postal || '';
101
- }
102
-
103
- return isResponse ? null : '';
104
- }
105
-
106
- async function searchAddressByPostal(postal, { raw = false, all = false, baseUrl = DEFAULT_API_BASE_URL } = {}) {
107
- const clean = normalizePostal(postal);
108
- if (!clean) return all ? [] : raw ? null : '';
109
-
110
- const url = `${baseUrl}?method=searchByPostal&postal=${encodeURIComponent(clean)}`;
111
- const response = await fetchJson(url);
112
- const locations = response?.location || [];
113
-
114
- if (!locations.length) {
115
- return all ? [] : raw ? null : '';
116
- }
117
-
118
- if (all) {
119
- return locations;
120
- }
121
-
122
- const first = locations[0];
123
- if (raw) {
124
- return first;
125
- }
126
-
127
- return `${first.prefecture || ''}${first.city || ''}${first.town || ''}`;
128
- }
129
-
130
- async function searchPostalByAddress(
131
- address,
132
- { raw = false, all = false, matching = 'suffix', baseUrl = DEFAULT_API_BASE_URL } = {}
133
- ) {
134
- if (!address || typeof address !== 'string' || !address.trim()) {
135
- return all ? [] : raw ? null : '';
136
- }
137
-
138
- const url = `${baseUrl}?method=suggest&matching=${encodeURIComponent(matching)}&keyword=${encodeURIComponent(address.trim())}`;
139
- const response = await fetchJson(url);
140
- const locations = response?.location || [];
141
-
142
- if (!locations.length) {
143
- return all ? [] : raw ? null : '';
144
- }
145
-
146
- if (all) {
147
- return locations;
148
- }
149
-
150
- const first = locations[0];
151
- return raw ? first : first.postal || '';
152
- }
153
-
154
- async function getAreas(baseUrl = DEFAULT_API_BASE_URL) {
155
- const url = `${baseUrl}?method=getAreas`;
156
- const response = await fetchJson(url);
157
- return response?.area || [];
158
- }
159
-
160
- async function getPrefectures(area, baseUrl = DEFAULT_API_BASE_URL) {
161
- const response = await getPrefecturesByArea(area, null, baseUrl);
162
- return response?.prefecture || [];
163
- }
164
-
165
- async function getCities(prefecture, area = '', baseUrl = DEFAULT_API_BASE_URL) {
166
- const response = await onGetCitiesByPrefecture(area, prefecture, null, baseUrl);
167
- const locations = response?.location || [];
168
- return locations.map((item) => (typeof item === 'string' ? item : item.city)).filter(Boolean);
169
- }
170
-
171
- async function getTowns(city, prefecture = '', baseUrl = DEFAULT_API_BASE_URL) {
172
- const response = await onGetTownsByCity(city, prefecture, null, baseUrl);
173
- const locations = response?.location || [];
174
- return locations.map((item) => (typeof item === 'string' ? item : item.town)).filter(Boolean);
175
- }
176
-
177
- module.exports = {
178
- DEFAULT_API_BASE_URL,
179
- normalizePostal,
180
- getPrefecturesByArea,
181
- onGetCitiesByPrefecture,
182
- onGetTownsByCity,
183
- onSearchAddressByPostcode,
184
- onSearchPostcodeByAddress,
185
- searchAddressByPostal,
186
- searchPostalByAddress,
187
- getAreas,
188
- getPrefectures,
189
- getCities,
190
- getTowns
191
- };
package/src/index.js DELETED
@@ -1,297 +0,0 @@
1
- /**
2
- * jp-postal-lookup
3
- * Lightweight library to search Japanese addresses, postal codes, prefectures, cities, and towns
4
- * using HeartRails Geo API with zero runtime dependencies.
5
- *
6
- * Compatible with Node.js 18+ and modern browsers.
7
- */
8
-
9
- export const DEFAULT_API_BASE_URL = 'https://geoapi.heartrails.com/api/json';
10
-
11
- /**
12
- * Format postal code by removing hyphens and full-width spaces.
13
- * E.g., "100-0001" -> "1000001"
14
- * @param {string|number} postal
15
- * @returns {string}
16
- */
17
- export function normalizePostal(postal) {
18
- if (postal === null || postal === undefined) return '';
19
- return String(postal)
20
- .replace(/[ー--]/g, '')
21
- .replace(/[\s\u3000]/g, '')
22
- .trim();
23
- }
24
-
25
- /**
26
- * Internal fetch helper with JSON parsing and error handling.
27
- * @param {string} url
28
- * @param {RequestInit} [options]
29
- * @returns {Promise<any>}
30
- */
31
- async function fetchJson(url, options = {}) {
32
- const fetchFn = typeof globalThis.fetch === 'function' ? globalThis.fetch : null;
33
- if (!fetchFn) {
34
- throw new Error('jp-postal-lookup: Fetch API is not available in this environment.');
35
- }
36
-
37
- try {
38
- const response = await fetchFn(url, options);
39
- if (!response.ok) {
40
- return null;
41
- }
42
- const data = await response.json();
43
- return data?.response ?? null;
44
- } catch {
45
- return null;
46
- }
47
- }
48
-
49
- /**
50
- * Get list of prefectures by area.
51
- * @param {string} area - e.g., '関東', '近畿', '中部', etc.
52
- * @param {Function} [callback] - Optional callback (response) => void
53
- * @param {string} [baseUrl]
54
- * @returns {Promise<any>}
55
- */
56
- export async function getPrefecturesByArea(area, callback, baseUrl = DEFAULT_API_BASE_URL) {
57
- const url = `${baseUrl}?method=getPrefectures&area=${encodeURIComponent(area || '')}`;
58
- const result = await fetchJson(url);
59
- if (typeof callback === 'function') {
60
- callback(result);
61
- }
62
- return result;
63
- }
64
-
65
- /**
66
- * Get cities by area and prefecture.
67
- * @param {string} area
68
- * @param {string} prefecture - e.g., '東京都'
69
- * @param {Function} [callback]
70
- * @param {string} [baseUrl]
71
- * @returns {Promise<any>}
72
- */
73
- export async function onGetCitiesByPrefecture(area, prefecture, callback, baseUrl = DEFAULT_API_BASE_URL) {
74
- const queryArea = area ? `&area=${encodeURIComponent(area)}` : '';
75
- const url = `${baseUrl}?method=getCities${queryArea}&prefecture=${encodeURIComponent(prefecture || '')}`;
76
- const result = await fetchJson(url);
77
- if (typeof callback === 'function') {
78
- callback(result);
79
- }
80
- return result;
81
- }
82
-
83
- /**
84
- * Get towns by city and prefecture.
85
- * @param {string} city - e.g., '千代田区'
86
- * @param {string} prefecture - e.g., '東京都'
87
- * @param {Function} [callback]
88
- * @param {string} [baseUrl]
89
- * @returns {Promise<any>}
90
- */
91
- export async function onGetTownsByCity(city, prefecture, callback, baseUrl = DEFAULT_API_BASE_URL) {
92
- const url = `${baseUrl}?method=getTowns&city=${encodeURIComponent(city || '')}&prefecture=${encodeURIComponent(prefecture || '')}`;
93
- const result = await fetchJson(url);
94
- if (typeof callback === 'function') {
95
- callback(result);
96
- }
97
- return result;
98
- }
99
-
100
- /**
101
- * Search address by postal code.
102
- * @param {string|number} postcode - Japanese postal code (with or without hyphen)
103
- * @param {boolean} [isResponse=false] - If true, returns the raw location object; otherwise returns formatted address string
104
- * @param {string} [baseUrl]
105
- * @returns {Promise<string|object|null>}
106
- */
107
- export async function onSearchAddressByPostcode(postcode, isResponse = false, baseUrl = DEFAULT_API_BASE_URL) {
108
- const cleanCode = normalizePostal(postcode);
109
- if (!cleanCode) return isResponse ? null : '';
110
-
111
- const url = `${baseUrl}?method=searchByPostal&postal=${encodeURIComponent(cleanCode)}`;
112
- const response = await fetchJson(url);
113
-
114
- if (!response) {
115
- return isResponse ? null : '';
116
- }
117
-
118
- const dataAddress = response?.location;
119
- if (Array.isArray(dataAddress) && dataAddress.length > 0) {
120
- const address = dataAddress[0];
121
- if (isResponse) {
122
- return address;
123
- }
124
- return `${address.prefecture || ''}${address.city || ''}${address.town || ''}`;
125
- }
126
-
127
- return isResponse ? null : '';
128
- }
129
-
130
- /**
131
- * Search postal code by address string.
132
- * @param {string} address - Address keyword (e.g. '東京都千代田区')
133
- * @param {boolean} [isResponse=false] - If true, returns the raw location object; otherwise returns postal string
134
- * @param {string} [matching='suffix'] - 'prefix', 'suffix', or 'like'
135
- * @param {string} [baseUrl]
136
- * @returns {Promise<string|object|null>}
137
- */
138
- export async function onSearchPostcodeByAddress(
139
- address,
140
- isResponse = false,
141
- matching = 'suffix',
142
- baseUrl = DEFAULT_API_BASE_URL
143
- ) {
144
- if (!address || typeof address !== 'string' || !address.trim()) {
145
- return isResponse ? null : '';
146
- }
147
-
148
- const url = `${baseUrl}?method=suggest&matching=${encodeURIComponent(matching)}&keyword=${encodeURIComponent(address.trim())}`;
149
- const response = await fetchJson(url);
150
-
151
- if (!response) {
152
- return isResponse ? null : '';
153
- }
154
-
155
- const dataAddress = response?.location;
156
- if (Array.isArray(dataAddress) && dataAddress.length > 0) {
157
- const item = dataAddress[0];
158
- return isResponse ? item : item?.postal || '';
159
- }
160
-
161
- return isResponse ? null : '';
162
- }
163
-
164
- // -------------------------------------------------------------
165
- // Modern Idiomatic Aliases
166
- // -------------------------------------------------------------
167
-
168
- /**
169
- * Search full address by postal code.
170
- * @param {string|number} postal
171
- * @param {object} [options]
172
- * @param {boolean} [options.raw=false]
173
- * @param {boolean} [options.all=false] - Return array of all matched locations
174
- * @param {string} [options.baseUrl]
175
- * @returns {Promise<string|object|object[]|null>}
176
- */
177
- export async function searchAddressByPostal(postal, { raw = false, all = false, baseUrl = DEFAULT_API_BASE_URL } = {}) {
178
- const clean = normalizePostal(postal);
179
- if (!clean) return all ? [] : raw ? null : '';
180
-
181
- const url = `${baseUrl}?method=searchByPostal&postal=${encodeURIComponent(clean)}`;
182
- const response = await fetchJson(url);
183
- const locations = response?.location || [];
184
-
185
- if (!locations.length) {
186
- return all ? [] : raw ? null : '';
187
- }
188
-
189
- if (all) {
190
- return locations;
191
- }
192
-
193
- const first = locations[0];
194
- if (raw) {
195
- return first;
196
- }
197
-
198
- return `${first.prefecture || ''}${first.city || ''}${first.town || ''}`;
199
- }
200
-
201
- /**
202
- * Search postal code by address keyword.
203
- * @param {string} address
204
- * @param {object} [options]
205
- * @param {boolean} [options.raw=false]
206
- * @param {boolean} [options.all=false]
207
- * @param {'prefix'|'suffix'|'like'} [options.matching='suffix']
208
- * @param {string} [options.baseUrl]
209
- * @returns {Promise<string|object|object[]|null>}
210
- */
211
- export async function searchPostalByAddress(
212
- address,
213
- { raw = false, all = false, matching = 'suffix', baseUrl = DEFAULT_API_BASE_URL } = {}
214
- ) {
215
- if (!address || typeof address !== 'string' || !address.trim()) {
216
- return all ? [] : raw ? null : '';
217
- }
218
-
219
- const url = `${baseUrl}?method=suggest&matching=${encodeURIComponent(matching)}&keyword=${encodeURIComponent(address.trim())}`;
220
- const response = await fetchJson(url);
221
- const locations = response?.location || [];
222
-
223
- if (!locations.length) {
224
- return all ? [] : raw ? null : '';
225
- }
226
-
227
- if (all) {
228
- return locations;
229
- }
230
-
231
- const first = locations[0];
232
- return raw ? first : first.postal || '';
233
- }
234
-
235
- /**
236
- * Get areas in Japan (Hokkaido, Tohoku, Kanto, Chubu, Kinki, Chugoku, Shikoku, Kyushu).
237
- * @param {string} [baseUrl]
238
- * @returns {Promise<string[]>}
239
- */
240
- export async function getAreas(baseUrl = DEFAULT_API_BASE_URL) {
241
- const url = `${baseUrl}?method=getAreas`;
242
- const response = await fetchJson(url);
243
- return response?.area || [];
244
- }
245
-
246
- /**
247
- * Get prefectures in an area.
248
- * @param {string} area
249
- * @param {string} [baseUrl]
250
- * @returns {Promise<string[]>}
251
- */
252
- export async function getPrefectures(area, baseUrl = DEFAULT_API_BASE_URL) {
253
- const response = await getPrefecturesByArea(area, null, baseUrl);
254
- return response?.prefecture || [];
255
- }
256
-
257
- /**
258
- * Get cities in a prefecture.
259
- * @param {string} prefecture
260
- * @param {string} [area]
261
- * @param {string} [baseUrl]
262
- * @returns {Promise<string[]>}
263
- */
264
- export async function getCities(prefecture, area = '', baseUrl = DEFAULT_API_BASE_URL) {
265
- const response = await onGetCitiesByPrefecture(area, prefecture, null, baseUrl);
266
- const locations = response?.location || [];
267
- return locations.map((item) => (typeof item === 'string' ? item : item.city)).filter(Boolean);
268
- }
269
-
270
- /**
271
- * Get towns in a city.
272
- * @param {string} city
273
- * @param {string} [prefecture]
274
- * @param {string} [baseUrl]
275
- * @returns {Promise<string[]>}
276
- */
277
- export async function getTowns(city, prefecture = '', baseUrl = DEFAULT_API_BASE_URL) {
278
- const response = await onGetTownsByCity(city, prefecture, null, baseUrl);
279
- const locations = response?.location || [];
280
- return locations.map((item) => (typeof item === 'string' ? item : item.town)).filter(Boolean);
281
- }
282
-
283
- export default {
284
- DEFAULT_API_BASE_URL,
285
- normalizePostal,
286
- getPrefecturesByArea,
287
- onGetCitiesByPrefecture,
288
- onGetTownsByCity,
289
- onSearchAddressByPostcode,
290
- onSearchPostcodeByAddress,
291
- searchAddressByPostal,
292
- searchPostalByAddress,
293
- getAreas,
294
- getPrefectures,
295
- getCities,
296
- getTowns
297
- };