@shuvi/platform-shared 1.0.0-rc.18 → 1.0.0-rc.19

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.
@@ -70,23 +70,28 @@ function errorHelper(msg, statusCode = 500) {
70
70
  }
71
71
  export function runLoaders(matches, loadersByRouteId, { query, req, getAppContext }) {
72
72
  return __awaiter(this, void 0, void 0, function* () {
73
+ const loaderDatas = {};
73
74
  if (!matches.length) {
74
- return [];
75
+ return loaderDatas;
75
76
  }
76
77
  const appContext = getAppContext();
77
78
  const createLoader = (match) => () => __awaiter(this, void 0, void 0, function* () {
78
79
  const loaderFn = loadersByRouteId[match.route.id];
80
+ if (typeof loaderFn !== 'function') {
81
+ return;
82
+ }
79
83
  let res;
80
84
  try {
81
85
  const value = yield loaderFn(Object.assign({ pathname: match.pathname, params: match.params, query: query, redirect: redirectHelper, error: errorHelper, appContext }, (req ? { req } : {})));
82
- if (value) {
83
- res = createJson(value);
86
+ if (value === undefined) {
87
+ throw new Error(`You defined a loader for route "${match.route.path}" but didn't return ` +
88
+ `anything from your \`loader\` function. Please return a value or \`null\`.`);
84
89
  }
90
+ res = createJson(value);
85
91
  }
86
92
  catch (error) {
87
93
  if (process.env.NODE_ENV === 'development' && !isResponse(error)) {
88
94
  console.error(`loader function error of route "${match.route.path}"`);
89
- console.error(error);
90
95
  }
91
96
  throw error;
92
97
  }
@@ -94,7 +99,6 @@ export function runLoaders(matches, loadersByRouteId, { query, req, getAppContex
94
99
  });
95
100
  // call loaders in parallel
96
101
  const resultList = yield runInParallerAndBail(matches.map(createLoader));
97
- const loaderDatas = {};
98
102
  for (let index = 0; index < resultList.length; index++) {
99
103
  const item = resultList[index];
100
104
  if (item.error) {
@@ -64,6 +64,6 @@ export interface IRouteLoaderContext {
64
64
  */
65
65
  appContext: IAppContext;
66
66
  }
67
- export declare type Loader<T extends {} = {}> = (loaderContenxt: IRouteLoaderContext) => Promise<T | void | undefined> | T | void | undefined;
68
- export declare type NormalizedLoader = (loaderContenxt: IRouteLoaderContext) => Promise<Response | undefined>;
67
+ export declare type Loader<T = any> = (loaderContext: IRouteLoaderContext) => Promise<T> | T;
68
+ export declare type NormalizedLoader = (loaderContext: IRouteLoaderContext) => Promise<Response | undefined>;
69
69
  export declare type LoaderDataRecord = Record<string, any>;
@@ -20,7 +20,7 @@ const builtinRuntimePluginHooks = {
20
20
  dispose
21
21
  };
22
22
  export const getManager = () => createHookManager(builtinRuntimePluginHooks, false);
23
- export const { createPluginBefore: createRuntimePluginBefore, createPlugin: createRuntimePlugin, createPluginAfter: createRuntimePluginAfter } = createPluginCreator(getManager());
23
+ export const { createPluginBefore: createRuntimePluginBefore, createPlugin: createRuntimePlugin, createPluginAfter: createRuntimePluginAfter } = createPluginCreator();
24
24
  export const initPlugins = (pluginManager, plugins) => __awaiter(void 0, void 0, void 0, function* () {
25
25
  // clear plugin at development mode every time
26
26
  if (process.env.NODE_ENV === 'development') {
@@ -8,7 +8,6 @@ export declare function normalizeRoutePath(rawPath: string): string;
8
8
  export declare function combineComponents(fisrt: string, sec: string): string;
9
9
  export declare const isRouteFile: (file: string) => boolean;
10
10
  export declare const getAllowFilesAndDirs: (dirname: string) => Promise<string[]>;
11
- export declare const hasAllowFiles: (files: string[]) => boolean;
12
11
  export declare const readDir: (fullPath: string) => Promise<string[]>;
13
12
  export declare const hasRouteChildren: (files: string[], parentPath: string) => Promise<boolean>;
14
13
  declare const fileTypeChecker: FileTypeChecker;
@@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.sortRoutes = exports.fileTypeChecker = exports.hasRouteChildren = exports.readDir = exports.hasAllowFiles = exports.getAllowFilesAndDirs = exports.isRouteFile = exports.combineComponents = exports.normalizeRoutePath = exports.parseDynamicPath = void 0;
15
+ exports.sortRoutes = exports.fileTypeChecker = exports.hasRouteChildren = exports.readDir = exports.getAllowFilesAndDirs = exports.isRouteFile = exports.combineComponents = exports.normalizeRoutePath = exports.parseDynamicPath = void 0;
16
16
  const fs_1 = __importDefault(require("fs"));
17
17
  const path_1 = require("path");
18
18
  const file_1 = require("@shuvi/utils/lib/file");
@@ -136,8 +136,6 @@ const getAllowFilesAndDirs = (dirname) => __awaiter(void 0, void 0, void 0, func
136
136
  return result;
137
137
  });
138
138
  exports.getAllowFilesAndDirs = getAllowFilesAndDirs;
139
- const hasAllowFiles = (files) => files.some(file => (0, exports.isRouteFile)(file));
140
- exports.hasAllowFiles = hasAllowFiles;
141
139
  const readDir = (fullPath) => {
142
140
  return fs_1.default.promises.readdir(fullPath, { encoding: 'utf-8' });
143
141
  };
@@ -29,7 +29,6 @@ const getRawRoutesFromDir = (dirname, excludes) => __awaiter(void 0, void 0, voi
29
29
  const patterns = (0, matchSpec_1.getFileMatcherPatterns)(rootDirname, excludes);
30
30
  const visitDirectory = (dirname, routes, parentDir) => __awaiter(void 0, void 0, void 0, function* () {
31
31
  const files = yield (0, helpers_1.getAllowFilesAndDirs)(dirname);
32
- const onlyHasDir = !(0, helpers_1.hasAllowFiles)(files);
33
32
  if (!files.length) {
34
33
  warnings.push({
35
34
  type: 'dir',
@@ -49,20 +48,14 @@ const getRawRoutesFromDir = (dirname, excludes) => __awaiter(void 0, void 0, voi
49
48
  }
50
49
  const segment = parentDir === '' ? '/' : (0, helpers_1.normalizeRoutePath)(parentDir);
51
50
  if (isDir) {
52
- if (onlyHasDir) {
53
- // only indent segment,routes was in same level.
54
- yield visitDirectory(filepath, routes, `${parentDir}/${file}`);
55
- }
56
- else {
57
- const route = {
58
- kind: 'dir',
59
- filepath,
60
- segment: segment,
61
- children: []
62
- };
63
- routes.push(route);
64
- yield visitDirectory(filepath, route.children, file);
65
- }
51
+ const route = {
52
+ kind: 'dir',
53
+ filepath,
54
+ segment: segment,
55
+ children: []
56
+ };
57
+ routes.push(route);
58
+ yield visitDirectory(filepath, route.children, file);
66
59
  }
67
60
  else {
68
61
  const ext = (0, path_1.extname)(file);
@@ -77,23 +77,28 @@ function errorHelper(msg, statusCode = 500) {
77
77
  }
78
78
  function runLoaders(matches, loadersByRouteId, { query, req, getAppContext }) {
79
79
  return __awaiter(this, void 0, void 0, function* () {
80
+ const loaderDatas = {};
80
81
  if (!matches.length) {
81
- return [];
82
+ return loaderDatas;
82
83
  }
83
84
  const appContext = getAppContext();
84
85
  const createLoader = (match) => () => __awaiter(this, void 0, void 0, function* () {
85
86
  const loaderFn = loadersByRouteId[match.route.id];
87
+ if (typeof loaderFn !== 'function') {
88
+ return;
89
+ }
86
90
  let res;
87
91
  try {
88
92
  const value = yield loaderFn(Object.assign({ pathname: match.pathname, params: match.params, query: query, redirect: redirectHelper, error: errorHelper, appContext }, (req ? { req } : {})));
89
- if (value) {
90
- res = (0, response_1.json)(value);
93
+ if (value === undefined) {
94
+ throw new Error(`You defined a loader for route "${match.route.path}" but didn't return ` +
95
+ `anything from your \`loader\` function. Please return a value or \`null\`.`);
91
96
  }
97
+ res = (0, response_1.json)(value);
92
98
  }
93
99
  catch (error) {
94
100
  if (process.env.NODE_ENV === 'development' && !(0, response_1.isResponse)(error)) {
95
101
  console.error(`loader function error of route "${match.route.path}"`);
96
- console.error(error);
97
102
  }
98
103
  throw error;
99
104
  }
@@ -101,7 +106,6 @@ function runLoaders(matches, loadersByRouteId, { query, req, getAppContext }) {
101
106
  });
102
107
  // call loaders in parallel
103
108
  const resultList = yield runInParallerAndBail(matches.map(createLoader));
104
- const loaderDatas = {};
105
109
  for (let index = 0; index < resultList.length; index++) {
106
110
  const item = resultList[index];
107
111
  if (item.error) {
@@ -64,6 +64,6 @@ export interface IRouteLoaderContext {
64
64
  */
65
65
  appContext: IAppContext;
66
66
  }
67
- export declare type Loader<T extends {} = {}> = (loaderContenxt: IRouteLoaderContext) => Promise<T | void | undefined> | T | void | undefined;
68
- export declare type NormalizedLoader = (loaderContenxt: IRouteLoaderContext) => Promise<Response | undefined>;
67
+ export declare type Loader<T = any> = (loaderContext: IRouteLoaderContext) => Promise<T> | T;
68
+ export declare type NormalizedLoader = (loaderContext: IRouteLoaderContext) => Promise<Response | undefined>;
69
69
  export declare type LoaderDataRecord = Record<string, any>;
@@ -25,7 +25,7 @@ const builtinRuntimePluginHooks = {
25
25
  };
26
26
  const getManager = () => (0, hook_1.createHookManager)(builtinRuntimePluginHooks, false);
27
27
  exports.getManager = getManager;
28
- _a = (0, plugins_1.createPluginCreator)((0, exports.getManager)()), exports.createRuntimePluginBefore = _a.createPluginBefore, exports.createRuntimePlugin = _a.createPlugin, exports.createRuntimePluginAfter = _a.createPluginAfter;
28
+ _a = (0, plugins_1.createPluginCreator)(), exports.createRuntimePluginBefore = _a.createPluginBefore, exports.createRuntimePlugin = _a.createPlugin, exports.createRuntimePluginAfter = _a.createPluginAfter;
29
29
  const initPlugins = (pluginManager, plugins) => __awaiter(void 0, void 0, void 0, function* () {
30
30
  // clear plugin at development mode every time
31
31
  if (process.env.NODE_ENV === 'development') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shuvi/platform-shared",
3
- "version": "1.0.0-rc.18",
3
+ "version": "1.0.0-rc.19",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/shuvijs/shuvi.git",
@@ -83,18 +83,18 @@
83
83
  "node": ">= 12.0.0"
84
84
  },
85
85
  "dependencies": {
86
- "@shuvi/hook": "1.0.0-rc.18",
86
+ "@shuvi/hook": "1.0.0-rc.19",
87
87
  "@shuvi/redox": "0.0.7",
88
- "@shuvi/router": "1.0.0-rc.18",
89
- "@shuvi/runtime": "1.0.0-rc.18",
90
- "@shuvi/service": "1.0.0-rc.18",
91
- "@shuvi/shared": "1.0.0-rc.18",
92
- "@shuvi/toolpack": "1.0.0-rc.18",
93
- "@shuvi/utils": "1.0.0-rc.18",
88
+ "@shuvi/router": "1.0.0-rc.19",
89
+ "@shuvi/runtime": "1.0.0-rc.19",
90
+ "@shuvi/service": "1.0.0-rc.19",
91
+ "@shuvi/shared": "1.0.0-rc.19",
92
+ "@shuvi/toolpack": "1.0.0-rc.19",
93
+ "@shuvi/utils": "1.0.0-rc.19",
94
94
  "redux": "4.1.2"
95
95
  },
96
96
  "peerDependencies": {
97
- "@shuvi/service": "1.0.0-rc.18"
97
+ "@shuvi/service": "1.0.0-rc.19"
98
98
  },
99
99
  "devDependencies": {
100
100
  "@types/minimatch": "3.0.5"