Skip to content

Commit

Permalink
TypeScript def file and build time detection
Browse files Browse the repository at this point in the history
  • Loading branch information
xg-wang committed Oct 31, 2018
1 parent d7bb5af commit 5d7ba53
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down
31 changes: 24 additions & 7 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/toranb>
// TypeScript Version: 2.3
// Definitions by: Toran Billups <https://github.com/toranb>, Thomas Wang<https://github.com/xg-wang>
/// <reference lib="es2015" />
/// <reference lib="dom" />

declare module 'fetch' {
import RSVP from 'rsvp';
export default function fetch(input: RequestInfo, init?: RequestInit): RSVP.Promise<Response>;
}
import RSVP from 'rsvp';
export default function fetch(input: RequestInfo, init?: RequestInit): RSVP.Promise<Response>;
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;
};
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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"');
}
},

/*
Expand Down
8 changes: 8 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"noEmit": true,
"lib": [],
"types": []
},
"files": ["index.d.ts"]
}

0 comments on commit 5d7ba53

Please sign in to comment.