@@ -53,13 +53,14 @@ If you wish to use the built-in transports you will also need to install the pee
5353### JavaScript
5454
5555``` javascript
56- const what3words , { fetchTransport } = require (" @what3words/api" );
56+ const what3words ,
57+ { fetchTransport } = require (' @what3words/api' );
5758
5859const apiKey = ' <YOUR_API_KEY>' ;
5960const config = {
6061 host: ' https://api.what3words.com' ,
6162 apiVersion: ' v3' ,
62- }
63+ };
6364const transport = fetchTransport (); // or you can import 'axiosTransport' instead
6465const w3wService = what3words (apiKey, config, { transport });
6566
@@ -71,12 +72,17 @@ const w3wService = what3words(apiKey, config, { transport });
7172### Typescript
7273
7374``` typescript
74- import what3words , { ApiVersion , Transport , What3wordsService , axiosTransport } from ' @what3words/api' ;
75+ import what3words , {
76+ ApiVersion ,
77+ Transport ,
78+ What3wordsService ,
79+ axiosTransport ,
80+ } from ' @what3words/api' ;
7581
76- const apiKey: string = ' <YOUR_API_KEY>' ;
82+ const apiKey = ' <YOUR_API_KEY>' ;
7783const config: {
78- host: string ,
79- apiVersion: ApiVersion ,
84+ host: string ;
85+ apiVersion: ApiVersion ;
8086} = {
8187 host: ' https://api.what3words.com' ,
8288 apiVersion: ApiVersion .Version3 ,
@@ -179,95 +185,180 @@ import what3words, { ClientRequest, TransportResponse } from '@what3words/api';
179185import superagent from ' superagent' ;
180186
181187const API_KEY = ' <YOUR_API_KEY>' ;
182- const config = {} // This will ensure we do not override the defaults
183-
184- async function customTransport(request : ClientRequest ): Promise <TransportResponse > {
185- const { method, host, url, query = {}, headers = {}, body = {}, format } = request ;
186- return superagent [method ](` ${host }${url } ` )
187- .query ({ ... query , format })
188- .send (body )
189- .set (headers )
190- .end ((err , res ) => {
191- if (err ) throw err ;
192- const reponse: TransportResponse = {
193- status: res .status ,
194- statusText: res .
195- headers : res .headers ,
196- body: res .body ,
197- };
198- return response ;
199- })
188+ const config = {}; // This will ensure we do not override the defaults
189+
190+ function customTransport<ResponseType >(
191+ request : ClientRequest
192+ ): Promise <TransportResponse <ResponseType >> {
193+ const {
194+ method,
195+ host,
196+ url,
197+ query = {},
198+ headers = {},
199+ body = {},
200+ format,
201+ } = request ;
202+ return new Promise (resolve =>
203+ superagent [method ](` ${host }${url } ` )
204+ .query ({ ... query , format })
205+ .send (body || {})
206+ .set (headers )
207+ .end ((err , res ) => {
208+ if (err || ! res )
209+ return resolve ({
210+ status: err .status || 500 ,
211+ statusText: err .response .text || ' Internal Server Error' ,
212+ headers: err .headers || {},
213+ body: err .response .text || null ,
214+ });
215+ const response: TransportResponse <ResponseType > = {
216+ status: res .status ,
217+ statusText: res .text ,
218+ headers: res .headers ,
219+ body: res .body ,
220+ };
221+ resolve (response );
222+ })
223+ );
200224}
201225
202226const service = what3words (API_KEY , config , { transport: customTransport });
203- service .availableLanguages ()
227+ service
228+ .availableLanguages ()
204229 .then (({ languages }) => console .log (' Available languages' , languages ));
205230```
206231
207232### Autosuggest
208233
209234``` typescript
210- import { ApiVersion , AutosuggestClient , AutosuggestOptions , AutosuggestResponse } from ' @what3words/api' ;
235+ import {
236+ AutosuggestClient ,
237+ AutosuggestOptions ,
238+ AutosuggestResponse ,
239+ } from ' @what3words/api' ;
211240
212- const API_KEY: string = ' <YOUR_API_KEY>' ;
241+ const API_KEY = ' <YOUR_API_KEY>' ;
213242const client: AutosuggestClient = AutosuggestClient .init (API_KEY );
214243const options: AutosuggestOptions = {
215244 input: ' filled.count.s' ,
216245};
217- client .run (options )
246+ client
247+ .run (options )
218248 .then ((res : AutosuggestResponse ) =>
219- console .log (` suggestions for "${autosuggestOptions .input }" ` , res )
249+ console .log (` suggestions for "${options .input }" ` , res )
220250 );
221251```
222252
223253### Convert to Coordinates
224254
225255``` typescript
226- import { ConvertToCoordinatesClient , ConvertToCoordinatesOptions , ConvertToCoordinatesResponse } from ' @what3words/api' ;
256+ import {
257+ ConvertToCoordinatesClient ,
258+ ConvertToCoordinatesOptions ,
259+ FeatureCollectionResponse ,
260+ LocationGeoJsonResponse ,
261+ LocationJsonResponse ,
262+ } from ' @what3words/api' ;
227263
228264const API_KEY = ' <YOUR_API_KEY>' ;
229- const client: ConvertToCoordinatesClient = ConvertToCoordinatesClient .init (API_KEY )
265+ const client: ConvertToCoordinatesClient =
266+ ConvertToCoordinatesClient .init (API_KEY );
230267const options: ConvertToCoordinatesOptions = { words: ' filled.count.soap' };
231- client .run (options )
232- .then ((res : ConvertToCoordinatesResponse ) => console .log (' Convert to coordinates' , res ));
268+
269+ // If you want to retrieve the JSON response from our API
270+ client
271+ .run ({ ... options , format: ' json' }) // { format: 'json' } is the default response
272+ .then ((res : LocationJsonResponse ) =>
273+ console .log (' Convert to coordinates' , res )
274+ );
275+
276+ // If you want to retrieve the GeoJsonResponse from our API
277+ client
278+ .run ({ ... options , format: ' geojson' })
279+ .then ((res : FeatureCollectionResponse <LocationGeoJsonResponse >) =>
280+ console .log (' Convert to coordinates' , res )
281+ );
233282```
234283
235284### Convert to Three Word Address
236285
237286``` typescript
238- import { ConvertTo3waClient , ConvertTo3waOptions , ConvertTo3waResponse } from ' @what3words/api' ;
287+ import {
288+ ConvertTo3waClient ,
289+ ConvertTo3waOptions ,
290+ FeatureCollectionResponse ,
291+ LocationGeoJsonResponse ,
292+ LocationJsonResponse ,
293+ } from ' @what3words/api' ;
239294
240295const API_KEY = ' <YOUR_API_KEY>' ;
241- const client: ConvertTo3waClient = ConvertTo3waClient .init (API_KEY )
242- const options: ConvertTo3waOptions = { coordinates: { lat: 51.520847 , lng: - 0.195521 } };
243- client .run (options )
244- .then ((res : ConvertTo3waResponse ) => console .log (' Convert to 3wa' , res ));
296+ const client: ConvertTo3waClient = ConvertTo3waClient .init (API_KEY );
297+ const options: ConvertTo3waOptions = {
298+ coordinates: { lat: 51.520847 , lng: - 0.195521 },
299+ };
300+
301+ // If you want to retrieve the JSON response from our API
302+ client
303+ .run ({ ... options , format: ' json' }) // { format: 'json' } is the default response
304+ .then ((res : LocationJsonResponse ) => console .log (' Convert to 3wa' , res ));
305+
306+ // If you want to retrieve the GeoJsonResponse from our API
307+ client
308+ .run ({ ... options , format: ' geojson' })
309+ .then ((res : FeatureCollectionResponse <LocationGeoJsonResponse >) =>
310+ console .log (' Convert to 3wa' , res )
311+ );
245312```
246313
247314### Available Languages
248315
249316``` typescript
250- import { AvailableLanguagesClient , AvailableLanguagesResponse } from ' @what3words/api' ;
317+ import {
318+ AvailableLanguagesClient ,
319+ AvailableLanguagesResponse ,
320+ } from ' @what3words/api' ;
251321
252322const API_KEY = ' <YOUR_API_KEY>' ;
253323const client: AvailableLanguagesClient = AvailableLanguagesClient .init (API_KEY );
254- client .run ()
255- .then ((res : AvailableLanguagesResponse ) => console .log (' Available Languages' , res ));
324+ client
325+ .run ()
326+ .then ((res : AvailableLanguagesResponse ) =>
327+ console .log (' Available Languages' , res )
328+ );
256329```
257330
258331### Grid Section
259332
260333``` typescript
261- import { GridSectionClient , GridSectionOptions , GridSectionResponse } from ' @what3words/api' ;
334+ import {
335+ GridSectionClient ,
336+ GridSectionOptions ,
337+ FeatureCollectionResponse ,
338+ GridSectionGeoJsonResponse ,
339+ GridSectionJsonResponse ,
340+ } from ' ../src' ;
262341
263342const API_KEY = ' <YOUR_API_KEY>' ;
264- const client: AvailableLanguagesClient = AvailableLanguagesClient .init (API_KEY );
343+ const client: GridSectionClient = GridSectionClient .init (API_KEY );
265344const options: GridSectionOptions = {
266- southwest: { lat: 52.208867 , lng: 0.117540 },
267- northeast: { lat: 52.207988 , lng: 0.116126 }
345+ boundingBox: {
346+ southwest: { lat: 52.208867 , lng: 0.11754 },
347+ northeast: { lat: 52.207988 , lng: 0.116126 },
348+ },
268349};
269- client .run (options )
270- .then ((res : GridSectionResponse ) => console .log (' Grid Section' , res ));
350+
351+ // If you want to retrieve the JSON response from our API
352+ client
353+ .run ({ ... options , format: ' json' }) // { format: 'json' } is the default response
354+ .then ((res : GridSectionJsonResponse ) => console .log (' Grid Section' , res ));
355+
356+ // If you want to retrieve the JSON response from our API
357+ client
358+ .run ({ ... options , format: ' geojson' }) // { format: 'json' } is the default response
359+ .then ((res : FeatureCollectionResponse <GridSectionGeoJsonResponse >) =>
360+ console .log (' Grid Section' , res )
361+ );
271362```
272363
273364> __ The requested box must not exceed 4km from corner to corner, or a BadBoundingBoxTooBig error will be returned. Latitudes must be >= -90 and <= 90, but longitudes are allowed to wrap around 180. To specify a bounding-box that crosses the anti-meridian, use longitude greater than 180.__
0 commit comments