-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsaveToken.ts
More file actions
32 lines (27 loc) · 993 Bytes
/
saveToken.ts
File metadata and controls
32 lines (27 loc) · 993 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { writeFile } from "fs";
import { promisify } from "util";
const writeFileAsync = promisify(writeFile);
export const SAVE_TOKEN_FILE_PREFIX = "/tmp/node-api-toolkit-save-token-";
export type SaveTokenOpts = {
token: string;
tokenIdentifier?: string;
filePath?: string;
};
/**
* Saves a token to a file (pass either tokenIdentifier which will save to a unique file in /tmp/) or pass your own
* filePath
* @param param0.tokenIdentifier - Unique identifier for accessing this token (ex: DROPBOX_API_KEY_USER_123)
* @param param0.token - The token that you're trying to save
* @param param0.filePath - The file path to store the token in
*/
export default ({ tokenIdentifier, token, filePath }: SaveTokenOpts) => {
let path;
if (tokenIdentifier) {
path = `${SAVE_TOKEN_FILE_PREFIX}${tokenIdentifier}`;
} else if (filePath) {
path = filePath;
} else {
throw new Error("tokenIdentifier or filePath required");
}
return writeFileAsync(path, token);
};