Skip to content

Commit 5d7ba53

Browse files
committed
TypeScript def file and build time detection
1 parent d7bb5af commit 5d7ba53

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ Available imports:
3535
import fetch, { Headers, Request, Response, AbortController } from 'fetch';
3636
```
3737

38+
### Use with TypeScript
39+
To use `ember-fetch` with TypeScript or enable editor's type support, add `"fetch": "ember-cli/ember-fetch"` to your app's `devDependencies`.
40+
3841
### Use with Ember Data
3942
To have Ember Data utilize `fetch` instead of jQuery.ajax to make calls to your backend, extend your project's `application` adapter with the `adapter-fetch` mixin.
4043

@@ -65,7 +68,7 @@ export default {
6568
}
6669
```
6770

68-
For addon authors, if the addon supports Fastboot mode, `ember-fetch` should also be listed as a [peer dependency](https://docs.npmjs.com/files/package.json#peerdependencies).
71+
For addon authors, if the addon supports Fastboot mode, `ember-fetch` should also be listed as a [peer dependency](https://docs.npmjs.com/files/package.json#peerdependencies).
6972
This is because Fastboot only invokes top-level addon's `updateFastBootManifest` ([detail](https://github.com/ember-fastboot/ember-cli-fastboot/issues/597)), thus `ember-fetch` has to be a top-level addon installed by the host app.
7073

7174
### Allow native fetch

index.d.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1-
// Type definitions for Ember Fetch
1+
// Type definitions for ember-fetch
22
// Project: https://github.com/ember-cli/ember-fetch
3-
// Definitions by: Toran Billups <https://github.com/toranb>
4-
// TypeScript Version: 2.3
3+
// Definitions by: Toran Billups <https://github.com/toranb>, Thomas Wang<https://github.com/xg-wang>
4+
/// <reference lib="es2015" />
5+
/// <reference lib="dom" />
56

6-
declare module 'fetch' {
7-
import RSVP from 'rsvp';
8-
export default function fetch(input: RequestInfo, init?: RequestInit): RSVP.Promise<Response>;
9-
}
7+
import RSVP from 'rsvp';
8+
export default function fetch(input: RequestInfo, init?: RequestInit): RSVP.Promise<Response>;
9+
export const Headers: {
10+
prototype: Headers;
11+
new(init?: HeadersInit): Headers;
12+
};
13+
export const Request: {
14+
prototype: Request;
15+
new(input: RequestInfo, init?: RequestInit): Request;
16+
};
17+
export const Response: {
18+
prototype: Response;
19+
new(body?: BodyInit | null, init?: ResponseInit): Response;
20+
error(): Response;
21+
redirect(url: string, status?: number): Response;
22+
};
23+
export const AbortController: {
24+
prototype: AbortController;
25+
new(): AbortController;
26+
};

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const path = require('path');
4+
const fs = require('fs');
45
// We use a few different Broccoli plugins to build our trees:
56
//
67
// broccoli-templater: renders the contents of a file inside a template.
@@ -97,6 +98,14 @@ module.exports = {
9798
]
9899
}
99100
});
101+
102+
// Detect `tsconfig.json` as the evidence user need type support
103+
if (
104+
fs.existsSync(path.join(app.project.root, 'tsconfig.json')) &&
105+
!('fetch' in app.project.pkg.devDependencies)
106+
) {
107+
app.project.ui.writeWarnLine('To use ember-fetch with TypeScript, please add devDependency "fetch": "ember-cli/ember-fetch"');
108+
}
100109
},
101110

102111
/*

tsconfig.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"noEmit": true,
4+
"lib": [],
5+
"types": []
6+
},
7+
"files": ["index.d.ts"]
8+
}

0 commit comments

Comments
 (0)