@firstdraft.com/cli 0.9.0 → 0.10.0

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.
@@ -35,6 +35,13 @@ export function environmentTokenFor(origin, { apiToken, stagingApiToken }) {
35
35
  return hasToken(token) ? token : undefined;
36
36
  }
37
37
 
38
+ /** The variable that holds the environment token for an origin. @param {string} origin */
39
+ export function environmentTokenVariable(origin) {
40
+ return origin === STAGING_API_URL
41
+ ? "FIRSTDRAFT_STAGING_API_TOKEN"
42
+ : "FIRSTDRAFT_API_TOKEN";
43
+ }
44
+
38
45
  /**
39
46
  * @param {object} options
40
47
  * @param {typeof globalThis.fetch} [options.fetchFunction]
@@ -66,6 +73,10 @@ export function authenticateApiCommand({
66
73
 
67
74
  let selectedOrigin = staging ? STAGING_API_URL : undefined;
68
75
  const request = fetchFunction ?? globalThis.fetch;
76
+ /** @param {string} origin */
77
+ const tokenFor = (origin) =>
78
+ environmentTokenFor(origin, { apiToken, stagingApiToken }) ??
79
+ storedTokenFor(readStoredTokens(), origin);
69
80
  /** @type {typeof globalThis.fetch} */
70
81
  const authorizedFetch = (input, init) => {
71
82
  const endpoint = new URL(input instanceof Request ? input.url : input);
@@ -74,9 +85,7 @@ export function authenticateApiCommand({
74
85
  "The requested API environment does not match the Project origin. No request was made.",
75
86
  );
76
87
  }
77
- const token =
78
- environmentTokenFor(endpoint.origin, { apiToken, stagingApiToken }) ??
79
- storedToken(readStoredTokens(), endpoint.origin);
88
+ const token = tokenFor(endpoint.origin);
80
89
  if (token === undefined) throw new ApiAuthenticationRequiredError();
81
90
  selectedOrigin = endpoint.origin;
82
91
  return request(input, {
@@ -87,11 +96,21 @@ export function authenticateApiCommand({
87
96
  },
88
97
  });
89
98
  };
90
- return { apiUrl: configured, fetchFunction: authorizedFetch };
99
+ return {
100
+ apiUrl: configured,
101
+ fetchFunction: authorizedFetch,
102
+ /** Whether a request to `origin` would carry a token. @param {string} origin */
103
+ hasTokenFor: (origin) => tokenFor(origin) !== undefined,
104
+ };
91
105
  }
92
106
 
93
- /** @param {Readonly<Record<string, string>>} tokens @param {string} origin */
94
- function storedToken(tokens, origin) {
107
+ /**
108
+ * The token a saved login holds for exactly this origin, if any.
109
+ *
110
+ * @param {Readonly<Record<string, string>>} tokens
111
+ * @param {string} origin
112
+ */
113
+ export function storedTokenFor(tokens, origin) {
95
114
  const token = Object.hasOwn(tokens, origin) ? tokens[origin] : undefined;
96
115
  return hasToken(token) ? token : undefined;
97
116
  }
@@ -51,13 +51,41 @@ export const SERVICE_ROUTES = {
51
51
  method: "GET",
52
52
  path: "/v1/projects/{project_id}/compilations/{compilation_id}/artifact",
53
53
  },
54
- startPublication: {
55
- method: "PUT",
56
- path: "/v1/projects/{project_id}/github-publication",
54
+ readAccount: {
55
+ method: "GET",
56
+ path: "/v1/account",
57
+ },
58
+ sendProgressReport: {
59
+ method: "POST",
60
+ path: "/v1/projects/{project_id}/compilations/{compilation_id}/progress-reports",
61
+ },
62
+ sendFailureReport: {
63
+ method: "POST",
64
+ path: "/v1/projects/{project_id}/compilations/{compilation_id}/failure-reports",
57
65
  },
58
- readPublication: {
66
+ sendNote: {
67
+ method: "POST",
68
+ path: "/v1/projects/{project_id}/notes",
69
+ },
70
+ listMaintenanceItems: {
59
71
  method: "GET",
60
- path: "/v1/projects/{project_id}/github-publication",
72
+ path: "/v1/projects/{project_id}/environments/{environment_name}/items",
73
+ },
74
+ readMaintenanceItem: {
75
+ method: "GET",
76
+ path: "/v1/projects/{project_id}/environments/{environment_name}/items/{item_id}",
77
+ },
78
+ resolveMaintenanceItem: {
79
+ method: "POST",
80
+ path: "/v1/projects/{project_id}/environments/{environment_name}/items/{item_id}/resolution",
81
+ },
82
+ deferMaintenanceItem: {
83
+ method: "POST",
84
+ path: "/v1/projects/{project_id}/environments/{environment_name}/items/{item_id}/deferral",
85
+ },
86
+ askMaintenanceOwner: {
87
+ method: "POST",
88
+ path: "/v1/projects/{project_id}/environments/{environment_name}/items/{item_id}/owner-question",
61
89
  },
62
90
  requestOAuthToken: {
63
91
  method: "POST",
@@ -164,6 +192,23 @@ export function isProblemBody(response, body) {
164
192
  );
165
193
  }
166
194
 
195
+ /**
196
+ * The fields of a problem that isProblemBody accepted, and nothing else.
197
+ *
198
+ * @param {unknown} body
199
+ */
200
+ export function problemProjection(body) {
201
+ const problem = /** @type {Record<string, unknown>} */ (body);
202
+
203
+ return {
204
+ ...(problem.type === "about:blank" ? { type: problem.type } : {}),
205
+ title: problem.title,
206
+ status: problem.status,
207
+ code: problem.code,
208
+ detail: problem.detail,
209
+ };
210
+ }
211
+
167
212
  /**
168
213
  * @param {unknown} value
169
214
  * @returns {value is Record<string, unknown> & {severity: "error" | "warning"}}