Skip to content

Commit 721f4fa

Browse files
authored
Merge pull request #118 from xsnippet/apiv1
Add support for v1 api
2 parents 24b8525 + 40c7141 commit 721f4fa

File tree

4 files changed

+37
-23
lines changed

4 files changed

+37
-23
lines changed

src/actions/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const fetchRecentSnippets = marker => (dispatch) => {
1515
let qs = ''
1616
if (marker) { qs = `&marker=${marker}` }
1717

18-
return fetch(misc.getApiUri(`/snippets?limit=20${qs}`))
18+
return fetch(misc.getApiUri(`snippets?limit=20${qs}`))
1919
.then((response) => {
2020
const links = parseLinkHeader(response.headers.get('Link'))
2121

@@ -31,7 +31,7 @@ export const setSnippet = snippet => ({
3131
})
3232

3333
export const fetchSnippet = id => dispatch => (
34-
fetch(misc.getApiUri(`/snippets/${id}`))
34+
fetch(misc.getApiUri(`snippets/${id}`))
3535
.then(response => response.json())
3636
.then(json => dispatch(setSnippet(json)))
3737
)
@@ -42,13 +42,13 @@ export const setSyntaxes = syntaxes => ({
4242
})
4343

4444
export const fetchSyntaxes = dispatch => (
45-
fetch(misc.getApiUri('/syntaxes'))
45+
fetch(misc.getApiUri('syntaxes'))
4646
.then(response => response.json())
4747
.then(json => dispatch(setSyntaxes(json)))
4848
)
4949

5050
export const postSnippet = (snippet, onSuccess) => dispatch => (
51-
fetch(misc.getApiUri('/snippets'), {
51+
fetch(misc.getApiUri('snippets'), {
5252
method: 'POST',
5353
headers: {
5454
'Accept': 'application/json',

src/misc/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ export function formatDate(d) {
5252
return ISOdate.split('-').reverse().join('.')
5353
}
5454

55-
export function getApiUri(endpoint) {
56-
return `${conf.API_BASE_URI}${endpoint}`
55+
export function getApiUri(endpoint, version = 'v1') {
56+
return `${conf.API_BASE_URI}/${version}/${endpoint}`
5757
}

tests/helpers/index.test.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
import * as misc from '../../src/misc'
22

3-
describe('misc: formatDate', () => {
3+
describe('misc', () => {
44
it('should return properly formated date', () => {
55
const incomeDate = '2018-03-11T15:28:19'
66

77
expect(misc.formatDate(incomeDate)).toEqual('11.03.2018')
88
})
99

10-
it('should return properly format7ed date for Jan', () => {
10+
it('should return properly formatted date for Jan', () => {
1111
const incomeDate = '2018-01-01T23:28:19'
1212

1313
expect(misc.formatDate(incomeDate)).toEqual('01.01.2018')
1414
})
15+
16+
it('should construct correct url with default api version', () => {
17+
process.env.API_BASE_URI = '//api.xsnippet.org'
18+
const f = misc.getApiUri('snippets')
19+
20+
expect(f).toBe(`${process.env.API_BASE_URI}/v1/snippets`)
21+
})
22+
23+
it('should construct correct url with passed api version', () => {
24+
process.env.API_BASE_URI = '//api.xsnippet.org'
25+
const f = misc.getApiUri('snippets', 'v404')
26+
27+
expect(f).toBe(`${process.env.API_BASE_URI}/v404/snippets`)
28+
})
1529
})

tests/store.test.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,18 @@ describe('actions', () => {
4444
first: {
4545
limit: '20',
4646
rel: 'first',
47-
url: '//api.xsnippet.org/snippets?limit=20',
47+
url: '//api.xsnippet.org/v1/snippets?limit=20',
4848
},
4949
next: {
5050
limit: '20',
5151
marker: 28,
5252
rel: 'next',
53-
url: '//api.xsnippet.org/snippets?limit=20&marker=28',
53+
url: '//api.xsnippet.org/v1/snippets?limit=20&marker=28',
5454
},
5555
prev: {
5656
limit: '20',
5757
rel: 'prev',
58-
url: '//api.xsnippet.org/snippets?limit=20',
58+
url: '//api.xsnippet.org/v1/snippets?limit=20',
5959
},
6060
}
6161
const store = createStore()
@@ -82,10 +82,10 @@ describe('actions', () => {
8282
syntax: 'Python',
8383
},
8484
]
85-
const links = '<//api.xsnippet.org/snippets?limit=20>; rel="first", <//api.xsnippet.org/snippets?limit=20&marker=19>; rel="next", <//api.xsnippet.org/snippets?limit=20&marker=59>; rel="prev"'
85+
const links = '<//api.xsnippet.org/v1/snippets?limit=20>; rel="first", <//api.xsnippet.org/v1/snippets?limit=20&marker=19>; rel="next", <//api.xsnippet.org/v1/snippets?limit=20&marker=59>; rel="prev"'
8686

8787
fetchMock.getOnce(
88-
'//api.xsnippet.org/snippets?limit=20&marker=39',
88+
'//api.xsnippet.org/v1/snippets?limit=20&marker=39',
8989
{
9090
headers: { Link: links },
9191
body: snippets,
@@ -114,19 +114,19 @@ describe('actions', () => {
114114
first: {
115115
limit: '20',
116116
rel: 'first',
117-
url: '//api.xsnippet.org/snippets?limit=20',
117+
url: '//api.xsnippet.org/v1/snippets?limit=20',
118118
},
119119
next: {
120120
limit: '20',
121121
marker: '19',
122122
rel: 'next',
123-
url: '//api.xsnippet.org/snippets?limit=20&marker=19',
123+
url: '//api.xsnippet.org/v1/snippets?limit=20&marker=19',
124124
},
125125
prev: {
126126
limit: '20',
127127
marker: '59',
128128
rel: 'prev',
129-
url: '//api.xsnippet.org/snippets?limit=20&marker=59',
129+
url: '//api.xsnippet.org/v1/snippets?limit=20&marker=59',
130130
},
131131
},
132132
})
@@ -145,10 +145,10 @@ describe('actions', () => {
145145
syntax: 'Python',
146146
},
147147
]
148-
const links = '<//api.xsnippet.org/snippets?limit=20>; rel="first", <//api.xsnippet.org/snippets?limit=20&marker=39>; rel="next"'
148+
const links = '<//api.xsnippet.org/v1/snippets?limit=20>; rel="first", <//api.xsnippet.org/v1/snippets?limit=20&marker=39>; rel="next"'
149149

150150
fetchMock.getOnce(
151-
'//api.xsnippet.org/snippets?limit=20',
151+
'//api.xsnippet.org/v1/snippets?limit=20',
152152
{
153153
headers: { Link: links },
154154
body: snippets,
@@ -177,13 +177,13 @@ describe('actions', () => {
177177
first: {
178178
limit: '20',
179179
rel: 'first',
180-
url: '//api.xsnippet.org/snippets?limit=20',
180+
url: '//api.xsnippet.org/v1/snippets?limit=20',
181181
},
182182
next: {
183183
limit: '20',
184184
marker: '39',
185185
rel: 'next',
186-
url: '//api.xsnippet.org/snippets?limit=20&marker=39',
186+
url: '//api.xsnippet.org/v1/snippets?limit=20&marker=39',
187187
},
188188
},
189189
})
@@ -219,7 +219,7 @@ describe('actions', () => {
219219
syntax: 'Go',
220220
}
221221

222-
fetchMock.getOnce(`//api.xsnippet.org/snippets/${snippet.id}`, JSON.stringify(snippet))
222+
fetchMock.getOnce(`//api.xsnippet.org/v1/snippets/${snippet.id}`, JSON.stringify(snippet))
223223

224224
const store = createStore()
225225
await store.dispatch(actions.fetchSnippet(snippet.id))
@@ -254,7 +254,7 @@ describe('actions', () => {
254254
it('should create an action to fetch syntaxes', async () => {
255255
const syntaxes = ['JavaScript', 'Python', 'Java', 'Go', 'Plain Text']
256256

257-
fetchMock.getOnce('//api.xsnippet.org/syntaxes', JSON.stringify(syntaxes))
257+
fetchMock.getOnce('//api.xsnippet.org/v1/syntaxes', JSON.stringify(syntaxes))
258258

259259
const store = createStore()
260260
await store.dispatch(actions.fetchSyntaxes)
@@ -274,7 +274,7 @@ describe('actions', () => {
274274
syntax: 'JavaScript',
275275
}
276276

277-
fetchMock.postOnce('//api.xsnippet.org/snippets', JSON.stringify(snippet))
277+
fetchMock.postOnce('//api.xsnippet.org/v1/snippets', JSON.stringify(snippet))
278278

279279
const store = createStore()
280280
await store.dispatch(actions.postSnippet(snippet, () => {}))

0 commit comments

Comments
 (0)