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

Update to Got 9, rename endpoint option, and require Node.js 8 #29

Merged
merged 15 commits into from
Aug 23, 2018
Merged

Conversation

szmarczak
Copy link
Collaborator

@szmarczak szmarczak commented Aug 4, 2018

Fixes #24
Fixes #25

index.js Outdated
'head',
'delete'
];
if (options.method && options.method.toLowerCase() === 'put' && !options.body) {
Copy link
Owner

Choose a reason for hiding this comment

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

Don't we normalize the casing of options.method? If not, we should.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No. I'll send a PR.

index.js Outdated
ghGot[x] = (url, opts) => ghGot(url, Object.assign({}, opts, {method}));
ghGot.stream[x] = (url, opts) => ghGot.stream(url, Object.assign({}, opts, {method}));
}
return next(options).catch(err => {
Copy link
Owner

Choose a reason for hiding this comment

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

Can you make the handler function async and use await and normal try/catch here?

Copy link
Collaborator Author

@szmarczak szmarczak Aug 6, 2018

Choose a reason for hiding this comment

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

That's not currently possible, because got assumes this is a sync function.

Oh, wait. It returns a Promise anyway :P

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Nah. That breaks streams.

Copy link
Owner

@sindresorhus sindresorhus Aug 6, 2018

Choose a reason for hiding this comment

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

I don't see how that would break streams. Can you push what you changed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't see how that would break streams.

If handler is async, it'll return a Promise and not a stream.

Can you push what you changed?

Are you still sure?

Copy link
Owner

Choose a reason for hiding this comment

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

Oh, I had assumed handler could be an async function too, like the beforeRequest handler. I think the handler should be able to be an async function. I see many use-cases where you want to do some async stuff there.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, that'd be very nice. But there's another problem: need to do a wrapper for .cancel() and .on(). We could accept handler also as an object containing two properties: streamHandler and promiseHandler.

It'd look like:

// Your handler:
async function promiseHandler(options, next, wrap) {
	const promise = next(options);
	wrap(promise); // You always need to do this :(

	try {
		return await promise;
	} catch (err) {
		// Modify `err` here
		throw err;
	}
}

// create.js (got)
...
	const createWrapper = handler => promise => {
		handler.on = promise.on;
		handler.cancel = promise.cancel;
	};

	function got(url, options) {
		try {
			options = mergeOptions(defaults.options, options);
			const handler = defaults.handler(normalizeArguments(url, options, defaults), next, createWrapper(handler));
			return handler;
		} catch (error) {
			return Promise.reject(error);
		}
	}
...

A better solution would be to create a new hook: onError. You could modify that there.

@sindresorhus sindresorhus changed the title Update to Got 9 Update to Got 9, rename endpoint option, and require Node.js 8 Aug 6, 2018
@sindresorhus
Copy link
Owner

You have to update the docs about the endpoint => baseUrl change.

index.js Outdated
throw err;
});
}
if (options.method && options.method === 'PUT' && !options.body) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Assumes sindresorhus/got#547 is merged.

@szmarczak
Copy link
Collaborator Author

ping. any updates?

index.js Outdated

module.exports = ghGot;
module.exports = create();
module.exports.recreate = create;
Copy link
Owner

@sindresorhus sindresorhus Aug 8, 2018

Choose a reason for hiding this comment

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

If this is only for testing, it should be behind a if (process.env.TEST) { (you'll need ava@1.0.0-beta.6 for this) check, if it's for users too, it should be documented with a use-case.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Isn't that process.env.NODE_ENV === 'test'?

Copy link
Owner

Choose a reason for hiding this comment

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

Yes, I meant process.env.NODE_ENV === 'test'...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's done already :)

@sindresorhus
Copy link
Owner

sindresorhus commented Aug 8, 2018

ping. any updates?

Isn't this blocked by sindresorhus/got#547?

@szmarczak
Copy link
Collaborator Author

Isn't this blocked by sindresorhus/got#547?

Yup. I just wanted to know if there's anything else to do :) (thanks for the review)

index.js Outdated

const url = /^https?/.test(path) ? path : opts.endpoint + path;
token: process.env.GITHUB_TOKEN,
baseUrl: process.env.GITHUB_ENDPOINT ? process.env.GITHUB_ENDPOINT.replace(/[^/]$/, '$&/') : 'https://api.github.com/',
Copy link
Owner

Choose a reason for hiding this comment

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

This can be simplified to:

baseUrl: process.env.GITHUB_ENDPOINT || 'https://api.github.com'

package.json Outdated
"is-plain-obj": "^1.1.0"
},
"devDependencies": {
"ava": "*",
"ava": "^1.0.0-beta.6",
"get-stream": "^3.0.0",
Copy link
Owner

Choose a reason for hiding this comment

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

Also update AVA and get-stream.

}
if (options.method && options.method === 'PUT' && !options.body) {
options.headers['content-length'] = 0;
}
Copy link
Owner

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

+1

index.js Outdated
});
}
return next(options).catch(err => {
if (err.response && isPlainObj(err.response.body)) {
Copy link
Owner

Choose a reason for hiding this comment

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

Do we really need to check if body is a plain object? I think we can just do if (err.response && err.response.body) {.

@sindresorhus
Copy link
Owner

In case you missed it: #29 (comment)

@sindresorhus sindresorhus merged commit 402f07d into sindresorhus:master Aug 23, 2018
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