-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathenv.ts
78 lines (71 loc) · 1.85 KB
/
env.ts
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { CookieJar, CookieOptions, wrapFetch } from "../deps.ts";
import { readline } from "./utils.ts";
export type Prompts = {
/**
* Prompt the user to enter the npf url.
*/
promptLogin: (url: string) => Promise<string>;
/**
* Prompt the user to enter the string.
*/
prompt: (tips: string) => Promise<string>;
};
export type Fetcher = {
get(opts: { url: string; headers?: HeadersInit }): Promise<Response>;
post(
opts: { url: string; body?: BodyInit; headers?: HeadersInit },
): Promise<Response>;
};
export type Logger = {
debug: (...msg: unknown[]) => void;
log: (...msg: unknown[]) => void;
warn: (...msg: unknown[]) => void;
error: (...msg: unknown[]) => void;
};
export type Env = {
prompts: Prompts;
logger: Logger;
newFetcher: (opts?: { cookies?: CookieOptions[] }) => Fetcher;
};
export const DEFAULT_ENV: Env = {
prompts: {
promptLogin: async (url: string) => {
console.log("Navigate to this URL in your browser:");
console.log(url);
console.log(
'Log in, right click the "Select this account" button, copy the link address, and paste it below:',
);
return await readline();
},
prompt: async (tips: string) => {
console.log(tips);
return await readline();
},
},
logger: {
debug: console.debug,
log: console.log,
warn: console.warn,
error: console.error,
},
newFetcher: ({ cookies } = {}) => {
const cookieJar = new CookieJar(cookies);
const fetch = wrapFetch({ cookieJar });
return {
async get({ url, headers }) {
return await fetch(url, {
method: "GET",
headers,
});
},
async post({ url, body, headers }) {
return await fetch(url, {
method: "POST",
headers,
body,
});
},
};
},
};
export type { CookieOptions };