Skip to content

Commit

Permalink
feat: pass output entry to the metadata callback
Browse files Browse the repository at this point in the history
  • Loading branch information
nd0ut committed Oct 26, 2023
1 parent 9e4707f commit 9153826
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
15 changes: 9 additions & 6 deletions abstract/UploaderBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,18 +607,22 @@ export class UploaderBlock extends ActivityBlock {
}
}

/** @private */
async getMetadata() {
/**
* @param {string} entryId
* @protected
*/
async getMetadataFor(entryId) {
const configValue = this.cfg.metadata ?? /** @type {import('../types').Metadata} */ (this.$['*uploadMetadata']);
if (typeof configValue === 'function') {
const metadata = await configValue();
const outputFileEntry = this.getOutputItem(entryId);
const metadata = await configValue(outputFileEntry);
return metadata;
}
return configValue;
}

/** @returns {Promise<import('@uploadcare/upload-client').FileFromOptions>} */
async getUploadClientOptions() {
/** @returns {import('@uploadcare/upload-client').FileFromOptions} */
getUploadClientOptions() {
let options = {
store: this.cfg.store,
publicKey: this.cfg.pubkey,
Expand All @@ -635,7 +639,6 @@ export class UploaderBlock extends ActivityBlock {
multipartMaxAttempts: this.cfg.multipartMaxAttempts,
checkForUrlDuplicates: !!this.cfg.checkForUrlDuplicates,
saveUrlForRecurrentUploads: !!this.cfg.saveUrlForRecurrentUploads,
metadata: await this.getMetadata(),
};

return options;
Expand Down
2 changes: 1 addition & 1 deletion blocks/DataOutput/DataOutput.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class DataOutput extends UploaderBlock {
};
return;
}
const uploadClientOptions = await this.getUploadClientOptions();
const uploadClientOptions = this.getUploadClientOptions();
const resp = await uploadFileGroup(uuidList, uploadClientOptions);
this.$.output = {
groupData: resp,
Expand Down
3 changes: 2 additions & 1 deletion blocks/FileItem/FileItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ export class FileItem extends UploaderBlock {
entry.setValue('abortController', abortController);

const uploadTask = async () => {
const uploadClientOptions = await this.getUploadClientOptions();
const uploadClientOptions = this.getUploadClientOptions();
return uploadFile(entry.getValue('file') || entry.getValue('externalUrl') || entry.getValue('uuid'), {
...uploadClientOptions,
fileName: entry.getValue('fileName'),
Expand All @@ -405,6 +405,7 @@ export class FileItem extends UploaderBlock {
this.$.progressUnknown = !progress.isComputable;
},
signal: abortController.signal,
metadata: await this.getMetadataFor(entry.uid),
});
};

Expand Down
2 changes: 1 addition & 1 deletion types/exported.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { UploadcareFile } from '@uploadcare/upload-client';

export type Metadata = import('@uploadcare/upload-client').Metadata;
export type MetadataCallback = () => Promise<Metadata>;
export type MetadataCallback = (fileEntry: OutputFileEntry) => Promise<Metadata> | Metadata;
export type ConfigType = {
pubkey: string;
multiple: boolean;
Expand Down

0 comments on commit 9153826

Please sign in to comment.