apollo-link-http-common
Advanced tools
Comparing version 0.2.3 to 0.2.4
@@ -5,2 +5,5 @@ # Change log | ||
### 0.2.4 | ||
- Update apollo-link [#559](https://github.com/apollographql/apollo-link/pull/559) | ||
### v0.2.3 | ||
@@ -7,0 +10,0 @@ - correct the warning message on no fetch found to node-fetch[PR#526](https://github.com/apollographql/apollo-link/pull/526) |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('graphql/language/printer')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'graphql/language/printer'], factory) : | ||
(factory((global.apolloLink = global.apolloLink || {}, global.apolloLink.httpCommon = {}),global.printer)); | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('graphql/language/printer')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'graphql/language/printer'], factory) : | ||
(factory((global.apolloLink = global.apolloLink || {}, global.apolloLink.httpCommon = {}),global.printer)); | ||
}(this, (function (exports,printer) { 'use strict'; | ||
var __assign = (undefined && undefined.__assign) || Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
var defaultHttpOptions = { | ||
includeQuery: true, | ||
includeExtensions: false, | ||
}; | ||
var defaultHeaders = { | ||
accept: '*/*', | ||
'content-type': 'application/json', | ||
}; | ||
var defaultOptions = { | ||
method: 'POST', | ||
}; | ||
var fallbackHttpConfig = { | ||
http: defaultHttpOptions, | ||
headers: defaultHeaders, | ||
options: defaultOptions, | ||
}; | ||
var throwServerError = function (response, result, message) { | ||
var error = new Error(message); | ||
error.response = response; | ||
error.statusCode = response.status; | ||
error.result = result; | ||
throw error; | ||
}; | ||
var parseAndCheckHttpResponse = function (operations) { return function (response) { | ||
return (response | ||
.text() | ||
.then(function (bodyText) { | ||
var __assign = (undefined && undefined.__assign) || Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
var defaultHttpOptions = { | ||
includeQuery: true, | ||
includeExtensions: false, | ||
}; | ||
var defaultHeaders = { | ||
// headers are case insensitive (https://stackoverflow.com/a/5259004) | ||
accept: '*/*', | ||
'content-type': 'application/json', | ||
}; | ||
var defaultOptions = { | ||
method: 'POST', | ||
}; | ||
var fallbackHttpConfig = { | ||
http: defaultHttpOptions, | ||
headers: defaultHeaders, | ||
options: defaultOptions, | ||
}; | ||
var throwServerError = function (response, result, message) { | ||
var error = new Error(message); | ||
error.response = response; | ||
error.statusCode = response.status; | ||
error.result = result; | ||
throw error; | ||
}; | ||
//TODO: when conditional types come in ts 2.8, operations should be a generic type that extends Operation | Array<Operation> | ||
var parseAndCheckHttpResponse = function (operations) { return function (response) { | ||
return (response | ||
.text() | ||
.then(function (bodyText) { | ||
try { | ||
return JSON.parse(bodyText); | ||
} | ||
catch (err) { | ||
var parseError = err; | ||
parseError.response = response; | ||
parseError.statusCode = response.status; | ||
parseError.bodyText = bodyText; | ||
return Promise.reject(parseError); | ||
} | ||
}) | ||
.then(function (result) { | ||
if (response.status >= 300) { | ||
//Network error | ||
throwServerError(response, result, "Response not successful: Received status code " + response.status); | ||
} | ||
//TODO should really error per response in a Batch based on properties | ||
// - could be done in a validation link | ||
if (!Array.isArray(result) && | ||
!result.hasOwnProperty('data') && | ||
!result.hasOwnProperty('errors')) { | ||
//Data error | ||
throwServerError(response, result, "Server response was missing for query '" + (Array.isArray(operations) | ||
? operations.map(function (op) { return op.operationName; }) | ||
: operations.operationName) + "'."); | ||
} | ||
return result; | ||
})); | ||
}; }; | ||
var checkFetcher = function (fetcher) { | ||
if (!fetcher && typeof fetch === 'undefined') { | ||
var library = 'unfetch'; | ||
if (typeof window === 'undefined') | ||
library = 'node-fetch'; | ||
throw new Error("\nfetch is not found globally and no fetcher passed, to fix pass a fetch for\nyour environment like https://www.npmjs.com/package/" + library + ".\n\nFor example:\nimport fetch from '" + library + "';\nimport { createHttpLink } from 'apollo-link-http';\n\nconst link = createHttpLink({ uri: '/graphql', fetch: fetch });"); | ||
} | ||
}; | ||
var createSignalIfSupported = function () { | ||
if (typeof AbortController === 'undefined') | ||
return { controller: false, signal: false }; | ||
var controller = new AbortController(); | ||
var signal = controller.signal; | ||
return { controller: controller, signal: signal }; | ||
}; | ||
var selectHttpOptionsAndBody = function (operation, fallbackConfig) { | ||
var configs = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
configs[_i - 2] = arguments[_i]; | ||
} | ||
var options = __assign({}, fallbackConfig.options, { headers: fallbackConfig.headers, credentials: fallbackConfig.credentials }); | ||
var http = fallbackConfig.http; | ||
/* | ||
* use the rest of the configs to populate the options | ||
* configs later in the list will overwrite earlier fields | ||
*/ | ||
configs.forEach(function (config) { | ||
options = __assign({}, options, config.options, { headers: __assign({}, options.headers, config.headers) }); | ||
if (config.credentials) | ||
options.credentials = config.credentials; | ||
http = __assign({}, http, config.http); | ||
}); | ||
//The body depends on the http options | ||
var operationName = operation.operationName, extensions = operation.extensions, variables = operation.variables, query = operation.query; | ||
var body = { operationName: operationName, variables: variables }; | ||
if (http.includeExtensions) | ||
body.extensions = extensions; | ||
// not sending the query (i.e persisted queries) | ||
if (http.includeQuery) | ||
body.query = printer.print(query); | ||
return { | ||
options: options, | ||
body: body, | ||
}; | ||
}; | ||
var serializeFetchParameter = function (p, label) { | ||
var serialized; | ||
try { | ||
return JSON.parse(bodyText); | ||
serialized = JSON.stringify(p); | ||
} | ||
catch (err) { | ||
var parseError = err; | ||
parseError.response = response; | ||
parseError.statusCode = response.status; | ||
parseError.bodyText = bodyText; | ||
return Promise.reject(parseError); | ||
catch (e) { | ||
var parseError = new Error("Network request failed. " + label + " is not serializable: " + e.message); | ||
parseError.parseError = e; | ||
throw parseError; | ||
} | ||
}) | ||
.then(function (result) { | ||
if (response.status >= 300) { | ||
throwServerError(response, result, "Response not successful: Received status code " + response.status); | ||
return serialized; | ||
}; | ||
//selects "/graphql" by default | ||
var selectURI = function (operation, fallbackURI) { | ||
var context = operation.getContext(); | ||
var contextURI = context.uri; | ||
if (contextURI) { | ||
return contextURI; | ||
} | ||
if (!Array.isArray(result) && | ||
!result.hasOwnProperty('data') && | ||
!result.hasOwnProperty('errors')) { | ||
throwServerError(response, result, "Server response was missing for query '" + (Array.isArray(operations) | ||
? operations.map(function (op) { return op.operationName; }) | ||
: operations.operationName) + "'."); | ||
else if (typeof fallbackURI === 'function') { | ||
return fallbackURI(operation); | ||
} | ||
return result; | ||
})); | ||
}; }; | ||
var checkFetcher = function (fetcher) { | ||
if (!fetcher && typeof fetch === 'undefined') { | ||
var library = 'unfetch'; | ||
if (typeof window === 'undefined') | ||
library = 'node-fetch'; | ||
throw new Error("\nfetch is not found globally and no fetcher passed, to fix pass a fetch for\nyour environment like https://www.npmjs.com/package/" + library + ".\n\nFor example:\nimport fetch from '" + library + "';\nimport { createHttpLink } from 'apollo-link-http';\n\nconst link = createHttpLink({ uri: '/graphql', fetch: fetch });"); | ||
} | ||
}; | ||
var createSignalIfSupported = function () { | ||
if (typeof AbortController === 'undefined') | ||
return { controller: false, signal: false }; | ||
var controller = new AbortController(); | ||
var signal = controller.signal; | ||
return { controller: controller, signal: signal }; | ||
}; | ||
var selectHttpOptionsAndBody = function (operation, fallbackConfig) { | ||
var configs = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
configs[_i - 2] = arguments[_i]; | ||
} | ||
var options = __assign({}, fallbackConfig.options, { headers: fallbackConfig.headers, credentials: fallbackConfig.credentials }); | ||
var http = fallbackConfig.http; | ||
configs.forEach(function (config) { | ||
options = __assign({}, options, config.options, { headers: __assign({}, options.headers, config.headers) }); | ||
if (config.credentials) | ||
options.credentials = config.credentials; | ||
http = __assign({}, http, config.http); | ||
}); | ||
var operationName = operation.operationName, extensions = operation.extensions, variables = operation.variables, query = operation.query; | ||
var body = { operationName: operationName, variables: variables }; | ||
if (http.includeExtensions) | ||
body.extensions = extensions; | ||
if (http.includeQuery) | ||
body.query = printer.print(query); | ||
return { | ||
options: options, | ||
body: body, | ||
else { | ||
return fallbackURI || '/graphql'; | ||
} | ||
}; | ||
}; | ||
var serializeFetchParameter = function (p, label) { | ||
var serialized; | ||
try { | ||
serialized = JSON.stringify(p); | ||
} | ||
catch (e) { | ||
var parseError = new Error("Network request failed. " + label + " is not serializable: " + e.message); | ||
parseError.parseError = e; | ||
throw parseError; | ||
} | ||
return serialized; | ||
}; | ||
var selectURI = function (operation, fallbackURI) { | ||
var context = operation.getContext(); | ||
var contextURI = context.uri; | ||
if (contextURI) { | ||
return contextURI; | ||
} | ||
else if (typeof fallbackURI === 'function') { | ||
return fallbackURI(operation); | ||
} | ||
else { | ||
return fallbackURI || '/graphql'; | ||
} | ||
}; | ||
exports.fallbackHttpConfig = fallbackHttpConfig; | ||
exports.throwServerError = throwServerError; | ||
exports.parseAndCheckHttpResponse = parseAndCheckHttpResponse; | ||
exports.checkFetcher = checkFetcher; | ||
exports.createSignalIfSupported = createSignalIfSupported; | ||
exports.selectHttpOptionsAndBody = selectHttpOptionsAndBody; | ||
exports.serializeFetchParameter = serializeFetchParameter; | ||
exports.selectURI = selectURI; | ||
exports.fallbackHttpConfig = fallbackHttpConfig; | ||
exports.throwServerError = throwServerError; | ||
exports.parseAndCheckHttpResponse = parseAndCheckHttpResponse; | ||
exports.checkFetcher = checkFetcher; | ||
exports.createSignalIfSupported = createSignalIfSupported; | ||
exports.selectHttpOptionsAndBody = selectHttpOptionsAndBody; | ||
exports.serializeFetchParameter = serializeFetchParameter; | ||
exports.selectURI = selectURI; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
}))); | ||
//# sourceMappingURL=bundle.umd.js.map |
@@ -35,7 +35,29 @@ import { Operation } from 'apollo-link'; | ||
export interface HttpOptions { | ||
/** | ||
* The URI to use when fetching operations. | ||
* | ||
* Defaults to '/graphql'. | ||
*/ | ||
uri?: string | UriFunction; | ||
/** | ||
* Passes the extensions field to your graphql server. | ||
* | ||
* Defaults to false. | ||
*/ | ||
includeExtensions?: boolean; | ||
/** | ||
* A `fetch`-compatible API to use when making requests. | ||
*/ | ||
fetch?: GlobalFetch['fetch']; | ||
/** | ||
* An object representing values to be sent as headers on the request. | ||
*/ | ||
headers?: any; | ||
/** | ||
* The credentials policy you want to use for the fetch call. | ||
*/ | ||
credentials?: string; | ||
/** | ||
* Any overrides of the fetch options argument to pass to the fetch call. | ||
*/ | ||
fetchOptions?: any; | ||
@@ -42,0 +64,0 @@ } |
@@ -15,2 +15,3 @@ var __assign = (this && this.__assign) || Object.assign || function(t) { | ||
var defaultHeaders = { | ||
// headers are case insensitive (https://stackoverflow.com/a/5259004) | ||
accept: '*/*', | ||
@@ -34,2 +35,3 @@ 'content-type': 'application/json', | ||
}; | ||
//TODO: when conditional types come in ts 2.8, operations should be a generic type that extends Operation | Array<Operation> | ||
export var parseAndCheckHttpResponse = function (operations) { return function (response) { | ||
@@ -52,7 +54,11 @@ return (response | ||
if (response.status >= 300) { | ||
//Network error | ||
throwServerError(response, result, "Response not successful: Received status code " + response.status); | ||
} | ||
//TODO should really error per response in a Batch based on properties | ||
// - could be done in a validation link | ||
if (!Array.isArray(result) && | ||
!result.hasOwnProperty('data') && | ||
!result.hasOwnProperty('errors')) { | ||
//Data error | ||
throwServerError(response, result, "Server response was missing for query '" + (Array.isArray(operations) | ||
@@ -87,2 +93,6 @@ ? operations.map(function (op) { return op.operationName; }) | ||
var http = fallbackConfig.http; | ||
/* | ||
* use the rest of the configs to populate the options | ||
* configs later in the list will overwrite earlier fields | ||
*/ | ||
configs.forEach(function (config) { | ||
@@ -94,2 +104,3 @@ options = __assign({}, options, config.options, { headers: __assign({}, options.headers, config.headers) }); | ||
}); | ||
//The body depends on the http options | ||
var operationName = operation.operationName, extensions = operation.extensions, variables = operation.variables, query = operation.query; | ||
@@ -99,2 +110,3 @@ var body = { operationName: operationName, variables: variables }; | ||
body.extensions = extensions; | ||
// not sending the query (i.e persisted queries) | ||
if (http.includeQuery) | ||
@@ -119,2 +131,3 @@ body.query = print(query); | ||
}; | ||
//selects "/graphql" by default | ||
export var selectURI = function (operation, fallbackURI) { | ||
@@ -121,0 +134,0 @@ var context = operation.getContext(); |
{ | ||
"name": "apollo-link-http-common", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "Http utilities for Apollo Link shared across all links using http", | ||
@@ -32,3 +32,3 @@ "main": "./lib/bundle.umd.js", | ||
"dependencies": { | ||
"apollo-link": "^1.2.1" | ||
"apollo-link": "^1.2.2" | ||
}, | ||
@@ -39,16 +39,16 @@ "peerDependencies": { | ||
"devDependencies": { | ||
"@types/graphql": "0.12.4", | ||
"@types/jest": "21.1.10", | ||
"browserify": "14.5.0", | ||
"@types/graphql": "0.12.6", | ||
"@types/jest": "22.2.2", | ||
"browserify": "16.1.1", | ||
"fetch-mock": "5.13.1", | ||
"graphql": "0.13.1", | ||
"graphql-tag": "2.7.3", | ||
"jest": "21.2.1", | ||
"graphql": "0.13.2", | ||
"graphql-tag": "2.8.0", | ||
"jest": "22.4.3", | ||
"object-to-querystring": "1.0.8", | ||
"rimraf": "2.6.1", | ||
"rollup": "0.56.2", | ||
"rollup": "0.57.1", | ||
"ts-jest": "21.2.4", | ||
"tslint": "5.9.1", | ||
"typescript": "2.7.2", | ||
"uglify-js": "3.3.11" | ||
"uglify-js": "3.3.16" | ||
}, | ||
@@ -65,3 +65,3 @@ "repository": { | ||
"transform": { | ||
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js" | ||
".(ts|tsx)": "ts-jest" | ||
}, | ||
@@ -68,0 +68,0 @@ "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
46624
821
Updatedapollo-link@^1.2.2