Unable to get version for NPM artifacts

Hi,

I’m developing a plugin for the nexus repository manager. I can get the artifact version for maven as below. But I’m having an issue getting the version for NPM artifacts. What is the best way to get the version programmatically? I’m using 3.26.1-02 nexus-plugin version.

MavenPath mavenPath = (MavenPath) mavenPathAttribute;

MavenPath parsedMavenPath = mavenPathParser .parsePath(mavenPath.getPath());

coordinates = parsedMavenPath.getCoordinates();

Seem like you already have Coordinates of your component, so you can just call coordinates.getVersion() to get version of that component. Please take a look at MavenPath.Coordinates implementation to find more.

We can get it for the Maven repository. But How do we get the version for the NPM?
Maven has the version details in the Maven path

For Npm repo type, it doesn’t have version details in the context

Hi @dsawa I have tried the below option, But it is giving a null value for the versions.

        TokenMatcher.State state = context.getAttributes().require(TokenMatcher.State.class);
        String packageName = (String)state.getTokens().get("packageName");
        String version = (String)state.getTokens().get("packageVersion");

Do you have a Component or Asset object for that NPM artifact? If you do, Asset::getComponent gives you the Component, which has Component::version.

However, your code snippet suggests you may have only the request context. In that case you need to remember that some of your request will be for metadata file that does not have any version associated with. If your context is from request for a tarball, then your code should be working fine, but if your request is coming for a metadata you need to obtain the tarball to get version (this is how npm client works).