Perpustakaan JavaScript untuk membuat permintaan ke What3Words REST API. Sekarang dengan dukungan yang lebih baik untuk digunakan dalam lingkungan berbasis browser dan node! Lihat dokumentasi API publik What3Words untuk informasi lebih lanjut tentang cara menggunakan API REST kami.
Pembungkus JavaScript What3Words memberi Anda akses terprogram ke:
NPM:
npm install @what3words/api
benang:
yarn add @what3words/api
Jika Anda ingin menggunakan transportasi bawaan, Anda juga perlu memasang dependensi rekan untuk mereka. Untuk informasi lebih lanjut tentang transportasi default, baca bagian transportasi.
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
menyediakan cara cepat dan mudah untuk membuat instantiate klien API yang dapat digunakan untuk membuat permintaan terhadap API What3Words. Ini juga menyediakan fungsi penolong untuk mengatur konfigurasi API, seperti versi host dan API dan kunci API Anda di seluruh klien API What3Words.
Klien What3Words API di perpustakaan ini digunakan untuk memvalidasi opsi permintaan, serial dan menangani permintaan/respons dan kesalahan terhadap titik akhir API. Setiap klien memperluas kelas ApiClient
abstrak.
Ada klien tertentu untuk setiap permintaan dan Anda dapat menggunakannya secara independen dari What3wordsService
. Ini bisa sangat berguna jika Anda ingin memperluas perilaku klien, meminimalkan kode Anda atau, dalam contoh yang lebih ekstrem, menggunakan transportasi khusus untuk menangani permintaan secara berbeda di setiap klien.
Setiap klien menerima parameter berikut:
Parameter | Tipe data | Nilai default |
---|---|---|
apikey | rangkaian | '' |
konfigurasi | config.host | https://api.what3words.com |
Config.Apignion | v3 |
Transportasi adalah fungsi yang bertanggung jawab untuk melaksanakan permintaan terhadap API. Diberi ClientRequest
transportasi harus mengembalikan janji yang diselesaikan menjadi TransportResponse
.
ClientRequest
terdiri dari properti berikut:
Milik | Tipe data |
---|---|
tuan rumah * | string |
URL * | string |
Metode * | get atau post |
pertanyaan | object |
header | object |
tubuh | object |
Format * | json atau geojson . Default: json |
TransportResponse
terdiri dari properti berikut:
Milik | Tipe data |
---|---|
Status * | number |
Statustext * | string |
tubuh * | any |
header | object |
Ada dua transportasi bawaan yang tersedia dengan perpustakaan ini yang dapat Anda gunakan; baik cross-fetch atau axios. Dengan menentukan transportasi mana yang ingin Anda gunakan pada inisialisasi dari What3wordsService
atau klien, jika Anda ingin instantiate klien untuk diri sendiri.
Ada dua transportasi bawaan yang tersedia:
Untuk menggunakan salah satu dari ini, Anda perlu menginstal ketergantungan rekan. Secara default, Cross-Fetch diasumsikan oleh What3wordsService
atau klien yang tidak parah di mana tidak ada override yang disediakan.
NPM:
npm install cross-fetch
atau
npm install axios
benang:
yarn add cross-fetch
atau
yarn add axios
Anda dapat memberikan transportasi khusus Anda sendiri, jika Anda ingin menggunakan perpustakaan lain untuk menangani permintaan, yang mungkin berguna jika Anda memiliki integrasi lain atau Anda sudah menggunakan perpustakaan HTTP di tempat lain.
Untuk melakukannya, Anda perlu mendefinisikan Transport
Anda sendiri dan meneruskannya ke What3wordsService
atau klien untuk menggunakannya.
Transport
kustom yang Anda buat harus menjadi fungsi yang menerima ClientRequest
sebagai argumen dan mengembalikan janji yang diselesaikan menjadi 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 )
) ;
Kotak yang diminta tidak boleh melebihi 4 km dari sudut ke sudut, atau kesalahan badboundingbloxoobig akan dikembalikan. Lintang harus> = -90 dan <= 90, tetapi bujur diizinkan untuk membungkus sekitar 180. Untuk menentukan kotak pembatas yang melintasi anti-Meridian, menggunakan bujur lebih besar dari 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>