Skip to content
This repository was archived by the owner on Mar 2, 2024. It is now read-only.

Commit 691f2e6

Browse files
committed
fix this.request is not a function
closes #2
1 parent 740655d commit 691f2e6

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/index.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export default function useAPI(endpoint = '/api.php') {
2-
return {
2+
const api = {
33
endpoint,
44
setEndpoint(endpoint) {
5-
this.endpoint = endpoint
5+
api.endpoint = endpoint
66
},
77
request(path, options = {}) {
88
path = (path.substr(0, 1) !== '/' ? '/' : '') + path
@@ -12,7 +12,7 @@ export default function useAPI(endpoint = '/api.php') {
1212
options.credentials = 'include'
1313
return new Promise(async (resolve, reject) => {
1414
try {
15-
const resp = await fetch(this.endpoint + path, options) 
15+
const resp = await fetch(api.endpoint + path, options) 
1616
if (resp.status === 200 || resp.ok) {
1717
const json = await resp.json()
1818
resolve(json.records || json)
@@ -25,34 +25,35 @@ export default function useAPI(endpoint = '/api.php') {
2525
})
2626
},
2727
create(table, item) {
28-
return this.request(`records/${table}`, { method: 'post', body: item })
28+
return api.request(`records/${table}`, { method: 'post', body: item })
2929
},
3030
read(table, key) {
31-
return this.request(`records/${table}/${key}`)
31+
return api.request(`records/${table}/${key}`)
3232
},
3333
update(table, key, item) {
34-
return this.request(`records/${table}/${key}`, { method: 'put', body: item })
34+
return api.request(`records/${table}/${key}`, { method: 'put', body: item })
3535
},
3636
delete(table, key) {
37-
return this.request(`records/${table}/${key}`, { method: 'delete' })
37+
return api.request(`records/${table}/${key}`, { method: 'delete' })
3838
},
3939
list(path) {
40-
return this.request('records/' + path)
40+
return api.request('records/' + path)
4141
},
4242
me() {
43-
return this.request('me')
43+
return api.request('me')
4444
},
4545
register(username, password) {
46-
return this.request('register', { method: 'post', body: { username, password } })
46+
return api.request('register', { method: 'post', body: { username, password } })
4747
},
4848
login(username, password) {
49-
return this.request('login', { method: 'post', body: { username, password } })
49+
return api.request('login', { method: 'post', body: { username, password } })
5050
},
5151
password(username, password, newPassword) {
52-
return this.request('password', { method: 'post', body: { username, password, newPassword } })
52+
return api.request('password', { method: 'post', body: { username, password, newPassword } })
5353
},
5454
logout() {
55-
return this.request('logout', { method: 'post' })
55+
return api.request('logout', { method: 'post' })
5656
}
5757
}
58+
return api
5859
}

0 commit comments

Comments
 (0)