@uxf/router 10.0.0-beta.16 → 10.0.0-beta.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -119,13 +119,13 @@ Create sitemap items
119
119
  ```tsx
120
120
  // sitemap-items.ts in @app-routes
121
121
 
122
- import { sitemapGenerator } from "@app-routes";
122
+ import { createSitemapGenerator, routeToUrl } from "@app-routes";
123
123
 
124
- export const sitemapItems = sitemapGenerator
125
- .add("index", async () => ({ loc: "http://localhost", lastmod: "2021-01-01", priority: 1 }))
126
- .add("blog/detail", async () => [
127
- { loc: "http://localhost/bolg/1", priority: 2 },
128
- { loc: "http://localhost/bolg/2", priority: 2 },
124
+ export const sitemapItems = createSitemapGenerator({baseUrl: 'http://localhost:3000', defaultPriority: 1})
125
+ .add("index", async (route) => ({ loc: routeToUrl(route) }))
126
+ .add("blog/detail", async (route) => [
127
+ { loc: routeToUrl(route, {id: 1}), priority: 2 },
128
+ { loc: routeToUrl(route, {id: 2}), priority: 2 },
129
129
  ])
130
130
  .skip("admin/index")
131
131
  .exhaustive();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uxf/router",
3
- "version": "10.0.0-beta.16",
3
+ "version": "10.0.0-beta.18",
4
4
  "description": "UXF Router",
5
5
  "author": "UXFans <dev@uxf.cz>",
6
6
  "homepage": "https://gitlab.com/uxf-npm/router#readme",
package/router.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { LinkProps } from "next/link";
2
- import { SitemapGeneratorType } from "./sitemapGenerator";
2
+ import { SitemapGeneratorOptions, SitemapGeneratorType } from "./sitemapGenerator";
3
3
  import { QueryParams } from "./types";
4
4
  export type FunctionParametersGenerator<RouteList> = {
5
5
  [K in keyof RouteList]: RouteList[K] extends null ? [K] : [K, RouteList[K]];
@@ -9,7 +9,7 @@ type RouteToUrlFunction<RouteList> = (...args: FunctionParametersGenerator<Route
9
9
  type Router<RouteList> = {
10
10
  route: RouteFunction<RouteList>;
11
11
  routeToUrl: RouteToUrlFunction<RouteList>;
12
- sitemapGenerator: SitemapGeneratorType<RouteList>;
12
+ createSitemapGenerator: (options?: SitemapGeneratorOptions) => SitemapGeneratorType<RouteList>;
13
13
  routes: {
14
14
  [key in keyof RouteList]: string;
15
15
  };
package/router.js CHANGED
@@ -70,7 +70,7 @@ function createRouter(routes) {
70
70
  }
71
71
  return activeRoute;
72
72
  },
73
- sitemapGenerator: (0, sitemapGenerator_1.createSitemapGenerator)(),
73
+ createSitemapGenerator: sitemapGenerator_1.createSitemapGenerator,
74
74
  routes,
75
75
  useQueryParams: () => (0, router_1.useRouter)().query,
76
76
  };
@@ -1,7 +1,9 @@
1
+ type ChangeFrequency = "always" | "hourly" | "daily" | "weekly" | "monthly" | "yearly" | "never";
1
2
  export type SitemapItem = {
2
3
  loc: string;
3
- priority: number;
4
+ priority?: number;
4
5
  lastmod?: string | null;
6
+ changefreq?: ChangeFrequency;
5
7
  };
6
8
  type RouteResolverResult = null | undefined | SitemapItem | Array<SitemapItem | null | undefined>;
7
9
  type RouteResolver<Route> = (route: Route) => Promise<RouteResolverResult>;
@@ -16,5 +18,10 @@ export type SitemapGeneratorType<RouteList> = {
16
18
  toXml: keyof RouteList extends never ? () => Promise<string> : MissingRoutesError<keyof RouteList>;
17
19
  toJson: keyof RouteList extends never ? () => Promise<any> : MissingRoutesError<keyof RouteList>;
18
20
  };
19
- export declare const createSitemapGenerator: <RouteList>() => SitemapGeneratorType<RouteList>;
21
+ export type SitemapGeneratorOptions = {
22
+ baseUrl?: string;
23
+ defaultPriority?: number;
24
+ defaultChangeFreq?: ChangeFrequency;
25
+ };
26
+ export declare const createSitemapGenerator: <RouteList>(options?: SitemapGeneratorOptions) => SitemapGeneratorType<RouteList>;
20
27
  export {};
@@ -5,28 +5,34 @@ function filterNullish(val) {
5
5
  return val.filter((item) => item !== null && item !== undefined);
6
6
  }
7
7
  class SitemapGenerator {
8
- constructor() {
8
+ constructor(options) {
9
9
  this.routeResolvers = {};
10
+ this.options = null;
10
11
  this.exhaustive = () => this;
11
12
  this.toXml = () => {
12
13
  return this.getSitemapItems().then((sitemapItems) => {
13
14
  return `<?xml version="1.0" encoding="UTF-8"?>
14
15
  <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
15
16
  ${sitemapItems
16
- .map((item) => `<url>
17
- <loc>${item.loc}</loc>
18
- <priority>${item.priority}</priority>
19
- ${item.lastmod ? `<lastmod>${item.lastmod}</lastmod>` : "<changefreq>monthly</changefreq>"}
20
- </url>`)
17
+ .map((item) => {
18
+ var _a, _b;
19
+ return `<url>
20
+ <loc>${(_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.baseUrl) !== null && _b !== void 0 ? _b : ""}${item.loc}</loc>
21
+ ${item.priority ? `<priority>${item.priority}</priority>` : ""}
22
+ ${item.lastmod ? `<lastmod>${item.lastmod}</lastmod>` : ""}
23
+ ${item.changefreq ? `<changefreq>${item.changefreq}</changefreq>` : ""}
24
+ </url>`;
25
+ })
21
26
  .join("")}
22
27
  </urlset>`;
23
28
  });
24
29
  };
25
30
  this.toJson = () => this.getSitemapItems().then(JSON.stringify);
31
+ this.options = options !== null && options !== void 0 ? options : null;
26
32
  }
27
33
  async getSitemapItems() {
28
34
  const routes = Object.keys(this.routeResolvers);
29
- return Promise.all(routes.map((route) => { var _a, _b; return (_b = (_a = this.routeResolvers)[route]) === null || _b === void 0 ? void 0 : _b.call(_a, route); })).then((resolverResults) => {
35
+ const allSitemapItems = await Promise.all(routes.map((route) => { var _a, _b; return (_b = (_a = this.routeResolvers)[route]) === null || _b === void 0 ? void 0 : _b.call(_a, route); })).then((resolverResults) => {
30
36
  const sitemapItems = [];
31
37
  resolverResults.forEach((result) => {
32
38
  if (!result) {
@@ -40,6 +46,14 @@ class SitemapGenerator {
40
46
  });
41
47
  return sitemapItems;
42
48
  });
49
+ return allSitemapItems.map((sitemapItem) => {
50
+ var _a, _b;
51
+ return ({
52
+ ...sitemapItem,
53
+ changefreq: sitemapItem.changefreq !== undefined ? sitemapItem.changefreq : (_a = this.options) === null || _a === void 0 ? void 0 : _a.defaultChangeFreq,
54
+ priority: sitemapItem.priority !== undefined ? sitemapItem.priority : (_b = this.options) === null || _b === void 0 ? void 0 : _b.defaultPriority,
55
+ });
56
+ });
43
57
  }
44
58
  add(route, resolver) {
45
59
  this.routeResolvers[route] = resolver;
@@ -49,5 +63,5 @@ class SitemapGenerator {
49
63
  return this;
50
64
  }
51
65
  }
52
- const createSitemapGenerator = () => new SitemapGenerator();
66
+ const createSitemapGenerator = (options) => new SitemapGenerator(options);
53
67
  exports.createSitemapGenerator = createSitemapGenerator;