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

Simpler instance creation #707

Merged
merged 38 commits into from Sep 7, 2019

Conversation

szmarczak
Copy link
Collaborator

@szmarczak szmarczak commented Jan 20, 2019

The same instances, but their creation is much simpler.

  • Merging instances:
    Now: instanceA.extend(instanceB, instanceC, ...)
    Before: got.mergeInstances(instanceA, instanceB, instanceC, ...)
  • Merging options:
    Now: instanceA.extend(optionsB, optionsC, ...)
    Before: instanceA.extend(optionsB).extend(optionsC).extend(...)
  • Merging instances and options:
    Now: instanceA.extend(optionsB, instanceC, ...)
    Before: got.mergeInstances(instanceA.extend(optionsB), instanceC)
  • Extending handlers:
    Now: instanceA.extend({handlers: [handlerB]})
    Before: got.mergeInstances(instanceA, got.create({handler: handlerB}))
  • Cloning instances to make them mutable:
    Now: instanceA.extend({mutableDefaults: true}) (not changed)
    Before: instanceA.extend({mutableDefaults: true})

Are there any breaking changes?

  1. got.create({handler: ...}) is pluralized and accepts an array.
  • Before: defaults.handler was a function containing private array of handlers.
  • Now: defaults.handlers is a public array of handlers.

This allows us to inherit properties from the original Promise.
Yup, it's safe to use async handlers now. All the properties (.cancel() etc.) will be proxied.

  1. got.mergeInstances(...instances) is deprecated. Use instanceA.extend(instanceB) instead.

Checklist


IssueHunt Summary

Referenced issues

This pull request has been submitted to:


IssueHunt has been backed by the following sponsors. Become a sponsor

@szmarczak szmarczak changed the title Simpler instances Simpler instance creation Jan 20, 2019
@sindresorhus

This comment has been minimized.

@sindresorhus
Copy link
Owner

Can you show how this will make it easier to create reusable plugins for Got?

@szmarczak szmarczak mentioned this pull request Feb 22, 2019
@szmarczak szmarczak added this to the v10 milestone Apr 2, 2019
@sindresorhus sindresorhus removed this from the v10 milestone Apr 18, 2019
@szmarczak szmarczak added the to be rebased The code needs to be rebased label Jun 21, 2019
@szmarczak szmarczak added breaking API changes / behavior changes and removed to be rebased The code needs to be rebased labels Jun 25, 2019
source/create.ts Outdated Show resolved Hide resolved
source/create.ts Outdated Show resolved Hide resolved
readme.md Show resolved Hide resolved
readme.md Show resolved Hide resolved
@sindresorhus
Copy link
Owner

I like the direction of this. I was sceptical at first with overloading .extend(), but seems very natural now.

@sindresorhus

This comment has been minimized.

@sindresorhus

This comment has been minimized.

@sindresorhus
Copy link
Owner

I still like to see #707 (comment).

@szmarczak
Copy link
Collaborator Author

A breaking change not mentioned:

It is:

  1. Handlers are no longer private.
    Before: defaults.handler was a function containing a private array of handlers.
    Now: defaults.handlers is a public array of handlers.

I still like to see #707 (comment).

I will try to think of something.

@szmarczak
Copy link
Collaborator Author

szmarczak commented Jul 11, 2019

Can you show how this will make it easier to create reusable plugins for Got?

https://github.com/szmarczak/got/blob/simpler-instances/documentation/lets-make-a-plugin.md

In this case gh-got is handler-oriented.
I could add a paragraph about mixing gh-got with the Limiting download & upload instance.
Anyway you can compare the old advanced-creation.md and the new one :)
What do you think?

I'll mention about mixing it with logger instances when I submit a PR for #559.

@szmarczak
Copy link
Collaborator Author

szmarczak commented Jul 12, 2019

Had to force push because GitHub said "there's a conflict", but there wasn't. As you can see there are no changes between 31e190f and 30cea78.

sorry didn't mean to close the PR

@szmarczak szmarczak closed this Jul 12, 2019
@szmarczak szmarczak reopened this Jul 12, 2019
readme.md Outdated Show resolved Hide resolved
documentation/advanced-creation.md Outdated Show resolved Hide resolved
documentation/advanced-creation.md Outdated Show resolved Hide resolved
documentation/advanced-creation.md Outdated Show resolved Hide resolved
documentation/advanced-creation.md Outdated Show resolved Hide resolved
documentation/advanced-creation.md Outdated Show resolved Hide resolved
documentation/lets-make-a-plugin.md Outdated Show resolved Hide resolved
documentation/lets-make-a-plugin.md Outdated Show resolved Hide resolved
readme.md Outdated Show resolved Hide resolved
readme.md Outdated Show resolved Hide resolved
source/as-promise.ts Outdated Show resolved Hide resolved
szmarczak and others added 25 commits September 7, 2019 14:46
Co-Authored-By: Sindre Sorhus <sindresorhus@gmail.com>
Co-Authored-By: Sindre Sorhus <sindresorhus@gmail.com>
Co-Authored-By: Sindre Sorhus <sindresorhus@gmail.com>
Co-Authored-By: Sindre Sorhus <sindresorhus@gmail.com>
Co-Authored-By: Sindre Sorhus <sindresorhus@gmail.com>
Co-Authored-By: Sindre Sorhus <sindresorhus@gmail.com>
Co-Authored-By: Sindre Sorhus <sindresorhus@gmail.com>
Co-Authored-By: Sindre Sorhus <sindresorhus@gmail.com>
Co-Authored-By: Sindre Sorhus <sindresorhus@gmail.com>
Co-Authored-By: Sindre Sorhus <sindresorhus@gmail.com>
Co-Authored-By: Sindre Sorhus <sindresorhus@gmail.com>
Co-Authored-By: Sindre Sorhus <sindresorhus@gmail.com>
Co-Authored-By: Sindre Sorhus <sindresorhus@gmail.com>
Co-Authored-By: Sindre Sorhus <sindresorhus@gmail.com>
Co-Authored-By: Sindre Sorhus <sindresorhus@gmail.com>
Co-Authored-By: Sindre Sorhus <sindresorhus@gmail.com>
Co-Authored-By: Sindre Sorhus <sindresorhus@gmail.com>
Co-Authored-By: Sindre Sorhus <sindresorhus@gmail.com>
Co-Authored-By: Sindre Sorhus <sindresorhus@gmail.com>
@sindresorhus sindresorhus merged commit 8eaef94 into sindresorhus:master Sep 7, 2019
@sindresorhus
Copy link
Owner

Looks great! 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking API changes / behavior changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

XSRF TOKEN support The handler method in got.create() should be able to be an async function
2 participants