typescript-fetch
Advanced tools
Comparing version 0.0.2 to 0.0.3
# typescript-fetch | ||
## 0.0.3 | ||
### Patch Changes | ||
- [`6b7bc10`](https://github.com/jacob-ebey/typescript-fetch/commit/6b7bc101b17e606b2ba01775e2444e07807c781f) Thanks [@jacob-ebey](https://github.com/jacob-ebey)! - add `json()` helper | ||
## 0.0.2 | ||
@@ -4,0 +10,0 @@ |
@@ -7,1 +7,8 @@ export function typeRequest(input, init) { | ||
} | ||
export function json(data, init) { | ||
init = (typeof init === "number" ? { status: init } : init) || {}; | ||
const headers = new Headers(init?.headers); | ||
init.headers = headers; | ||
headers.set("Content-Type", "application/json"); | ||
return new Response(JSON.stringify(data), init); | ||
} |
33
index.ts
@@ -0,1 +1,3 @@ | ||
import { Jsonifiable, Jsonify } from "type-fest"; | ||
export type RequestMethod = | ||
@@ -116,1 +118,32 @@ | "GET" | ||
} | ||
export interface TypedResponseInit<Status extends number> | ||
extends globalThis.ResponseInit { | ||
status?: Status; | ||
} | ||
export function json<Data extends Jsonifiable, Status extends number>( | ||
data: Data, | ||
init: Status | ||
): TypedResponseJSON<Status, Jsonify<Data>>; | ||
export function json<Data extends Jsonifiable, Status extends number>( | ||
data: Data, | ||
init: TypedResponseInit<Status> | ||
): TypedResponseJSON<Status, Jsonify<Data>>; | ||
export function json<Data extends Jsonifiable, Status extends 200>( | ||
data: Data, | ||
init?: never | ||
): TypedResponseJSON<Status, Jsonify<Data>>; | ||
export function json<Data extends Jsonifiable, Status extends number>( | ||
data: Data, | ||
init?: globalThis.ResponseInit | Status | ||
): TypedResponseJSON<Status, Jsonify<Data>> { | ||
init = (typeof init === "number" ? { status: init } : init) || {}; | ||
const headers = new Headers(init?.headers); | ||
init.headers = headers; | ||
headers.set("Content-Type", "application/json"); | ||
return new Response(JSON.stringify(data), init) as TypedResponseJSON< | ||
Status, | ||
Jsonify<Data> | ||
>; | ||
} |
{ | ||
"name": "typescript-fetch", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"main": "dist/index.js", | ||
@@ -9,2 +9,5 @@ "types": "dist/index.d.ts", | ||
}, | ||
"dependencies": { | ||
"type-fest": "^3.3.0" | ||
}, | ||
"devDependencies": { | ||
@@ -11,0 +14,0 @@ "typescript": "^4.9.3" |
Sorry, the diff of this file is not supported yet
8255
205
1
+ Addedtype-fest@^3.3.0
+ Addedtype-fest@3.13.1(transitive)