function resolveUserInputURL
resolveUserInputURL(
target: string,
pageUrl: string | null
): string | null

The absolute URL a target means, or null where it means nothing usable.

One rule, and it is the one a test runner's REPL wants: anything without a scheme is the PAGE's own server. .get /tests.js is the file the suite loaded, .get api/users is that route on the same origin, and anywhere else on the internet is spelled with an http:// in front of it.

The exceptions are the ones nobody would accept being read as a path: an explicit scheme, a protocol-relative //host/x, a host with a port on it — localhost:3000/x and 127.0.0.1:8080 are addresses in every tool that takes one, and reading them as filenames would be perverse — and a bare :3000/x, which is that same address with the part you never vary left out.

A bare example.com/x IS read as a path, deliberately: tests.js and fixtures/page.html are far commoner things to type at this prompt than a naked domain, and there is no way to tell the two apart without guessing at TLDs. Writing the scheme settles it, and the commands say so.

import { resolveUserInputURL } from './http-service.ts';

const page = 'http://localhost:4321/index.html';
resolveUserInputURL('/tests.js', page); // 'http://localhost:4321/tests.js'
resolveUserInputURL('api/users', page); // 'http://localhost:4321/api/users'
resolveUserInputURL('localhost:3000/x', page); // 'http://localhost:3000/x'
resolveUserInputURL(':3000/x', page); // 'http://localhost:3000/x' — the port on this machine
resolveUserInputURL('https://example.com', page); // 'https://example.com/'
resolveUserInputURL('/tests.js', null); // null — nothing to be relative to

Parameters

target: string
pageUrl: string | null

Return Type

string | null

Usage

import { resolveUserInputURL } from "lib/repl/http-service.ts";