feat: native interceptor can work with cookies

This commit is contained in:
Andrew Bastin
2023-10-24 02:43:23 +05:30
parent 654886b871
commit 071a6330e4
5 changed files with 55 additions and 24 deletions

View File

@@ -1,11 +1,11 @@
/* eslint-disable */
/* prettier-ignore */
// @ts-nocheck
// Generated by unplugin-vue-components
// generated by unplugin-vue-components
// We suggest you to commit this file into source control
// Read more: https://github.com/vuejs/core/pull/3399
import '@vue/runtime-core'
export {}
declare module 'vue' {
declare module '@vue/runtime-core' {
export interface GlobalComponents {
AppActionHandler: typeof import('./components/app/ActionHandler.vue')['default']
AppAnnouncement: typeof import('./components/app/Announcement.vue')['default']
@@ -59,7 +59,6 @@ declare module 'vue' {
CollectionsSaveRequest: typeof import('./components/collections/SaveRequest.vue')['default']
CollectionsTeamCollections: typeof import('./components/collections/TeamCollections.vue')['default']
CookiesAllModal: typeof import('./components/cookies/AllModal.vue')['default']
CookiesCookieJarModal: typeof import('./components/cookies/CookieJarModal.vue')['default']
CookiesEditCookie: typeof import('./components/cookies/EditCookie.vue')['default']
Environments: typeof import('./components/environments/index.vue')['default']
EnvironmentsAdd: typeof import('./components/environments/Add.vue')['default']
@@ -219,4 +218,5 @@ declare module 'vue' {
WorkspaceCurrent: typeof import('./components/workspace/Current.vue')['default']
WorkspaceSelector: typeof import('./components/workspace/Selector.vue')['default']
}
}

View File

@@ -10,6 +10,7 @@
"tauri": "tauri"
},
"dependencies": {
"dioc": "workspace:^",
"@hoppscotch/common": "workspace:^",
"@platform/auth": "^0.1.106",
"@tauri-apps/api": "^1.3.0",

View File

@@ -5,9 +5,9 @@ import { def as collectionsDef } from "./platform/collections/collections.platfo
import { def as settingsDef } from "./platform/settings/settings.platform"
import { def as historyDef } from "./platform/history/history.platform"
import { def as tabStateDef } from "./platform/tabState/tabState.platform"
import { nativeInterceptor } from "./platform/interceptors/native"
import { proxyInterceptor } from "@hoppscotch/common/platform/std/interceptors/proxy"
import { ExtensionInspectorService } from "@hoppscotch/common/platform/std/inspections/extension.inspector"
import { NativeInterceptorService } from "./platform/interceptors/native"
import { nextTick, ref, watch } from "vue"
import { emit, listen } from "@tauri-apps/api/event"
import { type } from "@tauri-apps/api/os"
@@ -40,7 +40,7 @@ createHoppApp("#app", {
interceptors: {
default: "native",
interceptors: [
{ type: "standalone", interceptor: nativeInterceptor },
{ type: "service", service: NativeInterceptorService },
{ type: "standalone", interceptor: proxyInterceptor },
],
},

View File

@@ -4,9 +4,11 @@ import {
InterceptorError,
RequestRunResult,
} from "@hoppscotch/common/services/interceptor.service"
import { CookieJarService } from "@hoppscotch/common/services/cookie-jar.service"
import axios, { AxiosRequestConfig, CancelToken } from "axios"
import { cloneDeep } from "lodash-es"
import { Body, HttpVerb, ResponseType, getClient } from '@tauri-apps/api/http'
import { Body, HttpVerb, ResponseType, getClient } from "@tauri-apps/api/http"
import { Service } from "dioc"
export const preProcessRequest = (
req: AxiosRequestConfig
@@ -55,19 +57,19 @@ async function runRequest(
if (processedReq.data instanceof FormData) {
let body_data = {}
for (const entry of processedReq.data.entries()) {
const [name, value] = entry;
const [name, value] = entry
if (value instanceof File) {
let file_data = await value.arrayBuffer()
body_data[name] = {
file: new Uint8Array(file_data),
fileName: value.name
file: new Uint8Array(file_data),
fileName: value.name,
}
}
}
body = Body.form(body_data);
body = Body.form(body_data)
}
const res = await client.request({
@@ -75,15 +77,15 @@ async function runRequest(
url: processedReq.url ?? "",
responseType: ResponseType.Binary,
headers: processedReq.headers,
body: body
});
body: body,
})
if (cancelled()) {
client.drop()
return E.left("cancellation")
}
res.data = new Uint8Array(res.data as number[]).buffer;
res.data = new Uint8Array(res.data as number[]).buffer
const timeEnd = Date.now()
@@ -123,20 +125,45 @@ async function runRequest(
}
}
export const nativeInterceptor: Interceptor = {
interceptorID: "native",
name: () => "Native",
selectable: { type: "selectable" },
runRequest(req) {
export class NativeInterceptorService extends Service implements Interceptor {
public static readonly ID = "NATIVE_INTERCEPTOR_SERVICE"
public interceptorID = "native" // TODO: i18n this
public name = () => "Native"
public selectable = { type: "selectable" as const }
public supportsCookies = true
public cookieJarService = this.bind(CookieJarService)
constructor() {
super()
}
public runRequest(req: any) {
const processedReq = preProcessRequest(req)
const relevantCookies = this.cookieJarService.getCookiesForURL(
new URL(processedReq.url!)
)
processedReq.headers["Cookie"] = relevantCookies
.map((cookie) => `${cookie.name!}=${cookie.value!}`)
.join(";")
let cancelled = false
const checkCancelled = () => { return cancelled }
const checkCancelled = () => {
return cancelled
}
return {
cancel: () => { cancelled = true },
cancel: () => {
cancelled = true
},
response: runRequest(processedReq, checkCancelled),
}
},
}
}