Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/inject cozy client #461

Merged
merged 19 commits into from
Feb 25, 2019
Merged

Feat/inject cozy client #461

merged 19 commits into from
Feb 25, 2019

Conversation

edas
Copy link
Contributor

@edas edas commented Feb 15, 2019

L'objectif est de permettre d'injecter directement cozy-client dans la barre plutôt que de transmettre des token et recréer un mini client interne

@edas edas requested a review from CPatchane as a code owner February 15, 2019 14:46
@gregorylegarec
Copy link
Contributor

Quick review : totally the right direction 👍

src/lib/stack-client.js Outdated Show resolved Hide resolved
src/lib/stack-client.js Outdated Show resolved Hide resolved
src/lib/stack-client.js Outdated Show resolved Hide resolved
src/lib/stack-client.js Outdated Show resolved Hide resolved
@edas edas force-pushed the feat/injectCozyClient branch 3 times, most recently from 7e1ca5f to 9720850 Compare February 15, 2019 17:57
@edas
Copy link
Contributor Author

edas commented Feb 15, 2019

I welcome any help to see how to test this PR.

My solution is to initialize a new blank app, initialize a cozy client, initialize the bar with it, put my app in a cozy… I am sure one of you could look at it more quickly

src/lib/normalize-url.js Outdated Show resolved Hide resolved
src/lib/normalize-url.js Outdated Show resolved Hide resolved
src/lib/normalize-url.js Outdated Show resolved Hide resolved
src/lib/normalize-url.js Outdated Show resolved Hide resolved
src/lib/normalize-url.js Outdated Show resolved Hide resolved
src/lib/normalize-url.js Outdated Show resolved Hide resolved
src/lib/stack-client.js Outdated Show resolved Hide resolved
src/lib/stack-client.js Outdated Show resolved Hide resolved
The bar takes either a full URL (mostly from mobile apps) or a domain without 
protocol (mostly in case of web apps). This component takes care of normalizing all 
cases to a full url object, in accordance to the `secure` optional parameter.

The purpose is to replace all other URL code in cozy-bar.
@edas edas force-pushed the feat/injectCozyClient branch from 4e97ccc to f6b6586 Compare February 17, 2019 19:21
src/lib/realtime.js Outdated Show resolved Hide resolved
src/lib/stack-client.js Outdated Show resolved Hide resolved
Copy link
Contributor

@ptbrowne ptbrowne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Main comments

  • Prefer to use async/await since it is more resilient to synchronous exceptions before the Promise is returned
  • Some jest fu

jest.fn().mockResolvedValue()
jest.spyOn()

My takeaways from this review

  • Normalize early so that downstream functions have less work to do
  • Introduce a proxy object to switch between legacy code and old code

src/lib/icon.js Show resolved Hide resolved
src/lib/icon.js Outdated Show resolved Hide resolved
src/lib/icon.js Outdated Show resolved Hide resolved
src/lib/stack.js Show resolved Hide resolved
const init = function(options) {
cozyClient = options.cozyClient
const legacyOptions = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const stackClient = getStackClient() so that it is not repeated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last stage will only have one call to getStackClient() so the repetition is only visible between commits of this PR

const getApp = function(slug) {
if (!slug) {
throw new Error('Missing slug')
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe have this function async ?

IMHO, instead of throwing, a Promise.reject() would be better since the normal return type is a promise and the calling code will expect a promise here. Anyway if the calling code is using await it will work.

src/lib/stack-client.js Show resolved Hide resolved
* @returns {Object} {usage, quota, isLimited}
*/
const getStorageData = function() {
return fetchJSON('GET', '/settings/disk-usage').then(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async/await ?

test/lib/stack-client/stack-client.cozyfetchjson.spec.js Outdated Show resolved Hide resolved
src/lib/realtime.js Outdated Show resolved Hide resolved
src/lib/stack-client.js Show resolved Hide resolved
src/lib/stack-client.js Show resolved Hide resolved
return function() {
if (cache === undefined) {
return fn().then(result =>
cache = result
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't a return missing here? You save the result in the cache, but the result is no longer available to the called of withCache.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And testing withCache would be a good idea. It's a simple function to test, but having the right implementation on the first try is harder.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used to not test internal non exported functions.

As long it's used only one time, let's consider it as an internal mechanism (in the legacy code it was merged in the getContext function, I only externalized it to be more readable).

If it ever begin to be used more in the future, then let's put it in its own file, with an export and a dedicated test

src/lib/stack-client.js Show resolved Hide resolved
src/lib/stack-client.js Outdated Show resolved Hide resolved
src/lib/stack-internal.js Show resolved Hide resolved
src/lib/stack-internal.js Show resolved Hide resolved
src/lib/stack.js Show resolved Hide resolved
edas added 2 commits February 20, 2019 10:44
- Remove `ssl` parameter normalization in the main index.jsx (send it as is to the lib/stack.js)
- Remove the old inline url management code from lib/stack.js in favor of a call to the new url normalization component
The parameter is not used in the receiving function anyways.
@edas edas force-pushed the feat/injectCozyClient branch 2 times, most recently from 9652033 to 45adf9a Compare February 20, 2019 10:09
edas added 4 commits February 20, 2019 12:20
We now pass the `getApp` function in parameter (it was previously a closure) but there should be no change to the underlying function.
Prepare two files :
- lib/stack-internal.js which will be the default legacy client (exact same code)
- lib/stack-client.js which will handle cozy client

For now stack-client.js and the main stack.js are only wrappers to the legacy client.
We introduce a proxy to access the stack client functions. It will redirect to the legacy one or to the cozy-client based one depending on initialization.
@edas edas force-pushed the feat/injectCozyClient branch from 45adf9a to 47a98af Compare February 20, 2019 11:42
@edas
Copy link
Contributor Author

edas commented Feb 21, 2019

[x] Code is tested in a sample dev app with a legacy client
[x] Code is tested in a sample dev app with a cozy-client instance

I should have answered all major feedback except:

Please tell me if one of these choices is a problem for you. Otherwise it's probably ok to merge. I plan a simple merge with no squash (all commits should be functional on their own, it will simplify future debug) but this is open to feedback.

Waiting for formal 👍 after this comment before merging

@edas edas changed the title [WIP] Feat/inject cozy client Feat/inject cozy client Feb 22, 2019
Copy link
Contributor

@gregorylegarec gregorylegarec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job 🎉

Just a little detail, our base eslint config implies eol-last set to default value always, so I think that all our text editor will try to fix this in our future commits.

By the way a yarn lint did not detect the problem. I do not understand why.

*/
const current = function() {
if (stack === undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: It seems that if(stack) is enough here. We will not have the case but with this test if stack is null the test will return false and we will not throw the following error.

@drazik drazik self-requested a review February 22, 2019 22:04
edas added 12 commits February 25, 2019 09:48
Also in this commit : how to forward a fetch to cozyClient with `fetchJSON()` in a way compatible with the legacy internal client
With an utility function to cache the result
The way we return the data for an instance without quota is really unsatisfying. We should probably plan to do something smarter in the future
simple wrapper around internal `fetchJSON()` maintaining compatibility with the old cozy-client-js
* Finish the `init` function so it directly initializes the realtime
* Do not initialize or reference the old internal client anymore
@edas edas force-pushed the feat/injectCozyClient branch from 83e21b5 to fc2d7c1 Compare February 25, 2019 08:48
@edas
Copy link
Contributor Author

edas commented Feb 25, 2019

linting issues fixed

@edas edas merged commit c05eec6 into master Feb 25, 2019
@cozy-bot
Copy link

🎉 This PR is included in version 6.14.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@kosssi kosssi deleted the feat/injectCozyClient branch February 25, 2019 09:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants