Click here to Skip to main content
15,888,461 members
Articles / Programming Languages / C#

Using the NPM API Wrapper for .NET

Rate me:
Please Sign up or sign in to vote.
2.71/5 (4 votes)
8 Nov 2023CPOL1 min read 3.8K   2   1
How to use the NuGet package named NPMWrap
A guide to using the NuGet packaged named NPMWrap (an API Wrapper for NPM for .NET)

Introduction

This could help with projects wanting to do things with the NPM Registry API/CLI, providing a set of easy to use and setup tools.

Using the Code

The first step to using NPMWrap is actually installing it, you can either use the CLI or (if you're using Visual Studio), Manage NuGet packages and install it from there. You can see the NuGet package (and the available commands to install it here).

Depending on how you want to use it, you can search the NPM Registry API for packages, it will automatically fetch the API and do things with the data to make it easier to handle.

Using the Registry API

If you want to search through the available packages, use the NPMWrap.Registry.SearchPackages, and to get a package version, use NPMWrap.Registry.GetPackageVersion.

Registry.SearchPackages

This returns a SearchResult object, and an example of its usage is:

C#
NPMWrap.Registry.Config Configuration = new NPMWrap.Registry.Config()
{
   UserAgent = "Your user agent" // Example: NPMWrap
};

SearchResult Result = await NPMWrap.Registry.SearchPackages("Express", Configuration);

Registry.GetPackageVersion

This returns a RegistryPackageVersion object, and an example of its usage (similar to Registry.SearchPackages) is:

C#
NPMWrap.Registry.Config Configuration = new NPMWrap.Registry.Config()
{
   UserAgent = "Your user agent" // Example: NPMWrap
}

RegistryPackageVersion PackageVersion = 
    await NPMWrap.Registry.GetPackageVersion("Express", Configuration);

And, if you want to get a specific version and not just the latest, put a little version code in between the first string and the Configuration.

And that's basically the Registry part.

Commands

Before you do anything with commands, there's a little configuration that needs to be setup, an example of this is:

C#
NPMWrap.Commands.Config Configuration = new NPMWrap.Commands.Config()
{
   UseYarn = false, // Depending on if you want to use Yarn or not.
   Directory = "C:/Path/To/Working/Directory", // If you leave this blank,
                                               // it will throw an exception.
   IsDebug = false, // If set to true and a command is run the CMD window will be visible.
   WaitForExit = true // If you wait for it to exit, 
                      // it may clog up the thread until NPM or Yarn has finished.
};

Commands.RunBaseInstall

This will basically just either do npm install or yarn based on the given configuration, for example:

C#
await RunBaseInstall(Configuration);

Commands.RunInstall

The installation command for adding a package, for example:

C#
await RunBaseInstall("Express", Configuration);

This also can be used to install specific versions of the package by just inserting the version code as a string after the first string.

Commands.RunUninstall

The uninstallation command removes a package, for example:

C#
await RunBaseUninstall("React", Configuration);

History

  • 8th November, 2023: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA8-Nov-23 14:11
professionalȘtefan-Mihai MOGA8-Nov-23 14:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.