Skip to content
This repository has been archived by the owner on Aug 20, 2020. It is now read-only.

Commit

Permalink
Introduce apollo-fetch-polyfill package
Browse files Browse the repository at this point in the history
Drop the global isomorphic-fetch polyfill from the main `apollo-fetch`
package and provide a convenience package that simply re-exports
`apollo-fetch` and includes the `isomorphic-fetch` polyfill.
  • Loading branch information
ctavan committed Sep 4, 2017
1 parent 21cc198 commit 5bf97d8
Show file tree
Hide file tree
Showing 15 changed files with 246 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

`apollo-fetch` is a lightweight client for GraphQL requests that supports middleware and afterware that modify requests and responses.

By default `apollo-fetch` uses `isomorphic-fetch`, but you have the option of using a custom fetch function.
By default `apollo-fetch` expects the [fetch API](https://developer.mozilla.org/en/docs/Web/API/Fetch_API) to be present in the global scope, but you have the option of using a custom fetch function.

There is a convenience wrapper module `apollo-fetch-polyfill` which comes with the `isomorphic-fetch` polyfill for environments where the fetch API is not present.

If you are interested in contributing, please read the [documentation on repository structure](docs/monorepo.md) and [Contributor Guide](CONTRIBUTING.md).

Expand Down
1 change: 1 addition & 0 deletions packages/apollo-fetch-polyfill/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/dist
1 change: 1 addition & 0 deletions packages/apollo-fetch-polyfill/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
5 changes: 5 additions & 0 deletions packages/apollo-fetch-polyfill/changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# apollo-fetch-polyfill change log

## 1.0.0

- Initial release.
39 changes: 39 additions & 0 deletions packages/apollo-fetch-polyfill/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "apollo-fetch-polyfill",
"version": "1.0.0",
"description": "Wrapper around apollo-fetch which includes the isomorphic-fetch polyfill",
"license": "MIT",
"author": "Christoph Tavan <[email protected]>",
"repository": {
"type": "git",
"url": "https://github.com/apollographql/apollo-fetch/tree/master/packages/apollo-fetch-polyfill"
},
"homepage": "https://github.com/apollographql/apollo-fetch#readme",
"bugs": "https://github.com/apollographql/apollo-fetch/issues",
"types": "./dist/main/index.d.ts",
"main": "./dist/main/index.js",
"module": "./dist/module/index.js",
"files": ["src", "dist"],
"scripts": {
"prebuild:lib": "npm run clean:lib",
"prebuild:test": "npm run clean:test",
"pretest": "npm run lint && npm run build:test",
"prepublishOnly": "npm run test && npm run build:lib",
"clean:lib": "rimraf dist",
"clean:test": "rimraf test/dist",
"build:lib": "tsc -p tsconfig.main.json && tsc -p tsconfig.module.json",
"build:test": "tsc -p tsconfig.test.json",
"lint": "tslint --type-check -p tsconfig.main.json && tslint --type-check -p tsconfig.test.json",
"test": "ava test/dist/test"
},
"dependencies": {
"apollo-fetch": "^0.5.2",
"isomorphic-fetch": "^2.2.1"
},
"devDependencies": {
"ava": "0.21.0",
"rimraf": "2.6.1",
"tslint": "5.6.0",
"typescript": "2.4.2"
}
}
3 changes: 3 additions & 0 deletions packages/apollo-fetch-polyfill/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import 'isomorphic-fetch';

export * from 'apollo-fetch';
14 changes: 14 additions & 0 deletions packages/apollo-fetch-polyfill/test/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import test from 'ava';
import * as apolloFetch from 'apollo-fetch';
import * as apolloFetchPolyfill from '../../src/index.js';

test('exports the same things as apollo-fetch', t => {
t.deepEqual(Object.keys(apolloFetchPolyfill), Object.keys(apolloFetch));
});

test('apollo-fetch-polyfill installs global fetch polyfill', t => {
t.is(typeof fetch, 'function');
t.is(typeof Headers, 'function');
t.is(typeof Request, 'function');
t.is(typeof Response, 'function');
});
12 changes: 12 additions & 0 deletions packages/apollo-fetch-polyfill/tsconfig.main.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"outDir": "dist/main",
"target": "es5",
"declaration": true,
"sourceMap": true,
"removeComments": true,
"noUnusedLocals": true,
"skipLibCheck": true
},
"include": ["src/**/*.ts"]
}
13 changes: 13 additions & 0 deletions packages/apollo-fetch-polyfill/tsconfig.module.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"outDir": "dist/module",
"target": "es5",
"module": "es6",
"moduleResolution": "node",
"sourceMap": true,
"removeComments": true,
"noUnusedLocals": true,
"skipLibCheck": true
},
"include": ["src/**/*.ts"]
}
9 changes: 9 additions & 0 deletions packages/apollo-fetch-polyfill/tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"outDir": "test/dist",
"target": "es5",
"noUnusedLocals": true,
"skipLibCheck": true
},
"include": ["src/**/*.ts", "test/src/**/*.ts"]
}
107 changes: 107 additions & 0 deletions packages/apollo-fetch-polyfill/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
{
"rules": {
"align": [false, "parameters", "arguments", "statements"],
"ban": false,
"class-name": true,
"curly": true,
"eofline": true,
"forin": true,
"indent": [true, "spaces", 2],
"interface-name": false,
"jsdoc-format": true,
"label-position": true,
"max-line-length": [true, 140],
"member-access": true,
"member-ordering": [
true,
"public-before-private",
"static-before-instance",
"variables-before-functions"
],
"no-any": false,
"no-arg": true,
"no-bitwise": true,
"no-conditional-assignment": true,
"no-consecutive-blank-lines": false,
"no-console": [true, "log", "debug", "info", "time", "timeEnd", "trace"],
"no-construct": true,
"no-debugger": true,
"no-duplicate-variable": true,
"no-empty": true,
"no-eval": true,
"no-inferrable-types": false,
"no-internal-module": true,
"no-null-keyword": false,
"no-parameter-properties": false,
"no-require-imports": false,
"no-shadowed-variable": true,
"no-switch-case-fall-through": true,
"no-trailing-whitespace": true,
"no-unused-expression": true,
"no-use-before-declare": true,
"no-var-keyword": true,
"no-var-requires": true,
"object-literal-sort-keys": false,
"one-line": [
true,
"check-open-brace",
"check-catch",
"check-else",
"check-finally",
"check-whitespace"
],
"quotemark": [true, "single", "avoid-escape"],
"radix": true,
"semicolon": [true, "always"],
"switch-default": true,
"trailing-comma": [
true,
{
"multiline": "always",
"singleline": "never"
}
],
"triple-equals": [true, "allow-null-check"],
"typedef": [
false,
"call-signature",
"parameter",
"arrow-parameter",
"property-declaration",
"variable-declaration",
"member-variable-declaration"
],
"typedef-whitespace": [
true,
{
"call-signature": "nospace",
"index-signature": "nospace",
"parameter": "nospace",
"property-declaration": "nospace",
"variable-declaration": "nospace"
},
{
"call-signature": "space",
"index-signature": "space",
"parameter": "space",
"property-declaration": "space",
"variable-declaration": "space"
}
],
"variable-name": [
true,
"check-format",
"allow-pascal-case",
"allow-leading-underscore",
"ban-keywords"
],
"whitespace": [
true,
"check-branch",
"check-decl",
"check-operator",
"check-separator",
"check-type"
]
}
}
7 changes: 3 additions & 4 deletions packages/apollo-fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"pretest": "npm run build-test",
"test": "npm run test-only --",
"posttest": "npm run lint",
"test-only": "mocha --reporter spec --full-trace dist/tests/tests.js",
"test-only": "mocha --reporter spec --full-trace dist/tests/tests.js && mocha --reporter spec --full-trace dist/tests/no-polyfill.js",
"test-watch": "mocha --reporter spec --full-trace dist/tests/tests.js --watch",
"coverage:test": "_mocha --reporter dot --full-trace dist/tests/tests.js",
"coverage": "nyc --reporter=lcov npm run coverage:test",
Expand All @@ -44,9 +44,7 @@
"prepublish": "npm run clean && npm run build",
"prepublishOnly": "npm run clean && npm run build"
},
"dependencies": {
"isomorphic-fetch": "^2.2.1"
},
"dependencies": {},
"devDependencies": {
"@types/chai": "4.0.4",
"@types/chai-as-promised": "0.0.31",
Expand All @@ -56,6 +54,7 @@
"fetch-mock": "5.12.2",
"graphql": "0.10.5",
"graphql-tag": "2.4.2",
"isomorphic-fetch": "^2.2.1",
"lerna": "2.1.2",
"lodash": "4.17.4",
"mocha": "3.5.0",
Expand Down
7 changes: 6 additions & 1 deletion packages/apollo-fetch/src/apollo-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
FetchError,
BatchError,
} from './types';
import 'isomorphic-fetch';

type WareStack =
| MiddlewareInterface[]
Expand Down Expand Up @@ -86,6 +85,12 @@ function throwBatchError(response) {
export function createApolloFetch(params: FetchOptions = {}): ApolloFetch {
const { constructOptions, customFetch } = params;

if (typeof fetch !== 'function' && !customFetch) {
throw new Error(
'Global fetch API must be present or customFetch must be provided',
);
}

const _uri = params.uri || '/graphql';
const middlewares = [];
const batchedMiddlewares = [];
Expand Down
1 change: 1 addition & 0 deletions packages/apollo-fetch/tests/apollo-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'isomorphic-fetch';
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as sinon from 'sinon';
Expand Down
29 changes: 29 additions & 0 deletions packages/apollo-fetch/tests/no-polyfill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import { createApolloFetch } from '../src/apollo-fetch';

chai.use(chaiAsPromised);

const { assert } = chai;

describe('apollo-fetch', () => {
describe('createApolloFetch', () => {
it('should throw if fetch api is not present', () => {
assert.throws(() => {
createApolloFetch({ uri: 'test' });
}, /Global fetch API must be present or customFetch must be provided/);
});

it('should not throw if fetch api is not present but customFetch is provided', () => {
assert.doesNotThrow(() => {
createApolloFetch({
uri: 'test',
customFetch: () =>
new Promise((resolve, reject) => {
resolve(new Response());
}),
});
});
});
});
});

0 comments on commit 5bf97d8

Please sign in to comment.