@ekino/rendr-loader
Advanced tools
Comparing version
declare module "types" { | ||
import { Page, RequestCtx } from "@ekino/rendr-core"; | ||
import { AxiosRequestConfig } from "axios"; | ||
export type MaybePage = Promise<Page | void> | Page | void; | ||
@@ -14,2 +15,6 @@ export type Loader = (ctx: RequestCtx, page: Page, next: (page?: MaybePage) => MaybePage) => MaybePage; | ||
} | ||
export type AxiosOptionsBuilder = (url: string, options: AxiosRequestConfig) => { | ||
url: string; | ||
options: AxiosRequestConfig; | ||
}; | ||
} | ||
@@ -21,4 +26,4 @@ declare module "services/inmemory" { | ||
declare module "services/api" { | ||
import { Loader } from "types"; | ||
export function createApiLoader(baseUrl: string): Loader; | ||
import { Loader, AxiosOptionsBuilder } from "types"; | ||
export function createApiLoader(baseUrl: string, optionsBuilder?: AxiosOptionsBuilder): Loader; | ||
} | ||
@@ -25,0 +30,0 @@ declare module "index" { |
@@ -105,10 +105,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
]; | ||
function createApiLoader(baseUrl) { | ||
function defaultOptionsBuilder(url, options) { | ||
return { url: url, options: options }; | ||
} | ||
function createApiLoader(baseUrl, optionsBuilder) { | ||
var _this = this; | ||
if (optionsBuilder === void 0) { optionsBuilder = defaultOptionsBuilder; } | ||
return function (ctx, page, next) { return __awaiter(_this, void 0, void 0, function () { | ||
var url, headers, responseType, response; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
var headers, responseType, _a, url, options, response; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
url = "" + baseUrl + ctx.asPath; | ||
headers = { | ||
@@ -126,8 +129,9 @@ "X-User-Agent": "ekino/rendr", | ||
} | ||
return [4, axios_1.default.get(url, { | ||
responseType: responseType, | ||
headers: headers, | ||
})]; | ||
_a = optionsBuilder("" + baseUrl + ctx.asPath, { | ||
responseType: responseType, | ||
headers: headers, | ||
}), url = _a.url, options = _a.options; | ||
return [4, axios_1.default.get(url, options)]; | ||
case 1: | ||
response = _a.sent(); | ||
response = _b.sent(); | ||
if (!("x-rendr-content-type" in response.headers)) { | ||
@@ -134,0 +138,0 @@ return [2, next()]; |
@@ -1,2 +0,2 @@ | ||
import { Loader } from "../types"; | ||
export declare function createApiLoader(baseUrl: string): Loader; | ||
import { Loader, AxiosOptionsBuilder } from "../types"; | ||
export declare function createApiLoader(baseUrl: string, optionsBuilder?: AxiosOptionsBuilder): Loader; |
@@ -48,10 +48,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
]; | ||
export function createApiLoader(baseUrl) { | ||
function defaultOptionsBuilder(url, options) { | ||
return { url: url, options: options }; | ||
} | ||
export function createApiLoader(baseUrl, optionsBuilder) { | ||
var _this = this; | ||
if (optionsBuilder === void 0) { optionsBuilder = defaultOptionsBuilder; } | ||
return function (ctx, page, next) { return __awaiter(_this, void 0, void 0, function () { | ||
var url, headers, responseType, response; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
var headers, responseType, _a, url, options, response; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
url = "" + baseUrl + ctx.asPath; | ||
headers = { | ||
@@ -69,8 +72,9 @@ "X-User-Agent": "ekino/rendr", | ||
} | ||
return [4, Axios.get(url, { | ||
responseType: responseType, | ||
headers: headers, | ||
})]; | ||
_a = optionsBuilder("" + baseUrl + ctx.asPath, { | ||
responseType: responseType, | ||
headers: headers, | ||
}), url = _a.url, options = _a.options; | ||
return [4, Axios.get(url, options)]; | ||
case 1: | ||
response = _a.sent(); | ||
response = _b.sent(); | ||
if (!("x-rendr-content-type" in response.headers)) { | ||
@@ -77,0 +81,0 @@ return [2, next()]; |
import { Page, RequestCtx } from "@ekino/rendr-core"; | ||
import { AxiosRequestConfig } from "axios"; | ||
export declare type MaybePage = Promise<Page | void> | Page | void; | ||
@@ -13,1 +14,5 @@ export declare type Loader = (ctx: RequestCtx, page: Page, next: (page?: MaybePage) => MaybePage) => MaybePage; | ||
} | ||
export declare type AxiosOptionsBuilder = (url: string, options: AxiosRequestConfig) => { | ||
url: string; | ||
options: AxiosRequestConfig; | ||
}; |
@@ -1,2 +0,2 @@ | ||
import { Loader } from "../types"; | ||
export declare function createApiLoader(baseUrl: string): Loader; | ||
import { Loader, AxiosOptionsBuilder } from "../types"; | ||
export declare function createApiLoader(baseUrl: string, optionsBuilder?: AxiosOptionsBuilder): Loader; |
@@ -53,10 +53,13 @@ "use strict"; | ||
]; | ||
function createApiLoader(baseUrl) { | ||
function defaultOptionsBuilder(url, options) { | ||
return { url: url, options: options }; | ||
} | ||
function createApiLoader(baseUrl, optionsBuilder) { | ||
var _this = this; | ||
if (optionsBuilder === void 0) { optionsBuilder = defaultOptionsBuilder; } | ||
return function (ctx, page, next) { return __awaiter(_this, void 0, void 0, function () { | ||
var url, headers, responseType, response; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
var headers, responseType, _a, url, options, response; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
url = "" + baseUrl + ctx.asPath; | ||
headers = { | ||
@@ -74,8 +77,9 @@ "X-User-Agent": "ekino/rendr", | ||
} | ||
return [4, axios_1.default.get(url, { | ||
responseType: responseType, | ||
headers: headers, | ||
})]; | ||
_a = optionsBuilder("" + baseUrl + ctx.asPath, { | ||
responseType: responseType, | ||
headers: headers, | ||
}), url = _a.url, options = _a.options; | ||
return [4, axios_1.default.get(url, options)]; | ||
case 1: | ||
response = _a.sent(); | ||
response = _b.sent(); | ||
if (!("x-rendr-content-type" in response.headers)) { | ||
@@ -82,0 +86,0 @@ return [2, next()]; |
import { Page, RequestCtx } from "@ekino/rendr-core"; | ||
import { AxiosRequestConfig } from "axios"; | ||
export declare type MaybePage = Promise<Page | void> | Page | void; | ||
@@ -13,1 +14,5 @@ export declare type Loader = (ctx: RequestCtx, page: Page, next: (page?: MaybePage) => MaybePage) => MaybePage; | ||
} | ||
export declare type AxiosOptionsBuilder = (url: string, options: AxiosRequestConfig) => { | ||
url: string; | ||
options: AxiosRequestConfig; | ||
}; |
{ | ||
"name": "@ekino/rendr-loader", | ||
"license": "MIT", | ||
"version": "0.0.17", | ||
"version": "0.0.18", | ||
"main": "dist/lib/index.js", | ||
@@ -13,3 +13,3 @@ "typing": "dist/lib/index.d.ts", | ||
"dependencies": { | ||
"@ekino/rendr-core": "0.0.17", | ||
"@ekino/rendr-core": "0.0.18", | ||
"axios": "^0.19.0", | ||
@@ -26,3 +26,3 @@ "path-match": "^1.2.4", | ||
}, | ||
"gitHead": "ed31cb2c6fdb510cdca6b6fc8800f04d0b4702e0" | ||
"gitHead": "5da66dfd84492ef08f34dfb6014f44a1e9969609" | ||
} |
import { createPage, Page } from "@ekino/rendr-core"; | ||
import { Readable, Writable } from "stream"; | ||
import Axios, { ResponseType } from "axios"; | ||
import Axios, { ResponseType, AxiosRequestConfig } from "axios"; | ||
import { Loader } from "../types"; | ||
import { Loader, AxiosOptionsBuilder } from "../types"; | ||
@@ -16,2 +16,6 @@ const headersToTransfers = [ | ||
function defaultOptionsBuilder(url: string, options: AxiosRequestConfig) { | ||
return { url, options }; | ||
} | ||
// The loader is used to load the Page definition from an API | ||
@@ -21,6 +25,7 @@ // This code can be called from the nodejs or the browser. | ||
// as the server will stream the content, and so cannot be run by the browser. | ||
export function createApiLoader(baseUrl: string): Loader { | ||
export function createApiLoader( | ||
baseUrl: string, | ||
optionsBuilder: AxiosOptionsBuilder = defaultOptionsBuilder | ||
): Loader { | ||
return async (ctx, page, next) => { | ||
const url = `${baseUrl}${ctx.asPath}`; | ||
const headers: any = { | ||
@@ -44,3 +49,3 @@ "X-User-Agent": "ekino/rendr", | ||
const response = await Axios.get(url, { | ||
const { url, options } = optionsBuilder(`${baseUrl}${ctx.asPath}`, { | ||
responseType, | ||
@@ -50,2 +55,4 @@ headers, | ||
const response = await Axios.get(url, options); | ||
if (!("x-rendr-content-type" in response.headers)) { | ||
@@ -52,0 +59,0 @@ // @todo: check how we can add a logger here |
import { Page, RequestCtx } from "@ekino/rendr-core"; | ||
import { AxiosRequestConfig } from "axios"; | ||
@@ -22,1 +23,6 @@ export type MaybePage = Promise<Page | void> | Page | void; | ||
} | ||
export type AxiosOptionsBuilder = ( | ||
url: string, | ||
options: AxiosRequestConfig | ||
) => { url: string; options: AxiosRequestConfig }; |
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
91646
2.81%1707
2.28%+ Added
- Removed
Updated