@settlemint/sdk-js
Version:
Core JavaScript SDK for integrating SettleMint's blockchain platform services into your applications
1,640 lines (1,627 loc) • 80.8 kB
JavaScript
/* SettleMint SDK - Main Package */
import { fetchWithRetry } from "@settlemint/sdk-utils/http";
import { ensureServer } from "@settlemint/sdk-utils/runtime";
import { AccessTokenSchema, IdSchema, LOCAL_INSTANCE, STANDALONE_INSTANCE, UrlSchema, validate } from "@settlemint/sdk-utils/validation";
import { GraphQLClient } from "graphql-request";
import { z } from "zod";
import { initGraphQLTada } from "gql.tada";
import { createHash } from "node:crypto";
//#region src/helpers/graphql.ts
/**
* Initializes the GraphQL client with specific configurations.
*
* @returns A configured GraphQL client instance.
*/
const graphql = initGraphQLTada();
//#endregion
//#region src/graphql/workspace.ts
/**
* GraphQL fragment containing core workspace fields.
*/
const WorkspaceFragment = graphql(`
fragment Workspace on Workspace {
id
uniqueName
name
applications {
id
uniqueName
name
}
}
`);
/**
* Query to fetch all workspaces and their applications.
*/
const getWorkspacesAndApplications = graphql(`
query getWorkspacesAndApplications {
workspaces {
...Workspace
childWorkspaces {
...Workspace
}
}
}
`, [WorkspaceFragment]);
/**
* Query to fetch a specific workspace by unique name.
*/
const getWorkspace = graphql(`
query getWorkspace($uniqueName: String!) {
workspaceByUniqueName(uniqueName: $uniqueName) {
...Workspace
}
}
`, [WorkspaceFragment]);
/**
* Mutation to create a new workspace.
*/
const createWorkspace = graphql(`
mutation CreateWorkspace(
$addressLine1: String
$addressLine2: String
$city: String
$companyName: String
$country: String
$name: String!
$parentId: String
$paymentMethodId: String
$postalCode: String
$taxIdType: String
$taxIdValue: String
) {
createWorkspace(
addressLine1: $addressLine1
addressLine2: $addressLine2
city: $city
companyName: $companyName
country: $country
name: $name
parentId: $parentId
paymentMethodId: $paymentMethodId
postalCode: $postalCode
taxIdType: $taxIdType
taxIdValue: $taxIdValue
) {
...Workspace
}
}
`, [WorkspaceFragment]);
/**
* Mutation to delete a workspace.
*/
const deleteWorkspace = graphql(`
mutation deleteWorkspace($uniqueName: String!) {
deleteWorkspaceByUniqueName(uniqueName: $uniqueName) {
...Workspace
}
}
`, [WorkspaceFragment]);
/**
* Mutation to add credits to a workspace.
*/
const addCredits = graphql(`
mutation addCredits($workspaceId: String!, $amount: Float!) {
addCredits(workspaceId: $workspaceId, amount: $amount)
}
`);
/**
* Creates a function to list all workspaces and their applications.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that returns all workspaces sorted by name
* @throws If the request fails
*/
const workspaceList = (gqlClient) => {
return async () => {
const { workspaces } = await gqlClient.request(getWorkspacesAndApplications);
const allWorkspaces = workspaces.reduce((acc, workspace) => {
acc.push(workspace);
if (workspace.childWorkspaces) {
acc.push(...workspace.childWorkspaces);
}
return acc;
}, []);
return allWorkspaces.sort((a, b) => a.name.localeCompare(b.name));
};
};
/**
* Creates a function to read a specific workspace by unique name.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that fetches a single workspace by unique name
* @throws If the workspace cannot be found or the request fails
*/
const workspaceRead = (gqlClient) => {
return async (workspaceUniqueName) => {
const { workspaceByUniqueName } = await gqlClient.request(getWorkspace, { uniqueName: workspaceUniqueName });
return workspaceByUniqueName;
};
};
/**
* Creates a function to create a new workspace.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that creates a new workspace with the provided configuration
* @throws If the creation fails or validation errors occur
*/
const workspaceCreate = (gqlClient) => {
return async (createWorkspaceArgs) => {
const { createWorkspace: workspace } = await gqlClient.request(createWorkspace, createWorkspaceArgs);
return workspace;
};
};
/**
* Creates a function to delete a workspace.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that deletes a workspace by unique name
* @throws If the workspace cannot be found or the deletion fails
*/
const workspaceDelete = (gqlClient) => {
return async (workspaceUniqueName) => {
const { deleteWorkspaceByUniqueName: workspace } = await gqlClient.request(deleteWorkspace, { uniqueName: workspaceUniqueName });
return workspace;
};
};
/**
* Creates a function to add credits to a workspace.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that adds credits to a workspace
* @throws If the workspace ID is invalid or amount is not positive
*/
const workspaceAddCredits = (gqlClient) => {
return async (workspaceId, amount) => {
const id = validate(IdSchema, workspaceId);
if (amount <= 0) {
throw new Error("Credit amount must be a positive number");
}
const { addCredits: result } = await gqlClient.request(addCredits, {
workspaceId: id,
amount
});
return result;
};
};
//#endregion
//#region src/graphql/application.ts
/**
* GraphQL fragment containing core application fields.
*/
const ApplicationFragment = graphql(`
fragment Application on Application {
id
uniqueName
name
workspace {
id
uniqueName
name
}
}
`);
/**
* Query to fetch applications for a workspace.
*/
const listApplications = graphql(`
query ListApplications($workspaceUniqueName: String!) {
workspaceByUniqueName(uniqueName: $workspaceUniqueName) {
applications {
...Application
}
}
}
`, [ApplicationFragment]);
/**
* Query to fetch a specific application.
*/
const readApplication = graphql(`
query ReadApplication($applicationUniqueName: String!) {
applicationByUniqueName(uniqueName: $applicationUniqueName) {
...Application
}
}
`, [ApplicationFragment]);
/**
* Mutation to create a new application.
*/
const createApplication = graphql(`
mutation CreateApplication($name: String!, $workspaceId: ID!) {
createApplication(name: $name, workspaceId: $workspaceId) {
...Application
}
}
`, [ApplicationFragment]);
/**
* Mutation to delete an application.
*/
const deleteApplication = graphql(`
mutation DeleteApplication($uniqueName: String!) {
deleteApplicationByUniqueName(uniqueName: $uniqueName) {
...Application
}
}
`, [ApplicationFragment]);
/**
* Creates a function to list applications in a workspace.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that fetches applications for a workspace
* @throws If the workspace cannot be found or the request fails
*/
const applicationList = (gqlClient) => {
return async (workspaceUniqueName) => {
const { workspaceByUniqueName: { applications } } = await gqlClient.request(listApplications, { workspaceUniqueName });
return applications;
};
};
/**
* Creates a function to fetch a specific application.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that fetches a single application by unique name
* @throws If the application cannot be found or the request fails
*/
const applicationRead = (gqlClient) => {
return async (applicationUniqueName) => {
const { applicationByUniqueName: application } = await gqlClient.request(readApplication, { applicationUniqueName });
return application;
};
};
/**
* Creates a function to create a new application.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that creates a new application with the provided configuration
* @throws If the creation fails or validation errors occur
*/
const applicationCreate = (gqlClient) => {
return async (args) => {
const { workspaceUniqueName,...otherArgs } = args;
const workspace = await workspaceRead(gqlClient)(workspaceUniqueName);
const { createApplication: application } = await gqlClient.request(createApplication, {
...otherArgs,
workspaceId: workspace.id
});
return application;
};
};
/**
* Creates a function to delete an application.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that deletes an application by unique name
* @throws If the application cannot be found or the deletion fails
*/
const applicationDelete = (gqlClient) => {
return async (applicationUniqueName) => {
const { deleteApplicationByUniqueName: application } = await gqlClient.request(deleteApplication, { uniqueName: applicationUniqueName });
return application;
};
};
//#endregion
//#region src/graphql/application-access-tokens.ts
const createApplicationAccessToken = graphql(`
mutation CreateApplicationAccessToken(
$applicationId: ID!,
$blockchainNetworkScope: BlockchainNetworkScopeInputType!,
$blockchainNodeScope: BlockchainNodeScopeInputType!,
$customDeploymentScope: CustomDeploymentScopeInputType!,
$insightsScope: InsightsScopeInputType!,
$integrationScope: IntegrationScopeInputType!,
$loadBalancerScope: LoadBalancerScopeInputType!,
$middlewareScope: MiddlewareScopeInputType!,
$name: String!,
$privateKeyScope: PrivateKeyScopeInputType!,
$smartContractSetScope: SmartContractSetScopeInputType!,
$storageScope: StorageScopeInputType!,
$validityPeriod: AccessTokenValidityPeriod!
) {
createApplicationAccessToken(
applicationId: $applicationId,
blockchainNetworkScope: $blockchainNetworkScope,
blockchainNodeScope: $blockchainNodeScope,
customDeploymentScope: $customDeploymentScope,
insightsScope: $insightsScope,
integrationScope: $integrationScope,
loadBalancerScope: $loadBalancerScope,
middlewareScope: $middlewareScope,
name: $name,
privateKeyScope: $privateKeyScope,
smartContractSetScope: $smartContractSetScope,
storageScope: $storageScope,
validityPeriod: $validityPeriod
) {
token
}
}
`, []);
/**
* Creates a new application.
*
* @param gqlClient - The GraphQL client instance used to execute the mutation.
* @returns A function that accepts the arguments for creating an application and returns a promise resolving to the created application.
*/
const applicationAccessTokenCreate = (gqlClient) => {
return async (args) => {
const { applicationUniqueName,...otherArgs } = args;
const application = await applicationRead(gqlClient)(applicationUniqueName);
const { createApplicationAccessToken: applicationAccessToken } = await gqlClient.request(createApplicationAccessToken, {
...otherArgs,
applicationId: application.id
});
if (!applicationAccessToken.token) {
throw new Error("Failed to create application access token");
}
return applicationAccessToken.token;
};
};
//#endregion
//#region src/defaults/cluster-service-defaults.ts
/**
* Sets the default values for a cluster service.
*
* @param args - The arguments for creating a cluster service.
* @returns The modified arguments with default values set.
*/
function setClusterServiceDefaults(args) {
return {
...args,
size: args.size ?? "SMALL",
type: args.type ?? "SHARED"
};
}
//#endregion
//#region src/defaults/blockchain-network-defaults.ts
/**
* Sets the default values for a blockchain network.
*
* @param args - The arguments for creating a blockchain network.
* @returns The modified arguments with default values set.
*/
function setNetworkDefaults(args) {
const clusterServiceArgs = setClusterServiceDefaults(args);
if (args.consensusAlgorithm === "BESU_QBFT") {
return {
...clusterServiceArgs,
chainId: args.chainId ?? 46040,
contractSizeLimit: args.contractSizeLimit ?? 2147483647,
evmStackSize: args.evmStackSize ?? 2048,
gasLimit: args.gasLimit ?? "9007199254740991",
gasPrice: args.gasPrice ?? 0,
secondsPerBlock: args.secondsPerBlock ?? 2
};
}
return clusterServiceArgs;
}
//#endregion
//#region src/graphql/blockchain-network.ts
/**
* Fragment containing core blockchain network fields.
*/
const BlockchainNetworkFragment = graphql(`
fragment BlockchainNetwork on BlockchainNetwork {
__typename
id
uniqueName
name
status
healthStatus
provider
region
... on BesuQBFTBlockchainNetwork {
chainId
}
... on BesuIbftv2BlockchainNetwork {
chainId
}
... on PolygonEdgePoABlockchainNetwork {
chainId
}
... on QuorumQBFTBlockchainNetwork {
chainId
}
... on GethCliqueBlockchainNetwork {
chainId
}
... on PublicEvmBlockchainNetwork {
chainId
}
blockchainNodes {
... on BlockchainNode {
id
name
uniqueName
endpoints {
id
label
displayValue
}
}
}
}
`);
/**
* Query to fetch blockchain networks for an application.
*/
const getBlockchainNetworks = graphql(`
query getBlockchainNetworks($applicationUniqueName: String!) {
blockchainNetworksByUniqueName(applicationUniqueName: $applicationUniqueName) {
items {
...BlockchainNetwork
}
}
}
`, [BlockchainNetworkFragment]);
/**
* Query to fetch a specific blockchain network.
*/
const getBlockchainNetwork = graphql(`
query getBlockchainNetwork($uniqueName: String!) {
blockchainNetworkByUniqueName(uniqueName: $uniqueName) {
...BlockchainNetwork
}
}
`, [BlockchainNetworkFragment]);
/**
* Mutation to create a new blockchain network.
*/
const createBlockchainNetwork = graphql(`
mutation createBlockchainNetwork(
$applicationId: ID!
$chainId: Int
$consensusAlgorithm: ConsensusAlgorithm!
$contractSizeLimit: Int
$evmStackSize: Int
$gasLimit: String
$gasPrice: Int
$name: String!
$nodeName: String!
$secondsPerBlock: Int
$provider: String!
$region: String!
$size: ClusterServiceSize
$type: ClusterServiceType
$batchTimeout: Float
$maxMessageCount: Int
$absoluteMaxBytes: Int
$preferredMaxBytes: Int
$endorsementPolicy: FabricEndorsementPolicy
$maxCodeSize: Int
$txnSizeLimit: Int
$besuIbft2Genesis: BesuIbft2GenesisInput
$besuQbftGenesis: BesuQbftGenesisInput
$quorumGenesis: QuorumGenesisInput
$externalNodes: [BlockchainNetworkExternalNodeInput!]
$privateKeyId: ID
) {
createBlockchainNetwork(
applicationId: $applicationId
chainId: $chainId
consensusAlgorithm: $consensusAlgorithm
contractSizeLimit: $contractSizeLimit
evmStackSize: $evmStackSize
gasLimit: $gasLimit
gasPrice: $gasPrice
name: $name
nodeName: $nodeName
secondsPerBlock: $secondsPerBlock
provider: $provider
region: $region
size: $size
type: $type
batchTimeout: $batchTimeout
maxMessageCount: $maxMessageCount
absoluteMaxBytes: $absoluteMaxBytes
preferredMaxBytes: $preferredMaxBytes
endorsementPolicy: $endorsementPolicy
maxCodeSize: $maxCodeSize
txnSizeLimit: $txnSizeLimit
besuIbft2Genesis: $besuIbft2Genesis
besuQbftGenesis: $besuQbftGenesis
quorumGenesis: $quorumGenesis
externalNodes: $externalNodes
keyMaterial: $privateKeyId
) {
...BlockchainNetwork
}
}
`, [BlockchainNetworkFragment]);
/**
* Mutation to delete a blockchain network.
*/
const deleteBlockchainNetwork = graphql(`
mutation deleteBlockchainNetwork($uniqueName: String!) {
deleteBlockchainNetworkByUniqueName(uniqueName: $uniqueName) {
...BlockchainNetwork
}
}
`, [BlockchainNetworkFragment]);
/**
* Mutation to restart a blockchain network.
*/
const restartBlockchainNetwork = graphql(`
mutation RestartBlockchainNetwork($uniqueName: String!) {
restartBlockchainNetworkByUniqueName(uniqueName: $uniqueName) {
...BlockchainNetwork
}
}
`, [BlockchainNetworkFragment]);
/**
* Mutation to pause a blockchain network.
*/
const pauseBlockchainNetwork = graphql(`
mutation PauseBlockchainNetwork($uniqueName: String!) {
pauseBlockchainNetworkByUniqueName(uniqueName: $uniqueName) {
...BlockchainNetwork
}
}
`, [BlockchainNetworkFragment]);
/**
* Mutation to resume a blockchain network.
*/
const resumeBlockchainNetwork = graphql(`
mutation ResumeBlockchainNetwork($uniqueName: String!) {
resumeBlockchainNetworkByUniqueName(uniqueName: $uniqueName) {
...BlockchainNetwork
}
}
`, [BlockchainNetworkFragment]);
/**
* Creates a function to list blockchain networks for a given application.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that fetches networks for an application
* @throws If the application cannot be found or the request fails
*/
const blockchainNetworkList = (gqlClient) => {
return async (applicationUniqueName) => {
const { blockchainNetworksByUniqueName: { items } } = await gqlClient.request(getBlockchainNetworks, { applicationUniqueName });
return items;
};
};
/**
* Creates a function to fetch a specific blockchain network.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that fetches a single network by unique name
* @throws If the network cannot be found or the request fails
*/
const blockchainNetworkRead = (gqlClient) => {
return async (blockchainNetworkUniqueName) => {
const { blockchainNetworkByUniqueName } = await gqlClient.request(getBlockchainNetwork, { uniqueName: blockchainNetworkUniqueName });
return blockchainNetworkByUniqueName;
};
};
/**
* Creates a function to create a new blockchain network.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that creates a new network with the provided configuration
* @throws If the creation fails or validation errors occur
*/
const blockchainNetworkCreate = (gqlClient) => {
return async (args) => {
const { applicationUniqueName,...otherArgs } = args;
const application = await applicationRead(gqlClient)(applicationUniqueName);
const blockchainNetworkArgs = setNetworkDefaults(otherArgs);
const { createBlockchainNetwork: blockchainNetwork } = await gqlClient.request(createBlockchainNetwork, {
...blockchainNetworkArgs,
applicationId: application.id
});
return blockchainNetwork;
};
};
/**
* Creates a function to delete a blockchain network.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that deletes a network by unique name
* @throws If the network cannot be found or the deletion fails
*/
const blockchainNetworkDelete = (gqlClient) => {
return async (blockchainNetworkUniqueName) => {
const { deleteBlockchainNetworkByUniqueName: blockchainNetwork } = await gqlClient.request(deleteBlockchainNetwork, { uniqueName: blockchainNetworkUniqueName });
return blockchainNetwork;
};
};
/**
* Creates a function to restart a blockchain network.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that restarts a network by unique name
* @throws If the network cannot be found or the restart fails
*/
const blockchainNetworkRestart = (gqlClient) => async (blockchainNetworkUniqueName) => {
const { restartBlockchainNetworkByUniqueName: blockchainNetwork } = await gqlClient.request(restartBlockchainNetwork, { uniqueName: blockchainNetworkUniqueName });
return blockchainNetwork;
};
/**
* Creates a function to pause a blockchain network.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that pauses a network by unique name
* @throws If the network cannot be found or the pause fails
*/
const blockchainNetworkPause = (gqlClient) => async (blockchainNetworkUniqueName) => {
const { pauseBlockchainNetworkByUniqueName: blockchainNetwork } = await gqlClient.request(pauseBlockchainNetwork, { uniqueName: blockchainNetworkUniqueName });
return blockchainNetwork;
};
/**
* Creates a function to resume a blockchain network.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that resumes a network by unique name
* @throws If the network cannot be found or the resume fails
*/
const blockchainNetworkResume = (gqlClient) => async (blockchainNetworkUniqueName) => {
const { resumeBlockchainNetworkByUniqueName: blockchainNetwork } = await gqlClient.request(resumeBlockchainNetwork, { uniqueName: blockchainNetworkUniqueName });
return blockchainNetwork;
};
//#endregion
//#region src/graphql/blockchain-node.ts
/**
* Fragment containing core blockchain node fields.
*/
const BlockchainNodeFragment = graphql(`
fragment BlockchainNode on BlockchainNode {
__typename
id
uniqueName
name
status
healthStatus
provider
region
isEvm
endpoints {
id
label
displayValue
}
credentials {
id
label
displayValue
}
blockchainNetwork {
... on AbstractClusterService {
id
name
uniqueName
... on BesuQBFTBlockchainNetwork {
chainId
}
... on BesuIbftv2BlockchainNetwork {
chainId
}
... on PolygonEdgePoABlockchainNetwork {
chainId
}
... on QuorumQBFTBlockchainNetwork {
chainId
}
... on GethCliqueBlockchainNetwork {
chainId
}
... on PublicEvmBlockchainNetwork {
chainId
}
}
}
privateKeys {
... on PrivateKey {
id
name
privateKeyType
address
}
}
}
`);
/**
* Query to fetch blockchain nodes for an application.
*/
const getBlockchainNodes = graphql(`
query getBlockchainNodes($applicationUniqueName: String!) {
blockchainNodesByUniqueName(applicationUniqueName: $applicationUniqueName) {
items {
...BlockchainNode
}
}
}
`, [BlockchainNodeFragment]);
/**
* Query to fetch a specific blockchain node.
*/
const getBlockchainNode = graphql(`
query getBlockchainNode($uniqueName: String!) {
blockchainNodeByUniqueName(uniqueName: $uniqueName) {
...BlockchainNode
}
}
`, [BlockchainNodeFragment]);
/**
* Mutation to create a blockchain node.
*/
const createBlockchainNode = graphql(`
mutation createBlockchainNode(
$applicationId: ID!
$blockchainNetworkId: ID!
$name: String!
$provider: String!
$region: String!
$size: ClusterServiceSize
$type: ClusterServiceType
$nodeType: NodeType
$keyMaterial: ID
) {
createBlockchainNode(
applicationId: $applicationId
blockchainNetworkId: $blockchainNetworkId
name: $name
provider: $provider
region: $region
size: $size
type: $type
nodeType: $nodeType
keyMaterial: $keyMaterial
) {
...BlockchainNode
}
}
`, [BlockchainNodeFragment]);
/**
* Mutation to restart a blockchain node.
*/
const restartBlockchainNode = graphql(`
mutation RestartBlockchainNode($uniqueName: String!) {
restartBlockchainNodeByUniqueName(uniqueName: $uniqueName) {
...BlockchainNode
}
}
`, [BlockchainNodeFragment]);
/**
* Mutation to pause a blockchain node.
*/
const pauseBlockchainNode = graphql(`
mutation PauseBlockchainNode($uniqueName: String!) {
pauseBlockchainNodeByUniqueName(uniqueName: $uniqueName) {
...BlockchainNode
}
}
`, [BlockchainNodeFragment]);
/**
* Mutation to resume a blockchain node.
*/
const resumeBlockchainNode = graphql(`
mutation ResumeBlockchainNode($uniqueName: String!) {
resumeBlockchainNodeByUniqueName(uniqueName: $uniqueName) {
...BlockchainNode
}
}
`, [BlockchainNodeFragment]);
/**
* Creates a function to list blockchain nodes for an application.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that fetches blockchain nodes for an application
* @throws If the application cannot be found or the request fails
*/
const blockchainNodeList = (gqlClient) => {
return async (applicationUniqueName) => {
const { blockchainNodesByUniqueName: { items } } = await gqlClient.request(getBlockchainNodes, { applicationUniqueName });
return items;
};
};
/**
* Creates a function to fetch a specific blockchain node.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that fetches a single blockchain node by unique name
* @throws If the blockchain node cannot be found or the request fails
*/
const blockchainNodeRead = (gqlClient) => {
return async (blockchainNodeUniqueName) => {
const { blockchainNodeByUniqueName } = await gqlClient.request(getBlockchainNode, { uniqueName: blockchainNodeUniqueName });
return blockchainNodeByUniqueName;
};
};
/**
* Creates a function to create a new blockchain node.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that creates a new blockchain node with the provided configuration
* @throws If the creation fails or validation errors occur
*/
const blockchainNodeCreate = (gqlClient) => {
return async (args) => {
const { applicationUniqueName, blockchainNetworkUniqueName,...otherArgs } = args;
const [application, blockchainNetwork] = await Promise.all([applicationRead(gqlClient)(applicationUniqueName), blockchainNetworkRead(gqlClient)(blockchainNetworkUniqueName)]);
const { createBlockchainNode: blockchainNode } = await gqlClient.request(createBlockchainNode, {
...otherArgs,
applicationId: application.id,
blockchainNetworkId: blockchainNetwork.id
});
return blockchainNode;
};
};
/**
* Creates a function to restart a blockchain node.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that restarts a blockchain node by unique name
* @throws If the blockchain node cannot be found or the restart fails
*/
const blockchainNodeRestart = (gqlClient) => async (blockchainNodeUniqueName) => {
const { restartBlockchainNodeByUniqueName: blockchainNode } = await gqlClient.request(restartBlockchainNode, { uniqueName: blockchainNodeUniqueName });
return blockchainNode;
};
/**
* Creates a function to pause a blockchain node.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that pauses a blockchain node by unique name
* @throws If the blockchain node cannot be found or the pause fails
*/
const blockchainNodePause = (gqlClient) => async (blockchainNodeUniqueName) => {
const { pauseBlockchainNodeByUniqueName: blockchainNode } = await gqlClient.request(pauseBlockchainNode, { uniqueName: blockchainNodeUniqueName });
return blockchainNode;
};
/**
* Creates a function to resume a blockchain node.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that resumes a blockchain node by unique name
* @throws If the blockchain node cannot be found or the resume fails
*/
const blockchainNodeResume = (gqlClient) => async (blockchainNodeUniqueName) => {
const { resumeBlockchainNodeByUniqueName: blockchainNode } = await gqlClient.request(resumeBlockchainNode, { uniqueName: blockchainNodeUniqueName });
return blockchainNode;
};
//#endregion
//#region src/graphql/custom-deployment.ts
/**
* Fragment containing core custom deployment fields.
*/
const CustomDeploymentFragment = graphql(`
fragment CustomDeployment on CustomDeployment {
id
uniqueName
name
status
healthStatus
provider
region
endpoints {
id
label
displayValue
}
credentials {
id
label
displayValue
}
}
`);
/**
* Query to fetch custom deployments for an application.
*/
const getCustomDeployments = graphql(`
query getCustomDeployments($applicationUniqueName: String!) {
customDeploymentsByUniqueName(applicationUniqueName: $applicationUniqueName) {
items {
...CustomDeployment
}
}
}
`, [CustomDeploymentFragment]);
/**
* Query to fetch a specific custom deployment.
*/
const getCustomDeployment = graphql(`
query getCustomDeployment($uniqueName: String!) {
customDeploymentByUniqueName(uniqueName: $uniqueName) {
...CustomDeployment
}
}
`, [CustomDeploymentFragment]);
/**
* Mutation to edit a custom deployment.
*/
const editCustomDeployment = graphql(`
mutation EditCustomDeployment($uniqueName: String!, $imageTag: String) {
editCustomDeploymentByUniqueName(uniqueName: $uniqueName, imageTag: $imageTag) {
...CustomDeployment
}
}
`, [CustomDeploymentFragment]);
/**
* Mutation to create a custom deployment.
*/
const createCustomDeployment = graphql(`
mutation CreateCustomDeployment(
$applicationId: ID!
$name: String!
$imageTag: String!
$imageName: String!
$imageRepository: String!
$environmentVariables: JSON
$port: Int!
$provider: String!
$region: String!
$size: ClusterServiceSize
$type: ClusterServiceType
) {
createCustomDeployment(
applicationId: $applicationId
name: $name
imageTag: $imageTag
imageName: $imageName
imageRepository: $imageRepository
port: $port
environmentVariables: $environmentVariables
provider: $provider
region: $region
size: $size
type: $type
) {
...CustomDeployment
}
}
`, [CustomDeploymentFragment]);
/**
* Mutation to restart a custom deployment.
*/
const restartCustomDeployment = graphql(`
mutation RestartCustomDeployment($uniqueName: String!) {
restartCustomDeploymentByUniqueName(uniqueName: $uniqueName) {
...CustomDeployment
}
}
`, [CustomDeploymentFragment]);
/**
* Mutation to pause a custom deployment.
*/
const pauseCustomDeployment = graphql(`
mutation PauseCustomDeployment($uniqueName: String!) {
pauseCustomDeploymentByUniqueName(uniqueName: $uniqueName) {
...CustomDeployment
}
}
`, [CustomDeploymentFragment]);
/**
* Mutation to resume a custom deployment.
*/
const resumeCustomDeployment = graphql(`
mutation ResumeCustomDeployment($uniqueName: String!) {
resumeCustomDeploymentByUniqueName(uniqueName: $uniqueName) {
...CustomDeployment
}
}
`, [CustomDeploymentFragment]);
/**
* Creates a function to list custom deployments for an application.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that fetches custom deployments for an application
* @throws If the application cannot be found or the request fails
*/
const customdeploymentList = (gqlClient) => {
return async (applicationUniqueName) => {
const { customDeploymentsByUniqueName: { items } } = await gqlClient.request(getCustomDeployments, { applicationUniqueName });
return items;
};
};
/**
* Creates a function to fetch a specific custom deployment.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that fetches a single custom deployment by unique name
* @throws If the custom deployment cannot be found or the request fails
*/
const customdeploymentRead = (gqlClient) => {
return async (customDeploymentUniqueName) => {
const { customDeploymentByUniqueName: customDeployment } = await gqlClient.request(getCustomDeployment, { uniqueName: customDeploymentUniqueName });
return customDeployment;
};
};
/**
* Creates a function to update a custom deployment.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that updates a custom deployment with a new image tag
* @throws If the custom deployment cannot be found or the update fails
*/
const customdeploymentUpdate = (gqlClient) => {
return async (customDeploymentUniqueName, imageTag) => {
const { editCustomDeploymentByUniqueName: cd } = await gqlClient.request(editCustomDeployment, {
uniqueName: customDeploymentUniqueName,
imageTag
});
return cd;
};
};
/**
* Creates a function to create a new custom deployment.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that creates a new custom deployment with the provided configuration
* @throws If the creation fails or validation errors occur
*/
const customdeploymentCreate = (gqlClient) => {
return async (args) => {
const { applicationUniqueName,...otherArgs } = args;
const application = await applicationRead(gqlClient)(applicationUniqueName);
const { createCustomDeployment: customDeployment } = await gqlClient.request(createCustomDeployment, {
...otherArgs,
applicationId: application.id
});
return customDeployment;
};
};
/**
* Creates a function to restart a custom deployment.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that restarts a custom deployment by unique name
* @throws If the custom deployment cannot be found or the restart fails
*/
const customDeploymentRestart = (gqlClient) => async (customDeploymentUniqueName) => {
const { restartCustomDeploymentByUniqueName: customDeployment } = await gqlClient.request(restartCustomDeployment, { uniqueName: customDeploymentUniqueName });
return customDeployment;
};
/**
* Creates a function to pause a custom deployment.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that pauses a custom deployment by unique name
* @throws If the custom deployment cannot be found or the pause fails
*/
const customDeploymentPause = (gqlClient) => async (customDeploymentUniqueName) => {
const { pauseCustomDeploymentByUniqueName: customDeployment } = await gqlClient.request(pauseCustomDeployment, { uniqueName: customDeploymentUniqueName });
return customDeployment;
};
/**
* Creates a function to resume a custom deployment.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that resumes a custom deployment by unique name
* @throws If the custom deployment cannot be found or the resume fails
*/
const customDeploymentResume = (gqlClient) => async (customDeploymentUniqueName) => {
const { resumeCustomDeploymentByUniqueName: customDeployment } = await gqlClient.request(resumeCustomDeployment, { uniqueName: customDeploymentUniqueName });
return customDeployment;
};
//#endregion
//#region src/graphql/foundry.ts
/**
* Query to fetch Foundry environment configuration for a blockchain node.
*/
const getFoundryEnvConfig = graphql(`
query GetFoundryEnvConfig($blockchainNodeUniqueName: String!) {
foundryEnvConfigByUniqueName(blockchainNodeUniqueName: $blockchainNodeUniqueName)
}
`);
/**
* Creates a function to fetch Foundry environment configuration.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that fetches Foundry environment configuration for a blockchain node
* @throws If the blockchain node cannot be found or the request fails
*/
const getEnv = (gqlClient) => {
return async (blockchainNodeUniqueName) => {
const { foundryEnvConfigByUniqueName } = await gqlClient.request(getFoundryEnvConfig, { blockchainNodeUniqueName });
return foundryEnvConfigByUniqueName;
};
};
//#endregion
//#region src/graphql/load-balancer.ts
/**
* GraphQL fragment containing core load balancer fields.
*/
const LoadBalancerFragment = graphql(`
fragment LoadBalancer on LoadBalancer {
__typename
id
uniqueName
name
status
healthStatus
provider
region
endpoints {
id
label
displayValue
}
}
`);
/**
* Query to fetch a specific load balancer.
*/
const getLoadBalancer = graphql(`
query GetLoadBalancer($uniqueName: String!) {
loadBalancerByUniqueName(uniqueName: $uniqueName) {
...LoadBalancer
}
}
`, [LoadBalancerFragment]);
/**
* Query to fetch all load balancers for an application.
*/
const getLoadBalancers = graphql(`
query getLoadBalancers($applicationUniqueName: String!) {
loadBalancersByUniqueName(applicationUniqueName: $applicationUniqueName) {
items {
...LoadBalancer
}
}
}
`, [LoadBalancerFragment]);
/**
* Mutation to create a load balancer.
*/
const createLoadBalancer = graphql(`
mutation createLoadBalancer(
$applicationId: ID!
$blockchainNetworkId: ID!
$name: String!
$provider: String!
$region: String!
$size: ClusterServiceSize
$type: ClusterServiceType
$connectedNodes: [ID!]!
) {
createLoadBalancer(
applicationId: $applicationId
blockchainNetworkId: $blockchainNetworkId
name: $name
provider: $provider
region: $region
size: $size
type: $type
connectedNodes: $connectedNodes
) {
...LoadBalancer
}
}
`, [LoadBalancerFragment]);
/**
* Mutation to restart a load balancer.
*/
const restartLoadBalancer = graphql(`
mutation RestartLoadBalancer($uniqueName: String!) {
restartLoadBalancerByUniqueName(uniqueName: $uniqueName) {
...LoadBalancer
}
}
`, [LoadBalancerFragment]);
/**
* Mutation to pause a load balancer.
*/
const pauseLoadBalancer = graphql(`
mutation PauseLoadBalancer($uniqueName: String!) {
pauseLoadBalancerByUniqueName(uniqueName: $uniqueName) {
...LoadBalancer
}
}
`, [LoadBalancerFragment]);
/**
* Mutation to resume a load balancer.
*/
const resumeLoadBalancer = graphql(`
mutation ResumeLoadBalancer($uniqueName: String!) {
resumeLoadBalancerByUniqueName(uniqueName: $uniqueName) {
...LoadBalancer
}
}
`, [LoadBalancerFragment]);
/**
* Creates a function to fetch a specific load balancer.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that fetches a single load balancer by unique name
* @throws If the load balancer cannot be found or the request fails
*/
const loadBalancerRead = (gqlClient) => {
return async (loadBalancerUniqueName) => {
const { loadBalancerByUniqueName: loadBalancer } = await gqlClient.request(getLoadBalancer, { uniqueName: loadBalancerUniqueName });
return loadBalancer;
};
};
/**
* Creates a function to list load balancers for an application.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that fetches load balancers for an application
* @throws If the application cannot be found or the request fails
*/
const loadBalancerList = (gqlClient) => {
return async (applicationUniqueName) => {
const { loadBalancersByUniqueName: { items } } = await gqlClient.request(getLoadBalancers, { applicationUniqueName });
return items;
};
};
/**
* Creates a function to create a load balancer.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that creates a load balancer
* @throws If the load balancer cannot be created or the request fails
*/
const loadBalancerCreate = (gqlClient) => {
return async (args) => {
const { applicationUniqueName, blockchainNetworkUniqueName, connectedNodesUniqueNames,...otherArgs } = args;
const [application, blockchainNetwork, connectedNodes] = await Promise.all([
applicationRead(gqlClient)(applicationUniqueName),
blockchainNetworkRead(gqlClient)(blockchainNetworkUniqueName),
Promise.all(connectedNodesUniqueNames.map((uniqueName) => blockchainNodeRead(gqlClient)(uniqueName)))
]);
const { createLoadBalancer: loadBalancer } = await gqlClient.request(createLoadBalancer, {
...otherArgs,
applicationId: application.id,
blockchainNetworkId: blockchainNetwork.id,
connectedNodes: connectedNodes.map((node) => node.id)
});
return loadBalancer;
};
};
/**
* Creates a function to restart a load balancer.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that restarts a load balancer
* @throws If the load balancer cannot be restarted or the request fails
*/
const loadBalancerRestart = (gqlClient) => async (loadBalancerUniqueName) => {
const { restartLoadBalancerByUniqueName: loadBalancer } = await gqlClient.request(restartLoadBalancer, { uniqueName: loadBalancerUniqueName });
return loadBalancer;
};
/**
* Creates a function to pause a load balancer.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that pauses a load balancer
* @throws If the load balancer cannot be paused or the request fails
*/
const loadBalancerPause = (gqlClient) => async (loadBalancerUniqueName) => {
const { pauseLoadBalancerByUniqueName: loadBalancer } = await gqlClient.request(pauseLoadBalancer, { uniqueName: loadBalancerUniqueName });
return loadBalancer;
};
/**
* Creates a function to resume a load balancer.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that resumes a load balancer
* @throws If the load balancer cannot be resumed or the request fails
*/
const loadBalancerResume = (gqlClient) => async (loadBalancerUniqueName) => {
const { resumeLoadBalancerByUniqueName: loadBalancer } = await gqlClient.request(resumeLoadBalancer, { uniqueName: loadBalancerUniqueName });
return loadBalancer;
};
//#endregion
//#region src/graphql/insights.ts
/**
* GraphQL fragment containing core insights fields.
*/
const InsightsFragment = graphql(`
fragment Insights on Insights {
__typename
id
uniqueName
name
status
healthStatus
provider
region
insightsCategory
endpoints {
id
label
displayValue
}
credentials {
id
label
displayValue
}
}
`);
/**
* Query to fetch insights for an application.
*/
const getInsights = graphql(`
query GetInsights($applicationUniqueName: String!) {
insightsListByUniqueName(applicationUniqueName: $applicationUniqueName) {
items {
...Insights
}
}
}
`, [InsightsFragment]);
/**
* Query to fetch a specific insight.
*/
const getInsight = graphql(`
query GetInsight($uniqueName: String!) {
insightsByUniqueName(uniqueName: $uniqueName) {
...Insights
}
}
`, [InsightsFragment]);
/**
* Mutation to create insights.
*/
const createInsights = graphql(`
mutation CreateInsights(
$applicationId: ID!
$name: String!
$insightsCategory: InsightsCategory!
$provider: String!
$region: String!
$size: ClusterServiceSize
$type: ClusterServiceType
$blockchainNode: ID
$loadBalancer: ID
) {
createInsights(
applicationId: $applicationId
name: $name
insightsCategory: $insightsCategory
provider: $provider
region: $region
size: $size
type: $type
blockchainNode: $blockchainNode
loadBalancer: $loadBalancer
) {
...Insights
}
}
`, [InsightsFragment]);
/**
* Mutation to restart insights.
*/
const restartInsights = graphql(`
mutation RestartInsights($uniqueName: String!) {
restartInsightsByUniqueName(uniqueName: $uniqueName) {
...Insights
}
}
`, [InsightsFragment]);
/**
* Mutation to pause insights.
*/
const pauseInsights = graphql(`
mutation PauseInsights($uniqueName: String!) {
pauseInsightsByUniqueName(uniqueName: $uniqueName) {
...Insights
}
}
`, [InsightsFragment]);
/**
* Mutation to resume insights.
*/
const resumeInsights = graphql(`
mutation ResumeInsights($uniqueName: String!) {
resumeInsightsByUniqueName(uniqueName: $uniqueName) {
...Insights
}
}
`, [InsightsFragment]);
/**
* Creates a function to list insights for an application.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that fetches insights for an application
* @throws If the application cannot be found or the request fails
*/
const insightsList = (gqlClient) => {
return async (applicationUniqueName) => {
const { insightsListByUniqueName: { items } } = await gqlClient.request(getInsights, { applicationUniqueName });
return items;
};
};
/**
* Creates a function to fetch a specific insight.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that fetches a single insight by unique name
* @throws If the insight cannot be found or the request fails
*/
const insightsRead = (gqlClient) => {
return async (insightsUniqueName) => {
const { insightsByUniqueName: insights } = await gqlClient.request(getInsight, { uniqueName: insightsUniqueName });
return insights;
};
};
/**
* Creates a function to create new insights.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that creates new insights with the provided configuration
* @throws If the creation fails or validation errors occur
*/
const insightsCreate = (gqlClient) => {
return async (args) => {
const { applicationUniqueName, blockchainNodeUniqueName, loadBalancerUniqueName,...otherArgs } = args;
const [application, blockchainNode, loadBalancer] = await Promise.all([
applicationRead(gqlClient)(applicationUniqueName),
blockchainNodeUniqueName ? blockchainNodeRead(gqlClient)(blockchainNodeUniqueName) : Promise.resolve(undefined),
loadBalancerUniqueName ? loadBalancerRead(gqlClient)(loadBalancerUniqueName) : Promise.resolve(undefined)
]);
const { createInsights: insights } = await gqlClient.request(createInsights, {
...otherArgs,
applicationId: application.id,
blockchainNode: blockchainNode?.id,
loadBalancer: loadBalancer?.id
});
return insights;
};
};
/**
* Creates a function to restart insights.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that restarts insights by unique name
* @throws If the insights cannot be found or the restart fails
*/
const insightsRestart = (gqlClient) => async (insightsUniqueName) => {
const { restartInsightsByUniqueName: insights } = await gqlClient.request(restartInsights, { uniqueName: insightsUniqueName });
return insights;
};
/**
* Creates a function to pause insights.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that pauses insights by unique name
* @throws If the insights cannot be found or the pause fails
*/
const insightsPause = (gqlClient) => async (insightsUniqueName) => {
const { pauseInsightsByUniqueName: insights } = await gqlClient.request(pauseInsights, { uniqueName: insightsUniqueName });
return insights;
};
/**
* Creates a function to resume insights.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that resumes insights by unique name
* @throws If the insights cannot be found or the resume fails
*/
const insightsResume = (gqlClient) => async (insightsUniqueName) => {
const { resumeInsightsByUniqueName: insights } = await gqlClient.request(resumeInsights, { uniqueName: insightsUniqueName });
return insights;
};
//#endregion
//#region src/graphql/integration-tool.ts
/**
* GraphQL fragment containing core integration fields.
*/
const IntegrationFragment = graphql(`
fragment Integration on Integration {
__typename
id
uniqueName
name
status
healthStatus
provider
region
integrationType
endpoints {
id
label
displayValue
}
credentials {
id
label
displayValue
}
}
`);
/**
* Query to fetch integrations for an application.
*/
const getIntegrations = graphql(`
query GetIntegrations($applicationUniqueName: String!) {
integrationsByUniqueName(applicationUniqueName: $applicationUniqueName) {
items {
...Integration
}
}
}
`, [IntegrationFragment]);
/**
* Query to fetch a specific integration.
*/
const getIntegration = graphql(`
query GetIntegration($uniqueName: String!) {
integrationByUniqueName(uniqueName: $uniqueName) {
...Integration
}
}
`, [IntegrationFragment]);
/**
* Mutation to create a new integration.
*/
const createIntegration = graphql(`
mutation CreateIntegration(
$applicationId: ID!
$name: String!
$integrationType: IntegrationType!
$provider: String!
$region: String!
$size: ClusterServiceSize
$type: ClusterServiceType
) {
createIntegration(
applicationId: $applicationId
name: $name
integrationType: $integrationType
provider: $provider
region: $region
size: $size
type: $type
) {
...Integration
}
}
`, [IntegrationFragment]);
/**
* Mutation to restart an integration.
*/
const restartIntegrationTool = graphql(`
mutation RestartIntegrationTool($uniqueName: String!) {
restartIntegrationByUniqueName(uniqueName: $uniqueName) {
...Integration
}
}
`, [IntegrationFragment]);
/**
* Mutation to pause an integration.
*/
const pauseIntegrationTool = graphql(`
mutation PauseIntegrationTool($uniqueName: String!) {
pauseIntegrationByUniqueName(uniqueName: $uniqueName) {
...Integration
}
}
`, [IntegrationFragment]);
/**
* Mutation to resume an integration.
*/
const resumeIntegrationTool = graphql(`
mutation ResumeIntegrationTool($uniqueName: String!) {
resumeIntegrationByUniqueName(uniqueName: $uniqueName) {
...Integration
}
}
`, [IntegrationFragment]);
/**
* Creates a function to list integration tools for an application.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that fetches integration tools for an application
* @throws If the application cannot be found or the request fails
*/
const integrationToolList = (gqlClient) => {
return async (applicationUniqueName) => {
const { integrationsByUniqueName: { items } } = await gqlClient.request(getIntegrations, { applicationUniqueName });
return items;
};
};
/**
* Creates a function to fetch a specific integration tool.
*
* @param gqlClient - The GraphQL client instance
* @returns Function that fetches a single integration tool by unique name
* @throws If the integration tool cannot be found or the request fails
*/
const integrationToolRead = (gqlClient) => {
return async (integrationUniqueName) => {
const { integrationByUniqueN