Repository URL to install this package:
|
Version:
7.1.2 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const apollo_datasource_rest_1 = require("apollo-datasource-rest");
const exotic_1 = require("exotic");
const fromOneRequestToSerialized_1 = require("../deps/fromOneRequestToSerialized");
const config_1 = require("../config");
function fromStoreToCacheOptions(store) {
if (store.has('cache') === false) {
return config_1.config.get('defaultCache');
}
else if (store.get('cache') === false) {
return undefined;
}
else {
return store.get('cache');
}
}
/**
* @todo may want to set this in `willSendRequest`?
* @todo support if headers already has `Content-Type`?
*/
function setContentType(headers) {
// @todo split
// === compat: 7.5 ===
if (config_1.config.get('shouldEncodeGetWithFormBody') === true) {
headers.set('Content-Type', 'application/x-www-form-urlencoded');
}
else {
headers.set('Content-Type', 'application/json');
}
}
class OneRequestApolloDataSource extends apollo_datasource_rest_1.RESTDataSource {
// oneRequest: OneRequest
cacheOptionsFor(response, request) {
const cacheOption = fromStoreToCacheOptions(this.store);
config_1.config
.get('logger')
.info('cache for: ' + request.url + JSON.stringify(cacheOption));
return cacheOption;
}
cacheKeyFor(request) {
const COOKIE_NAMESPACE_FIX_GET_FROM_ENV = process.env.GUEST_COOKIE_NAMESPACE || 'ckcjeu_';
const COOKIE_NAMESPACE_REGISTERED_FIX_GET_FROM_ENV = process.env.REGISTERED_COOKIE_NAMESPACE || 'ckcjeustat_';
const SESSION_ID_NAMESPACE = process.env.SESSION_ID_NAMESPACE || 'x-sk-session-id';
const registered = request.headers.get(COOKIE_NAMESPACE_REGISTERED_FIX_GET_FROM_ENV) || '';
const sessionId = request.headers.get(SESSION_ID_NAMESPACE) || '';
const guest = request.headers.get(COOKIE_NAMESPACE_FIX_GET_FROM_ENV) || '';
return guest + registered + sessionId + request.url;
}
// @todo should add logging hook here.....
// @note - this prevents errors from being thrown
// which may have unintended side effects
didReceiveResponse(response, request) {
// console.debug('[1request]')
// console.log({ response, request })
// console.debug('=== \n\n')
return response;
}
// requestWillMount / requestWillCreate
// just here for debugging
// async willSendRequest(request: RequestOptions): Promise<void> {
// if (process.env.NODE_ENV === 'production') return
// const url = await this.resolveURL(request)
// for (const [name, value] of request.params)
// url.searchParams.append(name, value)
// console.info('[1request] willSendRequest: ')
// console.log({ request, url: url.href })
// }
setup(store) {
this.store = store;
this.debugName = this.store.get('named');
this.baseURL = this.store.get('baseUrl');
return this;
}
params(params) {
console.warn('[apolloRestDataSource]:params', params);
return this;
}
path(path) {
console.warn('[apolloRestDataSource]:path', path);
return this;
}
get headers() {
return this.store.get('headers');
}
willSendRequest(request) {
const oneRequestAsObj = exotic_1.fromMapToObj(this.store);
const { params } = oneRequestAsObj;
if (params) {
Object.keys(params).forEach(key => {
request.params.set(key, exotic_1.isString(params[key]) ? params[key] : JSON.stringify(params[key]));
});
}
}
toDebug() {
const oneRequestAsObj = exotic_1.fromMapToObj(this.store);
const { headers, params, method, body } = oneRequestAsObj;
setContentType(headers);
const serializedHeaders = fromOneRequestToSerialized_1.fromHeadersToSerialized(headers);
const finalPath = fromOneRequestToSerialized_1.fromParamsToSerializedUrlParams(params, this.store);
const debugArgs = {
headers: serializedHeaders,
finalPath: finalPath.href,
params,
method,
body,
};
return debugArgs;
}
doRequest() {
const oneRequestAsObj = exotic_1.fromMapToObj(this.store);
const { headers, urlObj, method, body } = oneRequestAsObj;
setContentType(headers);
const serializedHeaders = fromOneRequestToSerialized_1.fromHeadersToSerialized(headers);
config_1.config
.get('logger')
.info('[1request] doRequest (apolloRestDataSource): ', this.toDebug());
const args = {
method,
path: urlObj.href,
body: undefined,
headers: serializedHeaders,
};
if (method.toLowerCase() === 'post' || method.toLowerCase() === 'patch') {
config_1.config.get('logger').debug('[1request] using patch / post');
args.body = body;
}
// this `as any` is because fetch is marked as private, so we bypass that
return this.fetch(args);
// === when forking ===
// return this._fetch(args)
// === actual request ===
// const secondArg = method === 'post' ? body : params
// return this[method.toLowerCase()](path, secondArg, { headers })
// === body on get ===
// const args = { method, path, params, body, headers }
// return (this as any).fetch(args)
}
}
exports.OneRequestApolloDataSource = OneRequestApolloDataSource;
/**
* @alias asRestDataSource
*/
function apolloRestDataSource(store) {
store
.get('context')
.log.debug('[1request] doRequest (adaptRequest - apolloRestDataSource): ');
return new OneRequestApolloDataSource().setup(store);
}
exports.apolloRestDataSource = apolloRestDataSource;
//# sourceMappingURL=apollo.js.map