qwik icon indicating copy to clipboard operation
qwik copied to clipboard

Qwik City - Add env to Qwik request context for Cloudflare Pages

Open elxris opened this issue 3 years ago • 6 comments

Is your feature request related to a problem?

I was trying to use the Cloudflare environment variables but I realised that I don't have access to them with Qwik City.

Describe the solution you'd like

I would like environment variables env to be included in the request context for server endpoints. (onGet, onPost, onPut, onPatch, and onDelete)

Describe alternatives you've considered

I could hardcode my secrets, but I don't think that's the way.

Additional context

No response

elxris avatar Aug 15 '22 05:08 elxris

Some extra info. Cloudflare pages functions (and workers) provide bindings which show up on the env parameter (not global). This isn't just for environment variables but also api access to KV namespaces, durable objects, R2 buckets and soon D1 databases. Definitely useful APIs for on___ functions

mrmcc3 avatar Aug 15 '22 08:08 mrmcc3

Qwik actually has a useEnvData function, so this should be as easy as passing the env param to the render options. @elxris @mrmcc3 Would either of you mind taking a look at my PR and making sure everything looks okay from a Cloudflare perspective?

nnelgxorz avatar Aug 15 '22 23:08 nnelgxorz

@nnelgxorz from the cloudflare api perspective everything just hangs off the env object so as long as you have access to that. qwik/city is very new to me but my (naive) experiments with useEnvData in onGet throws

Invoking 'use*()' method outside of invocation context.

mrmcc3 avatar Aug 16 '22 03:08 mrmcc3

Ah, you're right. The use*() functions have rules on where they can be used. You could still use useEnvData() inside of a component$ or other use methods.

It might still throw if you haven't supplied envData to the render options.

Edit: updated the example code in PR.

nnelgxorz avatar Aug 16 '22 03:08 nnelgxorz

I can imagine using environment variables for credentials when data fetching. All the cloudflare APIs are also essentially data fetching. Makes sense to have it avail. in on__ methods for retrieving/modifying data instead?

mrmcc3 avatar Aug 16 '22 03:08 mrmcc3

I think you're right. It would just involve a larger reworking of the Qwik City code. You could still use useEnvData inside of a useResource or useServerMount$ to do data fetching. When you call useEndpoint to get onGet data you're already getting a resource object back.

nnelgxorz avatar Aug 16 '22 03:08 nnelgxorz

@nnelgxorz but what if I don't want to expose my secrets to the client code at all?

Excuse me if I got it wrong but I feel that approach will expose my secrets to the client? I don't think this is good.

I understand that the on___ function code is not exposed to the client at all.

My on___ code is making a request that should not be exposed to the user. That's why I need the env exposed by Cloudflare in there.

Excuse me again if I'm getting all wrong, I'm 4-days old to Qwik.

elxris avatar Aug 16 '22 14:08 elxris

@elxris If it's in a useServerMount$ it will only run in the server. A useResource can run on both, so you would need to be careful. I think if the resource never changes, that code will not ship to the client. You might run into a sticky point with SPA navigation and resources though.

I'd say for now, you could have this PR and useServerMount$ and then Adam and Manu can figure out the scope issue for onGet, because I agree that should work.

nnelgxorz avatar Aug 16 '22 17:08 nnelgxorz

Oh and no need to apologies for being new to Qwik. Qwik is so new we're all pretty new to it. 😆

nnelgxorz avatar Aug 16 '22 17:08 nnelgxorz