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: add support for async based resource operation handlers #523

Merged
merged 8 commits into from
Aug 20, 2024

Conversation

pablopalacios
Copy link
Contributor

@pablopalacios pablopalacios commented Aug 18, 2024

I confirm that this contribution is made under the terms of the license found in the root directory of this repository's source tree and that I have the authority necessary to make this contribution on behalf of its copyright owner.

This PR enables fetchr to be used without any callback.

Before

function create(req, resource, params, body, config, callback) { 
    callback(err, data, meta)
}

After

async function create({ req, resource, params, body, config }) {
    return { data, meta }
 }

It's still possible to use the old signature since we detect the new one based on the number or arguments defined in the handler.

This change also make it simple to create handlers that do not require any argument:

Before

function create(req, resource, params, body, config, callback) {
    callback({ ok: true });
}

After

async function create() {
    return { data: { ok: true } };
}

The callback passed to .end method receives 3 arguments: err, data and
meta. Therefore, we don't need to capture meta in the serviceMeta
array as we used to do.
- Remove unnecessary setImmediate (since we started using promises, we
are not blocking the main thread anymore).

- Move _executeRequest to Request class (since it operates quite
heavily on the request object).

- _executeRequest always resolves now. The resolved object contains an
err, data and meta properties. This change allows us to simplify .end
and ._captureMetaAndStats methods.

- _captureMetaAndStats is simpler now, it takes the err and meta
object directly so we don't need to guess if they exist or not.
With this change, it's now possible to use fetchr fully
callbackless. If any resource handler is defined with one or less
arguments, we expect this resource to be async and we pass all the
arguments in just one object to it (which also simplifies the
signature of the handlers).
README.md Outdated Show resolved Hide resolved
Copy link
Contributor

@redonkulus redonkulus left a comment

Choose a reason for hiding this comment

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

This is great, all backwards compatible right? Tested on a real app?

@pablopalacios
Copy link
Contributor Author

pablopalacios commented Aug 20, 2024

@redonkulus yes, I tried with npm link on our main website code with both versions running (callback and async). All good!

@redonkulus redonkulus merged commit 1ea1cad into yahoo:main Aug 20, 2024
2 checks passed
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