Skip to content

Commit 94e3933

Browse files
James TsaiJames Tsai
authored andcommitted
feat: update v1-api client config
1 parent 32d040f commit 94e3933

2 files changed

Lines changed: 7 additions & 74 deletions

File tree

src/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import API from '@hackmd/api'
22

3-
// import config from './config'
3+
import config from './config'
44

5-
export const APIClient = new API("3KY8B7FSEQ7LRRH62E0C0E394K9YNAF8L0JQUY8MUH1I2BDFNF", "http://localhost:3000/api/openAPI/v1")
5+
export const APIClient = new API(config.accesstoken as string, config.hackmdAPIEndpointURL)

src/config.ts

Lines changed: 5 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,21 @@ import {homedir} from 'os'
44
import * as path from 'path'
55

66
let configDir
7-
if (process.env.HMD_CLI_CONFIG_DIR || process.env.CMD_CLI_CONFIG_DIR) {
8-
configDir = process.env.HMD_CLI_CONFIG_DIR || process.env.CMD_CLI_CONFIG_DIR || ''
7+
if (process.env.HMD_CLI_CONFIG_DIR) {
8+
configDir = process.env.HMD_CLI_CONFIG_DIR || ''
99
} else {
1010
configDir = path.join(homedir(), '.hackmd')
1111
}
1212

1313
const configFilePath = path.join(configDir, 'config.json')
14-
const defaultCookiePath = path.join(homedir(), '.hackmd', 'cookies.json')
1514

1615
const defaultConfig = {
17-
cookiePath: defaultCookiePath,
18-
serverUrl: 'https://hackmd.io',
19-
enterprise: true
20-
}
21-
22-
function toBooleanConfig(configValue?: string | boolean) {
23-
if (configValue && typeof configValue === 'string') {
24-
return (configValue === 'true')
25-
}
26-
return configValue
16+
hackmdAPIEndpointURL: 'https://api.hackmd.io/v1'
2717
}
2818

2919
const envConfig = {
30-
cookiePath: process.env.HMD_CLI_COOKIE_PATH || process.env.CMD_CLI_COOKIE_PATH,
31-
serverUrl: process.env.HMD_CLI_SERVER_URL || process.env.CMD_CLI_SERVER_URL,
32-
enterprise: (process.env.HMD_CLI_COOKIE_PATH || process.env.HMD_CLI_SERVER_URL)
33-
? true
34-
: (process.env.CMD_CLI_COOKIE_PATH || process.env.CMD_CLI_SERVER_URL)
35-
? false
36-
: toBooleanConfig(process.env.HMD_CLI_ENTERPRISE),
37-
loginID: process.env.HMD_CLI_ID || process.env.CMD_CLI_ID,
38-
loginPassword: process.env.HMD_CLI_PASSWORD || process.env.CMD_CLI_PASSWORD
20+
hackmdAPIEndpointURL: process.env.HMD_API_ENDPOINT_URL,
21+
accesstoken: process.env.HMD_API_ACCESS_TOKEN
3922
}
4023

4124
// look for a readable config file; we can merge it with the env.
@@ -66,54 +49,4 @@ ${err}
6649
// prefer environment config over file config
6750
const config = defaults(envConfig, readConfig, defaultConfig)
6851

69-
const cookieDirPath = path.dirname(config.cookiePath)
70-
try {
71-
fs.mkdirSync(cookieDirPath)
72-
} catch (err) {
73-
if (err.code !== 'EEXIST') {
74-
throw new Error(`
75-
76-
Could not create dir for cookie file at ${cookieDirPath}. Encountered exception:
77-
78-
${err}
79-
80-
`)
81-
}
82-
// at this point, the directory exists. if the cookie file does not exist,
83-
// ensure the dir is writable (because we will create the file); otherwise
84-
// ensure the file itself is writable.
85-
let hasExistingCookieFile = false
86-
try {
87-
fs.existsSync(config.cookiePath)
88-
hasExistingConfigFile = true
89-
// tslint:disable-next-line: no-unused
90-
} catch (ignored) {}
91-
92-
if (hasExistingCookieFile) {
93-
try {
94-
fs.accessSync(config.cookiePath, fs.constants.W_OK)
95-
} catch (err) {
96-
throw new Error(`
97-
98-
Cookie file ${config.cookiePath} is not writable. Encountered exception:
99-
100-
${err}
101-
102-
`)
103-
}
104-
} else {
105-
try {
106-
fs.accessSync(cookieDirPath, fs.constants.W_OK)
107-
} catch (err) {
108-
throw new Error(`
109-
110-
Dir for cookie file at ${cookieDirPath} is not writable. Encountered exception:
111-
112-
${err}
113-
114-
`)
115-
}
116-
}
117-
}
118-
11952
export default config

0 commit comments

Comments
 (0)