@ekino/rendr-core
Advanced tools
Comparing version 0.0.21-alpha.1 to 1.0.0-alpha.0
@@ -20,5 +20,7 @@ /// <reference types="node" /> | ||
} | ||
export class ClientSideError extends RendrError { | ||
constructor(...args: any[]); | ||
} | ||
} | ||
declare module "types" { | ||
import { IncomingMessage, ServerResponse } from "http"; | ||
import { Writable } from "stream"; | ||
@@ -48,2 +50,13 @@ export interface BlockDefinition { | ||
} | ||
export type PageType = RedirectPage | ResponsePage | Page; | ||
export type Body = string | ReadableStream | NodeJS.ReadableStream | any; | ||
export class RedirectPage { | ||
statusCode: number; | ||
location: string; | ||
} | ||
export class ResponsePage { | ||
statusCode: number; | ||
headers: Map; | ||
body: Body; | ||
} | ||
export class Page { | ||
@@ -60,14 +73,22 @@ statusCode: number; | ||
} | ||
export interface RequestCtx { | ||
hostname: string; | ||
pathname: string; | ||
query: Map; | ||
params: Map; | ||
asPath: string; | ||
export interface RendrCtx { | ||
isServerSide: boolean; | ||
isClientSide: boolean; | ||
req: IncomingMessage; | ||
res?: ServerResponse; | ||
settings: Settings; | ||
req: RendrRequest; | ||
} | ||
export interface RendrRequest { | ||
hostname: string; | ||
headers: Map; | ||
query: Map; | ||
pathname: string; | ||
params: Map; | ||
body: Body; | ||
method: string; | ||
} | ||
export interface RendrResponse { | ||
headers: Map; | ||
body: Body; | ||
statusCode: number; | ||
} | ||
export type Normalizer = (entry: any) => BlockDefinition; | ||
@@ -98,8 +119,15 @@ export type NormalizerList = { | ||
} | ||
declare module "stream" { | ||
import { Writable } from "stream"; | ||
import { Page } from "index"; | ||
import { Body } from "types"; | ||
export function pipe(source: Body, dest: Writable): Promise<void>; | ||
export function pipePageToClient(source: Body): Promise<Page>; | ||
} | ||
declare module "index" { | ||
import { IncomingMessage, ServerResponse } from "http"; | ||
import { Page, Cache, Head, BlockDefinition, RequestCtx } from "types"; | ||
import { Page, Cache, Head, BlockDefinition, RendrCtx, RedirectPage, ResponsePage, Map, Body } from "types"; | ||
export * from "types"; | ||
export * from "errors"; | ||
export * from "generator"; | ||
export * from "stream"; | ||
export function isObject(data: any): boolean; | ||
@@ -112,6 +140,6 @@ export function isArray(data: any): boolean; | ||
export function createPage(data?: any): Page; | ||
export function createContext(req: IncomingMessage | { | ||
url: string; | ||
}, res?: ServerResponse): RequestCtx; | ||
export function createContext(url: string): RendrCtx; | ||
export function createRedirectPage(statusCode: number, location: string): RedirectPage; | ||
export function createResponsePage(statusCode: number, headers: Map, body: Body): ResponsePage; | ||
export function mergePages(pages: Page[]): Page; | ||
} |
@@ -103,3 +103,3 @@ var __extends = (this && this.__extends) || (function () { | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.NormalizationError = exports.InternalServerError = exports.NotFoundError = exports.RendrError = void 0; | ||
exports.ClientSideError = exports.NormalizationError = exports.InternalServerError = exports.NotFoundError = exports.RendrError = void 0; | ||
function getErrorParameters(defaultMessage, args) { | ||
@@ -184,2 +184,16 @@ if (args.length === 0) { | ||
exports.NormalizationError = NormalizationError; | ||
var ClientSideError = (function (_super) { | ||
__extends(ClientSideError, _super); | ||
function ClientSideError() { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var _this = _super.call(this) || this; | ||
Object.assign(_this, __assign(__assign({}, getErrorParameters("Client Side Error", args)), { stack: new Error().stack, name: "ClientSideError" })); | ||
return _this; | ||
} | ||
return ClientSideError; | ||
}(RendrError)); | ||
exports.ClientSideError = ClientSideError; | ||
}); | ||
@@ -189,3 +203,17 @@ define("types", ["require", "exports"], function (require, exports) { | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Page = void 0; | ||
exports.Page = exports.ResponsePage = exports.RedirectPage = void 0; | ||
var RedirectPage = (function () { | ||
function RedirectPage() { | ||
this.statusCode = 200; | ||
} | ||
return RedirectPage; | ||
}()); | ||
exports.RedirectPage = RedirectPage; | ||
var ResponsePage = (function () { | ||
function ResponsePage() { | ||
this.statusCode = 200; | ||
} | ||
return ResponsePage; | ||
}()); | ||
exports.ResponsePage = ResponsePage; | ||
var Page = (function () { | ||
@@ -386,6 +414,47 @@ function Page() { | ||
}); | ||
define("index", ["require", "exports", "http", "url-parse", "types", "types", "errors", "generator"], function (require, exports, http_1, url_parse_1, types_1, types_2, errors_1, generator_2) { | ||
define("stream", ["require", "exports", "stream", "index"], function (require, exports, stream_2, index_1) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.mergePages = exports.createContext = exports.createPage = exports.normalizeBlockDefinition = exports.normalizeBlockDefinitions = exports.normalizeHead = exports.normalizeCache = exports.isArray = exports.isObject = void 0; | ||
exports.pipePageToClient = exports.pipe = void 0; | ||
function pipe(source, dest) { | ||
if (typeof source == "string") { | ||
source = stream_2.Readable.from(source); | ||
} | ||
return new Promise(function (resolve, reject) { | ||
if (!(source instanceof stream_2.Readable)) { | ||
return reject("Not a Readable object"); | ||
} | ||
source.pipe(dest); | ||
source.on("end", function (err) { | ||
if (err) { | ||
reject(err); | ||
} | ||
else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
} | ||
exports.pipe = pipe; | ||
function pipePageToClient(source) { | ||
if (typeof source == "string") { | ||
source = stream_2.Readable.from(source); | ||
} | ||
var data = ""; | ||
var dest = new stream_2.Writable({ | ||
write: function (chunk, _, callback) { | ||
data += chunk; | ||
callback(); | ||
}, | ||
}); | ||
return pipe(source, dest).then(function () { | ||
return index_1.createPage(JSON.parse(data)); | ||
}); | ||
} | ||
exports.pipePageToClient = pipePageToClient; | ||
}); | ||
define("index", ["require", "exports", "url-parse", "types", "types", "errors", "generator", "stream"], function (require, exports, url_parse_1, types_1, types_2, errors_1, generator_2, stream_3) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.mergePages = exports.createResponsePage = exports.createRedirectPage = exports.createContext = exports.createPage = exports.normalizeBlockDefinition = exports.normalizeBlockDefinitions = exports.normalizeHead = exports.normalizeCache = exports.isArray = exports.isObject = void 0; | ||
url_parse_1 = __importDefault(url_parse_1); | ||
@@ -395,2 +464,3 @@ __exportStar(types_2, exports); | ||
__exportStar(generator_2, exports); | ||
__exportStar(stream_3, exports); | ||
function isObject(data) { | ||
@@ -494,26 +564,37 @@ return data !== null && typeof data === "object"; | ||
exports.createPage = createPage; | ||
function createContext(req, res) { | ||
var isServerSide = false; | ||
var fullUrl = req.url; | ||
var asPath = req.url; | ||
if (req instanceof http_1.IncomingMessage) { | ||
isServerSide = true; | ||
fullUrl = "https://" + req.headers["host"] + req.url; | ||
} | ||
var url = url_parse_1.default(fullUrl, true); | ||
var ctx = { | ||
hostname: url.hostname, | ||
pathname: url.pathname, | ||
query: url.query, | ||
function createContext(url) { | ||
var isServerSide = !(window instanceof Window); | ||
var urlInfo = url_parse_1.default(url, true); | ||
var request = { | ||
hostname: urlInfo.hostname, | ||
pathname: urlInfo.pathname, | ||
query: urlInfo.query, | ||
params: {}, | ||
asPath: asPath, | ||
headers: {}, | ||
body: "", | ||
method: "GET", | ||
}; | ||
return { | ||
isServerSide: isServerSide, | ||
isClientSide: !isServerSide, | ||
req: isServerSide ? req : null, | ||
res: isServerSide ? res : null, | ||
settings: {}, | ||
req: request, | ||
}; | ||
return ctx; | ||
} | ||
exports.createContext = createContext; | ||
function createRedirectPage(statusCode, location) { | ||
var redirect = new types_1.RedirectPage(); | ||
redirect.location = location; | ||
redirect.statusCode = statusCode; | ||
return redirect; | ||
} | ||
exports.createRedirectPage = createRedirectPage; | ||
function createResponsePage(statusCode, headers, body) { | ||
var response = new types_1.ResponsePage(); | ||
response.headers = headers; | ||
response.body = body; | ||
response.statusCode = statusCode; | ||
return response; | ||
} | ||
exports.createResponsePage = createResponsePage; | ||
function mergePages(pages) { | ||
@@ -520,0 +601,0 @@ var page = new types_1.Page(); |
@@ -18,1 +18,4 @@ export declare class RendrError { | ||
} | ||
export declare class ClientSideError extends RendrError { | ||
constructor(...args: any[]); | ||
} |
@@ -104,2 +104,16 @@ var __extends = (this && this.__extends) || (function () { | ||
export { NormalizationError }; | ||
var ClientSideError = (function (_super) { | ||
__extends(ClientSideError, _super); | ||
function ClientSideError() { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var _this = _super.call(this) || this; | ||
Object.assign(_this, __assign(__assign({}, getErrorParameters("Client Side Error", args)), { stack: new Error().stack, name: "ClientSideError" })); | ||
return _this; | ||
} | ||
return ClientSideError; | ||
}(RendrError)); | ||
export { ClientSideError }; | ||
//# sourceMappingURL=errors.js.map |
@@ -1,7 +0,6 @@ | ||
/// <reference types="node" /> | ||
import { IncomingMessage, ServerResponse } from "http"; | ||
import { Page, Cache, Head, BlockDefinition, RequestCtx } from "./types"; | ||
import { Page, Cache, Head, BlockDefinition, RendrCtx, RedirectPage, ResponsePage, Map, Body } from "./types"; | ||
export * from "./types"; | ||
export * from "./errors"; | ||
export * from "./generator"; | ||
export * from "./stream"; | ||
export declare function isObject(data: any): boolean; | ||
@@ -14,5 +13,5 @@ export declare function isArray(data: any): boolean; | ||
export declare function createPage(data?: any): Page; | ||
export declare function createContext(req: IncomingMessage | { | ||
url: string; | ||
}, res?: ServerResponse): RequestCtx; | ||
export declare function createContext(url: string): RendrCtx; | ||
export declare function createRedirectPage(statusCode: number, location: string): RedirectPage; | ||
export declare function createResponsePage(statusCode: number, headers: Map, body: Body): ResponsePage; | ||
export declare function mergePages(pages: Page[]): Page; |
@@ -19,8 +19,8 @@ var __assign = (this && this.__assign) || function () { | ||
}; | ||
import { IncomingMessage } from "http"; | ||
import parse from "url-parse"; | ||
import { Page } from "./types"; | ||
import { Page, RedirectPage, ResponsePage, } from "./types"; | ||
export * from "./types"; | ||
export * from "./errors"; | ||
export * from "./generator"; | ||
export * from "./stream"; | ||
export function isObject(data) { | ||
@@ -117,25 +117,34 @@ return data !== null && typeof data === "object"; | ||
} | ||
export function createContext(req, res) { | ||
var isServerSide = false; | ||
var fullUrl = req.url; | ||
var asPath = req.url; | ||
if (req instanceof IncomingMessage) { | ||
isServerSide = true; | ||
fullUrl = "https://" + req.headers["host"] + req.url; | ||
} | ||
var url = parse(fullUrl, true); | ||
var ctx = { | ||
hostname: url.hostname, | ||
pathname: url.pathname, | ||
query: url.query, | ||
export function createContext(url) { | ||
var isServerSide = !(window instanceof Window); | ||
var urlInfo = parse(url, true); | ||
var request = { | ||
hostname: urlInfo.hostname, | ||
pathname: urlInfo.pathname, | ||
query: urlInfo.query, | ||
params: {}, | ||
asPath: asPath, | ||
headers: {}, | ||
body: "", | ||
method: "GET", | ||
}; | ||
return { | ||
isServerSide: isServerSide, | ||
isClientSide: !isServerSide, | ||
req: isServerSide ? req : null, | ||
res: isServerSide ? res : null, | ||
settings: {}, | ||
req: request, | ||
}; | ||
return ctx; | ||
} | ||
export function createRedirectPage(statusCode, location) { | ||
var redirect = new RedirectPage(); | ||
redirect.location = location; | ||
redirect.statusCode = statusCode; | ||
return redirect; | ||
} | ||
export function createResponsePage(statusCode, headers, body) { | ||
var response = new ResponsePage(); | ||
response.headers = headers; | ||
response.body = body; | ||
response.statusCode = statusCode; | ||
return response; | ||
} | ||
export function mergePages(pages) { | ||
@@ -142,0 +151,0 @@ var page = new Page(); |
/// <reference types="node" /> | ||
import { IncomingMessage, ServerResponse } from "http"; | ||
import { Writable } from "stream"; | ||
@@ -27,2 +26,13 @@ export interface BlockDefinition { | ||
} | ||
export declare type PageType = RedirectPage | ResponsePage | Page; | ||
export declare type Body = string | ReadableStream | NodeJS.ReadableStream | any; | ||
export declare class RedirectPage { | ||
statusCode: number; | ||
location: string; | ||
} | ||
export declare class ResponsePage { | ||
statusCode: number; | ||
headers: Map; | ||
body: Body; | ||
} | ||
export declare class Page { | ||
@@ -39,14 +49,22 @@ statusCode: number; | ||
} | ||
export interface RequestCtx { | ||
hostname: string; | ||
pathname: string; | ||
query: Map; | ||
params: Map; | ||
asPath: string; | ||
export interface RendrCtx { | ||
isServerSide: boolean; | ||
isClientSide: boolean; | ||
req: IncomingMessage; | ||
res?: ServerResponse; | ||
settings: Settings; | ||
req: RendrRequest; | ||
} | ||
export interface RendrRequest { | ||
hostname: string; | ||
headers: Map; | ||
query: Map; | ||
pathname: string; | ||
params: Map; | ||
body: Body; | ||
method: string; | ||
} | ||
export interface RendrResponse { | ||
headers: Map; | ||
body: Body; | ||
statusCode: number; | ||
} | ||
export declare type Normalizer = (entry: any) => BlockDefinition; | ||
@@ -53,0 +71,0 @@ export declare type NormalizerList = { |
@@ -0,1 +1,15 @@ | ||
var RedirectPage = (function () { | ||
function RedirectPage() { | ||
this.statusCode = 200; | ||
} | ||
return RedirectPage; | ||
}()); | ||
export { RedirectPage }; | ||
var ResponsePage = (function () { | ||
function ResponsePage() { | ||
this.statusCode = 200; | ||
} | ||
return ResponsePage; | ||
}()); | ||
export { ResponsePage }; | ||
var Page = (function () { | ||
@@ -2,0 +16,0 @@ function Page() { |
@@ -18,1 +18,4 @@ export declare class RendrError { | ||
} | ||
export declare class ClientSideError extends RendrError { | ||
constructor(...args: any[]); | ||
} |
@@ -27,3 +27,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.NormalizationError = exports.InternalServerError = exports.NotFoundError = exports.RendrError = void 0; | ||
exports.ClientSideError = exports.NormalizationError = exports.InternalServerError = exports.NotFoundError = exports.RendrError = void 0; | ||
function getErrorParameters(defaultMessage, args) { | ||
@@ -108,2 +108,16 @@ if (args.length === 0) { | ||
exports.NormalizationError = NormalizationError; | ||
var ClientSideError = (function (_super) { | ||
__extends(ClientSideError, _super); | ||
function ClientSideError() { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var _this = _super.call(this) || this; | ||
Object.assign(_this, __assign(__assign({}, getErrorParameters("Client Side Error", args)), { stack: new Error().stack, name: "ClientSideError" })); | ||
return _this; | ||
} | ||
return ClientSideError; | ||
}(RendrError)); | ||
exports.ClientSideError = ClientSideError; | ||
//# sourceMappingURL=errors.js.map |
@@ -1,7 +0,6 @@ | ||
/// <reference types="node" /> | ||
import { IncomingMessage, ServerResponse } from "http"; | ||
import { Page, Cache, Head, BlockDefinition, RequestCtx } from "./types"; | ||
import { Page, Cache, Head, BlockDefinition, RendrCtx, RedirectPage, ResponsePage, Map, Body } from "./types"; | ||
export * from "./types"; | ||
export * from "./errors"; | ||
export * from "./generator"; | ||
export * from "./stream"; | ||
export declare function isObject(data: any): boolean; | ||
@@ -14,5 +13,5 @@ export declare function isArray(data: any): boolean; | ||
export declare function createPage(data?: any): Page; | ||
export declare function createContext(req: IncomingMessage | { | ||
url: string; | ||
}, res?: ServerResponse): RequestCtx; | ||
export declare function createContext(url: string): RendrCtx; | ||
export declare function createRedirectPage(statusCode: number, location: string): RedirectPage; | ||
export declare function createResponsePage(statusCode: number, headers: Map, body: Body): ResponsePage; | ||
export declare function mergePages(pages: Page[]): Page; |
@@ -34,4 +34,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.mergePages = exports.createContext = exports.createPage = exports.normalizeBlockDefinition = exports.normalizeBlockDefinitions = exports.normalizeHead = exports.normalizeCache = exports.isArray = exports.isObject = void 0; | ||
var http_1 = require("http"); | ||
exports.mergePages = exports.createResponsePage = exports.createRedirectPage = exports.createContext = exports.createPage = exports.normalizeBlockDefinition = exports.normalizeBlockDefinitions = exports.normalizeHead = exports.normalizeCache = exports.isArray = exports.isObject = void 0; | ||
var url_parse_1 = __importDefault(require("url-parse")); | ||
@@ -42,2 +41,3 @@ var types_1 = require("./types"); | ||
__exportStar(require("./generator"), exports); | ||
__exportStar(require("./stream"), exports); | ||
function isObject(data) { | ||
@@ -141,26 +141,37 @@ return data !== null && typeof data === "object"; | ||
exports.createPage = createPage; | ||
function createContext(req, res) { | ||
var isServerSide = false; | ||
var fullUrl = req.url; | ||
var asPath = req.url; | ||
if (req instanceof http_1.IncomingMessage) { | ||
isServerSide = true; | ||
fullUrl = "https://" + req.headers["host"] + req.url; | ||
} | ||
var url = url_parse_1.default(fullUrl, true); | ||
var ctx = { | ||
hostname: url.hostname, | ||
pathname: url.pathname, | ||
query: url.query, | ||
function createContext(url) { | ||
var isServerSide = !(window instanceof Window); | ||
var urlInfo = url_parse_1.default(url, true); | ||
var request = { | ||
hostname: urlInfo.hostname, | ||
pathname: urlInfo.pathname, | ||
query: urlInfo.query, | ||
params: {}, | ||
asPath: asPath, | ||
headers: {}, | ||
body: "", | ||
method: "GET", | ||
}; | ||
return { | ||
isServerSide: isServerSide, | ||
isClientSide: !isServerSide, | ||
req: isServerSide ? req : null, | ||
res: isServerSide ? res : null, | ||
settings: {}, | ||
req: request, | ||
}; | ||
return ctx; | ||
} | ||
exports.createContext = createContext; | ||
function createRedirectPage(statusCode, location) { | ||
var redirect = new types_1.RedirectPage(); | ||
redirect.location = location; | ||
redirect.statusCode = statusCode; | ||
return redirect; | ||
} | ||
exports.createRedirectPage = createRedirectPage; | ||
function createResponsePage(statusCode, headers, body) { | ||
var response = new types_1.ResponsePage(); | ||
response.headers = headers; | ||
response.body = body; | ||
response.statusCode = statusCode; | ||
return response; | ||
} | ||
exports.createResponsePage = createResponsePage; | ||
function mergePages(pages) { | ||
@@ -167,0 +178,0 @@ var page = new types_1.Page(); |
/// <reference types="node" /> | ||
import { IncomingMessage, ServerResponse } from "http"; | ||
import { Writable } from "stream"; | ||
@@ -27,2 +26,13 @@ export interface BlockDefinition { | ||
} | ||
export declare type PageType = RedirectPage | ResponsePage | Page; | ||
export declare type Body = string | ReadableStream | NodeJS.ReadableStream | any; | ||
export declare class RedirectPage { | ||
statusCode: number; | ||
location: string; | ||
} | ||
export declare class ResponsePage { | ||
statusCode: number; | ||
headers: Map; | ||
body: Body; | ||
} | ||
export declare class Page { | ||
@@ -39,14 +49,22 @@ statusCode: number; | ||
} | ||
export interface RequestCtx { | ||
hostname: string; | ||
pathname: string; | ||
query: Map; | ||
params: Map; | ||
asPath: string; | ||
export interface RendrCtx { | ||
isServerSide: boolean; | ||
isClientSide: boolean; | ||
req: IncomingMessage; | ||
res?: ServerResponse; | ||
settings: Settings; | ||
req: RendrRequest; | ||
} | ||
export interface RendrRequest { | ||
hostname: string; | ||
headers: Map; | ||
query: Map; | ||
pathname: string; | ||
params: Map; | ||
body: Body; | ||
method: string; | ||
} | ||
export interface RendrResponse { | ||
headers: Map; | ||
body: Body; | ||
statusCode: number; | ||
} | ||
export declare type Normalizer = (entry: any) => BlockDefinition; | ||
@@ -53,0 +71,0 @@ export declare type NormalizerList = { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Page = void 0; | ||
exports.Page = exports.ResponsePage = exports.RedirectPage = void 0; | ||
var RedirectPage = (function () { | ||
function RedirectPage() { | ||
this.statusCode = 200; | ||
} | ||
return RedirectPage; | ||
}()); | ||
exports.RedirectPage = RedirectPage; | ||
var ResponsePage = (function () { | ||
function ResponsePage() { | ||
this.statusCode = 200; | ||
} | ||
return ResponsePage; | ||
}()); | ||
exports.ResponsePage = ResponsePage; | ||
var Page = (function () { | ||
@@ -5,0 +19,0 @@ function Page() { |
{ | ||
"name": "@ekino/rendr-core", | ||
"license": "MIT", | ||
"version": "0.0.21-alpha.1", | ||
"version": "1.0.0-alpha.0", | ||
"main": "dist/lib/index.js", | ||
@@ -22,3 +22,3 @@ "typing": "dist/lib/index.d.ts", | ||
}, | ||
"gitHead": "99576698a64b2fd9a95a902461813552106588c4" | ||
"gitHead": "69fbb418c854bdafb58808fa25a6edc19552f4f5" | ||
} |
@@ -88,1 +88,12 @@ // https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work | ||
} | ||
export class ClientSideError extends RendrError { | ||
constructor(...args: any[]) { | ||
super(); | ||
Object.assign(this, { | ||
...getErrorParameters("Client Side Error", args), | ||
stack: new Error().stack, | ||
name: "ClientSideError", | ||
}); | ||
} | ||
} |
import { createPage, mergePages, Page, createContext } from "./index"; | ||
import { IncomingMessage } from "http"; | ||
@@ -114,7 +113,8 @@ describe("test create page", () => { | ||
it("test clientSide context", () => { | ||
const ctx = createContext({ url: "https://ekino.com/foobar?foo=bar" }); | ||
const ctx = createContext("https://ekino.com/foobar?foo=bar"); | ||
expect(ctx.isClientSide).toBeTruthy(); | ||
expect(ctx.isServerSide).toBeFalsy(); | ||
expect(ctx.isServerSide).toBeTruthy(); | ||
expect(ctx.isClientSide).toBeFalsy(); | ||
expect(ctx).toMatchSnapshot(); | ||
expect(ctx.req.pathname).toBe("/foobar"); | ||
@@ -126,7 +126,2 @@ // try to set a setting | ||
}); | ||
it("test server side context", () => { | ||
// need to think about a clean way of mocking the IncomingMessage... without | ||
// no much of boilerplate. | ||
}); | ||
}); |
@@ -1,5 +0,15 @@ | ||
import { IncomingMessage, ServerResponse } from "http"; | ||
import parse from "url-parse"; | ||
import { Page, Cache, Head, BlockDefinition, RequestCtx } from "./types"; | ||
import { | ||
Page, | ||
Cache, | ||
Head, | ||
BlockDefinition, | ||
RendrCtx, | ||
RendrRequest, | ||
RedirectPage, | ||
ResponsePage, | ||
Map, | ||
Body, | ||
} from "./types"; | ||
@@ -9,2 +19,3 @@ export * from "./types"; | ||
export * from "./generator"; | ||
export * from "./stream"; | ||
@@ -134,23 +145,13 @@ export function isObject(data: any): boolean { | ||
*/ | ||
export function createContext( | ||
req: IncomingMessage | { url: string }, | ||
res?: ServerResponse | ||
): RequestCtx { | ||
// default to client, why not! | ||
let isServerSide = false; | ||
let fullUrl = req.url; | ||
export function createContext(url: string): RendrCtx { | ||
// jest defines a set of global variable, for now, this | ||
// is the only reliable option found. | ||
let isServerSide = !(window instanceof Window); | ||
const asPath = req.url; | ||
const urlInfo = parse(url, true); | ||
if (req instanceof IncomingMessage) { | ||
isServerSide = true; | ||
fullUrl = `https://${req.headers["host"]}${req.url}`; | ||
} | ||
const url = parse(fullUrl, true); | ||
const ctx: RequestCtx = { | ||
hostname: url.hostname, | ||
pathname: url.pathname, | ||
query: url.query, | ||
const request: RendrRequest = { | ||
hostname: urlInfo.hostname, | ||
pathname: urlInfo.pathname, | ||
query: urlInfo.query, | ||
// params is empty because for now there are no | ||
@@ -160,15 +161,36 @@ // routing to analyse the pathname, this will be done later on | ||
params: {}, | ||
asPath, | ||
headers: {}, | ||
body: "", | ||
method: "GET", | ||
}; | ||
return { | ||
isServerSide, | ||
isClientSide: !isServerSide, | ||
// @ts-ignore | ||
req: isServerSide ? req : null, | ||
// @ts-ignore | ||
res: isServerSide ? res : null, | ||
settings: {}, | ||
req: request, | ||
}; | ||
} | ||
return ctx; | ||
export function createRedirectPage(statusCode: number, location: string) { | ||
const redirect = new RedirectPage(); | ||
redirect.location = location; | ||
redirect.statusCode = statusCode; | ||
return redirect; | ||
} | ||
export function createResponsePage( | ||
statusCode: number, | ||
headers: Map, | ||
body: Body | ||
) { | ||
const response = new ResponsePage(); | ||
response.headers = headers; | ||
response.body = body; | ||
response.statusCode = statusCode; | ||
return response; | ||
} | ||
// last page get the priority | ||
@@ -175,0 +197,0 @@ export function mergePages(pages: Page[]): Page { |
@@ -1,2 +0,1 @@ | ||
import { IncomingMessage, ServerResponse } from "http"; | ||
import { Writable } from "stream"; | ||
@@ -31,2 +30,16 @@ | ||
export type PageType = RedirectPage | ResponsePage | Page; | ||
export type Body = string | ReadableStream | NodeJS.ReadableStream | any; | ||
export class RedirectPage { | ||
statusCode: number = 200; | ||
location: string; | ||
} | ||
export class ResponsePage { | ||
statusCode: number = 200; | ||
headers: Map; | ||
body: Body; | ||
} | ||
// -- Page definition | ||
@@ -54,18 +67,25 @@ export class Page { | ||
// -- NextJs Controller signature | ||
export interface RequestCtx { | ||
hostname: string; // the hostname requested | ||
pathname: string; // the path - without the query string | ||
query: Map; // the query string, parsed | ||
params: Map; // the params from the routing, ie param from nice url | ||
asPath: string; // the complete path - pathname + query | ||
export interface RendrCtx { | ||
isServerSide: boolean; | ||
isClientSide: boolean; | ||
req: IncomingMessage; | ||
res?: ServerResponse; | ||
// anything that need to be set during the life time of the request, | ||
// this data will no be exposed on the end user. | ||
settings: Settings; | ||
req: RendrRequest; | ||
} | ||
export interface RendrRequest { | ||
hostname: string; // the hostname requested | ||
headers: Map; | ||
query: Map; // the query string, parsed | ||
pathname: string; // the path - without the query string | ||
params: Map; // the params from the routing, ie param from nice url | ||
body: Body; | ||
method: string; | ||
} | ||
export interface RendrResponse { | ||
headers: Map; | ||
body: Body; | ||
statusCode: number; | ||
} | ||
export type Normalizer = (entry: any) => BlockDefinition; | ||
@@ -72,0 +92,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
147739
49
2987
0