Repository: withastro/astro
File: packages/astro/src/runtime.ts (TypeScript)

Code before:

  if (searchResult.statusCode === 301) {
    return { statusCode: 301, location: searchResult.pathname };
  }

  const snowpackURL = searchResult.location.snowpackURL;
  let rss: { data: any[] & CollectionRSS } = {} as any;

  try {
    const mod = await snowpackRuntime.importModule(snowpackURL);
    debug(logging, 'resolve', `${reqPath} -> ${snowpackURL}`);

    // handle collection
    let collection = {} as CollectionResult;
    let additionalURLs = new Set<string>();

    if (mod.exports.createCollection) {
      const createCollection: CreateCollection = await mod.exports.createCollection();
      const VALID_KEYS = new Set(['data', 'routes', 'permalink', 'pageSize', 'rss']);
      for (const key of Object.keys(createCollection)) {
        if (!VALID_KEYS.has(key)) {
          throw new Error(`[createCollection] unknown option: "${key}". Expected one of ${[...VALID_KEYS].join(', ')}.`);
        }
      }
      let { data: loadData, routes, permalink, pageSize, rss: createRSS } = createCollection;
      if (!loadData) throw new Error(`[createCollection] must return \`data()\` function to create a collection.`);
      if (!pageSize) pageSize = 25; // can’t be 0
      let currentParams: Params = {};

      // params
      if (routes || permalink) {
        if (!routes) throw new Error('[createCollection] `permalink` requires `routes` as well.');
        if (!permalink) throw new Error('[createCollection] `routes` requires `permalink` as well.');

        let requestedParams = routes.find((p) => {
          const baseURL = (permalink as any)({ params: p });
          additionalURLs.add(baseURL);
          return baseURL === reqPath || `${baseURL}/${searchResult.currentPage || 1}` === reqPath;
        });
        if (requestedParams) {
          currentParams = requestedParams;
          collection.params = requestedParams;
        }
      }

      let data: any[] = await loadData({ params: currentParams });
      if (!data) throw new Error(`[createCollection] \`data()\` returned nothing (empty data)"`);

      // handle RSS
      if (createRSS) {
        rss = {

Review comment (suggestion):
Add a few more helpful error messages to `createCollection()`

Code after:

  if (searchResult.statusCode === 301) {
    return { statusCode: 301, location: searchResult.pathname };
  }

  const snowpackURL = searchResult.location.snowpackURL;
  let rss: { data: any[] & CollectionRSS } = {} as any;

  try {
    const mod = await snowpackRuntime.importModule(snowpackURL);
    debug(logging, 'resolve', `${reqPath} -> ${snowpackURL}`);

    // handle collection
    let collection = {} as CollectionResult;
    let additionalURLs = new Set<string>();

    if (mod.exports.createCollection) {
      const createCollection: CreateCollection = await mod.exports.createCollection();
      const VALID_KEYS = new Set(['data', 'routes', 'permalink', 'pageSize', 'rss']);
      for (const key of Object.keys(createCollection)) {
        if (!VALID_KEYS.has(key)) {
          throw new Error(`[createCollection] unknown option: "${key}". Expected one of ${[...VALID_KEYS].join(', ')}.`);
        }
      }
      let { data: loadData, routes, permalink, pageSize, rss: createRSS } = createCollection;
      if (!loadData) throw new Error(`[createCollection] must return \`data()\` function to create a collection.`);
      if (!pageSize) pageSize = 25; // can’t be 0
      let currentParams: Params = {};

      // params
      if (routes || permalink) {
        if (!routes) throw new Error('[createCollection] `permalink` requires `routes` as well.');
        if (!permalink) throw new Error('[createCollection] `routes` requires `permalink` as well.');

        let requestedParams = routes.find((p) => {
          const baseURL = (permalink as any)({ params: p });
          additionalURLs.add(baseURL);
          return baseURL === reqPath || `${baseURL}/${searchResult.currentPage || 1}` === reqPath;
        });
        if (requestedParams) {
          currentParams = requestedParams;
          collection.params = requestedParams;
        }
      }

      let data: any[] = await loadData({ params: currentParams });
      if (!data) throw new Error(`[createCollection] \`data()\` returned nothing (empty data)"`);
      if (!Array.isArray(data)) data = [data]; // note: this is supposed to be a little friendlier to the user, but should we error out instead?

      // handle RSS
      if (createRSS) {