Skip to content

url-loader@5.6.0#166

Merged
colbyfayock merged 25 commits intomainfrom
beta
Aug 7, 2024
Merged

url-loader@5.6.0#166
colbyfayock merged 25 commits intomainfrom
beta

Conversation

@colbyfayock
Copy link
Collaborator

This creates a new set of helpers that will help framework libraries reduce the boilerplate needed to instantiate new instances of the upload widget.

  • getUploadWidgetOptions: this takes in a set of options that deal with the configuration of the upload widget, including account credentials and signature callback function
  • generateSignatureCallback: this generates a new callback function that given an endpoint and fetcher (like the fetch API), will make it easier to pass the resulting function to the widget options above
  • generateUploadWidgetResultCallback: when uploading with the widget, different events are produced depending on the state of the upload, this helps manage all of the different events via a single callback function with the ability to pass in and tap into the different events

Issue Ticket Number

Fixes #164

Usage

const signatureCallback = generateSignatureCallback({
  signatureEndpoint: '/asdf',
  fetch
})

const uploadOptions = getUploadWidgetOptions({
  signatureCallback
}, {
  cloud: {
    cloudName: 'testcloud',
    apiKey: 'abcd1234'
  }
});

const resultsCallback = generateUploadWidgetResultCallback({
  onError: (uploadError) => {
    // Example on storing error
    // setError(uploadError);
  },
  onResult: (uploadResult) => {
    // Example of storing results on any event
    // setResults(uploadResult);
  },
  onSuccess: (uploadResult) => {
    // Tapping into individual events
  }
});

cloudinary.createUploadWidget(uploadOptions, resultsCallback);

Description

Issue Ticket Number

Fixes #<ISSUE_NUMBER>

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Fix or improve the documentation
  • This change requires a documentation update

Checklist

  • I have followed the contributing guidelines of this project as mentioned in CONTRIBUTING.md
  • I have created an issue ticket for this PR
  • I have checked to ensure there aren't other open Pull Requests for the same update/change?
  • I have performed a self-review of my own code
  • I have run tests locally to ensure they all pass
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes needed to the documentation

colbyfayock and others added 7 commits August 2, 2024 11:53
…adWidgetResultCallback (#165)

# Description

This creates a new set of helpers that will help framework libraries
reduce the boilerplate needed to instantiate new instances of the upload
widget.

* getUploadWidgetOptions: this takes in a set of options that deal with
the configuration of the upload widget, including account credentials
and signature callback function
* generateSignatureCallback: this generates a new callback function that
given an endpoint and fetcher (like the fetch API), will make it easier
to pass the resulting function to the widget options above
* generateUploadWidgetResultCallback: when uploading with the widget,
different events are produced depending on the state of the upload, this
helps manage all of the different events via a single callback function
with the ability to pass in and tap into the different events

## Issue Ticket Number

Fixes #164 

## Usage

```
const signatureCallback = generateSignatureCallback({
  signatureEndpoint: '/asdf',
  fetch
})

const uploadOptions = getUploadWidgetOptions({
  signatureCallback
}, {
  cloud: {
    cloudName: 'testcloud',
    apiKey: 'abcd1234'
  }
});

const resultsCallback = generateUploadWidgetResultCallback({
  onError: (uploadError) => {
    // Example on storing error
    // setError(uploadError);
  },
  onResult: (uploadResult) => {
    // Example of storing results on any event
    // setResults(uploadResult);
  },
  onSuccess: (uploadResult) => {
    // Tapping into individual events
  }
});

cloudinary.createUploadWidget(uploadOptions, resultsCallback);
```
# [@cloudinary-util/url-loader-v5.4.0-beta.1](https://github.com/cloudinary-community/cloudinary-util/compare/@cloudinary-util/url-loader-v5.3.1...@cloudinary-util/url-loader-v5.4.0-beta.1) (2024-08-02)

### Features

* getUploadWidgetOptions, generateSignatureCallback, generateUploadWidgetResultCallback ([#165](#165)) ([4e3adc4](4e3adc4)), closes [#164](#164)
# [@cloudinary-util/types-v1.2.0-beta.1](https://github.com/cloudinary-community/cloudinary-util/compare/@cloudinary-util/types-v1.1.0...@cloudinary-util/types-v1.2.0-beta.1) (2024-08-02)

### Features

* getUploadWidgetOptions, generateSignatureCallback, generateUploadWidgetResultCallback ([#165](#165)) ([4e3adc4](4e3adc4)), closes [#164](#164)
# [@cloudinary-util/url-loader-v5.5.0-beta.1](https://github.com/cloudinary-community/cloudinary-util/compare/@cloudinary-util/url-loader-v5.4.0...@cloudinary-util/url-loader-v5.5.0-beta.1) (2024-08-02)

### Features

* getUploadWidgetOptions, generateSignatureCallback, generateUploadWidgetResultCallback ([#165](#165)) ([4e3adc4](4e3adc4)), closes [#164](#164)
# [@cloudinary-util/util-v3.2.0-beta.1](https://github.com/cloudinary-community/cloudinary-util/compare/@cloudinary-util/util-v3.1.0...@cloudinary-util/util-v3.2.0-beta.1) (2024-08-02)

### Features

* getUploadWidgetOptions, generateSignatureCallback, generateUploadWidgetResultCallback ([#165](#165)) ([4e3adc4](4e3adc4)), closes [#164](#164)
# [@cloudinary-util/url-loader-v5.5.0-beta.1](https://github.com/cloudinary-community/cloudinary-util/compare/@cloudinary-util/url-loader-v5.4.0...@cloudinary-util/url-loader-v5.5.0-beta.1) (2024-08-02)

### Features

* getUploadWidgetOptions, generateSignatureCallback, generateUploadWidgetResultCallback ([#165](#165)) ([4e3adc4](4e3adc4)), closes [#164](#164)
colbyfayock and others added 15 commits August 2, 2024 12:33
# [@cloudinary-util/util-v3.2.0-beta.1](https://github.com/cloudinary-community/cloudinary-util/compare/@cloudinary-util/util-v3.1.0...@cloudinary-util/util-v3.2.0-beta.1) (2024-08-02)

### Features

* getUploadWidgetOptions, generateSignatureCallback, generateUploadWidgetResultCallback ([#165](#165)) ([4e3adc4](4e3adc4)), closes [#164](#164)
# [@cloudinary-util/types-v1.2.0-beta.1](https://github.com/cloudinary-community/cloudinary-util/compare/@cloudinary-util/types-v1.1.0...@cloudinary-util/types-v1.2.0-beta.1) (2024-08-02)

### Features

* getUploadWidgetOptions, generateSignatureCallback, generateUploadWidgetResultCallback ([#165](#165)) ([4e3adc4](4e3adc4)), closes [#164](#164)
# [@cloudinary-util/url-loader-v5.6.0-beta.1](https://github.com/cloudinary-community/cloudinary-util/compare/@cloudinary-util/url-loader-v5.5.0...@cloudinary-util/url-loader-v5.6.0-beta.1) (2024-08-02)

### Features

* force version ([cb63bff](cb63bff))
* getUploadWidgetOptions, generateSignatureCallback, generateUploadWidgetResultCallback ([#165](#165)) ([4e3adc4](4e3adc4)), closes [#164](#164)
@colbyfayock
Copy link
Collaborator Author

I'm working on implementing these methods and thinking about the direction of generateUploadWidgetResultCallback

In my example, Next.js, I'm using the callback methods in 2 ways, 1 as a direct callback (onSuccess) and as a supplemental one as an Action (onSuccessAction) with a slightly different signature

here's an initial look at the implementation of this function along with the previous logic that split up these different callbacks

const resultsCallback = generateUploadWidgetResultCallback({
    onError: (uploadError) => {
      setError(uploadError);

      if ( typeof onError === 'function' ) {
        onError(uploadError, {
          widget: widget.current,
          ...instanceMethods
        });
      }
    },
    onResult: (uploadResult) => {
      if ( typeof uploadResult?.event !== 'string' ) return;

      setResults(uploadResult);

      const widgetEvent = WIDGET_EVENTS[uploadResult.event] as keyof typeof props;

      if ( typeof widgetEvent === 'string' && typeof props[widgetEvent] === 'function' ) {
        const callback = props[widgetEvent] as CldUploadEventCallback;
        callback(uploadResult, {
          widget: widget.current,
          ...instanceMethods
        });
      }

      const widgetEventAction = `${widgetEvent}Action` as keyof typeof props;

      if ( widgetEventAction && typeof props[widgetEventAction] === 'function' ) {
        const action = props[widgetEventAction] as CldUploadEventAction;
        action(uploadResult);
      }
    }
  });

this somewhat deters from the original simplicity goal where this requires still defining the callbacks and handling them all within a single onResult callback as opposed to just dumping the callbacks into the widget, but it feels like this may be unavoidable in this instance and any code to try to supply both callbacks is ultimately going to result in a bunch of code on the Next.js side

the other point is each of the callbacks in the generateUploadWidgetResultCallback are only Typed to pass the the results of the event, not the results and the instance methods as is currently shown here. this is also somewhat unavoidable because this is intentionally detatched from instance creation so it never knows about it, leaving some of this code additionally unavoidable

does this method even help? if only a little bit? it may provide a slightly cleaner interface and centralize some, but not all of the props

would love your thoughts @Baroshem and @ghostdevv

@colbyfayock colbyfayock changed the title url-loader@5.5.0 url-loader@5.6.0 Aug 5, 2024
@ghostdevv
Copy link
Contributor

I think it's ok to have a onResult callback that is then up to us to handle because each framework has different ways of doing events that might be otherwise incompatible

@colbyfayock colbyfayock merged commit 26736af into main Aug 7, 2024
@colbyfayock colbyfayock deleted the beta branch August 7, 2024 19:09
github-actions bot pushed a commit that referenced this pull request Aug 7, 2024
@github-actions
Copy link

github-actions bot commented Aug 7, 2024

🎉 This PR is included in version @cloudinary-util/util-v3.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

github-actions bot pushed a commit that referenced this pull request Aug 7, 2024
# [@cloudinary-util/url-loader-v5.6.0](https://github.com/cloudinary-community/cloudinary-util/compare/@cloudinary-util/url-loader-v5.5.0...@cloudinary-util/url-loader-v5.6.0) (2024-08-07)

### Bug Fixes

* falsy check for replacebackground ([5574e39](5574e39))

### Features

* url-loader@5.6.0 ([#166](#166)) ([26736af](26736af)), closes [#164](#164)
@github-actions
Copy link

github-actions bot commented Aug 7, 2024

🎉 This PR is included in version @cloudinary-util/url-loader-v5.6.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

github-actions bot pushed a commit that referenced this pull request Aug 7, 2024
@github-actions
Copy link

github-actions bot commented Aug 7, 2024

🎉 This PR is included in version @cloudinary-util/types-v1.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

getUploadWidgetOptions

3 participants