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

V12 #151

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft

V12 #151

wants to merge 3 commits into from

Conversation

thgreasi
Copy link
Contributor

@thgreasi thgreasi commented Aug 13, 2020

  • Convert to typescript and publish typings
  • Add a required baseUrl parameter
  • Default refreshToken to true only when the target provided request.send baseUrl/url option matches the default baseUrl option passed to the factory function.
  • change the refreshToken endpoint to /user/v1/refresh-token

See: https://www.flowdock.com/app/rulemotion/resin-frontend/threads/XVvB7BhMFMXAXsQNwACFg5seFyQ

@thgreasi thgreasi self-assigned this Aug 13, 2020
lib/request.ts Outdated
if (options == null) {
options = {};
options = {} as BalenaRequestOptions;
Copy link
Contributor

Choose a reason for hiding this comment

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

This should not need to be cast, if options is actually optional and therefore every property is actually optional then the types should reflect that and not need casting. In fairness though I wouldn't be surprised if it's actually required and this defaulting should be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So as part of this commit I just converted to TS, but I agree on removing all such defaulting so that we can get rid of this weird casting.

tsconfig.json Outdated
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"module": "commonjs",
"declaration": false,
"declaration": true,
"outDir": "build",
"noImplicitAny": false,
Copy link
Contributor

Choose a reason for hiding this comment

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

If you've converted it all to typescript then you can set this to true

Suggested change
"noImplicitAny": false,
"noImplicitAny": true,

and also remove the checkJs/allowJs below

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Going for "strict": true, instead since it enables this as well.

package.json Outdated
Comment on lines 21 to 22
"lint": "balena-lint -e js -e ts --typescript lib",
"lint-fix": "balena-lint -e js -e ts --typescript --fix lib",
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there still any js? If not you can remove the extension args altogether

lib/request.ts Outdated
Comment on lines 189 to 209
return exports.interceptors
.slice()
.reverse()
.reduce(function (promise, { response, responseError }) {
if (response != null || responseError != null) {
return promise.then(response, responseError);
} else {
return promise;
}
}, initialPromise);
Copy link
Contributor

Choose a reason for hiding this comment

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

If you're able to simplify this reduce promise chain building usign async/await I would love that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me try that as a minor after we gather all the breaking changes.

lib/request.ts Outdated
Comment on lines 189 to 202
return exports.interceptors
.slice()
.reverse()
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it'd be worth thinking about making the exposed interface be addInterceptor: (interceptor: Interceptor) => void instead of interceptions: Interceptor[], that way internally addInterceptor can do interceptors.unshift(interceptor) and we can avoid the need to clone/reverse the array for every request and therefore reduce GC pressure

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 like the idea but this would require a breaking change probably in the SDK, and we are doing major releases often enough there lately 🤔

lib/request.ts Outdated
@@ -328,7 +388,6 @@ export function getRequest({
* throw error
* )
*/
exports.interceptors = interceptors;
Copy link
Contributor

Choose a reason for hiding this comment

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

All the docs for inteceptors are directly above this, I think you'll need to move the docs somewhere else now for it to all format/generate correctly

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Had some trouble to get this right but eventually I had to take a shortcut. Hope you are fine with it :)
Fun fact, the doc generation was already broken on master...

lib/request.ts Outdated
Comment on lines 439 to 447
async function refreshToken({
baseUrl,
}: Pick<BalenaRequestOptions, 'baseUrl'>): Promise<string> {
Copy link
Contributor

Choose a reason for hiding this comment

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

There is a question to be asked here as to whether "balena-request" should know about refreshing tokens or whether that should actually be moved into "balena-auth" instead?

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 wasn't part of the discussion when this was added, but I'm guessing that the reason might be that balena-auth doesn't have access to the balena-request instance, but it's the other way around.
And since balena-request atm does refresh the token when before doing a request when the token is old enough, so we will have to think about a way that we don't have a weird circular dependency schema.

lib/progress.ts Outdated
Comment on lines 83 to 85
if (requestAsync == null) {
requestAsync = utils.getRequestAsync();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's worth converting all these nullish defaults to es6 defaults instead since we're doing a breaking

Copy link
Contributor Author

@thgreasi thgreasi Aug 14, 2020

Choose a reason for hiding this comment

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

Will do.

lib/progress.ts Outdated
@@ -105,13 +110,13 @@ export function estimate(requestAsync, isBrowser) {

const stream = require('stream');
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you go through and make sure all requires like this have an added as typeof import... - in javascript the require call itself is typed when possible but in typescript you have to explicitly type it, meaning with the conversion we'll actually have lost typings in some places

Change-type: squash
Signed-off-by: Thodoris Greasidis <[email protected]>
Change-type: squash
Signed-off-by: Thodoris Greasidis <[email protected]>
Resolves: #96
Change-type: major
Signed-off-by: Thodoris Greasidis <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants