Skip to content

feat: Add createNetwork function for easy API key usage #1800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

janniks
Copy link
Collaborator

@janniks janniks commented Jun 10, 2025

This PR was published to npm with the version 7.1.2-pr.0+d24d3278
e.g. npm install @stacks/[email protected]+d24d3278 --save-exact

Background

It's a bit cumbersome to add API key middleware to network objects currently. This should be super easy.

Feature

createNetwork Creates a customized Stacks network.

This function allows you to create a network based on a predefined network
(mainnet, testnet, devnet, mocknet) or a custom network object. You can also customize
the network with an API key or other client options.

// Create a basic network from a network name
const network = createNetwork('mainnet');
const network = createNetwork(STACKS_MAINNET);
// Create a network with an API key
const network = createNetwork('testnet', 'my-api-key');
const network = createNetwork(STACKS_TESTNET, 'my-api-key');
// Create a network with options object
const network = createNetwork({
  network: 'mainnet',
  apiKey: 'my-api-key',
});
// Create a network with options object with custom API key options
const network = createNetwork({
  network: 'mainnet',
  apiKey: 'my-api-key',
  host: /\.example\.com$/, // default is /(.*)api(.*)(\.stacks\.co|\.hiro\.so)$/i
  httpHeader: 'x-custom-api-key', // default is 'x-api-key'
});
// Create a network with custom client options
const network = createNetwork({
  network: STACKS_TESTNET,
  client: {
    baseUrl: 'https://custom-api.example.com',
    fetch: customFetchFunction
  }
});

Copy link

vercel bot commented Jun 10, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
stacksjs-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 10, 2025 9:22am

@janniks janniks requested review from obycode and Copilot June 10, 2025 09:22
@janniks janniks self-assigned this Jun 10, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces the createNetwork function to streamline the creation of customized Stacks network objects, including built‐in support for API keys and client options.

  • Added createNetwork overloads in network.ts, integrating API key middleware via createApiKeyMiddleware
  • Extended tests in network.test.ts to cover various usage scenarios including default, API key, and custom client configurations

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
packages/network/tests/network.test.ts Added comprehensive tests to validate createNetwork behavior with network names, network objects, API key usage, and custom client support
packages/network/src/network.ts Implemented createNetwork with overloads and integrated API key middleware for API key configuration

Copy link

codecov bot commented Jun 10, 2025

Codecov Report

Attention: Patch coverage is 90.90909% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
packages/network/src/network.ts 90.90% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@janniks janniks changed the title feat: Add createNetwork function for easy API key usage feat: Add createNetwork function for easy API key usage Jun 10, 2025
Copy link
Member

@obycode obycode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants