-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b531785
Showing
27 changed files
with
3,532 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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¶m2=2¶m3=value3¶m4=value4%20with%20spaces" | ||
|
||
console.log(qs); // QS = "param1=value1¶m2=2¶m3=value3¶m4=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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } }); |
Oops, something went wrong.