@burdy-cms/web-utils
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -1,2 +0,2 @@ | ||
import { IBurdyPage, IBurdyTag } from '../types'; | ||
import { IBurdyPage, IBurdySearch, IBurdyTag } from '../types'; | ||
export declare type GetPageOptions = { | ||
@@ -43,4 +43,4 @@ draft?: boolean; | ||
getPage: <T = any>(url: string, options?: GetPageOptions) => Promise<IBurdyPage<T>>; | ||
searchPages: <T_1 = any>(host: string, options: SearchPagesOptions) => Promise<IBurdyPage<T_1>[]>; | ||
searchTags: (host: string, options: SearchTagsOptions) => Promise<IBurdyTag[]>; | ||
searchPages: <T_1 = any>(host: string, options: SearchPagesOptions) => Promise<IBurdySearch<IBurdyPage<T_1>>>; | ||
searchTags: (host: string, options: SearchTagsOptions) => Promise<IBurdySearch<IBurdyTag>>; | ||
}; |
@@ -76,7 +76,7 @@ "use strict"; | ||
params.page = options.page; | ||
const { data: pages } = yield axios_1.default.get(`${host}/api/search/posts`, { | ||
const { data } = yield axios_1.default.get(`${host}/api/search/posts`, { | ||
params, | ||
headers | ||
}); | ||
return pages; | ||
return data; | ||
}), | ||
@@ -106,8 +106,8 @@ searchTags: (host, options) => __awaiter(void 0, void 0, void 0, function* () { | ||
params.page = options.page; | ||
const { data: tags } = yield axios_1.default.get(`${host}/api/search/posts`, { | ||
const { data } = yield axios_1.default.get(`${host}/api/search/tags`, { | ||
params, | ||
headers | ||
}); | ||
return tags; | ||
return data; | ||
}) | ||
}; |
import { Key } from 'path-to-regexp'; | ||
import { Rewrites, RewritesMap, RewritesReturn } from '../types'; | ||
import { RewriteDestination, RewritesMap } from '../types'; | ||
export declare const rewrite: (string: string, params?: Record<Key['name'], string | undefined>) => string; | ||
export declare const matchAndRewrite: (path: string, map: Rewrites[]) => string; | ||
export declare const matchAndRewriteMap: <T>(path: string, map?: RewritesMap<T>[], baseUrl?: string) => RewritesReturn<T>; | ||
export declare const matchAndRewriteMap: <T>(path: string, map?: RewritesMap<T>[], baseUrl?: string) => RewriteDestination<T>; |
@@ -6,5 +6,6 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.matchAndRewriteMap = exports.matchAndRewrite = exports.rewrite = void 0; | ||
exports.matchAndRewriteMap = exports.rewrite = void 0; | ||
const path_to_regexp_1 = require("path-to-regexp"); | ||
const deepcopy_1 = __importDefault(require("deepcopy")); | ||
const RESERVED_KEY = '$__path'; | ||
const escapeRegExp = (str) => { | ||
@@ -21,26 +22,14 @@ return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1'); | ||
exports.rewrite = rewrite; | ||
const matchAndRewrite = (path, map) => { | ||
let resultArray; | ||
let keys = []; | ||
map = (0, deepcopy_1.default)(map); | ||
const rule = map.find((rule) => { | ||
keys = []; | ||
const regexp = (0, path_to_regexp_1.pathToRegexp)(rule.source, keys); | ||
resultArray = regexp.exec(path); | ||
return !!resultArray; | ||
}); | ||
if (!rule) { | ||
return null; | ||
} | ||
const params = {}; | ||
keys.forEach((key, index) => { | ||
params[key.name] = resultArray === null || resultArray === void 0 ? void 0 : resultArray[index + 1]; | ||
}); | ||
return (0, exports.rewrite)((rule === null || rule === void 0 ? void 0 : rule.rewrite) || '', params); | ||
}; | ||
exports.matchAndRewrite = matchAndRewrite; | ||
const normalizeRewritesMap = (rewrites) => rewrites.map(rewrite => (Object.assign(Object.assign({}, rewrite), { destination: typeof rewrite.destination === 'string' ? | ||
{ [RESERVED_KEY]: rewrite.destination } : | ||
rewrite.destination }))); | ||
const normalizeDestination = (destination) => Object.keys(destination).length === 1 && (destination === null || destination === void 0 ? void 0 : destination[RESERVED_KEY]) ? | ||
destination[RESERVED_KEY] : | ||
destination; | ||
const matchAndRewriteMap = (path, map = [], baseUrl = '') => { | ||
var _a; | ||
let resultArray; | ||
let keys = []; | ||
map = (0, deepcopy_1.default)(map); | ||
map = normalizeRewritesMap(map); | ||
const rule = map.find((rule) => { | ||
@@ -53,3 +42,6 @@ keys = []; | ||
if (!rule) { | ||
return Object.keys(map).reduce((previousValue, currentValue) => (Object.assign(Object.assign({}, previousValue), { [currentValue]: null })), {}); | ||
const firstDestination = normalizeDestination(((_a = map === null || map === void 0 ? void 0 : map[0]) === null || _a === void 0 ? void 0 : _a.destination) || ''); | ||
return typeof firstDestination === 'string' ? | ||
null : | ||
Object.keys(firstDestination).reduce((previousValue, currentValue) => (Object.assign(Object.assign({}, previousValue), { [currentValue]: null })), {}); | ||
} | ||
@@ -60,8 +52,8 @@ const params = {}; | ||
}); | ||
Object.keys(rule.rewrites || {}).forEach((key) => { | ||
Object.keys(rule.destination || {}).forEach((key) => { | ||
var _a; | ||
rule.rewrites = Object.assign(Object.assign({}, (rule.rewrites || {})), { [key]: baseUrl.replace(/\/$/i, '') + (0, exports.rewrite)((_a = rule.rewrites) === null || _a === void 0 ? void 0 : _a[key], params) }); | ||
rule.destination[key] = baseUrl.replace(/\/$/i, '') + (0, exports.rewrite)((_a = rule.destination) === null || _a === void 0 ? void 0 : _a[key], params); | ||
}); | ||
return rule.rewrites; | ||
return normalizeDestination(rule.destination); | ||
}; | ||
exports.matchAndRewriteMap = matchAndRewriteMap; |
import { RewriteOptions, RewritesObject } from '../types'; | ||
export declare const createRewrites: <T extends {}>(options: RewriteOptions<T>) => RewritesObject<T>; | ||
export declare const createRewrites: <T extends string | {}>(options: RewriteOptions<T>) => RewritesObject<T>; |
@@ -7,7 +7,5 @@ "use strict"; | ||
getOrigin: () => (options === null || options === void 0 ? void 0 : options.origin) || '', | ||
getRewriteMap: () => options.rewriteMap, | ||
getRewrite: () => options.rewrite, | ||
rewrite: (path) => (0, build_path_1.matchAndRewrite)(path, options.rewrite), | ||
rewriteMap: (path) => (0, build_path_1.matchAndRewriteMap)(path, options.rewriteMap, (options === null || options === void 0 ? void 0 : options.origin) || '') | ||
getRewrite: () => (options === null || options === void 0 ? void 0 : options.rewrite) || [], | ||
rewrite: (path) => (0, build_path_1.matchAndRewriteMap)(path, options.rewrite, (options === null || options === void 0 ? void 0 : options.origin) || '') | ||
}); | ||
exports.createRewrites = createRewrites; |
@@ -6,22 +6,17 @@ export * from './model'; | ||
}; | ||
export declare type RewritesMap<T extends {}> = { | ||
export declare type RewriteDestination<T> = T extends string ? string | null : { | ||
[key in keyof T]: string | null; | ||
}; | ||
export declare type RewritesMap<T extends {} | string> = { | ||
source: string; | ||
rewrites: { | ||
[key in keyof T]: string; | ||
}; | ||
destination: RewriteDestination<T>; | ||
}; | ||
export declare type RewriteOptions<T extends {}> = { | ||
export declare type RewriteOptions<T extends {} | string> = { | ||
origin?: string; | ||
rewrite?: Rewrites[]; | ||
rewriteMap?: RewritesMap<T>[]; | ||
rewrite: RewritesMap<T>[]; | ||
}; | ||
export declare type RewritesReturn<T> = { | ||
[key in keyof T]: string; | ||
}; | ||
export declare type RewritesObject<T extends {}> = { | ||
rewriteMap: (path: string) => RewritesReturn<T>; | ||
rewrite: (path: string) => string | undefined; | ||
export declare type RewritesObject<T extends {} | string> = { | ||
rewrite: (path: string) => RewriteDestination<T>; | ||
getOrigin: () => string | undefined; | ||
getRewrite: () => Rewrites[]; | ||
getRewriteMap: () => RewritesMap<T>[]; | ||
getRewrite: () => RewritesMap<T>[]; | ||
}; |
@@ -38,2 +38,8 @@ export declare type IBurdyAuthor = { | ||
} | ||
export declare type IBurdySearch<T = any> = { | ||
count: number; | ||
results: T[]; | ||
page?: number; | ||
limit?: number; | ||
}; | ||
export interface IBurdyImage { | ||
@@ -40,0 +46,0 @@ id?: number; |
{ | ||
"name": "@burdy-cms/web-utils", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Burdy web utilities", | ||
@@ -5,0 +5,0 @@ "author": "Team Burdy <team@burdy.io>", |
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
44244
991