Skip to content

Commit c7e786d

Browse files
c5hawdowneyfe
andauthored
[TT-7263] Make peer dependencies 'cross-fetch' and 'axios' optional (#47)
* Attempt to use optional deps by providing transport instead of transport name * [fix] Make peer dependencies optional * [fix] further modular require improvements * [mod] bump to prod version Co-authored-by: Frances Downey <frances@what3words.com>
1 parent 97cfda9 commit c7e786d

File tree

9 files changed

+47
-26
lines changed

9 files changed

+47
-26
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,7 @@ $RECYCLE.BIN/
206206
# Windows shortcuts
207207
*.lnk
208208

209-
# End of https://www.toptal.com/developers/gitignore/api/node,macos,windows,visualstudiocode
209+
# End of https://www.toptal.com/developers/gitignore/api/node,macos,windows,visualstudiocode
210+
211+
# Snyk cacke
212+
.dccache

package-lock.json

Lines changed: 24 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": false,
33
"name": "@what3words/api",
4-
"version": "4.0.5",
4+
"version": "4.0.6",
55
"description": "what3words JavaScript API",
66
"homepage": "https://github.com/what3words/w3w-node-wrapper#readme",
77
"main": "dist/index.js",
@@ -27,6 +27,14 @@
2727
"axios": "^0.21.2",
2828
"cross-fetch": "^3.1.5"
2929
},
30+
"peerDependenciesMeta": {
31+
"axios": {
32+
"optional": true
33+
},
34+
"cross-fetch": {
35+
"optional": true
36+
}
37+
},
3038
"devDependencies": {
3139
"@istanbuljs/nyc-config-typescript": "^1.0.1",
3240
"@testing-library/react": "^12.1.3",

src/lib/transport/axios.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import axios from 'axios';
1+
import { AxiosResponse } from 'axios';
22
import type { Transport, TransportResponse } from './model';
33
import { ClientRequest } from '../client';
44
import { errorHandler } from './error';
55

66
export function axiosTransport(): Transport {
7+
const axios = require('axios');
78
return async function axiosTransport<T>(
89
req: ClientRequest
910
): Promise<TransportResponse<T>> {
@@ -16,7 +17,7 @@ export function axiosTransport(): Transport {
1617
params,
1718
};
1819
return await axios(options)
19-
.then(res => {
20+
.then((res: AxiosResponse) => {
2021
const response = errorHandler({
2122
status: res.status,
2223
statusText: res.statusText,
@@ -25,7 +26,7 @@ export function axiosTransport(): Transport {
2526
});
2627
return response;
2728
})
28-
.catch(err => {
29+
.catch((err: any) => {
2930
if (err.isAxiosError)
3031
errorHandler<T>({
3132
status: err.response?.status || err.status || 500,

src/lib/transport/fetch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import fetch from 'cross-fetch';
21
import type { Transport, TransportResponse } from './model';
32
import { ClientRequest } from '../client';
43
import { errorHandler } from './error';
54
import { searchParams } from '../serializer';
65

76
export function fetchTransport(): Transport {
7+
const fetch = require('cross-fetch');
88
return async function fetchTransport<T>(
99
req: ClientRequest
1010
): Promise<TransportResponse<T>> {

src/lib/transport/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ export * from './axios';
22
export * from './error';
33
export * from './fetch';
44
export * from './model';
5-
export * from './parse';

src/lib/transport/parse.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import type {
1818
LocationGeoJsonResponse,
1919
LocationJsonResponse,
2020
} from './client';
21-
import { ApiClientConfiguration, getTransport } from './lib';
21+
import { ApiClientConfiguration } from './lib';
2222
import type { Transport } from './lib';
2323

2424
export type What3wordsService = {
@@ -48,9 +48,9 @@ export type What3wordsService = {
4848
export function what3words(
4949
apiKey?: string,
5050
config?: ApiClientConfiguration,
51-
opts?: { transport: 'fetch' | 'axios' | Transport }
51+
opts?: { transport: Transport }
5252
): What3wordsService {
53-
const transport = getTransport(opts?.transport);
53+
const transport = opts?.transport || require('./lib').fetchTransport();
5454
const autosuggestClient = new AutosuggestClient(apiKey, config, transport);
5555
const availableLanguagesClient = new AvailableLanguagesClient(
5656
apiKey,

test/service.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import what3words, {
55
ApiClientConfiguration,
66
ApiVersion,
77
searchParams,
8+
axiosTransport,
89
} from '../src';
910
import { What3wordsService } from '../src/service';
1011

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

0 commit comments

Comments
 (0)