jira-insights-api
v2.1.2
Published
A Node.js library that automatically generates a TypeScript client for the Atlassian JSM Insight API from the OpenAPI specification
Maintainers
Readme
Atlassian JSM Insight API
A Node.js library that automatically generates a TypeScript client for the Atlassian JSM Insight API from the OpenAPI specification.
📋 Table of Contents
- Features
- Installation
- Quick Start
- Authentication
- API Reference
- Examples
- Development
- Documentation
- Contributing
- License
✨ Features
- Pre-Generated Client: Includes a pre-generated client from the Atlassian JSM Insight API OpenAPI specification
- Runtime Ready: Works reliably in all environments including Docker containers without runtime generation
- Type Safety: Fully typed TypeScript client with accurate models and service definitions
- Modern: Built with modern JavaScript practices and tools
- Flexible: Configure via environment variables or programmatic options
- Comprehensive: Access to all JSM Insight API endpoints and features
- ESM Compatible: Works with both CommonJS and ES Modules projects
- Workspace Discovery: Automatically discovers the workspace ID required for JSM Insight API
📦 Installation
npm install jira-insights-api🚀 Quick Start
import { initAssetsApiClient } from 'jira-insights-api';
import dotenv from 'dotenv';
// Load environment variables
dotenv.config();
async function main() {
// Initialize the client
const insightClient = await initAssetsApiClient({
email: '[email protected]',
apiToken: 'your-api-token-here',
instance: 'your-instance-name'
});
// Use the client to make API calls
const schemaList = await insightClient.DefaultService.schemaList();
console.log(`Found ${schemaList.values.length} schemas`);
}
main().catch(console.error);🔐 Authentication
The Atlassian JSM Insight API uses Basic authentication with email and API token. You can provide your credentials in one of two ways:
1. Environment Variables
Create a .env file in your project root:
[email protected]
ASSETS_API_TOKEN=your-api-token-here
JIRA_INSTANCE=your-instance-nameThen load it in your code:
import dotenv from 'dotenv';
import { initAssetsApiClient } from 'jira-insights-api';
dotenv.config();
const insightClient = await initAssetsApiClient();2. Configuration Options
Pass authentication details directly to the client:
const insightClient = await initAssetsApiClient({
email: '[email protected]',
apiToken: 'your-api-token-here',
instance: 'your-instance-name'
});Getting an API Token
- Go to https://id.atlassian.com/manage-profile/security/api-tokens
- Click "Create API token"
- Give your token a name and click "Create"
- Copy the token value (you won't be able to see it again)
📚 API Reference
The client exposes the following services:
DefaultService
The main service for interacting with Atlassian JSM Insight API. Some key methods include:
schemaList(): Get all schemasobjectTypeFindAll(): Get all object typesobjectFindAll(): Get all objectsobjectFindById(id): Get a specific object by IDobjectCreate(data): Create a new objectobjectUpdate(id, data): Update an existing objectobjectDelete(id): Delete an objectobjectFindHistoryEntries(id): Get history for an object
For a complete list of available methods, initialize the client and explore the available services and methods:
const insightClient = await initAssetsApiClient();
console.log(Object.keys(insightClient.DefaultService));📝 Examples
Working with Objects
// Get all objects of a specific type
const objects = await insightClient.DefaultService.objectFindAll({
objectTypeId: '1',
includeAttributes: true
});
// Get a specific object
const object = await insightClient.DefaultService.objectFindById({
id: '123456789'
});
// Create a new object
const newObject = await insightClient.DefaultService.objectCreate({
requestBody: {
name: 'New Asset',
objectTypeId: '1',
attributes: {
description: 'A new asset created via API'
}
}
});
// Update an object
await insightClient.DefaultService.objectUpdate({
id: '123456789',
requestBody: {
name: 'Updated Asset Name'
}
});
// Delete an object
await insightClient.DefaultService.objectDelete({
id: '123456789'
});Pagination
async function getAllObjects(objectTypeId) {
const allObjects = [];
let page = 1;
let hasMore = true;
while (hasMore) {
const response = await insightClient.DefaultService.objectFindAll({
objectTypeId,
includeAttributes: true,
page,
resultsPerPage: 50
});
if (response && response.length > 0) {
allObjects.push(...response);
console.log(`Retrieved page ${page} with ${response.length} objects (total: ${allObjects.length})`);
page++;
} else {
hasMore = false;
}
}
return allObjects;
}
const allObjects = await getAllObjects('1'); // Replace with an actual object type IDError Handling
try {
const object = await insightClient.DefaultService.objectFindById({
id: 'non-existent-id'
});
} catch (error) {
if (error.status === 404) {
console.error('Object not found');
} else {
console.error('API error:', error.message);
}
}See the examples directory for more usage examples.
🛠️ Development
Project Structure
jira-insights-api/
├── src/
│ ├── downloadAssetsApiSpec.ts # Downloads the OpenAPI spec
│ ├── generateAssetsApiClient.ts # Generates the TypeScript client
│ ├── fixGeneratedCode.ts # Fixes issues in generated code
│ ├── prebuild.ts # Pre-build script for client generation
│ ├── index.ts # Main entry point
│ └── generated/ # Generated API client code (development only)
├── examples/
│ ├── basic-usage.ts # Basic usage example
│ └── advanced-usage.ts # Advanced usage example
├── docs/
│ ├── API.md # API reference documentation
│ └── GETTING_STARTED.md # Getting started guide
├── dist/ # Compiled JavaScript
│ └── generated/ # Pre-generated client code included in the packagePre-Generated Client
Starting with version 2.1.1, this package includes a pre-generated client in the published package. This means:
- No runtime generation is required when using the package
- Works reliably in all environments, including Docker containers
- Faster initialization and better performance
- Consistent behavior across different environments
The client is generated during the package build process and included in the published package.
Available Scripts
npm run download: Download the latest Atlassian JSM Insight API specificationnpm run generate: Generate the TypeScript client from the specification (runs fix automatically after)npm run fix: Fix any issues in the generated codenpm run build: Build the project (includes pre-generating the client)npm run clean: Clean the build directorynpm run clean:generated: Remove all generated files (API spec and generated code)npm run reset: Clean both build and generated filesnpm run example: Run the basic usage examplenpm run example:advanced: Run the advanced usage example
Continuous Integration
This project uses GitHub Actions for continuous integration and deployment:
- Automated Publishing: The package is automatically published to npm when:
- Changes are pushed to the
mainbranch that modifypackage.json - A new GitHub Release is created
- The workflow is manually triggered
- Changes are pushed to the
The CI pipeline:
- Downloads the latest Atlassian JSM Insight API specification
- Generates the TypeScript client during the build process
- Builds the package with the pre-generated client included
- Publishes to npm with public access
To set up automated publishing in your fork:
- Create an npm access token with publish permissions
- Add the token as a GitHub repository secret named
NPM_TOKEN
Configuration Options
interface AssetsApiClientOptions {
// Email address for authentication
email?: string;
// API token for authentication
apiToken?: string;
// Jira instance name (e.g., 'your-instance' from 'your-instance.atlassian.net')
instance?: string;
// Workspace ID for JSM Insight API (if not provided, it will be discovered automatically)
workspaceId?: string;
// Legacy base URL (not recommended for new projects)
baseUrl?: string;
// Path to the API specification file
specFile?: string;
// Directory to output the generated code
outputDir?: string;
// Whether to regenerate the client code
regenerate?: boolean;
}📖 Documentation
Comprehensive documentation is available in the docs directory:
- Getting Started Guide - A step-by-step guide to get up and running
- API Reference - Detailed information about all available methods and options
Additional resources:
- Examples - Example scripts demonstrating various use cases
- Atlassian JSM Insight API Documentation - Official Atlassian documentation
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
