apollo-angular-link-http
Version:
An Apollo Link to allow sending a single http request per operation.
107 lines (102 loc) • 4.03 kB
JavaScript
import { __extends, __decorate } from 'tslib';
import { Injectable, NgModule } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, ApolloLink } from 'apollo-link';
import { print } from 'graphql';
import { prioritize, mergeHeaders, fetch } from 'apollo-angular-link-http-common';
import { extractFiles } from 'extract-files';
// XXX find a better name for it
var HttpLinkHandler = /** @class */ (function (_super) {
__extends(HttpLinkHandler, _super);
function HttpLinkHandler(httpClient, options) {
var _this = _super.call(this) || this;
_this.httpClient = httpClient;
_this.options = options;
_this.requester = function (operation) {
return new Observable(function (observer) {
var context = operation.getContext();
// decides which value to pick, Context, Options or to just use the default
var pick = function (key, init) {
return prioritize(context[key], _this.options[key], init);
};
var includeQuery = pick('includeQuery', true);
var includeExtensions = pick('includeExtensions', false);
var method = pick('method', 'POST');
var url = pick('uri', 'graphql');
var withCredentials = pick('withCredentials');
var useMultipart = pick('useMultipart');
var req = {
method: method,
url: typeof url === 'function' ? url(operation) : url,
body: {
operationName: operation.operationName,
variables: operation.variables,
},
options: {
withCredentials: withCredentials,
useMultipart: useMultipart,
headers: _this.options.headers,
},
};
if (includeExtensions) {
req.body.extensions = operation.extensions;
}
if (includeQuery) {
req.body.query = print(operation.query);
}
if (context.headers) {
req.options.headers = mergeHeaders(req.options.headers, context.headers);
}
var sub = fetch(req, _this.httpClient, extractFiles).subscribe({
next: function (response) {
operation.setContext({ response: response });
observer.next(response.body);
},
error: function (err) { return observer.error(err); },
complete: function () { return observer.complete(); },
});
return function () {
if (!sub.closed) {
sub.unsubscribe();
}
};
});
};
return _this;
}
HttpLinkHandler.prototype.request = function (op) {
return this.requester(op);
};
return HttpLinkHandler;
}(ApolloLink));
var HttpLink = /** @class */ (function () {
function HttpLink(httpClient) {
this.httpClient = httpClient;
}
HttpLink.prototype.create = function (options) {
return new HttpLinkHandler(this.httpClient, options);
};
HttpLink.ctorParameters = function () { return [
{ type: HttpClient }
]; };
HttpLink = __decorate([
Injectable()
], HttpLink);
return HttpLink;
}());
var PROVIDERS = [HttpLink];
var HttpLinkModule = /** @class */ (function () {
function HttpLinkModule() {
}
HttpLinkModule = __decorate([
NgModule({
providers: PROVIDERS,
})
], HttpLinkModule);
return HttpLinkModule;
}());
/**
* Generated bundle index. Do not edit.
*/
export { HttpLink, HttpLinkHandler, HttpLinkModule, PROVIDERS };
//# sourceMappingURL=ngApolloLinkHttp.js.map