Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,7 @@ $RECYCLE.BIN/
# Windows shortcuts
*.lnk

# End of https://www.toptal.com/developers/gitignore/api/node,macos,windows,visualstudiocode
# End of https://www.toptal.com/developers/gitignore/api/node,macos,windows,visualstudiocode

# Snyk cacke
.dccache
26 changes: 24 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": false,
"name": "@what3words/api",
"version": "4.0.5",
"version": "4.0.6",
"description": "what3words JavaScript API",
"homepage": "https://github.com/what3words/w3w-node-wrapper#readme",
"main": "dist/index.js",
Expand All @@ -27,6 +27,14 @@
"axios": "^0.21.2",
"cross-fetch": "^3.1.5"
},
"peerDependenciesMeta": {
"axios": {
"optional": true
},
"cross-fetch": {
"optional": true
}
},
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "^1.0.1",
"@testing-library/react": "^12.1.3",
Expand Down
7 changes: 4 additions & 3 deletions src/lib/transport/axios.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import axios from 'axios';
import { AxiosResponse } from 'axios';
import type { Transport, TransportResponse } from './model';
import { ClientRequest } from '../client';
import { errorHandler } from './error';

export function axiosTransport(): Transport {
const axios = require('axios');
return async function axiosTransport<T>(
req: ClientRequest
): Promise<TransportResponse<T>> {
Expand All @@ -16,7 +17,7 @@ export function axiosTransport(): Transport {
params,
};
return await axios(options)
.then(res => {
.then((res: AxiosResponse) => {
const response = errorHandler({
status: res.status,
statusText: res.statusText,
Expand All @@ -25,7 +26,7 @@ export function axiosTransport(): Transport {
});
return response;
})
.catch(err => {
.catch((err: any) => {
if (err.isAxiosError)
errorHandler<T>({
status: err.response?.status || err.status || 500,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/transport/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import fetch from 'cross-fetch';
import type { Transport, TransportResponse } from './model';
import { ClientRequest } from '../client';
import { errorHandler } from './error';
import { searchParams } from '../serializer';

export function fetchTransport(): Transport {
const fetch = require('cross-fetch');
return async function fetchTransport<T>(
req: ClientRequest
): Promise<TransportResponse<T>> {
Expand Down
1 change: 0 additions & 1 deletion src/lib/transport/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ export * from './axios';
export * from './error';
export * from './fetch';
export * from './model';
export * from './parse';
13 changes: 0 additions & 13 deletions src/lib/transport/parse.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type {
LocationGeoJsonResponse,
LocationJsonResponse,
} from './client';
import { ApiClientConfiguration, getTransport } from './lib';
import { ApiClientConfiguration } from './lib';
import type { Transport } from './lib';

export type What3wordsService = {
Expand Down Expand Up @@ -48,9 +48,9 @@ export type What3wordsService = {
export function what3words(
apiKey?: string,
config?: ApiClientConfiguration,
opts?: { transport: 'fetch' | 'axios' | Transport }
opts?: { transport: Transport }
): What3wordsService {
const transport = getTransport(opts?.transport);
const transport = opts?.transport || require('./lib').fetchTransport();
const autosuggestClient = new AutosuggestClient(apiKey, config, transport);
const availableLanguagesClient = new AvailableLanguagesClient(
apiKey,
Expand Down
3 changes: 2 additions & 1 deletion test/service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import what3words, {
ApiClientConfiguration,
ApiVersion,
searchParams,
axiosTransport,
} from '../src';
import { What3wordsService } from '../src/service';

Expand Down Expand Up @@ -120,7 +121,7 @@ describe('what3words', () => {
describe('Axios Transport', () => {
let input: string;
beforeEach(() => {
service = what3words(apiKey, config, { transport: 'axios' });
service = what3words(apiKey, config, { transport: axiosTransport() });
input = CHANCE.string();
nock(`${config.host!}/${config.apiVersion}`)
.get(`/autosuggest?${searchParams({ input, key: apiKey })}`)
Expand Down