`prepareAuth` errors with multiple requests
Closed this issue · 11 comments
I'm seeing an error when making multiple authenticated requests as shown below:
const sdk = require('api')('https://docs.readme.com/developers/openapi/5e1a4f6284248b0018149174');
async function run() {
const changelogs = await sdk.auth('API_KEY').getChangelogs()
.then(res => res.json())
.then(res => {
return res;
});
return sdk.auth('API_KEY').getChangelog({ slug: changelogs[0].slug })
.then(res => res.json())
.then(res => {
console.log(res);
});
}
run();
I get this error:
TypeError: Cannot read property 'length' of undefined
at authKeys.forEach (code/node_modules/api/src/lib/prepareAuth.js:21:17)
at Array.forEach (<anonymous>)
at module.exports (code/node_modules/api/src/lib/prepareAuth.js:19:12)
at fetchOperation (code/node_modules/api/src/index.js:37:87)
at Object.assign.args (code/node_modules/api/src/index.js:63:22)
at Proxy.<anonymous> (code/node_modules/api/src/index.js:117:33)
at run (code/index.js:10:55)
For some reason, it's having trouble loading securitySchemes
here:
api/packages/api/src/lib/prepareAuth.js
Line 21 in cc67a0c
Failing test for this here: 56ce53e
I dont think it's possible for us to know the difference between a global call to auth, and a local call to auth:
sdk.auth(apiKey);
await sdk
.auth(apiKey)
.getSomething();
Since there's shared auth state, doing the following mutates the global state and doesn't work:
await sdk
.auth(apiKey1)
.getSomething()
.then(() => mock1.done());
await sdk
.auth(apiKey2)
.getSomething()
.then(() => mock2.done());
I think short of renaming the global/local functions, there's not much we can do here. We could possibly use a Proxy.set() and do something like this:
// global auth via proxy setter
sdk.auth = apiKey
// local auth via auth() method chaining
await sdk
.auth(apiKey)
.getSomething();
Then we could prefer one over the other, but idk.
TypeError: Cannot read properties of undefined (reading 'length')
at /node_modules/api/src/lib/prepareAuth.js:26:17
at Array.forEach ()
at module.exports (/node_modules/api/src/lib/prepareAuth.js:24:12)
at node_modules/api/src/index.js:57:53
at processTicksAndRejections (node:internal/process/task_queues:96:5)
Still having the problem
@Oguntoye can you share the code you're using, or is it similar to the other examples in this ticket?
how about an await sdk.close(); to close the connection manually?
@TomMiller-mas We're removing support for auth chaining in #431 as auth differing between API requests is extremely rare.
I am not using chaining and getting this error. The first time I run the API call it works fine. The second time I get the error. We are calling an API weekly to pick up data from another service and add it to our systems. The plan is to call the endpoint with curl daily or weekly depending on the client and let it kick off the process using express.js server. Here is the call using SDK. Thoughts on how I can reset SDK so it doesn't error out the second time? I am using async/await style coding with try.. catch..finally blocks. Is there anything I can put in the finally block to reset the SDK?
sdk.auth(webreq.body.token);
apiData = await sdk.getVehicleInfo({
types: "engineStates, obdOdometerMeters",
});
const jsonData = apiData.data;
< Loop thru jsonData and update our database>```
@TomMiller-mas Are you calling sdk.auth()
within a loop, or is it just a lone call?
Only once in the function. There are about 60 vehicles in the database. For testing, I set the loop to loop 10 times. So I have a itemcount > 10 break; If that works, 5 minutes later I call the procedure again the break set for 20 and I get the error. So within the context of the function call it only gets called once. The whole function gets called multiple times. This is also a multitenant function, so I really want to make sure it doesn't stay active. I don't want it to call the wrong customer's API / data.
@TomMiller-mas I just published a v4.4 to npm that incorporates the work that @domharrington did in #431 to rewrite the internals on the .auth()
call so this error you're having should no longer.
Let me know if it still persists!
I just installed it and ran the function 4 or 5 times in a row with no error. Thanks to both of you for the hard work!!!!