AI Decision System
Replayable, offline-verifiable records for AI-assisted go/no-go decisions: experiment promotions, typed model answers and human-approved actions.
JavaScript용 URL 빌더 라이브러리
A URL builder library for JavaScript.
개발 도구TypeScript · JavaScriptGitHub @balazsbotond직접 설치운영자 미확인
써 보셨어요? 한 번만 눌러 주세요. 로그인은 필요 없어요.
메이커가 쓴 소개는 위의 한 줄이 전부입니다. 더 자세한 내용은 저장소 README에서 읽을 수 있습니다. 저장소 README ↗
README 전체를 불러오는 중입니다.
README 전문 보기 ↗urlcat is a tiny JavaScript library that makes building URLs very convenient and prevents common mistakes.
When I call an HTTP API, I usually need to add dynamic parameters to the URL:
const API_URL = 'https://api.example.com/';
function getUserPosts(id, blogId, limit, offset) {
const requestUrl = `${API_URL}/users/${id}/blogs/${blogId}/posts?limit=${limit}&offset=${offset}`;
// send HTTP request
}
As you can see, this minimal example is already rather hard to read. It is also incorrect:
API_URL constant so this resulted in a URL containing duplicate slashes (https://api.example.com//users).encodeURIComponentI can use the built-in URL class to prevent duplicate slashes and URLSearchParams to escape the query string. But I still need to escape all path parameters manually.
const API_URL = 'https://api.example.com/';
function getUserPosts(id, blogId, limit, offset) {
const escapedId = encodeURIComponent(id);
const escapedBlogId = encodeURIComponent(blogId);
const path = `/users/${escapedId}/blogs/${escapedBlogId}`;
const url = new URL(path, API_URL);
url.search = new URLSearchParams({ limit, offset });
const requestUrl = url.href;
// send HTTP request
}
Such a simple task and yet very hard to read and tedious to write! This is where this tiny library can help you:
const API_URL = 'https://api.example.com/';
function getUserPosts(id, limit, offset) {
const requestUrl = urlcat(API_URL, '/users/:id/posts', { id, limit, offset });
// send HTTP request
}
The library handles:
Currently, the package is distributed via npm. Zip downloads are also available since v3.0.0 (A CDN is coming soon).
npm install --save urlcat
Node 14 and above are officially supported; CI checks the packed package on every even-numbered Node release line from 14 up to the current one.
To build full URLs (most common use case):
const urlcat = require('urlcat').default;
To use any of the utility functions:
const { query, subst, join } = require('urlcat');
To use all exported functions:
const { default: urlcat, query, subst, join } = require('urlcat');
Starting with v3.1.0, urlcat supports ESM!
To build full URLs (most common use case):
import urlcat from 'urlcat';
To use any of the utility functions:
import { query, subst, join } from 'urlcat';
To use all exported functions:
import urlcat, { query, subst, join } from 'urlcat';
TypeScript 2.1 and above are officially supported.
To build full URLs (most common use case):
import urlcat from 'urlcat';
To use any of the utility functions:
import { query, subst, join } from 'urlcat';
To use all exported functions:
import urlcat, { query, subst, join } from 'urlcat';
This library provides its own type definitions. "It just works", no need to install anything from @types.
ParamMap: an object with string keystype ParamMap = Record<string, any>;
For example, { firstParam: 1, 'second-param': 2 } is a valid ParamMap.
urlcat: build full URLsfunction urlcat(baseUrl: string, pathTemplate: string, params: ParamMap): string
function urlcat(baseUrl: string, pathTemplate: string): string
function urlcat(baseTemplate: string, params: ParamMap): string
NOTE about empty path segments:
RFC 3986 allows empty path segments in URLs (for example, https://example.com//users////2). urlcat keeps any empty path segments that aren't at the concatenation boundary between baseUrl and pathTemplate. To include an empty path segment there are two options:
urlcat('https://example.com/', '//users', { q: 1 }) → https://example.com//users?q=1baseTemplate overload: urlcat('https://example.com//users', { q: 1 }) → https://example.com//users?q=1query: build query stringsfunction query(params: ParamMap): string
Builds a query string using the key-value pairs specified. Keys and values are escaped, then joined by the '&' character.
subst: substitute path parametersfunction subst(template: string, params: ParamMap): string
Substitutes parameters with values in a template string. template may contain 0 or more parameter placeholders. Placeholders start with a colon (:), followed by a parameter name that can only contain uppercase or lowercase letters. Any placeholders found in the template are replaced with the value under the corresponding key in params.
join: join two strings using exactly one separatorfunction join(part1: string, separator: string, part2: string): string
Joins the two parts using exactly one separator. If a separator is present at the end of part1 or the beginning of part2, it is removed, then the two parts are joined using separator.
Thank you for using urlcat!
If you need any help using this library, feel free to create a GitHub issue, and ask your questions. I'll try to answer as quickly as possible.
Contributions of any kind (pull requests, bug reports, feature requests, documentation, design) are more than welcome! If you like this project and want to help, but feel like you are stuck, feel free to contact the maintainer (Botond Balázs <[email protected]>).
Building the project should be quick and easy. If it isn't, it's the maintainer's fault. Please report any problems with building in a GitHub issue.
You need Node.js 22.12 or later to build urlcat and run its tests (the version in .nvmrc is the one used for development). The published package itself runs on Node 14 and above.
First, clone the git repository:
git clone [email protected]:balazsbotond/urlcat.git
Then switch to the newly created urlcat directory and install the dependencies:
cd urlcat
npm install
You can then run the unit tests to verify that everything works correctly:
npm test
And finally, build the library:
npm run build
The output will appear in the dist directory.
Happy hacking!
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!