skip to Main Content

I get the error mentioned in the title for the following typescript middleware: (target: es6, module commonjs)

    try {
        const response = await fetch(URL);
        if (response.status !== 200) {
            throw 'request not successful'
        }
        const data = await response.json();
        const { keys } = data;
        for (let i = 0; i < keys.length; i++) {
            const key_id = keys[i].kid;
            const modulus = keys[i].n;
            const exponent = keys[i].e;
            const key_type = keys[i].kty;
            const jwk = { kty: key_type, n: modulus, e: exponent };
            const pem = jwkToPem(jwk);
            pems[key_id] = pem;
        }
        console.log("got PEMS")
    } catch (error) {
        console.log(error)
        console.log('Error! Unable to download JWKs');
    }
}

Where ive defined this globally let pems: { [key: string]: string } = {}, not sure exactly where my issue lies any help would be appreciated

Followed the exact tutorial to this however still stuck on this one error message, possibly the tutorial was outdated im not exactly sure

2

Answers


  1. fetch returns a Promise that resolves to a Response. Response.json returns a promise which resolves to JSON, which does not contain a keys property (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON).

    const { keys } = data;
    

    This is a destructuring assignment, which tries to unpack the keys property from data. data at that point is a JSON object and doesn’t contain a keys property.

    Login or Signup to reply.
  2. I’m not able to recreate exactly because the return type of response.json should be any.

    What if you cast the result of response.json to your expected type? E.g.,

    const data = (await response.json()) as {
      keys: Array<{
        kid: string;
        n: string;
        e: string;
        kty: string;
      }>;
    };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search