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