What3Words REST APIにリクエストを行うJavaScriptライブラリ。ブラウザベースの環境とノードベースの両方の環境での使用をよりよくサポートしています! REST APIの使用方法の詳細については、what3words public APIドキュメントを参照してください。
what3words javascriptラッパーは、次のようにプログラム的なアクセスを提供します。
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 whow3words APIに対してリクエストを行うために使用できるAPIクライアントをすばやく簡単にインスタンス化する方法を提供します。また、HostやAPIバージョンなどのAPI構成を設定するためのヘルパー関数を提供し、APIキーはwhow3words APIクライアントに渡ります。
このライブラリのwhat3words APIクライアントは、リクエストオプション、シリアル化、およびAPIエンドポイントに対するリクエスト/応答、およびエラーを検証するために使用されます。各クライアントは、抽象的なApiClientクラスを拡張します。
各リクエストに特定のクライアントがあり、 What3wordsServiceとは独立してそれらを使用できます。これは、クライアントの動作を拡張したり、コードを最小限に抑えたり、より極端な例では、各クライアントでリクエストを異なる方法で処理するためにカスタムトランスポートを使用したりする場合に特に便利です。
すべてのクライアントが次のパラメーターを受け入れます。
| パラメーター | データタイプ | デフォルト値 |
|---|---|---|
| アピケイ | 弦 | '' |
| config | 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 |
このライブラリには、使用できる2つの組み込みトランスポートがあります。クロスフェッチまたはaxiosのいずれか。自分のためにクライアントをインスタンス化したい場合は、 What3wordsServiceまたはクライアントの初期化に使用したいトランスポートを指定することにより。
利用可能な2つの組み込みトランスポートがあります。
これらのいずれかを使用するには、ピア依存関係をインストールする必要があります。デフォルトでは、Cross-Fetchは、Overrideが提供されていない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 )
) ;要求されたボックスはコーナーからコーナーまで4kmを超えてはなりません。または、BadboundingBoxTOOBIGエラーが返されます。緯度は> = -90および<= 90でなければなりませんが、長期は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>