Caseblocks JavaScript API Reference (Modern Functions)

The Caseblocks scripting environment provides various functions for interacting with Caseblocks data and external services. This document lists non-deprecated methods and utilities, which are recommended for new script development.

Table of Contents

  1. Core Environment
  2. Utility Functions

Core Environment

Caseblocks

Type: Reference to the Caseblocks node object.

This is a reference to the Caseblocks node npm package

You can use this to interact with objects in your Caseblocks environment.


requestContext()

const context = requestContext();

Retrieves any current request context metadata, such as authorization tokens or user/session information. Some internal calls may leverage this context automatically.


generateGuid()

const myGuid = generateGuid();

Generates a new GUID (Universally Unique Identifier) as a string.


graphql(query, variables, url)

graphql(`
  query GetCase($id: ID!) {
    case(id: $id) {
      title
      status
    }
  }
`, { id: "1234" })
  .then(response => {
    // handle GraphQL response data
  })
  .catch(error => {
    // handle error
  });

Executes a GraphQL query or mutation against the default or specified GraphQL endpoint.

  • query: A string containing your GraphQL operation.
  • variables: An object mapping variable names to values for the query.
  • url (optional): Specify a custom GraphQL endpoint; defaults to the system’s standard endpoint.
  • Returns: A Promise resolving with the JSON response.

Use Case: Reading or modifying Caseblocks data (e.g., creating, updating, or fetching cases) in a structured, robust manner.


fetch

This is the standard fetch() function, similar to what you’d use in modern browsers.

fetch("https://example.com/api/data")
  .then(res => res.json())
  .then(data => {
    // handle the data object
  })
  .catch(err => {
    // handle errors
  });

Use fetch() for RESTful or other HTTP/HTTPS requests to external APIs, or to perform custom calls within Caseblocks, when GraphQL is not suitable.


Utility Functions

The utils object provides convenience methods for compressing and decompressing data.

utils.deflate()

utils.deflate(
  "some UTF-8 text",
  (base64Zipped) => {
    // base64Zipped is the compressed data in Base64 format
  },
  (error) => {
    // handle error
  }
);

Purpose: Compress a UTF-8 string and return a Base64-encoded representation.

Parameters

  • str: The UTF-8 string to compress.
  • callback: Function invoked on success, receiving the Base64-encoded data.
  • errorHandler: Function invoked on failure, receiving an error object.

utils.inflate()

utils.inflate(
  base64ZippedString,
  (unzipped) => {
    // unzipped is the original UTF-8 string
  },
  (error) => {
    // handle error
  }
);

Purpose: Decompress a Base64-encoded, zipped string back to its original UTF-8 form.

Parameters

  • str: Base64-encoded compressed data.
  • callback: Function invoked on success, receiving the uncompressed text.
  • errorHandler: Function invoked on failure, receiving an error object.

Best Practices

  • Use GraphQL for Caseblocks Data: Whenever possible, prefer graphql() for creating, reading, updating, or deleting cases to leverage structured schemas and robust error handling.
  • Use Fetch for HTTP: For external APIs or custom requests, fetch() is the modern, standard approach.
  • Access Caseblocks: The Caseblocks object is available for advanced or specialized usage. Consider using the provided higher-level functions before resorting to direct library calls.
  • Error Handling: Wrap asynchronous calls (fetch, graphql) in try/catch blocks or .catch chains to handle network or server issues gracefully.
  • Performance Considerations: When working with large strings or JSON data, especially in utils.deflate() and utils.inflate(), ensure you handle potential memory constraints and error conditions.

This concludes the reference for the non-deprecated functions in the Caseblocks JavaScript environment. By using these modern methods—particularly graphql() and fetch()—you can develop robust, maintainable scripts aligned with best practices in Caseblocks.


Copyright © 2024 Caseblocks Limited.