Oracle Cloud Infrastructure Node.js/TypeScript Client for Work Requests
The `oci-workrequests` package is an official Oracle Cloud Infrastructure (OCI) Node.js/TypeScript client specifically designed to interact with the OCI Work Requests service. Currently at version 2.130.0, this package is part of the broader OCI TypeScript SDK and receives very frequent updates, often on a daily or weekly basis, reflecting continuous feature development and alignment with new OCI service capabilities. It provides a type-safe and idiomatic interface for managing asynchronous operations within OCI, allowing developers to programmatically monitor the status and outcomes of long-running tasks across various OCI services. Key differentiators include its tight integration with the OCI ecosystem, comprehensive type definitions, and adherence to OCI's API standards, simplifying the development of robust cloud automation and management scripts.
Common errors
-
Error: NotAuthenticated: The required information to complete authentication was not supplied.
cause The authentication details provider could not find valid credentials (e.g., `~/.oci/config` file missing or malformed, environment variables not set).fixVerify your OCI configuration file (`~/.oci/config`) exists and is correctly formatted, or ensure environment variables like `OCI_CLI_PROFILE` and `OCI_CLI_CONFIG_FILE` are set if using a non-default profile/location. -
Error: ServiceError: Authorization failed or requested resource not found. (opc-request-id: <request-id>)
cause The authenticated user lacks the necessary IAM policies to perform the requested operation, or the specified resource (e.g., compartmentId) does not exist or is incorrect.fixCheck your OCI IAM policies to ensure the user or instance principal has permissions for the Work Requests service. Double-check the OCIDs used in your request for correctness. -
TypeError: Cannot read properties of undefined (reading 'WorkRequestsClient')
cause Incorrect import statement or `oci-workrequests` package not installed or incorrectly resolved by Node.js/TypeScript.fixEnsure `npm install oci-workrequests` has been run successfully and your import statement is `import { WorkRequestsClient } from 'oci-workrequests';`.
Warnings
- gotcha OCI SDK authentication can be complex, requiring a correctly configured `~/.oci/config` file, instance principals for compute instances, or session tokens for Cloud Shell. Incorrect setup is a common source of errors.
- breaking Major version updates to the overall OCI SDK (e.g., from v2 to v3) often involve significant changes to module structure, authentication patterns, or API interfaces, requiring code adjustments.
- gotcha The `oci-workrequests` package (and other service-specific packages) receives very frequent updates. While this brings new features, it can also introduce subtle behavior changes or dependency conflicts if not managed carefully.
- gotcha OCI API calls, especially for listing operations, often require a `compartmentId`. Forgetting this or providing an incorrect OCID will result in unauthorized or not found errors.
Install
-
npm install oci-workrequests -
yarn add oci-workrequests -
pnpm add oci-workrequests
Imports
- WorkRequestsClient
const WorkRequestsClient = require('oci-workrequests');import { WorkRequestsClient } from 'oci-workrequests'; - common
import common from 'oci-common';
import * as common from 'oci-common';
- ListWorkRequestsRequest
import { requests } from 'oci-workrequests'; const request: requests.ListWorkRequestsRequest = { /* ... */ };
Quickstart
import { WorkRequestsClient } from 'oci-workrequests';
import * as common from 'oci-common';
import { requests } from 'oci-workrequests';
async function listAllWorkRequests(): Promise<void> {
try {
// Configure authentication using default provider (e.g., OCI config file or instance principal)
const provider = new common.auth.DefaultAuthenticationDetailsProvider();
// Initialize the WorkRequests client
const client = new WorkRequestsClient({
authenticationDetailsProvider: provider
});
// Define the request to list work requests
// Replace 'ocid1.compartment.oc1..aaaaaaaaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' with your compartment OCID
const listRequest: requests.ListWorkRequestsRequest = {
compartmentId: process.env.OCI_COMPARTMENT_ID ?? 'ocid1.compartment.oc1..aaaaaaaaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
// You can add filters like status, resourceId, etc.
limit: 10 // Limit to 10 results for brevity
};
// Call the ListWorkRequests API
console.log('Listing OCI Work Requests...');
let response = await client.listWorkRequests(listRequest);
if (response.items && response.items.length > 0) {
console.log(`Found ${response.items.length} work requests:`)
for (const wr of response.items) {
console.log(` ID: ${wr.id}, Status: ${wr.status}, Resource: ${wr.resources ? wr.resources[0]?.identifier : 'N/A'}`);
}
} else {
console.log('No work requests found in the specified compartment.');
}
} catch (error) {
console.error('Error listing work requests:', error);
}
}
listAllWorkRequests();