#Getting `url` property for an endpoint with `import.meta.glob`

9 messages · Page 1 of 1 (latest)

weak jungle
#

Hey Astro people. I am using import.meta.glob to get a list of files in my pages directory, and I need to know the url for the file, so that I can associate the route for each file with an action to perform (e.g. authenticate). I see that .astro and .md have the url property, but "Other files" do not. So, for an endpoint that is a .js or .ts file, there is no url property.

You can see the docs describing this here: https://5-0-0-beta.docs.astro.build/en/guides/imports/#import-type-utilities
You can see endpoint files being considered "Other files" here: https://5-0-0-beta.docs.astro.build/en/guides/imports/#other-files

I can "fix" this by adding export const url = '/my/path'; to my endpoint file, but that seems unnecessary as I have to repeat the route based on the directory structure. Is there a reason why .js and .ts don't have the url property? Is this something I could put a PR into for? I'm assuming that an "endpoint" is a .js or .ts file in the /pages directory that conforms to the spec found at https://docs.astro.build/en/guides/endpoints/.

Thanks!

Docs

Learn how to create endpoints that serve any kind of data

Docs

Learn how to import different file types into your Astro project.

keen wolf
weak jungle
#

Thanks for the response, however I'm not seeing a context object in the output that I get from import.meta.glob

Right now, my middleware looks like this:

const files = await import.meta.glob('../pages/*.(astro|js)', { eager: true });

export function onRequest(context, next) {
  console.log(files);
  
  return next();
}

and I have two files, a index.astro file and a test.js file.

my test.js file looks like this:

export const data = 'my data';

export function GET(context) {
  return context.redirect('/');
}

for my index.astro file, I have a url field as per https://5-0-0-beta.docs.astro.build/en/reference/api-reference/#astro-files

for my test.js endpoint, I only have data and GET.

I guess, as this is just a 'plain old javascript' file, that is all that can be exported. Just wonder how Astro includes the url for the markdown and astro files.

Thanks!

#

FYI, I am using a debugger to see what I have during my middleware, you can see here.

#

In this case, url = '' makes sense, as it is the root index file.

keen wolf
#

Are you on Astro 4 or 5? because you are sharing docs links from both 🙂

weak jungle
#

sorry, on astro 5

keen wolf
#

And what is the reason you are needing to import the pages as a glob in your middleware?

If you hit your endpoint from a page, the url for that page will be in the endpoint context

weak jungle
#

in this example, i want to add authentication to a subset of pages. so, in my authentication middleware, i make a route map, where i get all the files in the pages directory (sans underscore, as they are not routable) using import.meta.glob. then, on pages that I do not want authentication on, i add a export const authenticated = false to the code fence on astro files. this way, i can make a map of with url as the key and authenticated as the value, with the default being true. and then on each request, match the url to authentication.