typescript-fetch
Advanced tools
Comparing version 0.0.3 to 0.0.4
# typescript-fetch | ||
## 0.0.4 | ||
### Patch Changes | ||
- [`3eb5546`](https://github.com/jacob-ebey/typescript-fetch/commit/3eb554685e6490f2b79e574d458deb52e6472b79) Thanks [@jacob-ebey](https://github.com/jacob-ebey)! - add typed URL | ||
## 0.0.3 | ||
@@ -4,0 +10,0 @@ |
@@ -0,1 +1,4 @@ | ||
export function typedURL(request) { | ||
return new URL(request.url); | ||
} | ||
export function typeRequest(input, init) { | ||
@@ -2,0 +5,0 @@ return new Request(input, init); |
@@ -1,3 +0,4 @@ | ||
import { typedRequest } from "."; | ||
import { typedRequest, typedURL, } from "."; | ||
export function fetchTests() { | ||
function assertRequest(_request) { } | ||
// Should be able to type basic Requests | ||
@@ -23,2 +24,7 @@ assertRequest(typedRequest("https://.../test")); | ||
assertPath("/.../.../"); | ||
function assertTypedURL(_url) { } | ||
assertTypedURL(typedURL(null)); | ||
assertTypedURL(typedURL(null)); | ||
assertTypedURL(typedURL(null)); | ||
assertTypedURL(typedURL(null)); | ||
} |
44
index.ts
@@ -39,8 +39,50 @@ import { Jsonifiable, Jsonify } from "type-fest"; | ||
export interface TypedURLSearchParams<Key extends string> | ||
extends globalThis.URLSearchParams { | ||
append(name: Key, value: string): void; | ||
delete(name: Key): void; | ||
get(name: Key): string | null; | ||
getAll(name: Key): string[]; | ||
has(name: Key): boolean; | ||
set(name: Key, value: string): void; | ||
} | ||
export interface TypedURL< | ||
Pathname extends PathnameFormat, | ||
SearchParamKeys extends string | ||
> { | ||
href: URLFormat<Pathname>; | ||
searchParams: TypedURLSearchParams<SearchParamKeys>; | ||
} | ||
export function typedURL<Request extends TypedRequest<any, any>>( | ||
request: Request | ||
) { | ||
return new URL(request.url) as Request extends TypedRequest< | ||
any, | ||
infer Pathname, | ||
infer SearchParamKeys | ||
> | ||
? TypedURL<inferPath<Pathname>, SearchParamKeys> | ||
: unknown; | ||
} | ||
export interface TypedFormData<Key extends string> extends globalThis.FormData { | ||
append(key: Key, value: FormDataEntryValue, fileName?: string): void; | ||
delete(key: Key): void; | ||
get(key: Key): FormDataEntryValue | null; | ||
getAll(key: Key): FormDataEntryValue[]; | ||
has(key: Key): boolean; | ||
set(key: Key, value: FormDataEntryValue, fileName?: string): void; | ||
} | ||
export interface TypedRequest< | ||
Method extends RequestMethod, | ||
Pathname extends string | ||
Pathname extends string, | ||
SearchParamKeys extends string = string, | ||
FormDataKeys extends string = string | ||
> extends globalThis.Request { | ||
method: Method; | ||
url: URLFormat<inferPath<Pathname>>; | ||
formData(): Promise<TypedFormData<FormDataKeys>>; | ||
} | ||
@@ -47,0 +89,0 @@ |
{ | ||
"name": "typescript-fetch", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "types": "dist/index.d.ts", |
33
tests.ts
@@ -1,9 +0,15 @@ | ||
import { typedRequest, type TypedRequest, type inferPath } from "."; | ||
import { | ||
typedRequest, | ||
typedURL, | ||
type TypedRequest, | ||
type TypedURL, | ||
type inferPath, | ||
} from "."; | ||
declare function assertRequest< | ||
TR extends TypedRequest<any, any>, | ||
TR2 extends TR = TR | ||
>(_request: TR2): void; | ||
export function fetchTests() { | ||
function assertRequest< | ||
TR extends TypedRequest<any, any>, | ||
TR2 extends TR = TR | ||
>(_request: TR2) {} | ||
export function fetchTests() { | ||
// Should be able to type basic Requests | ||
@@ -33,2 +39,17 @@ assertRequest<TypedRequest<"GET", "/test">>(typedRequest("https://.../test")); | ||
assertPath<inferPath<"/*">>("/.../.../"); | ||
function assertTypedURL<TU extends TypedURL<any, any>>(_url: TU) {} | ||
assertTypedURL<TypedURL<inferPath<"/">, never>>( | ||
typedURL(null as unknown as TypedRequest<"GET", "/">) | ||
); | ||
assertTypedURL<TypedURL<inferPath<"/">, "">>( | ||
typedURL(null as unknown as TypedRequest<"GET", "/">) | ||
); | ||
assertTypedURL<TypedURL<inferPath<"/">, "a">>( | ||
typedURL(null as unknown as TypedRequest<"GET", "/", "a">) | ||
); | ||
assertTypedURL<TypedURL<inferPath<"/">, "a" | "b">>( | ||
typedURL(null as unknown as TypedRequest<"GET", "/", "a" | "b">) | ||
); | ||
} |
Sorry, the diff of this file is not supported yet
10609
271