ไลบรารี JavaScript เพื่อร้องขอไปยัง What3words REST API ขณะนี้มีการสนับสนุนที่ดีขึ้นสำหรับการใช้งานภายในทั้งเบราว์เซอร์และสภาพแวดล้อมที่ใช้โหนด! ดูเอกสาร API สาธารณะ 3 คำสำหรับข้อมูลเพิ่มเติมเกี่ยวกับวิธีการใช้ REST API ของเรา
what3words JavaScript wrapper ช่วยให้คุณเข้าถึงโปรแกรม:
NPM:
npm install @what3words/api
เส้นด้าย:
yarn add @what3words/api
หากคุณต้องการใช้การขนส่งในตัวคุณจะต้องติดตั้งการพึ่งพาเพียร์สำหรับพวกเขา สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการขนส่งเริ่มต้นอ่านส่วนเกี่ยวกับการขนส่ง
const what3words ,
{ fetchTransport } = require ( '@what3words/api' ) ;
const apiKey = '<YOUR_API_KEY>' ;
const config = {
host : 'https://api.what3words.com' ,
apiVersion : 'v3' ,
} ;
const transport = fetchTransport ( ) ; // or you can import 'axiosTransport' instead
const w3wService = what3words ( apiKey , config , { transport } ) ;
// you can uncomment the following lines to set your api key and config after instantiation of the w3w service
// w3wService.setApiKey(apiKey);
// w3wService.setConfig(config);
import what3words , {
ApiVersion ,
Transport ,
What3wordsService ,
axiosTransport ,
} from '@what3words/api' ;
const apiKey = '<YOUR_API_KEY>' ;
const config : {
host : string ;
apiVersion : ApiVersion ;
} = {
host : 'https://api.what3words.com' ,
apiVersion : ApiVersion . Version3 ,
} ;
const transport : Transport = axiosTransport ( ) ;
const w3wService : What3wordsService = what3words ( apiKey , config , { transport } ) ;
// code continues...
What3wordsService
ให้วิธีที่ง่ายและง่ายดายในการสร้างอินสแตนซ์ไคลเอนต์ API ที่สามารถใช้เพื่อร้องขอกับสิ่งที่ API 3 คำ นอกจากนี้ยังมีฟังก์ชั่นผู้ช่วยสำหรับการตั้งค่าการกำหนดค่า API เช่นโฮสต์และเวอร์ชัน API และคีย์ API ของคุณผ่านไคลเอนต์ API ของ What3words
ไคลเอนต์ API ของ What3words ในไลบรารีนี้ใช้เพื่อตรวจสอบตัวเลือกการร้องขอ, อนุกรมและจัดการคำขอ/การตอบกลับและข้อผิดพลาดกับจุดสิ้นสุด API ไคลเอนต์แต่ละรายขยายคลาส Abstract ApiClient
มีไคลเอนต์เฉพาะสำหรับแต่ละคำขอและคุณสามารถใช้งานได้อย่างอิสระจาก What3wordsService
สิ่งนี้มีประโยชน์อย่างยิ่งหากคุณต้องการขยายพฤติกรรมไคลเอนต์ลดรหัสของคุณหรือในตัวอย่างที่รุนแรงยิ่งขึ้นให้ใช้การขนส่งที่กำหนดเองเพื่อจัดการคำขอที่แตกต่างกันในไคลเอนต์แต่ละตัว
ลูกค้าทุกคนยอมรับพารามิเตอร์ต่อไปนี้:
พารามิเตอร์ | ข้อมูล | ค่าเริ่มต้น |
---|---|---|
apikey | สาย | '' |
การกำหนดค่า | config.host | https://api.what3words.com |
config.apiversion | v3 |
การขนส่งเป็นฟังก์ชั่นที่รับผิดชอบในการดำเนินการตามคำขอกับ API เมื่อพิจารณา ClientRequest
การขนส่งควรส่งคืนสัญญาที่แก้ไขไปยัง TransportResponse
ClientRequest
ประกอบด้วยคุณสมบัติต่อไปนี้:
คุณสมบัติ | ข้อมูล |
---|---|
เจ้าภาพ * | string |
URL * | string |
วิธี * | get หรือ post |
สอบถาม | object |
ส่วนหัว | object |
ร่างกาย | object |
รูปแบบ * | json หรือ geojson ค่าเริ่มต้น: json |
TransportResponse
ประกอบด้วยคุณสมบัติต่อไปนี้:
คุณสมบัติ | ข้อมูล |
---|---|
สถานะ * | number |
Statustext * | string |
ร่างกาย * | any |
ส่วนหัว | object |
มีการขนส่งในตัวสองแบบพร้อมห้องสมุดนี้ที่คุณสามารถใช้ได้ ไม่ว่าจะเป็นข้ามหรือ axios โดยการระบุการขนส่งที่คุณต้องการใช้ในการเริ่มต้นของ What3wordsService
หรือลูกค้าหากคุณต้องการสร้างอินสแตนซ์ลูกค้าด้วยตัวคุณเอง
มีการขนส่งในตัวสองแบบ:
เพื่อที่จะใช้อย่างใดอย่างหนึ่งคุณจะต้องติดตั้งการพึ่งพาเพียร์ โดยค่าเริ่มต้น cross-fetch จะถูกสันนิษฐานโดย What3wordsService
หรือไคลเอนต์อินสแตนซ์ใด ๆ ที่ไม่มีการแทนที่
NPM:
npm install cross-fetch
หรือ
npm install axios
เส้นด้าย:
yarn add cross-fetch
หรือ
yarn add axios
คุณสามารถให้การขนส่งที่กำหนดเองของคุณเองหากคุณต้องการใช้ห้องสมุดอื่นสำหรับการจัดการคำขอซึ่งอาจเป็นประโยชน์หากคุณมีการรวมเข้าด้วยกันอื่น ๆ หรือคุณใช้ไลบรารี HTTP ที่อื่นแล้ว
ในการทำเช่นนั้นคุณต้องกำหนดการ Transport
ของคุณเองและส่งผ่านไปยัง What3wordsService
หรือลูกค้าเพื่อใช้
Transport
ที่กำหนดเองที่คุณสร้างควรเป็นฟังก์ชั่นที่ยอมรับ ClientRequest
เป็นอาร์กิวเมนต์และส่งคืนสัญญาที่แก้ไขไปยัง TransportResponse
import what3words , { ClientRequest , TransportResponse } from '@what3words/api' ;
import superagent from 'superagent' ;
const API_KEY = '<YOUR_API_KEY>' ;
const config = { } ; // This will ensure we do not override the defaults
function customTransport < ResponseType > (
request : ClientRequest
) : Promise < TransportResponse < ResponseType > > {
const {
method ,
host ,
url ,
query = { } ,
headers = { } ,
body = { } ,
format ,
} = request ;
return new Promise ( resolve =>
superagent [ method ] ( ` ${ host } ${ url } ` )
. query ( { ... query , format } )
. send ( body || { } )
. set ( headers )
. end ( ( err , res ) => {
if ( err || ! res )
return resolve ( {
status : err . status || 500 ,
statusText : err . response . text || 'Internal Server Error' ,
headers : err . headers || { } ,
body : err . response . text || null ,
} ) ;
const response : TransportResponse < ResponseType > = {
status : res . status ,
statusText : res . text ,
headers : res . headers ,
body : res . body ,
} ;
resolve ( response ) ;
} )
) ;
}
const service = what3words ( API_KEY , config , { transport : customTransport } ) ;
service
. availableLanguages ( )
. then ( ( { languages } ) => console . log ( 'Available languages' , languages ) ) ;
import {
AutosuggestClient ,
AutosuggestOptions ,
AutosuggestResponse ,
} from '@what3words/api' ;
const API_KEY = '<YOUR_API_KEY>' ;
const client : AutosuggestClient = AutosuggestClient . init ( API_KEY ) ;
const options : AutosuggestOptions = {
input : 'filled.count.s' ,
} ;
client
. run ( options )
. then ( ( res : AutosuggestResponse ) =>
console . log ( `suggestions for " ${ options . input } "` , res )
) ;
import {
ConvertToCoordinatesClient ,
ConvertToCoordinatesOptions ,
FeatureCollectionResponse ,
LocationGeoJsonResponse ,
LocationJsonResponse ,
} from '@what3words/api' ;
const API_KEY = '<YOUR_API_KEY>' ;
const client : ConvertToCoordinatesClient =
ConvertToCoordinatesClient . init ( API_KEY ) ;
const options : ConvertToCoordinatesOptions = { words : 'filled.count.soap' } ;
// If you want to retrieve the JSON response from our API
client
. run ( { ... options , format : 'json' } ) // { format: 'json' } is the default response
. then ( ( res : LocationJsonResponse ) =>
console . log ( 'Convert to coordinates' , res )
) ;
// If you want to retrieve the GeoJsonResponse from our API
client
. run ( { ... options , format : 'geojson' } )
. then ( ( res : FeatureCollectionResponse < LocationGeoJsonResponse > ) =>
console . log ( 'Convert to coordinates' , res )
) ;
import {
ConvertTo3waClient ,
ConvertTo3waOptions ,
FeatureCollectionResponse ,
LocationGeoJsonResponse ,
LocationJsonResponse ,
} from '@what3words/api' ;
const API_KEY = '<YOUR_API_KEY>' ;
const client : ConvertTo3waClient = ConvertTo3waClient . init ( API_KEY ) ;
const options : ConvertTo3waOptions = {
coordinates : { lat : 51.520847 , lng : - 0.195521 } ,
} ;
// If you want to retrieve the JSON response from our API
client
. run ( { ... options , format : 'json' } ) // { format: 'json' } is the default response
. then ( ( res : LocationJsonResponse ) => console . log ( 'Convert to 3wa' , res ) ) ;
// If you want to retrieve the GeoJsonResponse from our API
client
. run ( { ... options , format : 'geojson' } )
. then ( ( res : FeatureCollectionResponse < LocationGeoJsonResponse > ) =>
console . log ( 'Convert to 3wa' , res )
) ;
import {
AvailableLanguagesClient ,
AvailableLanguagesResponse ,
} from '@what3words/api' ;
const API_KEY = '<YOUR_API_KEY>' ;
const client : AvailableLanguagesClient = AvailableLanguagesClient . init ( API_KEY ) ;
client
. run ( )
. then ( ( res : AvailableLanguagesResponse ) =>
console . log ( 'Available Languages' , res )
) ;
import {
GridSectionClient ,
GridSectionOptions ,
FeatureCollectionResponse ,
GridSectionGeoJsonResponse ,
GridSectionJsonResponse ,
} from '../src' ;
const API_KEY = '<YOUR_API_KEY>' ;
const client : GridSectionClient = GridSectionClient . init ( API_KEY ) ;
const options : GridSectionOptions = {
boundingBox : {
southwest : { lat : 52.208867 , lng : 0.11754 } ,
northeast : { lat : 52.207988 , lng : 0.116126 } ,
} ,
} ;
// If you want to retrieve the JSON response from our API
client
. run ( { ... options , format : 'json' } ) // { format: 'json' } is the default response
. then ( ( res : GridSectionJsonResponse ) => console . log ( 'Grid Section' , res ) ) ;
// If you want to retrieve the JSON response from our API
client
. run ( { ... options , format : 'geojson' } ) // { format: 'json' } is the default response
. then ( ( res : FeatureCollectionResponse < GridSectionGeoJsonResponse > ) =>
console . log ( 'Grid Section' , res )
) ;
กล่องที่ร้องขอจะต้องไม่เกิน 4 กม. จากมุมหนึ่งไปยังอีกมุมหนึ่งหรือข้อผิดพลาด badboundingboxtoobig จะถูกส่งคืน ละติจูดจะต้องเป็น> = -90 และ <= 90 แต่ longitudes ได้รับอนุญาตให้ห่อประมาณ 180 เพื่อระบุขอบเขตของกล่องที่ข้ามการต่อต้านการใช้งานใช้ลองจิจูดมากกว่า 180
import {
GridSectionClient ,
GridSectionOptions ,
FeatureCollectionResponse ,
GridSectionGeoJsonResponse ,
GridSectionJsonResponse ,
} from '../src' ;
const API_KEY = '<YOUR_API_KEY>' ;
const client : GridSectionClient = GridSectionClient . init ( API_KEY ) ;
const options : GridSectionOptions = {
boundingBox : {
southwest : { lat : 52.208867 , lng : 0.11754 } ,
northeast : { lat : 52.207988 , lng : 0.116126 } ,
} ,
} ;
// Search a string for any character sequences that could be three word addresses
client . findPossible3wa ( 'filled.count.soap' ) ; // returns ['filled.count.soap']
client . findPossible3wa (
'this string contains a three word address substring: filled.count.soap'
) ; // returns ['filled.count.soap']
client . findPossible3wa ( 'filled.count' ) ; // returns []
// Search a string for any character sequences that could be three word addresses
client . isPossible3wa ( 'filled.count.soap' ) ; // returns true
client . isPossible3wa (
'this string contains a three word address substring: filled.count.soap'
) ; // returns false
client . isPossible3wa ( 'filled.count' ) ; // returns false
// Search a string for any character sequences that could be three word addresses
client . isValid3wa ( 'filled.count.soap' ) ; // returns Promise<true>
client . isValid3wa (
'this string contains a three word address substring: filled.count.soap'
) ; // returns Promise<false>
client . isValid3wa ( 'filled.count.negative' ) ; // returns Promise<false>