|
| 1 | +import cheerio from 'cheerio' |
| 2 | +import * as fs from 'fs-extra' |
| 3 | +import nodeFetch from 'node-fetch' |
| 4 | +import tough = require('tough-cookie') |
| 5 | +import FileCookieStore from 'tough-cookie-filestore' |
| 6 | +import * as url from 'url' |
| 7 | + |
| 8 | +interface APIOptions { |
| 9 | + serverUrl: string |
| 10 | + cookiePath: string, |
| 11 | + enterprise: boolean |
| 12 | +} |
| 13 | + |
| 14 | +type nodeFetchType = (url: RequestInfo, init?: RequestInit | undefined) => Promise<Response> |
| 15 | + |
| 16 | +function encodeFormComponent(form: object) { |
| 17 | + return Object.entries(form).map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`).join('&') |
| 18 | +} |
| 19 | + |
| 20 | +export enum ExportType { |
| 21 | + PDF, |
| 22 | + SLIDE, |
| 23 | + MD, |
| 24 | + HTML |
| 25 | +} |
| 26 | + |
| 27 | +export type HistoryItem = { |
| 28 | + id: string |
| 29 | + text: string |
| 30 | + time: number | string |
| 31 | + tags: string[] |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * codimd API Client |
| 36 | + */ |
| 37 | +class API { |
| 38 | + public readonly serverUrl: string |
| 39 | + private readonly enterprise: boolean |
| 40 | + private readonly _fetch: nodeFetchType |
| 41 | + |
| 42 | + constructor(config: APIOptions) { |
| 43 | + const {serverUrl, cookiePath, enterprise} = config |
| 44 | + |
| 45 | + fs.ensureFileSync(cookiePath) |
| 46 | + |
| 47 | + const jar = new FileCookieStore(cookiePath) |
| 48 | + const fetch: nodeFetchType = require('fetch-cookie')(nodeFetch, new tough.CookieJar(jar as any)) |
| 49 | + |
| 50 | + this._fetch = fetch |
| 51 | + this.serverUrl = serverUrl |
| 52 | + this.enterprise = enterprise |
| 53 | + } |
| 54 | + |
| 55 | + async login(email: string, password: string) { |
| 56 | + const response = await this.fetch(`${this.serverUrl}/login`, { |
| 57 | + method: 'post', |
| 58 | + body: encodeFormComponent({email, password}), |
| 59 | + headers: await this.wrapHeaders({ |
| 60 | + 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + return response.status === 200 |
| 65 | + } |
| 66 | + |
| 67 | + async loginLdap(username: string, password: string) { |
| 68 | + const response = await this.fetch(`${this.serverUrl}/auth/ldap`, { |
| 69 | + method: 'post', |
| 70 | + body: encodeFormComponent({username, password}), |
| 71 | + headers: await this.wrapHeaders({ |
| 72 | + 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' |
| 73 | + }) |
| 74 | + }) |
| 75 | + return response.status === 200 |
| 76 | + } |
| 77 | + |
| 78 | + async logout() { |
| 79 | + const response = await this.fetch(`${this.serverUrl}/logout`, { |
| 80 | + method: this.enterprise ? 'POST' : 'GET', |
| 81 | + headers: await this.wrapHeaders({ |
| 82 | + 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', |
| 83 | + }) |
| 84 | + }) |
| 85 | + return response.status === 200 |
| 86 | + } |
| 87 | + |
| 88 | + async isLogin() { |
| 89 | + const response = await this.fetch(`${this.serverUrl}/me`) |
| 90 | + return response.status === 200 |
| 91 | + } |
| 92 | + |
| 93 | + async getMe() { |
| 94 | + const response = await this.fetch(`${this.serverUrl}/me`) |
| 95 | + return response.json() |
| 96 | + } |
| 97 | + |
| 98 | + async getHistory(): Promise<{ history: HistoryItem[] }> { |
| 99 | + const response = await this.fetch(`${this.serverUrl}/history`) |
| 100 | + return response.json() |
| 101 | + } |
| 102 | + |
| 103 | + async newNote(body: string) { |
| 104 | + let response |
| 105 | + if (this.enterprise) { |
| 106 | + response = await this.fetch(`${this.serverUrl}/new`, { |
| 107 | + method: 'POST', |
| 108 | + body: encodeFormComponent({content: body}), |
| 109 | + headers: await this.wrapHeaders({ |
| 110 | + 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', |
| 111 | + }) |
| 112 | + }) |
| 113 | + } else { |
| 114 | + const contentType = 'text/markdown;charset=UTF-8' |
| 115 | + response = await this.fetch(`${this.serverUrl}/new`, { |
| 116 | + method: 'POST', |
| 117 | + body, |
| 118 | + headers: { |
| 119 | + 'Content-Type': contentType |
| 120 | + } |
| 121 | + }) |
| 122 | + } |
| 123 | + |
| 124 | + if (response.status === 200) { |
| 125 | + return response.url |
| 126 | + } else { |
| 127 | + throw new Error('Create note failed') |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + async export(noteId: string, type: ExportType, output: string) { |
| 132 | + let res: Response |
| 133 | + switch (type) { |
| 134 | + case ExportType.PDF: |
| 135 | + res = await this.fetch(`${this.serverUrl}/${noteId}/pdf`) |
| 136 | + break |
| 137 | + case ExportType.HTML: |
| 138 | + res = await this.fetch(`${this.serverUrl}/s/${noteId}`) |
| 139 | + break |
| 140 | + case ExportType.SLIDE: |
| 141 | + res = await this.fetch(`${this.serverUrl}/${noteId}/slide`) |
| 142 | + break |
| 143 | + case ExportType.MD: |
| 144 | + default: |
| 145 | + res = await this.fetch(`${this.serverUrl}/${noteId}/download`) |
| 146 | + } |
| 147 | + |
| 148 | + return this.downloadFile(res, output) |
| 149 | + } |
| 150 | + |
| 151 | + private async downloadFile(res: any, output: string) { |
| 152 | + const fileStream = fs.createWriteStream(output) |
| 153 | + |
| 154 | + await new Promise((resolve, reject) => { |
| 155 | + res.body.pipe(fileStream) |
| 156 | + res.body.on('error', (err: any) => { |
| 157 | + reject(err) |
| 158 | + }) |
| 159 | + fileStream.on('finish', function () { |
| 160 | + resolve() |
| 161 | + }) |
| 162 | + }) |
| 163 | + } |
| 164 | + |
| 165 | + get fetch() { |
| 166 | + return this._fetch |
| 167 | + } |
| 168 | + |
| 169 | + get domain() { |
| 170 | + return url.parse(this.serverUrl).host |
| 171 | + } |
| 172 | + |
| 173 | + private async wrapHeaders(headers: any) { |
| 174 | + if (this.enterprise) { |
| 175 | + const csrf = await this.loadCSRFToken() |
| 176 | + return { |
| 177 | + ...headers, |
| 178 | + 'X-XSRF-Token': csrf |
| 179 | + } |
| 180 | + } else { |
| 181 | + return headers |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + private async loadCSRFToken() { |
| 186 | + const html = await this.fetch(`${this.serverUrl}`).then(r => r.text()) |
| 187 | + const $ = cheerio.load(html) |
| 188 | + |
| 189 | + return $('meta[name="csrf-token"]').attr('content') || '' |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +export default API |
0 commit comments