Update URL templates to use opts objects

This commit is contained in:
Alex Gleason committed 2026-01-14 11:16:40 -06:00
1 parent 5d075e02ab
commit a8eec32842
23 files changed
+67 -50

No files matched your search

+1 -1
View File
@@ -284,7 +284,7 @@ export function useNewProviderOAuth() {
redirectUri: window.location.origin + '/oauth/newprovider', redirectUri: window.location.origin + '/oauth/newprovider',
usePKCE: false, // Set to true if provider supports PKCE usePKCE: false, // Set to true if provider supports PKCE
getUserInfo: async (accessToken: string, corsProxy: string) => { getUserInfo: async (accessToken: string, corsProxy: string) => {
const userUrl = proxyUrl(corsProxy, 'https://api.provider.com/user'); const userUrl = proxyUrl({ template: corsProxy, url: 'https://api.provider.com/user' });
const userResponse = await fetch(userUrl, { const userResponse = await fetch(userUrl, {
headers: { headers: {
'Authorization': `Bearer ${accessToken}`, 'Authorization': `Bearer ${accessToken}`,
+1 -1
View File
@@ -36,7 +36,7 @@ export function ExternalFavicon({
parsedUrl.hostname = parsedUrl.hostname.replace(/^(ai\.|api\.)/, ''); parsedUrl.hostname = parsedUrl.hostname.replace(/^(ai\.|api\.)/, '');
// Normalize the URL to ensure it has a protocol // Normalize the URL to ensure it has a protocol
return faviconUrl(config.faviconUrl, parsedUrl); return faviconUrl({ template: config.faviconUrl, url: parsedUrl });
} catch { } catch {
return; return;
} }
@@ -63,10 +63,10 @@ export function CloudflareDeployForm({
const workersUrl = `${baseURL}/accounts/${accountId}/workers/scripts`; const workersUrl = `${baseURL}/accounts/${accountId}/workers/scripts`;
const [subdomainResponse, workersResponse] = await Promise.all([ const [subdomainResponse, workersResponse] = await Promise.all([
fetch(corsProxy ? proxyUrl(corsProxy, subdomainUrl) : subdomainUrl, { fetch(corsProxy ? proxyUrl({ template: corsProxy, url: subdomainUrl }) : subdomainUrl, {
headers: { 'Authorization': `Bearer ${apiKey}` }, headers: { 'Authorization': `Bearer ${apiKey}` },
}), }),
fetch(corsProxy ? proxyUrl(corsProxy, workersUrl) : workersUrl, { fetch(corsProxy ? proxyUrl({ template: corsProxy, url: workersUrl }) : workersUrl, {
headers: { 'Authorization': `Bearer ${apiKey}` }, headers: { 'Authorization': `Bearer ${apiKey}` },
}), }),
]); ]);
+1 -1
View File
@@ -56,7 +56,7 @@ export function DenoDeployForm({
try { try {
const url = `${baseURL}/organizations/${organizationId}/projects?limit=100`; const url = `${baseURL}/organizations/${organizationId}/projects?limit=100`;
const targetUrl = corsProxy ? proxyUrl(corsProxy, url) : url; const targetUrl = corsProxy ? proxyUrl({ template: corsProxy, url }) : url;
const response = await fetch(targetUrl, { const response = await fetch(targetUrl, {
headers: { 'Authorization': `Bearer ${apiKey}` }, headers: { 'Authorization': `Bearer ${apiKey}` },
+1 -1
View File
@@ -53,7 +53,7 @@ export function NetlifyDeployForm({
try { try {
const url = `${baseURL}/sites`; const url = `${baseURL}/sites`;
const targetUrl = corsProxy ? proxyUrl(corsProxy, url) : url; const targetUrl = corsProxy ? proxyUrl({ template: corsProxy, url }) : url;
const response = await fetch(targetUrl, { const response = await fetch(targetUrl, {
headers: { headers: {
+1 -1
View File
@@ -154,7 +154,7 @@ export function SyncStep({ projectId, remoteUrl }: SyncStepProps) {
if (url.protocol === 'nostr:') { if (url.protocol === 'nostr:') {
const nostrUri = await NostrURI.parse(url.href); const nostrUri = await NostrURI.parse(url.href);
return ngitWebUrl(config.ngitWebUrl, nostrUri); return ngitWebUrl({ template: config.ngitWebUrl, nostrURI: nostrUri });
} }
return null; return null;
+1 -1
View File
@@ -30,7 +30,7 @@ export function useGitHubOAuth() {
redirectUri: window.location.origin + '/oauth/github', redirectUri: window.location.origin + '/oauth/github',
usePKCE: true, // GitHub supports PKCE usePKCE: true, // GitHub supports PKCE
getUserInfo: async (accessToken: string, corsProxy: string) => { getUserInfo: async (accessToken: string, corsProxy: string) => {
const userUrl = proxyUrl(corsProxy, 'https://api.github.com/user'); const userUrl = proxyUrl({ template: corsProxy, url: 'https://api.github.com/user' });
const userResponse = await fetch(userUrl, { const userResponse = await fetch(userUrl, {
headers: { headers: {
'Authorization': `Bearer ${accessToken}`, 'Authorization': `Bearer ${accessToken}`,
+1 -1
View File
@@ -30,7 +30,7 @@ export function useNetlifyOAuth() {
redirectUri: window.location.origin + '/oauth/netlify', redirectUri: window.location.origin + '/oauth/netlify',
usePKCE: false, // Netlify doesn't support PKCE usePKCE: false, // Netlify doesn't support PKCE
getUserInfo: async (accessToken: string, corsProxy: string) => { getUserInfo: async (accessToken: string, corsProxy: string) => {
const userUrl = proxyUrl(corsProxy, 'https://api.netlify.com/api/v1/user'); const userUrl = proxyUrl({ template: corsProxy, url: 'https://api.netlify.com/api/v1/user' });
const userResponse = await fetch(userUrl, { const userResponse = await fetch(userUrl, {
headers: { headers: {
'Authorization': `Bearer ${accessToken}`, 'Authorization': `Bearer ${accessToken}`,
+1 -1
View File
@@ -169,7 +169,7 @@ export function useOAuth(config: OAuthConfig) {
} }
// Exchange code for access token // Exchange code for access token
const tokenUrl = proxyUrl(corsProxy, config.tokenUrl); const tokenUrl = proxyUrl({ template: corsProxy, url: config.tokenUrl });
const tokenResponse = await fetch(tokenUrl, { const tokenResponse = await fetch(tokenUrl, {
method: 'POST', method: 'POST',
headers: { headers: {
+1 -1
View File
@@ -34,7 +34,7 @@ export function useVercelOAuth() {
redirectUri: window.location.origin + '/oauth/vercel', redirectUri: window.location.origin + '/oauth/vercel',
usePKCE: false, // Vercel doesn't require PKCE usePKCE: false, // Vercel doesn't require PKCE
getUserInfo: async (accessToken: string, corsProxy: string) => { getUserInfo: async (accessToken: string, corsProxy: string) => {
const userUrl = proxyUrl(corsProxy, 'https://api.vercel.com/v2/user'); const userUrl = proxyUrl({ template: corsProxy, url: 'https://api.vercel.com/v2/user' });
const userResponse = await fetch(userUrl, { const userResponse = await fetch(userUrl, {
headers: { headers: {
'Authorization': `Bearer ${accessToken}`, 'Authorization': `Bearer ${accessToken}`,
+1 -1
View File
@@ -47,7 +47,7 @@ export function createAIClient(provider: AIProvider, user?: NUser, corsProxy?: s
// If proxy is enabled and we have a CORS proxy URL, modify the request URL // If proxy is enabled and we have a CORS proxy URL, modify the request URL
if (provider.proxy && corsProxy) { if (provider.proxy && corsProxy) {
request = new Request(proxyUrl(corsProxy, request.url), request); request = new Request(proxyUrl({ template: corsProxy, url: request.url }), request);
} }
return fetch(request); return fetch(request);
+1 -1
View File
@@ -66,7 +66,7 @@ export class CurlCommand implements ShellCommand {
// Proxy the URL through configured CorsProxy // Proxy the URL through configured CorsProxy
const originalUrl = url.toString(); const originalUrl = url.toString();
const proxiedUrl = proxyUrl(this.corsProxy, originalUrl); const proxiedUrl = proxyUrl({ template: this.corsProxy, url: originalUrl });
// Prepare request // Prepare request
const requestInit: RequestInit = { const requestInit: RequestInit = {
+5 -5
View File
@@ -140,7 +140,7 @@ export class CloudflareAdapter implements DeployAdapter {
private async getWorkersSubdomain(): Promise<string> { private async getWorkersSubdomain(): Promise<string> {
const url = `${this.baseURL}/accounts/${this.accountId}/workers/subdomain`; const url = `${this.baseURL}/accounts/${this.accountId}/workers/subdomain`;
const targetUrl = this.corsProxy ? proxyUrl(this.corsProxy, url) : url; const targetUrl = this.corsProxy ? proxyUrl({ template: this.corsProxy, url }) : url;
const response = await fetch(targetUrl, { const response = await fetch(targetUrl, {
method: 'GET', method: 'GET',
@@ -160,7 +160,7 @@ export class CloudflareAdapter implements DeployAdapter {
private async enableWorkersDevSubdomain(scriptName: string): Promise<void> { private async enableWorkersDevSubdomain(scriptName: string): Promise<void> {
const url = `${this.baseURL}/accounts/${this.accountId}/workers/scripts/${scriptName}/subdomain`; const url = `${this.baseURL}/accounts/${this.accountId}/workers/scripts/${scriptName}/subdomain`;
const targetUrl = this.corsProxy ? proxyUrl(this.corsProxy, url) : url; const targetUrl = this.corsProxy ? proxyUrl({ template: this.corsProxy, url }) : url;
const response = await fetch(targetUrl, { const response = await fetch(targetUrl, {
method: 'POST', method: 'POST',
@@ -185,7 +185,7 @@ export class CloudflareAdapter implements DeployAdapter {
manifest: AssetManifest manifest: AssetManifest
): Promise<InitializeAssetsResponse> { ): Promise<InitializeAssetsResponse> {
const url = `${this.baseURL}/accounts/${this.accountId}/workers/scripts/${scriptName}/assets-upload-session`; const url = `${this.baseURL}/accounts/${this.accountId}/workers/scripts/${scriptName}/assets-upload-session`;
const targetUrl = this.corsProxy ? proxyUrl(this.corsProxy, url) : url; const targetUrl = this.corsProxy ? proxyUrl({ template: this.corsProxy, url }) : url;
const response = await fetch(targetUrl, { const response = await fetch(targetUrl, {
method: 'POST', method: 'POST',
@@ -211,7 +211,7 @@ export class CloudflareAdapter implements DeployAdapter {
hashToFile: Map<string, { path: string; info: FileInfo }> hashToFile: Map<string, { path: string; info: FileInfo }>
): Promise<UploadResponse> { ): Promise<UploadResponse> {
const url = `${this.baseURL}/accounts/${this.accountId}/workers/assets/upload?base64=true`; const url = `${this.baseURL}/accounts/${this.accountId}/workers/assets/upload?base64=true`;
const targetUrl = this.corsProxy ? proxyUrl(this.corsProxy, url) : url; const targetUrl = this.corsProxy ? proxyUrl({ template: this.corsProxy, url }) : url;
const formData = new FormData(); const formData = new FormData();
@@ -251,7 +251,7 @@ export class CloudflareAdapter implements DeployAdapter {
private async deployWorker(scriptName: string, assetsJwt: string): Promise<WorkerScript> { private async deployWorker(scriptName: string, assetsJwt: string): Promise<WorkerScript> {
const url = `${this.baseURL}/accounts/${this.accountId}/workers/scripts/${scriptName}`; const url = `${this.baseURL}/accounts/${this.accountId}/workers/scripts/${scriptName}`;
const targetUrl = this.corsProxy ? proxyUrl(this.corsProxy, url) : url; const targetUrl = this.corsProxy ? proxyUrl({ template: this.corsProxy, url }) : url;
// Create a minimal worker script that serves static assets // Create a minimal worker script that serves static assets
// This is an assets-only worker - Cloudflare will automatically serve the uploaded assets // This is an assets-only worker - Cloudflare will automatically serve the uploaded assets
+3 -3
View File
@@ -96,7 +96,7 @@ export class DenoDeployAdapter implements DeployAdapter {
private async findProject(projectName: string): Promise<Project | null> { private async findProject(projectName: string): Promise<Project | null> {
const url = `${this.baseURL}/organizations/${this.organizationId}/projects?q=${encodeURIComponent(projectName)}`; const url = `${this.baseURL}/organizations/${this.organizationId}/projects?q=${encodeURIComponent(projectName)}`;
const targetUrl = this.corsProxy ? proxyUrl(this.corsProxy, url) : url; const targetUrl = this.corsProxy ? proxyUrl({ template: this.corsProxy, url }) : url;
const response = await fetch(targetUrl, { const response = await fetch(targetUrl, {
method: 'GET', method: 'GET',
@@ -116,7 +116,7 @@ export class DenoDeployAdapter implements DeployAdapter {
private async createProject(projectName: string): Promise<Project> { private async createProject(projectName: string): Promise<Project> {
const url = `${this.baseURL}/organizations/${this.organizationId}/projects`; const url = `${this.baseURL}/organizations/${this.organizationId}/projects`;
const targetUrl = this.corsProxy ? proxyUrl(this.corsProxy, url) : url; const targetUrl = this.corsProxy ? proxyUrl({ template: this.corsProxy, url }) : url;
const response = await fetch(targetUrl, { const response = await fetch(targetUrl, {
method: 'POST', method: 'POST',
@@ -143,7 +143,7 @@ export class DenoDeployAdapter implements DeployAdapter {
domain: string domain: string
): Promise<Deployment> { ): Promise<Deployment> {
const url = `${this.baseURL}/projects/${projectId}/deployments`; const url = `${this.baseURL}/projects/${projectId}/deployments`;
const targetUrl = this.corsProxy ? proxyUrl(this.corsProxy, url) : url; const targetUrl = this.corsProxy ? proxyUrl({ template: this.corsProxy, url }) : url;
// Create a minimal server script that serves static files // Create a minimal server script that serves static files
// For static sites, we use a simple file server entry point // For static sites, we use a simple file server entry point
+3 -3
View File
@@ -89,7 +89,7 @@ export class NetlifyAdapter implements DeployAdapter {
private async createSite(name: string): Promise<NetlifySite> { private async createSite(name: string): Promise<NetlifySite> {
const url = `${this.baseURL}/sites`; const url = `${this.baseURL}/sites`;
const targetUrl = this.corsProxy ? proxyUrl(this.corsProxy, url) : url; const targetUrl = this.corsProxy ? proxyUrl({ template: this.corsProxy, url }) : url;
const response = await fetch(targetUrl, { const response = await fetch(targetUrl, {
method: 'POST', method: 'POST',
@@ -113,7 +113,7 @@ export class NetlifyAdapter implements DeployAdapter {
private async getSite(siteId: string): Promise<NetlifySite> { private async getSite(siteId: string): Promise<NetlifySite> {
const url = `${this.baseURL}/sites/${siteId}`; const url = `${this.baseURL}/sites/${siteId}`;
const targetUrl = this.corsProxy ? proxyUrl(this.corsProxy, url) : url; const targetUrl = this.corsProxy ? proxyUrl({ template: this.corsProxy, url }) : url;
const response = await fetch(targetUrl, { const response = await fetch(targetUrl, {
method: 'GET', method: 'GET',
@@ -132,7 +132,7 @@ export class NetlifyAdapter implements DeployAdapter {
private async triggerDeploy(siteId: string, zipBlob: Blob): Promise<NetlifyBuildResponse> { private async triggerDeploy(siteId: string, zipBlob: Blob): Promise<NetlifyBuildResponse> {
const url = `${this.baseURL}/sites/${siteId}/deploys`; const url = `${this.baseURL}/sites/${siteId}/deploys`;
const targetUrl = this.corsProxy ? proxyUrl(this.corsProxy, url) : url; const targetUrl = this.corsProxy ? proxyUrl({ template: this.corsProxy, url }) : url;
const response = await fetch(targetUrl, { const response = await fetch(targetUrl, {
method: 'POST', method: 'POST',
+1 -1
View File
@@ -72,7 +72,7 @@ export class ShakespeareAdapter implements DeployAdapter {
// Apply proxy if configured // Apply proxy if configured
if (this.corsProxy) { if (this.corsProxy) {
request = new Request(proxyUrl(this.corsProxy, request.url), request); request = new Request(proxyUrl({ template: this.corsProxy, url: request.url }), request);
} }
// Deploy // Deploy
+1 -1
View File
@@ -68,7 +68,7 @@ export class VercelAdapter implements DeployAdapter {
} }
// Apply proxy if configured // Apply proxy if configured
const targetUrl = this.corsProxy ? proxyUrl(this.corsProxy, apiUrl) : apiUrl; const targetUrl = this.corsProxy ? proxyUrl({ template: this.corsProxy, url: apiUrl }) : apiUrl;
// Deploy to Vercel // Deploy to Vercel
const response = await fetch(targetUrl, { const response = await fetch(targetUrl, {
+11 -5
View File
@@ -1,15 +1,21 @@
import UriTemplate from 'uri-templates'; import UriTemplate from 'uri-templates';
export interface FaviconUrlOpts {
template: string;
url: string | URL;
}
/** /**
* Generate a favicon URL from a template and input URL * Generate a favicon URL from a template and input URL
* @param template - URL template with placeholders like {hostname}, {origin}, etc. * @param opts - Options object
* @param url - The URL to extract parts from * @param opts.template - URL template with placeholders like {hostname}, {origin}, etc.
* @param opts.url - The URL to extract parts from
* @returns The hydrated favicon URL * @returns The hydrated favicon URL
*/ */
export function faviconUrl(template: string, url: string | URL): string { export function faviconUrl(opts: FaviconUrlOpts): string {
const u = new URL(url); const u = new URL(opts.url);
return UriTemplate(template).fill({ return UriTemplate(opts.template).fill({
href: u.href, href: u.href,
origin: u.origin, origin: u.origin,
protocol: u.protocol, protocol: u.protocol,
+1 -1
View File
@@ -1115,7 +1115,7 @@ class GitHttp implements HttpClient {
const url = new URL(request.url); const url = new URL(request.url);
const target = this.proxy const target = this.proxy
? proxyUrl(this.proxy, url) ? proxyUrl({ template: this.proxy, url })
: url.href; : url.href;
const init: RequestInit = { const init: RequestInit = {
+6 -6
View File
@@ -9,7 +9,7 @@ describe('ngitWebUrl', () => {
const identifier = 'my-repo'; const identifier = 'my-repo';
const nostrURI = new NostrURI({ pubkey, identifier }); const nostrURI = new NostrURI({ pubkey, identifier });
const result = ngitWebUrl(template, nostrURI); const result = ngitWebUrl({ template, nostrURI });
expect(result).toMatch(/^https:\/\/nostrhub\.io\/naddr1/); expect(result).toMatch(/^https:\/\/nostrhub\.io\/naddr1/);
}); });
@@ -20,7 +20,7 @@ describe('ngitWebUrl', () => {
const identifier = 'my-repo'; const identifier = 'my-repo';
const nostrURI = new NostrURI({ pubkey, identifier }); const nostrURI = new NostrURI({ pubkey, identifier });
const result = ngitWebUrl(template, nostrURI); const result = ngitWebUrl({ template, nostrURI });
expect(result).toMatch(/^https:\/\/example\.com\/npub1/); expect(result).toMatch(/^https:\/\/example\.com\/npub1/);
}); });
@@ -31,7 +31,7 @@ describe('ngitWebUrl', () => {
const identifier = 'my-repo'; const identifier = 'my-repo';
const nostrURI = new NostrURI({ pubkey, identifier }); const nostrURI = new NostrURI({ pubkey, identifier });
const result = ngitWebUrl(template, nostrURI); const result = ngitWebUrl({ template, nostrURI });
expect(result).toBe(`https://example.com/${pubkey}`); expect(result).toBe(`https://example.com/${pubkey}`);
}); });
@@ -42,7 +42,7 @@ describe('ngitWebUrl', () => {
const identifier = 'my-repo'; const identifier = 'my-repo';
const nostrURI = new NostrURI({ pubkey, identifier }); const nostrURI = new NostrURI({ pubkey, identifier });
const result = ngitWebUrl(template, nostrURI); const result = ngitWebUrl({ template, nostrURI });
expect(result).toBe('https://example.com/my-repo'); expect(result).toBe('https://example.com/my-repo');
}); });
@@ -53,7 +53,7 @@ describe('ngitWebUrl', () => {
const identifier = 'my-repo'; const identifier = 'my-repo';
const nostrURI = new NostrURI({ pubkey, identifier }); const nostrURI = new NostrURI({ pubkey, identifier });
const result = ngitWebUrl(template, nostrURI); const result = ngitWebUrl({ template, nostrURI });
expect(result).toMatch(/^https:\/\/example\.com\/npub1[^/]+\/my-repo$/); expect(result).toMatch(/^https:\/\/example\.com\/npub1[^/]+\/my-repo$/);
}); });
@@ -65,7 +65,7 @@ describe('ngitWebUrl', () => {
const relay = 'wss://relay.example.com/'; const relay = 'wss://relay.example.com/';
const nostrURI = new NostrURI({ pubkey, identifier, relay }); const nostrURI = new NostrURI({ pubkey, identifier, relay });
const result = ngitWebUrl(template, nostrURI); const result = ngitWebUrl({ template, nostrURI });
// The naddr should be longer when it includes relay information // The naddr should be longer when it includes relay information
expect(result).toMatch(/^https:\/\/nostrhub\.io\/naddr1/); expect(result).toMatch(/^https:\/\/nostrhub\.io\/naddr1/);
+14 -8
View File
@@ -2,20 +2,26 @@ import UriTemplate from 'uri-templates';
import { nip19 } from 'nostr-tools'; import { nip19 } from 'nostr-tools';
import { NostrURI } from './NostrURI'; import { NostrURI } from './NostrURI';
export interface NgitWebUrlOpts {
template: string;
nostrURI: NostrURI;
}
/** /**
* Generate a Nostr Git web URL from a template and Nostr URI * Generate a Nostr Git web URL from a template and Nostr URI
* @param template - URL template with placeholders like {naddr}, {npub}, {pubkey}, {identifier} * @param opts - Options object
* @param nostrURI - The Nostr URI containing pubkey, identifier, and optional relay * @param opts.template - URL template with placeholders like {naddr}, {npub}, {pubkey}, {identifier}
* @param opts.nostrURI - The Nostr URI containing pubkey, identifier, and optional relay
* @returns The hydrated web URL * @returns The hydrated web URL
*/ */
export function ngitWebUrl(template: string, nostrURI: NostrURI): string { export function ngitWebUrl(opts: NgitWebUrlOpts): string {
const naddr = nostrURI.toNaddr(); const naddr = opts.nostrURI.toNaddr();
const npub = nip19.npubEncode(nostrURI.pubkey); const npub = nip19.npubEncode(opts.nostrURI.pubkey);
return UriTemplate(template).fill({ return UriTemplate(opts.template).fill({
naddr, naddr,
npub, npub,
pubkey: nostrURI.pubkey, pubkey: opts.nostrURI.pubkey,
identifier: nostrURI.identifier, identifier: opts.nostrURI.identifier,
}); });
} }
+8 -3
View File
@@ -1,8 +1,13 @@
import UriTemplate from 'uri-templates'; import UriTemplate from 'uri-templates';
export function proxyUrl(template: string, url: string | URL): string { export interface ProxyUrlOpts {
const u = new URL(url); template: string;
return UriTemplate(template).fill({ url: string | URL;
}
export function proxyUrl(opts: ProxyUrlOpts): string {
const u = new URL(opts.url);
return UriTemplate(opts.template).fill({
href: u.href, href: u.href,
origin: u.origin, origin: u.origin,
protocol: u.protocol, protocol: u.protocol,
+1 -1
View File
@@ -137,7 +137,7 @@ export class GenerateImageTool implements Tool<GenerateImageParams> {
} }
} else if (firstImage.url) { } else if (firstImage.url) {
// Fetch the image from the URL // Fetch the image from the URL
const imageUrl = this.corsProxy ? proxyUrl(this.corsProxy, firstImage.url) : firstImage.url; const imageUrl = this.corsProxy ? proxyUrl({ template: this.corsProxy, url: firstImage.url }) : firstImage.url;
const imageResponse = await fetch(imageUrl); const imageResponse = await fetch(imageUrl);
if (!imageResponse.ok) { if (!imageResponse.ok) {