focalboard/webapp/src/octoClient.ts

211 lines
7 KiB
TypeScript
Raw Normal View History

2020-10-20 21:50:53 +02:00
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
2020-10-22 18:46:06 +02:00
import {IBlock, IMutableBlock} from './blocks/block'
2020-12-07 20:40:16 +01:00
import {IUser} from './user'
2020-10-20 21:52:56 +02:00
import {Utils} from './utils'
2020-10-08 18:21:27 +02:00
//
// OctoClient is the client interface to the server APIs
//
class OctoClient {
2020-10-20 21:50:53 +02:00
serverUrl: string
2020-12-04 16:03:09 +01:00
token?: string
2020-10-20 21:50:53 +02:00
2020-12-04 16:03:09 +01:00
constructor(serverUrl?: string, token?: string) {
this.serverUrl = serverUrl || window.location.origin
2020-12-04 16:03:09 +01:00
this.token = token
2020-10-22 18:46:06 +02:00
Utils.log(`OctoClient serverUrl: ${this.serverUrl}`)
2020-10-20 21:50:53 +02:00
}
2020-12-04 16:03:09 +01:00
async login(username: string, password: string): Promise<boolean> {
const path = '/api/v1/login'
const body = JSON.stringify({username, password, type: 'normal'})
const response = await fetch(this.serverUrl + path, {
method: 'POST',
headers: this.headers(),
body,
})
if (response.status === 200) {
const responseJson = (await response.json() || {}) as {token?: string}
this.token = responseJson.token
if (responseJson.token !== '') {
localStorage.setItem('sessionId', this.token || '')
return true
}
return false
}
return false
}
async register(email: string, username: string, password: string): Promise<boolean> {
const path = '/api/v1/register'
const body = JSON.stringify({email, username, password})
const response = await fetch(this.serverUrl + path, {
method: 'POST',
headers: this.headers(),
body,
})
if (response.status === 200) {
return true
}
return false
}
headers() {
return {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: this.token ? 'Bearer ' + this.token : '',
}
}
2020-12-07 20:40:16 +01:00
async getMe(): Promise<IUser|null> {
const path = '/api/v1/users/me'
const response = await fetch(this.serverUrl + path, {headers: this.headers()})
const user = (await response.json()) as IUser || null
return user
}
2020-11-12 19:16:59 +01:00
async getSubtree(rootId?: string, levels = 2): Promise<IBlock[]> {
const path = `/api/v1/blocks/${rootId}/subtree?l=${levels}`
2020-12-04 16:03:09 +01:00
const response = await fetch(this.serverUrl + path, {headers: this.headers()})
const blocks = (await response.json() || []) as IMutableBlock[]
this.fixBlocks(blocks)
return blocks
2020-10-20 21:50:53 +02:00
}
async exportFullArchive(): Promise<IBlock[]> {
2020-10-20 21:52:56 +02:00
const path = '/api/v1/blocks/export'
2020-12-04 16:03:09 +01:00
const response = await fetch(this.serverUrl + path, {headers: this.headers()})
const blocks = (await response.json() || []) as IMutableBlock[]
this.fixBlocks(blocks)
return blocks
2020-10-20 21:50:53 +02:00
}
async importFullArchive(blocks: readonly IBlock[]): Promise<Response> {
2020-10-20 21:50:53 +02:00
Utils.log(`importFullArchive: ${blocks.length} blocks(s)`)
blocks.forEach((block) => {
Utils.log(`\t ${block.type}, ${block.id}`)
})
2020-10-20 21:50:53 +02:00
const body = JSON.stringify(blocks)
2020-11-12 23:55:55 +01:00
return fetch(this.serverUrl + '/api/v1/blocks/import', {
method: 'POST',
2020-12-04 16:03:09 +01:00
headers: this.headers(),
2020-10-20 21:50:53 +02:00
body,
})
2020-10-20 21:50:53 +02:00
}
2020-10-21 22:20:00 +02:00
async getBlocksWithParent(parentId: string, type?: string): Promise<IBlock[]> {
2020-10-20 21:50:53 +02:00
let path: string
2020-10-21 22:20:00 +02:00
if (type) {
path = `/api/v1/blocks?parent_id=${encodeURIComponent(parentId)}&type=${encodeURIComponent(type)}`
2020-10-20 21:50:53 +02:00
} else {
path = `/api/v1/blocks?parent_id=${encodeURIComponent(parentId)}`
2020-10-20 21:50:53 +02:00
}
2020-10-21 22:20:00 +02:00
return this.getBlocksWithPath(path)
}
async getBlocksWithType(type: string): Promise<IBlock[]> {
const path = `/api/v1/blocks?type=${encodeURIComponent(type)}`
return this.getBlocksWithPath(path)
}
2020-10-20 21:50:53 +02:00
2020-10-21 22:20:00 +02:00
private async getBlocksWithPath(path: string): Promise<IBlock[]> {
2020-12-04 16:03:09 +01:00
const response = await fetch(this.serverUrl + path, {headers: this.headers()})
2020-10-21 03:28:55 +02:00
const blocks = (await response.json() || []) as IMutableBlock[]
2020-10-20 21:50:53 +02:00
this.fixBlocks(blocks)
return blocks
2020-10-20 21:50:53 +02:00
}
2020-11-12 23:55:55 +01:00
// TODO: Remove this fixup code
2020-10-21 03:28:55 +02:00
fixBlocks(blocks: IMutableBlock[]): void {
if (!blocks) {
2020-10-20 21:50:53 +02:00
return
}
for (const block of blocks) {
if (!block.fields) {
2020-10-20 21:50:53 +02:00
block.fields = {}
}
}
}
2020-10-21 03:28:55 +02:00
async updateBlock(block: IMutableBlock): Promise<Response> {
2020-10-20 21:50:53 +02:00
block.updateAt = Date.now()
2020-11-12 23:55:55 +01:00
return this.insertBlocks([block])
2020-10-20 21:50:53 +02:00
}
2020-10-21 03:28:55 +02:00
async updateBlocks(blocks: IMutableBlock[]): Promise<Response> {
2020-10-20 21:50:53 +02:00
const now = Date.now()
blocks.forEach((block) => {
block.updateAt = now
2020-10-20 21:52:56 +02:00
})
2020-11-12 23:55:55 +01:00
return this.insertBlocks(blocks)
2020-10-20 21:50:53 +02:00
}
async deleteBlock(blockId: string): Promise<Response> {
2020-10-22 18:46:06 +02:00
Utils.log(`deleteBlock: ${blockId}`)
2020-11-12 23:55:55 +01:00
return fetch(this.serverUrl + `/api/v1/blocks/${encodeURIComponent(blockId)}`, {
2020-10-20 21:50:53 +02:00
method: 'DELETE',
2020-12-04 16:03:09 +01:00
headers: this.headers(),
2020-10-20 21:50:53 +02:00
})
}
async insertBlock(block: IBlock): Promise<Response> {
return this.insertBlocks([block])
2020-10-20 21:50:53 +02:00
}
async insertBlocks(blocks: IBlock[]): Promise<Response> {
Utils.log(`insertBlocks: ${blocks.length} blocks(s)`)
blocks.forEach((block) => {
2020-11-12 19:16:59 +01:00
Utils.log(`\t ${block.type}, ${block.id}, ${block.title?.substr(0, 50) || ''}`)
})
const body = JSON.stringify(blocks)
2020-11-12 23:55:55 +01:00
return fetch(this.serverUrl + '/api/v1/blocks', {
2020-10-20 21:50:53 +02:00
method: 'POST',
2020-12-04 16:03:09 +01:00
headers: this.headers(),
2020-10-20 21:50:53 +02:00
body,
})
}
// Returns URL of uploaded file, or undefined on failure
async uploadFile(file: File): Promise<string | undefined> {
// IMPORTANT: We need to post the image as a form. The browser will convert this to a application/x-www-form-urlencoded POST
const formData = new FormData()
formData.append('file', file)
2020-10-20 21:50:53 +02:00
try {
2020-10-20 21:50:53 +02:00
const response = await fetch(this.serverUrl + '/api/v1/files', {
method: 'POST',
// TIPTIP: Leave out Content-Type here, it will be automatically set by the browser
headers: {
Accept: 'application/json',
2020-12-04 16:03:09 +01:00
Authorization: this.token ? 'Bearer ' + this.token : '',
2020-10-20 21:50:53 +02:00
},
body: formData,
})
2020-10-20 21:50:53 +02:00
if (response.status === 200) {
try {
const text = await response.text()
2020-10-20 21:50:53 +02:00
Utils.log(`uploadFile response: ${text}`)
const json = JSON.parse(text)
2020-10-20 21:50:53 +02:00
// const json = await response.json()
return json.url
} catch (e) {
Utils.logError(`uploadFile json ERROR: ${e}`)
2020-10-20 21:50:53 +02:00
}
}
} catch (e) {
Utils.logError(`uploadFile ERROR: ${e}`)
2020-10-20 21:50:53 +02:00
}
return undefined
}
2020-10-08 18:21:27 +02:00
}
2020-12-04 16:03:09 +01:00
const client = new OctoClient(undefined, localStorage.getItem('sessionId') || '')
export default client