From 21efd3dccfe85469491598533e5c09292248813d Mon Sep 17 00:00:00 2001 From: Evans Hauser Date: Tue, 25 Jul 2017 17:38:25 -0700 Subject: [PATCH 1/5] Removed Object.assign and destructuring of Reponse, which fails to include gets and set --- packages/apollo-fetch/src/apollo-fetch.ts | 165 +++++++++++----------- 1 file changed, 84 insertions(+), 81 deletions(-) diff --git a/packages/apollo-fetch/src/apollo-fetch.ts b/packages/apollo-fetch/src/apollo-fetch.ts index 86d64240..953e0e07 100644 --- a/packages/apollo-fetch/src/apollo-fetch.ts +++ b/packages/apollo-fetch/src/apollo-fetch.ts @@ -105,103 +105,106 @@ export function createApolloFetch(params: FetchOptions = {}): ApolloFetch { }); }; - const apolloFetch: ApolloFetch = Object.assign( - function (request: GraphQLRequest | GraphQLRequest[]): Promise { - let options = {}; - let parseError; + const apolloFetch = function (request: GraphQLRequest | GraphQLRequest[]): Promise { + let options = {}; + let parseError; - const batched = Array.isArray(request); + const batched = Array.isArray(request); - const requestObject = (batched ? { - requests: request, - options, - } : { + const requestObject = (batched ? { + requests: request, + options, + } : { request, options, }); - return applyMiddlewares(requestObject, batched) - .then((reqOpts) => { - const construct = (constructOptions || constructDefaultOptions); - const requestOrRequests = ((reqOpts).request || (reqOpts).requests); - return construct(requestOrRequests, reqOpts.options); - }) - .then( opts => { - options = {...opts}; - return (customFetch || fetch) (_uri, options); - }) - .then( response => response.text().then( raw => { - try { - const parsed = JSON.parse(raw); - return { ...response, raw, parsed }; - } catch (e) { - parseError = e; - - //pass parsed raw response onto afterware - return { ...response, raw }; - } - }), - //.catch() this should never happen: https://developer.mozilla.org/en-US/docs/Web/API/Body/text - ) - .then(response => applyAfterwares({ - response, - options, - }, batched)) - .then(({ response }) => { - if (response.parsed) { - if (batched) { - if (Array.isArray(response.parsed)) { - return response.parsed as FetchResult[]; - } else { - throwBatchError(response); - } + return applyMiddlewares(requestObject, batched) + .then((reqOpts) => { + const construct = (constructOptions || constructDefaultOptions); + const requestOrRequests = ((reqOpts).request || (reqOpts).requests); + return construct(requestOrRequests, reqOpts.options); + }) + .then(opts => { + options = { ...opts }; + return (customFetch || fetch)(_uri, options); + }) + .then(response => response.text().then(raw => { + try { + const parsed = JSON.parse(raw); + (response as ParsedResponse).raw = raw; + (response as ParsedResponse).parsed = parsed; + return response; + } catch (e) { + parseError = e; + + //pass parsed raw response onto afterware + (response as ParsedResponse).raw = raw; + return response; + } + }), + //.catch() this should never happen: https://developer.mozilla.org/en-US/docs/Web/API/Body/text + ) + .then(response => applyAfterwares({ + response, + options, + }, batched)) + .then(({ response }) => { + if (response.parsed) { + if (batched) { + if (Array.isArray(response.parsed)) { + return response.parsed as FetchResult[]; } else { - return { ...response.parsed }; + throwBatchError(response); } } else { - throwHttpError(response, parseError); + return { ...response.parsed }; } - }); - }, - { - use: (middleware: MiddlewareInterface) => { - if (typeof middleware === 'function') { - middlewares.push(middleware); } else { - throw new Error('Middleware must be a function'); + throwHttpError(response, parseError); } + }); + }; - return apolloFetch; - }, - useAfter: (afterware: AfterwareInterface) => { - if (typeof afterware === 'function') { - afterwares.push(afterware); - } else { - throw new Error('Afterware must be a function'); - } + (apolloFetch as any).use = (middleware: MiddlewareInterface) => { + if (typeof middleware === 'function') { + middlewares.push(middleware); + } else { + throw new Error('Middleware must be a function'); + } - return apolloFetch; - }, - batchUse: (middleware: BatchMiddlewareInterface) => { - if (typeof middleware === 'function') { - batchedMiddlewares.push(middleware); - } else { - throw new Error('Middleware must be a function'); - } + return apolloFetch; + }; - return apolloFetch; - }, - batchUseAfter: (afterware: BatchAfterwareInterface) => { - if (typeof afterware === 'function') { - batchedAfterwares.push(afterware); - } else { - throw new Error('Afterware must be a function'); - } + (apolloFetch as any).useAfter = (afterware: AfterwareInterface) => { + if (typeof afterware === 'function') { + afterwares.push(afterware); + } else { + throw new Error('Afterware must be a function'); + } - return apolloFetch; - }, - }, - ); + return apolloFetch; + }; + + (apolloFetch as any).batchUse = (middleware: BatchMiddlewareInterface) => { + if (typeof middleware === 'function') { + batchedMiddlewares.push(middleware); + } else { + throw new Error('Middleware must be a function'); + } + + return apolloFetch; + }; + + (apolloFetch as any).batchUseAfter = (afterware: BatchAfterwareInterface) => { + if (typeof afterware === 'function') { + batchedAfterwares.push(afterware); + } else { + throw new Error('Afterware must be a function'); + } + + return apolloFetch; + }; return apolloFetch as ApolloFetch; } From 7c08d177aea235a16073e69a08ba3dc61ed0b9fd Mon Sep 17 00:00:00 2001 From: Evans Hauser Date: Tue, 25 Jul 2017 17:52:40 -0700 Subject: [PATCH 2/5] Publish - apollo-fetch@0.4.0 --- packages/apollo-fetch/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/apollo-fetch/package.json b/packages/apollo-fetch/package.json index 44c0c17e..2de46e32 100644 --- a/packages/apollo-fetch/package.json +++ b/packages/apollo-fetch/package.json @@ -1,6 +1,6 @@ { "name": "apollo-fetch", - "version": "0.3.0", + "version": "0.4.0", "description": "Lightweight implementation of fetch for GraphQL requests", "author": "Evans Hauser ", "contributors": [ From bc7caee346e3e50a496b0b4b5e1cc9bba98bfdac Mon Sep 17 00:00:00 2001 From: Evans Hauser Date: Tue, 25 Jul 2017 22:32:31 -0700 Subject: [PATCH 3/5] Added batching and lerna to CHANGELOG --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e006f7d7..e4220ab1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### vNEXT +### 0.4.0 + - Move repository to lerna project [PR #10](https://github.com/apollographql/apollo-fetch/pull/10) - Support batched requests [PR #10](https://github.com/apollographql/apollo-fetch/pull/10) From 0f6d42598039f3519cb0a3b8fa02ef2908eeaf65 Mon Sep 17 00:00:00 2001 From: James Baxley Date: Mon, 24 Jul 2017 09:40:25 -0400 Subject: [PATCH 4/5] run prettier on files --- .gitignore | 6 +- package.json | 23 +- packages/apollo-fetch/package.json | 4 +- packages/apollo-fetch/src/apollo-fetch.ts | 104 +++--- packages/apollo-fetch/src/index.ts | 11 +- packages/apollo-fetch/src/types.ts | 25 +- packages/apollo-fetch/tests/apollo-fetch.ts | 358 ++++++++++---------- 7 files changed, 288 insertions(+), 243 deletions(-) diff --git a/.gitignore b/.gitignore index ff712e53..db6e80cf 100644 --- a/.gitignore +++ b/.gitignore @@ -47,4 +47,8 @@ jspm_packages .yarn-integrity # -dist \ No newline at end of file +dist + +# lockfiles +yarn.lock +package-lock.json diff --git a/package.json b/package.json index 71f28639..e257ba8d 100644 --- a/package.json +++ b/package.json @@ -5,9 +5,21 @@ "type": "git", "url": "git+https://github.com/apollographql/apollo-fetch.git" }, - "scripts":{ - "coverage" : "nyc --reporter=lcov lerna run -- coverage:test" + "scripts": { + "bootstrap": "npm i && lerna bootstrap", + "test": "lerna run -- test", + "lint-fix": "prettier --trailing-comma all --single-quote --write \"packages/*/{src,tests,benchmark}/**/*.{j,t}s*\"", + "lint-staged": "lint-staged", + "coverage": "nyc --reporter=lcov lerna run -- coverage:test" }, + "lint-staged": { + "*.ts*": [ + "prettier --trailing-comma all --single-quote --write", + "git add" + ], + "*.json*": ["prettier --write", "git add"] + }, + "pre-commit": "lint-staged", "devDependencies": { "@types/chai": "^4.0.0", "@types/chai-as-promised": "0.0.31", @@ -19,9 +31,12 @@ "graphql": "^0.10.3", "graphql-tag": "^2.4.2", "lerna": "^2.0.0", + "lint-staged": "^4.0.2", "lodash": "^4.17.4", "mocha": "^3.2.0", "nyc": "^11.0.3", + "pre-commit": "^1.2.2", + "prettier": "^1.5.3", "rimraf": "^2.5.4", "sinon": "^2.3.4", "source-map-support": "^0.4.5", @@ -29,9 +44,7 @@ "typescript": "^2.2.1" }, "nyc": { - "exclude": [ - "**/tests" - ] + "exclude": ["**/tests"] }, "//": { "//": "lerna run