pub-dev icon indicating copy to clipboard operation
pub-dev copied to clipboard

Is there a away to get latest package version with rest api

Open burhankhanzada opened this issue 2 years ago • 10 comments

i am working on my custom package and want to show the latest version on my package in my README.md file dynamically without updating it manually or by CI so i find shields.io have this kind of functionality but its generate bage for it and give a url to render it then i look it the source code of it and find out there is rest api for getting all version for a package from which shields.io extract the latest version.

GET https://pub.dartlang.org/packages/<package_name>.json

i wonder is there a way to get latest single version instead of full list of version.

burhankhanzada avatar Dec 11 '23 07:12 burhankhanzada

I think shields.io already has support for showing the latest published version on pub https://shields.io/badges/pub-version:

https://img.shields.io/pub/v/mobx.svg?label=mobx&color=blue yields mobx version

i wonder is there a way to get latest single version instead of full list of version.

We don't have a specific endpoint for that, but the https://pub.dartlang.org/packages/<package_name>.json contains a latest entry that is easy to pick out with a bit of json manipulation.

sigurdm avatar Dec 11 '23 10:12 sigurdm

i don't see any latest entry in the response of https://pub.dartlang.org/packages/<package_name>.json

for example the response of

https://pub.dartlang.org/packages/mobx.json

return this which don't have any latest entry

{
    "name": "mobx",
    "versions": [
        "2.2.3",
        "2.2.1",
        "2.2.0",
        "2.1.4",
        "2.1.3+1",
        "2.1.3",
        "2.1.2",
        "2.1.1",
        "2.1.0",
        "2.0.7+7",
        "2.0.7+6",
        "2.0.7+5",
        "2.0.7+4",
        "2.0.7+3",
        "2.0.7+2",
        "2.0.7",
        "2.0.6+1",
        "2.0.6",
        "2.0.5",
        "2.0.4",
        "2.0.3",
        "2.0.1",
        "2.0.0",
        "2.0.0-nullsafety.6",
        "2.0.0-nullsafety.5",
        "2.0.0-nullsafety.4",
        "2.0.0-nullsafety.3",
        "2.0.0-nullsafety.2",
        "2.0.0-nullsafety.1",
        "2.0.0-nullsafety.0",
        "1.2.1+4",
        "1.2.1+3",
        "1.2.1+2",
        "1.2.1+1",
        "1.2.1",
        "1.2.0",
        "1.1.1",
        "1.1.0",
        "1.0.2+2",
        "1.0.2+1",
        "1.0.2",
        "1.0.1",
        "1.0.0",
        "0.4.0+4",
        "0.4.0+3",
        "0.4.0+2",
        "0.4.0+1",
        "0.4.0",
        "0.3.10",
        "0.3.9+3",
        "0.3.9+2",
        "0.3.9+1",
        "0.3.9",
        "0.3.8+1",
        "0.3.8",
        "0.3.7",
        "0.3.6",
        "0.3.6-alpha",
        "0.3.5+1",
        "0.3.5",
        "0.3.4",
        "0.3.3+2",
        "0.3.3+1",
        "0.3.3",
        "0.3.3-alpha",
        "0.3.2+2",
        "0.3.2+1",
        "0.3.2",
        "0.3.1",
        "0.3.0+3",
        "0.3.0+2",
        "0.3.0",
        "0.3.0-alpha+4",
        "0.3.0-alpha+3",
        "0.3.0-alpha+2",
        "0.3.0-alpha",
        "0.2.1+1",
        "0.2.1",
        "0.2.0",
        "0.1.4",
        "0.1.3",
        "0.1.2",
        "0.1.1",
        "0.1.0",
        "0.0.37",
        "0.0.36",
        "0.0.35",
        "0.0.34",
        "0.0.33",
        "0.0.32",
        "0.0.31",
        "0.0.30",
        "0.0.29",
        "0.0.28",
        "0.0.27",
        "0.0.26",
        "0.0.25",
        "0.0.24",
        "0.0.22",
        "0.0.21",
        "0.0.20",
        "0.0.19",
        "0.0.18",
        "0.0.17",
        "0.0.16",
        "0.0.15",
        "0.0.14",
        "0.0.13",
        "0.0.12",
        "0.0.11",
        "0.0.10",
        "0.0.9",
        "0.0.8",
        "0.0.7",
        "0.0.6",
        "0.0.5",
        "0.0.4",
        "0.0.3",
        "0.0.2",
        "0.0.1"
    ]
}

burhankhanzada avatar Dec 11 '23 11:12 burhankhanzada

Ah, sorry I didn't notice, you are using the wrong endpoint.

You should use: https://pub.dev/api/packages/mobx

@isoos why do we even have this other endpoint (https://pub.dartlang.org/packages/mobx.json)?

sigurdm avatar Dec 11 '23 11:12 sigurdm

@jonasfj @sigurdm: I'm unsure: why do we have the /packages/<package>.json API endpoint?

@burhankhanzada: I think @sigurdm wanted to link to https://pub.dev/api/packages/<package> endpoint, which does have a latest.

isoos avatar Dec 11 '23 11:12 isoos

@isoos well https://pub.dev/api/packages/<package> is great have all details of every version and latest entry also as you mention but a https://pub.dev/api/packages/<package>/latest/version would be great use case for many package developers as its still difficult to extract version number from latest entry and and show in markdown README.md file.

burhankhanzada avatar Dec 11 '23 12:12 burhankhanzada

Even if you had an endpoint with the latest version number, how would you embed that in markdown?

And if you anyway have to pre-process the result from the endpoint there seem to be no problem with picking the version-number out of https://pub.dev/api/packages/<package> (for example using jq).

sigurdm avatar Dec 11 '23 12:12 sigurdm

well @sigurdm you are right even with simple respon of version is not use ful as can't be embedded without pre-processing like shields.io, sorry i didn't think about it.

burhankhanzada avatar Dec 11 '23 12:12 burhankhanzada

No worries. I think the shields are an ok option for most use-cases...

@jonasfj should we look into removing the https://pub.dartlang.org/packages/mobx.json endpoint? It seems to be a liability. But shields.io might use it...

sigurdm avatar Dec 11 '23 12:12 sigurdm

Yeah it does: https://github.com/badges/shields/blob/7d391b4277042b0f537ffc59948085fc1805ca4c/services/pub/pub.service.js#L48

sigurdm avatar Dec 11 '23 12:12 sigurdm

Created https://github.com/badges/shields/pull/9802 to at least get one less client of the endpoint...

sigurdm avatar Dec 11 '23 13:12 sigurdm