From 5d7ba538e46b83e3c09ba3a025190a1ff2a7e97a Mon Sep 17 00:00:00 2001 From: Thomas Wang Date: Sat, 27 Oct 2018 22:44:55 -0700 Subject: [PATCH] TypeScript def file and build time detection --- README.md | 5 ++++- index.d.ts | 31 ++++++++++++++++++++++++------- index.js | 9 +++++++++ tsconfig.json | 8 ++++++++ 4 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 tsconfig.json diff --git a/README.md b/README.md index 7c651626..f6d98ae8 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,9 @@ Available imports: import fetch, { Headers, Request, Response, AbortController } from 'fetch'; ``` +### Use with TypeScript +To use `ember-fetch` with TypeScript or enable editor's type support, add `"fetch": "ember-cli/ember-fetch"` to your app's `devDependencies`. + ### Use with Ember Data 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. @@ -65,7 +68,7 @@ export default { } ``` -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). +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). 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. ### Allow native fetch diff --git a/index.d.ts b/index.d.ts index 2c8d6f1d..81dcf13f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,9 +1,26 @@ -// Type definitions for Ember Fetch +// Type definitions for ember-fetch // Project: https://github.com/ember-cli/ember-fetch -// Definitions by: Toran Billups -// TypeScript Version: 2.3 +// Definitions by: Toran Billups , Thomas Wang +/// +/// -declare module 'fetch' { - import RSVP from 'rsvp'; - export default function fetch(input: RequestInfo, init?: RequestInit): RSVP.Promise; -} +import RSVP from 'rsvp'; +export default function fetch(input: RequestInfo, init?: RequestInit): RSVP.Promise; +export const Headers: { + prototype: Headers; + new(init?: HeadersInit): Headers; +}; +export const Request: { + prototype: Request; + new(input: RequestInfo, init?: RequestInit): Request; +}; +export const Response: { + prototype: Response; + new(body?: BodyInit | null, init?: ResponseInit): Response; + error(): Response; + redirect(url: string, status?: number): Response; +}; +export const AbortController: { + prototype: AbortController; + new(): AbortController; +}; \ No newline at end of file diff --git a/index.js b/index.js index 1f431a5a..7bf469a3 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ 'use strict'; const path = require('path'); +const fs = require('fs'); // We use a few different Broccoli plugins to build our trees: // // broccoli-templater: renders the contents of a file inside a template. @@ -97,6 +98,14 @@ module.exports = { ] } }); + + // Detect `tsconfig.json` as the evidence user need type support + if ( + fs.existsSync(path.join(app.project.root, 'tsconfig.json')) && + !('fetch' in app.project.pkg.devDependencies) + ) { + app.project.ui.writeWarnLine('To use ember-fetch with TypeScript, please add devDependency "fetch": "ember-cli/ember-fetch"'); + } }, /* diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 00000000..b31f5aa7 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,8 @@ +{ + "compilerOptions": { + "noEmit": true, + "lib": [], + "types": [] + }, + "files": ["index.d.ts"] +} \ No newline at end of file