https://discord.cloudflare.com logo
Join Discord
Powered by
# functions
  • c

    Cоlе

    12/09/2021, 9:44 PM
    o nvm, looks like you have to
    --bindings
    right
  • c

    Cоlе

    12/09/2021, 9:44 PM
    yea
  • c

    Cоlе

    12/09/2021, 9:44 PM
    if I want to supply more than one, is just command separated?
  • g

    Greg Brimble | Cloudflare Pages

    12/09/2021, 9:46 PM
    PagesFunction<{ KV: KVNamespace }, ‘param’, { mydata: number }>
  • g

    Greg Brimble | Cloudflare Pages

    12/09/2021, 9:46 PM
    Just repeat as necessary 😊
  • c

    Cоlе

    12/09/2021, 9:46 PM
    gotcha, thanks!
  • l

    lucasnad27

    12/09/2021, 9:54 PM
    It's my understanding that a worker surfaces environment variables by injecting global variables, all caps. I'm also assuming that Pages Functions use the same mechanism (I was unable to find any explicit mention of this in the Pages docs) Based on this assumption... If I have an environment variable,
    FOO
    with a value of
    BAR
    could I access this env val with something as simple as
    const foo = FOO;
    ?
  • l

    lucasnad27

    12/09/2021, 9:56 PM
    I understand this may be related to the above conversation re: PagesFunction.
  • l

    lucasnad27

    12/09/2021, 10:05 PM
    Ok, fooled around a bit and got it all working. Would still like to know if I am having a hard time finding the rights docs or they don't exist 🙂
    Copy code
    export const onRequestPost: PagesFunction<{ KV: KVNamespace }, 'param', { FOO : string }> = async ({
      request,
      env,
    }) => {
      return env.FOO
    };
    wrangler pages dev -b FOO=bar -- yarn dev
  • g

    geelen

    12/09/2021, 10:14 PM
    this has changed: https://blog.cloudflare.com/workers-javascript-modules/
  • g

    geelen

    12/09/2021, 10:15 PM
    Pages Functions only supports this new syntax
  • g

    geelen

    12/09/2021, 10:15 PM
    we're gradually replacing all the examples & defaults for normal workers too
  • a

    Adam Reed

    12/09/2021, 10:20 PM
    I spent an hour today trying to figure out how to dual-use a module library.
  • a

    Adam Reed

    12/09/2021, 10:20 PM
    As we needed a lib of constants as a module, and not as one.
  • a

    Adam Reed

    12/09/2021, 10:20 PM
    I gave up and just copy pasted it with and without 'export' on the const lol
  • a

    Adam Reed

    12/09/2021, 10:20 PM
    😐
  • j

    JustinNoel

    12/09/2021, 10:32 PM
    Thanks, I finally got it worked out like this:
    Copy code
    import { AwsClient } from "aws4fetch";
    
    // These are the only environment variables I expect to be available.
    type Env = {
      B2_ACCESS_KEY_ID: string;
      B2_SECRET_ACCESS_KEY: string;
    };
    
    
    // My "route" should have an id
    // Since this is a GET, there is no data.
    export const onRequestGet: PagesFunction<Env, "id", Record<string, unknown>> = async ({
      env,
      params,
    }) => {
      const { B2_ACCESS_KEY_ID, B2_SECRET_ACCESS_KEY } = env;
      const aws = new AwsClient({
        accessKeyId: B2_ACCESS_KEY_ID,
        secretAccessKey: B2_SECRET_ACCESS_KEY,
        region: "my-bucket-name.s3.us-west-001.backblazeb2.com",
      });
    
      try {
        const id = params?.id;
    
        if (!id) {
          return new Response("Bad Request", { status: 400 });
        }
    
        const documentUrl = `https://${aws.region}/${id}`;
        const signedRequest = await aws.sign(documentUrl);
    
        return await fetch(signedRequest);
      } catch (e) {
        console.log(e);
        return new Response("Server Error", { status: 503 });
      }
    };
  • s

    saibotsivad

    12/10/2021, 3:42 AM
    so I ran into a problem using the Stripe lib (it uses some NodeJS libs that require fancy bundler tooling to override) https://www.npmjs.com/package/stripe Stripe made a demo that uses Workers (with a custom Webpack config) but I can't figure out how a person could use custom Webpack configs on Pages Functions... is there a way to specify Webpack configs for Pages Functions?
  • e

    Erwin

    12/10/2021, 3:52 AM
    Nope.. it does not. The only way I think for that to work would be to build the stripe package as a
    stripe-workers
    package with the custom Webpack config and then import that in Pages.
  • s

    saibotsivad

    12/10/2021, 3:54 AM
    welp, alright then 😞 now I'm going to try reverse engineering the bits out of the Stripe SDK
  • s

    saibotsivad

    12/10/2021, 3:59 AM
    also, I couldn't figure out a way to view Worker-like logs from Pages Functions, which basically means I can't introspect them at all when they fail... is this expected? what's the expected development flow here?
  • e

    Erwin

    12/10/2021, 4:01 AM
    The development flow is still very much a work in progress.. which is why they are still very much in beta 🙂
  • e

    Erwin

    12/10/2021, 4:01 AM
    Your best bet is currently to run
    npx wrangler@beta pages dev
    to run everything either on the edge or locally in miniflare
  • e

    Erwin

    12/10/2021, 4:01 AM
    Oh no.. that runs only on miniflare
  • s

    saibotsivad

    12/10/2021, 4:03 AM
    hmm, I guess I didn't realize Pages Functions was still in beta 🤔
  • s

    saibotsivad

    12/10/2021, 4:03 AM
    the only mention of it being in beta is way down pretty much at the end of the page https://developers.cloudflare.com/pages/platform/functions
  • s

    saibotsivad

    12/10/2021, 4:04 AM
    if I could make a strong suggestion to the docs team, it would be to add "badges" or some such to the docs, to mark things as beta, so that it's clear from the beginning
  • e

    Erwin

    12/10/2021, 4:04 AM
    Ohh.. that is a great idea.. could you post that in the #773221328182050826 channel?
  • s

    saibotsivad

    12/10/2021, 4:04 AM
    love to
  • a

    albert

    12/10/2021, 8:43 AM
    This function throws
    TypeError: Cannot reconstruct a Request with a used body.
    when the POST body is not empty.
    Copy code
    mjs
    export const onRequestPost = async ({request, env}) => {
        await request.text()
        return new Response('OK :)')
    }
    I do not use the body in middleware.
    Copy code
    mjs
    export const onRequest = async ({next}) => {
        try {
            return await next()
        } catch (error) {
            return new Response(error, {status: 500, statusText: 'Albert Wrote Bad Code'})
        }
    }
    This seems like a bug :/
1...373839...392Latest