2017-07-20 01:55:40 +00:00
|
|
|
// @flow
|
|
|
|
import type {Plugin} from '../';
|
|
|
|
import type {RenderedRequest} from '../../common/render';
|
|
|
|
import * as misc from '../../common/misc';
|
|
|
|
|
2017-08-21 17:34:25 +00:00
|
|
|
export function init (
|
|
|
|
plugin: Plugin,
|
|
|
|
renderedRequest: RenderedRequest,
|
|
|
|
renderedContext: Object
|
|
|
|
): {request: Object} {
|
2017-07-20 01:55:40 +00:00
|
|
|
if (!renderedRequest) {
|
|
|
|
throw new Error('contexts.request initialized without request');
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
request: {
|
|
|
|
getId (): string {
|
|
|
|
return renderedRequest._id;
|
|
|
|
},
|
|
|
|
getName (): string {
|
|
|
|
return renderedRequest.name;
|
|
|
|
},
|
|
|
|
getUrl (): string {
|
|
|
|
// TODO: Get full URL, including querystring
|
|
|
|
return renderedRequest.url;
|
|
|
|
},
|
|
|
|
getMethod (): string {
|
|
|
|
return renderedRequest.method;
|
|
|
|
},
|
|
|
|
getHeader (name: string): string | null {
|
|
|
|
const headers = misc.filterHeaders(renderedRequest.headers, name);
|
|
|
|
if (headers.length) {
|
|
|
|
// Use the last header if there are multiple of the same
|
|
|
|
const header = headers[headers.length - 1];
|
|
|
|
return header.value || '';
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
2017-08-21 17:34:25 +00:00
|
|
|
getHeaders (): Array<{name: string, value: string}> {
|
|
|
|
return renderedRequest.headers.map(h => ({ name: h.name, value: h.value }));
|
|
|
|
},
|
2017-07-20 01:55:40 +00:00
|
|
|
hasHeader (name: string): boolean {
|
|
|
|
return this.getHeader(name) !== null;
|
|
|
|
},
|
|
|
|
removeHeader (name: string): void {
|
|
|
|
const headers = misc.filterHeaders(renderedRequest.headers, name);
|
|
|
|
renderedRequest.headers = renderedRequest.headers.filter(
|
|
|
|
h => !headers.includes(h)
|
|
|
|
);
|
|
|
|
},
|
|
|
|
setHeader (name: string, value: string): void {
|
|
|
|
const header = misc.filterHeaders(renderedRequest.headers, name)[0];
|
|
|
|
if (header) {
|
|
|
|
header.value = value;
|
|
|
|
} else {
|
|
|
|
this.addHeader(name, value);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
addHeader (name: string, value: string): void {
|
|
|
|
const header = misc.filterHeaders(renderedRequest.headers, name)[0];
|
|
|
|
if (!header) {
|
|
|
|
renderedRequest.headers.push({name, value});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
setCookie (name: string, value: string): void {
|
|
|
|
const cookie = renderedRequest.cookies.find(c => c.name === name);
|
|
|
|
if (cookie) {
|
|
|
|
cookie.value = value;
|
|
|
|
} else {
|
|
|
|
renderedRequest.cookies.push({name, value});
|
|
|
|
}
|
2017-08-21 17:34:25 +00:00
|
|
|
},
|
|
|
|
getEnvironmentVariable (name: string): string | number | boolean | Object | Array<any> | null {
|
|
|
|
return renderedContext[name];
|
|
|
|
},
|
2017-08-21 17:40:26 +00:00
|
|
|
getEnvironment (): Object {
|
2017-08-21 17:34:25 +00:00
|
|
|
return renderedContext;
|
2017-07-20 01:55:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: For these to make sense, we'd need to account for cookies in the jar as well
|
|
|
|
// addCookie (name: string, value: string): void {}
|
|
|
|
// getCookie (name: string): string | null {}
|
|
|
|
// removeCookie (name: string): void {}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|