Azure / azure-sdk-for-js

This repository is for active development of the Azure SDK for JavaScript (NodeJS & Browser). For consumers of the SDK we recommend visiting our public developer docs at https://docs.microsoft.com/javascript/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-js.
MIT License
2.03k stars 1.19k forks source link

[cosmos] React Native support #30500

Open pTinosq opened 1 month ago

pTinosq commented 1 month ago

Is your feature request related to a problem? Please describe. This library doesn't seem to work with React Native CLI projects

Describe the solution you'd like Some sort of polyfill / other solution for projects that want to use libraries that will work with react native

Describe alternatives you've considered We have considered creating a fork with these updated changes.

Additional context The error we're getting is

error: Error: Unable to resolve module isomorphic-webcrypto from C:\Users\x\OneDrive - x\Desktop\test-cosmos-port\AwesomeProject\node_modules\@azure\cosmos\dist-esm\src\utils\globalCrypto.native.js: isomorphic-webcrypto could not be found within the project or in these directories:
  node_modules
  4 | // doing so requires adding a bunch of react packages as peer dependencies. So,
  5 | // it is being loaded dynamically here to not cause compiler error.
> 6 | const globalCrypto = require("isomorphic-webcrypto"); // eslint-disable-line import/no-extraneous-dependencies, @typescript-eslint/no-require-imports
    |                               ^
  7 | export { globalCrypto };
  8 | //# sourceMappingURL=globalCrypto.native.js.map
pTinosq commented 1 month ago

I have created this shims.ts file:

import '@reatre/core-asynciterator-polyfill';
import * as crypto from 'isomorphic-webcrypto';
import 'react-native-get-random-values';
import 'react-native-url-polyfill/auto';
import 'text-encoding-polyfill';
const getRandomValues = global.crypto.getRandomValues;
global.crypto = crypto;
global.crypto.getRandomValues = getRandomValues;

I constantly get this error and cannot seem to find a way around it: [TypeError: Cannot read property 'userConsistencyPolicy' of null]

It seems to always occur when trying to interact with a client that is created using a connection string

const client = new CosmosClient(
      'AccountEndpoint=https://xxx.documents.azure.com:443/;AccountKey=KxxxB==;',
    );

client
    .getReadEndpoint()
    .then(endpoint => {
      console.log('Read endpoint:', endpoint);
    })
    .catch(error => {
      console.error(error);
    });

Any support would be greatly appreciated

jeremymeng commented 1 month ago

maybe a duplicate of https://github.com/Azure/azure-sdk-for-js/issues/26574

also related https://github.com/Azure/azure-sdk-for-js/issues/22628