recursive.codes

recursive.codes

recursive.codes


The Personal Blog of Todd Sharp

OCI SDK For TypeScript Is Now Available - Here's How To Use It In Your JavaScript Projects

Posted By: Todd Sharp on 6/4/2020 12:00 GMT
Tagged: Cloud, JavaScript

I have blogged quite a bit about our OCI Java SDK in the past. It's easy to use and a very intuitive and consistent SDK that can perform just about any possible task you can imagine when it comes to the Oracle Cloud. But I'm not just a Java developer. I use JavaScript quite a bit too on both the client and the server-side. In fact, when it comes to simple "one-off" scripts, let's be honest it is usually much quicker and easier to throw it together in JS than in Java. That's why I'm so happy to hear that our OCI SDK for TypeScript has been made generally available.

Note: Even though it's called the SDK for TypeScript, it can be used in any "vanilla" JavaScript project or TypeScript project on the server.

In this post I'm going to show you some examples of using the TypeScript SDK, but first let me share a few important links that you will want to bookmark if you'll be doing any work with it.

Important Links

Using The SDK With JavaScript On The Server

The TypeScript SDK can easily be used in a "vanilla" Node.JS application. To get started, check the examples linked above or create a simple Node project and install the SDK with: 

npm install oci-sdk

Now pull in some dependencies. In this example, we'll use the Object Storage and the Core modules (see the full list of services supported) so we'll need to pull in oci-objectstorage and oci-core to make calls with the necessary clients. We'll also need oci-common and we'll pull in util to prettify our output later on.

Next, set a few variables to sore our configuration file details and our compartmentId and tenancyName:

We'll need to create an instance of an authentication provider that we'll have to pass to the clients when we construct them. For this, we'll use a ConfigFileAuthenticationDetailsProvider from the oci-common module.

Now we can create an instance of the ComputeClient, passing our authProvider:

Now we can construct a listInstancesRequest and call listInstances on the client. Since we're using plain JavaScript, the listInstancesRequest will just be a simple object containing keys relating to the expected parameters. The call to the client will return a Promise, so we can get our results in the then() method, collect our instance ID and display name, and log those values.

If we save and run this script, we'll get output similar to the following:

The object storage module follows the same patterns, so it's simple to construct an objectStorageClient and perform operations like listBuckets and listObjects.

If Promises aren't your thing, you can use async/await:

Using The SDK With TypeScript On The Server

Another option with the TypeScript SDK is to use it directly with TypeScript scripts. This gives you the added benefit of explicitly typed objects which means your IDE will provide you with plenty of insight into the SDK. Here's an example of using a TypeScript file to grab the namespace from the object storage client. It looks mostly the same, but note the addition of the types for the provider, client, request and response objects.

Summary

In this post, we looked at the brand new TypeScript SDK for OCI. We learned how to construct authentication providers, module clients, and requests and how to send those requests to the OCI API via the client instances. If you have any questions or feedback, please leave a comment below or refer to the documentation or GitHub links above. Otherwise, get started integrating this into your Node projects today!

Photo by Jaclyn Clark on Unsplash



Related Posts

Querying Autonomous Database from an Oracle Function (The Quick, Easy & Completely Secure Way)

Querying Autonomous Database from an Oracle Function (The Quick, Easy & Completely Secure Way)

I've written many blog posts about connecting to an Autonomous DB instance in the past. Best practices evolve as tools, services, and frameworks become...

Brain to the Cloud - Part III - Examining the Relationship Between Brain Activity and Video Game Performance

Brain to the Cloud - Part III - Examining the Relationship Between Brain Activity and Video Game Performance

In my last post, we looked at the technical aspects of my Brain to the Cloud project including much of the code that was used to collect and analyze the...

Brain to the Cloud - Part II - How I Uploaded My Brain to the Cloud

Brain to the Cloud - Part II - How I Uploaded My Brain to the Cloud

In my last post, we went over the inspiration, objectives, and architecture for my Brain to the Cloud project. In this post, we'll look in-depth at the...

Note: Comments are currently closed on this blog. Disqus is simply too bloated to justify its use with the low volume of comments on this blog. Please visit my contact page if you have something to say!