1+ import cheerio from 'cheerio'
12import * as fs from 'fs-extra'
23import nodeFetch from 'node-fetch'
34import tough = require( 'tough-cookie' )
@@ -8,7 +9,8 @@ import config from './config'
89
910interface APIOptions {
1011 serverUrl : string
11- cookiePath : string
12+ cookiePath : string ,
13+ enterprise : boolean
1214}
1315
1416type nodeFetchType = ( url : RequestInfo , init ?: RequestInit | undefined ) => Promise < Response >
@@ -36,10 +38,11 @@ export type HistoryItem = {
3638 */
3739class API {
3840 public readonly serverUrl : string
41+ private readonly enterprise : boolean
3942 private readonly _fetch : nodeFetchType
4043
41- constructor ( ) {
42- const { serverUrl, cookiePath} : APIOptions = config
44+ constructor ( config : APIOptions ) {
45+ const { serverUrl, cookiePath, enterprise } = config
4346
4447 fs . ensureFileSync ( cookiePath )
4548
@@ -48,32 +51,39 @@ class API {
4851
4952 this . _fetch = fetch
5053 this . serverUrl = serverUrl
54+ this . enterprise = enterprise
5155 }
5256
5357 async login ( email : string , password : string ) {
5458 const response = await this . fetch ( `${ this . serverUrl } /login` , {
5559 method : 'post' ,
5660 body : encodeFormComponent ( { email, password} ) ,
57- headers : {
61+ headers : await this . wrapHeaders ( {
5862 'Content-Type' : 'application/x-www-form-urlencoded;charset=UTF-8'
59- }
63+ } )
6064 } )
65+
6166 return response . status === 200
6267 }
6368
6469 async loginLdap ( username : string , password : string ) {
6570 const response = await this . fetch ( `${ this . serverUrl } /auth/ldap` , {
6671 method : 'post' ,
6772 body : encodeFormComponent ( { username, password} ) ,
68- headers : {
73+ headers : await this . wrapHeaders ( {
6974 'Content-Type' : 'application/x-www-form-urlencoded;charset=UTF-8'
70- }
75+ } )
7176 } )
7277 return response . status === 200
7378 }
7479
7580 async logout ( ) {
76- const response = await this . fetch ( `${ this . serverUrl } /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+ } )
7787 return response . status === 200
7888 }
7989
@@ -93,14 +103,26 @@ class API {
93103 }
94104
95105 async newNote ( body : string ) {
96- const contentType = 'text/markdown;charset=UTF-8'
97- const response = await this . fetch ( `${ this . serverUrl } /new` , {
98- method : 'POST' ,
99- body,
100- headers : {
101- 'Content-Type' : contentType
102- }
103- } )
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+
104126 if ( response . status === 200 ) {
105127 return response . url
106128 } else {
@@ -149,8 +171,27 @@ class API {
149171 get domain ( ) {
150172 return url . parse ( this . serverUrl ) . host
151173 }
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+ }
152193}
153194
154195export default API
155196
156- export const APIClient = new API ( )
197+ export const APIClient = new API ( config )
0 commit comments