function setQueryParams
setQueryParams(
url: string,
params: Readonly<Record<string, unknown>>
): string

url, with params folded into its query: { page: 2 } on /api/users is /api/users?page=2.

What a GET's argument means. A GET carries no body — fetch throws a bare TypeError if you try — so an object after the URL can only be one thing, and writing it as an object is how somebody at a JavaScript prompt says a query without hand-encoding it.

A name already in the URL is replaced rather than repeated, because the object is the later and more specific statement. An array repeats the name, which is what every server that takes a list expects; anything deeper is JSON, which is what the ones that take a structure expect. A value that is null or undefined is not a parameter at all: a value you do not have is not one you meant to send.

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

setQueryParams('http://x/api/users', { page: 2 }); // 'http://x/api/users?page=2'
setQueryParams('http://x/a?page=1', { page: 2, tag: ['new', 'old'] });
// 'http://x/a?page=2&tag=new&tag=old'
setQueryParams('http://x/a', { missing: undefined }); // 'http://x/a'

Parameters

url: string
params: Readonly<Record<string, unknown>>

Return Type

string

Usage

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