mean-expert-official / loopback-sdk-builder

Tool for auto-generating Software Development Kits (SDKs) for LoopBack
Other
399 stars 178 forks source link

Some fixes for react native #602

Open Djinne opened 6 years ago

Djinne commented 6 years ago

What type of issue are you creating?

What version of this module are you using?

Write other if any: "@mean-expert/loopback-sdk-builder": "^2.3.1",

Please add a description for your issue:

I was building this for react native and found some issues and seemed to have fixed them: base.service.js

if (postBodyKeys.length === 1) { body = postBody[postBodyKeys.shift()]; } else if(postBodyKeys.length> 1) { body = postBody; } else{ body=undefined; } instead of: if (postBodyKeys.length === 1) { body = postBody[postBodyKeys.shift()]; } else if(postBodyKeys.length> 1) { body = postBody; }

because it was making the body "{}" instead of undefined which broke the get requests

moved the 'from' from import { Observable, from, Subject } from 'rxjs' to: import { from } from 'rxjs/observable/from'; Because it wasn't getting the dependency for some reason

import { _throw } from 'rxjs/observable/throw'; return _throw(error || 'Server error');

instead of throwError in the error.service.js

Djinne commented 6 years ago

I also remove if (instance) { return instance; } from base.service.js because it was not allowing new classes to be used. IE if i wrote var x=new Api.SDKModule({services:["PersonApi","ApplicationApi"]}); then the applicationApi will be overwritten by personApi because it was first

jonathan-casarrubias commented 6 years ago

@Djinne do you think you can make a PR including those patches? that would be awesome for the community using react.

Cheers Jon

Djinne commented 6 years ago

I cannot comment on whether this works on normal ReactJS as I'm using react native. Sorry I'm new, how do you create a pull request? I have pulled the master branch now how do I push to a new branch so I cant install it with npm? I have made changes in the ejs files but getting the dreaded "ejs.filters.q = (obj) => JSON.stringify(obj, null, 2); TypeError: Cannot set property 'q' of undefined"

kind regards Djinne

Djinne commented 6 years ago

Okey, easy fix of copying the node_modules folder from your master branch. How do I push to a branch that you have set up for review? Its working on my my end perfectly, just need someone to test the angular2 side of it to make sure I haven't broken it

Djinne commented 6 years ago

I think the best thing would be to separate reactJs from reactNative because the localstorage in react native is Async :/

jonathan-casarrubias commented 6 years ago

@Djinne thanks for looking into this, the ejs.filters.q issue is result of an updated version of EJS, by installing the version defined within the package.json would remove that issue.

If the react and reactnative apis are not the same, you might want to add a new flag for reactnative and condition that patch you have worked on

Djinne commented 6 years ago

My final hurdle now is trying to get the Auth in each model to update. It feels like there is a race condition with the async local-storage and setting of the auth token in each model. Leading to some models with the accesstoken and some without.

I'm currently adding an updateAll in the main index.js so after logging in it loops through each model API to update to the new token. But i think the Object.keys(this).map is overwritting the 'this' while in the map loop. I am pretty sure there is a less hacky way of doing all this though

Perhaps a manual check on each request to get the token directly from storage in the base.services.js though the auth class? Instead of the Auth classes property (which starts off null)

Djinne commented 6 years ago

Either that or don't set a new Auth class for each api model in the base model. And somehow wrap all the models in one auth class

Djinne commented 6 years ago

I don't know how bad of a fix it is, but I made the token in auth.service a static variable which can be accessed directly with LoopbackAuth.token or one of the methods that return it such as

getCurrentUserId() { return LoopBackAuth.token.userId; }

This has the benefit of keeping all models with an auth class with a common token between them

collaorodrigo7 commented 6 years ago

Hey @Djinne !, Great you figure all that out! Did you ever had the chance to push to a PR?. If not do you have your changes accessible anywhere? I am currently working on a React Native project, and might be able to help improve some stuff as well.

Djinne commented 6 years ago

Hi @collaorodrigo7,

No I haven't got round to it, I will soon though, I did find one more issue with the order of the post requests for the custom models, they should have the urlParams and routeParams swopped around.

let result = this.request(_method, _url, _urlParams,_routeParams, _postBody, null, customHeaders);

collaorodrigo7 commented 6 years ago

thanks, I will be looking at it in more detail soon!. let me know once you make your code available somewhere

Amenocy commented 5 years ago

you can check my fork, i made some fixes , but bugs still remains ...