Skip to content

Commit e1c79dd

Browse files
committed
fix: allow for absolute api namespaces
prefix with / only when needed
1 parent 3595dae commit e1c79dd

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/callAPIMethod.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ const defaults = {
1414
}
1515
};
1616

17+
export function getAPIPrefix(APINamespace) {
18+
const reAbsolute = /^(\/|https?:\/\/)/ig;
19+
return reAbsolute.test(APINamespace) ? APINamespace : '/' + APINamespace;
20+
}
21+
1722
function getFullPath(APINamespace, namespace, path=[]) {
1823
path = [].concat(path);
19-
return uri.join('/', APINamespace, namespace, ...path);
24+
return uri.join(getAPIPrefix(APINamespace), namespace, ...path);
2025
}
2126

2227
function callAPI(APINamespace, fetchOptions, namespace = '', { path=[], query={}, options={}, method='json' }) {

test/callAPIMethod.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import test from 'ava';
2+
import { getAPIPrefix } from '../src/callAPIMethod';
3+
4+
test('getAPIPrefix', t => {
5+
t.is(getAPIPrefix('api'), '/api', 'relative path');
6+
t.is(getAPIPrefix('/api'), '/api', 'absolute path');
7+
t.is(getAPIPrefix('http://api.example.com'), 'http://api.example.com', 'http url');
8+
t.is(getAPIPrefix('https://api.example.com'), 'https://api.example.com', 'http url');
9+
t.is(getAPIPrefix('//api.example.com'), '//api.example.com', 'protocol-less url');
10+
});

test/createAPI.spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import test from 'ava';
22
import createAPI from '../src/createAPI';
33

4-
test.failing('no tests', t => {
5-
t.fail()
4+
const api = createAPI();
5+
6+
test('coverage', t => {
7+
t.pass()
68
});

0 commit comments

Comments
 (0)