Skip to content

Commit 9645d1b

Browse files
Merge pull request #131 from opencomponents/replace-fetch
[v2] Replace ajax calls with fetch
2 parents 156e6aa + 1f8a40e commit 9645d1b

File tree

4 files changed

+357
-170
lines changed

4 files changed

+357
-170
lines changed

src/oc-client.js

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@ export function createOc(oc) {
3232
dataRenderedAttribute = "data-rendered",
3333
dataRenderingAttribute = "data-rendering",
3434
logError = (msg) => console.log(msg),
35-
logInfo = (msg) => ocConf.debug && console.log(msg);
35+
logInfo = (msg) => ocConf.debug && console.log(msg),
36+
handleFetchResponse = (response) => {
37+
if (!response.ok) throw response;
38+
if (response.headers.get("Content-Type") !== "x-text/stream")
39+
return response.json();
40+
41+
return oc._decode(response.body).then((decoded) => decoded.value);
42+
};
3643

3744
// constants
3845
const RETRY_INTERVAL =
@@ -210,10 +217,10 @@ export function createOc(oc) {
210217

211218
const getData = (options, cb) => {
212219
cb = cb || noop;
213-
const version = options.version;
214-
const baseUrl = options.baseUrl;
215-
const name = options.name;
216-
const json = options.json;
220+
const version = options.version,
221+
baseUrl = options.baseUrl,
222+
name = options.name,
223+
json = options.json;
217224
isRequired("version", version);
218225
isRequired("baseUrl", baseUrl);
219226
isRequired("name", name);
@@ -229,24 +236,29 @@ export function createOc(oc) {
229236
],
230237
};
231238
const headers = getHeaders();
232-
const ajaxOptions = {
233-
method: "POST",
234-
url: baseUrl,
235-
data: jsonRequest ? JSON.stringify(data) : data,
236-
headers: headers,
237-
crossDomain: true,
238-
success: (apiResponse) => {
239-
const response = apiResponse[0].response;
240-
const err = response.error ? response.details || response.error : null;
241-
cb(err, response.data, apiResponse[0]);
242-
},
243-
error: cb,
244-
};
239+
245240
if (jsonRequest) {
246241
headers["Content-Type"] = "application/json";
247242
}
248243

249-
$.ajax(ajaxOptions);
244+
fetch(baseUrl, {
245+
method: "POST",
246+
headers: headers,
247+
body: jsonRequest ? JSON.stringify(data) : $.param(data),
248+
})
249+
.then(handleFetchResponse)
250+
.then((apiResponse) => {
251+
if (!options.action) {
252+
const response = apiResponse[0].response;
253+
const err = response.error
254+
? response.details || response.error
255+
: null;
256+
cb(err, response.data, apiResponse[0]);
257+
} else {
258+
cb(null, apiResponse.data);
259+
}
260+
})
261+
.catch(cb);
250262
};
251263
oc.getData = getData;
252264
oc.getAction = (options) => {
@@ -474,15 +486,17 @@ export function createOc(oc) {
474486
if (!href) {
475487
callback(MESSAGES_ERRORS_RENDERING + MESSAGES_ERRORS_HREF_MISSING);
476488
} else {
477-
$.ajax({
478-
url: addParametersToHref(href, {
489+
fetch(
490+
addParametersToHref(href, {
479491
...ocConf.globalParameters,
480492
...(RETRY_SEND_NUMBER ? { __oc_Retry: retryNumber } : {}),
481493
}),
482-
headers: getHeaders(),
483-
contentType: "text/plain",
484-
crossDomain: true,
485-
success: (apiResponse) => {
494+
{
495+
headers: getHeaders(),
496+
},
497+
)
498+
.then(handleFetchResponse)
499+
.then((apiResponse) => {
486500
const template = apiResponse.template;
487501
apiResponse.data.id = ocId;
488502
apiResponse.data.element = element;
@@ -505,8 +519,8 @@ export function createOc(oc) {
505519
});
506520
}
507521
});
508-
},
509-
error: (err) => {
522+
})
523+
.catch((err) => {
510524
if (err && err.status == 429) {
511525
retries[href] = 0;
512526
}
@@ -528,8 +542,7 @@ export function createOc(oc) {
528542
callback(interpolate(MESSAGES_ERRORS_RETRY_FAILED, href));
529543
},
530544
);
531-
},
532-
});
545+
});
533546
}
534547
});
535548
};

0 commit comments

Comments
 (0)