swellstores / swell-js

JS library for building storefronts and checkouts with Swell ecommerce.
https://swell.is
MIT License
139 stars 30 forks source link

React Native support #38

Open codingstyle opened 3 years ago

codingstyle commented 3 years ago

I am currently integrating the SDK in a React Native app written mainly with TypeScript. So far Swell JS SDK works well, there are only two blocking issues:

1) Buffer has to be set as a global variable to that SDK works correctly 2) Session management relies exclusively on browser cookies. Maybe there is an abstraction layer to add for supporting both web and React Native?

For the moment, I have created a quick and dirty workaround to emulate document object (cookies are saved separately using AsyncStorage).

This is main index.js :

import buffer from 'buffer';
global.Buffer = buffer.Buffer;

import SwellDocument from 'utils/SwellDocument';
global.document = new SwellDocument();

And this is SwellUtils.tsfile for the cookie workaround:

class SwellCookie {
  public stringValue: string = '';

  public match = (re: RegExp): RegExpMatchArray | null => {
    return this.stringValue.match(re);
  }

  public toString = (): string => {
    return this.stringValue;
  }
};

export default class SwellDocument {
  private _cookie: SwellCookie = new SwellCookie();

  get cookie(): SwellCookie | string {
    return this._cookie;
  }

  set cookie(value: SwellCookie | string) {
      this._cookie.stringValue = value as string;
  }
};
ericingram commented 3 years ago

@codingstyle glad you brought this up and we definitely want to support React Native. I suppose we could test whether global.document exists and if not use a mock like your example.