harden getApId and getNullableApId against type confusion
This commit is contained in:
@@ -75,24 +75,31 @@ export function getOneApId(value: ApObject): string {
|
|||||||
/**
|
/**
|
||||||
* Get ActivityStreams Object id
|
* Get ActivityStreams Object id
|
||||||
*/
|
*/
|
||||||
export function getApId(value: string | IObject | [string | IObject]): string {
|
export function getApId(source: string | IObject | [string | IObject]): string {
|
||||||
// eslint-disable-next-line no-param-reassign
|
const value = getNullableApId(source);
|
||||||
value = fromTuple(value);
|
|
||||||
|
|
||||||
if (typeof value === 'string') return value;
|
if (value == null) {
|
||||||
if (typeof value.id === 'string') return value.id;
|
throw new IdentifiableError('ad2dc287-75c1-44c4-839d-3d2e64576675', `invalid AP object ${value}: missing or invalid id`);
|
||||||
throw new IdentifiableError('ad2dc287-75c1-44c4-839d-3d2e64576675', `invalid AP object ${value}: missing id`);
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get ActivityStreams Object id, or null if not present
|
* Get ActivityStreams Object id, or null if not present
|
||||||
*/
|
*/
|
||||||
export function getNullableApId(value: string | IObject | [string | IObject]): string | null {
|
export function getNullableApId(source: string | IObject | [string | IObject]): string | null {
|
||||||
// eslint-disable-next-line no-param-reassign
|
const value: unknown = fromTuple(source);
|
||||||
value = fromTuple(value);
|
|
||||||
|
if (value != null) {
|
||||||
|
if (typeof value === 'string') {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
if (typeof (value) === 'object' && 'id' in value && typeof (value.id) === 'string') {
|
||||||
|
return value.id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof value === 'string') return value;
|
|
||||||
if (typeof value.id === 'string') return value.id;
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user