kintone/js-sdk

`buildBaseUrl()` throws an error when `location` is undefined and not passing `baseUrl`

zaki-yama opened this issue · 3 comments

This behavior is related to #855, but still occurs even after merging #855.

Target Package

  • @kintone/rest-api-client (all versions)

Environment

  • React Native (We confirmed the behavior with Expo Go)

Current Behavior

  • Create a React Native project with expo-cli
$ npm install --global expo-cli
$ expo init my-project
  • Install the latest @kintone/rest-api-client
$ npm install @kintone/rest-api-client
  • Modify App.tsx to create KintoneRestAPIClient without baseUrl
 import React from 'react';
 import { StyleSheet, Text, View } from 'react-native';

+import { KintoneRestAPIClient } from "@kintone/rest-api-client";
+
 export default function App() {
+  const client = new KintoneRestAPIClient();
+
   return (
     <View style={styles.container}>
       <Text>Open up App.tsx to start working on your app!</Text>
  • Run npm start and open the app with Expo Go (in iPhone or Android phone)

Then an error occurs.

ReferenceError: Can't find variable: location

Expected Behavior

It would be nice if we can see more helpful error message.
(For example, "In this environment, `baseUrl` is required" )

Cause

The metro (a bundler for React Native) refers to the main field of package.json and injects browser platform dependencies.
Then, React Native environment throws an error because it doesn't have a location object.

Server log
Started Metro Bundler
iOS Bundling complete 7559ms
iOS Running app on iPhone (4)

ReferenceError: Can't find variable: location

This error is located at:
    in App (created by ExpoRoot)
    in ExpoRoot
    in RCTView (created by View)
    in View (created by AppContainer)
    in DevAppContainer (created by AppContainer)
    in RCTView (created by View)
    in View (created by AppContainer)
    in AppContainer
    in main(RootComponent)
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:95:4 in reportException
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:141:19 in handleException
at node_modules/react-native/Libraries/Core/ReactFiberErrorDialog.js:52:4 in showErrorDialog
at node_modules/react-native/Libraries/ReactNative/renderApplication.js:76:4 in renderApplication
at node_modules/react-native/Libraries/ReactNative/AppRegistry.js:119:25 in runnables.appKey.run
at node_modules/react-native/Libraries/ReactNative/AppRegistry.js:213:4 in runApplication

ReferenceError: Can't find variable: location

This error is located at:
    in App (created by ExpoRoot)
    in ExpoRoot
    in RCTView (created by View)
    in View (created by AppContainer)
    in DevAppContainer (created by AppContainer)
    in RCTView (created by View)
    in View (created by AppContainer)
    in AppContainer
    in main(RootComponent)
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:95:4 in reportException
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:141:19 in handleException
at node_modules/react-native/Libraries/Core/setUpErrorHandling.js:24:6 in handleError
at node_modules/expo-error-recovery/build/ErrorRecovery.fx.js:12:21 in ErrorUtils.setGlobalHandler$argument_0

Proposal

Add the react-native field to package.json and create React Native dependencies.
By default, metro refers react-native field before the browser and main fields.
https://facebook.github.io/metro/docs/configuration/#resolvermainfields

NOTE: metro does not yet support the exports field.
cf. facebook/metro#670 (comment)

When debugging with the local @kintone/rest-api-client, we have to copy the built package to my-project/node_modules.
It's because metro does not support yarn link(facebook/metro#68 and facebook/metro#1).

cp -rf path/to/js-sdk/packages/rest-api-client/ path/to/my-project/node_modules/@kintone/rest-api-client/

We don't support React Native environment but can throw a more helpful error message if location is not defined on browser environments.