mirror of
https://github.com/BoostIo/Boostnote
synced 2026-01-04 20:49:19 +00:00
article CRUD with socket
This commit is contained in:
54
lib/api.js
54
lib/api.js
@@ -1,107 +1,111 @@
|
||||
var request = require('superagent-promise')(require('superagent'), Promise)
|
||||
var apiUrl = require('../config').apiUrl
|
||||
import superagent from 'superagent'
|
||||
import superagentPromise from 'superagent-promise'
|
||||
import { API_URL } from '../config'
|
||||
import auth from 'boost/auth'
|
||||
|
||||
const request = superagentPromise(superagent, Promise)
|
||||
|
||||
export function login (input) {
|
||||
return request
|
||||
.post(apiUrl + 'auth/login')
|
||||
.post(API_URL + 'auth/login')
|
||||
.send(input)
|
||||
}
|
||||
|
||||
export function signup (input) {
|
||||
return request
|
||||
.post(apiUrl + 'auth/register')
|
||||
.post(API_URL + 'auth/register')
|
||||
.send(input)
|
||||
}
|
||||
|
||||
export function fetchCurrentUser () {
|
||||
return request
|
||||
.get(apiUrl + 'auth/user')
|
||||
.get(API_URL + 'auth/user')
|
||||
.set({
|
||||
Authorization: 'Bearer ' + localStorage.getItem('token')
|
||||
Authorization: 'Bearer ' + auth.token()
|
||||
})
|
||||
}
|
||||
|
||||
export function fetchArticles (userId) {
|
||||
return request
|
||||
.get(apiUrl + 'teams/' + userId + '/articles')
|
||||
.get(API_URL + 'teams/' + userId + '/articles')
|
||||
.set({
|
||||
Authorization: 'Bearer ' + localStorage.getItem('token')
|
||||
Authorization: 'Bearer ' + auth.token()
|
||||
})
|
||||
}
|
||||
|
||||
export function createArticle (input) {
|
||||
return request
|
||||
.post(apiUrl + 'articles/')
|
||||
.post(API_URL + 'articles/')
|
||||
.set({
|
||||
Authorization: 'Bearer ' + localStorage.getItem('token')
|
||||
Authorization: 'Bearer ' + auth.token()
|
||||
})
|
||||
.send(input)
|
||||
}
|
||||
|
||||
export function saveArticle (input) {
|
||||
return request
|
||||
.put(apiUrl + 'articles/' + input.id)
|
||||
.put(API_URL + 'articles/' + input.id)
|
||||
.set({
|
||||
Authorization: 'Bearer ' + localStorage.getItem('token')
|
||||
Authorization: 'Bearer ' + auth.token()
|
||||
})
|
||||
.send(input)
|
||||
}
|
||||
|
||||
export function destroyArticle (articleId) {
|
||||
return request
|
||||
.del(apiUrl + 'articles/' + articleId)
|
||||
.del(API_URL + 'articles/' + articleId)
|
||||
.set({
|
||||
Authorization: 'Bearer ' + localStorage.getItem('token')
|
||||
Authorization: 'Bearer ' + auth.token()
|
||||
})
|
||||
}
|
||||
|
||||
export function createTeam (input) {
|
||||
return request
|
||||
.post(apiUrl + 'teams')
|
||||
.post(API_URL + 'teams')
|
||||
.set({
|
||||
Authorization: 'Bearer ' + localStorage.getItem('token')
|
||||
Authorization: 'Bearer ' + auth.token()
|
||||
})
|
||||
.send(input)
|
||||
}
|
||||
|
||||
export function searchUser (key) {
|
||||
return request
|
||||
.get(apiUrl + 'search/users')
|
||||
.get(API_URL + 'search/users')
|
||||
.query({key: key})
|
||||
}
|
||||
|
||||
export function setMember (teamId, input) {
|
||||
return request
|
||||
.post(apiUrl + 'teams/' + teamId + '/members')
|
||||
.post(API_URL + 'teams/' + teamId + '/members')
|
||||
.set({
|
||||
Authorization: 'Bearer ' + localStorage.getItem('token')
|
||||
Authorization: 'Bearer ' + auth.token()
|
||||
})
|
||||
.send(input)
|
||||
}
|
||||
|
||||
export function deleteMember (teamId, input) {
|
||||
return request
|
||||
.del(apiUrl + 'teams/' + teamId + '/members')
|
||||
.del(API_URL + 'teams/' + teamId + '/members')
|
||||
.set({
|
||||
Authorization: 'Bearer ' + localStorage.getItem('token')
|
||||
Authorization: 'Bearer ' + auth.token()
|
||||
})
|
||||
.send(input)
|
||||
}
|
||||
|
||||
export function createFolder (input) {
|
||||
return request
|
||||
.post(apiUrl + 'folders/')
|
||||
.post(API_URL + 'folders/')
|
||||
.set({
|
||||
Authorization: 'Bearer ' + localStorage.getItem('token')
|
||||
Authorization: 'Bearer ' + auth.token()
|
||||
})
|
||||
.send(input)
|
||||
}
|
||||
|
||||
export function sendEmail (input) {
|
||||
return request
|
||||
.post(apiUrl + 'mail')
|
||||
.post(API_URL + 'mail')
|
||||
.set({
|
||||
Authorization: 'Bearer ' + localStorage.getItem('token')
|
||||
Authorization: 'Bearer ' + auth.token()
|
||||
})
|
||||
.send(input)
|
||||
}
|
||||
|
||||
34
lib/auth.js
Normal file
34
lib/auth.js
Normal file
@@ -0,0 +1,34 @@
|
||||
// initial value
|
||||
var currentUser = JSON.parse(localStorage.getItem('currentUser'))
|
||||
var currentToken = localStorage.getItem('token')
|
||||
|
||||
function user (user, newToken) {
|
||||
if (user != null) {
|
||||
localStorage.setItem('currentUser', JSON.stringify(user))
|
||||
currentUser = user
|
||||
}
|
||||
|
||||
if (newToken != null) {
|
||||
localStorage.setItem('token', newToken)
|
||||
currentToken = newToken
|
||||
}
|
||||
|
||||
return currentUser
|
||||
}
|
||||
|
||||
function token () {
|
||||
return currentToken
|
||||
}
|
||||
|
||||
function clear () {
|
||||
localStorage.removeItem('currentUser')
|
||||
localStorage.removeItem('token')
|
||||
currentUser = null
|
||||
currentToken = null
|
||||
}
|
||||
|
||||
export default {
|
||||
user,
|
||||
token,
|
||||
clear
|
||||
}
|
||||
Reference in New Issue
Block a user