Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippDehler committed Feb 11, 2023
0 parents commit b531785
Show file tree
Hide file tree
Showing 27 changed files with 3,532 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules/
82 changes: 82 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Query String Builder

A JavaScript library for building and encoding query strings.

## Installation

```bash
npm install ts-query-string

```

## Usage

```typescript
import { QueryStringBuilder } from "ts-query-string";

const qs = QueryStringBuilder()
.add({ name: "param1", value: "value1" })
.add({ name: "param2", value: 2 })
.add({ name: "param3", value: "value3" })
.add({ name: "param4", value: "value4 with spaces" });
.build();
type QS = typeof qs; // QS = "param1=value1&param2=2&param3=value3&param4=value4%20with%20spaces"

console.log(qs); // QS = "param1=value1&param2=2&param3=value3&param4=value4%20with%20spaces"
```

## API Reference

`QueryStringBuilder`

A class for building a query string.

### add

Add a name-value pair to the query string.

```typescript
function add<Name extends string, Value extends ResolveableQueryValue>(input: {
name: Name;
value: Value;
}): QueryStringBuilder<[...T, typeof input]>;
```

### build

Build the query string from the name-value pairs.

```typescript
function build(): EncodeQueryString<T>;
```

### EncodeQueryString

A type that represents a query string encoded from the name-value pairs of type T.

```typescript
type EncodeQueryString<T extends QueryValue[]> = ...
```
### EncodeUri
A type that represents the result of encoding a ResolveableQueryValue type.
```typescript
type EncodeUri<T extends ResolveableQueryValue> = ...
```
### ResolveableQueryValue
A type that represents a value that can be resolved to a string.
```typescript
type ResolveableQueryValue = string | number | boolean;
```

### Notes

- The `add` method returns a new instance of `QueryStringBuilder` with the new name-value pair added to the type parameter `T`.
- If you insert a value that is not a `ResolveableQueryValue` type, the compiler will throw an error.
- This package is written in TypeScript and the types are exported.
- If somehow you get an `Unecodeable<{character}>` in your query string type please open an issue or pull request.
5 changes: 5 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { EncodeQueryString, EncodeUri } from "./src/literalEncoder";
import { QueryStringBuilder } from "./src/QueryStringBuilder";
import { serializeQueryNameValue, serializeQueryNameValues } from "./src/serializer";
import { QueryValue, ResolveableQueryValue } from "./src/types";
export { QueryStringBuilder, serializeQueryNameValues, serializeQueryNameValue, EncodeUri, EncodeQueryString, QueryValue, ResolveableQueryValue, };
8 changes: 8 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.serializeQueryNameValue = exports.serializeQueryNameValues = exports.QueryStringBuilder = void 0;
const QueryStringBuilder_1 = require("./src/QueryStringBuilder");
Object.defineProperty(exports, "QueryStringBuilder", { enumerable: true, get: function () { return QueryStringBuilder_1.QueryStringBuilder; } });
const serializer_1 = require("./src/serializer");
Object.defineProperty(exports, "serializeQueryNameValue", { enumerable: true, get: function () { return serializer_1.serializeQueryNameValue; } });
Object.defineProperty(exports, "serializeQueryNameValues", { enumerable: true, get: function () { return serializer_1.serializeQueryNameValues; } });
Loading

0 comments on commit b531785

Please sign in to comment.