Skip to content

Commit

Permalink
fix: look for URL in channel before falling back to default (#440)
Browse files Browse the repository at this point in the history
This fixes #344 and
makes it possible to connect to staging services with w3ui.

This PR obsoletes #404
per @alanshaw's suggestion in
#404 (comment)
based on reasoning in
web3-storage/ucanto#214 (comment)
which points out that some sort of conditional or casting is probably
unavoidable here.

My solution casts the channel to have an optional `url` field and looks
in that field for a URL before falling back to the default `HOST`.

Have tested this locally with w3ui and verified it makes it possible to
connect to staging services from w3ui - huzzah!
  • Loading branch information
travis committed Feb 21, 2023
1 parent 0b73960 commit 6fa2cba
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/access-client/src/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export { AgentData }
const HOST = 'https://access.web3.storage'
const PRINCIPAL = DID.parse('did:web:web3.storage')

/**
* @typedef {import('./types').Service} Service
*/

/**
* Creates a Ucanto connection for the w3access API
*
Expand All @@ -37,7 +41,6 @@ const PRINCIPAL = DID.parse('did:web:web3.storage')
* import { connection } from '@web3-storage/access/agent'
* ```
*
* @template {import('./types').Service} Service
* @template {Ucanto.DID} T - DID method
* @param {object} [options]
* @param {Ucanto.Principal<T>} [options.principal] - w3access API Principal
Expand Down Expand Up @@ -79,7 +82,9 @@ export class Agent {
* @param {import('./types').AgentOptions} [options]
*/
constructor(data, options = {}) {
this.url = options.url ?? new URL(HOST)
/** @type { Client.Channel<Service> & { url?: URL } | undefined } */
const channel = options.connection?.channel
this.url = options.url ?? channel?.url ?? new URL(HOST)
this.connection =
options.connection ??
connection({
Expand Down

0 comments on commit 6fa2cba

Please sign in to comment.