# uri-tag

> ES6 template literal tag to encode URI components

Latest version **2.0.0** (published 2023-04-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install uri-tag
pnpm add uri-tag
yarn add uri-tag
bun add uri-tag
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2023-04-15 |
| First published | 2018-04-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >= 14.15 |
| Dependencies | 0 |
| Unpacked size | 21.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | mkrause |
| Maintainers | mkrause |
| Keywords | uri, url, encode, encoding, escape, escaping, tag, template, es2015-tag, es6-tag, es6, tagged |

## Links

- npm: https://www.npmjs.com/package/uri-tag
- Repository: https://github.com/mkrause/uri-tag
- Homepage: https://github.com/mkrause/uri-tag#readme
- Issues: https://github.com/mkrause/uri-tag/issues
- npm.io page: https://npm.io/package/uri-tag

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [jwt-simple](https://npm.io/package/jwt-simple.md) — 259.5K weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2023-04-15
- 1.4.2 — 2023-04-08
- 1.4.1 — 2021-12-15
- 1.4.0 — 2021-06-12
- 1.3.0 — 2020-08-15
- 1.2.3 — 2020-05-28
- 1.2.2 — 2020-05-28
- 1.2.1 — 2020-05-28
- 1.2.0 — 2020-04-24
- 1.1.7 — 2020-04-02
- 1.1.6 — 2020-03-06
- 1.1.5 — 2020-03-05
- 1.1.4 — 2020-01-12
- 1.1.3 — 2019-10-27
- 1.1.2 — 2019-10-27
- … 10 more at https://npm.io/package/uri-tag/versions

## README

# uri-tag

![MIT](https://img.shields.io/npm/l/uri-tag?style=flat-square)
[![npm](https://img.shields.io/npm/v/uri-tag.svg?style=flat-square)](https://www.npmjs.com/package/uri-tag)
[![GitHub Actions](https://github.com/mkrause/uri-tag/actions/workflows/nodejs.yml/badge.svg)](https://github.com/mkrause/uri-tag/actions)
[![Types](https://img.shields.io/npm/types/uri-tag)](https://www.typescriptlang.org)

A [template literal tag](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) to encode URI components. Allows you to to build a URI string, where any expressions will be safely encoded.

```js
import uri from 'uri-tag';

const name = getName(); // Arbitrary user input
const status = getStatus(); // Arbitrary user input

const myUri = uri`https://example.com/api/users?name=${name}&status=${status}`;
```

Uses [RFC 3986](https://tools.ietf.org/html/rfc3986) compliant URI encoding to encode any unsafe character sequences to their escaped representations:

```js
const query = 'query with special chars ! ? foo=bar %';
const endpoint = uri`/api/search?q=${query}`;

// endpoint === '/api/search?q=query%20with%20special%20chars%20%21%20%3F%20foo%3Dbar%20%25'
```

To bypass encoding for a specific component, you can use `uri.raw`:

```js
const apiBase = 'https://example.com/api/v1';
const query = 'foo/bar';
const endpoint = uri`${uri.raw(apiBase)}/users?name=${query}`;

// endpoint === 'https://example.com/api/v1/users?name=foo%2Fbar'
```

`uri.raw` uses a unique [symbol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol) under the hood, so that only code with access to the `uri-tag` module can pass in a raw template variable. Any user input from an external source (so, strings, JSON objects, etc.) will not be able to access this symbol.


## Types

This package includes definitions for TypeScript.


## Similar packages

* [encody](https://www.npmjs.com/package/encody)
* [encodeuricomponent-tag](https://www.npmjs.com/package/encodeuricomponent-tag)
* [url-escape-tag](https://www.npmjs.com/package/url-escape-tag) (Relies on NodeJS `querystring` module, requires a polyfill for browser usage.)
* [url-tagged-template](https://www.npmjs.com/package/url-tagged-template) (Different goals, parses URLs to their components rather than returning a URL string.)

---
_Source: https://npm.io/package/uri-tag · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
