-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add option to change path of stored file
- improve validations - improve example that it can upload multiple files per model - update readme
- Loading branch information
1 parent
90bbb2b
commit 29572e5
Showing
11 changed files
with
270 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { BaseRecord, UploadedFile } from 'admin-bro' | ||
import { expect } from 'chai' | ||
import sinon, { createStubInstance } from 'sinon' | ||
|
||
import { buildRemotePath } from './build-remote-path' | ||
|
||
describe('buildPath', () => { | ||
let recordStub: BaseRecord | ||
const recordId = '1' | ||
const File: UploadedFile = { | ||
name: 'some-name.pdf', | ||
path: '/some-path.pdf', | ||
size: 111, | ||
type: 'txt', | ||
} | ||
|
||
after(() => { | ||
sinon.restore() | ||
}) | ||
|
||
before(() => { | ||
recordStub = createStubInstance(BaseRecord, { | ||
id: sinon.stub<any, string>().returns(recordId), | ||
isValid: sinon.stub<any, boolean>().returns(true), | ||
update: sinon.stub<any, Promise<BaseRecord>>().returnsThis(), | ||
}) | ||
recordStub.params = {} | ||
}) | ||
|
||
it('returns default path when no custom function is given', () => { | ||
expect(buildRemotePath(recordStub, File)).to.equal(`${recordId}/${File.name}`) | ||
}) | ||
|
||
it('returns default custom path when function is given', () => { | ||
const newPath = '1/1/filename' | ||
const fnStub = sinon.stub<[BaseRecord, string], string>().returns(newPath) | ||
|
||
const path = buildRemotePath(recordStub, File, fnStub) | ||
|
||
expect(path).to.equal(newPath) | ||
expect(fnStub).to.have.been.calledWith(recordStub, File.name) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { expect } from 'chai' | ||
import { ERROR_MESSAGES } from './constants' | ||
import { validateProperties } from './validate-properties' | ||
|
||
describe('validateProperties', () => { | ||
it('does not throw an all properties have different values', () => { | ||
expect(() => { | ||
validateProperties({ | ||
key: 'sameValue', | ||
file: 'otherValue', | ||
}) | ||
}).not.to.throw() | ||
}) | ||
|
||
it('throws an error when 2 properties have the same values', () => { | ||
expect(() => { | ||
validateProperties({ | ||
key: 'sameValue', | ||
file: 'sameValue', | ||
}) | ||
}).to.throw(ERROR_MESSAGES.DUPLICATED_KEYS([{ | ||
keys: ['key', 'file'], | ||
value: 'sameValue', | ||
}])) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { DuplicateOccurrence, ERROR_MESSAGES } from './constants' | ||
import { UploadOptions } from './upload-options.type' | ||
|
||
/** | ||
* Checks if values for properties given by the user are different | ||
* | ||
* @private | ||
*/ | ||
export const validateProperties = (properties: UploadOptions['properties']): void => { | ||
// counting how many occurrences of given value are in the keys. | ||
const mappedFields = Object.keys(properties).reduce((memo, key) => { | ||
const property = properties[key] ? { | ||
[properties[key]]: { | ||
keys: memo[properties[key]] ? [...memo[properties[key]].keys, key] : [key], | ||
value: properties[key], | ||
} } : {} | ||
return { | ||
...memo, | ||
...property, | ||
} | ||
}, {} as Record<string, DuplicateOccurrence>) | ||
|
||
const duplicated = Object.values(mappedFields).filter((value) => value.keys.length > 1) | ||
|
||
if (duplicated.length) { | ||
throw new Error(ERROR_MESSAGES.DUPLICATED_KEYS(duplicated)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.