Skip to content

Commit

Permalink
fix: The types used by the Card Update builder are broken for array f…
Browse files Browse the repository at this point in the history
…ields, resulting in everything being the 'any' type.
  • Loading branch information
adam-coster committed Oct 2, 2021
1 parent e4223cb commit c140250
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
38 changes: 24 additions & 14 deletions src/lib/entities/BravoCardUpdateBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import type { FavroApi } from '$/index.js';
import type { ExtractKeysByValue, RequiredBy } from '$/types/Utility.js';
import type {
Defined,
ExtractKeysByValue,
RequiredBy,
} from '$/types/Utility.js';
import { BravoError } from '../errors.js';
import {
addToUniqueArray,
addToUniqueArrayBy,
createIsMatchFilter,
ensureArrayExistsAndAddUnique,
ensureArrayExistsAndAddUniqueBy,
isMatch,
removeFromArray,
Expand All @@ -24,9 +28,9 @@ export type CustomFieldOrId<FieldType extends FavroApi.CustomFieldType = any> =
| BravoCustomFieldDefinition<FieldType>
| BravoCustomField<FieldType>;

export type BravoCardFieldWithArrayValue = ExtractKeysByValue<
export type BravoCardFieldWithStringArrayValue = ExtractKeysByValue<
Required<FavroApi.Card.UpdateBody>,
any[]
string[]
>;
/**
* A Card update can be pretty complex, and to save API
Expand Down Expand Up @@ -178,9 +182,12 @@ export class BravoCardUpdateBuilder {
return this;
}

private setCustomFieldUniquely(
customFieldOrId: CustomFieldOrId,
update: FavroApi.CustomFieldValue.UpdateBody,
private setCustomFieldUniquely<FieldType extends FavroApi.CustomFieldType>(
customFieldOrId: CustomFieldOrId<FieldType>,
update: Omit<
FavroApi.CustomFieldValue.UpdateBody<FieldType>,
'customFieldId'
>,
) {
const customFieldId =
typeof customFieldOrId == 'string'
Expand All @@ -191,11 +198,11 @@ export class BravoCardUpdateBuilder {
);
if (currentIdx > -1) {
this.update.customFields.splice(currentIdx, 1, {
customFieldId,
...update,
customFieldId,
});
} else {
this.update.customFields.push({ customFieldId, ...update });
this.update.customFields.push({ ...update, customFieldId });
}
return this;
}
Expand Down Expand Up @@ -420,16 +427,19 @@ export class BravoCardUpdateBuilder {
* appear in another array, e.g. to prevent an "add" and "remove"
* version of the same action from both containing the same value.
*/
private addToUniqueArray<Field extends BravoCardFieldWithArrayValue>(
private addToUniqueArray<Field extends BravoCardFieldWithStringArrayValue>(
updateField: Field,
values: FavroApi.Card.UpdateBody[Field],
opposingField?: BravoCardFieldWithArrayValue,
values: Defined<FavroApi.Card.UpdateBody[Field]>,
opposingField?: BravoCardFieldWithStringArrayValue,
) {
this.update[updateField] ||= [];
ensureArrayExistsAndAddUnique(this.update[updateField], values);
const update = (this.update[updateField] || []) as Defined<
FavroApi.Card.UpdateBody[Field]
>;
addToUniqueArray(update, values);
if (opposingField) {
removeFromArray(this.update[opposingField], values);
}
this.update[updateField] = update;
return this;
}
}
2 changes: 1 addition & 1 deletion src/lib/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export function ensureArrayExistsAndAddUniqueBy<T>(
}

export function removeFromArray<T>(array: T[] | undefined, values: T | T[]) {
for (const value of Array.isArray(values) ? values : [values]) {
for (const value of wrapIfNotArray(values)) {
const idx = array?.findIndex((a) => a === value);
if (typeof idx == 'number' && idx > -1) {
array!.splice(idx, 1);
Expand Down
7 changes: 4 additions & 3 deletions src/types/FavroApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,11 +507,12 @@ export namespace FavroApi {
* // ... do stuff
* \}
*/
export type Model<FieldType extends CustomFieldType = any> =
export type Model<FieldType extends CustomFieldType = CustomFieldType> =
Models[FieldType];

export type UpdateBody<FieldType extends CustomFieldType = any> =
UpdateBodies[FieldType];
export type UpdateBody<
FieldType extends CustomFieldType = CustomFieldType,
> = UpdateBodies[FieldType];

/**
* Data structures used to update Custom Field values on cards.
Expand Down
5 changes: 5 additions & 0 deletions src/types/Utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,8 @@ export type MimeType =
| 'text/plain'
| 'text/html'
| 'text/markdown';

export type Nullish = null | undefined;
export type NotNullish<T> = Exclude<T, Nullish>;
export type NotNull<T> = Exclude<T, null>;
export type Defined<T> = Exclude<T, undefined>;

0 comments on commit c140250

Please sign in to comment.