-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #159 from developit/typescript-support
Add full TypeScript definitions and export them, add basic TS tests.
- Loading branch information
Showing
6 changed files
with
232 additions
and
42 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,58 @@ | ||
import unfetch, { Unfetch } from ".."; | ||
import isomorphicUnfetch from "../packages/isomorphic-unfetch"; | ||
|
||
describe("TypeScript", () => { | ||
describe("browser", () => { | ||
beforeAll(() => { | ||
function XMLHttpRequest() { | ||
const res = { | ||
setRequestHeader: jest.fn(), | ||
getAllResponseHeaders: jest.fn().mockReturnValue(""), | ||
getResponseHeader: jest.fn().mockReturnValue(""), | ||
open: jest.fn((method, url) => { | ||
res.responseURL = url; | ||
}), | ||
send: jest.fn(), | ||
status: 200, | ||
statusText: "OK", | ||
get responseText() { | ||
return this.responseURL.replace(/^data:\,/, ""); | ||
}, | ||
responseURL: null, | ||
onload: () => {}, | ||
}; | ||
setTimeout(() => res.onload()); | ||
return res; | ||
} | ||
|
||
// @ts-ignore-next-line | ||
global.XMLHttpRequest = jest.fn(XMLHttpRequest); | ||
}); | ||
|
||
it("should have valid TypeScript types", async () => { | ||
const res: Unfetch.Response = await unfetch("data:,test"); | ||
const text = await res.text(); | ||
expect(text).toBe("test"); | ||
}); | ||
|
||
// This fails because we're abusing Arrays as iterables: | ||
// it("should allow cast to Response", async () => { | ||
// const res: Response = await unfetch("data:,test"); | ||
// const r = res.headers.keys()[0] | ||
// }); | ||
}); | ||
|
||
describe("isomorphic-unfetch", () => { | ||
it("should allow use of standard types like Response", async () => { | ||
const res: Response = await isomorphicUnfetch(new URL("data:,test")); | ||
const blob: Blob = await res.blob(); | ||
}); | ||
|
||
it("should accept Headers", async () => { | ||
isomorphicUnfetch("data:,test", { | ||
headers: new Headers({ a: "b" }), | ||
mode: "cors", | ||
}); | ||
}); | ||
}); | ||
}); |
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,12 @@ | ||
{ | ||
"compilerOptions": { | ||
"noEmit": true, | ||
"checkJs": true, | ||
"target": "ESNext", | ||
"moduleResolution": "Node", | ||
"allowSyntheticDefaultImports": true, | ||
}, | ||
"include": [ | ||
"./**/*.ts" | ||
] | ||
} |