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

    richardwong

    12/22/2021, 5:11 AM
    Good
  • r

    richardwong

    12/22/2021, 5:12 AM
    One more thing , how can we handle file download request in functions, is there a demo ?
  • s

    sandeepsihari

    12/22/2021, 9:11 AM
    Hey there I am using Pages functions for my api need along with Next.Js for the frontend. I have added couple of environment variables on the pages dashboard and i want to use that in my localhost/dev machine. i use this command to run the project in local
    Copy code
    npx wrangler pages dev -- npx next dev
    sample function code here
    TEXTLOCAL_API_KEY
    is the environment variable being added on the pages dashboard
    Copy code
    export const onRequestPost = async ({ request }) => {
        const { phone } = await request.json();
        return new Response(
            JSON.stringify({ phone, TEXTLOCAL_API_KEY }),
            {
                headers: {
                    "content-type": "application/json;charset=UTF-8",
                },
            }
        );
    };
    This is the error message i got when i try to execute the code
  • s

    sandeepsihari

    12/22/2021, 9:18 AM
  • g

    Greg Brimble | Cloudflare Pages

    12/22/2021, 9:23 AM
    Copy code
    ts
    export const onRequestPost = async ({ request, env }) => {
        const { phone } = await request.json();
        return new Response(
            JSON.stringify({ phone, env.TEXTLOCAL_API_KEY }),
            {
                headers: {
                    "content-type": "application/json;charset=UTF-8",
                },
            }
        );
    };
  • g

    Greg Brimble | Cloudflare Pages

    12/22/2021, 9:23 AM
    Try that instead.
  • g

    Greg Brimble | Cloudflare Pages

    12/22/2021, 9:23 AM
    Oh, and you'll need to run
    npx wrangler pages dev --binding TEXTLOCAL_API_KEY=somevalue -- npx next dev
  • g

    Greg Brimble | Cloudflare Pages

    12/22/2021, 9:25 AM
    https://developers.cloudflare.com/pages/platform/functions#writing-your-first-function https://developers.cloudflare.com/workers/runtime-apis/fetch-event#bindings-1
  • s

    sandeepsihari

    12/22/2021, 9:28 AM
    Thanks Greg. works perfectly 👍
  • w

    Walshy | Pages

    12/22/2021, 4:56 PM
    Has anyone tried to see if toucan-js works on functions? (either right away or with just a little hacking around)
  • g

    Greg Brimble | Cloudflare Pages

    12/22/2021, 4:56 PM
    It does
  • w

    Walshy | Pages

    12/22/2021, 4:56 PM
    Ah awesome, ty
  • g

    Greg Brimble | Cloudflare Pages

    12/22/2021, 4:58 PM
    https://gist.github.com/GregBrimble/55700bdc3c411d5c156a84265b1f3fe2
  • j

    Josh

    12/22/2021, 4:58 PM
    👀
  • g

    Greg Brimble | Cloudflare Pages

    12/22/2021, 4:59 PM
    I might be working on something...
  • w

    Walshy | Pages

    12/22/2021, 5:00 PM
    Oh yeah? 👀
  • w

    Walshy | Pages

    12/22/2021, 5:00 PM
    ty for the gist
  • c

    coucoueoeuf

    12/23/2021, 12:43 AM
    Is there any way currently to develop on Pages Functions + Durable Objects in a single project? Such that: - When you push a commit, changes in both Functions and Durable Objetcs are deployed - Local development is supported with Wrangler to see changes from both Functions and Durable Objects locally
  • s

    sandeepsihari

    12/23/2021, 9:51 AM
    So I have two functions/apis
    /api/send-otp
    to generate and send a otp on phone number through sms/whatsapp and another
    /api/verify-otp
    which will verify it and return a jwt token to be used in further requests. right now i am storing the otp along with the
    expirationTime
    inside fauna database so that i can verify the user input otp against db otp.
    Copy code
    await dbClient.query(
      q.Update(q.Ref(q.Collection("Users"), user.ref.id), {
        data: {
          otp,
          otpExpiry,
        },
      })
    );
    Is there a better way to achieve this using KV store or
    Durable Objects
    ?
  • e

    Erwin

    12/23/2021, 9:56 AM
    You should be able to just throw that in KV? Use key something like ‘otp::${username}::${otp}’. You can even set the expiration time as a TTL in KV, so that if you try to read it later than that it will fail automatically.
  • g

    Greg Brimble | Cloudflare Pages

    12/23/2021, 11:56 AM
    Note that KV’s minimum TTL is 60s though. I’d suggest Durable Objects for anything short-lived.
  • s

    sandeepsihari

    12/23/2021, 11:58 AM
    That should be fine. I want the minimum expiration time to be 60s it worked out pretty well for me 😃
  • m

    matekosor

    12/23/2021, 12:05 PM
    how to debug functions on cloudflare pages? I tried to use wrangler tail (npm install @cloudflare/wrangler), but I get: Error: Failed to create tail: ⚠️ Code 10007: workers.api.error.script_not_found
  • g

    Greg Brimble | Cloudflare Pages

    12/23/2021, 12:08 PM
    You can use the wrangler2 beta
    npm install wrangler@beta
    and
    wrangler pages dev --help
  • g

    Greg Brimble | Cloudflare Pages

    12/23/2021, 12:08 PM
    We don't yet support tailing from production
  • m

    matekosor

    12/23/2021, 12:29 PM
    thanks. is it possible to mimic that by sending logs from function somewhere else (3th party) where they can be examined?
  • s

    sandeepsihari

    12/23/2021, 12:44 PM
    This is working great for env variables but how can i set and use KV bindings in localhost ? In documentation it says this namespace will be available globally like
    OTP.get("some_key")
    or
    OTP.set("some_key", some_value)
    its not clear how can i set this in localhost using
    wrangler@beta
    like
    npx wrangler pages dev --binding OTP={} -- npx next dev
    ?
  • g

    Greg Brimble | Cloudflare Pages

    12/23/2021, 12:48 PM
    You could use
    waitUntil
    to send logs of requests anywhere you like, yes 🙂
  • g

    Greg Brimble | Cloudflare Pages

    12/23/2021, 12:50 PM
    wrangler pages dev ./my-static-assets-folder --kv OTP
    And then you access it with:
    env.OTP.get("some_key")
  • s

    sandeepsihari

    12/23/2021, 6:33 PM
    Unable to sign/generate JWT token using
    jose
    library
    Copy code
    import * as jose from "jose";
    token = await new jose.SignJWT({ phone: "123456987" })
                .setProtectedHeader({ alg: "RS256" })
                .setIssuedAt()
                .setIssuer(JWT_ISSUER)
                .setAudience(JWT_AUDIENCE)
                .setExpirationTime(JWT_EXPIRATION_TIME)
                .sign(env.JWT_PRIVATE_KEY);
    and as a binding i have set
    JWT_PRIVATE_KEY=thisisverysecureprivatekey
    but getting this error
1...484950...392Latest