-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make param type of BlobApi.getUrl()
less strict?
#483
Comments
What are the differences vs the attachment type? I thought it was only that the blobId has a variant property, which is necessary. |
Yeah that's the primary difference in terms of defined fields, but I think there's something about how export function someFunction(
attachment: Observation['attachments'][number],
// Also tried simplifying this to just 'original' | 'preview' | 'thumbnail'
variant: BlobVariant<
// TODO: Ideally derive this from `attachment` param or something that looks simpler...
Exclude<Observation['attachments'][number]['type'], 'UNRECOGNIZED'>
>,
) {
return project.$blobs.getUrl({
driveId: attachment.driveDiscoveryId,
name: attachment.name,
type: attachment.type,
variant,
})
} I'm getting the following error:
Wish I could provide a TS playground link but importing the alpha version of mapeo schema doesn't seem feasible. UPDATE: Seems like the main complaint is about using return project.$blobs.getUrl({
driveId: attachment.driveDiscoveryId,
name: attachment.name,
type: 'photo', // works with 'video' and 'audio' too
variant,
}); |
Yeah it's because each type has different variants available, so the type is strict to stop you requesting a variant that does not exist for the given type. |
Yeah the reasoning makes sense - is the only solution to do some (somewhat tedious) runtime checks before passing the arguments? UPDATE: Not as tedious as I thought but definitely looks a little funny: switch (attachment.type) {
case 'UNRECOGNIZED': {
throw new Error('Cannot get URL for unrecognized attachment type');
}
case 'video':
case 'audio': {
if (variant !== 'original') {
throw new Error('Cannot get URL of attachment for variant');
}
return project.$blobs.getUrl({
driveId: attachment.driveDiscoveryId,
name: attachment.name,
type: attachment.type,
variant,
});
}
case 'photo': {
return project.$blobs.getUrl({
driveId: attachment.driveDiscoveryId,
name: attachment.name,
type: attachment.type,
variant,
});
}
} UPDATE: What would be more ideal is to make sure that the |
Description
Currently doing some frontend work that uses the BlobApi to get the http url for a specified observation attachment. I'm having a difficult time appeasing TypeScript due to the
getUrl(blobId)
expecting aBlobId
type, which is more precise (uses a discriminate union) and notably differs from an attachment type as described byObservation['attachments'][number]
in mapeo schema.The
getUrl(blobId)
works great if passing a value created byBlobApi.create()
(as demonstrated in our tests), but given the type challenges when using the context of observation attachments, I'm wondering if we could update the param type to avoid convoluted typescript-specific code in the consuming application. Perhaps there's something I'm missing in terms of usage that is giving me problems with Typescript but my initial thought is that it would probably be easier to just update the param type (and maybe introduce runtime validation if really necessary)The text was updated successfully, but these errors were encountered: