yahoo / fetchr

Universal data access layer for web applications.
Other
453 stars 83 forks source link

feat: add support for async based resource operation handlers #523

Closed pablopalacios closed 4 weeks ago

pablopalacios commented 1 month ago

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 } };
}
pablopalacios commented 1 month ago

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