@firstdraft.com/cli 0.7.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -25
- package/docs/README.md +15 -13
- package/docs/commands.md +211 -52
- package/docs/errors.md +42 -6
- package/package.json +6 -4
- package/src/api-authentication.js +55 -13
- package/src/api-response.js +73 -3
- package/src/cli.js +395 -6
- package/src/commands/compilation.js +21 -9
- package/src/commands/login.js +314 -0
- package/src/commands/logout.js +107 -0
- package/src/commands/plan-publish.js +16 -13
- package/src/commands/plan-push.js +5 -5
- package/src/commands/plan-status.js +5 -5
- package/src/compilation-artifact.js +2 -2
- package/src/credentials.js +343 -0
- package/src/oauth.js +565 -0
- package/RELEASING.md +0 -114
- package/docs/release-history.md +0 -77
|
@@ -4,6 +4,37 @@ export const STAGING_API_URL = "https://staging.firstdraft.com";
|
|
|
4
4
|
|
|
5
5
|
export class ApiAuthenticationRequiredError extends Error {}
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Rejects `--staging` together with a FIRSTDRAFT_API_URL that names another
|
|
9
|
+
* origin.
|
|
10
|
+
*
|
|
11
|
+
* @param {string | undefined} apiUrl
|
|
12
|
+
* @param {boolean} staging
|
|
13
|
+
*/
|
|
14
|
+
export function assertStagingSelection(apiUrl, staging) {
|
|
15
|
+
if (
|
|
16
|
+
staging &&
|
|
17
|
+
apiUrl !== undefined &&
|
|
18
|
+
normalizeApiUrl(apiUrl) !== STAGING_API_URL
|
|
19
|
+
) {
|
|
20
|
+
throw new PlanStateConfigurationError(
|
|
21
|
+
"--staging conflicts with FIRSTDRAFT_API_URL. Unset it or select the staging origin.",
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Selects the environment token for an origin: staging uses its own variable,
|
|
28
|
+
* production and custom origins share FIRSTDRAFT_API_TOKEN.
|
|
29
|
+
*
|
|
30
|
+
* @param {string} origin
|
|
31
|
+
* @param {{apiToken?: string, stagingApiToken?: string}} tokens
|
|
32
|
+
*/
|
|
33
|
+
export function environmentTokenFor(origin, { apiToken, stagingApiToken }) {
|
|
34
|
+
const token = origin === STAGING_API_URL ? stagingApiToken : apiToken;
|
|
35
|
+
return hasToken(token) ? token : undefined;
|
|
36
|
+
}
|
|
37
|
+
|
|
7
38
|
/**
|
|
8
39
|
* @param {object} options
|
|
9
40
|
* @param {typeof globalThis.fetch} [options.fetchFunction]
|
|
@@ -11,6 +42,9 @@ export class ApiAuthenticationRequiredError extends Error {}
|
|
|
11
42
|
* @param {string} [options.stagingApiToken]
|
|
12
43
|
* @param {string} [options.apiUrl]
|
|
13
44
|
* @param {boolean} [options.staging]
|
|
45
|
+
* @param {() => Readonly<Record<string, string>>} [options.readStoredTokens]
|
|
46
|
+
* Stored `firstdraft login` tokens keyed by exact origin. Consulted only when
|
|
47
|
+
* the environment has no token for the requested origin.
|
|
14
48
|
*/
|
|
15
49
|
export function authenticateApiCommand({
|
|
16
50
|
fetchFunction,
|
|
@@ -18,19 +52,17 @@ export function authenticateApiCommand({
|
|
|
18
52
|
stagingApiToken,
|
|
19
53
|
apiUrl,
|
|
20
54
|
staging = false,
|
|
55
|
+
readStoredTokens = () => ({}),
|
|
21
56
|
}) {
|
|
22
|
-
|
|
23
|
-
staging &&
|
|
24
|
-
apiUrl !== undefined &&
|
|
25
|
-
normalizeApiUrl(apiUrl) !== STAGING_API_URL
|
|
26
|
-
) {
|
|
27
|
-
throw new PlanStateConfigurationError(
|
|
28
|
-
"--staging conflicts with FIRSTDRAFT_API_URL. Unset it or select the staging origin.",
|
|
29
|
-
);
|
|
30
|
-
}
|
|
57
|
+
assertStagingSelection(apiUrl, staging);
|
|
31
58
|
|
|
32
59
|
const configured = staging ? STAGING_API_URL : apiUrl;
|
|
33
|
-
if (
|
|
60
|
+
if (
|
|
61
|
+
!hasToken(apiToken) &&
|
|
62
|
+
!hasToken(stagingApiToken) &&
|
|
63
|
+
!Object.values(readStoredTokens()).some(hasToken)
|
|
64
|
+
)
|
|
65
|
+
return null;
|
|
34
66
|
|
|
35
67
|
let selectedOrigin = staging ? STAGING_API_URL : undefined;
|
|
36
68
|
const request = fetchFunction ?? globalThis.fetch;
|
|
@@ -43,8 +75,9 @@ export function authenticateApiCommand({
|
|
|
43
75
|
);
|
|
44
76
|
}
|
|
45
77
|
const token =
|
|
46
|
-
endpoint.origin
|
|
47
|
-
|
|
78
|
+
environmentTokenFor(endpoint.origin, { apiToken, stagingApiToken }) ??
|
|
79
|
+
storedToken(readStoredTokens(), endpoint.origin);
|
|
80
|
+
if (token === undefined) throw new ApiAuthenticationRequiredError();
|
|
48
81
|
selectedOrigin = endpoint.origin;
|
|
49
82
|
return request(input, {
|
|
50
83
|
...init,
|
|
@@ -57,7 +90,16 @@ export function authenticateApiCommand({
|
|
|
57
90
|
return { apiUrl: configured, fetchFunction: authorizedFetch };
|
|
58
91
|
}
|
|
59
92
|
|
|
60
|
-
/** @param {string
|
|
93
|
+
/** @param {Readonly<Record<string, string>>} tokens @param {string} origin */
|
|
94
|
+
function storedToken(tokens, origin) {
|
|
95
|
+
const token = Object.hasOwn(tokens, origin) ? tokens[origin] : undefined;
|
|
96
|
+
return hasToken(token) ? token : undefined;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* @param {string | undefined} token
|
|
101
|
+
* @returns {token is string}
|
|
102
|
+
*/
|
|
61
103
|
function hasToken(token) {
|
|
62
104
|
return token !== undefined && token.trim().length > 0;
|
|
63
105
|
}
|
package/src/api-response.js
CHANGED
|
@@ -22,14 +22,84 @@ export class FirstDraftProtocolError extends Error {
|
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Every Service API route the CLI requests. docs/commands.md#service-endpoints
|
|
27
|
+
* has one row for each, and test/service-endpoints.test.js compares the two.
|
|
28
|
+
*/
|
|
29
|
+
export const SERVICE_ROUTES = {
|
|
30
|
+
pushPlan: {
|
|
31
|
+
method: "PUT",
|
|
32
|
+
path: "/v1/projects/{project_id}/foundation-plan",
|
|
33
|
+
},
|
|
34
|
+
readAnalysis: {
|
|
35
|
+
method: "GET",
|
|
36
|
+
path: "/v1/projects/{project_id}/analysis",
|
|
37
|
+
},
|
|
38
|
+
startCompilation: {
|
|
39
|
+
method: "POST",
|
|
40
|
+
path: "/v1/projects/{project_id}/compilations",
|
|
41
|
+
},
|
|
42
|
+
readCompilation: {
|
|
43
|
+
method: "GET",
|
|
44
|
+
path: "/v1/projects/{project_id}/compilations/{compilation_id}",
|
|
45
|
+
},
|
|
46
|
+
downloadArtifact: {
|
|
47
|
+
method: "GET",
|
|
48
|
+
path: "/v1/projects/{project_id}/compilations/{compilation_id}/artifact",
|
|
49
|
+
},
|
|
50
|
+
startPublication: {
|
|
51
|
+
method: "PUT",
|
|
52
|
+
path: "/v1/projects/{project_id}/github-publication",
|
|
53
|
+
},
|
|
54
|
+
readPublication: {
|
|
55
|
+
method: "GET",
|
|
56
|
+
path: "/v1/projects/{project_id}/github-publication",
|
|
57
|
+
},
|
|
58
|
+
requestOAuthToken: {
|
|
59
|
+
method: "POST",
|
|
60
|
+
path: "/oauth/token",
|
|
61
|
+
},
|
|
62
|
+
requestDeviceAuthorization: {
|
|
63
|
+
method: "POST",
|
|
64
|
+
path: "/oauth/device_authorization",
|
|
65
|
+
},
|
|
66
|
+
revokeOAuthToken: {
|
|
67
|
+
method: "POST",
|
|
68
|
+
path: "/oauth/revoke",
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/** @typedef {{method: string, url: URL}} ServiceEndpoint */
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @param {{method: string, path: string}} route
|
|
76
|
+
* @param {string} apiUrl
|
|
77
|
+
* @param {Record<string, string>} parameters
|
|
78
|
+
* @returns {ServiceEndpoint}
|
|
79
|
+
*/
|
|
80
|
+
export function serviceEndpoint(route, apiUrl, parameters) {
|
|
81
|
+
const endpointPath = route.path.replace(/\{(\w+)\}/g, (_, name) => {
|
|
82
|
+
const value = parameters[name];
|
|
83
|
+
if (value === undefined) {
|
|
84
|
+
throw new Error(`${route.path} needs a ${name} value.`);
|
|
85
|
+
}
|
|
86
|
+
return value;
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
return { method: route.method, url: new URL(endpointPath, apiUrl) };
|
|
90
|
+
}
|
|
91
|
+
|
|
25
92
|
/**
|
|
26
93
|
* @param {typeof globalThis.fetch} fetchFunction
|
|
27
|
-
* @param {
|
|
28
|
-
* @param {RequestInit} request
|
|
94
|
+
* @param {ServiceEndpoint} endpoint
|
|
95
|
+
* @param {Omit<RequestInit, "method">} request
|
|
29
96
|
*/
|
|
30
97
|
export async function sendRequest(fetchFunction, endpoint, request) {
|
|
31
98
|
try {
|
|
32
|
-
return await fetchFunction(endpoint,
|
|
99
|
+
return await fetchFunction(endpoint.url, {
|
|
100
|
+
...request,
|
|
101
|
+
method: endpoint.method,
|
|
102
|
+
});
|
|
33
103
|
} catch (error) {
|
|
34
104
|
if (
|
|
35
105
|
error instanceof PlanStateConfigurationError ||
|