Skip to content

Commit

Permalink
fix(NextcloudBookmarks): Use CapacitorHttp to avoid cors errors in ca…
Browse files Browse the repository at this point in the history
…pacitor 5

fixes #1486

Signed-off-by: Marcel Klehr <[email protected]>
  • Loading branch information
marcelklehr committed Dec 19, 2023
1 parent 2543758 commit 128410a
Showing 1 changed file with 65 additions and 4 deletions.
69 changes: 65 additions & 4 deletions src/lib/adapters/NextcloudBookmarks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Nextcloud ADAPTER
// All owncloud specifc stuff goes in here
import { Capacitor } from '@capacitor/core'
import { Capacitor, CapacitorHttp as Http } from '@capacitor/core'
import Adapter from '../interfaces/Adapter'
import HtmlSerializer from '../serializers/Html'
import Logger from '../Logger'
Expand Down Expand Up @@ -867,9 +867,6 @@ export default class NextcloudBookmarksAdapter implements Adapter, BulkImportRes
const url = this.normalizeServerURL(this.server.url) + relUrl
let res
let timedOut = false
const authString = Base64.encode(
this.server.username + ':' + this.server.password
)

if (type && type.includes('application/json')) {
body = JSON.stringify(body)
Expand All @@ -881,6 +878,14 @@ export default class NextcloudBookmarksAdapter implements Adapter, BulkImportRes
body = params.toString()
}

if (Capacitor.getPlatform() !== 'web') {
return this.sendRequestNative(verb, url, type, body, returnRawResponse)
}

const authString = Base64.encode(
this.server.username + ':' + this.server.password
)

try {
res = await this.fetchQueue.add(() =>
Promise.race([
Expand Down Expand Up @@ -964,4 +969,60 @@ export default class NextcloudBookmarksAdapter implements Adapter, BulkImportRes

return res.status === 200
}

private async sendRequestNative(verb: string, url: string, type: string, body: any, returnRawResponse: boolean) {
let res
let timedOut = false
const authString = Base64.encode(
this.server.username + ':' + this.server.password
)
try {
res = await this.fetchQueue.add(() =>
Promise.race([
Http.request({
url,
method: verb,
disableRedirects: !this.server.allowRedirects,
headers: {
...(type && type !== 'multipart/form-data' && { 'Content-type': type }),
Authorization: 'Basic ' + authString,
},
responseType: 'json',
...(body && !['get', 'head'].includes(verb.toLowerCase()) && { data: body }),
}),
new Promise((resolve, reject) =>
setTimeout(() => {
timedOut = true
reject(new RequestTimeoutError())
}, TIMEOUT)
),
])
)
} catch (e) {
if (timedOut) throw e
console.log(e)
throw new NetworkError()
}

if (res.status < 400 && res.status >= 300) {
throw new RedirectError()
}

if (returnRawResponse) {
return res
}

if (res.status === 401 || res.status === 403) {
throw new AuthenticationError()
}
if (res.status === 503 || res.status > 400) {
throw new HttpError(res.status, verb)
}
const json = res.data
if (json.status !== 'success') {
throw new Error('Nextcloud API error: \n' + JSON.stringify(json))
}

return json
}
}

0 comments on commit 128410a

Please sign in to comment.