parent
30a60ebb28
commit
025ea4ff48
@ -1,141 +1,132 @@ |
||||
import { isArray, isObject, isUndefined, forEach } from 'lodash'; |
||||
import { ENTRYPOINT } from '../config/entrypoint'; |
||||
import SubmissionError from '../error/SubmissionError'; |
||||
import { normalize } from './hydra'; |
||||
|
||||
const MIME_TYPE = 'application/ld+json'; |
||||
|
||||
const makeParamArray = (key, arr) => |
||||
arr.map(val => `${key}[]=${val}`).join('&'); |
||||
|
||||
export default function(id, options = {}) { |
||||
console.log('fetch'); |
||||
console.log(options); |
||||
|
||||
if ("undefined" === typeof options.headers) { |
||||
options.headers = {}; |
||||
} |
||||
|
||||
if (!options.headers.hasOwnProperty("Accept")) { |
||||
options.headers = { ...options.headers, Accept: MIME_TYPE }; |
||||
} |
||||
|
||||
if ( |
||||
undefined !== options.body && |
||||
!(options.body instanceof FormData) && |
||||
!options.headers.hasOwnProperty("Content-Type") |
||||
) { |
||||
options.headers = { ...options.headers, "Content-Type": MIME_TYPE }; |
||||
} |
||||
|
||||
if (options.params) { |
||||
const params = normalize(options.params); |
||||
let queryString = Object.keys(params) |
||||
.map(key => |
||||
Array.isArray(params[key]) |
||||
? makeParamArray(key, params[key]) |
||||
: `${key}=${params[key]}` |
||||
) |
||||
.join('&'); |
||||
id = `${id}?${queryString}`; |
||||
console.log('URL', id); |
||||
} |
||||
|
||||
const entryPoint = ENTRYPOINT + (ENTRYPOINT.endsWith('/') ? '' : '/'); |
||||
|
||||
if ('PUT' === options.method) { |
||||
const payload = options.body && JSON.parse(options.body); |
||||
if (isObject(payload) && payload['@id']) { |
||||
options.body = JSON.stringify(normalize(payload)); |
||||
} |
||||
} |
||||
|
||||
/*const payload = options.body && JSON.parse(options.body); |
||||
import { isObject } from "lodash" |
||||
import { ENTRYPOINT } from "../config/entrypoint" |
||||
import SubmissionError from "../error/SubmissionError" |
||||
import { normalize } from "./hydra" |
||||
|
||||
const MIME_TYPE = "application/ld+json" |
||||
|
||||
const makeParamArray = (key, arr) => arr.map((val) => `${key}[]=${val}`).join("&") |
||||
|
||||
export default function (id, options = {}) { |
||||
console.log("fetch") |
||||
console.log(options) |
||||
|
||||
if ("undefined" === typeof options.headers) { |
||||
options.headers = {} |
||||
} |
||||
|
||||
if (!options.headers.hasOwnProperty("Accept")) { |
||||
options.headers = { ...options.headers, Accept: MIME_TYPE } |
||||
} |
||||
|
||||
if ( |
||||
undefined !== options.body && |
||||
!(options.body instanceof FormData) && |
||||
!options.headers.hasOwnProperty("Content-Type") |
||||
) { |
||||
options.headers = { ...options.headers, "Content-Type": MIME_TYPE } |
||||
} |
||||
|
||||
if (options.params) { |
||||
const params = normalize(options.params) |
||||
let queryString = Object.keys(params) |
||||
.map((key) => (Array.isArray(params[key]) ? makeParamArray(key, params[key]) : `${key}=${params[key]}`)) |
||||
.join("&") |
||||
id = `${id}?${queryString}` |
||||
console.log("URL", id) |
||||
} |
||||
|
||||
const entryPoint = ENTRYPOINT + (ENTRYPOINT.endsWith("/") ? "" : "/") |
||||
|
||||
if ("PUT" === options.method) { |
||||
const payload = options.body && JSON.parse(options.body) |
||||
if (isObject(payload) && payload["@id"]) { |
||||
options.body = JSON.stringify(normalize(payload)); |
||||
}*/ |
||||
|
||||
/*if (useAxios) { |
||||
console.log('axios'); |
||||
let url = new URL(id, entryPoint); |
||||
console.log(formData); |
||||
return axios({ |
||||
url: url.toString(), |
||||
method: 'POST', |
||||
//headers: options.headers,
|
||||
data: formData, |
||||
headers: { |
||||
'Content-Type': 'multipart/form-data' |
||||
}, |
||||
onUploadProgress: function (progressEvent) { |
||||
console.log('progress'); |
||||
//console.log(progressEvent);
|
||||
console.log(options.body); |
||||
|
||||
let uploadPercentage = parseInt(Math.round((progressEvent.loaded / progressEvent.total) * 100)); |
||||
options.body['__progress'] = uploadPercentage; |
||||
options.body['__progressLabel'] = uploadPercentage; |
||||
this.uploadPercentage = uploadPercentage; |
||||
console.log(options.body); |
||||
//options.body.set('__progressLabel', uploadPercentage);
|
||||
}.bind(this) |
||||
} |
||||
).then(response => { |
||||
options.body['__uploaded'] = 1; |
||||
options.body['uploadFile']['__uploaded'] = 1 |
||||
options.body = JSON.stringify(normalize(payload)) |
||||
} |
||||
} |
||||
|
||||
/*const payload = options.body && JSON.parse(options.body); |
||||
if (isObject(payload) && payload["@id"]) { |
||||
options.body = JSON.stringify(normalize(payload)); |
||||
}*/ |
||||
|
||||
/*if (useAxios) { |
||||
console.log('axios'); |
||||
let url = new URL(id, entryPoint); |
||||
console.log(formData); |
||||
return axios({ |
||||
url: url.toString(), |
||||
method: 'POST', |
||||
//headers: options.headers,
|
||||
data: formData, |
||||
headers: { |
||||
'Content-Type': 'multipart/form-data' |
||||
}, |
||||
onUploadProgress: function (progressEvent) { |
||||
console.log('progress'); |
||||
//console.log(progressEvent);
|
||||
console.log(options.body); |
||||
|
||||
let uploadPercentage = parseInt(Math.round((progressEvent.loaded / progressEvent.total) * 100)); |
||||
options.body['__progress'] = uploadPercentage; |
||||
options.body['__progressLabel'] = uploadPercentage; |
||||
this.uploadPercentage = uploadPercentage; |
||||
console.log(options.body); |
||||
//options.body.set('__progressLabel', uploadPercentage);
|
||||
}.bind(this) |
||||
} |
||||
).then(response => { |
||||
options.body['__uploaded'] = 1; |
||||
options.body['uploadFile']['__uploaded'] = 1 |
||||
|
||||
console.log(response); |
||||
console.log('SUCCESS!!'); |
||||
console.log(response); |
||||
console.log('SUCCESS!!'); |
||||
|
||||
return response.data; |
||||
}) |
||||
.catch(function (response) { |
||||
console.log(response); |
||||
console.log('FAILURE!!'); |
||||
}); |
||||
}*/ |
||||
return response.data; |
||||
}) |
||||
.catch(function (response) { |
||||
console.log(response); |
||||
console.log('FAILURE!!'); |
||||
}); |
||||
}*/ |
||||
|
||||
console.log('ready to fetch'); |
||||
console.log("ready to fetch") |
||||
|
||||
return global.fetch(new URL(id, entryPoint), options).then(response => { |
||||
console.log(response, 'global.fetch'); |
||||
return global.fetch(new URL(id, entryPoint), options).then((response) => { |
||||
console.log(response, "global.fetch") |
||||
|
||||
if (response.ok) { |
||||
return response; |
||||
return response |
||||
} |
||||
|
||||
return response.json().then(json => { |
||||
let error = |
||||
json['hydra:description'] || |
||||
json['hydra:title'] || |
||||
'An error occurred.'; |
||||
return response.json().then( |
||||
(json) => { |
||||
let error = json["hydra:description"] || json["hydra:title"] || "An error occurred." |
||||
|
||||
if (json['code'] && 401 === json['code']) { |
||||
error = 'Not allowed'; |
||||
if (json["code"] && 401 === json["code"]) { |
||||
error = "Not allowed" |
||||
} |
||||
|
||||
if (json['error']) { |
||||
error = json['error']; |
||||
if (json["error"]) { |
||||
error = json["error"] |
||||
} |
||||
|
||||
console.log(error, 'fetch error'); |
||||
console.log(error, "fetch error") |
||||
|
||||
if (!json.violations) { |
||||
console.log('violations'); |
||||
throw Error(error); |
||||
console.log("violations") |
||||
throw Error(error) |
||||
} |
||||
|
||||
let errors = { _error: error }; |
||||
json.violations.map( |
||||
violation => (errors[violation.propertyPath] = violation.message) |
||||
); |
||||
let errors = { _error: error } |
||||
json.violations.map((violation) => (errors[violation.propertyPath] = violation.message)) |
||||
|
||||
throw new SubmissionError(errors); |
||||
throw new SubmissionError(errors) |
||||
}, |
||||
() => { |
||||
console.log('error3'); |
||||
throw new Error(response.statusText || 'An error occurred.'); |
||||
} |
||||
); |
||||
}); |
||||
console.log("error3") |
||||
throw new Error(response.statusText || "An error occurred.") |
||||
}, |
||||
) |
||||
}) |
||||
} |
||||
|
Loading…
Reference in new issue