|
1 | 1 | // @ts-check |
2 | 2 |
|
| 3 | +import { |
| 4 | + GitHubProjectUnknownFieldOptionError, |
| 5 | + GitHubProjectUpdateReadOnlyFieldError, |
| 6 | +} from "../../index.js"; |
3 | 7 | import { queryItemFieldNodes } from "./queries.js"; |
4 | 8 |
|
5 | 9 | /** |
@@ -45,26 +49,29 @@ export function getFieldsUpdateQueryAndFields(state, fields) { |
45 | 49 | .map((key) => [key, fields[key] === "" ? null : fields[key]]) |
46 | 50 | ); |
47 | 51 |
|
48 | | - const readOnlyFields = Object.keys(existingFields) |
49 | | - .map((key) => [key, state.fields[key].userName]) |
50 | | - .filter(([key]) => { |
51 | | - const field = state.fields[key]; |
| 52 | + const readOnlyFields = Object.entries(existingFields) |
| 53 | + .map(([id, userValue]) => ({ |
| 54 | + id, |
| 55 | + // @ts-expect-error - assume state.fields[id] is not OptionalNonExistingField |
| 56 | + name: String(state.fields[id].name), |
| 57 | + userName: state.fields[id].userName, |
| 58 | + userValue, |
| 59 | + })) |
| 60 | + .filter(({ id, name }) => { |
| 61 | + const field = state.fields[id]; |
52 | 62 | return READ_ONLY_FIELDS.some((readOnlyField) => { |
53 | 63 | return state.matchFieldName( |
54 | 64 | readOnlyField.toLowerCase(), |
55 | 65 |
|
56 | | - // @ts-expect-error - TODO: unclear why `field` is typed as potential "string" here |
57 | | - field.name.toLowerCase().trim() |
| 66 | + name.toLowerCase().trim() |
58 | 67 | ); |
59 | 68 | }); |
60 | 69 | }); |
61 | 70 |
|
62 | 71 | if (readOnlyFields.length > 0) { |
63 | | - throw new Error( |
64 | | - `[github-project] Cannot update read-only fields: ${readOnlyFields |
65 | | - .map(([key, value]) => `"${value}" (.${key})`) |
66 | | - .join(", ")}` |
67 | | - ); |
| 72 | + throw new GitHubProjectUpdateReadOnlyFieldError({ |
| 73 | + fields: readOnlyFields, |
| 74 | + }); |
68 | 75 | } |
69 | 76 |
|
70 | 77 | /** @type {Record<string, {query: string, key: string, value: string|undefined}>[]} */ |
@@ -103,9 +110,9 @@ export function getFieldsUpdateQueryAndFields(state, fields) { |
103 | 110 |
|
104 | 111 | const query = ` |
105 | 112 | ${alias}: updateProjectV2ItemFieldValue(input: {projectId: $projectId, itemId: $itemId, fieldId: "${fieldId}", ${toItemFieldValueInput( |
106 | | - field, |
107 | | - valueOrOption |
108 | | - )}}) { |
| 113 | + field, |
| 114 | + valueOrOption |
| 115 | + )}}) { |
109 | 116 | ${queryNodes} |
110 | 117 | } |
111 | 118 | `; |
@@ -186,20 +193,19 @@ function findFieldOptionIdAndValue(state, field, value) { |
186 | 193 | ) || []; |
187 | 194 |
|
188 | 195 | if (!optionId) { |
189 | | - const knownOptions = Object.keys(field.optionsByValue); |
190 | | - const existingOptionsString = knownOptions |
191 | | - .map((value) => `- ${value}`) |
192 | | - .join("\n"); |
| 196 | + const options = Object.entries(field.optionsByValue).map(([name, id]) => { |
| 197 | + return { name, id }; |
| 198 | + }); |
193 | 199 |
|
194 | 200 | throw Object.assign( |
195 | | - new Error( |
196 | | - `[github-project] "${value}" is an invalid option for "${field.name}".\n\nKnown options are:\n${existingOptionsString}` |
197 | | - ), |
198 | | - { |
199 | | - code: "E_GITHUB_PROJECT_UNKNOWN_FIELD_OPTION", |
200 | | - knownOptions, |
201 | | - userOption: value, |
202 | | - } |
| 201 | + new GitHubProjectUnknownFieldOptionError({ |
| 202 | + field: { |
| 203 | + id: field.id, |
| 204 | + name: field.name, |
| 205 | + options, |
| 206 | + }, |
| 207 | + userValue: value, |
| 208 | + }) |
203 | 209 | ); |
204 | 210 | } |
205 | 211 |
|
|
0 commit comments