Skip to content

Commit

Permalink
Extend options for loadDataFromUrl()
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Mar 5, 2021
1 parent 638750c commit 78b6e88
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

- Fixed `source` view supported syntaxes to list modes with no mime
- Fixed editor's hint popup positioning when widget is not fit into the page bounds
- Added `options` parameter for `loadDataFromUrl()` which is passing as is into `fetch()` as second argument
- Added `options` parameter for `loadDataFromUrl()` with following options:
- `fetch` – is passing as is into `fetch()` as second argument
- `isResponseOk` – a function to check response is ok
- `getContentSize` - a function to determine a size of decoded content
- `validateData` - a function to check payload is valid response data, must throw an error if data is invalid
- Changed `Widget#setContainer()` to append only if container is an instance of `Node`

## 1.0.0-beta.57 (04-03-2021)
Expand Down
28 changes: 22 additions & 6 deletions src/core/utils/load-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ async function loadDataFromStreamInternal(request, progress) {

try {
const startTime = Date.now();
const { stream, data: explicitData, size: payloadSize } = await stage('request', request);
const {
stream,
data: explicitData,
size: payloadSize,
validateData
} = await stage('request', request);
const requestTime = Date.now() - startTime;
const { data, size } = explicitData || await stage('receive', () =>
jsonFromStream(stream, Number(payloadSize) || 0, state => progress.asyncSet({
Expand All @@ -81,6 +86,10 @@ async function loadDataFromStreamInternal(request, progress) {
}))
);

if (typeof validateData === 'function') {
validateData(data);
}

await progress.asyncSet({ stage: 'done' });

return {
Expand Down Expand Up @@ -147,18 +156,25 @@ export function loadDataFromEvent(event) {
}

export function loadDataFromUrl(url, dataField, options) {
options = options || {};

const explicitData = typeof url === 'string' ? undefined : url;
const isResponseOk = options.isResponseOk || (response => response.ok);
const getContentSize = options.getContentSize || ((url, response) => {
return isSameOrigin(url) && !response.headers.get('content-encoding')
? response.headers.get('content-length')
: response.headers.get('x-file-size');
});

return loadDataFromStream(
async () => {
const response = await fetch(explicitData ? 'data:application/json,{}' : url, options);
const response = await fetch(explicitData ? 'data:application/json,{}' : url, options.fetch);

if (response.ok) {
if (isResponseOk(response)) {
return explicitData ? { data: explicitData } : {
stream: response.body,
size: isSameOrigin(url) && !response.headers.get('content-encoding')
? response.headers.get('content-length')
: response.headers.get('x-file-size')
size: getContentSize(url, response),
validateData: options.validateData
};
}

Expand Down

0 comments on commit 78b6e88

Please sign in to comment.