Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generic types for getQuery and parseQuery #131

Merged
merged 8 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.vscode
.idea
node_modules
*.log
.DS_Store
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"lint:fix": "eslint --fix --ext .ts . && prettier -w src test",
"prepack": "pnpm build",
"release": "pnpm test && changelogen --release && npm publish && git push --follow-tags",
"test": "pnpm lint && vitest run"
"test": "pnpm lint && vitest --run typecheck && vitest run"
},
"devDependencies": {
"@types/node": "^20.4.5",
Expand Down
9 changes: 7 additions & 2 deletions src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ export type QueryValue =
| number
| undefined
| null
| boolean
| Array<QueryValue>
| Record<string, any>;

export type QueryObject = Record<string, QueryValue | QueryValue[]>;

export type ParsedQuery = Record<string, string | string[]>;

export function parseQuery(parametersString = ""): ParsedQuery {
export function parseQuery<T extends ParsedQuery = ParsedQuery>(
parametersString = ""
): T {
const object: ParsedQuery = {};
if (parametersString[0] === "?") {
parametersString = parametersString.slice(1);
Expand All @@ -38,7 +43,7 @@ export function parseQuery(parametersString = ""): ParsedQuery {
object[key] = [object[key] as string, value];
}
}
return object;
return object as T;
}

export function encodeQueryItem(
Expand Down
8 changes: 5 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { $URL } from "./url";
import { parseURL, stringifyParsedURL } from "./parse";
import { ParsedQuery, QueryObject, parseQuery, stringifyQuery } from "./query";
import { QueryObject, parseQuery, stringifyQuery, ParsedQuery } from "./query";
import { decode } from "./encoding";

export function isRelative(inputString: string) {
Expand Down Expand Up @@ -132,8 +132,10 @@ export function withQuery(input: string, query: QueryObject): string {
return stringifyParsedURL(parsed);
}

export function getQuery(input: string): ParsedQuery {
return parseQuery(parseURL(input).search);
export function getQuery<T extends ParsedQuery = ParsedQuery>(
input: string
): T {
return parseQuery<T>(parseURL(input).search);
}

export function isEmptyURL(url: string) {
Expand Down
4 changes: 4 additions & 0 deletions test/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ describe("getQuery", () => {
},
"http://foo.com/?param=3&param=": { param: ["3", ""] },
"http://foo.com/?param=&param=3": { param: ["", "3"] },
"http://foo.com/?param=": { param: "" },
"http://foo.com/?param=%7B%22a%22:%5B%7B%22obj%22:%5B1,2,3%5D%7D%5D%7D": {
param: '{"a":[{"obj":[1,2,3]}]}',
},
};

for (const t in tests) {
Expand Down
14 changes: 14 additions & 0 deletions test/types.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, expectTypeOf, test } from "vitest";
import { getQuery, parseQuery } from "../src";

describe("query", () => {
test("getQuery generic type support", () => {
const result = getQuery<{ foo: string }>("http://foo.com/?foo=bar");
expectTypeOf(result).toEqualTypeOf<{ foo: string }>();
});

test("parseQuery generic type support", () => {
const result = parseQuery<{ foo: string }>("http://foo.com/?foo=bar");
expectTypeOf(result).toEqualTypeOf<{ foo: string }>();
});
});