heroiclabs / nakama-js

JavaScript client for Nakama server written in TypeScript.
https://heroiclabs.com/docs/nakama/client-libraries/javascript/
Apache License 2.0
182 stars 54 forks source link

'cocos-ignore' is not valid enum #192

Open Bl4ckburn opened 2 months ago

Bl4ckburn commented 2 months ago

Hey everyone, I am currently using nakama-js in combination with ionic (capacitor) and after upgrading the version of capacitor-android to 5.4.0 (minimum) and compile project to APK it shows an error in app from nakama (even though I don't use the cocos engine):

TYPEERROR: FAILED TO CONSTRUCT 'REQUEST': FAILED TO READ THE 'CREDENTIALS' PROPERTY FROM 'REQUESTINIT': THE PROVIDED VALUE 'COCOS-IGNORE' IS NOT A VALID ENUM VALUE OF TYPE REQUESTCREDENTIALS.

It looks as if newer versions of lib.dom block the ability to change 'credentials'. My proposal is to simply checking whether the 'credentials' property is readonly, just to start with. I don't know how the cocos engine itself reacts to the change.

checking function:

function isWritable<T extends Object>(obj: T, key: keyof T) {
  const desc = Object.getOwnPropertyDescriptor(obj, key) || {}
  return Boolean(desc.writable)
}

and change condition line before credentials assign in buildFetchOptions function from: if (!descriptor?.set) { to if (!descriptor?.set && isWritable(fetchOptions, 'credentials')) {

I would be grateful if someone had a better idea on how to solve this problem, without cutting off the cocos engine in the future by above condition.

lugehorsam commented 2 months ago

Hey @Bl4ckburn it appears that in the Ionic runtime, the credentials property is not writable, whereas in cocos, it actually just lacks a setter. So if you want to submit a PR with your change where we keep the existing logic but change cocos-ignore to just ignore (to make it more generic) and to also skip assignment if the credentials property is not writable, and test that on Ionic that'd definitely be appreciated.

Bl4ckburn commented 2 months ago

Can you tell me how options params object looks like in cocos engine after executing buildFetchOptions function?

After deeper analysis it comes out that in WEB and Mobile APP options is always an empty object (in my case), so my isWritable function always return false. Thats why it's working. I am afraid that this condition may completely block the cocos engine from replacing credentials.

Just in case you're wondering what values ​​"credentials" can take.

type RequestCredentials = "include" | "omit" | "same-origin";

That is code from lib.dom.d.ts in typescript lib.

lugehorsam commented 2 months ago

Hey @Bl4ckburn unfortunately this is not something I can prioritize at the moment, but you are welcome to investigate.